blob: dd865785943cdd36c507b0dec2293ff83597b842 [file] [log] [blame]
Asutosh Das0ef24812012-12-18 16:14:02 +05301/*
2 * drivers/mmc/host/sdhci-msm.c - Qualcomm Technologies, Inc. MSM SDHCI Platform
3 * driver source file
4 *
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05305 * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
Asutosh Das0ef24812012-12-18 16:14:02 +05306 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 and
9 * only version 2 as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 */
17
18#include <linux/module.h>
19#include <linux/mmc/host.h>
20#include <linux/mmc/card.h>
21#include <linux/mmc/sdio_func.h>
22#include <linux/gfp.h>
23#include <linux/of.h>
24#include <linux/of_gpio.h>
25#include <linux/regulator/consumer.h>
26#include <linux/types.h>
27#include <linux/input.h>
28#include <linux/platform_device.h>
29#include <linux/wait.h>
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070030#include <linux/io.h>
31#include <linux/delay.h>
32#include <linux/scatterlist.h>
33#include <linux/slab.h>
34#include <linux/mmc/mmc.h>
Sahitya Tummala581df132013-03-12 14:57:46 +053035#include <linux/mmc/slot-gpio.h>
Sahitya Tummalaeaa21862013-03-20 19:34:59 +053036#include <linux/dma-mapping.h>
Sahitya Tummala66b0fe32013-04-25 11:50:56 +053037#include <linux/iopoll.h>
Pratibhasagar V9acf2642013-11-21 21:07:21 +053038#include <linux/pinctrl/consumer.h>
39#include <linux/iopoll.h>
Sahitya Tummala8a3e8182013-03-10 14:12:52 +053040#include <linux/msm-bus.h>
Asutosh Das0ef24812012-12-18 16:14:02 +053041
42#include "sdhci-pltfm.h"
43
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -070044#define SDHCI_VER_100 0x2B
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +053045
46#define CORE_VERSION_MAJOR_MASK 0xF0000000
47#define CORE_VERSION_MAJOR_SHIFT 28
48
Asutosh Das0ef24812012-12-18 16:14:02 +053049#define CORE_HC_MODE 0x78
50#define HC_MODE_EN 0x1
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070051#define FF_CLK_SW_RST_DIS (1 << 13)
Asutosh Das0ef24812012-12-18 16:14:02 +053052
Sahitya Tummala481fbb02013-08-06 15:22:28 +053053#define CORE_GENERICS 0x70
54#define SWITCHABLE_SIGNALLING_VOL (1 << 29)
55
Asutosh Das0ef24812012-12-18 16:14:02 +053056#define CORE_POWER 0x0
57#define CORE_SW_RST (1 << 7)
58
Sahitya Tummala67717bc2013-08-02 09:21:37 +053059#define CORE_MCI_VERSION 0x050
60#define CORE_TESTBUS_CONFIG 0x0CC
61#define CORE_TESTBUS_ENA (1 << 3)
62#define CORE_SDCC_DEBUG_REG 0x124
63
Asutosh Das0ef24812012-12-18 16:14:02 +053064#define CORE_PWRCTL_STATUS 0xDC
65#define CORE_PWRCTL_MASK 0xE0
66#define CORE_PWRCTL_CLEAR 0xE4
67#define CORE_PWRCTL_CTL 0xE8
68
69#define CORE_PWRCTL_BUS_OFF 0x01
70#define CORE_PWRCTL_BUS_ON (1 << 1)
71#define CORE_PWRCTL_IO_LOW (1 << 2)
72#define CORE_PWRCTL_IO_HIGH (1 << 3)
73
74#define CORE_PWRCTL_BUS_SUCCESS 0x01
75#define CORE_PWRCTL_BUS_FAIL (1 << 1)
76#define CORE_PWRCTL_IO_SUCCESS (1 << 2)
77#define CORE_PWRCTL_IO_FAIL (1 << 3)
78
79#define INT_MASK 0xF
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070080#define MAX_PHASES 16
81
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070082#define CORE_DLL_CONFIG 0x100
83#define CORE_CMD_DAT_TRACK_SEL (1 << 0)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070084#define CORE_DLL_EN (1 << 16)
85#define CORE_CDR_EN (1 << 17)
86#define CORE_CK_OUT_EN (1 << 18)
87#define CORE_CDR_EXT_EN (1 << 19)
88#define CORE_DLL_PDN (1 << 29)
89#define CORE_DLL_RST (1 << 30)
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070090
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070091#define CORE_DLL_STATUS 0x108
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -070092#define CORE_DLL_LOCK (1 << 7)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -070093
94#define CORE_VENDOR_SPEC 0x10C
95#define CORE_CLK_PWRSAVE (1 << 1)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -070096#define CORE_HC_MCLK_SEL_DFLT (2 << 8)
97#define CORE_HC_MCLK_SEL_HS400 (3 << 8)
98#define CORE_HC_MCLK_SEL_MASK (3 << 8)
Asutosh Dase5e9ca62013-07-30 19:08:36 +053099#define CORE_HC_AUTO_CMD21_EN (1 << 6)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700100#define CORE_IO_PAD_PWR_SWITCH (1 << 16)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700101#define CORE_HC_SELECT_IN_EN (1 << 18)
102#define CORE_HC_SELECT_IN_HS400 (6 << 19)
103#define CORE_HC_SELECT_IN_MASK (7 << 19)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700104
Krishna Konda7feab352013-09-17 23:55:40 -0700105#define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530106#define CORE_8_BIT_SUPPORT (1 << 18)
107#define CORE_3_3V_SUPPORT (1 << 24)
108#define CORE_3_0V_SUPPORT (1 << 25)
109#define CORE_1_8V_SUPPORT (1 << 26)
Krishna Konda7feab352013-09-17 23:55:40 -0700110#define CORE_SYS_BUS_SUPPORT_64_BIT 28
111
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530112#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
113#define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
114
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700115#define CORE_CSR_CDC_CTLR_CFG0 0x130
116#define CORE_SW_TRIG_FULL_CALIB (1 << 16)
117#define CORE_HW_AUTOCAL_ENA (1 << 17)
118
119#define CORE_CSR_CDC_CTLR_CFG1 0x134
120#define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
121#define CORE_TIMER_ENA (1 << 16)
122
123#define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
124#define CORE_CSR_CDC_REFCOUNT_CFG 0x140
125#define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
126#define CORE_CDC_OFFSET_CFG 0x14C
127#define CORE_CSR_CDC_DELAY_CFG 0x150
128#define CORE_CDC_SLAVE_DDA_CFG 0x160
129#define CORE_CSR_CDC_STATUS0 0x164
130#define CORE_CALIBRATION_DONE (1 << 0)
131
132#define CORE_CDC_ERROR_CODE_MASK 0x7000000
133
134#define CORE_CSR_CDC_GEN_CFG 0x178
135#define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
136#define CORE_CDC_SWITCH_RC_EN (1 << 1)
137
138#define CORE_DDR_200_CFG 0x184
139#define CORE_CDC_T4_DLY_SEL (1 << 0)
140#define CORE_START_CDC_TRAFFIC (1 << 6)
141
Sahitya Tummala67717bc2013-08-02 09:21:37 +0530142#define CORE_MCI_DATA_CNT 0x30
143#define CORE_MCI_STATUS 0x34
144#define CORE_MCI_FIFO_CNT 0x44
145
146#define CORE_TESTBUS_SEL2_BIT 4
147#define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT)
148
Asutosh Das648f9d12013-01-10 21:11:04 +0530149/* 8KB descriptors */
150#define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530151#define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
Asutosh Das648f9d12013-01-10 21:11:04 +0530152
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700153#define CORE_FREQ_100MHZ (100 * 1000 * 1000)
154
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700155#define INVALID_TUNING_PHASE -1
156
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +0530157#define CORE_VERSION_TARGET_MASK 0x000000FF
158
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700159static const u32 tuning_block_64[] = {
160 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
161 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
162 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
163 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
164};
165
166static const u32 tuning_block_128[] = {
167 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
168 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
169 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
170 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
171 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
172 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
173 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
174 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
175};
Asutosh Das0ef24812012-12-18 16:14:02 +0530176
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -0700177static int disable_slots;
178/* root can write, others read */
179module_param(disable_slots, int, S_IRUGO|S_IWUSR);
180
Asutosh Das0ef24812012-12-18 16:14:02 +0530181/* This structure keeps information per regulator */
182struct sdhci_msm_reg_data {
183 /* voltage regulator handle */
184 struct regulator *reg;
185 /* regulator name */
186 const char *name;
187 /* voltage level to be set */
188 u32 low_vol_level;
189 u32 high_vol_level;
190 /* Load values for low power and high power mode */
191 u32 lpm_uA;
192 u32 hpm_uA;
193
194 /* is this regulator enabled? */
195 bool is_enabled;
196 /* is this regulator needs to be always on? */
197 bool is_always_on;
198 /* is low power mode setting required for this regulator? */
199 bool lpm_sup;
200 bool set_voltage_sup;
201};
202
Asutosh Das598a4d42014-02-05 16:38:23 +0530203#define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */
Asutosh Das0ef24812012-12-18 16:14:02 +0530204/*
205 * This structure keeps information for all the
206 * regulators required for a SDCC slot.
207 */
208struct sdhci_msm_slot_reg_data {
209 /* keeps VDD/VCC regulator info */
210 struct sdhci_msm_reg_data *vdd_data;
211 /* keeps VDD IO regulator info */
212 struct sdhci_msm_reg_data *vdd_io_data;
213};
214
215struct sdhci_msm_gpio {
216 u32 no;
217 const char *name;
218 bool is_enabled;
219};
220
221struct sdhci_msm_gpio_data {
222 struct sdhci_msm_gpio *gpio;
223 u8 size;
224};
225
226struct sdhci_msm_pin_data {
227 /*
228 * = 1 if controller pins are using gpios
229 * = 0 if controller has dedicated MSM pads
230 */
Asutosh Das0ef24812012-12-18 16:14:02 +0530231 struct sdhci_msm_gpio_data *gpio_data;
232};
233
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530234struct sdhci_pinctrl_data {
235 struct pinctrl *pctrl;
236 struct pinctrl_state *pins_active;
237 struct pinctrl_state *pins_sleep;
238};
239
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530240struct sdhci_msm_bus_voting_data {
241 struct msm_bus_scale_pdata *bus_pdata;
242 unsigned int *bw_vecs;
243 unsigned int bw_vecs_size;
244};
245
Asutosh Das0ef24812012-12-18 16:14:02 +0530246struct sdhci_msm_pltfm_data {
247 /* Supported UHS-I Modes */
248 u32 caps;
249
250 /* More capabilities */
251 u32 caps2;
252
253 unsigned long mmc_bus_width;
Asutosh Das0ef24812012-12-18 16:14:02 +0530254 struct sdhci_msm_slot_reg_data *vreg_data;
255 bool nonremovable;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530256 bool pin_cfg_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +0530257 struct sdhci_msm_pin_data *pin_data;
Pratibhasagar V9acf2642013-11-21 21:07:21 +0530258 struct sdhci_pinctrl_data *pctrl_data;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +0530259 u32 cpu_dma_latency_us;
Sahitya Tummala581df132013-03-12 14:57:46 +0530260 int status_gpio; /* card detection GPIO that is configured as IRQ */
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530261 struct sdhci_msm_bus_voting_data *voting_data;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530262 u32 *sup_clk_table;
263 unsigned char sup_clk_cnt;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530264};
265
266struct sdhci_msm_bus_vote {
267 uint32_t client_handle;
268 uint32_t curr_vote;
269 int min_bw_vote;
270 int max_bw_vote;
271 bool is_max_bw_needed;
272 struct delayed_work vote_work;
273 struct device_attribute max_bus_bw;
Asutosh Das0ef24812012-12-18 16:14:02 +0530274};
275
276struct sdhci_msm_host {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530277 struct platform_device *pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +0530278 void __iomem *core_mem; /* MSM SDCC mapped address */
279 struct clk *clk; /* main SD/MMC bus clock */
280 struct clk *pclk; /* SDHC peripheral bus clock */
281 struct clk *bus_clk; /* SDHC bus voter clock */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700282 struct clk *ff_clk; /* CDC calibration fixed feedback clock */
283 struct clk *sleep_clk; /* CDC calibration sleep clock */
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +0530284 atomic_t clks_on; /* Set if clocks are enabled */
Asutosh Das0ef24812012-12-18 16:14:02 +0530285 struct sdhci_msm_pltfm_data *pdata;
286 struct mmc_host *mmc;
287 struct sdhci_pltfm_data sdhci_msm_pdata;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +0530288 u32 curr_pwr_state;
289 u32 curr_io_level;
290 struct completion pwr_irq_completion;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +0530291 struct sdhci_msm_bus_vote msm_bus_vote;
Sahitya Tummala5c55b932013-06-20 14:00:18 +0530292 struct device_attribute polling;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530293 u32 clk_rate; /* Keeps track of current clock rate that is set */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700294 bool tuning_done;
295 bool calibration_done;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700296 u8 saved_tuning_phase;
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530297 bool en_auto_cmd21;
298 struct device_attribute auto_cmd21_attr;
Asutosh Das9d7ee2f2013-11-08 12:33:47 +0530299 atomic_t controller_clock;
Asutosh Das0ef24812012-12-18 16:14:02 +0530300};
301
302enum vdd_io_level {
303 /* set vdd_io_data->low_vol_level */
304 VDD_IO_LOW,
305 /* set vdd_io_data->high_vol_level */
306 VDD_IO_HIGH,
307 /*
308 * set whatever there in voltage_level (third argument) of
309 * sdhci_msm_set_vdd_io_vol() function.
310 */
311 VDD_IO_SET_LEVEL,
312};
313
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700314/* MSM platform specific tuning */
315static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
316 u8 poll)
317{
318 int rc = 0;
319 u32 wait_cnt = 50;
320 u8 ck_out_en = 0;
321 struct mmc_host *mmc = host->mmc;
322
323 /* poll for CK_OUT_EN bit. max. poll time = 50us */
324 ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
325 CORE_CK_OUT_EN);
326
327 while (ck_out_en != poll) {
328 if (--wait_cnt == 0) {
329 pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
330 mmc_hostname(mmc), __func__, poll);
331 rc = -ETIMEDOUT;
332 goto out;
333 }
334 udelay(1);
335
336 ck_out_en = !!(readl_relaxed(host->ioaddr +
337 CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
338 }
339out:
340 return rc;
341}
342
Asutosh Dase5e9ca62013-07-30 19:08:36 +0530343/*
344 * Enable CDR to track changes of DAT lines and adjust sampling
345 * point according to voltage/temperature variations
346 */
347static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host)
348{
349 int rc = 0;
350 u32 config;
351
352 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
353 config |= CORE_CDR_EN;
354 config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN);
355 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
356
357 rc = msm_dll_poll_ck_out_en(host, 0);
358 if (rc)
359 goto err;
360
361 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
362 CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
363
364 rc = msm_dll_poll_ck_out_en(host, 1);
365 if (rc)
366 goto err;
367 goto out;
368err:
369 pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__);
370out:
371 return rc;
372}
373
374static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute
375 *attr, const char *buf, size_t count)
376{
377 struct sdhci_host *host = dev_get_drvdata(dev);
378 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
379 struct sdhci_msm_host *msm_host = pltfm_host->priv;
380 u32 tmp;
381 unsigned long flags;
382
383 if (!kstrtou32(buf, 0, &tmp)) {
384 spin_lock_irqsave(&host->lock, flags);
385 msm_host->en_auto_cmd21 = !!tmp;
386 spin_unlock_irqrestore(&host->lock, flags);
387 }
388 return count;
389}
390
391static ssize_t show_auto_cmd21(struct device *dev,
392 struct device_attribute *attr, char *buf)
393{
394 struct sdhci_host *host = dev_get_drvdata(dev);
395 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
396 struct sdhci_msm_host *msm_host = pltfm_host->priv;
397
398 return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21);
399}
400
401/* MSM auto-tuning handler */
402static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host,
403 bool enable,
404 u32 type)
405{
406 int rc = 0;
407 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
408 struct sdhci_msm_host *msm_host = pltfm_host->priv;
409 u32 val = 0;
410
411 if (!msm_host->en_auto_cmd21)
412 return 0;
413
414 if (type == MMC_SEND_TUNING_BLOCK_HS200)
415 val = CORE_HC_AUTO_CMD21_EN;
416 else
417 return 0;
418
419 if (enable) {
420 rc = msm_enable_cdr_cm_sdc4_dll(host);
421 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
422 val, host->ioaddr + CORE_VENDOR_SPEC);
423 } else {
424 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
425 ~val, host->ioaddr + CORE_VENDOR_SPEC);
426 }
427 return rc;
428}
429
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700430static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
431{
432 int rc = 0;
433 u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
434 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
435 0x8};
436 unsigned long flags;
437 u32 config;
438 struct mmc_host *mmc = host->mmc;
439
440 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
441 spin_lock_irqsave(&host->lock, flags);
442
443 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
444 config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
445 config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
446 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
447
448 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
449 rc = msm_dll_poll_ck_out_en(host, 0);
450 if (rc)
451 goto err_out;
452
453 /*
454 * Write the selected DLL clock output phase (0 ... 15)
455 * to CDR_SELEXT bit field of DLL_CONFIG register.
456 */
457 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
458 & ~(0xF << 20))
459 | (grey_coded_phase_table[phase] << 20)),
460 host->ioaddr + CORE_DLL_CONFIG);
461
462 /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
463 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
464 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
465
466 /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
467 rc = msm_dll_poll_ck_out_en(host, 1);
468 if (rc)
469 goto err_out;
470
471 config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
472 config |= CORE_CDR_EN;
473 config &= ~CORE_CDR_EXT_EN;
474 writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
475 goto out;
476
477err_out:
478 pr_err("%s: %s: Failed to set DLL phase: %d\n",
479 mmc_hostname(mmc), __func__, phase);
480out:
481 spin_unlock_irqrestore(&host->lock, flags);
482 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
483 return rc;
484}
485
486/*
487 * Find out the greatest range of consecuitive selected
488 * DLL clock output phases that can be used as sampling
489 * setting for SD3.0 UHS-I card read operation (in SDR104
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700490 * timing mode) or for eMMC4.5 card read operation (in
491 * HS400/HS200 timing mode).
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700492 * Select the 3/4 of the range and configure the DLL with the
493 * selected DLL clock output phase.
494 */
495
496static int msm_find_most_appropriate_phase(struct sdhci_host *host,
497 u8 *phase_table, u8 total_phases)
498{
499 int ret;
500 u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
501 u8 phases_per_row[MAX_PHASES] = {0};
502 int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
503 int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
504 bool phase_0_found = false, phase_15_found = false;
505 struct mmc_host *mmc = host->mmc;
506
507 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
508 if (!total_phases || (total_phases > MAX_PHASES)) {
509 pr_err("%s: %s: invalid argument: total_phases=%d\n",
510 mmc_hostname(mmc), __func__, total_phases);
511 return -EINVAL;
512 }
513
514 for (cnt = 0; cnt < total_phases; cnt++) {
515 ranges[row_index][col_index] = phase_table[cnt];
516 phases_per_row[row_index] += 1;
517 col_index++;
518
519 if ((cnt + 1) == total_phases) {
520 continue;
521 /* check if next phase in phase_table is consecutive or not */
522 } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
523 row_index++;
524 col_index = 0;
525 }
526 }
527
528 if (row_index >= MAX_PHASES)
529 return -EINVAL;
530
531 /* Check if phase-0 is present in first valid window? */
532 if (!ranges[0][0]) {
533 phase_0_found = true;
534 phase_0_raw_index = 0;
535 /* Check if cycle exist between 2 valid windows */
536 for (cnt = 1; cnt <= row_index; cnt++) {
537 if (phases_per_row[cnt]) {
538 for (i = 0; i < phases_per_row[cnt]; i++) {
539 if (ranges[cnt][i] == 15) {
540 phase_15_found = true;
541 phase_15_raw_index = cnt;
542 break;
543 }
544 }
545 }
546 }
547 }
548
549 /* If 2 valid windows form cycle then merge them as single window */
550 if (phase_0_found && phase_15_found) {
551 /* number of phases in raw where phase 0 is present */
552 u8 phases_0 = phases_per_row[phase_0_raw_index];
553 /* number of phases in raw where phase 15 is present */
554 u8 phases_15 = phases_per_row[phase_15_raw_index];
555
556 if (phases_0 + phases_15 >= MAX_PHASES)
557 /*
558 * If there are more than 1 phase windows then total
559 * number of phases in both the windows should not be
560 * more than or equal to MAX_PHASES.
561 */
562 return -EINVAL;
563
564 /* Merge 2 cyclic windows */
565 i = phases_15;
566 for (cnt = 0; cnt < phases_0; cnt++) {
567 ranges[phase_15_raw_index][i] =
568 ranges[phase_0_raw_index][cnt];
569 if (++i >= MAX_PHASES)
570 break;
571 }
572
573 phases_per_row[phase_0_raw_index] = 0;
574 phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
575 }
576
577 for (cnt = 0; cnt <= row_index; cnt++) {
578 if (phases_per_row[cnt] > curr_max) {
579 curr_max = phases_per_row[cnt];
580 selected_row_index = cnt;
581 }
582 }
583
584 i = ((curr_max * 3) / 4);
585 if (i)
586 i--;
587
588 ret = (int)ranges[selected_row_index][i];
589
590 if (ret >= MAX_PHASES) {
591 ret = -EINVAL;
592 pr_err("%s: %s: invalid phase selected=%d\n",
593 mmc_hostname(mmc), __func__, ret);
594 }
595
596 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
597 return ret;
598}
599
600static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
601{
602 u32 mclk_freq = 0;
603
604 /* Program the MCLK value to MCLK_FREQ bit field */
605 if (host->clock <= 112000000)
606 mclk_freq = 0;
607 else if (host->clock <= 125000000)
608 mclk_freq = 1;
609 else if (host->clock <= 137000000)
610 mclk_freq = 2;
611 else if (host->clock <= 150000000)
612 mclk_freq = 3;
613 else if (host->clock <= 162000000)
614 mclk_freq = 4;
615 else if (host->clock <= 175000000)
616 mclk_freq = 5;
617 else if (host->clock <= 187000000)
618 mclk_freq = 6;
619 else if (host->clock <= 200000000)
620 mclk_freq = 7;
621
622 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
623 & ~(7 << 24)) | (mclk_freq << 24)),
624 host->ioaddr + CORE_DLL_CONFIG);
625}
626
627/* Initialize the DLL (Programmable Delay Line ) */
628static int msm_init_cm_dll(struct sdhci_host *host)
629{
630 struct mmc_host *mmc = host->mmc;
631 int rc = 0;
632 unsigned long flags;
633 u32 wait_cnt;
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530634 bool prev_pwrsave, curr_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700635
636 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
637 spin_lock_irqsave(&host->lock, flags);
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530638 prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
639 CORE_CLK_PWRSAVE);
640 curr_pwrsave = prev_pwrsave;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700641 /*
642 * Make sure that clock is always enabled when DLL
643 * tuning is in progress. Keeping PWRSAVE ON may
644 * turn off the clock. So let's disable the PWRSAVE
645 * here and re-enable it once tuning is completed.
646 */
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530647 if (prev_pwrsave) {
648 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
649 & ~CORE_CLK_PWRSAVE),
650 host->ioaddr + CORE_VENDOR_SPEC);
651 curr_pwrsave = false;
652 }
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700653
654 /* Write 1 to DLL_RST bit of DLL_CONFIG register */
655 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
656 | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
657
658 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
659 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
660 | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
661 msm_cm_dll_set_freq(host);
662
663 /* Write 0 to DLL_RST bit of DLL_CONFIG register */
664 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
665 & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
666
667 /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
668 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
669 & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
670
671 /* Set DLL_EN bit to 1. */
672 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
673 | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
674
675 /* Set CK_OUT_EN bit to 1. */
676 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
677 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
678
679 wait_cnt = 50;
680 /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
681 while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
682 CORE_DLL_LOCK)) {
683 /* max. wait for 50us sec for LOCK bit to be set */
684 if (--wait_cnt == 0) {
685 pr_err("%s: %s: DLL failed to LOCK\n",
686 mmc_hostname(mmc), __func__);
687 rc = -ETIMEDOUT;
688 goto out;
689 }
690 /* wait for 1us before polling again */
691 udelay(1);
692 }
693
694out:
Subhash Jadavani99bca3b2013-05-28 18:21:57 +0530695 /* Restore the correct PWRSAVE state */
696 if (prev_pwrsave ^ curr_pwrsave) {
697 u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
698
699 if (prev_pwrsave)
700 reg |= CORE_CLK_PWRSAVE;
701 else
702 reg &= ~CORE_CLK_PWRSAVE;
703
704 writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
705 }
706
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700707 spin_unlock_irqrestore(&host->lock, flags);
708 pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
709 return rc;
710}
711
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700712static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
713{
714 u32 wait_cnt;
715 int ret = 0;
716 int cdc_err = 0;
717 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
718 struct sdhci_msm_host *msm_host = pltfm_host->priv;
719
720 pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
721
722 /*
723 * Retuning in HS400 (DDR mode) will fail, just reset the
724 * tuning block and restore the saved tuning phase.
725 */
726 ret = msm_init_cm_dll(host);
727 if (ret)
728 goto out;
729
730 /* Set the selected phase in delay line hw block */
731 ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
732 if (ret)
733 goto out;
734
735 /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
736 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
737 | CORE_CMD_DAT_TRACK_SEL),
738 host->ioaddr + CORE_DLL_CONFIG);
739
740 /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
741 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
742 & ~CORE_CDC_T4_DLY_SEL),
743 host->ioaddr + CORE_DDR_200_CFG);
744
745 /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
746 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
747 & ~CORE_CDC_SWITCH_BYPASS_OFF),
748 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
749
750 /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
751 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
752 | CORE_CDC_SWITCH_RC_EN),
753 host->ioaddr + CORE_CSR_CDC_GEN_CFG);
754
755 /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
756 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
757 & ~CORE_START_CDC_TRAFFIC),
758 host->ioaddr + CORE_DDR_200_CFG);
759
760 /*
761 * Perform CDC Register Initialization Sequence
762 *
763 * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
764 * CORE_CSR_CDC_CTLR_CFG1 0x3011111
765 * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
766 * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
767 * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
768 * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
769 * CORE_CSR_CDC_DELAY_CFG 0x3AC
770 * CORE_CDC_OFFSET_CFG 0x0
771 * CORE_CDC_SLAVE_DDA_CFG 0x16334
772 */
773
774 writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
775 writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
776 writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
777 writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
778 writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
779 writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
780 writel_relaxed(0x3AC, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
781 writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
782 writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
783
784 /* CDC HW Calibration */
785
786 /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
787 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
788 | CORE_SW_TRIG_FULL_CALIB),
789 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
790
791 /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
792 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
793 & ~CORE_SW_TRIG_FULL_CALIB),
794 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
795
796 /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
797 writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
798 | CORE_HW_AUTOCAL_ENA),
799 host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
800
801 /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
802 writel_relaxed((readl_relaxed(host->ioaddr +
803 CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
804 host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
805
806 mb();
807
808 /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
809 wait_cnt = 50;
810 while (!(readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
811 & CORE_CALIBRATION_DONE)) {
812 /* max. wait for 50us sec for CALIBRATION_DONE bit to be set */
813 if (--wait_cnt == 0) {
814 pr_err("%s: %s: CDC Calibration was not completed\n",
815 mmc_hostname(host->mmc), __func__);
816 ret = -ETIMEDOUT;
817 goto out;
818 }
819 /* wait for 1us before polling again */
820 udelay(1);
821 }
822
823 /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
824 cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
825 & CORE_CDC_ERROR_CODE_MASK;
826 if (cdc_err) {
827 pr_err("%s: %s: CDC Error Code %d\n",
828 mmc_hostname(host->mmc), __func__, cdc_err);
829 ret = -EINVAL;
830 goto out;
831 }
832
833 /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
834 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
835 | CORE_START_CDC_TRAFFIC),
836 host->ioaddr + CORE_DDR_200_CFG);
837out:
838 pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
839 __func__, ret);
840 return ret;
841}
842
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700843int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
844{
845 unsigned long flags;
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530846 int tuning_seq_cnt = 3;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700847 u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0;
848 const u32 *tuning_block_pattern = tuning_block_64;
849 int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
850 int rc;
851 struct mmc_host *mmc = host->mmc;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530852 struct mmc_ios ios = host->mmc->ios;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700853 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
854 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530855
856 /*
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700857 * Tuning is required for SDR104, HS200 and HS400 cards and
858 * if clock frequency is greater than 100MHz in these modes.
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530859 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -0700860 if (host->clock <= CORE_FREQ_100MHZ ||
861 !((ios.timing == MMC_TIMING_MMC_HS400) ||
862 (ios.timing == MMC_TIMING_MMC_HS200) ||
863 (ios.timing == MMC_TIMING_UHS_SDR104)))
Sahitya Tummala22dd3362013-02-28 19:50:51 +0530864 return 0;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700865
866 pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700867
868 /* CDCLP533 HW calibration is only required for HS400 mode*/
869 if (msm_host->tuning_done && !msm_host->calibration_done &&
870 (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
871 rc = sdhci_msm_cdclp533_calibration(host);
872 spin_lock_irqsave(&host->lock, flags);
873 if (!rc)
874 msm_host->calibration_done = true;
875 spin_unlock_irqrestore(&host->lock, flags);
876 goto out;
877 }
878
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700879 spin_lock_irqsave(&host->lock, flags);
880
881 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) &&
882 (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
883 tuning_block_pattern = tuning_block_128;
884 size = sizeof(tuning_block_128);
885 }
886 spin_unlock_irqrestore(&host->lock, flags);
887
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700888 data_buf = kmalloc(size, GFP_KERNEL);
889 if (!data_buf) {
890 rc = -ENOMEM;
891 goto out;
892 }
893
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530894retry:
895 /* first of all reset the tuning block */
896 rc = msm_init_cm_dll(host);
897 if (rc)
898 goto kfree;
899
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700900 phase = 0;
901 do {
902 struct mmc_command cmd = {0};
903 struct mmc_data data = {0};
904 struct mmc_request mrq = {
905 .cmd = &cmd,
906 .data = &data
907 };
908 struct scatterlist sg;
909
910 /* set the phase in delay line hw block */
911 rc = msm_config_cm_dll_phase(host, phase);
912 if (rc)
913 goto kfree;
914
915 cmd.opcode = opcode;
916 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
917
918 data.blksz = size;
919 data.blocks = 1;
920 data.flags = MMC_DATA_READ;
921 data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
922
923 data.sg = &sg;
924 data.sg_len = 1;
925 sg_init_one(&sg, data_buf, size);
926 memset(data_buf, 0, size);
927 mmc_wait_for_req(mmc, &mrq);
928
929 if (!cmd.error && !data.error &&
930 !memcmp(data_buf, tuning_block_pattern, size)) {
931 /* tuning is successful at this tuning point */
932 tuned_phases[tuned_phase_cnt++] = phase;
933 pr_debug("%s: %s: found good phase = %d\n",
934 mmc_hostname(mmc), __func__, phase);
935 }
936 } while (++phase < 16);
937
938 if (tuned_phase_cnt) {
939 rc = msm_find_most_appropriate_phase(host, tuned_phases,
940 tuned_phase_cnt);
941 if (rc < 0)
942 goto kfree;
943 else
944 phase = (u8)rc;
945
946 /*
947 * Finally set the selected phase in delay
948 * line hw block.
949 */
950 rc = msm_config_cm_dll_phase(host, phase);
951 if (rc)
952 goto kfree;
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700953 msm_host->saved_tuning_phase = phase;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700954 pr_debug("%s: %s: finally setting the tuning phase to %d\n",
955 mmc_hostname(mmc), __func__, phase);
956 } else {
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530957 if (--tuning_seq_cnt)
958 goto retry;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700959 /* tuning failed */
960 pr_err("%s: %s: no tuning point found\n",
961 mmc_hostname(mmc), __func__);
Sahitya Tummala9fe16532013-06-13 10:36:57 +0530962 rc = -EIO;
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700963 }
964
965kfree:
966 kfree(data_buf);
967out:
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -0700968 spin_lock_irqsave(&host->lock, flags);
969 if (!rc)
970 msm_host->tuning_done = true;
971 spin_unlock_irqrestore(&host->lock, flags);
972 pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -0700973 return rc;
974}
975
Asutosh Das0ef24812012-12-18 16:14:02 +0530976static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
977{
978 struct sdhci_msm_gpio_data *curr;
979 int i, ret = 0;
980
981 curr = pdata->pin_data->gpio_data;
982 for (i = 0; i < curr->size; i++) {
983 if (!gpio_is_valid(curr->gpio[i].no)) {
984 ret = -EINVAL;
985 pr_err("%s: Invalid gpio = %d\n", __func__,
986 curr->gpio[i].no);
987 goto free_gpios;
988 }
989 if (enable) {
990 ret = gpio_request(curr->gpio[i].no,
991 curr->gpio[i].name);
992 if (ret) {
993 pr_err("%s: gpio_request(%d, %s) failed %d\n",
994 __func__, curr->gpio[i].no,
995 curr->gpio[i].name, ret);
996 goto free_gpios;
997 }
998 curr->gpio[i].is_enabled = true;
999 } else {
1000 gpio_free(curr->gpio[i].no);
1001 curr->gpio[i].is_enabled = false;
1002 }
1003 }
1004 return ret;
1005
1006free_gpios:
1007 for (i--; i >= 0; i--) {
1008 gpio_free(curr->gpio[i].no);
1009 curr->gpio[i].is_enabled = false;
1010 }
1011 return ret;
1012}
1013
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301014static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata,
1015 bool enable)
1016{
1017 int ret = 0;
1018
1019 if (enable)
1020 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1021 pdata->pctrl_data->pins_active);
1022 else
1023 ret = pinctrl_select_state(pdata->pctrl_data->pctrl,
1024 pdata->pctrl_data->pins_sleep);
1025
1026 if (ret < 0)
1027 pr_err("%s state for pinctrl failed with %d\n",
1028 enable ? "Enabling" : "Disabling", ret);
1029
1030 return ret;
1031}
1032
Asutosh Das0ef24812012-12-18 16:14:02 +05301033static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
1034{
1035 int ret = 0;
1036
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301037 if (pdata->pin_cfg_sts == enable) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301038 return 0;
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301039 } else if (pdata->pctrl_data) {
1040 ret = sdhci_msm_setup_pinctrl(pdata, enable);
1041 goto out;
1042 } else if (!pdata->pin_data) {
1043 return 0;
1044 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301045
1046 ret = sdhci_msm_setup_gpio(pdata, enable);
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301047
1048out:
Asutosh Das0ef24812012-12-18 16:14:02 +05301049 if (!ret)
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301050 pdata->pin_cfg_sts = enable;
Asutosh Das0ef24812012-12-18 16:14:02 +05301051
1052 return ret;
1053}
1054
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301055static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
1056 u32 **out, int *len, u32 size)
1057{
1058 int ret = 0;
1059 struct device_node *np = dev->of_node;
1060 size_t sz;
1061 u32 *arr = NULL;
1062
1063 if (!of_get_property(np, prop_name, len)) {
1064 ret = -EINVAL;
1065 goto out;
1066 }
1067 sz = *len = *len / sizeof(*arr);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001068 if (sz <= 0 || (size > 0 && (sz > size))) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301069 dev_err(dev, "%s invalid size\n", prop_name);
1070 ret = -EINVAL;
1071 goto out;
1072 }
1073
1074 arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
1075 if (!arr) {
1076 dev_err(dev, "%s failed allocating memory\n", prop_name);
1077 ret = -ENOMEM;
1078 goto out;
1079 }
1080
1081 ret = of_property_read_u32_array(np, prop_name, arr, sz);
1082 if (ret < 0) {
1083 dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
1084 goto out;
1085 }
1086 *out = arr;
1087out:
1088 if (ret)
1089 *len = 0;
1090 return ret;
1091}
1092
Asutosh Das0ef24812012-12-18 16:14:02 +05301093#define MAX_PROP_SIZE 32
1094static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
1095 struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
1096{
1097 int len, ret = 0;
1098 const __be32 *prop;
1099 char prop_name[MAX_PROP_SIZE];
1100 struct sdhci_msm_reg_data *vreg;
1101 struct device_node *np = dev->of_node;
1102
1103 snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
1104 if (!of_parse_phandle(np, prop_name, 0)) {
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301105 dev_info(dev, "No vreg data found for %s\n", vreg_name);
Asutosh Das0ef24812012-12-18 16:14:02 +05301106 return ret;
1107 }
1108
1109 vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
1110 if (!vreg) {
1111 dev_err(dev, "No memory for vreg: %s\n", vreg_name);
1112 ret = -ENOMEM;
1113 return ret;
1114 }
1115
1116 vreg->name = vreg_name;
1117
1118 snprintf(prop_name, MAX_PROP_SIZE,
1119 "qcom,%s-always-on", vreg_name);
1120 if (of_get_property(np, prop_name, NULL))
1121 vreg->is_always_on = true;
1122
1123 snprintf(prop_name, MAX_PROP_SIZE,
1124 "qcom,%s-lpm-sup", vreg_name);
1125 if (of_get_property(np, prop_name, NULL))
1126 vreg->lpm_sup = true;
1127
1128 snprintf(prop_name, MAX_PROP_SIZE,
1129 "qcom,%s-voltage-level", vreg_name);
1130 prop = of_get_property(np, prop_name, &len);
1131 if (!prop || (len != (2 * sizeof(__be32)))) {
1132 dev_warn(dev, "%s %s property\n",
1133 prop ? "invalid format" : "no", prop_name);
1134 } else {
1135 vreg->low_vol_level = be32_to_cpup(&prop[0]);
1136 vreg->high_vol_level = be32_to_cpup(&prop[1]);
1137 }
1138
1139 snprintf(prop_name, MAX_PROP_SIZE,
1140 "qcom,%s-current-level", vreg_name);
1141 prop = of_get_property(np, prop_name, &len);
1142 if (!prop || (len != (2 * sizeof(__be32)))) {
1143 dev_warn(dev, "%s %s property\n",
1144 prop ? "invalid format" : "no", prop_name);
1145 } else {
1146 vreg->lpm_uA = be32_to_cpup(&prop[0]);
1147 vreg->hpm_uA = be32_to_cpup(&prop[1]);
1148 }
1149
1150 *vreg_data = vreg;
1151 dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
1152 vreg->name, vreg->is_always_on ? "always_on," : "",
1153 vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
1154 vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
1155
1156 return ret;
1157}
1158
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301159static int sdhci_msm_parse_pinctrl_info(struct device *dev,
1160 struct sdhci_msm_pltfm_data *pdata)
1161{
1162 struct sdhci_pinctrl_data *pctrl_data;
1163 struct pinctrl *pctrl;
1164 int ret = 0;
1165
1166 /* Try to obtain pinctrl handle */
1167 pctrl = devm_pinctrl_get(dev);
1168 if (IS_ERR(pctrl)) {
1169 ret = PTR_ERR(pctrl);
1170 goto out;
1171 }
1172 pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL);
1173 if (!pctrl_data) {
1174 dev_err(dev, "No memory for sdhci_pinctrl_data\n");
1175 ret = -ENOMEM;
1176 goto out;
1177 }
1178 pctrl_data->pctrl = pctrl;
1179 /* Look-up and keep the states handy to be used later */
1180 pctrl_data->pins_active = pinctrl_lookup_state(
1181 pctrl_data->pctrl, "active");
1182 if (IS_ERR(pctrl_data->pins_active)) {
1183 ret = PTR_ERR(pctrl_data->pins_active);
1184 dev_err(dev, "Could not get active pinstates, err:%d\n", ret);
1185 goto out;
1186 }
1187 pctrl_data->pins_sleep = pinctrl_lookup_state(
1188 pctrl_data->pctrl, "sleep");
1189 if (IS_ERR(pctrl_data->pins_sleep)) {
1190 ret = PTR_ERR(pctrl_data->pins_sleep);
1191 dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret);
1192 goto out;
1193 }
1194 pdata->pctrl_data = pctrl_data;
1195out:
1196 return ret;
1197}
1198
Asutosh Das0ef24812012-12-18 16:14:02 +05301199#define GPIO_NAME_MAX_LEN 32
1200static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
1201 struct sdhci_msm_pltfm_data *pdata)
1202{
1203 int ret = 0, cnt, i;
1204 struct sdhci_msm_pin_data *pin_data;
1205 struct device_node *np = dev->of_node;
1206
Pratibhasagar V9acf2642013-11-21 21:07:21 +05301207 ret = sdhci_msm_parse_pinctrl_info(dev, pdata);
1208 if (!ret) {
1209 goto out;
1210 } else if (ret == -EPROBE_DEFER) {
1211 dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret);
1212 goto out;
1213 } else {
1214 dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n",
1215 ret);
1216 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301217 pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
1218 if (!pin_data) {
1219 dev_err(dev, "No memory for pin_data\n");
1220 ret = -ENOMEM;
1221 goto out;
1222 }
1223
1224 cnt = of_gpio_count(np);
1225 if (cnt > 0) {
1226 pin_data->gpio_data = devm_kzalloc(dev,
1227 sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
1228 if (!pin_data->gpio_data) {
1229 dev_err(dev, "No memory for gpio_data\n");
1230 ret = -ENOMEM;
1231 goto out;
1232 }
1233 pin_data->gpio_data->size = cnt;
1234 pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
1235 sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
1236
1237 if (!pin_data->gpio_data->gpio) {
1238 dev_err(dev, "No memory for gpio\n");
1239 ret = -ENOMEM;
1240 goto out;
1241 }
1242
1243 for (i = 0; i < cnt; i++) {
1244 const char *name = NULL;
1245 char result[GPIO_NAME_MAX_LEN];
1246 pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
1247 of_property_read_string_index(np,
1248 "qcom,gpio-names", i, &name);
1249
1250 snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
1251 dev_name(dev), name ? name : "?");
1252 pin_data->gpio_data->gpio[i].name = result;
1253 dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
1254 pin_data->gpio_data->gpio[i].name,
1255 pin_data->gpio_data->gpio[i].no);
1256 pdata->pin_data = pin_data;
1257 }
1258 }
1259
1260out:
1261 if (ret)
1262 dev_err(dev, "%s failed with err %d\n", __func__, ret);
1263 return ret;
1264}
1265
1266/* Parse platform data */
1267static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
1268{
1269 struct sdhci_msm_pltfm_data *pdata = NULL;
1270 struct device_node *np = dev->of_node;
1271 u32 bus_width = 0;
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301272 u32 cpu_dma_latency;
Asutosh Das0ef24812012-12-18 16:14:02 +05301273 int len, i;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301274 int clk_table_len;
1275 u32 *clk_table = NULL;
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301276 enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301277
1278 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1279 if (!pdata) {
1280 dev_err(dev, "failed to allocate memory for platform data\n");
1281 goto out;
1282 }
1283
Sujit Reddy Thumma1958d3d2013-06-03 09:54:32 +05301284 pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
1285 if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
1286 pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
Sahitya Tummala581df132013-03-12 14:57:46 +05301287
Asutosh Das0ef24812012-12-18 16:14:02 +05301288 of_property_read_u32(np, "qcom,bus-width", &bus_width);
1289 if (bus_width == 8)
1290 pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
1291 else if (bus_width == 4)
1292 pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
1293 else {
1294 dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
1295 pdata->mmc_bus_width = 0;
1296 }
1297
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05301298 if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
1299 &cpu_dma_latency))
1300 pdata->cpu_dma_latency_us = cpu_dma_latency;
Asutosh Das598a4d42014-02-05 16:38:23 +05301301 else
1302 pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05301303 if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
1304 &clk_table, &clk_table_len, 0)) {
1305 dev_err(dev, "failed parsing supported clock rates\n");
1306 goto out;
1307 }
1308 if (!clk_table || !clk_table_len) {
1309 dev_err(dev, "Invalid clock table\n");
1310 goto out;
1311 }
1312 pdata->sup_clk_table = clk_table;
1313 pdata->sup_clk_cnt = clk_table_len;
1314
Asutosh Das0ef24812012-12-18 16:14:02 +05301315 pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
1316 sdhci_msm_slot_reg_data),
1317 GFP_KERNEL);
1318 if (!pdata->vreg_data) {
1319 dev_err(dev, "failed to allocate memory for vreg data\n");
1320 goto out;
1321 }
1322
1323 if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
1324 "vdd")) {
1325 dev_err(dev, "failed parsing vdd data\n");
1326 goto out;
1327 }
1328 if (sdhci_msm_dt_parse_vreg_info(dev,
1329 &pdata->vreg_data->vdd_io_data,
1330 "vdd-io")) {
1331 dev_err(dev, "failed parsing vdd-io data\n");
1332 goto out;
1333 }
1334
1335 if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
1336 dev_err(dev, "failed parsing gpio data\n");
1337 goto out;
1338 }
1339
Asutosh Das0ef24812012-12-18 16:14:02 +05301340 len = of_property_count_strings(np, "qcom,bus-speed-mode");
1341
1342 for (i = 0; i < len; i++) {
1343 const char *name = NULL;
1344
1345 of_property_read_string_index(np,
1346 "qcom,bus-speed-mode", i, &name);
1347 if (!name)
1348 continue;
1349
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07001350 if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
1351 pdata->caps2 |= MMC_CAP2_HS400_1_8V;
1352 else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
1353 pdata->caps2 |= MMC_CAP2_HS400_1_2V;
1354 else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
Asutosh Das0ef24812012-12-18 16:14:02 +05301355 pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
1356 else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
1357 pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
1358 else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
1359 pdata->caps |= MMC_CAP_1_8V_DDR
1360 | MMC_CAP_UHS_DDR50;
1361 else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
1362 pdata->caps |= MMC_CAP_1_2V_DDR
1363 | MMC_CAP_UHS_DDR50;
1364 }
1365
1366 if (of_get_property(np, "qcom,nonremovable", NULL))
1367 pdata->nonremovable = true;
1368
1369 return pdata;
1370out:
1371 return NULL;
1372}
1373
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301374/* Returns required bandwidth in Bytes per Sec */
1375static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
1376 struct mmc_ios *ios)
1377{
Sahitya Tummala2886c922013-04-03 18:03:31 +05301378 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1379 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1380
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301381 unsigned int bw;
1382
Sahitya Tummala2886c922013-04-03 18:03:31 +05301383 bw = msm_host->clk_rate;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301384 /*
1385 * For DDR mode, SDCC controller clock will be at
1386 * the double rate than the actual clock that goes to card.
1387 */
1388 if (ios->bus_width == MMC_BUS_WIDTH_4)
1389 bw /= 2;
1390 else if (ios->bus_width == MMC_BUS_WIDTH_1)
1391 bw /= 8;
1392
1393 return bw;
1394}
1395
1396static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
1397 unsigned int bw)
1398{
1399 unsigned int *table = host->pdata->voting_data->bw_vecs;
1400 unsigned int size = host->pdata->voting_data->bw_vecs_size;
1401 int i;
1402
1403 if (host->msm_bus_vote.is_max_bw_needed && bw)
1404 return host->msm_bus_vote.max_bw_vote;
1405
1406 for (i = 0; i < size; i++) {
1407 if (bw <= table[i])
1408 break;
1409 }
1410
1411 if (i && (i == size))
1412 i--;
1413
1414 return i;
1415}
1416
1417/*
1418 * This function must be called with host lock acquired.
1419 * Caller of this function should also ensure that msm bus client
1420 * handle is not null.
1421 */
1422static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
1423 int vote,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301424 unsigned long *flags)
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301425{
1426 struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
1427 int rc = 0;
1428
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301429 BUG_ON(!flags);
1430
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301431 if (vote != msm_host->msm_bus_vote.curr_vote) {
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301432 spin_unlock_irqrestore(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301433 rc = msm_bus_scale_client_update_request(
1434 msm_host->msm_bus_vote.client_handle, vote);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301435 spin_lock_irqsave(&host->lock, *flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301436 if (rc) {
1437 pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
1438 mmc_hostname(host->mmc),
1439 msm_host->msm_bus_vote.client_handle, vote, rc);
1440 goto out;
1441 }
1442 msm_host->msm_bus_vote.curr_vote = vote;
1443 }
1444out:
1445 return rc;
1446}
1447
1448/*
1449 * Internal work. Work to set 0 bandwidth for msm bus.
1450 */
1451static void sdhci_msm_bus_work(struct work_struct *work)
1452{
1453 struct sdhci_msm_host *msm_host;
1454 struct sdhci_host *host;
1455 unsigned long flags;
1456
1457 msm_host = container_of(work, struct sdhci_msm_host,
1458 msm_bus_vote.vote_work.work);
1459 host = platform_get_drvdata(msm_host->pdev);
1460
1461 if (!msm_host->msm_bus_vote.client_handle)
1462 return;
1463
1464 spin_lock_irqsave(&host->lock, flags);
1465 /* don't vote for 0 bandwidth if any request is in progress */
1466 if (!host->mrq) {
1467 sdhci_msm_bus_set_vote(msm_host,
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301468 msm_host->msm_bus_vote.min_bw_vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301469 } else
1470 pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
1471 mmc_hostname(host->mmc), __func__);
1472 spin_unlock_irqrestore(&host->lock, flags);
1473}
1474
1475/*
1476 * This function cancels any scheduled delayed work and sets the bus
1477 * vote based on bw (bandwidth) argument.
1478 */
1479static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
1480 unsigned int bw)
1481{
1482 int vote;
1483 unsigned long flags;
1484 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1485 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1486
1487 cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
1488 spin_lock_irqsave(&host->lock, flags);
1489 vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
Sujit Reddy Thumma024c0582013-08-06 11:21:33 +05301490 sdhci_msm_bus_set_vote(msm_host, vote, &flags);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301491 spin_unlock_irqrestore(&host->lock, flags);
1492}
1493
1494#define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
1495
1496/* This function queues a work which will set the bandwidth requiement to 0 */
1497static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
1498{
1499 unsigned long flags;
1500 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1501 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1502
1503 spin_lock_irqsave(&host->lock, flags);
1504 if (msm_host->msm_bus_vote.min_bw_vote !=
1505 msm_host->msm_bus_vote.curr_vote)
1506 queue_delayed_work(system_wq,
1507 &msm_host->msm_bus_vote.vote_work,
1508 msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
1509 spin_unlock_irqrestore(&host->lock, flags);
1510}
1511
1512static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
1513 struct platform_device *pdev)
1514{
1515 int rc = 0;
1516 struct msm_bus_scale_pdata *bus_pdata;
1517
1518 struct sdhci_msm_bus_voting_data *data;
1519 struct device *dev = &pdev->dev;
1520
1521 data = devm_kzalloc(dev,
1522 sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
1523 if (!data) {
1524 dev_err(&pdev->dev,
1525 "%s: failed to allocate memory\n", __func__);
1526 rc = -ENOMEM;
1527 goto out;
1528 }
1529 data->bus_pdata = msm_bus_cl_get_pdata(pdev);
1530 if (data->bus_pdata) {
1531 rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
1532 &data->bw_vecs, &data->bw_vecs_size, 0);
1533 if (rc) {
1534 dev_err(&pdev->dev,
1535 "%s: Failed to get bus-bw-vectors-bps\n",
1536 __func__);
1537 goto out;
1538 }
1539 host->pdata->voting_data = data;
1540 }
1541 if (host->pdata->voting_data &&
1542 host->pdata->voting_data->bus_pdata &&
1543 host->pdata->voting_data->bw_vecs &&
1544 host->pdata->voting_data->bw_vecs_size) {
1545
1546 bus_pdata = host->pdata->voting_data->bus_pdata;
1547 host->msm_bus_vote.client_handle =
1548 msm_bus_scale_register_client(bus_pdata);
1549 if (!host->msm_bus_vote.client_handle) {
1550 dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
1551 rc = -EFAULT;
1552 goto out;
1553 }
1554 /* cache the vote index for minimum and maximum bandwidth */
1555 host->msm_bus_vote.min_bw_vote =
1556 sdhci_msm_bus_get_vote_for_bw(host, 0);
1557 host->msm_bus_vote.max_bw_vote =
1558 sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
1559 } else {
1560 devm_kfree(dev, data);
1561 }
1562
1563out:
1564 return rc;
1565}
1566
1567static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
1568{
1569 if (host->msm_bus_vote.client_handle)
1570 msm_bus_scale_unregister_client(
1571 host->msm_bus_vote.client_handle);
1572}
1573
1574static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
1575{
1576 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1577 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1578 struct mmc_ios *ios = &host->mmc->ios;
1579 unsigned int bw;
1580
1581 if (!msm_host->msm_bus_vote.client_handle)
1582 return;
1583
1584 bw = sdhci_get_bw_required(host, ios);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301585 if (enable) {
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301586 sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05301587 } else {
1588 /*
1589 * If clock gating is enabled, then remove the vote
1590 * immediately because clocks will be disabled only
1591 * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
1592 * additional delay is required to remove the bus vote.
1593 */
1594#ifdef CONFIG_MMC_CLKGATE
1595 if (host->mmc->clkgate_delay)
1596 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
1597 else
1598#endif
1599 sdhci_msm_bus_queue_work(host);
1600 }
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301601}
1602
Asutosh Das0ef24812012-12-18 16:14:02 +05301603/* Regulator utility functions */
1604static int sdhci_msm_vreg_init_reg(struct device *dev,
1605 struct sdhci_msm_reg_data *vreg)
1606{
1607 int ret = 0;
1608
1609 /* check if regulator is already initialized? */
1610 if (vreg->reg)
1611 goto out;
1612
1613 /* Get the regulator handle */
1614 vreg->reg = devm_regulator_get(dev, vreg->name);
1615 if (IS_ERR(vreg->reg)) {
1616 ret = PTR_ERR(vreg->reg);
1617 pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
1618 __func__, vreg->name, ret);
1619 goto out;
1620 }
1621
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301622 if (regulator_count_voltages(vreg->reg) > 0) {
1623 vreg->set_voltage_sup = true;
1624 /* sanity check */
1625 if (!vreg->high_vol_level || !vreg->hpm_uA) {
1626 pr_err("%s: %s invalid constraints specified\n",
1627 __func__, vreg->name);
1628 ret = -EINVAL;
1629 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301630 }
1631
1632out:
1633 return ret;
1634}
1635
1636static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
1637{
1638 if (vreg->reg)
1639 devm_regulator_put(vreg->reg);
1640}
1641
1642static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
1643 *vreg, int uA_load)
1644{
1645 int ret = 0;
1646
1647 /*
1648 * regulators that do not support regulator_set_voltage also
1649 * do not support regulator_set_optimum_mode
1650 */
1651 if (vreg->set_voltage_sup) {
1652 ret = regulator_set_load(vreg->reg, uA_load);
1653 if (ret < 0)
1654 pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n",
1655 __func__, vreg->name, uA_load, ret);
1656 else
1657 /*
1658 * regulator_set_load() can return non zero
1659 * value even for success case.
1660 */
1661 ret = 0;
1662 }
1663 return ret;
1664}
1665
1666static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
1667 int min_uV, int max_uV)
1668{
1669 int ret = 0;
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301670 if (vreg->set_voltage_sup) {
1671 ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
1672 if (ret) {
1673 pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
Asutosh Das0ef24812012-12-18 16:14:02 +05301674 __func__, vreg->name, min_uV, max_uV, ret);
1675 }
Asutosh Dasc58cc7a2013-06-28 15:03:44 +05301676 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301677
1678 return ret;
1679}
1680
1681static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
1682{
1683 int ret = 0;
1684
1685 /* Put regulator in HPM (high power mode) */
1686 ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
1687 if (ret < 0)
1688 return ret;
1689
1690 if (!vreg->is_enabled) {
1691 /* Set voltage level */
1692 ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
1693 vreg->high_vol_level);
1694 if (ret)
1695 return ret;
1696 }
1697 ret = regulator_enable(vreg->reg);
1698 if (ret) {
1699 pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
1700 __func__, vreg->name, ret);
1701 return ret;
1702 }
1703 vreg->is_enabled = true;
1704 return ret;
1705}
1706
1707static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
1708{
1709 int ret = 0;
1710
1711 /* Never disable regulator marked as always_on */
1712 if (vreg->is_enabled && !vreg->is_always_on) {
1713 ret = regulator_disable(vreg->reg);
1714 if (ret) {
1715 pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
1716 __func__, vreg->name, ret);
1717 goto out;
1718 }
1719 vreg->is_enabled = false;
1720
1721 ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
1722 if (ret < 0)
1723 goto out;
1724
1725 /* Set min. voltage level to 0 */
1726 ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
1727 if (ret)
1728 goto out;
1729 } else if (vreg->is_enabled && vreg->is_always_on) {
1730 if (vreg->lpm_sup) {
1731 /* Put always_on regulator in LPM (low power mode) */
1732 ret = sdhci_msm_vreg_set_optimum_mode(vreg,
1733 vreg->lpm_uA);
1734 if (ret < 0)
1735 goto out;
1736 }
1737 }
1738out:
1739 return ret;
1740}
1741
1742static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
1743 bool enable, bool is_init)
1744{
1745 int ret = 0, i;
1746 struct sdhci_msm_slot_reg_data *curr_slot;
1747 struct sdhci_msm_reg_data *vreg_table[2];
1748
1749 curr_slot = pdata->vreg_data;
1750 if (!curr_slot) {
1751 pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
1752 __func__);
1753 goto out;
1754 }
1755
1756 vreg_table[0] = curr_slot->vdd_data;
1757 vreg_table[1] = curr_slot->vdd_io_data;
1758
1759 for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
1760 if (vreg_table[i]) {
1761 if (enable)
1762 ret = sdhci_msm_vreg_enable(vreg_table[i]);
1763 else
1764 ret = sdhci_msm_vreg_disable(vreg_table[i]);
1765 if (ret)
1766 goto out;
1767 }
1768 }
1769out:
1770 return ret;
1771}
1772
1773/*
1774 * Reset vreg by ensuring it is off during probe. A call
1775 * to enable vreg is needed to balance disable vreg
1776 */
1777static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
1778{
1779 int ret;
1780
1781 ret = sdhci_msm_setup_vreg(pdata, 1, true);
1782 if (ret)
1783 return ret;
1784 ret = sdhci_msm_setup_vreg(pdata, 0, true);
1785 return ret;
1786}
1787
1788/* This init function should be called only once for each SDHC slot */
1789static int sdhci_msm_vreg_init(struct device *dev,
1790 struct sdhci_msm_pltfm_data *pdata,
1791 bool is_init)
1792{
1793 int ret = 0;
1794 struct sdhci_msm_slot_reg_data *curr_slot;
1795 struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
1796
1797 curr_slot = pdata->vreg_data;
1798 if (!curr_slot)
1799 goto out;
1800
1801 curr_vdd_reg = curr_slot->vdd_data;
1802 curr_vdd_io_reg = curr_slot->vdd_io_data;
1803
1804 if (!is_init)
1805 /* Deregister all regulators from regulator framework */
1806 goto vdd_io_reg_deinit;
1807
1808 /*
1809 * Get the regulator handle from voltage regulator framework
1810 * and then try to set the voltage level for the regulator
1811 */
1812 if (curr_vdd_reg) {
1813 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
1814 if (ret)
1815 goto out;
1816 }
1817 if (curr_vdd_io_reg) {
1818 ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
1819 if (ret)
1820 goto vdd_reg_deinit;
1821 }
1822 ret = sdhci_msm_vreg_reset(pdata);
1823 if (ret)
1824 dev_err(dev, "vreg reset failed (%d)\n", ret);
1825 goto out;
1826
1827vdd_io_reg_deinit:
1828 if (curr_vdd_io_reg)
1829 sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
1830vdd_reg_deinit:
1831 if (curr_vdd_reg)
1832 sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
1833out:
1834 return ret;
1835}
1836
1837
1838static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
1839 enum vdd_io_level level,
1840 unsigned int voltage_level)
1841{
1842 int ret = 0;
1843 int set_level;
1844 struct sdhci_msm_reg_data *vdd_io_reg;
1845
1846 if (!pdata->vreg_data)
1847 return ret;
1848
1849 vdd_io_reg = pdata->vreg_data->vdd_io_data;
1850 if (vdd_io_reg && vdd_io_reg->is_enabled) {
1851 switch (level) {
1852 case VDD_IO_LOW:
1853 set_level = vdd_io_reg->low_vol_level;
1854 break;
1855 case VDD_IO_HIGH:
1856 set_level = vdd_io_reg->high_vol_level;
1857 break;
1858 case VDD_IO_SET_LEVEL:
1859 set_level = voltage_level;
1860 break;
1861 default:
1862 pr_err("%s: invalid argument level = %d",
1863 __func__, level);
1864 ret = -EINVAL;
1865 return ret;
1866 }
1867 ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
1868 set_level);
1869 }
1870 return ret;
1871}
1872
1873static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
1874{
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001875 struct sdhci_host *host = (struct sdhci_host *)data;
1876 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1877 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Asutosh Das0ef24812012-12-18 16:14:02 +05301878 u8 irq_status = 0;
1879 u8 irq_ack = 0;
1880 int ret = 0;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301881 int pwr_state = 0, io_level = 0;
1882 unsigned long flags;
Asutosh Das0ef24812012-12-18 16:14:02 +05301883
1884 irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
1885 pr_debug("%s: Received IRQ(%d), status=0x%x\n",
1886 mmc_hostname(msm_host->mmc), irq, irq_status);
1887
1888 /* Clear the interrupt */
1889 writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
1890 /*
1891 * SDHC has core_mem and hc_mem device memory and these memory
1892 * addresses do not fall within 1KB region. Hence, any update to
1893 * core_mem address space would require an mb() to ensure this gets
1894 * completed before its next update to registers within hc_mem.
1895 */
1896 mb();
1897
1898 /* Handle BUS ON/OFF*/
1899 if (irq_status & CORE_PWRCTL_BUS_ON) {
1900 ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301901 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301902 ret = sdhci_msm_setup_pins(msm_host->pdata, true);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301903 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1904 VDD_IO_HIGH, 0);
1905 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301906 if (ret)
1907 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1908 else
1909 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301910
1911 pwr_state = REQ_BUS_ON;
1912 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301913 }
1914 if (irq_status & CORE_PWRCTL_BUS_OFF) {
1915 ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301916 if (!ret) {
Asutosh Das0ef24812012-12-18 16:14:02 +05301917 ret = sdhci_msm_setup_pins(msm_host->pdata, false);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301918 ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
1919 VDD_IO_LOW, 0);
1920 }
Asutosh Das0ef24812012-12-18 16:14:02 +05301921 if (ret)
1922 irq_ack |= CORE_PWRCTL_BUS_FAIL;
1923 else
1924 irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301925
1926 pwr_state = REQ_BUS_OFF;
1927 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301928 }
1929 /* Handle IO LOW/HIGH */
1930 if (irq_status & CORE_PWRCTL_IO_LOW) {
1931 /* Switch voltage Low */
1932 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
1933 if (ret)
1934 irq_ack |= CORE_PWRCTL_IO_FAIL;
1935 else
1936 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301937
1938 io_level = REQ_IO_LOW;
Asutosh Das0ef24812012-12-18 16:14:02 +05301939 }
1940 if (irq_status & CORE_PWRCTL_IO_HIGH) {
1941 /* Switch voltage High */
1942 ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
1943 if (ret)
1944 irq_ack |= CORE_PWRCTL_IO_FAIL;
1945 else
1946 irq_ack |= CORE_PWRCTL_IO_SUCCESS;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301947
1948 io_level = REQ_IO_HIGH;
Asutosh Das0ef24812012-12-18 16:14:02 +05301949 }
1950
1951 /* ACK status to the core */
1952 writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
1953 /*
1954 * SDHC has core_mem and hc_mem device memory and these memory
1955 * addresses do not fall within 1KB region. Hence, any update to
1956 * core_mem address space would require an mb() to ensure this gets
1957 * completed before its next update to registers within hc_mem.
1958 */
1959 mb();
1960
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301961 if (io_level & REQ_IO_HIGH)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001962 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
1963 ~CORE_IO_PAD_PWR_SWITCH),
1964 host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301965 else if (io_level & REQ_IO_LOW)
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07001966 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
1967 CORE_IO_PAD_PWR_SWITCH),
1968 host->ioaddr + CORE_VENDOR_SPEC);
1969 mb();
1970
Asutosh Das0ef24812012-12-18 16:14:02 +05301971 pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
1972 mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05301973 spin_lock_irqsave(&host->lock, flags);
1974 if (pwr_state)
1975 msm_host->curr_pwr_state = pwr_state;
1976 if (io_level)
1977 msm_host->curr_io_level = io_level;
1978 complete(&msm_host->pwr_irq_completion);
1979 spin_unlock_irqrestore(&host->lock, flags);
1980
Asutosh Das0ef24812012-12-18 16:14:02 +05301981 return IRQ_HANDLED;
1982}
1983
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05301984static ssize_t
Sahitya Tummala5c55b932013-06-20 14:00:18 +05301985show_polling(struct device *dev, struct device_attribute *attr, char *buf)
1986{
1987 struct sdhci_host *host = dev_get_drvdata(dev);
1988 int poll;
1989 unsigned long flags;
1990
1991 spin_lock_irqsave(&host->lock, flags);
1992 poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
1993 spin_unlock_irqrestore(&host->lock, flags);
1994
1995 return snprintf(buf, PAGE_SIZE, "%d\n", poll);
1996}
1997
1998static ssize_t
1999store_polling(struct device *dev, struct device_attribute *attr,
2000 const char *buf, size_t count)
2001{
2002 struct sdhci_host *host = dev_get_drvdata(dev);
2003 int value;
2004 unsigned long flags;
2005
2006 if (!kstrtou32(buf, 0, &value)) {
2007 spin_lock_irqsave(&host->lock, flags);
2008 if (value) {
2009 host->mmc->caps |= MMC_CAP_NEEDS_POLL;
2010 mmc_detect_change(host->mmc, 0);
2011 } else {
2012 host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
2013 }
2014 spin_unlock_irqrestore(&host->lock, flags);
2015 }
2016 return count;
2017}
2018
2019static ssize_t
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302020show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2021 char *buf)
2022{
2023 struct sdhci_host *host = dev_get_drvdata(dev);
2024 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2025 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2026
2027 return snprintf(buf, PAGE_SIZE, "%u\n",
2028 msm_host->msm_bus_vote.is_max_bw_needed);
2029}
2030
2031static ssize_t
2032store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
2033 const char *buf, size_t count)
2034{
2035 struct sdhci_host *host = dev_get_drvdata(dev);
2036 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2037 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2038 uint32_t value;
2039 unsigned long flags;
2040
2041 if (!kstrtou32(buf, 0, &value)) {
2042 spin_lock_irqsave(&host->lock, flags);
2043 msm_host->msm_bus_vote.is_max_bw_needed = !!value;
2044 spin_unlock_irqrestore(&host->lock, flags);
2045 }
2046 return count;
2047}
2048
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302049static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
Asutosh Das0ef24812012-12-18 16:14:02 +05302050{
2051 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2052 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302053 unsigned long flags;
2054 bool done = false;
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302055 u32 io_sig_sts;
Asutosh Das0ef24812012-12-18 16:14:02 +05302056
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302057 spin_lock_irqsave(&host->lock, flags);
2058 pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
2059 mmc_hostname(host->mmc), __func__, req_type,
2060 msm_host->curr_pwr_state, msm_host->curr_io_level);
Sahitya Tummala481fbb02013-08-06 15:22:28 +05302061 io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS);
2062 /*
2063 * The IRQ for request type IO High/Low will be generated when -
2064 * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW.
2065 * 2. If 1 is true and when there is a state change in 1.8V enable
2066 * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of
2067 * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core
2068 * layer tries to set it to 3.3V before card detection happens, the
2069 * IRQ doesn't get triggered as there is no state change in this bit.
2070 * The driver already handles this case by changing the IO voltage
2071 * level to high as part of controller power up sequence. Hence, check
2072 * for host->pwr to handle a case where IO voltage high request is
2073 * issued even before controller power up.
2074 */
2075 if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) {
2076 if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) ||
2077 ((req_type & REQ_IO_HIGH) && !host->pwr)) {
2078 pr_debug("%s: do not wait for power IRQ that never comes\n",
2079 mmc_hostname(host->mmc));
2080 spin_unlock_irqrestore(&host->lock, flags);
2081 return;
2082 }
2083 }
2084
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302085 if ((req_type & msm_host->curr_pwr_state) ||
2086 (req_type & msm_host->curr_io_level))
2087 done = true;
2088 spin_unlock_irqrestore(&host->lock, flags);
Asutosh Das0ef24812012-12-18 16:14:02 +05302089
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302090 /*
2091 * This is needed here to hanlde a case where IRQ gets
2092 * triggered even before this function is called so that
2093 * x->done counter of completion gets reset. Otherwise,
2094 * next call to wait_for_completion returns immediately
2095 * without actually waiting for the IRQ to be handled.
2096 */
2097 if (done)
2098 init_completion(&msm_host->pwr_irq_completion);
2099 else
2100 wait_for_completion(&msm_host->pwr_irq_completion);
2101
2102 pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
2103 __func__, req_type);
Asutosh Das0ef24812012-12-18 16:14:02 +05302104}
2105
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002106static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
2107{
2108 if (enable)
2109 writel_relaxed((readl_relaxed(host->ioaddr +
2110 CORE_DLL_CONFIG) | CORE_CDR_EN),
2111 host->ioaddr + CORE_DLL_CONFIG);
2112 else
2113 writel_relaxed((readl_relaxed(host->ioaddr +
2114 CORE_DLL_CONFIG) & ~CORE_CDR_EN),
2115 host->ioaddr + CORE_DLL_CONFIG);
2116}
2117
Asutosh Das648f9d12013-01-10 21:11:04 +05302118static unsigned int sdhci_msm_max_segs(void)
2119{
2120 return SDHCI_MSM_MAX_SEGMENTS;
2121}
2122
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302123static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302124{
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302125 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2126 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302127
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302128 return msm_host->pdata->sup_clk_table[0];
2129}
2130
2131static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
2132{
2133 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2134 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2135 int max_clk_index = msm_host->pdata->sup_clk_cnt;
2136
2137 return msm_host->pdata->sup_clk_table[max_clk_index - 1];
2138}
2139
2140static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
2141 u32 req_clk)
2142{
2143 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2144 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2145 unsigned int sel_clk = -1;
2146 unsigned char cnt;
2147
2148 if (req_clk < sdhci_msm_get_min_clock(host)) {
2149 sel_clk = sdhci_msm_get_min_clock(host);
2150 return sel_clk;
2151 }
2152
2153 for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
2154 if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
2155 break;
2156 } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
2157 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2158 break;
2159 } else {
2160 sel_clk = msm_host->pdata->sup_clk_table[cnt];
2161 }
2162 }
2163 return sel_clk;
2164}
2165
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302166static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
2167{
2168 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2169 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2170 int rc = 0;
2171
2172 if (atomic_read(&msm_host->controller_clock))
2173 return 0;
2174
2175 sdhci_msm_bus_voting(host, 1);
2176
2177 if (!IS_ERR(msm_host->pclk)) {
2178 rc = clk_prepare_enable(msm_host->pclk);
2179 if (rc) {
2180 pr_err("%s: %s: failed to enable the pclk with error %d\n",
2181 mmc_hostname(host->mmc), __func__, rc);
2182 goto remove_vote;
2183 }
2184 }
2185
2186 rc = clk_prepare_enable(msm_host->clk);
2187 if (rc) {
2188 pr_err("%s: %s: failed to enable the host-clk with error %d\n",
2189 mmc_hostname(host->mmc), __func__, rc);
2190 goto disable_pclk;
2191 }
2192
2193 atomic_set(&msm_host->controller_clock, 1);
2194 pr_debug("%s: %s: enabled controller clock\n",
2195 mmc_hostname(host->mmc), __func__);
2196 goto out;
2197
2198disable_pclk:
2199 if (!IS_ERR(msm_host->pclk))
2200 clk_disable_unprepare(msm_host->pclk);
2201remove_vote:
2202 if (msm_host->msm_bus_vote.client_handle)
2203 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2204out:
2205 return rc;
2206}
2207
2208
2209
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302210static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
2211{
2212 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2213 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2214 int rc = 0;
2215
2216 if (enable && !atomic_read(&msm_host->clks_on)) {
2217 pr_debug("%s: request to enable clocks\n",
2218 mmc_hostname(host->mmc));
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302219
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302220 /*
2221 * The bus-width or the clock rate might have changed
2222 * after controller clocks are enbaled, update bus vote
2223 * in such case.
2224 */
2225 if (atomic_read(&msm_host->controller_clock))
2226 sdhci_msm_bus_voting(host, 1);
2227
2228 rc = sdhci_msm_enable_controller_clock(host);
2229 if (rc)
2230 goto remove_vote;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302231
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302232 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2233 rc = clk_prepare_enable(msm_host->bus_clk);
2234 if (rc) {
2235 pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
2236 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302237 goto disable_controller_clk;
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302238 }
2239 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002240 if (!IS_ERR(msm_host->ff_clk)) {
2241 rc = clk_prepare_enable(msm_host->ff_clk);
2242 if (rc) {
2243 pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
2244 mmc_hostname(host->mmc), __func__, rc);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302245 goto disable_bus_clk;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002246 }
2247 }
2248 if (!IS_ERR(msm_host->sleep_clk)) {
2249 rc = clk_prepare_enable(msm_host->sleep_clk);
2250 if (rc) {
2251 pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
2252 mmc_hostname(host->mmc), __func__, rc);
2253 goto disable_ff_clk;
2254 }
2255 }
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302256 mb();
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302257
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302258 } else if (!enable && atomic_read(&msm_host->clks_on)) {
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302259 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
2260 mb();
Sahitya Tummaladc182982013-08-20 15:32:09 +05302261 /*
2262 * During 1.8V signal switching the clock source must
2263 * still be ON as it requires accessing SDHC
2264 * registers (SDHCi host control2 register bit 3 must
2265 * be written and polled after stopping the SDCLK).
2266 */
2267 if (host->mmc->card_clock_off)
2268 return 0;
2269 pr_debug("%s: request to disable clocks\n",
2270 mmc_hostname(host->mmc));
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002271 if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
2272 clk_disable_unprepare(msm_host->sleep_clk);
2273 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2274 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302275 clk_disable_unprepare(msm_host->clk);
2276 if (!IS_ERR(msm_host->pclk))
2277 clk_disable_unprepare(msm_host->pclk);
2278 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2279 clk_disable_unprepare(msm_host->bus_clk);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302280
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302281 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302282 sdhci_msm_bus_voting(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302283 }
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302284 atomic_set(&msm_host->clks_on, enable);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302285 goto out;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002286disable_ff_clk:
2287 if (!IS_ERR_OR_NULL(msm_host->ff_clk))
2288 clk_disable_unprepare(msm_host->ff_clk);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302289disable_bus_clk:
2290 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2291 clk_disable_unprepare(msm_host->bus_clk);
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302292disable_controller_clk:
2293 if (!IS_ERR_OR_NULL(msm_host->clk))
2294 clk_disable_unprepare(msm_host->clk);
2295 if (!IS_ERR_OR_NULL(msm_host->pclk))
2296 clk_disable_unprepare(msm_host->pclk);
2297 atomic_set(&msm_host->controller_clock, 0);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302298remove_vote:
2299 if (msm_host->msm_bus_vote.client_handle)
2300 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302301out:
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302302 return rc;
2303}
2304
2305static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
2306{
2307 int rc;
2308 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2309 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2310 struct mmc_ios curr_ios = host->mmc->ios;
2311 u32 sup_clock, ddr_clock;
Sahitya Tummala043744a2013-06-24 09:55:33 +05302312 bool curr_pwrsave;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302313
2314 if (!clock) {
Sujit Reddy Thummabf1aecc2014-01-10 10:58:54 +05302315 /*
2316 * disable pwrsave to ensure clock is not auto-gated until
2317 * the rate is >400KHz (initialization complete).
2318 */
2319 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2320 ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302321 sdhci_msm_prepare_clocks(host, false);
2322 host->clock = clock;
2323 goto out;
2324 }
2325
2326 rc = sdhci_msm_prepare_clocks(host, true);
2327 if (rc)
2328 goto out;
2329
Sahitya Tummala043744a2013-06-24 09:55:33 +05302330 curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
2331 CORE_CLK_PWRSAVE);
Sahitya Tummalae000b242013-08-29 16:21:08 +05302332 if ((clock > 400000) &&
Sahitya Tummala043744a2013-06-24 09:55:33 +05302333 !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
2334 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2335 | CORE_CLK_PWRSAVE,
2336 host->ioaddr + CORE_VENDOR_SPEC);
2337 /*
2338 * Disable pwrsave for a newly added card if doesn't allow clock
2339 * gating.
2340 */
2341 else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
2342 writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2343 & ~CORE_CLK_PWRSAVE,
2344 host->ioaddr + CORE_VENDOR_SPEC);
2345
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302346 sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002347 if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
2348 (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302349 /*
2350 * The SDHC requires internal clock frequency to be double the
2351 * actual clock that will be set for DDR mode. The controller
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002352 * uses the faster clock(100/400MHz) for some of its parts and
2353 * send the actual required clock (50/200MHz) to the card.
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302354 */
2355 ddr_clock = clock * 2;
2356 sup_clock = sdhci_msm_get_sup_clk_rate(host,
2357 ddr_clock);
2358 }
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002359
2360 /*
2361 * In general all timing modes are controlled via UHS mode select in
2362 * Host Control2 register. eMMC specific HS200/HS400 doesn't have
2363 * their respective modes defined here, hence we use these values.
2364 *
2365 * HS200 - SDR104 (Since they both are equivalent in functionality)
2366 * HS400 - This involves multiple configurations
2367 * Initially SDR104 - when tuning is required as HS200
2368 * Then when switching to DDR @ 400MHz (HS400) we use
2369 * the vendor specific HC_SELECT_IN to control the mode.
2370 *
2371 * In addition to controlling the modes we also need to select the
2372 * correct input clock for DLL depending on the mode.
2373 *
2374 * HS400 - divided clock (free running MCLK/2)
2375 * All other modes - default (free running MCLK)
2376 */
2377 if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
2378 /* Select the divided clock (free running MCLK/2) */
2379 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2380 & ~CORE_HC_MCLK_SEL_MASK)
2381 | CORE_HC_MCLK_SEL_HS400),
2382 host->ioaddr + CORE_VENDOR_SPEC);
2383 /*
2384 * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
2385 * register
2386 */
2387 if (msm_host->tuning_done && !msm_host->calibration_done) {
2388 /*
2389 * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
2390 * field in VENDOR_SPEC_FUNC
2391 */
2392 writel_relaxed((readl_relaxed(host->ioaddr + \
2393 CORE_VENDOR_SPEC)
2394 | CORE_HC_SELECT_IN_HS400
2395 | CORE_HC_SELECT_IN_EN),
2396 host->ioaddr + CORE_VENDOR_SPEC);
2397 }
2398 } else {
2399 /* Select the default clock (free running MCLK) */
2400 writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2401 & ~CORE_HC_MCLK_SEL_MASK)
2402 | CORE_HC_MCLK_SEL_DFLT),
2403 host->ioaddr + CORE_VENDOR_SPEC);
2404
2405 /*
2406 * Disable HC_SELECT_IN to be able to use the UHS mode select
2407 * configuration from Host Control2 register for all other
2408 * modes.
2409 *
2410 * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
2411 * in VENDOR_SPEC_FUNC
2412 */
2413 writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
2414 & ~CORE_HC_SELECT_IN_EN
2415 & ~CORE_HC_SELECT_IN_MASK),
2416 host->ioaddr + CORE_VENDOR_SPEC);
2417 }
2418 mb();
2419
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302420 if (sup_clock != msm_host->clk_rate) {
2421 pr_debug("%s: %s: setting clk rate to %u\n",
2422 mmc_hostname(host->mmc), __func__, sup_clock);
2423 rc = clk_set_rate(msm_host->clk, sup_clock);
2424 if (rc) {
2425 pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
2426 mmc_hostname(host->mmc), __func__,
2427 sup_clock, rc);
2428 goto out;
2429 }
2430 msm_host->clk_rate = sup_clock;
2431 host->clock = clock;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302432 /*
2433 * Update the bus vote in case of frequency change due to
2434 * clock scaling.
2435 */
2436 sdhci_msm_bus_voting(host, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302437 }
2438out:
2439 sdhci_set_clock(host, clock);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302440}
2441
Sahitya Tummala14613432013-03-21 11:13:25 +05302442static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
2443 unsigned int uhs)
2444{
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002445 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2446 struct sdhci_msm_host *msm_host = pltfm_host->priv;
Sahitya Tummala14613432013-03-21 11:13:25 +05302447 u16 ctrl_2;
2448
2449 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2450 /* Select Bus Speed Mode for host */
2451 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002452 if (uhs == MMC_TIMING_MMC_HS400)
2453 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2454 else if (uhs == MMC_TIMING_MMC_HS200)
Sahitya Tummala14613432013-03-21 11:13:25 +05302455 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2456 else if (uhs == MMC_TIMING_UHS_SDR12)
2457 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
2458 else if (uhs == MMC_TIMING_UHS_SDR25)
2459 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
2460 else if (uhs == MMC_TIMING_UHS_SDR50)
2461 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
2462 else if (uhs == MMC_TIMING_UHS_SDR104)
2463 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
2464 else if (uhs == MMC_TIMING_UHS_DDR50)
2465 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302466 /*
2467 * When clock frquency is less than 100MHz, the feedback clock must be
2468 * provided and DLL must not be used so that tuning can be skipped. To
2469 * provide feedback clock, the mode selection can be any value less
2470 * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
2471 */
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002472 if (host->clock <= CORE_FREQ_100MHZ) {
2473 if ((uhs == MMC_TIMING_MMC_HS400) ||
2474 (uhs == MMC_TIMING_MMC_HS200) ||
2475 (uhs == MMC_TIMING_UHS_SDR104))
2476 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302477
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002478 /*
2479 * Make sure DLL is disabled when not required
2480 *
2481 * Write 1 to DLL_RST bit of DLL_CONFIG register
2482 */
2483 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2484 | CORE_DLL_RST),
2485 host->ioaddr + CORE_DLL_CONFIG);
2486
2487 /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
2488 writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
2489 | CORE_DLL_PDN),
2490 host->ioaddr + CORE_DLL_CONFIG);
2491 mb();
2492
2493 /*
2494 * The DLL needs to be restored and CDCLP533 recalibrated
2495 * when the clock frequency is set back to 400MHz.
2496 */
2497 msm_host->calibration_done = false;
2498 }
2499
2500 pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
2501 mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
Sahitya Tummala14613432013-03-21 11:13:25 +05302502 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
2503
2504}
2505
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302506#define MAX_TEST_BUS 20
2507
2508void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
2509{
2510 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2511 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2512 int tbsel, tbsel2;
2513 int i, index = 0;
2514 u32 test_bus_val = 0;
2515 u32 debug_reg[MAX_TEST_BUS] = {0};
2516
2517 pr_info("----------- VENDOR REGISTER DUMP -----------\n");
2518 pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
2519 readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
2520 readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
2521 readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
2522 pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n",
2523 readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
2524 readl_relaxed(host->ioaddr + CORE_DLL_STATUS),
2525 readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION));
2526 pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n",
2527 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC),
2528 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0),
2529 readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1));
2530
2531 /*
2532 * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits
2533 * of CORE_TESTBUS_CONFIG register.
2534 *
2535 * To select test bus 0 to 7 use tbsel and to select any test bus
2536 * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg,
2537 * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register
2538 * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110.
2539 */
2540 for (tbsel2 = 0; tbsel2 < 3; tbsel2++) {
2541 for (tbsel = 0; tbsel < 8; tbsel++) {
2542 if (index >= MAX_TEST_BUS)
2543 break;
2544 test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) |
2545 tbsel | CORE_TESTBUS_ENA;
2546 writel_relaxed(test_bus_val,
2547 msm_host->core_mem + CORE_TESTBUS_CONFIG);
2548 debug_reg[index++] = readl_relaxed(msm_host->core_mem +
2549 CORE_SDCC_DEBUG_REG);
2550 }
2551 }
2552 for (i = 0; i < MAX_TEST_BUS; i = i + 4)
2553 pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
2554 i, i + 3, debug_reg[i], debug_reg[i+1],
2555 debug_reg[i+2], debug_reg[i+3]);
2556 /* Disable test bus */
2557 writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
2558 CORE_TESTBUS_CONFIG);
2559}
2560
Asutosh Das0ef24812012-12-18 16:14:02 +05302561static struct sdhci_ops sdhci_msm_ops = {
Sahitya Tummala14613432013-03-21 11:13:25 +05302562 .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
Asutosh Das0ef24812012-12-18 16:14:02 +05302563 .check_power_status = sdhci_msm_check_power_status,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002564 .platform_execute_tuning = sdhci_msm_execute_tuning,
2565 .toggle_cdr = sdhci_msm_toggle_cdr,
Asutosh Das648f9d12013-01-10 21:11:04 +05302566 .get_max_segments = sdhci_msm_max_segs,
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302567 .set_clock = sdhci_msm_set_clock,
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302568 .get_min_clock = sdhci_msm_get_min_clock,
2569 .get_max_clock = sdhci_msm_get_max_clock,
Sahitya Tummala67717bc2013-08-02 09:21:37 +05302570 .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302571 .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd,
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302572 .enable_controller_clock = sdhci_msm_enable_controller_clock,
Asutosh Das0ef24812012-12-18 16:14:02 +05302573};
2574
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302575static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host,
2576 struct sdhci_host *host)
2577{
2578 u32 version, caps;
2579 u16 minor;
2580 u8 major;
2581
2582 version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
2583 major = (version & CORE_VERSION_MAJOR_MASK) >>
2584 CORE_VERSION_MAJOR_SHIFT;
2585 minor = version & CORE_VERSION_TARGET_MASK;
2586
2587 /*
2588 * Starting with SDCC 5 controller (core major version = 1)
Pratibhasagar Vada47992013-12-09 20:42:32 +05302589 * controller won't advertise 3.0v and 8-bit features except for
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302590 * some targets.
2591 */
2592 if (major >= 1 && minor != 0x11 && minor != 0x12) {
2593 caps = CORE_3_0V_SUPPORT;
Pratibhasagar Vada47992013-12-09 20:42:32 +05302594 if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA)
2595 caps |= CORE_8_BIT_SUPPORT;
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302596 writel_relaxed(
2597 (readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES) |
2598 caps), host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0);
2599 }
2600}
2601
Asutosh Das0ef24812012-12-18 16:14:02 +05302602static int sdhci_msm_probe(struct platform_device *pdev)
2603{
2604 struct sdhci_host *host;
2605 struct sdhci_pltfm_host *pltfm_host;
2606 struct sdhci_msm_host *msm_host;
2607 struct resource *core_memres = NULL;
2608 int ret = 0, pwr_irq = 0, dead = 0;
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002609 u16 host_version;
Subhash Jadavani28137342013-05-14 17:46:43 +05302610 u32 pwr, irq_status, irq_ctl;
Asutosh Das0ef24812012-12-18 16:14:02 +05302611
2612 pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
2613 msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
2614 GFP_KERNEL);
2615 if (!msm_host) {
2616 ret = -ENOMEM;
2617 goto out;
2618 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302619
2620 msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
2621 host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0);
2622 if (IS_ERR(host)) {
2623 ret = PTR_ERR(host);
2624 goto out;
2625 }
2626
2627 pltfm_host = sdhci_priv(host);
2628 pltfm_host->priv = msm_host;
2629 msm_host->mmc = host->mmc;
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302630 msm_host->pdev = pdev;
Asutosh Das0ef24812012-12-18 16:14:02 +05302631
2632 /* Extract platform data */
2633 if (pdev->dev.of_node) {
Venkat Gopalakrishnan270580a2013-03-11 12:17:57 -07002634 ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
2635 if (ret < 0) {
2636 dev_err(&pdev->dev, "Failed to get slot index %d\n",
2637 ret);
2638 goto pltfm_free;
2639 }
2640 if (disable_slots & (1 << (ret - 1))) {
2641 dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
2642 ret);
2643 ret = -ENODEV;
2644 goto pltfm_free;
2645 }
2646
Asutosh Das0ef24812012-12-18 16:14:02 +05302647 msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
2648 if (!msm_host->pdata) {
2649 dev_err(&pdev->dev, "DT parsing error\n");
2650 goto pltfm_free;
2651 }
2652 } else {
2653 dev_err(&pdev->dev, "No device tree node\n");
2654 goto pltfm_free;
2655 }
2656
2657 /* Setup Clocks */
2658
2659 /* Setup SDCC bus voter clock. */
2660 msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
2661 if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
2662 /* Vote for max. clk rate for max. performance */
2663 ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
2664 if (ret)
2665 goto pltfm_free;
2666 ret = clk_prepare_enable(msm_host->bus_clk);
2667 if (ret)
2668 goto pltfm_free;
2669 }
2670
2671 /* Setup main peripheral bus clock */
2672 msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
2673 if (!IS_ERR(msm_host->pclk)) {
2674 ret = clk_prepare_enable(msm_host->pclk);
2675 if (ret)
2676 goto bus_clk_disable;
2677 }
Asutosh Das9d7ee2f2013-11-08 12:33:47 +05302678 atomic_set(&msm_host->controller_clock, 1);
Asutosh Das0ef24812012-12-18 16:14:02 +05302679
2680 /* Setup SDC MMC clock */
2681 msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
2682 if (IS_ERR(msm_host->clk)) {
2683 ret = PTR_ERR(msm_host->clk);
2684 goto pclk_disable;
2685 }
2686
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302687 /* Set to the minimum supported clock frequency */
2688 ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
2689 if (ret) {
2690 dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302691 goto pclk_disable;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302692 }
Sahitya Tummala020ede0d2013-06-07 13:03:07 +05302693 ret = clk_prepare_enable(msm_host->clk);
2694 if (ret)
2695 goto pclk_disable;
2696
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302697 msm_host->clk_rate = sdhci_msm_get_min_clock(host);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302698 atomic_set(&msm_host->clks_on, 1);
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302699
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002700 /* Setup CDC calibration fixed feedback clock */
2701 msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
2702 if (!IS_ERR(msm_host->ff_clk)) {
2703 ret = clk_prepare_enable(msm_host->ff_clk);
2704 if (ret)
2705 goto clk_disable;
2706 }
2707
2708 /* Setup CDC calibration sleep clock */
2709 msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
2710 if (!IS_ERR(msm_host->sleep_clk)) {
2711 ret = clk_prepare_enable(msm_host->sleep_clk);
2712 if (ret)
2713 goto ff_clk_disable;
2714 }
2715
Venkat Gopalakrishnan9e069632013-06-12 11:16:37 -07002716 msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
2717
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302718 ret = sdhci_msm_bus_register(msm_host, pdev);
2719 if (ret)
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002720 goto sleep_clk_disable;
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302721
2722 if (msm_host->msm_bus_vote.client_handle)
2723 INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
2724 sdhci_msm_bus_work);
2725 sdhci_msm_bus_voting(host, 1);
2726
Asutosh Das0ef24812012-12-18 16:14:02 +05302727 /* Setup regulators */
2728 ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
2729 if (ret) {
2730 dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302731 goto bus_unregister;
Asutosh Das0ef24812012-12-18 16:14:02 +05302732 }
2733
2734 /* Reset the core and Enable SDHC mode */
2735 core_memres = platform_get_resource_byname(pdev,
2736 IORESOURCE_MEM, "core_mem");
2737 msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
2738 resource_size(core_memres));
2739
2740 if (!msm_host->core_mem) {
2741 dev_err(&pdev->dev, "Failed to remap registers\n");
2742 ret = -ENOMEM;
2743 goto vreg_deinit;
2744 }
2745
Stepan Moskovchenkoa4d0fa72013-09-13 22:19:33 -07002746 /* Unset HC_MODE_EN bit in HC_MODE register */
2747 writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
2748
Asutosh Das0ef24812012-12-18 16:14:02 +05302749 /* Set SW_RST bit in POWER register (Offset 0x0) */
Sahitya Tummala66b0fe32013-04-25 11:50:56 +05302750 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
2751 CORE_SW_RST, msm_host->core_mem + CORE_POWER);
2752 /*
2753 * SW reset can take upto 10HCLK + 15MCLK cycles.
2754 * Calculating based on min clk rates (hclk = 27MHz,
2755 * mclk = 400KHz) it comes to ~40us. Let's poll for
2756 * max. 1ms for reset completion.
2757 */
2758 ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
2759 pwr, !(pwr & CORE_SW_RST), 100, 10);
2760
2761 if (ret) {
2762 dev_err(&pdev->dev, "reset failed (%d)\n", ret);
2763 goto vreg_deinit;
2764 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302765 /* Set HC_MODE_EN bit in HC_MODE register */
2766 writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
2767
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002768 /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
2769 writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
2770 FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
2771
Pratibhasagar Vd8acb7c2013-11-11 01:32:21 +05302772 sdhci_set_default_hw_caps(msm_host, host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302773 /*
Subhash Jadavani28137342013-05-14 17:46:43 +05302774 * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
2775 * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
2776 * interrupt in GIC (by registering the interrupt handler), we need to
2777 * ensure that any pending power irq interrupt status is acknowledged
2778 * otherwise power irq interrupt handler would be fired prematurely.
2779 */
2780 irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
2781 writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
2782 irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
2783 if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
2784 irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
2785 if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
2786 irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
2787 writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
2788 /*
2789 * Ensure that above writes are propogated before interrupt enablement
2790 * in GIC.
2791 */
2792 mb();
2793
2794 /*
Asutosh Das0ef24812012-12-18 16:14:02 +05302795 * Following are the deviations from SDHC spec v3.0 -
2796 * 1. Card detection is handled using separate GPIO.
2797 * 2. Bus power control is handled by interacting with PMIC.
2798 */
2799 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
2800 host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302801 host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
2802 host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
Sahitya Tummala87d43942013-04-12 11:49:11 +05302803 host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
Sahitya Tummala314162c2013-04-12 12:11:20 +05302804 host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
Sahitya Tummala7c9780d2013-04-12 11:59:25 +05302805 host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
Asutosh Das0ef24812012-12-18 16:14:02 +05302806
Sahitya Tummalaa5733ab52013-06-10 16:32:51 +05302807 if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
2808 host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
2809
Stephen Boyd8dce5c62013-04-24 14:19:46 -07002810 host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002811 dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
2812 host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
2813 SDHCI_VENDOR_VER_SHIFT));
2814 if (((host_version & SDHCI_VENDOR_VER_MASK) >>
2815 SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
2816 /*
2817 * Add 40us delay in interrupt handler when
2818 * operating at initialization frequency(400KHz).
2819 */
2820 host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
2821 /*
2822 * Set Software Reset for DAT line in Software
2823 * Reset Register (Bit 2).
2824 */
2825 host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
2826 }
2827
Asutosh Das214b9662013-06-13 14:27:42 +05302828 host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
2829
Venkat Gopalakrishnana58f91f2012-09-17 16:00:15 -07002830 /* Setup PWRCTL irq */
Asutosh Das0ef24812012-12-18 16:14:02 +05302831 pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
2832 if (pwr_irq < 0) {
2833 dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
2834 pwr_irq);
2835 goto vreg_deinit;
2836 }
2837 ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL,
2838 sdhci_msm_pwr_irq, IRQF_ONESHOT,
Venkat Gopalakrishnan7944a372012-09-11 16:13:31 -07002839 dev_name(&pdev->dev), host);
Asutosh Das0ef24812012-12-18 16:14:02 +05302840 if (ret) {
2841 dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
2842 pwr_irq, ret);
2843 goto vreg_deinit;
2844 }
2845
2846 /* Enable pwr irq interrupts */
2847 writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
2848
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302849#ifdef CONFIG_MMC_CLKGATE
2850 /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
2851 msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
2852#endif
2853
Asutosh Das0ef24812012-12-18 16:14:02 +05302854 /* Set host capabilities */
2855 msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
2856 msm_host->mmc->caps |= msm_host->pdata->caps;
Asutosh Das0ef24812012-12-18 16:14:02 +05302857 msm_host->mmc->caps2 |= msm_host->pdata->caps2;
2858 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR;
2859 msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL;
Sahitya Tummala22dd3362013-02-28 19:50:51 +05302860 msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE;
Subhash Jadavani6d472b22013-05-29 15:52:10 +05302861 msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
Asutosh Dasbea115d2013-06-24 18:20:45 +05302862 msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
Asutosh Das0ef24812012-12-18 16:14:02 +05302863
2864 if (msm_host->pdata->nonremovable)
2865 msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
2866
Sahitya Tummalac6f48d42013-03-10 07:03:17 +05302867 host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
2868
Sahitya Tummala1f52eaa2013-03-20 19:24:01 +05302869 init_completion(&msm_host->pwr_irq_completion);
2870
Sahitya Tummala581df132013-03-12 14:57:46 +05302871 if (gpio_is_valid(msm_host->pdata->status_gpio)) {
2872 ret = mmc_gpio_request_cd(msm_host->mmc,
2873 msm_host->pdata->status_gpio, 0);
2874 if (ret) {
2875 dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
2876 __func__, ret);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302877 goto vreg_deinit;
Sahitya Tummala581df132013-03-12 14:57:46 +05302878 }
2879 }
2880
Krishna Konda7feab352013-09-17 23:55:40 -07002881 if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) &&
2882 (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) {
2883 host->dma_mask = DMA_BIT_MASK(64);
2884 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2885 } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
Sahitya Tummalaeaa21862013-03-20 19:34:59 +05302886 host->dma_mask = DMA_BIT_MASK(32);
2887 mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
2888 } else {
2889 dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
2890 }
2891
Asutosh Das0ef24812012-12-18 16:14:02 +05302892 ret = sdhci_add_host(host);
2893 if (ret) {
2894 dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
Sahitya Tummala581df132013-03-12 14:57:46 +05302895 goto vreg_deinit;
Asutosh Das0ef24812012-12-18 16:14:02 +05302896 }
2897
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302898 msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
2899 msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
2900 sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
2901 msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
2902 msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
2903 ret = device_create_file(&pdev->dev,
2904 &msm_host->msm_bus_vote.max_bus_bw);
2905 if (ret)
2906 goto remove_host;
2907
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302908 if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
2909 msm_host->polling.show = show_polling;
2910 msm_host->polling.store = store_polling;
2911 sysfs_attr_init(&msm_host->polling.attr);
2912 msm_host->polling.attr.name = "polling";
2913 msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
2914 ret = device_create_file(&pdev->dev, &msm_host->polling);
2915 if (ret)
2916 goto remove_max_bus_bw_file;
2917 }
Asutosh Dase5e9ca62013-07-30 19:08:36 +05302918
2919 msm_host->auto_cmd21_attr.show = show_auto_cmd21;
2920 msm_host->auto_cmd21_attr.store = store_auto_cmd21;
2921 sysfs_attr_init(&msm_host->auto_cmd21_attr.attr);
2922 msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21";
2923 msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR;
2924 ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr);
2925 if (ret) {
2926 pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n",
2927 mmc_hostname(host->mmc), __func__, ret);
2928 device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr);
2929 }
2930
Asutosh Das0ef24812012-12-18 16:14:02 +05302931 /* Successful initialization */
2932 goto out;
2933
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302934remove_max_bus_bw_file:
2935 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302936remove_host:
2937 dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
2938 sdhci_remove_host(host, dead);
2939vreg_deinit:
2940 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummala79edc6a2013-05-23 15:59:22 +05302941bus_unregister:
2942 if (msm_host->msm_bus_vote.client_handle)
2943 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2944 sdhci_msm_bus_unregister(msm_host);
Venkat Gopalakrishnan7f691572013-06-23 17:36:46 -07002945sleep_clk_disable:
2946 if (!IS_ERR(msm_host->sleep_clk))
2947 clk_disable_unprepare(msm_host->sleep_clk);
2948ff_clk_disable:
2949 if (!IS_ERR(msm_host->ff_clk))
2950 clk_disable_unprepare(msm_host->ff_clk);
Asutosh Das0ef24812012-12-18 16:14:02 +05302951clk_disable:
2952 if (!IS_ERR(msm_host->clk))
2953 clk_disable_unprepare(msm_host->clk);
2954pclk_disable:
2955 if (!IS_ERR(msm_host->pclk))
2956 clk_disable_unprepare(msm_host->pclk);
2957bus_clk_disable:
2958 if (!IS_ERR_OR_NULL(msm_host->bus_clk))
2959 clk_disable_unprepare(msm_host->bus_clk);
2960pltfm_free:
2961 sdhci_pltfm_free(pdev);
2962out:
2963 pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
2964 return ret;
2965}
2966
2967static int sdhci_msm_remove(struct platform_device *pdev)
2968{
2969 struct sdhci_host *host = platform_get_drvdata(pdev);
2970 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
2971 struct sdhci_msm_host *msm_host = pltfm_host->priv;
2972 struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
2973 int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
2974 0xffffffff);
2975
2976 pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
Sahitya Tummala5c55b932013-06-20 14:00:18 +05302977 if (!gpio_is_valid(msm_host->pdata->status_gpio))
2978 device_remove_file(&pdev->dev, &msm_host->polling);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302979 device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
Asutosh Das0ef24812012-12-18 16:14:02 +05302980 sdhci_remove_host(host, dead);
2981 sdhci_pltfm_free(pdev);
Sahitya Tummala581df132013-03-12 14:57:46 +05302982
Asutosh Das0ef24812012-12-18 16:14:02 +05302983 sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
Sahitya Tummalaa7f3c572013-01-11 11:30:45 +05302984
Pratibhasagar V9acf2642013-11-21 21:07:21 +05302985 sdhci_msm_setup_pins(pdata, true);
2986 sdhci_msm_setup_pins(pdata, false);
Sahitya Tummala8a3e8182013-03-10 14:12:52 +05302987
2988 if (msm_host->msm_bus_vote.client_handle) {
2989 sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
2990 sdhci_msm_bus_unregister(msm_host);
2991 }
Asutosh Das0ef24812012-12-18 16:14:02 +05302992 return 0;
2993}
2994
2995static const struct of_device_id sdhci_msm_dt_match[] = {
2996 {.compatible = "qcom,sdhci-msm"},
2997};
2998MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
2999
3000static struct platform_driver sdhci_msm_driver = {
3001 .probe = sdhci_msm_probe,
3002 .remove = sdhci_msm_remove,
3003 .driver = {
3004 .name = "sdhci_msm",
3005 .owner = THIS_MODULE,
3006 .of_match_table = sdhci_msm_dt_match,
3007 },
3008};
3009
3010module_platform_driver(sdhci_msm_driver);
3011
3012MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver");
3013MODULE_LICENSE("GPL v2");