Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1 | /* |
| 2 | * drivers/mmc/host/sdhci-msm.c - Qualcomm Technologies, Inc. MSM SDHCI Platform |
| 3 | * driver source file |
| 4 | * |
Venkat Gopalakrishnan | 0a92d53 | 2014-12-16 17:31:00 -0800 | [diff] [blame] | 5 | * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 6 | * |
| 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 Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 30 | #include <linux/io.h> |
| 31 | #include <linux/delay.h> |
| 32 | #include <linux/scatterlist.h> |
| 33 | #include <linux/slab.h> |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 34 | #include <linux/mmc/slot-gpio.h> |
Sahitya Tummala | eaa2186 | 2013-03-20 19:34:59 +0530 | [diff] [blame] | 35 | #include <linux/dma-mapping.h> |
Sahitya Tummala | 66b0fe3 | 2013-04-25 11:50:56 +0530 | [diff] [blame] | 36 | #include <linux/iopoll.h> |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 37 | #include <linux/pinctrl/consumer.h> |
| 38 | #include <linux/iopoll.h> |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 39 | #include <linux/msm-bus.h> |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 40 | #include <linux/pm_runtime.h> |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 41 | #include <trace/events/mmc.h> |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 42 | |
Sahitya Tummala | 5687473 | 2015-05-21 08:24:03 +0530 | [diff] [blame] | 43 | #include "sdhci-msm.h" |
Venkat Gopalakrishnan | d371f14 | 2015-05-29 17:49:46 -0700 | [diff] [blame] | 44 | #include "cmdq_hci.h" |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 45 | |
Venkat Gopalakrishnan | 0a92d53 | 2014-12-16 17:31:00 -0800 | [diff] [blame] | 46 | #define CORE_POWER 0x0 |
| 47 | #define CORE_SW_RST (1 << 7) |
| 48 | |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 49 | #define SDHCI_VER_100 0x2B |
Venkat Gopalakrishnan | 0a92d53 | 2014-12-16 17:31:00 -0800 | [diff] [blame] | 50 | #define CORE_MCI_DATA_CNT 0x30 |
| 51 | #define CORE_MCI_STATUS 0x34 |
| 52 | #define CORE_MCI_FIFO_CNT 0x44 |
| 53 | |
| 54 | #define CORE_VERSION_STEP_MASK 0x0000FFFF |
| 55 | #define CORE_VERSION_MINOR_MASK 0x0FFF0000 |
| 56 | #define CORE_VERSION_MINOR_SHIFT 16 |
| 57 | #define CORE_VERSION_MAJOR_MASK 0xF0000000 |
| 58 | #define CORE_VERSION_MAJOR_SHIFT 28 |
| 59 | #define CORE_VERSION_TARGET_MASK 0x000000FF |
| 60 | |
| 61 | #define CORE_GENERICS 0x70 |
| 62 | #define SWITCHABLE_SIGNALLING_VOL (1 << 29) |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 63 | |
| 64 | #define CORE_VERSION_MAJOR_MASK 0xF0000000 |
| 65 | #define CORE_VERSION_MAJOR_SHIFT 28 |
| 66 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 67 | #define CORE_HC_MODE 0x78 |
| 68 | #define HC_MODE_EN 0x1 |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 69 | #define FF_CLK_SW_RST_DIS (1 << 13) |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 70 | |
Sahitya Tummala | 67717bc | 2013-08-02 09:21:37 +0530 | [diff] [blame] | 71 | #define CORE_MCI_VERSION 0x050 |
| 72 | #define CORE_TESTBUS_CONFIG 0x0CC |
| 73 | #define CORE_TESTBUS_ENA (1 << 3) |
Venkat Gopalakrishnan | 0a92d53 | 2014-12-16 17:31:00 -0800 | [diff] [blame] | 74 | #define CORE_TESTBUS_SEL2_BIT 4 |
| 75 | #define CORE_TESTBUS_SEL2 (1 << CORE_TESTBUS_SEL2_BIT) |
Sahitya Tummala | 67717bc | 2013-08-02 09:21:37 +0530 | [diff] [blame] | 76 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 77 | #define CORE_PWRCTL_STATUS 0xDC |
| 78 | #define CORE_PWRCTL_MASK 0xE0 |
| 79 | #define CORE_PWRCTL_CLEAR 0xE4 |
| 80 | #define CORE_PWRCTL_CTL 0xE8 |
| 81 | |
| 82 | #define CORE_PWRCTL_BUS_OFF 0x01 |
| 83 | #define CORE_PWRCTL_BUS_ON (1 << 1) |
| 84 | #define CORE_PWRCTL_IO_LOW (1 << 2) |
| 85 | #define CORE_PWRCTL_IO_HIGH (1 << 3) |
| 86 | |
| 87 | #define CORE_PWRCTL_BUS_SUCCESS 0x01 |
| 88 | #define CORE_PWRCTL_BUS_FAIL (1 << 1) |
| 89 | #define CORE_PWRCTL_IO_SUCCESS (1 << 2) |
| 90 | #define CORE_PWRCTL_IO_FAIL (1 << 3) |
| 91 | |
| 92 | #define INT_MASK 0xF |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 93 | #define MAX_PHASES 16 |
| 94 | |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 95 | #define CORE_DLL_CONFIG 0x100 |
| 96 | #define CORE_CMD_DAT_TRACK_SEL (1 << 0) |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 97 | #define CORE_DLL_EN (1 << 16) |
| 98 | #define CORE_CDR_EN (1 << 17) |
| 99 | #define CORE_CK_OUT_EN (1 << 18) |
| 100 | #define CORE_CDR_EXT_EN (1 << 19) |
| 101 | #define CORE_DLL_PDN (1 << 29) |
| 102 | #define CORE_DLL_RST (1 << 30) |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 103 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 104 | #define CORE_DLL_STATUS 0x108 |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 105 | #define CORE_DLL_LOCK (1 << 7) |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 106 | #define CORE_DDR_DLL_LOCK (1 << 11) |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 107 | |
| 108 | #define CORE_VENDOR_SPEC 0x10C |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 109 | #define CORE_CLK_PWRSAVE (1 << 1) |
| 110 | #define CORE_HC_MCLK_SEL_DFLT (2 << 8) |
| 111 | #define CORE_HC_MCLK_SEL_HS400 (3 << 8) |
| 112 | #define CORE_HC_MCLK_SEL_MASK (3 << 8) |
| 113 | #define CORE_HC_AUTO_CMD21_EN (1 << 6) |
| 114 | #define CORE_IO_PAD_PWR_SWITCH_EN (1 << 15) |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 115 | #define CORE_IO_PAD_PWR_SWITCH (1 << 16) |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 116 | #define CORE_HC_SELECT_IN_EN (1 << 18) |
| 117 | #define CORE_HC_SELECT_IN_HS400 (6 << 19) |
| 118 | #define CORE_HC_SELECT_IN_MASK (7 << 19) |
Venkat Gopalakrishnan | 17edb35 | 2015-06-24 14:46:49 -0700 | [diff] [blame^] | 119 | #define CORE_VENDOR_SPEC_POR_VAL 0xA1C |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 120 | |
Venkat Gopalakrishnan | 0a92d53 | 2014-12-16 17:31:00 -0800 | [diff] [blame] | 121 | #define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114 |
| 122 | #define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118 |
| 123 | |
Krishna Konda | 7feab35 | 2013-09-17 23:55:40 -0700 | [diff] [blame] | 124 | #define CORE_VENDOR_SPEC_CAPABILITIES0 0x11C |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 125 | #define CORE_8_BIT_SUPPORT (1 << 18) |
| 126 | #define CORE_3_3V_SUPPORT (1 << 24) |
| 127 | #define CORE_3_0V_SUPPORT (1 << 25) |
| 128 | #define CORE_1_8V_SUPPORT (1 << 26) |
Gilad Broner | 2a10ca0 | 2014-10-02 17:20:35 +0300 | [diff] [blame] | 129 | #define CORE_SYS_BUS_SUPPORT_64_BIT BIT(28) |
Krishna Konda | 7feab35 | 2013-09-17 23:55:40 -0700 | [diff] [blame] | 130 | |
Venkat Gopalakrishnan | 0a92d53 | 2014-12-16 17:31:00 -0800 | [diff] [blame] | 131 | #define CORE_SDCC_DEBUG_REG 0x124 |
Sahitya Tummala | 67717bc | 2013-08-02 09:21:37 +0530 | [diff] [blame] | 132 | |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 133 | #define CORE_CSR_CDC_CTLR_CFG0 0x130 |
| 134 | #define CORE_SW_TRIG_FULL_CALIB (1 << 16) |
| 135 | #define CORE_HW_AUTOCAL_ENA (1 << 17) |
| 136 | |
| 137 | #define CORE_CSR_CDC_CTLR_CFG1 0x134 |
| 138 | #define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138 |
| 139 | #define CORE_TIMER_ENA (1 << 16) |
| 140 | |
| 141 | #define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C |
| 142 | #define CORE_CSR_CDC_REFCOUNT_CFG 0x140 |
| 143 | #define CORE_CSR_CDC_COARSE_CAL_CFG 0x144 |
| 144 | #define CORE_CDC_OFFSET_CFG 0x14C |
| 145 | #define CORE_CSR_CDC_DELAY_CFG 0x150 |
| 146 | #define CORE_CDC_SLAVE_DDA_CFG 0x160 |
| 147 | #define CORE_CSR_CDC_STATUS0 0x164 |
| 148 | #define CORE_CALIBRATION_DONE (1 << 0) |
| 149 | |
| 150 | #define CORE_CDC_ERROR_CODE_MASK 0x7000000 |
| 151 | |
| 152 | #define CORE_CSR_CDC_GEN_CFG 0x178 |
| 153 | #define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0) |
| 154 | #define CORE_CDC_SWITCH_RC_EN (1 << 1) |
| 155 | |
| 156 | #define CORE_DDR_200_CFG 0x184 |
| 157 | #define CORE_CDC_T4_DLY_SEL (1 << 0) |
| 158 | #define CORE_START_CDC_TRAFFIC (1 << 6) |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 159 | #define CORE_VENDOR_SPEC3 0x1B0 |
| 160 | #define CORE_PWRSAVE_DLL (1 << 3) |
| 161 | |
| 162 | #define CORE_DLL_CONFIG_2 0x1B4 |
| 163 | #define CORE_DDR_CAL_EN (1 << 0) |
Venkat Gopalakrishnan | c0a5c3a | 2015-02-03 16:10:07 -0800 | [diff] [blame] | 164 | #define CORE_FLL_CYCLE_CNT (1 << 18) |
| 165 | #define CORE_DLL_CLOCK_DISABLE (1 << 21) |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 166 | |
| 167 | #define CORE_DDR_CONFIG 0x1B8 |
| 168 | #define DDR_CONFIG_POR_VAL 0x80040853 |
| 169 | |
Sahitya Tummala | 5687473 | 2015-05-21 08:24:03 +0530 | [diff] [blame] | 170 | #define MSM_MMC_DEFAULT_CPU_DMA_LATENCY 200 /* usecs */ |
| 171 | |
Venkat Gopalakrishnan | 450745e | 2014-07-24 20:39:34 -0700 | [diff] [blame] | 172 | /* 512 descriptors */ |
| 173 | #define SDHCI_MSM_MAX_SEGMENTS (1 << 9) |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 174 | #define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */ |
Asutosh Das | 648f9d1 | 2013-01-10 21:11:04 +0530 | [diff] [blame] | 175 | |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 176 | #define CORE_FREQ_100MHZ (100 * 1000 * 1000) |
Venkat Gopalakrishnan | c0a5c3a | 2015-02-03 16:10:07 -0800 | [diff] [blame] | 177 | #define TCXO_FREQ 19200000 |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 178 | |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 179 | #define INVALID_TUNING_PHASE -1 |
| 180 | |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 181 | #define NUM_TUNING_PHASES 16 |
| 182 | #define MAX_DRV_TYPES_SUPPORTED_HS200 3 |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 183 | #define MSM_AUTOSUSPEND_DELAY_MS 100 |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 184 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 185 | static const u32 tuning_block_64[] = { |
| 186 | 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE, |
| 187 | 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777, |
| 188 | 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF, |
| 189 | 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7 |
| 190 | }; |
| 191 | |
| 192 | static const u32 tuning_block_128[] = { |
| 193 | 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC, |
| 194 | 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF, |
| 195 | 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF, |
| 196 | 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB, |
| 197 | 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC, |
| 198 | 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF, |
| 199 | 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB, |
| 200 | 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77 |
| 201 | }; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 202 | |
Venkat Gopalakrishnan | 270580a | 2013-03-11 12:17:57 -0700 | [diff] [blame] | 203 | static int disable_slots; |
| 204 | /* root can write, others read */ |
| 205 | module_param(disable_slots, int, S_IRUGO|S_IWUSR); |
| 206 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 207 | enum vdd_io_level { |
| 208 | /* set vdd_io_data->low_vol_level */ |
| 209 | VDD_IO_LOW, |
| 210 | /* set vdd_io_data->high_vol_level */ |
| 211 | VDD_IO_HIGH, |
| 212 | /* |
| 213 | * set whatever there in voltage_level (third argument) of |
| 214 | * sdhci_msm_set_vdd_io_vol() function. |
| 215 | */ |
| 216 | VDD_IO_SET_LEVEL, |
| 217 | }; |
| 218 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 219 | /* MSM platform specific tuning */ |
| 220 | static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, |
| 221 | u8 poll) |
| 222 | { |
| 223 | int rc = 0; |
| 224 | u32 wait_cnt = 50; |
| 225 | u8 ck_out_en = 0; |
| 226 | struct mmc_host *mmc = host->mmc; |
| 227 | |
| 228 | /* poll for CK_OUT_EN bit. max. poll time = 50us */ |
| 229 | ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) & |
| 230 | CORE_CK_OUT_EN); |
| 231 | |
| 232 | while (ck_out_en != poll) { |
| 233 | if (--wait_cnt == 0) { |
| 234 | pr_err("%s: %s: CK_OUT_EN bit is not %d\n", |
| 235 | mmc_hostname(mmc), __func__, poll); |
| 236 | rc = -ETIMEDOUT; |
| 237 | goto out; |
| 238 | } |
| 239 | udelay(1); |
| 240 | |
| 241 | ck_out_en = !!(readl_relaxed(host->ioaddr + |
| 242 | CORE_DLL_CONFIG) & CORE_CK_OUT_EN); |
| 243 | } |
| 244 | out: |
| 245 | return rc; |
| 246 | } |
| 247 | |
Asutosh Das | e5e9ca6 | 2013-07-30 19:08:36 +0530 | [diff] [blame] | 248 | /* |
| 249 | * Enable CDR to track changes of DAT lines and adjust sampling |
| 250 | * point according to voltage/temperature variations |
| 251 | */ |
| 252 | static int msm_enable_cdr_cm_sdc4_dll(struct sdhci_host *host) |
| 253 | { |
| 254 | int rc = 0; |
| 255 | u32 config; |
| 256 | |
| 257 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
| 258 | config |= CORE_CDR_EN; |
| 259 | config &= ~(CORE_CDR_EXT_EN | CORE_CK_OUT_EN); |
| 260 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); |
| 261 | |
| 262 | rc = msm_dll_poll_ck_out_en(host, 0); |
| 263 | if (rc) |
| 264 | goto err; |
| 265 | |
| 266 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) | |
| 267 | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG); |
| 268 | |
| 269 | rc = msm_dll_poll_ck_out_en(host, 1); |
| 270 | if (rc) |
| 271 | goto err; |
| 272 | goto out; |
| 273 | err: |
| 274 | pr_err("%s: %s: failed\n", mmc_hostname(host->mmc), __func__); |
| 275 | out: |
| 276 | return rc; |
| 277 | } |
| 278 | |
| 279 | static ssize_t store_auto_cmd21(struct device *dev, struct device_attribute |
| 280 | *attr, const char *buf, size_t count) |
| 281 | { |
| 282 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 283 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 284 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 285 | u32 tmp; |
| 286 | unsigned long flags; |
| 287 | |
| 288 | if (!kstrtou32(buf, 0, &tmp)) { |
| 289 | spin_lock_irqsave(&host->lock, flags); |
| 290 | msm_host->en_auto_cmd21 = !!tmp; |
| 291 | spin_unlock_irqrestore(&host->lock, flags); |
| 292 | } |
| 293 | return count; |
| 294 | } |
| 295 | |
| 296 | static ssize_t show_auto_cmd21(struct device *dev, |
| 297 | struct device_attribute *attr, char *buf) |
| 298 | { |
| 299 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 300 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 301 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 302 | |
| 303 | return snprintf(buf, PAGE_SIZE, "%d\n", msm_host->en_auto_cmd21); |
| 304 | } |
| 305 | |
| 306 | /* MSM auto-tuning handler */ |
| 307 | static int sdhci_msm_config_auto_tuning_cmd(struct sdhci_host *host, |
| 308 | bool enable, |
| 309 | u32 type) |
| 310 | { |
| 311 | int rc = 0; |
| 312 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 313 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 314 | u32 val = 0; |
| 315 | |
| 316 | if (!msm_host->en_auto_cmd21) |
| 317 | return 0; |
| 318 | |
| 319 | if (type == MMC_SEND_TUNING_BLOCK_HS200) |
| 320 | val = CORE_HC_AUTO_CMD21_EN; |
| 321 | else |
| 322 | return 0; |
| 323 | |
| 324 | if (enable) { |
| 325 | rc = msm_enable_cdr_cm_sdc4_dll(host); |
| 326 | writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) | |
| 327 | val, host->ioaddr + CORE_VENDOR_SPEC); |
| 328 | } else { |
| 329 | writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) & |
| 330 | ~val, host->ioaddr + CORE_VENDOR_SPEC); |
| 331 | } |
| 332 | return rc; |
| 333 | } |
| 334 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 335 | static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase) |
| 336 | { |
| 337 | int rc = 0; |
| 338 | u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, |
| 339 | 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, |
| 340 | 0x8}; |
| 341 | unsigned long flags; |
| 342 | u32 config; |
| 343 | struct mmc_host *mmc = host->mmc; |
| 344 | |
| 345 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
| 346 | spin_lock_irqsave(&host->lock, flags); |
| 347 | |
| 348 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
| 349 | config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN); |
| 350 | config |= (CORE_CDR_EXT_EN | CORE_DLL_EN); |
| 351 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); |
| 352 | |
| 353 | /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */ |
| 354 | rc = msm_dll_poll_ck_out_en(host, 0); |
| 355 | if (rc) |
| 356 | goto err_out; |
| 357 | |
| 358 | /* |
| 359 | * Write the selected DLL clock output phase (0 ... 15) |
| 360 | * to CDR_SELEXT bit field of DLL_CONFIG register. |
| 361 | */ |
| 362 | writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 363 | & ~(0xF << 20)) |
| 364 | | (grey_coded_phase_table[phase] << 20)), |
| 365 | host->ioaddr + CORE_DLL_CONFIG); |
| 366 | |
| 367 | /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */ |
| 368 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 369 | | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG); |
| 370 | |
| 371 | /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */ |
| 372 | rc = msm_dll_poll_ck_out_en(host, 1); |
| 373 | if (rc) |
| 374 | goto err_out; |
| 375 | |
| 376 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
| 377 | config |= CORE_CDR_EN; |
| 378 | config &= ~CORE_CDR_EXT_EN; |
| 379 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); |
| 380 | goto out; |
| 381 | |
| 382 | err_out: |
| 383 | pr_err("%s: %s: Failed to set DLL phase: %d\n", |
| 384 | mmc_hostname(mmc), __func__, phase); |
| 385 | out: |
| 386 | spin_unlock_irqrestore(&host->lock, flags); |
| 387 | pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__); |
| 388 | return rc; |
| 389 | } |
| 390 | |
| 391 | /* |
| 392 | * Find out the greatest range of consecuitive selected |
| 393 | * DLL clock output phases that can be used as sampling |
| 394 | * setting for SD3.0 UHS-I card read operation (in SDR104 |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 395 | * timing mode) or for eMMC4.5 card read operation (in |
| 396 | * HS400/HS200 timing mode). |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 397 | * Select the 3/4 of the range and configure the DLL with the |
| 398 | * selected DLL clock output phase. |
| 399 | */ |
| 400 | |
| 401 | static int msm_find_most_appropriate_phase(struct sdhci_host *host, |
| 402 | u8 *phase_table, u8 total_phases) |
| 403 | { |
| 404 | int ret; |
| 405 | u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} }; |
| 406 | u8 phases_per_row[MAX_PHASES] = {0}; |
| 407 | int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0; |
| 408 | int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0; |
| 409 | bool phase_0_found = false, phase_15_found = false; |
| 410 | struct mmc_host *mmc = host->mmc; |
| 411 | |
| 412 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
| 413 | if (!total_phases || (total_phases > MAX_PHASES)) { |
| 414 | pr_err("%s: %s: invalid argument: total_phases=%d\n", |
| 415 | mmc_hostname(mmc), __func__, total_phases); |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
| 419 | for (cnt = 0; cnt < total_phases; cnt++) { |
| 420 | ranges[row_index][col_index] = phase_table[cnt]; |
| 421 | phases_per_row[row_index] += 1; |
| 422 | col_index++; |
| 423 | |
| 424 | if ((cnt + 1) == total_phases) { |
| 425 | continue; |
| 426 | /* check if next phase in phase_table is consecutive or not */ |
| 427 | } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) { |
| 428 | row_index++; |
| 429 | col_index = 0; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (row_index >= MAX_PHASES) |
| 434 | return -EINVAL; |
| 435 | |
| 436 | /* Check if phase-0 is present in first valid window? */ |
| 437 | if (!ranges[0][0]) { |
| 438 | phase_0_found = true; |
| 439 | phase_0_raw_index = 0; |
| 440 | /* Check if cycle exist between 2 valid windows */ |
| 441 | for (cnt = 1; cnt <= row_index; cnt++) { |
| 442 | if (phases_per_row[cnt]) { |
| 443 | for (i = 0; i < phases_per_row[cnt]; i++) { |
| 444 | if (ranges[cnt][i] == 15) { |
| 445 | phase_15_found = true; |
| 446 | phase_15_raw_index = cnt; |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | /* If 2 valid windows form cycle then merge them as single window */ |
| 455 | if (phase_0_found && phase_15_found) { |
| 456 | /* number of phases in raw where phase 0 is present */ |
| 457 | u8 phases_0 = phases_per_row[phase_0_raw_index]; |
| 458 | /* number of phases in raw where phase 15 is present */ |
| 459 | u8 phases_15 = phases_per_row[phase_15_raw_index]; |
| 460 | |
| 461 | if (phases_0 + phases_15 >= MAX_PHASES) |
| 462 | /* |
| 463 | * If there are more than 1 phase windows then total |
| 464 | * number of phases in both the windows should not be |
| 465 | * more than or equal to MAX_PHASES. |
| 466 | */ |
| 467 | return -EINVAL; |
| 468 | |
| 469 | /* Merge 2 cyclic windows */ |
| 470 | i = phases_15; |
| 471 | for (cnt = 0; cnt < phases_0; cnt++) { |
| 472 | ranges[phase_15_raw_index][i] = |
| 473 | ranges[phase_0_raw_index][cnt]; |
| 474 | if (++i >= MAX_PHASES) |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | phases_per_row[phase_0_raw_index] = 0; |
| 479 | phases_per_row[phase_15_raw_index] = phases_15 + phases_0; |
| 480 | } |
| 481 | |
| 482 | for (cnt = 0; cnt <= row_index; cnt++) { |
| 483 | if (phases_per_row[cnt] > curr_max) { |
| 484 | curr_max = phases_per_row[cnt]; |
| 485 | selected_row_index = cnt; |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | i = ((curr_max * 3) / 4); |
| 490 | if (i) |
| 491 | i--; |
| 492 | |
| 493 | ret = (int)ranges[selected_row_index][i]; |
| 494 | |
| 495 | if (ret >= MAX_PHASES) { |
| 496 | ret = -EINVAL; |
| 497 | pr_err("%s: %s: invalid phase selected=%d\n", |
| 498 | mmc_hostname(mmc), __func__, ret); |
| 499 | } |
| 500 | |
| 501 | pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__); |
| 502 | return ret; |
| 503 | } |
| 504 | |
| 505 | static inline void msm_cm_dll_set_freq(struct sdhci_host *host) |
| 506 | { |
| 507 | u32 mclk_freq = 0; |
| 508 | |
| 509 | /* Program the MCLK value to MCLK_FREQ bit field */ |
| 510 | if (host->clock <= 112000000) |
| 511 | mclk_freq = 0; |
| 512 | else if (host->clock <= 125000000) |
| 513 | mclk_freq = 1; |
| 514 | else if (host->clock <= 137000000) |
| 515 | mclk_freq = 2; |
| 516 | else if (host->clock <= 150000000) |
| 517 | mclk_freq = 3; |
| 518 | else if (host->clock <= 162000000) |
| 519 | mclk_freq = 4; |
| 520 | else if (host->clock <= 175000000) |
| 521 | mclk_freq = 5; |
| 522 | else if (host->clock <= 187000000) |
| 523 | mclk_freq = 6; |
| 524 | else if (host->clock <= 200000000) |
| 525 | mclk_freq = 7; |
| 526 | |
| 527 | writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 528 | & ~(7 << 24)) | (mclk_freq << 24)), |
| 529 | host->ioaddr + CORE_DLL_CONFIG); |
| 530 | } |
| 531 | |
| 532 | /* Initialize the DLL (Programmable Delay Line ) */ |
| 533 | static int msm_init_cm_dll(struct sdhci_host *host) |
| 534 | { |
Venkat Gopalakrishnan | c0a5c3a | 2015-02-03 16:10:07 -0800 | [diff] [blame] | 535 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 536 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 537 | struct mmc_host *mmc = host->mmc; |
| 538 | int rc = 0; |
| 539 | unsigned long flags; |
| 540 | u32 wait_cnt; |
Subhash Jadavani | 99bca3b | 2013-05-28 18:21:57 +0530 | [diff] [blame] | 541 | bool prev_pwrsave, curr_pwrsave; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 542 | |
| 543 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
| 544 | spin_lock_irqsave(&host->lock, flags); |
Subhash Jadavani | 99bca3b | 2013-05-28 18:21:57 +0530 | [diff] [blame] | 545 | prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) & |
| 546 | CORE_CLK_PWRSAVE); |
| 547 | curr_pwrsave = prev_pwrsave; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 548 | /* |
| 549 | * Make sure that clock is always enabled when DLL |
| 550 | * tuning is in progress. Keeping PWRSAVE ON may |
| 551 | * turn off the clock. So let's disable the PWRSAVE |
| 552 | * here and re-enable it once tuning is completed. |
| 553 | */ |
Subhash Jadavani | 99bca3b | 2013-05-28 18:21:57 +0530 | [diff] [blame] | 554 | if (prev_pwrsave) { |
| 555 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
| 556 | & ~CORE_CLK_PWRSAVE), |
| 557 | host->ioaddr + CORE_VENDOR_SPEC); |
| 558 | curr_pwrsave = false; |
| 559 | } |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 560 | |
Venkat Gopalakrishnan | c0a5c3a | 2015-02-03 16:10:07 -0800 | [diff] [blame] | 561 | if (msm_host->use_updated_dll_reset) { |
| 562 | /* Disable the DLL clock */ |
| 563 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 564 | & ~CORE_CK_OUT_EN), |
| 565 | host->ioaddr + CORE_DLL_CONFIG); |
| 566 | |
| 567 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2) |
| 568 | | CORE_DLL_CLOCK_DISABLE), |
| 569 | host->ioaddr + CORE_DLL_CONFIG_2); |
| 570 | } |
| 571 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 572 | /* Write 1 to DLL_RST bit of DLL_CONFIG register */ |
| 573 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 574 | | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG); |
| 575 | |
| 576 | /* Write 1 to DLL_PDN bit of DLL_CONFIG register */ |
| 577 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 578 | | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG); |
| 579 | msm_cm_dll_set_freq(host); |
| 580 | |
Venkat Gopalakrishnan | c0a5c3a | 2015-02-03 16:10:07 -0800 | [diff] [blame] | 581 | if (msm_host->use_updated_dll_reset) { |
| 582 | u32 mclk_freq = 0; |
| 583 | |
| 584 | if ((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2) |
| 585 | & CORE_FLL_CYCLE_CNT)) |
| 586 | mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 8); |
| 587 | else |
| 588 | mclk_freq = (u32) ((host->clock / TCXO_FREQ) * 4); |
| 589 | |
| 590 | writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2) |
| 591 | & ~(0xFF << 10)) | (mclk_freq << 10)), |
| 592 | host->ioaddr + CORE_DLL_CONFIG_2); |
| 593 | /* wait for 5us before enabling DLL clock */ |
| 594 | udelay(5); |
| 595 | } |
| 596 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 597 | /* Write 0 to DLL_RST bit of DLL_CONFIG register */ |
| 598 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 599 | & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG); |
| 600 | |
| 601 | /* Write 0 to DLL_PDN bit of DLL_CONFIG register */ |
| 602 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 603 | & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG); |
| 604 | |
Venkat Gopalakrishnan | c0a5c3a | 2015-02-03 16:10:07 -0800 | [diff] [blame] | 605 | if (msm_host->use_updated_dll_reset) { |
| 606 | msm_cm_dll_set_freq(host); |
| 607 | /* Enable the DLL clock */ |
| 608 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2) |
| 609 | & ~CORE_DLL_CLOCK_DISABLE), |
| 610 | host->ioaddr + CORE_DLL_CONFIG_2); |
| 611 | } |
| 612 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 613 | /* Set DLL_EN bit to 1. */ |
| 614 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 615 | | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG); |
| 616 | |
| 617 | /* Set CK_OUT_EN bit to 1. */ |
| 618 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 619 | | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG); |
| 620 | |
| 621 | wait_cnt = 50; |
| 622 | /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */ |
| 623 | while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) & |
| 624 | CORE_DLL_LOCK)) { |
| 625 | /* max. wait for 50us sec for LOCK bit to be set */ |
| 626 | if (--wait_cnt == 0) { |
| 627 | pr_err("%s: %s: DLL failed to LOCK\n", |
| 628 | mmc_hostname(mmc), __func__); |
| 629 | rc = -ETIMEDOUT; |
| 630 | goto out; |
| 631 | } |
| 632 | /* wait for 1us before polling again */ |
| 633 | udelay(1); |
| 634 | } |
| 635 | |
| 636 | out: |
Subhash Jadavani | 99bca3b | 2013-05-28 18:21:57 +0530 | [diff] [blame] | 637 | /* Restore the correct PWRSAVE state */ |
| 638 | if (prev_pwrsave ^ curr_pwrsave) { |
| 639 | u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC); |
| 640 | |
| 641 | if (prev_pwrsave) |
| 642 | reg |= CORE_CLK_PWRSAVE; |
| 643 | else |
| 644 | reg &= ~CORE_CLK_PWRSAVE; |
| 645 | |
| 646 | writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC); |
| 647 | } |
| 648 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 649 | spin_unlock_irqrestore(&host->lock, flags); |
| 650 | pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__); |
| 651 | return rc; |
| 652 | } |
| 653 | |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 654 | static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host) |
| 655 | { |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 656 | u32 calib_done; |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 657 | int ret = 0; |
| 658 | int cdc_err = 0; |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 659 | |
| 660 | pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__); |
| 661 | |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 662 | /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */ |
| 663 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG) |
| 664 | & ~CORE_CDC_T4_DLY_SEL), |
| 665 | host->ioaddr + CORE_DDR_200_CFG); |
| 666 | |
| 667 | /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */ |
| 668 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG) |
| 669 | & ~CORE_CDC_SWITCH_BYPASS_OFF), |
| 670 | host->ioaddr + CORE_CSR_CDC_GEN_CFG); |
| 671 | |
| 672 | /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */ |
| 673 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG) |
| 674 | | CORE_CDC_SWITCH_RC_EN), |
| 675 | host->ioaddr + CORE_CSR_CDC_GEN_CFG); |
| 676 | |
| 677 | /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */ |
| 678 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG) |
| 679 | & ~CORE_START_CDC_TRAFFIC), |
| 680 | host->ioaddr + CORE_DDR_200_CFG); |
| 681 | |
| 682 | /* |
| 683 | * Perform CDC Register Initialization Sequence |
| 684 | * |
| 685 | * CORE_CSR_CDC_CTLR_CFG0 0x11800EC |
| 686 | * CORE_CSR_CDC_CTLR_CFG1 0x3011111 |
| 687 | * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000 |
| 688 | * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4 |
| 689 | * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020 |
| 690 | * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19 |
| 691 | * CORE_CSR_CDC_DELAY_CFG 0x3AC |
| 692 | * CORE_CDC_OFFSET_CFG 0x0 |
| 693 | * CORE_CDC_SLAVE_DDA_CFG 0x16334 |
| 694 | */ |
| 695 | |
| 696 | writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); |
| 697 | writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1); |
| 698 | writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0); |
| 699 | writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1); |
| 700 | writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG); |
| 701 | writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG); |
Subhash Jadavani | be406d9 | 2014-06-17 16:47:48 -0700 | [diff] [blame] | 702 | writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG); |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 703 | writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG); |
| 704 | writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG); |
| 705 | |
| 706 | /* CDC HW Calibration */ |
| 707 | |
| 708 | /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */ |
| 709 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0) |
| 710 | | CORE_SW_TRIG_FULL_CALIB), |
| 711 | host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); |
| 712 | |
| 713 | /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */ |
| 714 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0) |
| 715 | & ~CORE_SW_TRIG_FULL_CALIB), |
| 716 | host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); |
| 717 | |
| 718 | /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */ |
| 719 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0) |
| 720 | | CORE_HW_AUTOCAL_ENA), |
| 721 | host->ioaddr + CORE_CSR_CDC_CTLR_CFG0); |
| 722 | |
| 723 | /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */ |
| 724 | writel_relaxed((readl_relaxed(host->ioaddr + |
| 725 | CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA), |
| 726 | host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0); |
| 727 | |
| 728 | mb(); |
| 729 | |
| 730 | /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */ |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 731 | ret = readl_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0, |
| 732 | calib_done, (calib_done & CORE_CALIBRATION_DONE), 1, 50); |
| 733 | |
| 734 | if (ret == -ETIMEDOUT) { |
| 735 | pr_err("%s: %s: CDC Calibration was not completed\n", |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 736 | mmc_hostname(host->mmc), __func__); |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 737 | goto out; |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */ |
| 741 | cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0) |
| 742 | & CORE_CDC_ERROR_CODE_MASK; |
| 743 | if (cdc_err) { |
| 744 | pr_err("%s: %s: CDC Error Code %d\n", |
| 745 | mmc_hostname(host->mmc), __func__, cdc_err); |
| 746 | ret = -EINVAL; |
| 747 | goto out; |
| 748 | } |
| 749 | |
| 750 | /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */ |
| 751 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG) |
| 752 | | CORE_START_CDC_TRAFFIC), |
| 753 | host->ioaddr + CORE_DDR_200_CFG); |
| 754 | out: |
| 755 | pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc), |
| 756 | __func__, ret); |
| 757 | return ret; |
| 758 | } |
| 759 | |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 760 | static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host) |
| 761 | { |
Ritesh Harjani | 764065e | 2015-05-13 14:14:45 +0530 | [diff] [blame] | 762 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 763 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 764 | u32 dll_status; |
| 765 | int ret = 0; |
| 766 | |
| 767 | pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__); |
| 768 | |
| 769 | /* |
| 770 | * Currently the CORE_DDR_CONFIG register defaults to desired |
| 771 | * configuration on reset. Currently reprogramming the power on |
| 772 | * reset (POR) value in case it might have been modified by |
| 773 | * bootloaders. In the future, if this changes, then the desired |
| 774 | * values will need to be programmed appropriately. |
| 775 | */ |
| 776 | writel_relaxed(DDR_CONFIG_POR_VAL, host->ioaddr + CORE_DDR_CONFIG); |
| 777 | |
| 778 | /* Write 1 to DDR_CAL_EN field in CORE_DLL_CONFIG_2 */ |
| 779 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG_2) |
| 780 | | CORE_DDR_CAL_EN), |
| 781 | host->ioaddr + CORE_DLL_CONFIG_2); |
| 782 | |
| 783 | /* Poll on DDR_DLL_LOCK bit in CORE_DLL_STATUS to be set */ |
| 784 | ret = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS, |
| 785 | dll_status, (dll_status & CORE_DDR_DLL_LOCK), 10, 1000); |
| 786 | |
| 787 | if (ret == -ETIMEDOUT) { |
| 788 | pr_err("%s: %s: CM_DLL_SDC4 Calibration was not completed\n", |
| 789 | mmc_hostname(host->mmc), __func__); |
| 790 | goto out; |
| 791 | } |
| 792 | |
Ritesh Harjani | 764065e | 2015-05-13 14:14:45 +0530 | [diff] [blame] | 793 | /* |
| 794 | * set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3. |
| 795 | * when MCLK is gated OFF, it is not gated for less than 0.5us |
| 796 | * and MCLK must be switched on for at-least 1us before DATA |
| 797 | * starts coming. Controllers with 14lpp tech DLL cannot |
| 798 | * guarantee above requirement. So PWRSAVE_DLL should not be |
| 799 | * turned on for host controllers using this DLL. |
| 800 | */ |
| 801 | if (!msm_host->use_14lpp_dll) |
| 802 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC3) |
| 803 | | CORE_PWRSAVE_DLL), |
| 804 | host->ioaddr + CORE_VENDOR_SPEC3); |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 805 | mb(); |
| 806 | out: |
| 807 | pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc), |
| 808 | __func__, ret); |
| 809 | return ret; |
| 810 | } |
| 811 | |
| 812 | static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host) |
| 813 | { |
| 814 | int ret = 0; |
| 815 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 816 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 817 | |
| 818 | pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__); |
| 819 | |
| 820 | /* |
| 821 | * Retuning in HS400 (DDR mode) will fail, just reset the |
| 822 | * tuning block and restore the saved tuning phase. |
| 823 | */ |
| 824 | ret = msm_init_cm_dll(host); |
| 825 | if (ret) |
| 826 | goto out; |
| 827 | |
| 828 | /* Set the selected phase in delay line hw block */ |
| 829 | ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase); |
| 830 | if (ret) |
| 831 | goto out; |
| 832 | |
Krishna Konda | 0e8efba | 2014-06-23 14:50:38 -0700 | [diff] [blame] | 833 | /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */ |
| 834 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 835 | | CORE_CMD_DAT_TRACK_SEL), |
| 836 | host->ioaddr + CORE_DLL_CONFIG); |
| 837 | |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 838 | if (msm_host->use_cdclp533) |
| 839 | /* Calibrate CDCLP533 DLL HW */ |
| 840 | ret = sdhci_msm_cdclp533_calibration(host); |
| 841 | else |
| 842 | /* Calibrate CM_DLL_SDC4 HW */ |
| 843 | ret = sdhci_msm_cm_dll_sdc4_calibration(host); |
| 844 | out: |
| 845 | pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc), |
| 846 | __func__, ret); |
| 847 | return ret; |
| 848 | } |
| 849 | |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 850 | static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode, |
| 851 | u8 drv_type) |
| 852 | { |
| 853 | struct mmc_command cmd = {0}; |
| 854 | struct mmc_request mrq = {NULL}; |
| 855 | struct mmc_host *mmc = host->mmc; |
| 856 | u8 val = ((drv_type << 4) | 2); |
| 857 | |
| 858 | cmd.opcode = MMC_SWITCH; |
| 859 | cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | |
| 860 | (EXT_CSD_HS_TIMING << 16) | |
| 861 | (val << 8) | |
| 862 | EXT_CSD_CMD_SET_NORMAL; |
| 863 | cmd.flags = MMC_CMD_AC | MMC_RSP_R1B; |
| 864 | /* 1 sec */ |
| 865 | cmd.busy_timeout = 1000 * 1000; |
| 866 | |
| 867 | memset(cmd.resp, 0, sizeof(cmd.resp)); |
| 868 | cmd.retries = 3; |
| 869 | |
| 870 | mrq.cmd = &cmd; |
| 871 | cmd.data = NULL; |
| 872 | |
| 873 | mmc_wait_for_req(mmc, &mrq); |
| 874 | pr_debug("%s: %s: set card drive type to %d\n", |
| 875 | mmc_hostname(mmc), __func__, |
| 876 | drv_type); |
| 877 | } |
| 878 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 879 | int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode) |
| 880 | { |
| 881 | unsigned long flags; |
Sahitya Tummala | 9fe1653 | 2013-06-13 10:36:57 +0530 | [diff] [blame] | 882 | int tuning_seq_cnt = 3; |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 883 | u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 884 | const u32 *tuning_block_pattern = tuning_block_64; |
| 885 | int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */ |
| 886 | int rc; |
| 887 | struct mmc_host *mmc = host->mmc; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 888 | struct mmc_ios ios = host->mmc->ios; |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 889 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 890 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 891 | u8 drv_type = 0; |
| 892 | bool drv_type_changed = false; |
| 893 | struct mmc_card *card = host->mmc->card; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 894 | |
| 895 | /* |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 896 | * Tuning is required for SDR104, HS200 and HS400 cards and |
| 897 | * if clock frequency is greater than 100MHz in these modes. |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 898 | */ |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 899 | if (host->clock <= CORE_FREQ_100MHZ || |
| 900 | !((ios.timing == MMC_TIMING_MMC_HS400) || |
| 901 | (ios.timing == MMC_TIMING_MMC_HS200) || |
| 902 | (ios.timing == MMC_TIMING_UHS_SDR104))) |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 903 | return 0; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 904 | |
| 905 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 906 | |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 907 | /* CDC/SDC4 DLL HW calibration is only required for HS400 mode*/ |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 908 | if (msm_host->tuning_done && !msm_host->calibration_done && |
| 909 | (mmc->ios.timing == MMC_TIMING_MMC_HS400)) { |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 910 | rc = sdhci_msm_hs400_dll_calibration(host); |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 911 | spin_lock_irqsave(&host->lock, flags); |
| 912 | if (!rc) |
| 913 | msm_host->calibration_done = true; |
| 914 | spin_unlock_irqrestore(&host->lock, flags); |
| 915 | goto out; |
| 916 | } |
| 917 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 918 | spin_lock_irqsave(&host->lock, flags); |
| 919 | |
| 920 | if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) && |
| 921 | (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) { |
| 922 | tuning_block_pattern = tuning_block_128; |
| 923 | size = sizeof(tuning_block_128); |
| 924 | } |
| 925 | spin_unlock_irqrestore(&host->lock, flags); |
| 926 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 927 | data_buf = kmalloc(size, GFP_KERNEL); |
| 928 | if (!data_buf) { |
| 929 | rc = -ENOMEM; |
| 930 | goto out; |
| 931 | } |
| 932 | |
Sahitya Tummala | 9fe1653 | 2013-06-13 10:36:57 +0530 | [diff] [blame] | 933 | retry: |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 934 | tuned_phase_cnt = 0; |
| 935 | |
Sahitya Tummala | 9fe1653 | 2013-06-13 10:36:57 +0530 | [diff] [blame] | 936 | /* first of all reset the tuning block */ |
| 937 | rc = msm_init_cm_dll(host); |
| 938 | if (rc) |
| 939 | goto kfree; |
| 940 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 941 | phase = 0; |
| 942 | do { |
| 943 | struct mmc_command cmd = {0}; |
| 944 | struct mmc_data data = {0}; |
| 945 | struct mmc_request mrq = { |
| 946 | .cmd = &cmd, |
| 947 | .data = &data |
| 948 | }; |
| 949 | struct scatterlist sg; |
| 950 | |
| 951 | /* set the phase in delay line hw block */ |
| 952 | rc = msm_config_cm_dll_phase(host, phase); |
| 953 | if (rc) |
| 954 | goto kfree; |
| 955 | |
| 956 | cmd.opcode = opcode; |
| 957 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 958 | |
| 959 | data.blksz = size; |
| 960 | data.blocks = 1; |
| 961 | data.flags = MMC_DATA_READ; |
| 962 | data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */ |
| 963 | |
| 964 | data.sg = &sg; |
| 965 | data.sg_len = 1; |
| 966 | sg_init_one(&sg, data_buf, size); |
| 967 | memset(data_buf, 0, size); |
| 968 | mmc_wait_for_req(mmc, &mrq); |
| 969 | |
| 970 | if (!cmd.error && !data.error && |
| 971 | !memcmp(data_buf, tuning_block_pattern, size)) { |
| 972 | /* tuning is successful at this tuning point */ |
| 973 | tuned_phases[tuned_phase_cnt++] = phase; |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 974 | pr_debug("%s: %s: found *** good *** phase = %d\n", |
| 975 | mmc_hostname(mmc), __func__, phase); |
| 976 | } else { |
| 977 | pr_debug("%s: %s: found ## bad ## phase = %d\n", |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 978 | mmc_hostname(mmc), __func__, phase); |
| 979 | } |
| 980 | } while (++phase < 16); |
| 981 | |
Sahitya Tummala | dfdb4af | 2014-04-01 14:29:13 +0530 | [diff] [blame] | 982 | if ((tuned_phase_cnt == NUM_TUNING_PHASES) && |
| 983 | card && mmc_card_mmc(card)) { |
Krishna Konda | 96e6b11 | 2013-10-28 15:25:03 -0700 | [diff] [blame] | 984 | /* |
| 985 | * If all phases pass then its a problem. So change the card's |
| 986 | * drive type to a different value, if supported and repeat |
| 987 | * tuning until at least one phase fails. Then set the original |
| 988 | * drive type back. |
| 989 | * |
| 990 | * If all the phases still pass after trying all possible |
| 991 | * drive types, then one of those 16 phases will be picked. |
| 992 | * This is no different from what was going on before the |
| 993 | * modification to change drive type and retune. |
| 994 | */ |
| 995 | pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc), |
| 996 | tuned_phase_cnt); |
| 997 | |
| 998 | /* set drive type to other value . default setting is 0x0 */ |
| 999 | while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) { |
| 1000 | if (card->ext_csd.raw_driver_strength & |
| 1001 | (1 << drv_type)) { |
| 1002 | sdhci_msm_set_mmc_drv_type(host, opcode, |
| 1003 | drv_type); |
| 1004 | if (!drv_type_changed) |
| 1005 | drv_type_changed = true; |
| 1006 | goto retry; |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | /* reset drive type to default (50 ohm) if changed */ |
| 1012 | if (drv_type_changed) |
| 1013 | sdhci_msm_set_mmc_drv_type(host, opcode, 0); |
| 1014 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1015 | if (tuned_phase_cnt) { |
| 1016 | rc = msm_find_most_appropriate_phase(host, tuned_phases, |
| 1017 | tuned_phase_cnt); |
| 1018 | if (rc < 0) |
| 1019 | goto kfree; |
| 1020 | else |
| 1021 | phase = (u8)rc; |
| 1022 | |
| 1023 | /* |
| 1024 | * Finally set the selected phase in delay |
| 1025 | * line hw block. |
| 1026 | */ |
| 1027 | rc = msm_config_cm_dll_phase(host, phase); |
| 1028 | if (rc) |
| 1029 | goto kfree; |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 1030 | msm_host->saved_tuning_phase = phase; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1031 | pr_debug("%s: %s: finally setting the tuning phase to %d\n", |
| 1032 | mmc_hostname(mmc), __func__, phase); |
| 1033 | } else { |
Sahitya Tummala | 9fe1653 | 2013-06-13 10:36:57 +0530 | [diff] [blame] | 1034 | if (--tuning_seq_cnt) |
| 1035 | goto retry; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1036 | /* tuning failed */ |
| 1037 | pr_err("%s: %s: no tuning point found\n", |
| 1038 | mmc_hostname(mmc), __func__); |
Sahitya Tummala | 9fe1653 | 2013-06-13 10:36:57 +0530 | [diff] [blame] | 1039 | rc = -EIO; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | kfree: |
| 1043 | kfree(data_buf); |
| 1044 | out: |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 1045 | spin_lock_irqsave(&host->lock, flags); |
| 1046 | if (!rc) |
| 1047 | msm_host->tuning_done = true; |
| 1048 | spin_unlock_irqrestore(&host->lock, flags); |
| 1049 | pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc); |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1050 | return rc; |
| 1051 | } |
| 1052 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1053 | static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable) |
| 1054 | { |
| 1055 | struct sdhci_msm_gpio_data *curr; |
| 1056 | int i, ret = 0; |
| 1057 | |
| 1058 | curr = pdata->pin_data->gpio_data; |
| 1059 | for (i = 0; i < curr->size; i++) { |
| 1060 | if (!gpio_is_valid(curr->gpio[i].no)) { |
| 1061 | ret = -EINVAL; |
| 1062 | pr_err("%s: Invalid gpio = %d\n", __func__, |
| 1063 | curr->gpio[i].no); |
| 1064 | goto free_gpios; |
| 1065 | } |
| 1066 | if (enable) { |
| 1067 | ret = gpio_request(curr->gpio[i].no, |
| 1068 | curr->gpio[i].name); |
| 1069 | if (ret) { |
| 1070 | pr_err("%s: gpio_request(%d, %s) failed %d\n", |
| 1071 | __func__, curr->gpio[i].no, |
| 1072 | curr->gpio[i].name, ret); |
| 1073 | goto free_gpios; |
| 1074 | } |
| 1075 | curr->gpio[i].is_enabled = true; |
| 1076 | } else { |
| 1077 | gpio_free(curr->gpio[i].no); |
| 1078 | curr->gpio[i].is_enabled = false; |
| 1079 | } |
| 1080 | } |
| 1081 | return ret; |
| 1082 | |
| 1083 | free_gpios: |
| 1084 | for (i--; i >= 0; i--) { |
| 1085 | gpio_free(curr->gpio[i].no); |
| 1086 | curr->gpio[i].is_enabled = false; |
| 1087 | } |
| 1088 | return ret; |
| 1089 | } |
| 1090 | |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1091 | static int sdhci_msm_setup_pinctrl(struct sdhci_msm_pltfm_data *pdata, |
| 1092 | bool enable) |
| 1093 | { |
| 1094 | int ret = 0; |
| 1095 | |
| 1096 | if (enable) |
| 1097 | ret = pinctrl_select_state(pdata->pctrl_data->pctrl, |
| 1098 | pdata->pctrl_data->pins_active); |
| 1099 | else |
| 1100 | ret = pinctrl_select_state(pdata->pctrl_data->pctrl, |
| 1101 | pdata->pctrl_data->pins_sleep); |
| 1102 | |
| 1103 | if (ret < 0) |
| 1104 | pr_err("%s state for pinctrl failed with %d\n", |
| 1105 | enable ? "Enabling" : "Disabling", ret); |
| 1106 | |
| 1107 | return ret; |
| 1108 | } |
| 1109 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1110 | static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable) |
| 1111 | { |
| 1112 | int ret = 0; |
| 1113 | |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1114 | if (pdata->pin_cfg_sts == enable) { |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1115 | return 0; |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1116 | } else if (pdata->pctrl_data) { |
| 1117 | ret = sdhci_msm_setup_pinctrl(pdata, enable); |
| 1118 | goto out; |
| 1119 | } else if (!pdata->pin_data) { |
| 1120 | return 0; |
| 1121 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1122 | |
Sahitya Tummala | 1cd7e07 | 2014-02-14 13:19:01 +0530 | [diff] [blame] | 1123 | if (pdata->pin_data->is_gpio) |
| 1124 | ret = sdhci_msm_setup_gpio(pdata, enable); |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1125 | out: |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1126 | if (!ret) |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1127 | pdata->pin_cfg_sts = enable; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1128 | |
| 1129 | return ret; |
| 1130 | } |
| 1131 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1132 | static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name, |
| 1133 | u32 **out, int *len, u32 size) |
| 1134 | { |
| 1135 | int ret = 0; |
| 1136 | struct device_node *np = dev->of_node; |
| 1137 | size_t sz; |
| 1138 | u32 *arr = NULL; |
| 1139 | |
| 1140 | if (!of_get_property(np, prop_name, len)) { |
| 1141 | ret = -EINVAL; |
| 1142 | goto out; |
| 1143 | } |
| 1144 | sz = *len = *len / sizeof(*arr); |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 1145 | if (sz <= 0 || (size > 0 && (sz > size))) { |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1146 | dev_err(dev, "%s invalid size\n", prop_name); |
| 1147 | ret = -EINVAL; |
| 1148 | goto out; |
| 1149 | } |
| 1150 | |
| 1151 | arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL); |
| 1152 | if (!arr) { |
| 1153 | dev_err(dev, "%s failed allocating memory\n", prop_name); |
| 1154 | ret = -ENOMEM; |
| 1155 | goto out; |
| 1156 | } |
| 1157 | |
| 1158 | ret = of_property_read_u32_array(np, prop_name, arr, sz); |
| 1159 | if (ret < 0) { |
| 1160 | dev_err(dev, "%s failed reading array %d\n", prop_name, ret); |
| 1161 | goto out; |
| 1162 | } |
| 1163 | *out = arr; |
| 1164 | out: |
| 1165 | if (ret) |
| 1166 | *len = 0; |
| 1167 | return ret; |
| 1168 | } |
| 1169 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1170 | #define MAX_PROP_SIZE 32 |
| 1171 | static int sdhci_msm_dt_parse_vreg_info(struct device *dev, |
| 1172 | struct sdhci_msm_reg_data **vreg_data, const char *vreg_name) |
| 1173 | { |
| 1174 | int len, ret = 0; |
| 1175 | const __be32 *prop; |
| 1176 | char prop_name[MAX_PROP_SIZE]; |
| 1177 | struct sdhci_msm_reg_data *vreg; |
| 1178 | struct device_node *np = dev->of_node; |
| 1179 | |
| 1180 | snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name); |
| 1181 | if (!of_parse_phandle(np, prop_name, 0)) { |
Asutosh Das | c58cc7a | 2013-06-28 15:03:44 +0530 | [diff] [blame] | 1182 | dev_info(dev, "No vreg data found for %s\n", vreg_name); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1183 | return ret; |
| 1184 | } |
| 1185 | |
| 1186 | vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL); |
| 1187 | if (!vreg) { |
| 1188 | dev_err(dev, "No memory for vreg: %s\n", vreg_name); |
| 1189 | ret = -ENOMEM; |
| 1190 | return ret; |
| 1191 | } |
| 1192 | |
| 1193 | vreg->name = vreg_name; |
| 1194 | |
| 1195 | snprintf(prop_name, MAX_PROP_SIZE, |
| 1196 | "qcom,%s-always-on", vreg_name); |
| 1197 | if (of_get_property(np, prop_name, NULL)) |
| 1198 | vreg->is_always_on = true; |
| 1199 | |
| 1200 | snprintf(prop_name, MAX_PROP_SIZE, |
| 1201 | "qcom,%s-lpm-sup", vreg_name); |
| 1202 | if (of_get_property(np, prop_name, NULL)) |
| 1203 | vreg->lpm_sup = true; |
| 1204 | |
| 1205 | snprintf(prop_name, MAX_PROP_SIZE, |
| 1206 | "qcom,%s-voltage-level", vreg_name); |
| 1207 | prop = of_get_property(np, prop_name, &len); |
| 1208 | if (!prop || (len != (2 * sizeof(__be32)))) { |
| 1209 | dev_warn(dev, "%s %s property\n", |
| 1210 | prop ? "invalid format" : "no", prop_name); |
| 1211 | } else { |
| 1212 | vreg->low_vol_level = be32_to_cpup(&prop[0]); |
| 1213 | vreg->high_vol_level = be32_to_cpup(&prop[1]); |
| 1214 | } |
| 1215 | |
| 1216 | snprintf(prop_name, MAX_PROP_SIZE, |
| 1217 | "qcom,%s-current-level", vreg_name); |
| 1218 | prop = of_get_property(np, prop_name, &len); |
| 1219 | if (!prop || (len != (2 * sizeof(__be32)))) { |
| 1220 | dev_warn(dev, "%s %s property\n", |
| 1221 | prop ? "invalid format" : "no", prop_name); |
| 1222 | } else { |
| 1223 | vreg->lpm_uA = be32_to_cpup(&prop[0]); |
| 1224 | vreg->hpm_uA = be32_to_cpup(&prop[1]); |
| 1225 | } |
| 1226 | |
| 1227 | *vreg_data = vreg; |
| 1228 | dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n", |
| 1229 | vreg->name, vreg->is_always_on ? "always_on," : "", |
| 1230 | vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level, |
| 1231 | vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA); |
| 1232 | |
| 1233 | return ret; |
| 1234 | } |
| 1235 | |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1236 | static int sdhci_msm_parse_pinctrl_info(struct device *dev, |
| 1237 | struct sdhci_msm_pltfm_data *pdata) |
| 1238 | { |
| 1239 | struct sdhci_pinctrl_data *pctrl_data; |
| 1240 | struct pinctrl *pctrl; |
| 1241 | int ret = 0; |
| 1242 | |
| 1243 | /* Try to obtain pinctrl handle */ |
| 1244 | pctrl = devm_pinctrl_get(dev); |
| 1245 | if (IS_ERR(pctrl)) { |
| 1246 | ret = PTR_ERR(pctrl); |
| 1247 | goto out; |
| 1248 | } |
| 1249 | pctrl_data = devm_kzalloc(dev, sizeof(*pctrl_data), GFP_KERNEL); |
| 1250 | if (!pctrl_data) { |
| 1251 | dev_err(dev, "No memory for sdhci_pinctrl_data\n"); |
| 1252 | ret = -ENOMEM; |
| 1253 | goto out; |
| 1254 | } |
| 1255 | pctrl_data->pctrl = pctrl; |
| 1256 | /* Look-up and keep the states handy to be used later */ |
| 1257 | pctrl_data->pins_active = pinctrl_lookup_state( |
| 1258 | pctrl_data->pctrl, "active"); |
| 1259 | if (IS_ERR(pctrl_data->pins_active)) { |
| 1260 | ret = PTR_ERR(pctrl_data->pins_active); |
| 1261 | dev_err(dev, "Could not get active pinstates, err:%d\n", ret); |
| 1262 | goto out; |
| 1263 | } |
| 1264 | pctrl_data->pins_sleep = pinctrl_lookup_state( |
| 1265 | pctrl_data->pctrl, "sleep"); |
| 1266 | if (IS_ERR(pctrl_data->pins_sleep)) { |
| 1267 | ret = PTR_ERR(pctrl_data->pins_sleep); |
| 1268 | dev_err(dev, "Could not get sleep pinstates, err:%d\n", ret); |
| 1269 | goto out; |
| 1270 | } |
| 1271 | pdata->pctrl_data = pctrl_data; |
| 1272 | out: |
| 1273 | return ret; |
| 1274 | } |
| 1275 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1276 | #define GPIO_NAME_MAX_LEN 32 |
| 1277 | static int sdhci_msm_dt_parse_gpio_info(struct device *dev, |
| 1278 | struct sdhci_msm_pltfm_data *pdata) |
| 1279 | { |
| 1280 | int ret = 0, cnt, i; |
| 1281 | struct sdhci_msm_pin_data *pin_data; |
| 1282 | struct device_node *np = dev->of_node; |
| 1283 | |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1284 | ret = sdhci_msm_parse_pinctrl_info(dev, pdata); |
| 1285 | if (!ret) { |
| 1286 | goto out; |
| 1287 | } else if (ret == -EPROBE_DEFER) { |
| 1288 | dev_err(dev, "Pinctrl framework not registered, err:%d\n", ret); |
| 1289 | goto out; |
| 1290 | } else { |
| 1291 | dev_err(dev, "Parsing Pinctrl failed with %d, falling back on GPIO lib\n", |
| 1292 | ret); |
Sahitya Tummala | 1cd7e07 | 2014-02-14 13:19:01 +0530 | [diff] [blame] | 1293 | ret = 0; |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 1294 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1295 | pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL); |
| 1296 | if (!pin_data) { |
| 1297 | dev_err(dev, "No memory for pin_data\n"); |
| 1298 | ret = -ENOMEM; |
| 1299 | goto out; |
| 1300 | } |
| 1301 | |
| 1302 | cnt = of_gpio_count(np); |
| 1303 | if (cnt > 0) { |
| 1304 | pin_data->gpio_data = devm_kzalloc(dev, |
| 1305 | sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL); |
| 1306 | if (!pin_data->gpio_data) { |
| 1307 | dev_err(dev, "No memory for gpio_data\n"); |
| 1308 | ret = -ENOMEM; |
| 1309 | goto out; |
| 1310 | } |
| 1311 | pin_data->gpio_data->size = cnt; |
| 1312 | pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt * |
| 1313 | sizeof(struct sdhci_msm_gpio), GFP_KERNEL); |
| 1314 | |
| 1315 | if (!pin_data->gpio_data->gpio) { |
| 1316 | dev_err(dev, "No memory for gpio\n"); |
| 1317 | ret = -ENOMEM; |
| 1318 | goto out; |
| 1319 | } |
| 1320 | |
| 1321 | for (i = 0; i < cnt; i++) { |
| 1322 | const char *name = NULL; |
| 1323 | char result[GPIO_NAME_MAX_LEN]; |
| 1324 | pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i); |
| 1325 | of_property_read_string_index(np, |
| 1326 | "qcom,gpio-names", i, &name); |
| 1327 | |
| 1328 | snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s", |
| 1329 | dev_name(dev), name ? name : "?"); |
| 1330 | pin_data->gpio_data->gpio[i].name = result; |
| 1331 | dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__, |
| 1332 | pin_data->gpio_data->gpio[i].name, |
| 1333 | pin_data->gpio_data->gpio[i].no); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1334 | } |
| 1335 | } |
Sahitya Tummala | 1cd7e07 | 2014-02-14 13:19:01 +0530 | [diff] [blame] | 1336 | pdata->pin_data = pin_data; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1337 | out: |
| 1338 | if (ret) |
| 1339 | dev_err(dev, "%s failed with err %d\n", __func__, ret); |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
Maya Erez | 994cf2f | 2014-10-21 20:22:04 +0300 | [diff] [blame] | 1343 | #ifdef CONFIG_SMP |
| 1344 | static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata, |
| 1345 | struct device_node *np) |
| 1346 | { |
| 1347 | const char *cpu_affinity = NULL; |
| 1348 | |
| 1349 | pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ; |
| 1350 | if (!of_property_read_string(np, "qcom,cpu-affinity", |
| 1351 | &cpu_affinity)) { |
| 1352 | if (!strcmp(cpu_affinity, "all_cores")) |
| 1353 | pdata->cpu_affinity_type = PM_QOS_REQ_ALL_CORES; |
| 1354 | else if (!strcmp(cpu_affinity, "affine_cores")) |
| 1355 | pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_CORES; |
| 1356 | else if (!strcmp(cpu_affinity, "affine_irq")) |
| 1357 | pdata->cpu_affinity_type = PM_QOS_REQ_AFFINE_IRQ; |
| 1358 | } |
| 1359 | } |
| 1360 | #else |
| 1361 | static void sdhci_msm_populate_affinity_type(struct sdhci_msm_pltfm_data *pdata, |
| 1362 | struct device_node *np) |
| 1363 | { |
| 1364 | } |
| 1365 | #endif |
| 1366 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1367 | /* Parse platform data */ |
Dov Levenglick | c9033ab | 2015-03-10 16:00:56 +0200 | [diff] [blame] | 1368 | static |
| 1369 | struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev, |
| 1370 | struct sdhci_msm_host *msm_host) |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1371 | { |
| 1372 | struct sdhci_msm_pltfm_data *pdata = NULL; |
| 1373 | struct device_node *np = dev->of_node; |
| 1374 | u32 bus_width = 0; |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1375 | u32 cpu_dma_latency; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1376 | int len, i; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1377 | int clk_table_len; |
| 1378 | u32 *clk_table = NULL; |
Sujit Reddy Thumma | 1958d3d | 2013-06-03 09:54:32 +0530 | [diff] [blame] | 1379 | enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1380 | |
| 1381 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 1382 | if (!pdata) { |
| 1383 | dev_err(dev, "failed to allocate memory for platform data\n"); |
| 1384 | goto out; |
| 1385 | } |
| 1386 | |
Sujit Reddy Thumma | 1958d3d | 2013-06-03 09:54:32 +0530 | [diff] [blame] | 1387 | pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags); |
| 1388 | if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW)) |
| 1389 | pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH; |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 1390 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1391 | of_property_read_u32(np, "qcom,bus-width", &bus_width); |
| 1392 | if (bus_width == 8) |
| 1393 | pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA; |
| 1394 | else if (bus_width == 4) |
| 1395 | pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA; |
| 1396 | else { |
| 1397 | dev_notice(dev, "invalid bus-width, default to 1-bit mode\n"); |
| 1398 | pdata->mmc_bus_width = 0; |
| 1399 | } |
| 1400 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 1401 | if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us", |
| 1402 | &cpu_dma_latency)) |
| 1403 | pdata->cpu_dma_latency_us = cpu_dma_latency; |
Asutosh Das | 598a4d4 | 2014-02-05 16:38:23 +0530 | [diff] [blame] | 1404 | else |
| 1405 | pdata->cpu_dma_latency_us = MSM_MMC_DEFAULT_CPU_DMA_LATENCY; |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 1406 | |
| 1407 | if (sdhci_msm_dt_get_array(dev, "qcom,devfreq,freq-table", |
| 1408 | &msm_host->mmc->clk_scaling.freq_table, |
| 1409 | &msm_host->mmc->clk_scaling.freq_table_sz, 0)) |
| 1410 | pr_debug("%s: no clock scaling frequencies were supplied\n", |
| 1411 | dev_name(dev)); |
| 1412 | else if (!msm_host->mmc->clk_scaling.freq_table || |
| 1413 | !msm_host->mmc->clk_scaling.freq_table_sz) |
| 1414 | dev_err(dev, "bad dts clock scaling frequencies\n"); |
| 1415 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1416 | if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates", |
| 1417 | &clk_table, &clk_table_len, 0)) { |
| 1418 | dev_err(dev, "failed parsing supported clock rates\n"); |
| 1419 | goto out; |
| 1420 | } |
| 1421 | if (!clk_table || !clk_table_len) { |
| 1422 | dev_err(dev, "Invalid clock table\n"); |
| 1423 | goto out; |
| 1424 | } |
| 1425 | pdata->sup_clk_table = clk_table; |
| 1426 | pdata->sup_clk_cnt = clk_table_len; |
| 1427 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1428 | pdata->vreg_data = devm_kzalloc(dev, sizeof(struct |
| 1429 | sdhci_msm_slot_reg_data), |
| 1430 | GFP_KERNEL); |
| 1431 | if (!pdata->vreg_data) { |
| 1432 | dev_err(dev, "failed to allocate memory for vreg data\n"); |
| 1433 | goto out; |
| 1434 | } |
| 1435 | |
| 1436 | if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data, |
| 1437 | "vdd")) { |
| 1438 | dev_err(dev, "failed parsing vdd data\n"); |
| 1439 | goto out; |
| 1440 | } |
| 1441 | if (sdhci_msm_dt_parse_vreg_info(dev, |
| 1442 | &pdata->vreg_data->vdd_io_data, |
| 1443 | "vdd-io")) { |
| 1444 | dev_err(dev, "failed parsing vdd-io data\n"); |
| 1445 | goto out; |
| 1446 | } |
| 1447 | |
| 1448 | if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) { |
| 1449 | dev_err(dev, "failed parsing gpio data\n"); |
| 1450 | goto out; |
| 1451 | } |
| 1452 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1453 | len = of_property_count_strings(np, "qcom,bus-speed-mode"); |
| 1454 | |
| 1455 | for (i = 0; i < len; i++) { |
| 1456 | const char *name = NULL; |
| 1457 | |
| 1458 | of_property_read_string_index(np, |
| 1459 | "qcom,bus-speed-mode", i, &name); |
| 1460 | if (!name) |
| 1461 | continue; |
| 1462 | |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 1463 | if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v"))) |
| 1464 | pdata->caps2 |= MMC_CAP2_HS400_1_8V; |
| 1465 | else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v"))) |
| 1466 | pdata->caps2 |= MMC_CAP2_HS400_1_2V; |
| 1467 | else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v"))) |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1468 | pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR; |
| 1469 | else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v"))) |
| 1470 | pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR; |
| 1471 | else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v"))) |
| 1472 | pdata->caps |= MMC_CAP_1_8V_DDR |
| 1473 | | MMC_CAP_UHS_DDR50; |
| 1474 | else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v"))) |
| 1475 | pdata->caps |= MMC_CAP_1_2V_DDR |
| 1476 | | MMC_CAP_UHS_DDR50; |
| 1477 | } |
| 1478 | |
| 1479 | if (of_get_property(np, "qcom,nonremovable", NULL)) |
| 1480 | pdata->nonremovable = true; |
| 1481 | |
Guoping Yu | f7c9133 | 2014-08-20 16:56:18 +0800 | [diff] [blame] | 1482 | if (of_get_property(np, "qcom,nonhotplug", NULL)) |
| 1483 | pdata->nonhotplug = true; |
| 1484 | |
Venkat Gopalakrishnan | 9a62e04 | 2015-03-03 16:14:55 -0800 | [diff] [blame] | 1485 | pdata->largeaddressbus = |
| 1486 | of_property_read_bool(np, "qcom,large-address-bus"); |
| 1487 | |
Maya Erez | 994cf2f | 2014-10-21 20:22:04 +0300 | [diff] [blame] | 1488 | sdhci_msm_populate_affinity_type(pdata, np); |
| 1489 | |
Dov Levenglick | c9033ab | 2015-03-10 16:00:56 +0200 | [diff] [blame] | 1490 | if (of_property_read_bool(np, "qcom,wakeup-on-idle")) |
| 1491 | msm_host->mmc->wakeup_on_idle = true; |
| 1492 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1493 | return pdata; |
| 1494 | out: |
| 1495 | return NULL; |
| 1496 | } |
| 1497 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1498 | /* Returns required bandwidth in Bytes per Sec */ |
| 1499 | static unsigned int sdhci_get_bw_required(struct sdhci_host *host, |
| 1500 | struct mmc_ios *ios) |
| 1501 | { |
Sahitya Tummala | 2886c92 | 2013-04-03 18:03:31 +0530 | [diff] [blame] | 1502 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1503 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1504 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1505 | unsigned int bw; |
| 1506 | |
Sahitya Tummala | 2886c92 | 2013-04-03 18:03:31 +0530 | [diff] [blame] | 1507 | bw = msm_host->clk_rate; |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1508 | /* |
| 1509 | * For DDR mode, SDCC controller clock will be at |
| 1510 | * the double rate than the actual clock that goes to card. |
| 1511 | */ |
| 1512 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 1513 | bw /= 2; |
| 1514 | else if (ios->bus_width == MMC_BUS_WIDTH_1) |
| 1515 | bw /= 8; |
| 1516 | |
| 1517 | return bw; |
| 1518 | } |
| 1519 | |
| 1520 | static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host, |
| 1521 | unsigned int bw) |
| 1522 | { |
| 1523 | unsigned int *table = host->pdata->voting_data->bw_vecs; |
| 1524 | unsigned int size = host->pdata->voting_data->bw_vecs_size; |
| 1525 | int i; |
| 1526 | |
| 1527 | if (host->msm_bus_vote.is_max_bw_needed && bw) |
| 1528 | return host->msm_bus_vote.max_bw_vote; |
| 1529 | |
| 1530 | for (i = 0; i < size; i++) { |
| 1531 | if (bw <= table[i]) |
| 1532 | break; |
| 1533 | } |
| 1534 | |
| 1535 | if (i && (i == size)) |
| 1536 | i--; |
| 1537 | |
| 1538 | return i; |
| 1539 | } |
| 1540 | |
| 1541 | /* |
| 1542 | * This function must be called with host lock acquired. |
| 1543 | * Caller of this function should also ensure that msm bus client |
| 1544 | * handle is not null. |
| 1545 | */ |
| 1546 | static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host, |
| 1547 | int vote, |
Sujit Reddy Thumma | 024c058 | 2013-08-06 11:21:33 +0530 | [diff] [blame] | 1548 | unsigned long *flags) |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1549 | { |
| 1550 | struct sdhci_host *host = platform_get_drvdata(msm_host->pdev); |
| 1551 | int rc = 0; |
| 1552 | |
Sujit Reddy Thumma | 024c058 | 2013-08-06 11:21:33 +0530 | [diff] [blame] | 1553 | BUG_ON(!flags); |
| 1554 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1555 | if (vote != msm_host->msm_bus_vote.curr_vote) { |
Sujit Reddy Thumma | 024c058 | 2013-08-06 11:21:33 +0530 | [diff] [blame] | 1556 | spin_unlock_irqrestore(&host->lock, *flags); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1557 | rc = msm_bus_scale_client_update_request( |
| 1558 | msm_host->msm_bus_vote.client_handle, vote); |
Sujit Reddy Thumma | 024c058 | 2013-08-06 11:21:33 +0530 | [diff] [blame] | 1559 | spin_lock_irqsave(&host->lock, *flags); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1560 | if (rc) { |
| 1561 | pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n", |
| 1562 | mmc_hostname(host->mmc), |
| 1563 | msm_host->msm_bus_vote.client_handle, vote, rc); |
| 1564 | goto out; |
| 1565 | } |
| 1566 | msm_host->msm_bus_vote.curr_vote = vote; |
| 1567 | } |
| 1568 | out: |
| 1569 | return rc; |
| 1570 | } |
| 1571 | |
| 1572 | /* |
| 1573 | * Internal work. Work to set 0 bandwidth for msm bus. |
| 1574 | */ |
| 1575 | static void sdhci_msm_bus_work(struct work_struct *work) |
| 1576 | { |
| 1577 | struct sdhci_msm_host *msm_host; |
| 1578 | struct sdhci_host *host; |
| 1579 | unsigned long flags; |
| 1580 | |
| 1581 | msm_host = container_of(work, struct sdhci_msm_host, |
| 1582 | msm_bus_vote.vote_work.work); |
| 1583 | host = platform_get_drvdata(msm_host->pdev); |
| 1584 | |
| 1585 | if (!msm_host->msm_bus_vote.client_handle) |
| 1586 | return; |
| 1587 | |
| 1588 | spin_lock_irqsave(&host->lock, flags); |
| 1589 | /* don't vote for 0 bandwidth if any request is in progress */ |
| 1590 | if (!host->mrq) { |
| 1591 | sdhci_msm_bus_set_vote(msm_host, |
Sujit Reddy Thumma | 024c058 | 2013-08-06 11:21:33 +0530 | [diff] [blame] | 1592 | msm_host->msm_bus_vote.min_bw_vote, &flags); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1593 | } else |
| 1594 | pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n", |
| 1595 | mmc_hostname(host->mmc), __func__); |
| 1596 | spin_unlock_irqrestore(&host->lock, flags); |
| 1597 | } |
| 1598 | |
| 1599 | /* |
| 1600 | * This function cancels any scheduled delayed work and sets the bus |
| 1601 | * vote based on bw (bandwidth) argument. |
| 1602 | */ |
| 1603 | static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host, |
| 1604 | unsigned int bw) |
| 1605 | { |
| 1606 | int vote; |
| 1607 | unsigned long flags; |
| 1608 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1609 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1610 | |
| 1611 | cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work); |
| 1612 | spin_lock_irqsave(&host->lock, flags); |
| 1613 | vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw); |
Sujit Reddy Thumma | 024c058 | 2013-08-06 11:21:33 +0530 | [diff] [blame] | 1614 | sdhci_msm_bus_set_vote(msm_host, vote, &flags); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1615 | spin_unlock_irqrestore(&host->lock, flags); |
| 1616 | } |
| 1617 | |
| 1618 | #define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */ |
| 1619 | |
| 1620 | /* This function queues a work which will set the bandwidth requiement to 0 */ |
| 1621 | static void sdhci_msm_bus_queue_work(struct sdhci_host *host) |
| 1622 | { |
| 1623 | unsigned long flags; |
| 1624 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1625 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1626 | |
| 1627 | spin_lock_irqsave(&host->lock, flags); |
| 1628 | if (msm_host->msm_bus_vote.min_bw_vote != |
| 1629 | msm_host->msm_bus_vote.curr_vote) |
| 1630 | queue_delayed_work(system_wq, |
| 1631 | &msm_host->msm_bus_vote.vote_work, |
| 1632 | msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY)); |
| 1633 | spin_unlock_irqrestore(&host->lock, flags); |
| 1634 | } |
| 1635 | |
| 1636 | static int sdhci_msm_bus_register(struct sdhci_msm_host *host, |
| 1637 | struct platform_device *pdev) |
| 1638 | { |
| 1639 | int rc = 0; |
| 1640 | struct msm_bus_scale_pdata *bus_pdata; |
| 1641 | |
| 1642 | struct sdhci_msm_bus_voting_data *data; |
| 1643 | struct device *dev = &pdev->dev; |
| 1644 | |
| 1645 | data = devm_kzalloc(dev, |
| 1646 | sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL); |
| 1647 | if (!data) { |
| 1648 | dev_err(&pdev->dev, |
| 1649 | "%s: failed to allocate memory\n", __func__); |
| 1650 | rc = -ENOMEM; |
| 1651 | goto out; |
| 1652 | } |
| 1653 | data->bus_pdata = msm_bus_cl_get_pdata(pdev); |
| 1654 | if (data->bus_pdata) { |
| 1655 | rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps", |
| 1656 | &data->bw_vecs, &data->bw_vecs_size, 0); |
| 1657 | if (rc) { |
| 1658 | dev_err(&pdev->dev, |
| 1659 | "%s: Failed to get bus-bw-vectors-bps\n", |
| 1660 | __func__); |
| 1661 | goto out; |
| 1662 | } |
| 1663 | host->pdata->voting_data = data; |
| 1664 | } |
| 1665 | if (host->pdata->voting_data && |
| 1666 | host->pdata->voting_data->bus_pdata && |
| 1667 | host->pdata->voting_data->bw_vecs && |
| 1668 | host->pdata->voting_data->bw_vecs_size) { |
| 1669 | |
| 1670 | bus_pdata = host->pdata->voting_data->bus_pdata; |
| 1671 | host->msm_bus_vote.client_handle = |
| 1672 | msm_bus_scale_register_client(bus_pdata); |
| 1673 | if (!host->msm_bus_vote.client_handle) { |
| 1674 | dev_err(&pdev->dev, "msm_bus_scale_register_client()\n"); |
| 1675 | rc = -EFAULT; |
| 1676 | goto out; |
| 1677 | } |
| 1678 | /* cache the vote index for minimum and maximum bandwidth */ |
| 1679 | host->msm_bus_vote.min_bw_vote = |
| 1680 | sdhci_msm_bus_get_vote_for_bw(host, 0); |
| 1681 | host->msm_bus_vote.max_bw_vote = |
| 1682 | sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX); |
| 1683 | } else { |
| 1684 | devm_kfree(dev, data); |
| 1685 | } |
| 1686 | |
| 1687 | out: |
| 1688 | return rc; |
| 1689 | } |
| 1690 | |
| 1691 | static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host) |
| 1692 | { |
| 1693 | if (host->msm_bus_vote.client_handle) |
| 1694 | msm_bus_scale_unregister_client( |
| 1695 | host->msm_bus_vote.client_handle); |
| 1696 | } |
| 1697 | |
| 1698 | static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable) |
| 1699 | { |
| 1700 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1701 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1702 | struct mmc_ios *ios = &host->mmc->ios; |
| 1703 | unsigned int bw; |
| 1704 | |
| 1705 | if (!msm_host->msm_bus_vote.client_handle) |
| 1706 | return; |
| 1707 | |
| 1708 | bw = sdhci_get_bw_required(host, ios); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1709 | if (enable) { |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1710 | sdhci_msm_bus_cancel_work_and_set_vote(host, bw); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1711 | } else { |
| 1712 | /* |
| 1713 | * If clock gating is enabled, then remove the vote |
| 1714 | * immediately because clocks will be disabled only |
| 1715 | * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no |
| 1716 | * additional delay is required to remove the bus vote. |
| 1717 | */ |
| 1718 | #ifdef CONFIG_MMC_CLKGATE |
| 1719 | if (host->mmc->clkgate_delay) |
| 1720 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 1721 | else |
| 1722 | #endif |
| 1723 | sdhci_msm_bus_queue_work(host); |
| 1724 | } |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1725 | } |
| 1726 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1727 | /* Regulator utility functions */ |
| 1728 | static int sdhci_msm_vreg_init_reg(struct device *dev, |
| 1729 | struct sdhci_msm_reg_data *vreg) |
| 1730 | { |
| 1731 | int ret = 0; |
| 1732 | |
| 1733 | /* check if regulator is already initialized? */ |
| 1734 | if (vreg->reg) |
| 1735 | goto out; |
| 1736 | |
| 1737 | /* Get the regulator handle */ |
| 1738 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 1739 | if (IS_ERR(vreg->reg)) { |
| 1740 | ret = PTR_ERR(vreg->reg); |
| 1741 | pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n", |
| 1742 | __func__, vreg->name, ret); |
| 1743 | goto out; |
| 1744 | } |
| 1745 | |
Asutosh Das | c58cc7a | 2013-06-28 15:03:44 +0530 | [diff] [blame] | 1746 | if (regulator_count_voltages(vreg->reg) > 0) { |
| 1747 | vreg->set_voltage_sup = true; |
| 1748 | /* sanity check */ |
| 1749 | if (!vreg->high_vol_level || !vreg->hpm_uA) { |
| 1750 | pr_err("%s: %s invalid constraints specified\n", |
| 1751 | __func__, vreg->name); |
| 1752 | ret = -EINVAL; |
| 1753 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | out: |
| 1757 | return ret; |
| 1758 | } |
| 1759 | |
| 1760 | static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg) |
| 1761 | { |
| 1762 | if (vreg->reg) |
| 1763 | devm_regulator_put(vreg->reg); |
| 1764 | } |
| 1765 | |
| 1766 | static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data |
| 1767 | *vreg, int uA_load) |
| 1768 | { |
| 1769 | int ret = 0; |
| 1770 | |
| 1771 | /* |
| 1772 | * regulators that do not support regulator_set_voltage also |
| 1773 | * do not support regulator_set_optimum_mode |
| 1774 | */ |
| 1775 | if (vreg->set_voltage_sup) { |
| 1776 | ret = regulator_set_load(vreg->reg, uA_load); |
| 1777 | if (ret < 0) |
| 1778 | pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n", |
| 1779 | __func__, vreg->name, uA_load, ret); |
| 1780 | else |
| 1781 | /* |
| 1782 | * regulator_set_load() can return non zero |
| 1783 | * value even for success case. |
| 1784 | */ |
| 1785 | ret = 0; |
| 1786 | } |
| 1787 | return ret; |
| 1788 | } |
| 1789 | |
| 1790 | static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg, |
| 1791 | int min_uV, int max_uV) |
| 1792 | { |
| 1793 | int ret = 0; |
Asutosh Das | c58cc7a | 2013-06-28 15:03:44 +0530 | [diff] [blame] | 1794 | if (vreg->set_voltage_sup) { |
| 1795 | ret = regulator_set_voltage(vreg->reg, min_uV, max_uV); |
| 1796 | if (ret) { |
| 1797 | pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n", |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1798 | __func__, vreg->name, min_uV, max_uV, ret); |
| 1799 | } |
Asutosh Das | c58cc7a | 2013-06-28 15:03:44 +0530 | [diff] [blame] | 1800 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1801 | |
| 1802 | return ret; |
| 1803 | } |
| 1804 | |
| 1805 | static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg) |
| 1806 | { |
| 1807 | int ret = 0; |
| 1808 | |
| 1809 | /* Put regulator in HPM (high power mode) */ |
| 1810 | ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA); |
| 1811 | if (ret < 0) |
| 1812 | return ret; |
| 1813 | |
| 1814 | if (!vreg->is_enabled) { |
| 1815 | /* Set voltage level */ |
| 1816 | ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level, |
| 1817 | vreg->high_vol_level); |
| 1818 | if (ret) |
| 1819 | return ret; |
| 1820 | } |
| 1821 | ret = regulator_enable(vreg->reg); |
| 1822 | if (ret) { |
| 1823 | pr_err("%s: regulator_enable(%s) failed. ret=%d\n", |
| 1824 | __func__, vreg->name, ret); |
| 1825 | return ret; |
| 1826 | } |
| 1827 | vreg->is_enabled = true; |
| 1828 | return ret; |
| 1829 | } |
| 1830 | |
| 1831 | static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg) |
| 1832 | { |
| 1833 | int ret = 0; |
| 1834 | |
| 1835 | /* Never disable regulator marked as always_on */ |
| 1836 | if (vreg->is_enabled && !vreg->is_always_on) { |
| 1837 | ret = regulator_disable(vreg->reg); |
| 1838 | if (ret) { |
| 1839 | pr_err("%s: regulator_disable(%s) failed. ret=%d\n", |
| 1840 | __func__, vreg->name, ret); |
| 1841 | goto out; |
| 1842 | } |
| 1843 | vreg->is_enabled = false; |
| 1844 | |
| 1845 | ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0); |
| 1846 | if (ret < 0) |
| 1847 | goto out; |
| 1848 | |
| 1849 | /* Set min. voltage level to 0 */ |
| 1850 | ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level); |
| 1851 | if (ret) |
| 1852 | goto out; |
| 1853 | } else if (vreg->is_enabled && vreg->is_always_on) { |
| 1854 | if (vreg->lpm_sup) { |
| 1855 | /* Put always_on regulator in LPM (low power mode) */ |
| 1856 | ret = sdhci_msm_vreg_set_optimum_mode(vreg, |
| 1857 | vreg->lpm_uA); |
| 1858 | if (ret < 0) |
| 1859 | goto out; |
| 1860 | } |
| 1861 | } |
| 1862 | out: |
| 1863 | return ret; |
| 1864 | } |
| 1865 | |
| 1866 | static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata, |
| 1867 | bool enable, bool is_init) |
| 1868 | { |
| 1869 | int ret = 0, i; |
| 1870 | struct sdhci_msm_slot_reg_data *curr_slot; |
| 1871 | struct sdhci_msm_reg_data *vreg_table[2]; |
| 1872 | |
| 1873 | curr_slot = pdata->vreg_data; |
| 1874 | if (!curr_slot) { |
| 1875 | pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n", |
| 1876 | __func__); |
| 1877 | goto out; |
| 1878 | } |
| 1879 | |
| 1880 | vreg_table[0] = curr_slot->vdd_data; |
| 1881 | vreg_table[1] = curr_slot->vdd_io_data; |
| 1882 | |
| 1883 | for (i = 0; i < ARRAY_SIZE(vreg_table); i++) { |
| 1884 | if (vreg_table[i]) { |
| 1885 | if (enable) |
| 1886 | ret = sdhci_msm_vreg_enable(vreg_table[i]); |
| 1887 | else |
| 1888 | ret = sdhci_msm_vreg_disable(vreg_table[i]); |
| 1889 | if (ret) |
| 1890 | goto out; |
| 1891 | } |
| 1892 | } |
| 1893 | out: |
| 1894 | return ret; |
| 1895 | } |
| 1896 | |
| 1897 | /* |
| 1898 | * Reset vreg by ensuring it is off during probe. A call |
| 1899 | * to enable vreg is needed to balance disable vreg |
| 1900 | */ |
| 1901 | static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata) |
| 1902 | { |
| 1903 | int ret; |
| 1904 | |
| 1905 | ret = sdhci_msm_setup_vreg(pdata, 1, true); |
| 1906 | if (ret) |
| 1907 | return ret; |
| 1908 | ret = sdhci_msm_setup_vreg(pdata, 0, true); |
| 1909 | return ret; |
| 1910 | } |
| 1911 | |
| 1912 | /* This init function should be called only once for each SDHC slot */ |
| 1913 | static int sdhci_msm_vreg_init(struct device *dev, |
| 1914 | struct sdhci_msm_pltfm_data *pdata, |
| 1915 | bool is_init) |
| 1916 | { |
| 1917 | int ret = 0; |
| 1918 | struct sdhci_msm_slot_reg_data *curr_slot; |
| 1919 | struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg; |
| 1920 | |
| 1921 | curr_slot = pdata->vreg_data; |
| 1922 | if (!curr_slot) |
| 1923 | goto out; |
| 1924 | |
| 1925 | curr_vdd_reg = curr_slot->vdd_data; |
| 1926 | curr_vdd_io_reg = curr_slot->vdd_io_data; |
| 1927 | |
| 1928 | if (!is_init) |
| 1929 | /* Deregister all regulators from regulator framework */ |
| 1930 | goto vdd_io_reg_deinit; |
| 1931 | |
| 1932 | /* |
| 1933 | * Get the regulator handle from voltage regulator framework |
| 1934 | * and then try to set the voltage level for the regulator |
| 1935 | */ |
| 1936 | if (curr_vdd_reg) { |
| 1937 | ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg); |
| 1938 | if (ret) |
| 1939 | goto out; |
| 1940 | } |
| 1941 | if (curr_vdd_io_reg) { |
| 1942 | ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg); |
| 1943 | if (ret) |
| 1944 | goto vdd_reg_deinit; |
| 1945 | } |
| 1946 | ret = sdhci_msm_vreg_reset(pdata); |
| 1947 | if (ret) |
| 1948 | dev_err(dev, "vreg reset failed (%d)\n", ret); |
| 1949 | goto out; |
| 1950 | |
| 1951 | vdd_io_reg_deinit: |
| 1952 | if (curr_vdd_io_reg) |
| 1953 | sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg); |
| 1954 | vdd_reg_deinit: |
| 1955 | if (curr_vdd_reg) |
| 1956 | sdhci_msm_vreg_deinit_reg(curr_vdd_reg); |
| 1957 | out: |
| 1958 | return ret; |
| 1959 | } |
| 1960 | |
| 1961 | |
| 1962 | static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata, |
| 1963 | enum vdd_io_level level, |
| 1964 | unsigned int voltage_level) |
| 1965 | { |
| 1966 | int ret = 0; |
| 1967 | int set_level; |
| 1968 | struct sdhci_msm_reg_data *vdd_io_reg; |
| 1969 | |
| 1970 | if (!pdata->vreg_data) |
| 1971 | return ret; |
| 1972 | |
| 1973 | vdd_io_reg = pdata->vreg_data->vdd_io_data; |
| 1974 | if (vdd_io_reg && vdd_io_reg->is_enabled) { |
| 1975 | switch (level) { |
| 1976 | case VDD_IO_LOW: |
| 1977 | set_level = vdd_io_reg->low_vol_level; |
| 1978 | break; |
| 1979 | case VDD_IO_HIGH: |
| 1980 | set_level = vdd_io_reg->high_vol_level; |
| 1981 | break; |
| 1982 | case VDD_IO_SET_LEVEL: |
| 1983 | set_level = voltage_level; |
| 1984 | break; |
| 1985 | default: |
| 1986 | pr_err("%s: invalid argument level = %d", |
| 1987 | __func__, level); |
| 1988 | ret = -EINVAL; |
| 1989 | return ret; |
| 1990 | } |
| 1991 | ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level, |
| 1992 | set_level); |
| 1993 | } |
| 1994 | return ret; |
| 1995 | } |
| 1996 | |
| 1997 | static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data) |
| 1998 | { |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1999 | struct sdhci_host *host = (struct sdhci_host *)data; |
| 2000 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2001 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2002 | u8 irq_status = 0; |
| 2003 | u8 irq_ack = 0; |
| 2004 | int ret = 0; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2005 | int pwr_state = 0, io_level = 0; |
| 2006 | unsigned long flags; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2007 | |
| 2008 | irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS); |
| 2009 | pr_debug("%s: Received IRQ(%d), status=0x%x\n", |
| 2010 | mmc_hostname(msm_host->mmc), irq, irq_status); |
| 2011 | |
| 2012 | /* Clear the interrupt */ |
| 2013 | writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR)); |
| 2014 | /* |
| 2015 | * SDHC has core_mem and hc_mem device memory and these memory |
| 2016 | * addresses do not fall within 1KB region. Hence, any update to |
| 2017 | * core_mem address space would require an mb() to ensure this gets |
| 2018 | * completed before its next update to registers within hc_mem. |
| 2019 | */ |
| 2020 | mb(); |
| 2021 | |
| 2022 | /* Handle BUS ON/OFF*/ |
| 2023 | if (irq_status & CORE_PWRCTL_BUS_ON) { |
| 2024 | ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2025 | if (!ret) { |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2026 | ret = sdhci_msm_setup_pins(msm_host->pdata, true); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2027 | ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata, |
| 2028 | VDD_IO_HIGH, 0); |
| 2029 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2030 | if (ret) |
| 2031 | irq_ack |= CORE_PWRCTL_BUS_FAIL; |
| 2032 | else |
| 2033 | irq_ack |= CORE_PWRCTL_BUS_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2034 | |
| 2035 | pwr_state = REQ_BUS_ON; |
| 2036 | io_level = REQ_IO_HIGH; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2037 | } |
| 2038 | if (irq_status & CORE_PWRCTL_BUS_OFF) { |
| 2039 | ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2040 | if (!ret) { |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2041 | ret = sdhci_msm_setup_pins(msm_host->pdata, false); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2042 | ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata, |
| 2043 | VDD_IO_LOW, 0); |
| 2044 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2045 | if (ret) |
| 2046 | irq_ack |= CORE_PWRCTL_BUS_FAIL; |
| 2047 | else |
| 2048 | irq_ack |= CORE_PWRCTL_BUS_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2049 | |
| 2050 | pwr_state = REQ_BUS_OFF; |
| 2051 | io_level = REQ_IO_LOW; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2052 | } |
| 2053 | /* Handle IO LOW/HIGH */ |
| 2054 | if (irq_status & CORE_PWRCTL_IO_LOW) { |
| 2055 | /* Switch voltage Low */ |
| 2056 | ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0); |
| 2057 | if (ret) |
| 2058 | irq_ack |= CORE_PWRCTL_IO_FAIL; |
| 2059 | else |
| 2060 | irq_ack |= CORE_PWRCTL_IO_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2061 | |
| 2062 | io_level = REQ_IO_LOW; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2063 | } |
| 2064 | if (irq_status & CORE_PWRCTL_IO_HIGH) { |
| 2065 | /* Switch voltage High */ |
| 2066 | ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0); |
| 2067 | if (ret) |
| 2068 | irq_ack |= CORE_PWRCTL_IO_FAIL; |
| 2069 | else |
| 2070 | irq_ack |= CORE_PWRCTL_IO_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2071 | |
| 2072 | io_level = REQ_IO_HIGH; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2073 | } |
| 2074 | |
| 2075 | /* ACK status to the core */ |
| 2076 | writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL)); |
| 2077 | /* |
| 2078 | * SDHC has core_mem and hc_mem device memory and these memory |
| 2079 | * addresses do not fall within 1KB region. Hence, any update to |
| 2080 | * core_mem address space would require an mb() to ensure this gets |
| 2081 | * completed before its next update to registers within hc_mem. |
| 2082 | */ |
| 2083 | mb(); |
| 2084 | |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 2085 | if ((io_level & REQ_IO_HIGH) && (msm_host->caps_0 & CORE_3_0V_SUPPORT)) |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 2086 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) & |
| 2087 | ~CORE_IO_PAD_PWR_SWITCH), |
| 2088 | host->ioaddr + CORE_VENDOR_SPEC); |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 2089 | else if ((io_level & REQ_IO_LOW) || |
| 2090 | (msm_host->caps_0 & CORE_1_8V_SUPPORT)) |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 2091 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) | |
| 2092 | CORE_IO_PAD_PWR_SWITCH), |
| 2093 | host->ioaddr + CORE_VENDOR_SPEC); |
| 2094 | mb(); |
| 2095 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2096 | pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n", |
| 2097 | mmc_hostname(msm_host->mmc), irq, ret, irq_ack); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2098 | spin_lock_irqsave(&host->lock, flags); |
| 2099 | if (pwr_state) |
| 2100 | msm_host->curr_pwr_state = pwr_state; |
| 2101 | if (io_level) |
| 2102 | msm_host->curr_io_level = io_level; |
| 2103 | complete(&msm_host->pwr_irq_completion); |
| 2104 | spin_unlock_irqrestore(&host->lock, flags); |
| 2105 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2106 | return IRQ_HANDLED; |
| 2107 | } |
| 2108 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 2109 | static ssize_t |
Sahitya Tummala | 5c55b93 | 2013-06-20 14:00:18 +0530 | [diff] [blame] | 2110 | show_polling(struct device *dev, struct device_attribute *attr, char *buf) |
| 2111 | { |
| 2112 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 2113 | int poll; |
| 2114 | unsigned long flags; |
| 2115 | |
| 2116 | spin_lock_irqsave(&host->lock, flags); |
| 2117 | poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL); |
| 2118 | spin_unlock_irqrestore(&host->lock, flags); |
| 2119 | |
| 2120 | return snprintf(buf, PAGE_SIZE, "%d\n", poll); |
| 2121 | } |
| 2122 | |
| 2123 | static ssize_t |
| 2124 | store_polling(struct device *dev, struct device_attribute *attr, |
| 2125 | const char *buf, size_t count) |
| 2126 | { |
| 2127 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 2128 | int value; |
| 2129 | unsigned long flags; |
| 2130 | |
| 2131 | if (!kstrtou32(buf, 0, &value)) { |
| 2132 | spin_lock_irqsave(&host->lock, flags); |
| 2133 | if (value) { |
| 2134 | host->mmc->caps |= MMC_CAP_NEEDS_POLL; |
| 2135 | mmc_detect_change(host->mmc, 0); |
| 2136 | } else { |
| 2137 | host->mmc->caps &= ~MMC_CAP_NEEDS_POLL; |
| 2138 | } |
| 2139 | spin_unlock_irqrestore(&host->lock, flags); |
| 2140 | } |
| 2141 | return count; |
| 2142 | } |
| 2143 | |
| 2144 | static ssize_t |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 2145 | show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr, |
| 2146 | char *buf) |
| 2147 | { |
| 2148 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 2149 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2150 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2151 | |
| 2152 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 2153 | msm_host->msm_bus_vote.is_max_bw_needed); |
| 2154 | } |
| 2155 | |
| 2156 | static ssize_t |
| 2157 | store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr, |
| 2158 | const char *buf, size_t count) |
| 2159 | { |
| 2160 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 2161 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2162 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2163 | uint32_t value; |
| 2164 | unsigned long flags; |
| 2165 | |
| 2166 | if (!kstrtou32(buf, 0, &value)) { |
| 2167 | spin_lock_irqsave(&host->lock, flags); |
| 2168 | msm_host->msm_bus_vote.is_max_bw_needed = !!value; |
| 2169 | spin_unlock_irqrestore(&host->lock, flags); |
| 2170 | } |
| 2171 | return count; |
| 2172 | } |
| 2173 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2174 | static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type) |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2175 | { |
| 2176 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2177 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2178 | unsigned long flags; |
| 2179 | bool done = false; |
Sahitya Tummala | 481fbb0 | 2013-08-06 15:22:28 +0530 | [diff] [blame] | 2180 | u32 io_sig_sts; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2181 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2182 | spin_lock_irqsave(&host->lock, flags); |
| 2183 | pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n", |
| 2184 | mmc_hostname(host->mmc), __func__, req_type, |
| 2185 | msm_host->curr_pwr_state, msm_host->curr_io_level); |
Sahitya Tummala | 481fbb0 | 2013-08-06 15:22:28 +0530 | [diff] [blame] | 2186 | io_sig_sts = readl_relaxed(msm_host->core_mem + CORE_GENERICS); |
| 2187 | /* |
| 2188 | * The IRQ for request type IO High/Low will be generated when - |
| 2189 | * 1. SWITCHABLE_SIGNALLING_VOL is enabled in HW. |
| 2190 | * 2. If 1 is true and when there is a state change in 1.8V enable |
| 2191 | * bit (bit 3) of SDHCI_HOST_CONTROL2 register. The reset state of |
| 2192 | * that bit is 0 which indicates 3.3V IO voltage. So, when MMC core |
| 2193 | * layer tries to set it to 3.3V before card detection happens, the |
| 2194 | * IRQ doesn't get triggered as there is no state change in this bit. |
| 2195 | * The driver already handles this case by changing the IO voltage |
| 2196 | * level to high as part of controller power up sequence. Hence, check |
| 2197 | * for host->pwr to handle a case where IO voltage high request is |
| 2198 | * issued even before controller power up. |
| 2199 | */ |
| 2200 | if (req_type & (REQ_IO_HIGH | REQ_IO_LOW)) { |
| 2201 | if (!(io_sig_sts & SWITCHABLE_SIGNALLING_VOL) || |
| 2202 | ((req_type & REQ_IO_HIGH) && !host->pwr)) { |
| 2203 | pr_debug("%s: do not wait for power IRQ that never comes\n", |
| 2204 | mmc_hostname(host->mmc)); |
| 2205 | spin_unlock_irqrestore(&host->lock, flags); |
| 2206 | return; |
| 2207 | } |
| 2208 | } |
| 2209 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2210 | if ((req_type & msm_host->curr_pwr_state) || |
| 2211 | (req_type & msm_host->curr_io_level)) |
| 2212 | done = true; |
| 2213 | spin_unlock_irqrestore(&host->lock, flags); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2214 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2215 | /* |
| 2216 | * This is needed here to hanlde a case where IRQ gets |
| 2217 | * triggered even before this function is called so that |
| 2218 | * x->done counter of completion gets reset. Otherwise, |
| 2219 | * next call to wait_for_completion returns immediately |
| 2220 | * without actually waiting for the IRQ to be handled. |
| 2221 | */ |
| 2222 | if (done) |
| 2223 | init_completion(&msm_host->pwr_irq_completion); |
| 2224 | else |
| 2225 | wait_for_completion(&msm_host->pwr_irq_completion); |
| 2226 | |
| 2227 | pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc), |
| 2228 | __func__, req_type); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2229 | } |
| 2230 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 2231 | static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable) |
| 2232 | { |
| 2233 | if (enable) |
| 2234 | writel_relaxed((readl_relaxed(host->ioaddr + |
| 2235 | CORE_DLL_CONFIG) | CORE_CDR_EN), |
| 2236 | host->ioaddr + CORE_DLL_CONFIG); |
| 2237 | else |
| 2238 | writel_relaxed((readl_relaxed(host->ioaddr + |
| 2239 | CORE_DLL_CONFIG) & ~CORE_CDR_EN), |
| 2240 | host->ioaddr + CORE_DLL_CONFIG); |
| 2241 | } |
| 2242 | |
Asutosh Das | 648f9d1 | 2013-01-10 21:11:04 +0530 | [diff] [blame] | 2243 | static unsigned int sdhci_msm_max_segs(void) |
| 2244 | { |
| 2245 | return SDHCI_MSM_MAX_SEGMENTS; |
| 2246 | } |
| 2247 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2248 | static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host) |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2249 | { |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2250 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2251 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2252 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2253 | return msm_host->pdata->sup_clk_table[0]; |
| 2254 | } |
| 2255 | |
| 2256 | static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host) |
| 2257 | { |
| 2258 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2259 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2260 | int max_clk_index = msm_host->pdata->sup_clk_cnt; |
| 2261 | |
| 2262 | return msm_host->pdata->sup_clk_table[max_clk_index - 1]; |
| 2263 | } |
| 2264 | |
| 2265 | static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host, |
| 2266 | u32 req_clk) |
| 2267 | { |
| 2268 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2269 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2270 | unsigned int sel_clk = -1; |
| 2271 | unsigned char cnt; |
| 2272 | |
| 2273 | if (req_clk < sdhci_msm_get_min_clock(host)) { |
| 2274 | sel_clk = sdhci_msm_get_min_clock(host); |
| 2275 | return sel_clk; |
| 2276 | } |
| 2277 | |
| 2278 | for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) { |
| 2279 | if (msm_host->pdata->sup_clk_table[cnt] > req_clk) { |
| 2280 | break; |
| 2281 | } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) { |
| 2282 | sel_clk = msm_host->pdata->sup_clk_table[cnt]; |
| 2283 | break; |
| 2284 | } else { |
| 2285 | sel_clk = msm_host->pdata->sup_clk_table[cnt]; |
| 2286 | } |
| 2287 | } |
| 2288 | return sel_clk; |
| 2289 | } |
| 2290 | |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2291 | static int sdhci_msm_enable_controller_clock(struct sdhci_host *host) |
| 2292 | { |
| 2293 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2294 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2295 | int rc = 0; |
| 2296 | |
| 2297 | if (atomic_read(&msm_host->controller_clock)) |
| 2298 | return 0; |
| 2299 | |
| 2300 | sdhci_msm_bus_voting(host, 1); |
| 2301 | |
| 2302 | if (!IS_ERR(msm_host->pclk)) { |
| 2303 | rc = clk_prepare_enable(msm_host->pclk); |
| 2304 | if (rc) { |
| 2305 | pr_err("%s: %s: failed to enable the pclk with error %d\n", |
| 2306 | mmc_hostname(host->mmc), __func__, rc); |
| 2307 | goto remove_vote; |
| 2308 | } |
| 2309 | } |
| 2310 | |
| 2311 | rc = clk_prepare_enable(msm_host->clk); |
| 2312 | if (rc) { |
| 2313 | pr_err("%s: %s: failed to enable the host-clk with error %d\n", |
| 2314 | mmc_hostname(host->mmc), __func__, rc); |
| 2315 | goto disable_pclk; |
| 2316 | } |
| 2317 | |
| 2318 | atomic_set(&msm_host->controller_clock, 1); |
| 2319 | pr_debug("%s: %s: enabled controller clock\n", |
| 2320 | mmc_hostname(host->mmc), __func__); |
| 2321 | goto out; |
| 2322 | |
| 2323 | disable_pclk: |
| 2324 | if (!IS_ERR(msm_host->pclk)) |
| 2325 | clk_disable_unprepare(msm_host->pclk); |
| 2326 | remove_vote: |
| 2327 | if (msm_host->msm_bus_vote.client_handle) |
| 2328 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 2329 | out: |
| 2330 | return rc; |
| 2331 | } |
| 2332 | |
| 2333 | |
| 2334 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2335 | static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable) |
| 2336 | { |
| 2337 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2338 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2339 | int rc = 0; |
| 2340 | |
| 2341 | if (enable && !atomic_read(&msm_host->clks_on)) { |
| 2342 | pr_debug("%s: request to enable clocks\n", |
| 2343 | mmc_hostname(host->mmc)); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2344 | |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2345 | /* |
| 2346 | * The bus-width or the clock rate might have changed |
| 2347 | * after controller clocks are enbaled, update bus vote |
| 2348 | * in such case. |
| 2349 | */ |
| 2350 | if (atomic_read(&msm_host->controller_clock)) |
| 2351 | sdhci_msm_bus_voting(host, 1); |
| 2352 | |
| 2353 | rc = sdhci_msm_enable_controller_clock(host); |
| 2354 | if (rc) |
| 2355 | goto remove_vote; |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2356 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2357 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) { |
| 2358 | rc = clk_prepare_enable(msm_host->bus_clk); |
| 2359 | if (rc) { |
| 2360 | pr_err("%s: %s: failed to enable the bus-clock with error %d\n", |
| 2361 | mmc_hostname(host->mmc), __func__, rc); |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2362 | goto disable_controller_clk; |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2363 | } |
| 2364 | } |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2365 | if (!IS_ERR(msm_host->ff_clk)) { |
| 2366 | rc = clk_prepare_enable(msm_host->ff_clk); |
| 2367 | if (rc) { |
| 2368 | pr_err("%s: %s: failed to enable the ff_clk with error %d\n", |
| 2369 | mmc_hostname(host->mmc), __func__, rc); |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2370 | goto disable_bus_clk; |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | if (!IS_ERR(msm_host->sleep_clk)) { |
| 2374 | rc = clk_prepare_enable(msm_host->sleep_clk); |
| 2375 | if (rc) { |
| 2376 | pr_err("%s: %s: failed to enable the sleep_clk with error %d\n", |
| 2377 | mmc_hostname(host->mmc), __func__, rc); |
| 2378 | goto disable_ff_clk; |
| 2379 | } |
| 2380 | } |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2381 | mb(); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2382 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2383 | } else if (!enable && atomic_read(&msm_host->clks_on)) { |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2384 | sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); |
| 2385 | mb(); |
Sahitya Tummala | dc18298 | 2013-08-20 15:32:09 +0530 | [diff] [blame] | 2386 | /* |
| 2387 | * During 1.8V signal switching the clock source must |
| 2388 | * still be ON as it requires accessing SDHC |
| 2389 | * registers (SDHCi host control2 register bit 3 must |
| 2390 | * be written and polled after stopping the SDCLK). |
| 2391 | */ |
| 2392 | if (host->mmc->card_clock_off) |
| 2393 | return 0; |
| 2394 | pr_debug("%s: request to disable clocks\n", |
| 2395 | mmc_hostname(host->mmc)); |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2396 | if (!IS_ERR_OR_NULL(msm_host->sleep_clk)) |
| 2397 | clk_disable_unprepare(msm_host->sleep_clk); |
| 2398 | if (!IS_ERR_OR_NULL(msm_host->ff_clk)) |
| 2399 | clk_disable_unprepare(msm_host->ff_clk); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2400 | clk_disable_unprepare(msm_host->clk); |
| 2401 | if (!IS_ERR(msm_host->pclk)) |
| 2402 | clk_disable_unprepare(msm_host->pclk); |
| 2403 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) |
| 2404 | clk_disable_unprepare(msm_host->bus_clk); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2405 | |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2406 | atomic_set(&msm_host->controller_clock, 0); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2407 | sdhci_msm_bus_voting(host, 0); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2408 | } |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2409 | atomic_set(&msm_host->clks_on, enable); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2410 | goto out; |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2411 | disable_ff_clk: |
| 2412 | if (!IS_ERR_OR_NULL(msm_host->ff_clk)) |
| 2413 | clk_disable_unprepare(msm_host->ff_clk); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2414 | disable_bus_clk: |
| 2415 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) |
| 2416 | clk_disable_unprepare(msm_host->bus_clk); |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2417 | disable_controller_clk: |
| 2418 | if (!IS_ERR_OR_NULL(msm_host->clk)) |
| 2419 | clk_disable_unprepare(msm_host->clk); |
| 2420 | if (!IS_ERR_OR_NULL(msm_host->pclk)) |
| 2421 | clk_disable_unprepare(msm_host->pclk); |
| 2422 | atomic_set(&msm_host->controller_clock, 0); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2423 | remove_vote: |
| 2424 | if (msm_host->msm_bus_vote.client_handle) |
| 2425 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2426 | out: |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2427 | return rc; |
| 2428 | } |
| 2429 | |
| 2430 | static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock) |
| 2431 | { |
| 2432 | int rc; |
| 2433 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2434 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2435 | struct mmc_ios curr_ios = host->mmc->ios; |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 2436 | u32 sup_clock, ddr_clock, dll_lock; |
Sahitya Tummala | 043744a | 2013-06-24 09:55:33 +0530 | [diff] [blame] | 2437 | bool curr_pwrsave; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2438 | |
| 2439 | if (!clock) { |
Sujit Reddy Thumma | bf1aecc | 2014-01-10 10:58:54 +0530 | [diff] [blame] | 2440 | /* |
| 2441 | * disable pwrsave to ensure clock is not auto-gated until |
| 2442 | * the rate is >400KHz (initialization complete). |
| 2443 | */ |
| 2444 | writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) & |
| 2445 | ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC); |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2446 | sdhci_msm_prepare_clocks(host, false); |
| 2447 | host->clock = clock; |
| 2448 | goto out; |
| 2449 | } |
| 2450 | |
| 2451 | rc = sdhci_msm_prepare_clocks(host, true); |
| 2452 | if (rc) |
| 2453 | goto out; |
| 2454 | |
Sahitya Tummala | 043744a | 2013-06-24 09:55:33 +0530 | [diff] [blame] | 2455 | curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) & |
| 2456 | CORE_CLK_PWRSAVE); |
Sahitya Tummala | e000b24 | 2013-08-29 16:21:08 +0530 | [diff] [blame] | 2457 | if ((clock > 400000) && |
Venkat Gopalakrishnan | c0a36727 | 2015-02-24 13:09:09 -0800 | [diff] [blame] | 2458 | !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card)) |
Sahitya Tummala | 043744a | 2013-06-24 09:55:33 +0530 | [diff] [blame] | 2459 | writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
| 2460 | | CORE_CLK_PWRSAVE, |
| 2461 | host->ioaddr + CORE_VENDOR_SPEC); |
| 2462 | /* |
| 2463 | * Disable pwrsave for a newly added card if doesn't allow clock |
| 2464 | * gating. |
| 2465 | */ |
Venkat Gopalakrishnan | c0a36727 | 2015-02-24 13:09:09 -0800 | [diff] [blame] | 2466 | else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card)) |
Sahitya Tummala | 043744a | 2013-06-24 09:55:33 +0530 | [diff] [blame] | 2467 | writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
| 2468 | & ~CORE_CLK_PWRSAVE, |
| 2469 | host->ioaddr + CORE_VENDOR_SPEC); |
| 2470 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2471 | sup_clock = sdhci_msm_get_sup_clk_rate(host, clock); |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2472 | if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) || |
Venkat Gopalakrishnan | 0a29da9 | 2015-01-09 12:19:16 -0800 | [diff] [blame] | 2473 | (curr_ios.timing == MMC_TIMING_MMC_DDR52) || |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2474 | (curr_ios.timing == MMC_TIMING_MMC_HS400)) { |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2475 | /* |
| 2476 | * The SDHC requires internal clock frequency to be double the |
| 2477 | * actual clock that will be set for DDR mode. The controller |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2478 | * uses the faster clock(100/400MHz) for some of its parts and |
| 2479 | * send the actual required clock (50/200MHz) to the card. |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2480 | */ |
| 2481 | ddr_clock = clock * 2; |
| 2482 | sup_clock = sdhci_msm_get_sup_clk_rate(host, |
| 2483 | ddr_clock); |
| 2484 | } |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2485 | |
| 2486 | /* |
| 2487 | * In general all timing modes are controlled via UHS mode select in |
| 2488 | * Host Control2 register. eMMC specific HS200/HS400 doesn't have |
| 2489 | * their respective modes defined here, hence we use these values. |
| 2490 | * |
| 2491 | * HS200 - SDR104 (Since they both are equivalent in functionality) |
| 2492 | * HS400 - This involves multiple configurations |
| 2493 | * Initially SDR104 - when tuning is required as HS200 |
| 2494 | * Then when switching to DDR @ 400MHz (HS400) we use |
| 2495 | * the vendor specific HC_SELECT_IN to control the mode. |
| 2496 | * |
| 2497 | * In addition to controlling the modes we also need to select the |
| 2498 | * correct input clock for DLL depending on the mode. |
| 2499 | * |
| 2500 | * HS400 - divided clock (free running MCLK/2) |
| 2501 | * All other modes - default (free running MCLK) |
| 2502 | */ |
| 2503 | if (curr_ios.timing == MMC_TIMING_MMC_HS400) { |
| 2504 | /* Select the divided clock (free running MCLK/2) */ |
| 2505 | writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
| 2506 | & ~CORE_HC_MCLK_SEL_MASK) |
| 2507 | | CORE_HC_MCLK_SEL_HS400), |
| 2508 | host->ioaddr + CORE_VENDOR_SPEC); |
| 2509 | /* |
| 2510 | * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC |
| 2511 | * register |
| 2512 | */ |
| 2513 | if (msm_host->tuning_done && !msm_host->calibration_done) { |
| 2514 | /* |
| 2515 | * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN |
| 2516 | * field in VENDOR_SPEC_FUNC |
| 2517 | */ |
| 2518 | writel_relaxed((readl_relaxed(host->ioaddr + \ |
| 2519 | CORE_VENDOR_SPEC) |
| 2520 | | CORE_HC_SELECT_IN_HS400 |
| 2521 | | CORE_HC_SELECT_IN_EN), |
| 2522 | host->ioaddr + CORE_VENDOR_SPEC); |
| 2523 | } |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 2524 | if (!host->mmc->ios.old_rate && !msm_host->use_cdclp533) { |
| 2525 | /* |
| 2526 | * Poll on DLL_LOCK and DDR_DLL_LOCK bits in |
| 2527 | * CORE_DLL_STATUS to be set. This should get set |
| 2528 | * with in 15 us at 200 MHz. |
| 2529 | */ |
| 2530 | rc = readl_poll_timeout(host->ioaddr + CORE_DLL_STATUS, |
| 2531 | dll_lock, (dll_lock & (CORE_DLL_LOCK | |
| 2532 | CORE_DDR_DLL_LOCK)), 10, 1000); |
| 2533 | if (rc == -ETIMEDOUT) |
| 2534 | pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n", |
| 2535 | mmc_hostname(host->mmc), |
| 2536 | dll_lock); |
| 2537 | } |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2538 | } else { |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 2539 | if (!msm_host->use_cdclp533) |
| 2540 | /* set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3 */ |
| 2541 | writel_relaxed((readl_relaxed(host->ioaddr + |
| 2542 | CORE_VENDOR_SPEC3) & ~CORE_PWRSAVE_DLL), |
| 2543 | host->ioaddr + CORE_VENDOR_SPEC3); |
| 2544 | |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2545 | /* Select the default clock (free running MCLK) */ |
| 2546 | writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
| 2547 | & ~CORE_HC_MCLK_SEL_MASK) |
| 2548 | | CORE_HC_MCLK_SEL_DFLT), |
| 2549 | host->ioaddr + CORE_VENDOR_SPEC); |
| 2550 | |
| 2551 | /* |
| 2552 | * Disable HC_SELECT_IN to be able to use the UHS mode select |
| 2553 | * configuration from Host Control2 register for all other |
| 2554 | * modes. |
| 2555 | * |
| 2556 | * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field |
| 2557 | * in VENDOR_SPEC_FUNC |
| 2558 | */ |
| 2559 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
| 2560 | & ~CORE_HC_SELECT_IN_EN |
| 2561 | & ~CORE_HC_SELECT_IN_MASK), |
| 2562 | host->ioaddr + CORE_VENDOR_SPEC); |
| 2563 | } |
| 2564 | mb(); |
| 2565 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2566 | if (sup_clock != msm_host->clk_rate) { |
| 2567 | pr_debug("%s: %s: setting clk rate to %u\n", |
| 2568 | mmc_hostname(host->mmc), __func__, sup_clock); |
| 2569 | rc = clk_set_rate(msm_host->clk, sup_clock); |
| 2570 | if (rc) { |
| 2571 | pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n", |
| 2572 | mmc_hostname(host->mmc), __func__, |
| 2573 | sup_clock, rc); |
| 2574 | goto out; |
| 2575 | } |
| 2576 | msm_host->clk_rate = sup_clock; |
| 2577 | host->clock = clock; |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2578 | /* |
| 2579 | * Update the bus vote in case of frequency change due to |
| 2580 | * clock scaling. |
| 2581 | */ |
| 2582 | sdhci_msm_bus_voting(host, 1); |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2583 | } |
| 2584 | out: |
| 2585 | sdhci_set_clock(host, clock); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2586 | } |
| 2587 | |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 2588 | static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host, |
| 2589 | unsigned int uhs) |
| 2590 | { |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2591 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2592 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 2593 | u16 ctrl_2; |
| 2594 | |
| 2595 | ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 2596 | /* Select Bus Speed Mode for host */ |
| 2597 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; |
Venkat Gopalakrishnan | 0a29da9 | 2015-01-09 12:19:16 -0800 | [diff] [blame] | 2598 | if ((uhs == MMC_TIMING_MMC_HS400) || |
| 2599 | (uhs == MMC_TIMING_MMC_HS200) || |
| 2600 | (uhs == MMC_TIMING_UHS_SDR104)) |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 2601 | ctrl_2 |= SDHCI_CTRL_UHS_SDR104; |
| 2602 | else if (uhs == MMC_TIMING_UHS_SDR12) |
| 2603 | ctrl_2 |= SDHCI_CTRL_UHS_SDR12; |
| 2604 | else if (uhs == MMC_TIMING_UHS_SDR25) |
| 2605 | ctrl_2 |= SDHCI_CTRL_UHS_SDR25; |
| 2606 | else if (uhs == MMC_TIMING_UHS_SDR50) |
| 2607 | ctrl_2 |= SDHCI_CTRL_UHS_SDR50; |
Venkat Gopalakrishnan | 0a29da9 | 2015-01-09 12:19:16 -0800 | [diff] [blame] | 2608 | else if ((uhs == MMC_TIMING_UHS_DDR50) || |
| 2609 | (uhs == MMC_TIMING_MMC_DDR52)) |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 2610 | ctrl_2 |= SDHCI_CTRL_UHS_DDR50; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2611 | /* |
| 2612 | * When clock frquency is less than 100MHz, the feedback clock must be |
| 2613 | * provided and DLL must not be used so that tuning can be skipped. To |
| 2614 | * provide feedback clock, the mode selection can be any value less |
| 2615 | * than 3'b011 in bits [2:0] of HOST CONTROL2 register. |
| 2616 | */ |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2617 | if (host->clock <= CORE_FREQ_100MHZ) { |
| 2618 | if ((uhs == MMC_TIMING_MMC_HS400) || |
| 2619 | (uhs == MMC_TIMING_MMC_HS200) || |
| 2620 | (uhs == MMC_TIMING_UHS_SDR104)) |
| 2621 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2622 | |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2623 | /* |
| 2624 | * Make sure DLL is disabled when not required |
| 2625 | * |
| 2626 | * Write 1 to DLL_RST bit of DLL_CONFIG register |
| 2627 | */ |
| 2628 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 2629 | | CORE_DLL_RST), |
| 2630 | host->ioaddr + CORE_DLL_CONFIG); |
| 2631 | |
| 2632 | /* Write 1 to DLL_PDN bit of DLL_CONFIG register */ |
| 2633 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 2634 | | CORE_DLL_PDN), |
| 2635 | host->ioaddr + CORE_DLL_CONFIG); |
| 2636 | mb(); |
| 2637 | |
| 2638 | /* |
| 2639 | * The DLL needs to be restored and CDCLP533 recalibrated |
| 2640 | * when the clock frequency is set back to 400MHz. |
| 2641 | */ |
| 2642 | msm_host->calibration_done = false; |
| 2643 | } |
| 2644 | |
| 2645 | pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n", |
| 2646 | mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2); |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 2647 | sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
| 2648 | |
| 2649 | } |
| 2650 | |
Venkat Gopalakrishnan | 3481197 | 2015-03-04 14:39:01 -0800 | [diff] [blame] | 2651 | #define MAX_TEST_BUS 60 |
Sahitya Tummala | 67717bc | 2013-08-02 09:21:37 +0530 | [diff] [blame] | 2652 | |
| 2653 | void sdhci_msm_dump_vendor_regs(struct sdhci_host *host) |
| 2654 | { |
| 2655 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2656 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2657 | int tbsel, tbsel2; |
| 2658 | int i, index = 0; |
| 2659 | u32 test_bus_val = 0; |
| 2660 | u32 debug_reg[MAX_TEST_BUS] = {0}; |
| 2661 | |
| 2662 | pr_info("----------- VENDOR REGISTER DUMP -----------\n"); |
| 2663 | pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n", |
| 2664 | readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT), |
| 2665 | readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT), |
| 2666 | readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS)); |
| 2667 | pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x | SDCC ver: 0x%08x\n", |
| 2668 | readl_relaxed(host->ioaddr + CORE_DLL_CONFIG), |
| 2669 | readl_relaxed(host->ioaddr + CORE_DLL_STATUS), |
| 2670 | readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION)); |
| 2671 | pr_info("Vndr func: 0x%08x | Vndr adma err : addr0: 0x%08x addr1: 0x%08x\n", |
| 2672 | readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC), |
| 2673 | readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR0), |
| 2674 | readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC_ADMA_ERR_ADDR1)); |
| 2675 | |
| 2676 | /* |
| 2677 | * tbsel indicates [2:0] bits and tbsel2 indicates [7:4] bits |
| 2678 | * of CORE_TESTBUS_CONFIG register. |
| 2679 | * |
| 2680 | * To select test bus 0 to 7 use tbsel and to select any test bus |
| 2681 | * above 7 use (tbsel2 | tbsel) to get the test bus number. For eg, |
| 2682 | * to select test bus 14, write 0x1E to CORE_TESTBUS_CONFIG register |
| 2683 | * i.e., tbsel2[7:4] = 0001, tbsel[2:0] = 110. |
| 2684 | */ |
Venkat Gopalakrishnan | 3481197 | 2015-03-04 14:39:01 -0800 | [diff] [blame] | 2685 | for (tbsel2 = 0; tbsel2 < 7; tbsel2++) { |
Sahitya Tummala | 67717bc | 2013-08-02 09:21:37 +0530 | [diff] [blame] | 2686 | for (tbsel = 0; tbsel < 8; tbsel++) { |
| 2687 | if (index >= MAX_TEST_BUS) |
| 2688 | break; |
| 2689 | test_bus_val = (tbsel2 << CORE_TESTBUS_SEL2_BIT) | |
| 2690 | tbsel | CORE_TESTBUS_ENA; |
| 2691 | writel_relaxed(test_bus_val, |
| 2692 | msm_host->core_mem + CORE_TESTBUS_CONFIG); |
| 2693 | debug_reg[index++] = readl_relaxed(msm_host->core_mem + |
| 2694 | CORE_SDCC_DEBUG_REG); |
| 2695 | } |
| 2696 | } |
| 2697 | for (i = 0; i < MAX_TEST_BUS; i = i + 4) |
| 2698 | pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n", |
| 2699 | i, i + 3, debug_reg[i], debug_reg[i+1], |
| 2700 | debug_reg[i+2], debug_reg[i+3]); |
Venkat Gopalakrishnan | 06f7f79 | 2015-05-29 17:56:59 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | static void sdhci_msm_clear_set_dumpregs(struct sdhci_host *host, bool set) |
| 2704 | { |
| 2705 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2706 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2707 | |
| 2708 | if (set) { |
| 2709 | writel_relaxed(CORE_TESTBUS_ENA, |
| 2710 | msm_host->core_mem + CORE_TESTBUS_CONFIG); |
| 2711 | } else { |
| 2712 | u32 value; |
| 2713 | |
| 2714 | value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG); |
| 2715 | value &= ~CORE_TESTBUS_ENA; |
| 2716 | writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG); |
| 2717 | } |
Sahitya Tummala | 67717bc | 2013-08-02 09:21:37 +0530 | [diff] [blame] | 2718 | } |
| 2719 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2720 | static struct sdhci_ops sdhci_msm_ops = { |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 2721 | .set_uhs_signaling = sdhci_msm_set_uhs_signaling, |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2722 | .check_power_status = sdhci_msm_check_power_status, |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 2723 | .platform_execute_tuning = sdhci_msm_execute_tuning, |
| 2724 | .toggle_cdr = sdhci_msm_toggle_cdr, |
Asutosh Das | 648f9d1 | 2013-01-10 21:11:04 +0530 | [diff] [blame] | 2725 | .get_max_segments = sdhci_msm_max_segs, |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2726 | .set_clock = sdhci_msm_set_clock, |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2727 | .get_min_clock = sdhci_msm_get_min_clock, |
| 2728 | .get_max_clock = sdhci_msm_get_max_clock, |
Sahitya Tummala | 67717bc | 2013-08-02 09:21:37 +0530 | [diff] [blame] | 2729 | .dump_vendor_regs = sdhci_msm_dump_vendor_regs, |
Asutosh Das | e5e9ca6 | 2013-07-30 19:08:36 +0530 | [diff] [blame] | 2730 | .config_auto_tuning_cmd = sdhci_msm_config_auto_tuning_cmd, |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2731 | .enable_controller_clock = sdhci_msm_enable_controller_clock, |
Venkat Gopalakrishnan | b8cb707 | 2015-01-09 11:04:34 -0800 | [diff] [blame] | 2732 | .set_bus_width = sdhci_set_bus_width, |
Venkat Gopalakrishnan | 411df07 | 2015-01-09 11:09:44 -0800 | [diff] [blame] | 2733 | .reset = sdhci_reset, |
Venkat Gopalakrishnan | 06f7f79 | 2015-05-29 17:56:59 -0700 | [diff] [blame] | 2734 | .clear_set_dumpregs = sdhci_msm_clear_set_dumpregs, |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2735 | }; |
| 2736 | |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 2737 | static void sdhci_set_default_hw_caps(struct sdhci_msm_host *msm_host, |
| 2738 | struct sdhci_host *host) |
| 2739 | { |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 2740 | u32 version, caps = 0; |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 2741 | u16 minor; |
| 2742 | u8 major; |
| 2743 | |
| 2744 | version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION); |
| 2745 | major = (version & CORE_VERSION_MAJOR_MASK) >> |
| 2746 | CORE_VERSION_MAJOR_SHIFT; |
| 2747 | minor = version & CORE_VERSION_TARGET_MASK; |
| 2748 | |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 2749 | caps = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES); |
| 2750 | |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 2751 | /* |
| 2752 | * Starting with SDCC 5 controller (core major version = 1) |
Venkat Gopalakrishnan | 3ac8fd3 | 2014-08-28 18:15:45 -0700 | [diff] [blame] | 2753 | * controller won't advertise 3.0v, 1.8v and 8-bit features |
| 2754 | * except for some targets. |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 2755 | */ |
| 2756 | if (major >= 1 && minor != 0x11 && minor != 0x12) { |
Venkat Gopalakrishnan | 3ac8fd3 | 2014-08-28 18:15:45 -0700 | [diff] [blame] | 2757 | struct sdhci_msm_reg_data *vdd_io_reg; |
Venkat Gopalakrishnan | 3ac8fd3 | 2014-08-28 18:15:45 -0700 | [diff] [blame] | 2758 | /* |
| 2759 | * Enable 1.8V support capability on controllers that |
| 2760 | * support dual voltage |
| 2761 | */ |
| 2762 | vdd_io_reg = msm_host->pdata->vreg_data->vdd_io_data; |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 2763 | if (vdd_io_reg && (vdd_io_reg->high_vol_level > 2700000)) |
| 2764 | caps |= CORE_3_0V_SUPPORT; |
| 2765 | if (vdd_io_reg && (vdd_io_reg->low_vol_level < 1950000)) |
Venkat Gopalakrishnan | 3ac8fd3 | 2014-08-28 18:15:45 -0700 | [diff] [blame] | 2766 | caps |= CORE_1_8V_SUPPORT; |
Pratibhasagar V | ada4799 | 2013-12-09 20:42:32 +0530 | [diff] [blame] | 2767 | if (msm_host->pdata->mmc_bus_width == MMC_CAP_8_BIT_DATA) |
| 2768 | caps |= CORE_8_BIT_SUPPORT; |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 2769 | } |
Krishna Konda | 2faa7bb | 2014-06-04 01:25:16 -0700 | [diff] [blame] | 2770 | |
| 2771 | /* |
| 2772 | * SDCC 5 controller with major version 1, minor version 0x34 and later |
| 2773 | * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL. |
| 2774 | */ |
| 2775 | if ((major == 1) && (minor < 0x34)) |
| 2776 | msm_host->use_cdclp533 = true; |
Gilad Broner | 2a10ca0 | 2014-10-02 17:20:35 +0300 | [diff] [blame] | 2777 | |
| 2778 | /* |
Venkat Gopalakrishnan | c0a5c3a | 2015-02-03 16:10:07 -0800 | [diff] [blame] | 2779 | * SDCC 5 controller with major version 1, minor version 0x42 and later |
| 2780 | * will require additional steps when resetting DLL. |
| 2781 | */ |
| 2782 | if ((major == 1) && (minor >= 0x42)) |
| 2783 | msm_host->use_updated_dll_reset = true; |
| 2784 | |
| 2785 | /* |
Talel Shenhar | 9a25b88 | 2015-06-02 13:36:35 +0300 | [diff] [blame] | 2786 | * SDCC 5 controller with major version 1 and minor version 0x42, |
| 2787 | * 0x46 and 0x49 currently uses 14lpp tech DLL whose internal |
| 2788 | * gating cannot guarantee MCLK timing requirement i.e. |
Ritesh Harjani | 764065e | 2015-05-13 14:14:45 +0530 | [diff] [blame] | 2789 | * when MCLK is gated OFF, it is not gated for less than 0.5us |
| 2790 | * and MCLK must be switched on for at-least 1us before DATA |
| 2791 | * starts coming. |
| 2792 | */ |
Talel Shenhar | 9a25b88 | 2015-06-02 13:36:35 +0300 | [diff] [blame] | 2793 | if ((major == 1) && ((minor == 0x42) || (minor == 0x46) || |
| 2794 | (minor == 0x49))) |
Ritesh Harjani | 764065e | 2015-05-13 14:14:45 +0530 | [diff] [blame] | 2795 | msm_host->use_14lpp_dll = true; |
| 2796 | /* |
Gilad Broner | 2a10ca0 | 2014-10-02 17:20:35 +0300 | [diff] [blame] | 2797 | * Mask 64-bit support for controller with 32-bit address bus so that |
| 2798 | * smaller descriptor size will be used and improve memory consumption. |
Gilad Broner | 2a10ca0 | 2014-10-02 17:20:35 +0300 | [diff] [blame] | 2799 | */ |
Venkat Gopalakrishnan | 9a62e04 | 2015-03-03 16:14:55 -0800 | [diff] [blame] | 2800 | if (!msm_host->pdata->largeaddressbus) |
| 2801 | caps &= ~CORE_SYS_BUS_SUPPORT_64_BIT; |
| 2802 | |
Gilad Broner | 2a10ca0 | 2014-10-02 17:20:35 +0300 | [diff] [blame] | 2803 | writel_relaxed(caps, host->ioaddr + CORE_VENDOR_SPEC_CAPABILITIES0); |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 2804 | /* keep track of the value in SDHCI_CAPABILITIES */ |
| 2805 | msm_host->caps_0 = caps; |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 2806 | } |
| 2807 | |
Venkat Gopalakrishnan | d371f14 | 2015-05-29 17:49:46 -0700 | [diff] [blame] | 2808 | #ifdef CONFIG_MMC_CQ_HCI |
| 2809 | static void sdhci_msm_cmdq_init(struct sdhci_host *host, |
| 2810 | struct platform_device *pdev) |
| 2811 | { |
| 2812 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2813 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2814 | |
| 2815 | host->cq_host = cmdq_pltfm_init(pdev); |
| 2816 | if (IS_ERR(host->cq_host)) |
| 2817 | dev_dbg(&pdev->dev, "cmdq-pltfm init: failed: %ld\n", |
| 2818 | PTR_ERR(host->cq_host)); |
| 2819 | else |
| 2820 | msm_host->mmc->caps2 |= MMC_CAP2_CMD_QUEUE; |
| 2821 | } |
| 2822 | #else |
| 2823 | static void sdhci_msm_cmdq_init(struct sdhci_host *host, |
| 2824 | struct platform_device *pdev) |
| 2825 | { |
| 2826 | |
| 2827 | } |
| 2828 | #endif |
| 2829 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2830 | static int sdhci_msm_probe(struct platform_device *pdev) |
| 2831 | { |
| 2832 | struct sdhci_host *host; |
| 2833 | struct sdhci_pltfm_host *pltfm_host; |
| 2834 | struct sdhci_msm_host *msm_host; |
| 2835 | struct resource *core_memres = NULL; |
Konstantein Dorfman | e0be02d | 2015-01-27 12:06:02 +0200 | [diff] [blame] | 2836 | int ret = 0, dead = 0; |
Stephen Boyd | 8dce5c6 | 2013-04-24 14:19:46 -0700 | [diff] [blame] | 2837 | u16 host_version; |
Venkat Gopalakrishnan | 17edb35 | 2015-06-24 14:46:49 -0700 | [diff] [blame^] | 2838 | u32 irq_status, irq_ctl; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2839 | |
| 2840 | pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__); |
| 2841 | msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host), |
| 2842 | GFP_KERNEL); |
| 2843 | if (!msm_host) { |
| 2844 | ret = -ENOMEM; |
| 2845 | goto out; |
| 2846 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2847 | |
| 2848 | msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops; |
| 2849 | host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0); |
| 2850 | if (IS_ERR(host)) { |
| 2851 | ret = PTR_ERR(host); |
| 2852 | goto out; |
| 2853 | } |
| 2854 | |
| 2855 | pltfm_host = sdhci_priv(host); |
| 2856 | pltfm_host->priv = msm_host; |
| 2857 | msm_host->mmc = host->mmc; |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 2858 | msm_host->pdev = pdev; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2859 | |
| 2860 | /* Extract platform data */ |
| 2861 | if (pdev->dev.of_node) { |
Venkat Gopalakrishnan | 270580a | 2013-03-11 12:17:57 -0700 | [diff] [blame] | 2862 | ret = of_alias_get_id(pdev->dev.of_node, "sdhc"); |
| 2863 | if (ret < 0) { |
| 2864 | dev_err(&pdev->dev, "Failed to get slot index %d\n", |
| 2865 | ret); |
| 2866 | goto pltfm_free; |
| 2867 | } |
| 2868 | if (disable_slots & (1 << (ret - 1))) { |
| 2869 | dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__, |
| 2870 | ret); |
| 2871 | ret = -ENODEV; |
| 2872 | goto pltfm_free; |
| 2873 | } |
| 2874 | |
Dov Levenglick | c9033ab | 2015-03-10 16:00:56 +0200 | [diff] [blame] | 2875 | msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev, |
| 2876 | msm_host); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2877 | if (!msm_host->pdata) { |
| 2878 | dev_err(&pdev->dev, "DT parsing error\n"); |
| 2879 | goto pltfm_free; |
| 2880 | } |
| 2881 | } else { |
| 2882 | dev_err(&pdev->dev, "No device tree node\n"); |
| 2883 | goto pltfm_free; |
| 2884 | } |
| 2885 | |
| 2886 | /* Setup Clocks */ |
| 2887 | |
| 2888 | /* Setup SDCC bus voter clock. */ |
| 2889 | msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk"); |
| 2890 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) { |
| 2891 | /* Vote for max. clk rate for max. performance */ |
| 2892 | ret = clk_set_rate(msm_host->bus_clk, INT_MAX); |
| 2893 | if (ret) |
| 2894 | goto pltfm_free; |
| 2895 | ret = clk_prepare_enable(msm_host->bus_clk); |
| 2896 | if (ret) |
| 2897 | goto pltfm_free; |
| 2898 | } |
| 2899 | |
| 2900 | /* Setup main peripheral bus clock */ |
| 2901 | msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk"); |
| 2902 | if (!IS_ERR(msm_host->pclk)) { |
| 2903 | ret = clk_prepare_enable(msm_host->pclk); |
| 2904 | if (ret) |
| 2905 | goto bus_clk_disable; |
| 2906 | } |
Asutosh Das | 9d7ee2f | 2013-11-08 12:33:47 +0530 | [diff] [blame] | 2907 | atomic_set(&msm_host->controller_clock, 1); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2908 | |
| 2909 | /* Setup SDC MMC clock */ |
| 2910 | msm_host->clk = devm_clk_get(&pdev->dev, "core_clk"); |
| 2911 | if (IS_ERR(msm_host->clk)) { |
| 2912 | ret = PTR_ERR(msm_host->clk); |
| 2913 | goto pclk_disable; |
| 2914 | } |
| 2915 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2916 | /* Set to the minimum supported clock frequency */ |
| 2917 | ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host)); |
| 2918 | if (ret) { |
| 2919 | dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret); |
Sahitya Tummala | 020ede0d | 2013-06-07 13:03:07 +0530 | [diff] [blame] | 2920 | goto pclk_disable; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2921 | } |
Sahitya Tummala | 020ede0d | 2013-06-07 13:03:07 +0530 | [diff] [blame] | 2922 | ret = clk_prepare_enable(msm_host->clk); |
| 2923 | if (ret) |
| 2924 | goto pclk_disable; |
| 2925 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2926 | msm_host->clk_rate = sdhci_msm_get_min_clock(host); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2927 | atomic_set(&msm_host->clks_on, 1); |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2928 | |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2929 | /* Setup CDC calibration fixed feedback clock */ |
| 2930 | msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk"); |
| 2931 | if (!IS_ERR(msm_host->ff_clk)) { |
| 2932 | ret = clk_prepare_enable(msm_host->ff_clk); |
| 2933 | if (ret) |
| 2934 | goto clk_disable; |
| 2935 | } |
| 2936 | |
| 2937 | /* Setup CDC calibration sleep clock */ |
| 2938 | msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk"); |
| 2939 | if (!IS_ERR(msm_host->sleep_clk)) { |
| 2940 | ret = clk_prepare_enable(msm_host->sleep_clk); |
| 2941 | if (ret) |
| 2942 | goto ff_clk_disable; |
| 2943 | } |
| 2944 | |
Venkat Gopalakrishnan | 9e06963 | 2013-06-12 11:16:37 -0700 | [diff] [blame] | 2945 | msm_host->saved_tuning_phase = INVALID_TUNING_PHASE; |
| 2946 | |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2947 | ret = sdhci_msm_bus_register(msm_host, pdev); |
| 2948 | if (ret) |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2949 | goto sleep_clk_disable; |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2950 | |
| 2951 | if (msm_host->msm_bus_vote.client_handle) |
| 2952 | INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work, |
| 2953 | sdhci_msm_bus_work); |
| 2954 | sdhci_msm_bus_voting(host, 1); |
| 2955 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2956 | /* Setup regulators */ |
| 2957 | ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true); |
| 2958 | if (ret) { |
| 2959 | dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2960 | goto bus_unregister; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2961 | } |
| 2962 | |
| 2963 | /* Reset the core and Enable SDHC mode */ |
| 2964 | core_memres = platform_get_resource_byname(pdev, |
| 2965 | IORESOURCE_MEM, "core_mem"); |
Asutosh Das | 890bdee | 2014-08-08 23:01:42 +0530 | [diff] [blame] | 2966 | if (!core_memres) { |
| 2967 | dev_err(&pdev->dev, "Failed to get iomem resource\n"); |
| 2968 | goto vreg_deinit; |
| 2969 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2970 | msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start, |
| 2971 | resource_size(core_memres)); |
| 2972 | |
| 2973 | if (!msm_host->core_mem) { |
| 2974 | dev_err(&pdev->dev, "Failed to remap registers\n"); |
| 2975 | ret = -ENOMEM; |
| 2976 | goto vreg_deinit; |
| 2977 | } |
| 2978 | |
Sahitya Tummala | 66b0fe3 | 2013-04-25 11:50:56 +0530 | [diff] [blame] | 2979 | /* |
Venkat Gopalakrishnan | 17edb35 | 2015-06-24 14:46:49 -0700 | [diff] [blame^] | 2980 | * Reset the vendor spec register to power on reset state. |
Sahitya Tummala | 66b0fe3 | 2013-04-25 11:50:56 +0530 | [diff] [blame] | 2981 | */ |
Venkat Gopalakrishnan | 17edb35 | 2015-06-24 14:46:49 -0700 | [diff] [blame^] | 2982 | writel_relaxed(CORE_VENDOR_SPEC_POR_VAL, |
| 2983 | host->ioaddr + CORE_VENDOR_SPEC); |
Sahitya Tummala | 66b0fe3 | 2013-04-25 11:50:56 +0530 | [diff] [blame] | 2984 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2985 | /* Set HC_MODE_EN bit in HC_MODE register */ |
| 2986 | writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE)); |
| 2987 | |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 2988 | /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */ |
| 2989 | writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) | |
| 2990 | FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE); |
| 2991 | |
Pratibhasagar V | d8acb7c | 2013-11-11 01:32:21 +0530 | [diff] [blame] | 2992 | sdhci_set_default_hw_caps(msm_host, host); |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 2993 | |
| 2994 | /* |
| 2995 | * Set the PAD_PWR_SWTICH_EN bit so that the PAD_PWR_SWITCH bit can |
| 2996 | * be used as required later on. |
| 2997 | */ |
| 2998 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) | |
| 2999 | CORE_IO_PAD_PWR_SWITCH_EN), |
| 3000 | host->ioaddr + CORE_VENDOR_SPEC); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3001 | /* |
Subhash Jadavani | 2813734 | 2013-05-14 17:46:43 +0530 | [diff] [blame] | 3002 | * CORE_SW_RST above may trigger power irq if previous status of PWRCTL |
| 3003 | * was either BUS_ON or IO_HIGH_V. So before we enable the power irq |
| 3004 | * interrupt in GIC (by registering the interrupt handler), we need to |
| 3005 | * ensure that any pending power irq interrupt status is acknowledged |
| 3006 | * otherwise power irq interrupt handler would be fired prematurely. |
| 3007 | */ |
| 3008 | irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS); |
| 3009 | writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR)); |
| 3010 | irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL); |
| 3011 | if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF)) |
| 3012 | irq_ctl |= CORE_PWRCTL_BUS_SUCCESS; |
| 3013 | if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW)) |
| 3014 | irq_ctl |= CORE_PWRCTL_IO_SUCCESS; |
| 3015 | writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL)); |
Krishna Konda | 46fd143 | 2014-10-30 21:13:27 -0700 | [diff] [blame] | 3016 | |
Subhash Jadavani | 2813734 | 2013-05-14 17:46:43 +0530 | [diff] [blame] | 3017 | /* |
| 3018 | * Ensure that above writes are propogated before interrupt enablement |
| 3019 | * in GIC. |
| 3020 | */ |
| 3021 | mb(); |
| 3022 | |
| 3023 | /* |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3024 | * Following are the deviations from SDHC spec v3.0 - |
| 3025 | * 1. Card detection is handled using separate GPIO. |
| 3026 | * 2. Bus power control is handled by interacting with PMIC. |
| 3027 | */ |
| 3028 | host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION; |
| 3029 | host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 3030 | host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN; |
| 3031 | host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK; |
Sahitya Tummala | 87d4394 | 2013-04-12 11:49:11 +0530 | [diff] [blame] | 3032 | host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD; |
Sahitya Tummala | 314162c | 2013-04-12 12:11:20 +0530 | [diff] [blame] | 3033 | host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE; |
Sahitya Tummala | 7c9780d | 2013-04-12 11:59:25 +0530 | [diff] [blame] | 3034 | host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3035 | |
Sahitya Tummala | a5733ab5 | 2013-06-10 16:32:51 +0530 | [diff] [blame] | 3036 | if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK) |
| 3037 | host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4; |
| 3038 | |
Stephen Boyd | 8dce5c6 | 2013-04-24 14:19:46 -0700 | [diff] [blame] | 3039 | host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION)); |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3040 | dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n", |
| 3041 | host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >> |
| 3042 | SDHCI_VENDOR_VER_SHIFT)); |
| 3043 | if (((host_version & SDHCI_VENDOR_VER_MASK) >> |
| 3044 | SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) { |
| 3045 | /* |
| 3046 | * Add 40us delay in interrupt handler when |
| 3047 | * operating at initialization frequency(400KHz). |
| 3048 | */ |
| 3049 | host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR; |
| 3050 | /* |
| 3051 | * Set Software Reset for DAT line in Software |
| 3052 | * Reset Register (Bit 2). |
| 3053 | */ |
| 3054 | host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT; |
| 3055 | } |
| 3056 | |
Asutosh Das | 214b966 | 2013-06-13 14:27:42 +0530 | [diff] [blame] | 3057 | host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR; |
| 3058 | |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 3059 | /* Setup PWRCTL irq */ |
Konstantein Dorfman | e0be02d | 2015-01-27 12:06:02 +0200 | [diff] [blame] | 3060 | msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq"); |
| 3061 | if (msm_host->pwr_irq < 0) { |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3062 | dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n", |
Konstantein Dorfman | e0be02d | 2015-01-27 12:06:02 +0200 | [diff] [blame] | 3063 | msm_host->pwr_irq); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3064 | goto vreg_deinit; |
| 3065 | } |
Konstantein Dorfman | e0be02d | 2015-01-27 12:06:02 +0200 | [diff] [blame] | 3066 | ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL, |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3067 | sdhci_msm_pwr_irq, IRQF_ONESHOT, |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 3068 | dev_name(&pdev->dev), host); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3069 | if (ret) { |
| 3070 | dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n", |
Konstantein Dorfman | e0be02d | 2015-01-27 12:06:02 +0200 | [diff] [blame] | 3071 | msm_host->pwr_irq, ret); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3072 | goto vreg_deinit; |
| 3073 | } |
| 3074 | |
| 3075 | /* Enable pwr irq interrupts */ |
| 3076 | writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK)); |
| 3077 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 3078 | #ifdef CONFIG_MMC_CLKGATE |
| 3079 | /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */ |
| 3080 | msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY; |
| 3081 | #endif |
| 3082 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3083 | /* Set host capabilities */ |
| 3084 | msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width; |
| 3085 | msm_host->mmc->caps |= msm_host->pdata->caps; |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3086 | msm_host->mmc->caps |= MMC_CAP_AGGRESSIVE_PM; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3087 | msm_host->mmc->caps2 |= msm_host->pdata->caps2; |
Venkat Gopalakrishnan | 97c1611 | 2014-12-16 18:26:25 -0800 | [diff] [blame] | 3088 | msm_host->mmc->caps2 |= MMC_CAP2_BOOTPART_NOACC; |
| 3089 | msm_host->mmc->caps2 |= MMC_CAP2_FULL_PWR_CYCLE; |
Subhash Jadavani | 6d472b2 | 2013-05-29 15:52:10 +0530 | [diff] [blame] | 3090 | msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE; |
Venkat Gopalakrishnan | 97c1611 | 2014-12-16 18:26:25 -0800 | [diff] [blame] | 3091 | msm_host->mmc->caps2 |= MMC_CAP2_HS400_POST_TUNING; |
Talel Shenhar | a834c02 | 2015-05-13 14:08:39 +0300 | [diff] [blame] | 3092 | msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE; |
Asutosh Das | bea115d | 2013-06-24 18:20:45 +0530 | [diff] [blame] | 3093 | msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3094 | |
| 3095 | if (msm_host->pdata->nonremovable) |
| 3096 | msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE; |
| 3097 | |
Guoping Yu | f7c9133 | 2014-08-20 16:56:18 +0800 | [diff] [blame] | 3098 | if (msm_host->pdata->nonhotplug) |
| 3099 | msm_host->mmc->caps2 |= MMC_CAP2_NONHOTPLUG; |
| 3100 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 3101 | host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us; |
Maya Erez | 994cf2f | 2014-10-21 20:22:04 +0300 | [diff] [blame] | 3102 | host->pm_qos_req_dma.type = msm_host->pdata->cpu_affinity_type; |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 3103 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 3104 | init_completion(&msm_host->pwr_irq_completion); |
| 3105 | |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 3106 | if (gpio_is_valid(msm_host->pdata->status_gpio)) { |
Sahitya Tummala | 6ddabb4 | 2014-06-05 13:26:55 +0530 | [diff] [blame] | 3107 | /* |
| 3108 | * Set up the card detect GPIO in active configuration before |
| 3109 | * configuring it as an IRQ. Otherwise, it can be in some |
| 3110 | * weird/inconsistent state resulting in flood of interrupts. |
| 3111 | */ |
| 3112 | sdhci_msm_setup_pins(msm_host->pdata, true); |
| 3113 | |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 3114 | ret = mmc_gpio_request_cd(msm_host->mmc, |
| 3115 | msm_host->pdata->status_gpio, 0); |
| 3116 | if (ret) { |
| 3117 | dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n", |
| 3118 | __func__, ret); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 3119 | goto vreg_deinit; |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 3120 | } |
| 3121 | } |
| 3122 | |
Krishna Konda | 7feab35 | 2013-09-17 23:55:40 -0700 | [diff] [blame] | 3123 | if ((sdhci_readl(host, SDHCI_CAPABILITIES) & SDHCI_CAN_64BIT) && |
| 3124 | (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(64)))) { |
| 3125 | host->dma_mask = DMA_BIT_MASK(64); |
| 3126 | mmc_dev(host->mmc)->dma_mask = &host->dma_mask; |
| 3127 | } else if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) { |
Sahitya Tummala | eaa2186 | 2013-03-20 19:34:59 +0530 | [diff] [blame] | 3128 | host->dma_mask = DMA_BIT_MASK(32); |
| 3129 | mmc_dev(host->mmc)->dma_mask = &host->dma_mask; |
| 3130 | } else { |
| 3131 | dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__); |
| 3132 | } |
| 3133 | |
Venkat Gopalakrishnan | d371f14 | 2015-05-29 17:49:46 -0700 | [diff] [blame] | 3134 | sdhci_msm_cmdq_init(host, pdev); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3135 | ret = sdhci_add_host(host); |
| 3136 | if (ret) { |
| 3137 | dev_err(&pdev->dev, "Add host failed (%d)\n", ret); |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 3138 | goto vreg_deinit; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3139 | } |
| 3140 | |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3141 | pm_runtime_set_active(&pdev->dev); |
| 3142 | pm_runtime_enable(&pdev->dev); |
| 3143 | pm_runtime_set_autosuspend_delay(&pdev->dev, MSM_AUTOSUSPEND_DELAY_MS); |
| 3144 | pm_runtime_use_autosuspend(&pdev->dev); |
| 3145 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 3146 | msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw; |
| 3147 | msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw; |
| 3148 | sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr); |
| 3149 | msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw"; |
| 3150 | msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR; |
| 3151 | ret = device_create_file(&pdev->dev, |
| 3152 | &msm_host->msm_bus_vote.max_bus_bw); |
| 3153 | if (ret) |
| 3154 | goto remove_host; |
| 3155 | |
Sahitya Tummala | 5c55b93 | 2013-06-20 14:00:18 +0530 | [diff] [blame] | 3156 | if (!gpio_is_valid(msm_host->pdata->status_gpio)) { |
| 3157 | msm_host->polling.show = show_polling; |
| 3158 | msm_host->polling.store = store_polling; |
| 3159 | sysfs_attr_init(&msm_host->polling.attr); |
| 3160 | msm_host->polling.attr.name = "polling"; |
| 3161 | msm_host->polling.attr.mode = S_IRUGO | S_IWUSR; |
| 3162 | ret = device_create_file(&pdev->dev, &msm_host->polling); |
| 3163 | if (ret) |
| 3164 | goto remove_max_bus_bw_file; |
| 3165 | } |
Asutosh Das | e5e9ca6 | 2013-07-30 19:08:36 +0530 | [diff] [blame] | 3166 | |
| 3167 | msm_host->auto_cmd21_attr.show = show_auto_cmd21; |
| 3168 | msm_host->auto_cmd21_attr.store = store_auto_cmd21; |
| 3169 | sysfs_attr_init(&msm_host->auto_cmd21_attr.attr); |
| 3170 | msm_host->auto_cmd21_attr.attr.name = "enable_auto_cmd21"; |
| 3171 | msm_host->auto_cmd21_attr.attr.mode = S_IRUGO | S_IWUSR; |
| 3172 | ret = device_create_file(&pdev->dev, &msm_host->auto_cmd21_attr); |
| 3173 | if (ret) { |
| 3174 | pr_err("%s: %s: failed creating auto-cmd21 attr: %d\n", |
| 3175 | mmc_hostname(host->mmc), __func__, ret); |
| 3176 | device_remove_file(&pdev->dev, &msm_host->auto_cmd21_attr); |
| 3177 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3178 | /* Successful initialization */ |
| 3179 | goto out; |
| 3180 | |
Sahitya Tummala | 5c55b93 | 2013-06-20 14:00:18 +0530 | [diff] [blame] | 3181 | remove_max_bus_bw_file: |
| 3182 | device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3183 | remove_host: |
| 3184 | dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3185 | pm_runtime_disable(&pdev->dev); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3186 | sdhci_remove_host(host, dead); |
| 3187 | vreg_deinit: |
| 3188 | sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 3189 | bus_unregister: |
| 3190 | if (msm_host->msm_bus_vote.client_handle) |
| 3191 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 3192 | sdhci_msm_bus_unregister(msm_host); |
Venkat Gopalakrishnan | 7f69157 | 2013-06-23 17:36:46 -0700 | [diff] [blame] | 3193 | sleep_clk_disable: |
| 3194 | if (!IS_ERR(msm_host->sleep_clk)) |
| 3195 | clk_disable_unprepare(msm_host->sleep_clk); |
| 3196 | ff_clk_disable: |
| 3197 | if (!IS_ERR(msm_host->ff_clk)) |
| 3198 | clk_disable_unprepare(msm_host->ff_clk); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3199 | clk_disable: |
| 3200 | if (!IS_ERR(msm_host->clk)) |
| 3201 | clk_disable_unprepare(msm_host->clk); |
| 3202 | pclk_disable: |
| 3203 | if (!IS_ERR(msm_host->pclk)) |
| 3204 | clk_disable_unprepare(msm_host->pclk); |
| 3205 | bus_clk_disable: |
| 3206 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) |
| 3207 | clk_disable_unprepare(msm_host->bus_clk); |
| 3208 | pltfm_free: |
| 3209 | sdhci_pltfm_free(pdev); |
| 3210 | out: |
| 3211 | pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__); |
| 3212 | return ret; |
| 3213 | } |
| 3214 | |
| 3215 | static int sdhci_msm_remove(struct platform_device *pdev) |
| 3216 | { |
| 3217 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 3218 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 3219 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 3220 | struct sdhci_msm_pltfm_data *pdata = msm_host->pdata; |
| 3221 | int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == |
| 3222 | 0xffffffff); |
| 3223 | |
| 3224 | pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__); |
Sahitya Tummala | 5c55b93 | 2013-06-20 14:00:18 +0530 | [diff] [blame] | 3225 | if (!gpio_is_valid(msm_host->pdata->status_gpio)) |
| 3226 | device_remove_file(&pdev->dev, &msm_host->polling); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 3227 | device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3228 | pm_runtime_disable(&pdev->dev); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3229 | sdhci_remove_host(host, dead); |
| 3230 | sdhci_pltfm_free(pdev); |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 3231 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3232 | sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 3233 | |
Pratibhasagar V | 9acf264 | 2013-11-21 21:07:21 +0530 | [diff] [blame] | 3234 | sdhci_msm_setup_pins(pdata, true); |
| 3235 | sdhci_msm_setup_pins(pdata, false); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 3236 | |
| 3237 | if (msm_host->msm_bus_vote.client_handle) { |
| 3238 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 3239 | sdhci_msm_bus_unregister(msm_host); |
| 3240 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3241 | return 0; |
| 3242 | } |
| 3243 | |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3244 | #ifdef CONFIG_PM |
| 3245 | static int sdhci_msm_runtime_suspend(struct device *dev) |
| 3246 | { |
| 3247 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 3248 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 3249 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3250 | ktime_t start = ktime_get(); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3251 | |
| 3252 | disable_irq(host->irq); |
| 3253 | disable_irq(msm_host->pwr_irq); |
| 3254 | |
| 3255 | /* |
| 3256 | * Remove the vote immediately only if clocks are off in which |
| 3257 | * case we might have queued work to remove vote but it may not |
| 3258 | * be completed before runtime suspend or system suspend. |
| 3259 | */ |
| 3260 | if (!atomic_read(&msm_host->clks_on)) { |
| 3261 | if (msm_host->msm_bus_vote.client_handle) |
| 3262 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 3263 | } |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3264 | trace_sdhci_msm_runtime_suspend(mmc_hostname(host->mmc), 0, |
| 3265 | ktime_to_us(ktime_sub(ktime_get(), start))); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3266 | |
| 3267 | return 0; |
| 3268 | } |
| 3269 | |
| 3270 | static int sdhci_msm_runtime_resume(struct device *dev) |
| 3271 | { |
| 3272 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 3273 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 3274 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3275 | ktime_t start = ktime_get(); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3276 | |
| 3277 | enable_irq(msm_host->pwr_irq); |
| 3278 | enable_irq(host->irq); |
| 3279 | |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3280 | trace_sdhci_msm_runtime_resume(mmc_hostname(host->mmc), 0, |
| 3281 | ktime_to_us(ktime_sub(ktime_get(), start))); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3282 | return 0; |
| 3283 | } |
| 3284 | |
| 3285 | static int sdhci_msm_suspend(struct device *dev) |
| 3286 | { |
| 3287 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 3288 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 3289 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3290 | int ret = 0; |
| 3291 | ktime_t start = ktime_get(); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3292 | |
| 3293 | if (gpio_is_valid(msm_host->pdata->status_gpio) && |
| 3294 | (msm_host->mmc->slot.cd_irq >= 0)) |
| 3295 | disable_irq(msm_host->mmc->slot.cd_irq); |
| 3296 | |
| 3297 | if (pm_runtime_suspended(dev)) { |
| 3298 | pr_debug("%s: %s: already runtime suspended\n", |
| 3299 | mmc_hostname(host->mmc), __func__); |
| 3300 | goto out; |
| 3301 | } |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3302 | ret = sdhci_msm_runtime_suspend(dev); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3303 | out: |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3304 | trace_sdhci_msm_suspend(mmc_hostname(host->mmc), ret, |
| 3305 | ktime_to_us(ktime_sub(ktime_get(), start))); |
| 3306 | return ret; |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | static int sdhci_msm_resume(struct device *dev) |
| 3310 | { |
| 3311 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 3312 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 3313 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 3314 | int ret = 0; |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3315 | ktime_t start = ktime_get(); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3316 | |
| 3317 | if (gpio_is_valid(msm_host->pdata->status_gpio) && |
| 3318 | (msm_host->mmc->slot.cd_irq >= 0)) |
| 3319 | enable_irq(msm_host->mmc->slot.cd_irq); |
| 3320 | |
| 3321 | if (pm_runtime_suspended(dev)) { |
| 3322 | pr_debug("%s: %s: runtime suspended, defer system resume\n", |
| 3323 | mmc_hostname(host->mmc), __func__); |
| 3324 | goto out; |
| 3325 | } |
| 3326 | |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3327 | ret = sdhci_msm_runtime_resume(dev); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3328 | out: |
Konstantin Dorfman | ddab0cc | 2015-02-25 16:23:50 +0200 | [diff] [blame] | 3329 | trace_sdhci_msm_resume(mmc_hostname(host->mmc), ret, |
| 3330 | ktime_to_us(ktime_sub(ktime_get(), start))); |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3331 | return ret; |
| 3332 | } |
| 3333 | |
| 3334 | static const struct dev_pm_ops sdhci_msm_pmops = { |
| 3335 | SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume) |
| 3336 | SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume, |
| 3337 | NULL) |
| 3338 | }; |
| 3339 | |
| 3340 | #define SDHCI_MSM_PMOPS (&sdhci_msm_pmops) |
| 3341 | |
| 3342 | #else |
| 3343 | #define SDHCI_MSM_PMOPS NULL |
| 3344 | #endif |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3345 | static const struct of_device_id sdhci_msm_dt_match[] = { |
| 3346 | {.compatible = "qcom,sdhci-msm"}, |
| 3347 | }; |
| 3348 | MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match); |
| 3349 | |
| 3350 | static struct platform_driver sdhci_msm_driver = { |
| 3351 | .probe = sdhci_msm_probe, |
| 3352 | .remove = sdhci_msm_remove, |
| 3353 | .driver = { |
| 3354 | .name = "sdhci_msm", |
| 3355 | .owner = THIS_MODULE, |
| 3356 | .of_match_table = sdhci_msm_dt_match, |
Konstantin Dorfman | 98377d3 | 2015-02-25 10:09:41 +0200 | [diff] [blame] | 3357 | .pm = SDHCI_MSM_PMOPS, |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 3358 | }, |
| 3359 | }; |
| 3360 | |
| 3361 | module_platform_driver(sdhci_msm_driver); |
| 3362 | |
| 3363 | MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver"); |
| 3364 | MODULE_LICENSE("GPL v2"); |