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