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 |
| 45 | |
| 46 | #define CORE_POWER 0x0 |
| 47 | #define CORE_SW_RST (1 << 7) |
| 48 | |
| 49 | #define CORE_PWRCTL_STATUS 0xDC |
| 50 | #define CORE_PWRCTL_MASK 0xE0 |
| 51 | #define CORE_PWRCTL_CLEAR 0xE4 |
| 52 | #define CORE_PWRCTL_CTL 0xE8 |
| 53 | |
| 54 | #define CORE_PWRCTL_BUS_OFF 0x01 |
| 55 | #define CORE_PWRCTL_BUS_ON (1 << 1) |
| 56 | #define CORE_PWRCTL_IO_LOW (1 << 2) |
| 57 | #define CORE_PWRCTL_IO_HIGH (1 << 3) |
| 58 | |
| 59 | #define CORE_PWRCTL_BUS_SUCCESS 0x01 |
| 60 | #define CORE_PWRCTL_BUS_FAIL (1 << 1) |
| 61 | #define CORE_PWRCTL_IO_SUCCESS (1 << 2) |
| 62 | #define CORE_PWRCTL_IO_FAIL (1 << 3) |
| 63 | |
| 64 | #define INT_MASK 0xF |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 65 | #define MAX_PHASES 16 |
| 66 | |
| 67 | #define CORE_DLL_LOCK (1 << 7) |
| 68 | #define CORE_DLL_EN (1 << 16) |
| 69 | #define CORE_CDR_EN (1 << 17) |
| 70 | #define CORE_CK_OUT_EN (1 << 18) |
| 71 | #define CORE_CDR_EXT_EN (1 << 19) |
| 72 | #define CORE_DLL_PDN (1 << 29) |
| 73 | #define CORE_DLL_RST (1 << 30) |
| 74 | #define CORE_DLL_CONFIG 0x100 |
| 75 | #define CORE_DLL_TEST_CTL 0x104 |
| 76 | #define CORE_DLL_STATUS 0x108 |
| 77 | |
| 78 | #define CORE_VENDOR_SPEC 0x10C |
| 79 | #define CORE_CLK_PWRSAVE (1 << 1) |
| 80 | #define CORE_IO_PAD_PWR_SWITCH (1 << 16) |
| 81 | |
Asutosh Das | 648f9d1 | 2013-01-10 21:11:04 +0530 | [diff] [blame] | 82 | /* 8KB descriptors */ |
| 83 | #define SDHCI_MSM_MAX_SEGMENTS (1 << 13) |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 84 | #define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */ |
Asutosh Das | 648f9d1 | 2013-01-10 21:11:04 +0530 | [diff] [blame] | 85 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 86 | static const u32 tuning_block_64[] = { |
| 87 | 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE, |
| 88 | 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777, |
| 89 | 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF, |
| 90 | 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7 |
| 91 | }; |
| 92 | |
| 93 | static const u32 tuning_block_128[] = { |
| 94 | 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC, |
| 95 | 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF, |
| 96 | 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF, |
| 97 | 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB, |
| 98 | 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC, |
| 99 | 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF, |
| 100 | 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB, |
| 101 | 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77 |
| 102 | }; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 103 | |
Venkat Gopalakrishnan | 270580a | 2013-03-11 12:17:57 -0700 | [diff] [blame] | 104 | static int disable_slots; |
| 105 | /* root can write, others read */ |
| 106 | module_param(disable_slots, int, S_IRUGO|S_IWUSR); |
| 107 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 108 | /* This structure keeps information per regulator */ |
| 109 | struct sdhci_msm_reg_data { |
| 110 | /* voltage regulator handle */ |
| 111 | struct regulator *reg; |
| 112 | /* regulator name */ |
| 113 | const char *name; |
| 114 | /* voltage level to be set */ |
| 115 | u32 low_vol_level; |
| 116 | u32 high_vol_level; |
| 117 | /* Load values for low power and high power mode */ |
| 118 | u32 lpm_uA; |
| 119 | u32 hpm_uA; |
| 120 | |
| 121 | /* is this regulator enabled? */ |
| 122 | bool is_enabled; |
| 123 | /* is this regulator needs to be always on? */ |
| 124 | bool is_always_on; |
| 125 | /* is low power mode setting required for this regulator? */ |
| 126 | bool lpm_sup; |
| 127 | bool set_voltage_sup; |
| 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * This structure keeps information for all the |
| 132 | * regulators required for a SDCC slot. |
| 133 | */ |
| 134 | struct sdhci_msm_slot_reg_data { |
| 135 | /* keeps VDD/VCC regulator info */ |
| 136 | struct sdhci_msm_reg_data *vdd_data; |
| 137 | /* keeps VDD IO regulator info */ |
| 138 | struct sdhci_msm_reg_data *vdd_io_data; |
| 139 | }; |
| 140 | |
| 141 | struct sdhci_msm_gpio { |
| 142 | u32 no; |
| 143 | const char *name; |
| 144 | bool is_enabled; |
| 145 | }; |
| 146 | |
| 147 | struct sdhci_msm_gpio_data { |
| 148 | struct sdhci_msm_gpio *gpio; |
| 149 | u8 size; |
| 150 | }; |
| 151 | |
| 152 | struct sdhci_msm_pin_data { |
| 153 | /* |
| 154 | * = 1 if controller pins are using gpios |
| 155 | * = 0 if controller has dedicated MSM pads |
| 156 | */ |
| 157 | bool cfg_sts; |
| 158 | struct sdhci_msm_gpio_data *gpio_data; |
| 159 | }; |
| 160 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 161 | struct sdhci_msm_bus_voting_data { |
| 162 | struct msm_bus_scale_pdata *bus_pdata; |
| 163 | unsigned int *bw_vecs; |
| 164 | unsigned int bw_vecs_size; |
| 165 | }; |
| 166 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 167 | struct sdhci_msm_pltfm_data { |
| 168 | /* Supported UHS-I Modes */ |
| 169 | u32 caps; |
| 170 | |
| 171 | /* More capabilities */ |
| 172 | u32 caps2; |
| 173 | |
| 174 | unsigned long mmc_bus_width; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 175 | struct sdhci_msm_slot_reg_data *vreg_data; |
| 176 | bool nonremovable; |
| 177 | struct sdhci_msm_pin_data *pin_data; |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 178 | u32 cpu_dma_latency_us; |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 179 | int status_gpio; /* card detection GPIO that is configured as IRQ */ |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 180 | struct sdhci_msm_bus_voting_data *voting_data; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 181 | u32 *sup_clk_table; |
| 182 | unsigned char sup_clk_cnt; |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | struct sdhci_msm_bus_vote { |
| 186 | uint32_t client_handle; |
| 187 | uint32_t curr_vote; |
| 188 | int min_bw_vote; |
| 189 | int max_bw_vote; |
| 190 | bool is_max_bw_needed; |
| 191 | struct delayed_work vote_work; |
| 192 | struct device_attribute max_bus_bw; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | struct sdhci_msm_host { |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 196 | struct platform_device *pdev; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 197 | void __iomem *core_mem; /* MSM SDCC mapped address */ |
| 198 | struct clk *clk; /* main SD/MMC bus clock */ |
| 199 | struct clk *pclk; /* SDHC peripheral bus clock */ |
| 200 | struct clk *bus_clk; /* SDHC bus voter clock */ |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 201 | atomic_t clks_on; /* Set if clocks are enabled */ |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 202 | struct sdhci_msm_pltfm_data *pdata; |
| 203 | struct mmc_host *mmc; |
| 204 | struct sdhci_pltfm_data sdhci_msm_pdata; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 205 | u32 curr_pwr_state; |
| 206 | u32 curr_io_level; |
| 207 | struct completion pwr_irq_completion; |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 208 | struct sdhci_msm_bus_vote msm_bus_vote; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 209 | u32 clk_rate; /* Keeps track of current clock rate that is set */ |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | enum vdd_io_level { |
| 213 | /* set vdd_io_data->low_vol_level */ |
| 214 | VDD_IO_LOW, |
| 215 | /* set vdd_io_data->high_vol_level */ |
| 216 | VDD_IO_HIGH, |
| 217 | /* |
| 218 | * set whatever there in voltage_level (third argument) of |
| 219 | * sdhci_msm_set_vdd_io_vol() function. |
| 220 | */ |
| 221 | VDD_IO_SET_LEVEL, |
| 222 | }; |
| 223 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 224 | /* MSM platform specific tuning */ |
| 225 | static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, |
| 226 | u8 poll) |
| 227 | { |
| 228 | int rc = 0; |
| 229 | u32 wait_cnt = 50; |
| 230 | u8 ck_out_en = 0; |
| 231 | struct mmc_host *mmc = host->mmc; |
| 232 | |
| 233 | /* poll for CK_OUT_EN bit. max. poll time = 50us */ |
| 234 | ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) & |
| 235 | CORE_CK_OUT_EN); |
| 236 | |
| 237 | while (ck_out_en != poll) { |
| 238 | if (--wait_cnt == 0) { |
| 239 | pr_err("%s: %s: CK_OUT_EN bit is not %d\n", |
| 240 | mmc_hostname(mmc), __func__, poll); |
| 241 | rc = -ETIMEDOUT; |
| 242 | goto out; |
| 243 | } |
| 244 | udelay(1); |
| 245 | |
| 246 | ck_out_en = !!(readl_relaxed(host->ioaddr + |
| 247 | CORE_DLL_CONFIG) & CORE_CK_OUT_EN); |
| 248 | } |
| 249 | out: |
| 250 | return rc; |
| 251 | } |
| 252 | |
| 253 | static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase) |
| 254 | { |
| 255 | int rc = 0; |
| 256 | u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4, |
| 257 | 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9, |
| 258 | 0x8}; |
| 259 | unsigned long flags; |
| 260 | u32 config; |
| 261 | struct mmc_host *mmc = host->mmc; |
| 262 | |
| 263 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
| 264 | spin_lock_irqsave(&host->lock, flags); |
| 265 | |
| 266 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
| 267 | config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN); |
| 268 | config |= (CORE_CDR_EXT_EN | CORE_DLL_EN); |
| 269 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); |
| 270 | |
| 271 | /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */ |
| 272 | rc = msm_dll_poll_ck_out_en(host, 0); |
| 273 | if (rc) |
| 274 | goto err_out; |
| 275 | |
| 276 | /* |
| 277 | * Write the selected DLL clock output phase (0 ... 15) |
| 278 | * to CDR_SELEXT bit field of DLL_CONFIG register. |
| 279 | */ |
| 280 | writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 281 | & ~(0xF << 20)) |
| 282 | | (grey_coded_phase_table[phase] << 20)), |
| 283 | host->ioaddr + CORE_DLL_CONFIG); |
| 284 | |
| 285 | /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */ |
| 286 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 287 | | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG); |
| 288 | |
| 289 | /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */ |
| 290 | rc = msm_dll_poll_ck_out_en(host, 1); |
| 291 | if (rc) |
| 292 | goto err_out; |
| 293 | |
| 294 | config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG); |
| 295 | config |= CORE_CDR_EN; |
| 296 | config &= ~CORE_CDR_EXT_EN; |
| 297 | writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG); |
| 298 | goto out; |
| 299 | |
| 300 | err_out: |
| 301 | pr_err("%s: %s: Failed to set DLL phase: %d\n", |
| 302 | mmc_hostname(mmc), __func__, phase); |
| 303 | out: |
| 304 | spin_unlock_irqrestore(&host->lock, flags); |
| 305 | pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__); |
| 306 | return rc; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Find out the greatest range of consecuitive selected |
| 311 | * DLL clock output phases that can be used as sampling |
| 312 | * setting for SD3.0 UHS-I card read operation (in SDR104 |
| 313 | * timing mode) or for eMMC4.5 card read operation (in HS200 |
| 314 | * timing mode). |
| 315 | * Select the 3/4 of the range and configure the DLL with the |
| 316 | * selected DLL clock output phase. |
| 317 | */ |
| 318 | |
| 319 | static int msm_find_most_appropriate_phase(struct sdhci_host *host, |
| 320 | u8 *phase_table, u8 total_phases) |
| 321 | { |
| 322 | int ret; |
| 323 | u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} }; |
| 324 | u8 phases_per_row[MAX_PHASES] = {0}; |
| 325 | int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0; |
| 326 | int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0; |
| 327 | bool phase_0_found = false, phase_15_found = false; |
| 328 | struct mmc_host *mmc = host->mmc; |
| 329 | |
| 330 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
| 331 | if (!total_phases || (total_phases > MAX_PHASES)) { |
| 332 | pr_err("%s: %s: invalid argument: total_phases=%d\n", |
| 333 | mmc_hostname(mmc), __func__, total_phases); |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
| 337 | for (cnt = 0; cnt < total_phases; cnt++) { |
| 338 | ranges[row_index][col_index] = phase_table[cnt]; |
| 339 | phases_per_row[row_index] += 1; |
| 340 | col_index++; |
| 341 | |
| 342 | if ((cnt + 1) == total_phases) { |
| 343 | continue; |
| 344 | /* check if next phase in phase_table is consecutive or not */ |
| 345 | } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) { |
| 346 | row_index++; |
| 347 | col_index = 0; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | if (row_index >= MAX_PHASES) |
| 352 | return -EINVAL; |
| 353 | |
| 354 | /* Check if phase-0 is present in first valid window? */ |
| 355 | if (!ranges[0][0]) { |
| 356 | phase_0_found = true; |
| 357 | phase_0_raw_index = 0; |
| 358 | /* Check if cycle exist between 2 valid windows */ |
| 359 | for (cnt = 1; cnt <= row_index; cnt++) { |
| 360 | if (phases_per_row[cnt]) { |
| 361 | for (i = 0; i < phases_per_row[cnt]; i++) { |
| 362 | if (ranges[cnt][i] == 15) { |
| 363 | phase_15_found = true; |
| 364 | phase_15_raw_index = cnt; |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* If 2 valid windows form cycle then merge them as single window */ |
| 373 | if (phase_0_found && phase_15_found) { |
| 374 | /* number of phases in raw where phase 0 is present */ |
| 375 | u8 phases_0 = phases_per_row[phase_0_raw_index]; |
| 376 | /* number of phases in raw where phase 15 is present */ |
| 377 | u8 phases_15 = phases_per_row[phase_15_raw_index]; |
| 378 | |
| 379 | if (phases_0 + phases_15 >= MAX_PHASES) |
| 380 | /* |
| 381 | * If there are more than 1 phase windows then total |
| 382 | * number of phases in both the windows should not be |
| 383 | * more than or equal to MAX_PHASES. |
| 384 | */ |
| 385 | return -EINVAL; |
| 386 | |
| 387 | /* Merge 2 cyclic windows */ |
| 388 | i = phases_15; |
| 389 | for (cnt = 0; cnt < phases_0; cnt++) { |
| 390 | ranges[phase_15_raw_index][i] = |
| 391 | ranges[phase_0_raw_index][cnt]; |
| 392 | if (++i >= MAX_PHASES) |
| 393 | break; |
| 394 | } |
| 395 | |
| 396 | phases_per_row[phase_0_raw_index] = 0; |
| 397 | phases_per_row[phase_15_raw_index] = phases_15 + phases_0; |
| 398 | } |
| 399 | |
| 400 | for (cnt = 0; cnt <= row_index; cnt++) { |
| 401 | if (phases_per_row[cnt] > curr_max) { |
| 402 | curr_max = phases_per_row[cnt]; |
| 403 | selected_row_index = cnt; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | i = ((curr_max * 3) / 4); |
| 408 | if (i) |
| 409 | i--; |
| 410 | |
| 411 | ret = (int)ranges[selected_row_index][i]; |
| 412 | |
| 413 | if (ret >= MAX_PHASES) { |
| 414 | ret = -EINVAL; |
| 415 | pr_err("%s: %s: invalid phase selected=%d\n", |
| 416 | mmc_hostname(mmc), __func__, ret); |
| 417 | } |
| 418 | |
| 419 | pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__); |
| 420 | return ret; |
| 421 | } |
| 422 | |
| 423 | static inline void msm_cm_dll_set_freq(struct sdhci_host *host) |
| 424 | { |
| 425 | u32 mclk_freq = 0; |
| 426 | |
| 427 | /* Program the MCLK value to MCLK_FREQ bit field */ |
| 428 | if (host->clock <= 112000000) |
| 429 | mclk_freq = 0; |
| 430 | else if (host->clock <= 125000000) |
| 431 | mclk_freq = 1; |
| 432 | else if (host->clock <= 137000000) |
| 433 | mclk_freq = 2; |
| 434 | else if (host->clock <= 150000000) |
| 435 | mclk_freq = 3; |
| 436 | else if (host->clock <= 162000000) |
| 437 | mclk_freq = 4; |
| 438 | else if (host->clock <= 175000000) |
| 439 | mclk_freq = 5; |
| 440 | else if (host->clock <= 187000000) |
| 441 | mclk_freq = 6; |
| 442 | else if (host->clock <= 200000000) |
| 443 | mclk_freq = 7; |
| 444 | |
| 445 | writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 446 | & ~(7 << 24)) | (mclk_freq << 24)), |
| 447 | host->ioaddr + CORE_DLL_CONFIG); |
| 448 | } |
| 449 | |
| 450 | /* Initialize the DLL (Programmable Delay Line ) */ |
| 451 | static int msm_init_cm_dll(struct sdhci_host *host) |
| 452 | { |
| 453 | struct mmc_host *mmc = host->mmc; |
| 454 | int rc = 0; |
| 455 | unsigned long flags; |
| 456 | u32 wait_cnt; |
| 457 | |
| 458 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
| 459 | spin_lock_irqsave(&host->lock, flags); |
| 460 | |
| 461 | /* |
| 462 | * Make sure that clock is always enabled when DLL |
| 463 | * tuning is in progress. Keeping PWRSAVE ON may |
| 464 | * turn off the clock. So let's disable the PWRSAVE |
| 465 | * here and re-enable it once tuning is completed. |
| 466 | */ |
| 467 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
| 468 | & ~CORE_CLK_PWRSAVE), |
| 469 | host->ioaddr + CORE_VENDOR_SPEC); |
| 470 | |
| 471 | /* Write 1 to DLL_RST bit of DLL_CONFIG register */ |
| 472 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 473 | | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG); |
| 474 | |
| 475 | /* Write 1 to DLL_PDN bit of DLL_CONFIG register */ |
| 476 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 477 | | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG); |
| 478 | msm_cm_dll_set_freq(host); |
| 479 | |
| 480 | /* Write 0 to DLL_RST bit of DLL_CONFIG register */ |
| 481 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 482 | & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG); |
| 483 | |
| 484 | /* Write 0 to DLL_PDN bit of DLL_CONFIG register */ |
| 485 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 486 | & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG); |
| 487 | |
| 488 | /* Set DLL_EN bit to 1. */ |
| 489 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 490 | | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG); |
| 491 | |
| 492 | /* Set CK_OUT_EN bit to 1. */ |
| 493 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) |
| 494 | | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG); |
| 495 | |
| 496 | wait_cnt = 50; |
| 497 | /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */ |
| 498 | while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) & |
| 499 | CORE_DLL_LOCK)) { |
| 500 | /* max. wait for 50us sec for LOCK bit to be set */ |
| 501 | if (--wait_cnt == 0) { |
| 502 | pr_err("%s: %s: DLL failed to LOCK\n", |
| 503 | mmc_hostname(mmc), __func__); |
| 504 | rc = -ETIMEDOUT; |
| 505 | goto out; |
| 506 | } |
| 507 | /* wait for 1us before polling again */ |
| 508 | udelay(1); |
| 509 | } |
| 510 | |
| 511 | out: |
| 512 | /* re-enable PWRSAVE */ |
| 513 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) | |
| 514 | CORE_CLK_PWRSAVE), |
| 515 | host->ioaddr + CORE_VENDOR_SPEC); |
| 516 | spin_unlock_irqrestore(&host->lock, flags); |
| 517 | pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__); |
| 518 | return rc; |
| 519 | } |
| 520 | |
| 521 | int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode) |
| 522 | { |
| 523 | unsigned long flags; |
| 524 | u8 phase, *data_buf, tuned_phases[16], tuned_phase_cnt = 0; |
| 525 | const u32 *tuning_block_pattern = tuning_block_64; |
| 526 | int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */ |
| 527 | int rc; |
| 528 | struct mmc_host *mmc = host->mmc; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 529 | struct mmc_ios ios = host->mmc->ios; |
| 530 | |
| 531 | /* |
| 532 | * Tuning is required for SDR104 and HS200 cards and if clock frequency |
| 533 | * is greater than 100MHz in these modes. |
| 534 | */ |
| 535 | if (host->clock <= (100 * 1000 * 1000) || |
| 536 | !(ios.timing == MMC_TIMING_MMC_HS200 || |
| 537 | ios.timing == MMC_TIMING_UHS_SDR104)) |
| 538 | return 0; |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 539 | |
| 540 | pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__); |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 541 | spin_lock_irqsave(&host->lock, flags); |
| 542 | |
| 543 | if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) && |
| 544 | (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) { |
| 545 | tuning_block_pattern = tuning_block_128; |
| 546 | size = sizeof(tuning_block_128); |
| 547 | } |
| 548 | spin_unlock_irqrestore(&host->lock, flags); |
| 549 | |
| 550 | /* first of all reset the tuning block */ |
| 551 | rc = msm_init_cm_dll(host); |
| 552 | if (rc) |
| 553 | goto out; |
| 554 | |
| 555 | data_buf = kmalloc(size, GFP_KERNEL); |
| 556 | if (!data_buf) { |
| 557 | rc = -ENOMEM; |
| 558 | goto out; |
| 559 | } |
| 560 | |
| 561 | phase = 0; |
| 562 | do { |
| 563 | struct mmc_command cmd = {0}; |
| 564 | struct mmc_data data = {0}; |
| 565 | struct mmc_request mrq = { |
| 566 | .cmd = &cmd, |
| 567 | .data = &data |
| 568 | }; |
| 569 | struct scatterlist sg; |
| 570 | |
| 571 | /* set the phase in delay line hw block */ |
| 572 | rc = msm_config_cm_dll_phase(host, phase); |
| 573 | if (rc) |
| 574 | goto kfree; |
| 575 | |
| 576 | cmd.opcode = opcode; |
| 577 | cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; |
| 578 | |
| 579 | data.blksz = size; |
| 580 | data.blocks = 1; |
| 581 | data.flags = MMC_DATA_READ; |
| 582 | data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */ |
| 583 | |
| 584 | data.sg = &sg; |
| 585 | data.sg_len = 1; |
| 586 | sg_init_one(&sg, data_buf, size); |
| 587 | memset(data_buf, 0, size); |
| 588 | mmc_wait_for_req(mmc, &mrq); |
| 589 | |
| 590 | if (!cmd.error && !data.error && |
| 591 | !memcmp(data_buf, tuning_block_pattern, size)) { |
| 592 | /* tuning is successful at this tuning point */ |
| 593 | tuned_phases[tuned_phase_cnt++] = phase; |
| 594 | pr_debug("%s: %s: found good phase = %d\n", |
| 595 | mmc_hostname(mmc), __func__, phase); |
| 596 | } |
| 597 | } while (++phase < 16); |
| 598 | |
| 599 | if (tuned_phase_cnt) { |
| 600 | rc = msm_find_most_appropriate_phase(host, tuned_phases, |
| 601 | tuned_phase_cnt); |
| 602 | if (rc < 0) |
| 603 | goto kfree; |
| 604 | else |
| 605 | phase = (u8)rc; |
| 606 | |
| 607 | /* |
| 608 | * Finally set the selected phase in delay |
| 609 | * line hw block. |
| 610 | */ |
| 611 | rc = msm_config_cm_dll_phase(host, phase); |
| 612 | if (rc) |
| 613 | goto kfree; |
| 614 | pr_debug("%s: %s: finally setting the tuning phase to %d\n", |
| 615 | mmc_hostname(mmc), __func__, phase); |
| 616 | } else { |
| 617 | /* tuning failed */ |
| 618 | pr_err("%s: %s: no tuning point found\n", |
| 619 | mmc_hostname(mmc), __func__); |
| 620 | rc = -EAGAIN; |
| 621 | } |
| 622 | |
| 623 | kfree: |
| 624 | kfree(data_buf); |
| 625 | out: |
| 626 | pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__); |
| 627 | return rc; |
| 628 | } |
| 629 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 630 | static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable) |
| 631 | { |
| 632 | struct sdhci_msm_gpio_data *curr; |
| 633 | int i, ret = 0; |
| 634 | |
| 635 | curr = pdata->pin_data->gpio_data; |
| 636 | for (i = 0; i < curr->size; i++) { |
| 637 | if (!gpio_is_valid(curr->gpio[i].no)) { |
| 638 | ret = -EINVAL; |
| 639 | pr_err("%s: Invalid gpio = %d\n", __func__, |
| 640 | curr->gpio[i].no); |
| 641 | goto free_gpios; |
| 642 | } |
| 643 | if (enable) { |
| 644 | ret = gpio_request(curr->gpio[i].no, |
| 645 | curr->gpio[i].name); |
| 646 | if (ret) { |
| 647 | pr_err("%s: gpio_request(%d, %s) failed %d\n", |
| 648 | __func__, curr->gpio[i].no, |
| 649 | curr->gpio[i].name, ret); |
| 650 | goto free_gpios; |
| 651 | } |
| 652 | curr->gpio[i].is_enabled = true; |
| 653 | } else { |
| 654 | gpio_free(curr->gpio[i].no); |
| 655 | curr->gpio[i].is_enabled = false; |
| 656 | } |
| 657 | } |
| 658 | return ret; |
| 659 | |
| 660 | free_gpios: |
| 661 | for (i--; i >= 0; i--) { |
| 662 | gpio_free(curr->gpio[i].no); |
| 663 | curr->gpio[i].is_enabled = false; |
| 664 | } |
| 665 | return ret; |
| 666 | } |
| 667 | |
| 668 | static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable) |
| 669 | { |
| 670 | int ret = 0; |
| 671 | |
| 672 | if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable)) |
| 673 | return 0; |
| 674 | |
| 675 | ret = sdhci_msm_setup_gpio(pdata, enable); |
| 676 | if (!ret) |
| 677 | pdata->pin_data->cfg_sts = enable; |
| 678 | |
| 679 | return ret; |
| 680 | } |
| 681 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 682 | static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name, |
| 683 | u32 **out, int *len, u32 size) |
| 684 | { |
| 685 | int ret = 0; |
| 686 | struct device_node *np = dev->of_node; |
| 687 | size_t sz; |
| 688 | u32 *arr = NULL; |
| 689 | |
| 690 | if (!of_get_property(np, prop_name, len)) { |
| 691 | ret = -EINVAL; |
| 692 | goto out; |
| 693 | } |
| 694 | sz = *len = *len / sizeof(*arr); |
| 695 | if (sz <= 0 || (size > 0 && (sz != size))) { |
| 696 | dev_err(dev, "%s invalid size\n", prop_name); |
| 697 | ret = -EINVAL; |
| 698 | goto out; |
| 699 | } |
| 700 | |
| 701 | arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL); |
| 702 | if (!arr) { |
| 703 | dev_err(dev, "%s failed allocating memory\n", prop_name); |
| 704 | ret = -ENOMEM; |
| 705 | goto out; |
| 706 | } |
| 707 | |
| 708 | ret = of_property_read_u32_array(np, prop_name, arr, sz); |
| 709 | if (ret < 0) { |
| 710 | dev_err(dev, "%s failed reading array %d\n", prop_name, ret); |
| 711 | goto out; |
| 712 | } |
| 713 | *out = arr; |
| 714 | out: |
| 715 | if (ret) |
| 716 | *len = 0; |
| 717 | return ret; |
| 718 | } |
| 719 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 720 | #define MAX_PROP_SIZE 32 |
| 721 | static int sdhci_msm_dt_parse_vreg_info(struct device *dev, |
| 722 | struct sdhci_msm_reg_data **vreg_data, const char *vreg_name) |
| 723 | { |
| 724 | int len, ret = 0; |
| 725 | const __be32 *prop; |
| 726 | char prop_name[MAX_PROP_SIZE]; |
| 727 | struct sdhci_msm_reg_data *vreg; |
| 728 | struct device_node *np = dev->of_node; |
| 729 | |
| 730 | snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name); |
| 731 | if (!of_parse_phandle(np, prop_name, 0)) { |
| 732 | dev_err(dev, "No vreg data found for %s\n", vreg_name); |
| 733 | ret = -EINVAL; |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL); |
| 738 | if (!vreg) { |
| 739 | dev_err(dev, "No memory for vreg: %s\n", vreg_name); |
| 740 | ret = -ENOMEM; |
| 741 | return ret; |
| 742 | } |
| 743 | |
| 744 | vreg->name = vreg_name; |
| 745 | |
| 746 | snprintf(prop_name, MAX_PROP_SIZE, |
| 747 | "qcom,%s-always-on", vreg_name); |
| 748 | if (of_get_property(np, prop_name, NULL)) |
| 749 | vreg->is_always_on = true; |
| 750 | |
| 751 | snprintf(prop_name, MAX_PROP_SIZE, |
| 752 | "qcom,%s-lpm-sup", vreg_name); |
| 753 | if (of_get_property(np, prop_name, NULL)) |
| 754 | vreg->lpm_sup = true; |
| 755 | |
| 756 | snprintf(prop_name, MAX_PROP_SIZE, |
| 757 | "qcom,%s-voltage-level", vreg_name); |
| 758 | prop = of_get_property(np, prop_name, &len); |
| 759 | if (!prop || (len != (2 * sizeof(__be32)))) { |
| 760 | dev_warn(dev, "%s %s property\n", |
| 761 | prop ? "invalid format" : "no", prop_name); |
| 762 | } else { |
| 763 | vreg->low_vol_level = be32_to_cpup(&prop[0]); |
| 764 | vreg->high_vol_level = be32_to_cpup(&prop[1]); |
| 765 | } |
| 766 | |
| 767 | snprintf(prop_name, MAX_PROP_SIZE, |
| 768 | "qcom,%s-current-level", vreg_name); |
| 769 | prop = of_get_property(np, prop_name, &len); |
| 770 | if (!prop || (len != (2 * sizeof(__be32)))) { |
| 771 | dev_warn(dev, "%s %s property\n", |
| 772 | prop ? "invalid format" : "no", prop_name); |
| 773 | } else { |
| 774 | vreg->lpm_uA = be32_to_cpup(&prop[0]); |
| 775 | vreg->hpm_uA = be32_to_cpup(&prop[1]); |
| 776 | } |
| 777 | |
| 778 | *vreg_data = vreg; |
| 779 | dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n", |
| 780 | vreg->name, vreg->is_always_on ? "always_on," : "", |
| 781 | vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level, |
| 782 | vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA); |
| 783 | |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | #define GPIO_NAME_MAX_LEN 32 |
| 788 | static int sdhci_msm_dt_parse_gpio_info(struct device *dev, |
| 789 | struct sdhci_msm_pltfm_data *pdata) |
| 790 | { |
| 791 | int ret = 0, cnt, i; |
| 792 | struct sdhci_msm_pin_data *pin_data; |
| 793 | struct device_node *np = dev->of_node; |
| 794 | |
| 795 | pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL); |
| 796 | if (!pin_data) { |
| 797 | dev_err(dev, "No memory for pin_data\n"); |
| 798 | ret = -ENOMEM; |
| 799 | goto out; |
| 800 | } |
| 801 | |
| 802 | cnt = of_gpio_count(np); |
| 803 | if (cnt > 0) { |
| 804 | pin_data->gpio_data = devm_kzalloc(dev, |
| 805 | sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL); |
| 806 | if (!pin_data->gpio_data) { |
| 807 | dev_err(dev, "No memory for gpio_data\n"); |
| 808 | ret = -ENOMEM; |
| 809 | goto out; |
| 810 | } |
| 811 | pin_data->gpio_data->size = cnt; |
| 812 | pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt * |
| 813 | sizeof(struct sdhci_msm_gpio), GFP_KERNEL); |
| 814 | |
| 815 | if (!pin_data->gpio_data->gpio) { |
| 816 | dev_err(dev, "No memory for gpio\n"); |
| 817 | ret = -ENOMEM; |
| 818 | goto out; |
| 819 | } |
| 820 | |
| 821 | for (i = 0; i < cnt; i++) { |
| 822 | const char *name = NULL; |
| 823 | char result[GPIO_NAME_MAX_LEN]; |
| 824 | pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i); |
| 825 | of_property_read_string_index(np, |
| 826 | "qcom,gpio-names", i, &name); |
| 827 | |
| 828 | snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s", |
| 829 | dev_name(dev), name ? name : "?"); |
| 830 | pin_data->gpio_data->gpio[i].name = result; |
| 831 | dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__, |
| 832 | pin_data->gpio_data->gpio[i].name, |
| 833 | pin_data->gpio_data->gpio[i].no); |
| 834 | pdata->pin_data = pin_data; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | out: |
| 839 | if (ret) |
| 840 | dev_err(dev, "%s failed with err %d\n", __func__, ret); |
| 841 | return ret; |
| 842 | } |
| 843 | |
| 844 | /* Parse platform data */ |
| 845 | static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev) |
| 846 | { |
| 847 | struct sdhci_msm_pltfm_data *pdata = NULL; |
| 848 | struct device_node *np = dev->of_node; |
| 849 | u32 bus_width = 0; |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 850 | u32 cpu_dma_latency; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 851 | int len, i; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 852 | int clk_table_len; |
| 853 | u32 *clk_table = NULL; |
Sujit Reddy Thumma | 1958d3d | 2013-06-03 09:54:32 +0530 | [diff] [blame] | 854 | enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 855 | |
| 856 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
| 857 | if (!pdata) { |
| 858 | dev_err(dev, "failed to allocate memory for platform data\n"); |
| 859 | goto out; |
| 860 | } |
| 861 | |
Sujit Reddy Thumma | 1958d3d | 2013-06-03 09:54:32 +0530 | [diff] [blame] | 862 | pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags); |
| 863 | if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW)) |
| 864 | pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH; |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 865 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 866 | of_property_read_u32(np, "qcom,bus-width", &bus_width); |
| 867 | if (bus_width == 8) |
| 868 | pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA; |
| 869 | else if (bus_width == 4) |
| 870 | pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA; |
| 871 | else { |
| 872 | dev_notice(dev, "invalid bus-width, default to 1-bit mode\n"); |
| 873 | pdata->mmc_bus_width = 0; |
| 874 | } |
| 875 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 876 | if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us", |
| 877 | &cpu_dma_latency)) |
| 878 | pdata->cpu_dma_latency_us = cpu_dma_latency; |
| 879 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 880 | if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates", |
| 881 | &clk_table, &clk_table_len, 0)) { |
| 882 | dev_err(dev, "failed parsing supported clock rates\n"); |
| 883 | goto out; |
| 884 | } |
| 885 | if (!clk_table || !clk_table_len) { |
| 886 | dev_err(dev, "Invalid clock table\n"); |
| 887 | goto out; |
| 888 | } |
| 889 | pdata->sup_clk_table = clk_table; |
| 890 | pdata->sup_clk_cnt = clk_table_len; |
| 891 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 892 | pdata->vreg_data = devm_kzalloc(dev, sizeof(struct |
| 893 | sdhci_msm_slot_reg_data), |
| 894 | GFP_KERNEL); |
| 895 | if (!pdata->vreg_data) { |
| 896 | dev_err(dev, "failed to allocate memory for vreg data\n"); |
| 897 | goto out; |
| 898 | } |
| 899 | |
| 900 | if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data, |
| 901 | "vdd")) { |
| 902 | dev_err(dev, "failed parsing vdd data\n"); |
| 903 | goto out; |
| 904 | } |
| 905 | if (sdhci_msm_dt_parse_vreg_info(dev, |
| 906 | &pdata->vreg_data->vdd_io_data, |
| 907 | "vdd-io")) { |
| 908 | dev_err(dev, "failed parsing vdd-io data\n"); |
| 909 | goto out; |
| 910 | } |
| 911 | |
| 912 | if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) { |
| 913 | dev_err(dev, "failed parsing gpio data\n"); |
| 914 | goto out; |
| 915 | } |
| 916 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 917 | len = of_property_count_strings(np, "qcom,bus-speed-mode"); |
| 918 | |
| 919 | for (i = 0; i < len; i++) { |
| 920 | const char *name = NULL; |
| 921 | |
| 922 | of_property_read_string_index(np, |
| 923 | "qcom,bus-speed-mode", i, &name); |
| 924 | if (!name) |
| 925 | continue; |
| 926 | |
| 927 | if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v"))) |
| 928 | pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR; |
| 929 | else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v"))) |
| 930 | pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR; |
| 931 | else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v"))) |
| 932 | pdata->caps |= MMC_CAP_1_8V_DDR |
| 933 | | MMC_CAP_UHS_DDR50; |
| 934 | else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v"))) |
| 935 | pdata->caps |= MMC_CAP_1_2V_DDR |
| 936 | | MMC_CAP_UHS_DDR50; |
| 937 | } |
| 938 | |
| 939 | if (of_get_property(np, "qcom,nonremovable", NULL)) |
| 940 | pdata->nonremovable = true; |
| 941 | |
| 942 | return pdata; |
| 943 | out: |
| 944 | return NULL; |
| 945 | } |
| 946 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 947 | /* Returns required bandwidth in Bytes per Sec */ |
| 948 | static unsigned int sdhci_get_bw_required(struct sdhci_host *host, |
| 949 | struct mmc_ios *ios) |
| 950 | { |
Sahitya Tummala | 2886c92 | 2013-04-03 18:03:31 +0530 | [diff] [blame] | 951 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 952 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 953 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 954 | unsigned int bw; |
| 955 | |
Sahitya Tummala | 2886c92 | 2013-04-03 18:03:31 +0530 | [diff] [blame] | 956 | bw = msm_host->clk_rate; |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 957 | /* |
| 958 | * For DDR mode, SDCC controller clock will be at |
| 959 | * the double rate than the actual clock that goes to card. |
| 960 | */ |
| 961 | if (ios->bus_width == MMC_BUS_WIDTH_4) |
| 962 | bw /= 2; |
| 963 | else if (ios->bus_width == MMC_BUS_WIDTH_1) |
| 964 | bw /= 8; |
| 965 | |
| 966 | return bw; |
| 967 | } |
| 968 | |
| 969 | static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host, |
| 970 | unsigned int bw) |
| 971 | { |
| 972 | unsigned int *table = host->pdata->voting_data->bw_vecs; |
| 973 | unsigned int size = host->pdata->voting_data->bw_vecs_size; |
| 974 | int i; |
| 975 | |
| 976 | if (host->msm_bus_vote.is_max_bw_needed && bw) |
| 977 | return host->msm_bus_vote.max_bw_vote; |
| 978 | |
| 979 | for (i = 0; i < size; i++) { |
| 980 | if (bw <= table[i]) |
| 981 | break; |
| 982 | } |
| 983 | |
| 984 | if (i && (i == size)) |
| 985 | i--; |
| 986 | |
| 987 | return i; |
| 988 | } |
| 989 | |
| 990 | /* |
| 991 | * This function must be called with host lock acquired. |
| 992 | * Caller of this function should also ensure that msm bus client |
| 993 | * handle is not null. |
| 994 | */ |
| 995 | static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host, |
| 996 | int vote, |
| 997 | unsigned long flags) |
| 998 | { |
| 999 | struct sdhci_host *host = platform_get_drvdata(msm_host->pdev); |
| 1000 | int rc = 0; |
| 1001 | |
| 1002 | if (vote != msm_host->msm_bus_vote.curr_vote) { |
| 1003 | spin_unlock_irqrestore(&host->lock, flags); |
| 1004 | rc = msm_bus_scale_client_update_request( |
| 1005 | msm_host->msm_bus_vote.client_handle, vote); |
| 1006 | spin_lock_irqsave(&host->lock, flags); |
| 1007 | if (rc) { |
| 1008 | pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n", |
| 1009 | mmc_hostname(host->mmc), |
| 1010 | msm_host->msm_bus_vote.client_handle, vote, rc); |
| 1011 | goto out; |
| 1012 | } |
| 1013 | msm_host->msm_bus_vote.curr_vote = vote; |
| 1014 | } |
| 1015 | out: |
| 1016 | return rc; |
| 1017 | } |
| 1018 | |
| 1019 | /* |
| 1020 | * Internal work. Work to set 0 bandwidth for msm bus. |
| 1021 | */ |
| 1022 | static void sdhci_msm_bus_work(struct work_struct *work) |
| 1023 | { |
| 1024 | struct sdhci_msm_host *msm_host; |
| 1025 | struct sdhci_host *host; |
| 1026 | unsigned long flags; |
| 1027 | |
| 1028 | msm_host = container_of(work, struct sdhci_msm_host, |
| 1029 | msm_bus_vote.vote_work.work); |
| 1030 | host = platform_get_drvdata(msm_host->pdev); |
| 1031 | |
| 1032 | if (!msm_host->msm_bus_vote.client_handle) |
| 1033 | return; |
| 1034 | |
| 1035 | spin_lock_irqsave(&host->lock, flags); |
| 1036 | /* don't vote for 0 bandwidth if any request is in progress */ |
| 1037 | if (!host->mrq) { |
| 1038 | sdhci_msm_bus_set_vote(msm_host, |
| 1039 | msm_host->msm_bus_vote.min_bw_vote, flags); |
| 1040 | } else |
| 1041 | pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n", |
| 1042 | mmc_hostname(host->mmc), __func__); |
| 1043 | spin_unlock_irqrestore(&host->lock, flags); |
| 1044 | } |
| 1045 | |
| 1046 | /* |
| 1047 | * This function cancels any scheduled delayed work and sets the bus |
| 1048 | * vote based on bw (bandwidth) argument. |
| 1049 | */ |
| 1050 | static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host, |
| 1051 | unsigned int bw) |
| 1052 | { |
| 1053 | int vote; |
| 1054 | unsigned long flags; |
| 1055 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1056 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1057 | |
| 1058 | cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work); |
| 1059 | spin_lock_irqsave(&host->lock, flags); |
| 1060 | vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw); |
| 1061 | sdhci_msm_bus_set_vote(msm_host, vote, flags); |
| 1062 | spin_unlock_irqrestore(&host->lock, flags); |
| 1063 | } |
| 1064 | |
| 1065 | #define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */ |
| 1066 | |
| 1067 | /* This function queues a work which will set the bandwidth requiement to 0 */ |
| 1068 | static void sdhci_msm_bus_queue_work(struct sdhci_host *host) |
| 1069 | { |
| 1070 | unsigned long flags; |
| 1071 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1072 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1073 | |
| 1074 | spin_lock_irqsave(&host->lock, flags); |
| 1075 | if (msm_host->msm_bus_vote.min_bw_vote != |
| 1076 | msm_host->msm_bus_vote.curr_vote) |
| 1077 | queue_delayed_work(system_wq, |
| 1078 | &msm_host->msm_bus_vote.vote_work, |
| 1079 | msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY)); |
| 1080 | spin_unlock_irqrestore(&host->lock, flags); |
| 1081 | } |
| 1082 | |
| 1083 | static int sdhci_msm_bus_register(struct sdhci_msm_host *host, |
| 1084 | struct platform_device *pdev) |
| 1085 | { |
| 1086 | int rc = 0; |
| 1087 | struct msm_bus_scale_pdata *bus_pdata; |
| 1088 | |
| 1089 | struct sdhci_msm_bus_voting_data *data; |
| 1090 | struct device *dev = &pdev->dev; |
| 1091 | |
| 1092 | data = devm_kzalloc(dev, |
| 1093 | sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL); |
| 1094 | if (!data) { |
| 1095 | dev_err(&pdev->dev, |
| 1096 | "%s: failed to allocate memory\n", __func__); |
| 1097 | rc = -ENOMEM; |
| 1098 | goto out; |
| 1099 | } |
| 1100 | data->bus_pdata = msm_bus_cl_get_pdata(pdev); |
| 1101 | if (data->bus_pdata) { |
| 1102 | rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps", |
| 1103 | &data->bw_vecs, &data->bw_vecs_size, 0); |
| 1104 | if (rc) { |
| 1105 | dev_err(&pdev->dev, |
| 1106 | "%s: Failed to get bus-bw-vectors-bps\n", |
| 1107 | __func__); |
| 1108 | goto out; |
| 1109 | } |
| 1110 | host->pdata->voting_data = data; |
| 1111 | } |
| 1112 | if (host->pdata->voting_data && |
| 1113 | host->pdata->voting_data->bus_pdata && |
| 1114 | host->pdata->voting_data->bw_vecs && |
| 1115 | host->pdata->voting_data->bw_vecs_size) { |
| 1116 | |
| 1117 | bus_pdata = host->pdata->voting_data->bus_pdata; |
| 1118 | host->msm_bus_vote.client_handle = |
| 1119 | msm_bus_scale_register_client(bus_pdata); |
| 1120 | if (!host->msm_bus_vote.client_handle) { |
| 1121 | dev_err(&pdev->dev, "msm_bus_scale_register_client()\n"); |
| 1122 | rc = -EFAULT; |
| 1123 | goto out; |
| 1124 | } |
| 1125 | /* cache the vote index for minimum and maximum bandwidth */ |
| 1126 | host->msm_bus_vote.min_bw_vote = |
| 1127 | sdhci_msm_bus_get_vote_for_bw(host, 0); |
| 1128 | host->msm_bus_vote.max_bw_vote = |
| 1129 | sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX); |
| 1130 | } else { |
| 1131 | devm_kfree(dev, data); |
| 1132 | } |
| 1133 | |
| 1134 | out: |
| 1135 | return rc; |
| 1136 | } |
| 1137 | |
| 1138 | static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host) |
| 1139 | { |
| 1140 | if (host->msm_bus_vote.client_handle) |
| 1141 | msm_bus_scale_unregister_client( |
| 1142 | host->msm_bus_vote.client_handle); |
| 1143 | } |
| 1144 | |
| 1145 | static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable) |
| 1146 | { |
| 1147 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1148 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1149 | struct mmc_ios *ios = &host->mmc->ios; |
| 1150 | unsigned int bw; |
| 1151 | |
| 1152 | if (!msm_host->msm_bus_vote.client_handle) |
| 1153 | return; |
| 1154 | |
| 1155 | bw = sdhci_get_bw_required(host, ios); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1156 | if (enable) { |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1157 | sdhci_msm_bus_cancel_work_and_set_vote(host, bw); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1158 | } else { |
| 1159 | /* |
| 1160 | * If clock gating is enabled, then remove the vote |
| 1161 | * immediately because clocks will be disabled only |
| 1162 | * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no |
| 1163 | * additional delay is required to remove the bus vote. |
| 1164 | */ |
| 1165 | #ifdef CONFIG_MMC_CLKGATE |
| 1166 | if (host->mmc->clkgate_delay) |
| 1167 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 1168 | else |
| 1169 | #endif |
| 1170 | sdhci_msm_bus_queue_work(host); |
| 1171 | } |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1172 | } |
| 1173 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1174 | /* Regulator utility functions */ |
| 1175 | static int sdhci_msm_vreg_init_reg(struct device *dev, |
| 1176 | struct sdhci_msm_reg_data *vreg) |
| 1177 | { |
| 1178 | int ret = 0; |
| 1179 | |
| 1180 | /* check if regulator is already initialized? */ |
| 1181 | if (vreg->reg) |
| 1182 | goto out; |
| 1183 | |
| 1184 | /* Get the regulator handle */ |
| 1185 | vreg->reg = devm_regulator_get(dev, vreg->name); |
| 1186 | if (IS_ERR(vreg->reg)) { |
| 1187 | ret = PTR_ERR(vreg->reg); |
| 1188 | pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n", |
| 1189 | __func__, vreg->name, ret); |
| 1190 | goto out; |
| 1191 | } |
| 1192 | |
| 1193 | /* sanity check */ |
| 1194 | if (!vreg->high_vol_level || !vreg->hpm_uA) { |
| 1195 | pr_err("%s: %s invalid constraints specified\n", |
| 1196 | __func__, vreg->name); |
| 1197 | ret = -EINVAL; |
| 1198 | } |
| 1199 | |
| 1200 | out: |
| 1201 | return ret; |
| 1202 | } |
| 1203 | |
| 1204 | static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg) |
| 1205 | { |
| 1206 | if (vreg->reg) |
| 1207 | devm_regulator_put(vreg->reg); |
| 1208 | } |
| 1209 | |
| 1210 | static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data |
| 1211 | *vreg, int uA_load) |
| 1212 | { |
| 1213 | int ret = 0; |
| 1214 | |
| 1215 | /* |
| 1216 | * regulators that do not support regulator_set_voltage also |
| 1217 | * do not support regulator_set_optimum_mode |
| 1218 | */ |
| 1219 | if (vreg->set_voltage_sup) { |
| 1220 | ret = regulator_set_load(vreg->reg, uA_load); |
| 1221 | if (ret < 0) |
| 1222 | pr_err("%s: regulator_set_load(reg=%s,uA_load=%d) failed. ret=%d\n", |
| 1223 | __func__, vreg->name, uA_load, ret); |
| 1224 | else |
| 1225 | /* |
| 1226 | * regulator_set_load() can return non zero |
| 1227 | * value even for success case. |
| 1228 | */ |
| 1229 | ret = 0; |
| 1230 | } |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
| 1234 | static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg, |
| 1235 | int min_uV, int max_uV) |
| 1236 | { |
| 1237 | int ret = 0; |
| 1238 | |
| 1239 | ret = regulator_set_voltage(vreg->reg, min_uV, max_uV); |
| 1240 | if (ret) { |
| 1241 | pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n", |
| 1242 | __func__, vreg->name, min_uV, max_uV, ret); |
| 1243 | } |
| 1244 | |
| 1245 | return ret; |
| 1246 | } |
| 1247 | |
| 1248 | static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg) |
| 1249 | { |
| 1250 | int ret = 0; |
| 1251 | |
| 1252 | /* Put regulator in HPM (high power mode) */ |
| 1253 | ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA); |
| 1254 | if (ret < 0) |
| 1255 | return ret; |
| 1256 | |
| 1257 | if (!vreg->is_enabled) { |
| 1258 | /* Set voltage level */ |
| 1259 | ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level, |
| 1260 | vreg->high_vol_level); |
| 1261 | if (ret) |
| 1262 | return ret; |
| 1263 | } |
| 1264 | ret = regulator_enable(vreg->reg); |
| 1265 | if (ret) { |
| 1266 | pr_err("%s: regulator_enable(%s) failed. ret=%d\n", |
| 1267 | __func__, vreg->name, ret); |
| 1268 | return ret; |
| 1269 | } |
| 1270 | vreg->is_enabled = true; |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg) |
| 1275 | { |
| 1276 | int ret = 0; |
| 1277 | |
| 1278 | /* Never disable regulator marked as always_on */ |
| 1279 | if (vreg->is_enabled && !vreg->is_always_on) { |
| 1280 | ret = regulator_disable(vreg->reg); |
| 1281 | if (ret) { |
| 1282 | pr_err("%s: regulator_disable(%s) failed. ret=%d\n", |
| 1283 | __func__, vreg->name, ret); |
| 1284 | goto out; |
| 1285 | } |
| 1286 | vreg->is_enabled = false; |
| 1287 | |
| 1288 | ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0); |
| 1289 | if (ret < 0) |
| 1290 | goto out; |
| 1291 | |
| 1292 | /* Set min. voltage level to 0 */ |
| 1293 | ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level); |
| 1294 | if (ret) |
| 1295 | goto out; |
| 1296 | } else if (vreg->is_enabled && vreg->is_always_on) { |
| 1297 | if (vreg->lpm_sup) { |
| 1298 | /* Put always_on regulator in LPM (low power mode) */ |
| 1299 | ret = sdhci_msm_vreg_set_optimum_mode(vreg, |
| 1300 | vreg->lpm_uA); |
| 1301 | if (ret < 0) |
| 1302 | goto out; |
| 1303 | } |
| 1304 | } |
| 1305 | out: |
| 1306 | return ret; |
| 1307 | } |
| 1308 | |
| 1309 | static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata, |
| 1310 | bool enable, bool is_init) |
| 1311 | { |
| 1312 | int ret = 0, i; |
| 1313 | struct sdhci_msm_slot_reg_data *curr_slot; |
| 1314 | struct sdhci_msm_reg_data *vreg_table[2]; |
| 1315 | |
| 1316 | curr_slot = pdata->vreg_data; |
| 1317 | if (!curr_slot) { |
| 1318 | pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n", |
| 1319 | __func__); |
| 1320 | goto out; |
| 1321 | } |
| 1322 | |
| 1323 | vreg_table[0] = curr_slot->vdd_data; |
| 1324 | vreg_table[1] = curr_slot->vdd_io_data; |
| 1325 | |
| 1326 | for (i = 0; i < ARRAY_SIZE(vreg_table); i++) { |
| 1327 | if (vreg_table[i]) { |
| 1328 | if (enable) |
| 1329 | ret = sdhci_msm_vreg_enable(vreg_table[i]); |
| 1330 | else |
| 1331 | ret = sdhci_msm_vreg_disable(vreg_table[i]); |
| 1332 | if (ret) |
| 1333 | goto out; |
| 1334 | } |
| 1335 | } |
| 1336 | out: |
| 1337 | return ret; |
| 1338 | } |
| 1339 | |
| 1340 | /* |
| 1341 | * Reset vreg by ensuring it is off during probe. A call |
| 1342 | * to enable vreg is needed to balance disable vreg |
| 1343 | */ |
| 1344 | static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata) |
| 1345 | { |
| 1346 | int ret; |
| 1347 | |
| 1348 | ret = sdhci_msm_setup_vreg(pdata, 1, true); |
| 1349 | if (ret) |
| 1350 | return ret; |
| 1351 | ret = sdhci_msm_setup_vreg(pdata, 0, true); |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
| 1355 | /* This init function should be called only once for each SDHC slot */ |
| 1356 | static int sdhci_msm_vreg_init(struct device *dev, |
| 1357 | struct sdhci_msm_pltfm_data *pdata, |
| 1358 | bool is_init) |
| 1359 | { |
| 1360 | int ret = 0; |
| 1361 | struct sdhci_msm_slot_reg_data *curr_slot; |
| 1362 | struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg; |
| 1363 | |
| 1364 | curr_slot = pdata->vreg_data; |
| 1365 | if (!curr_slot) |
| 1366 | goto out; |
| 1367 | |
| 1368 | curr_vdd_reg = curr_slot->vdd_data; |
| 1369 | curr_vdd_io_reg = curr_slot->vdd_io_data; |
| 1370 | |
| 1371 | if (!is_init) |
| 1372 | /* Deregister all regulators from regulator framework */ |
| 1373 | goto vdd_io_reg_deinit; |
| 1374 | |
| 1375 | /* |
| 1376 | * Get the regulator handle from voltage regulator framework |
| 1377 | * and then try to set the voltage level for the regulator |
| 1378 | */ |
| 1379 | if (curr_vdd_reg) { |
| 1380 | ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg); |
| 1381 | if (ret) |
| 1382 | goto out; |
| 1383 | } |
| 1384 | if (curr_vdd_io_reg) { |
| 1385 | ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg); |
| 1386 | if (ret) |
| 1387 | goto vdd_reg_deinit; |
| 1388 | } |
| 1389 | ret = sdhci_msm_vreg_reset(pdata); |
| 1390 | if (ret) |
| 1391 | dev_err(dev, "vreg reset failed (%d)\n", ret); |
| 1392 | goto out; |
| 1393 | |
| 1394 | vdd_io_reg_deinit: |
| 1395 | if (curr_vdd_io_reg) |
| 1396 | sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg); |
| 1397 | vdd_reg_deinit: |
| 1398 | if (curr_vdd_reg) |
| 1399 | sdhci_msm_vreg_deinit_reg(curr_vdd_reg); |
| 1400 | out: |
| 1401 | return ret; |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata, |
| 1406 | enum vdd_io_level level, |
| 1407 | unsigned int voltage_level) |
| 1408 | { |
| 1409 | int ret = 0; |
| 1410 | int set_level; |
| 1411 | struct sdhci_msm_reg_data *vdd_io_reg; |
| 1412 | |
| 1413 | if (!pdata->vreg_data) |
| 1414 | return ret; |
| 1415 | |
| 1416 | vdd_io_reg = pdata->vreg_data->vdd_io_data; |
| 1417 | if (vdd_io_reg && vdd_io_reg->is_enabled) { |
| 1418 | switch (level) { |
| 1419 | case VDD_IO_LOW: |
| 1420 | set_level = vdd_io_reg->low_vol_level; |
| 1421 | break; |
| 1422 | case VDD_IO_HIGH: |
| 1423 | set_level = vdd_io_reg->high_vol_level; |
| 1424 | break; |
| 1425 | case VDD_IO_SET_LEVEL: |
| 1426 | set_level = voltage_level; |
| 1427 | break; |
| 1428 | default: |
| 1429 | pr_err("%s: invalid argument level = %d", |
| 1430 | __func__, level); |
| 1431 | ret = -EINVAL; |
| 1432 | return ret; |
| 1433 | } |
| 1434 | ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level, |
| 1435 | set_level); |
| 1436 | } |
| 1437 | return ret; |
| 1438 | } |
| 1439 | |
| 1440 | static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data) |
| 1441 | { |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1442 | struct sdhci_host *host = (struct sdhci_host *)data; |
| 1443 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1444 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1445 | u8 irq_status = 0; |
| 1446 | u8 irq_ack = 0; |
| 1447 | int ret = 0; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1448 | int pwr_state = 0, io_level = 0; |
| 1449 | unsigned long flags; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1450 | |
| 1451 | irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS); |
| 1452 | pr_debug("%s: Received IRQ(%d), status=0x%x\n", |
| 1453 | mmc_hostname(msm_host->mmc), irq, irq_status); |
| 1454 | |
| 1455 | /* Clear the interrupt */ |
| 1456 | writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR)); |
| 1457 | /* |
| 1458 | * SDHC has core_mem and hc_mem device memory and these memory |
| 1459 | * addresses do not fall within 1KB region. Hence, any update to |
| 1460 | * core_mem address space would require an mb() to ensure this gets |
| 1461 | * completed before its next update to registers within hc_mem. |
| 1462 | */ |
| 1463 | mb(); |
| 1464 | |
| 1465 | /* Handle BUS ON/OFF*/ |
| 1466 | if (irq_status & CORE_PWRCTL_BUS_ON) { |
| 1467 | ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1468 | if (!ret) { |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1469 | ret = sdhci_msm_setup_pins(msm_host->pdata, true); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1470 | ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata, |
| 1471 | VDD_IO_HIGH, 0); |
| 1472 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1473 | if (ret) |
| 1474 | irq_ack |= CORE_PWRCTL_BUS_FAIL; |
| 1475 | else |
| 1476 | irq_ack |= CORE_PWRCTL_BUS_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1477 | |
| 1478 | pwr_state = REQ_BUS_ON; |
| 1479 | io_level = REQ_IO_HIGH; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1480 | } |
| 1481 | if (irq_status & CORE_PWRCTL_BUS_OFF) { |
| 1482 | ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1483 | if (!ret) { |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1484 | ret = sdhci_msm_setup_pins(msm_host->pdata, false); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1485 | ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata, |
| 1486 | VDD_IO_LOW, 0); |
| 1487 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1488 | if (ret) |
| 1489 | irq_ack |= CORE_PWRCTL_BUS_FAIL; |
| 1490 | else |
| 1491 | irq_ack |= CORE_PWRCTL_BUS_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1492 | |
| 1493 | pwr_state = REQ_BUS_OFF; |
| 1494 | io_level = REQ_IO_LOW; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1495 | } |
| 1496 | /* Handle IO LOW/HIGH */ |
| 1497 | if (irq_status & CORE_PWRCTL_IO_LOW) { |
| 1498 | /* Switch voltage Low */ |
| 1499 | ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0); |
| 1500 | if (ret) |
| 1501 | irq_ack |= CORE_PWRCTL_IO_FAIL; |
| 1502 | else |
| 1503 | irq_ack |= CORE_PWRCTL_IO_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1504 | |
| 1505 | io_level = REQ_IO_LOW; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1506 | } |
| 1507 | if (irq_status & CORE_PWRCTL_IO_HIGH) { |
| 1508 | /* Switch voltage High */ |
| 1509 | ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0); |
| 1510 | if (ret) |
| 1511 | irq_ack |= CORE_PWRCTL_IO_FAIL; |
| 1512 | else |
| 1513 | irq_ack |= CORE_PWRCTL_IO_SUCCESS; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1514 | |
| 1515 | io_level = REQ_IO_HIGH; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | /* ACK status to the core */ |
| 1519 | writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL)); |
| 1520 | /* |
| 1521 | * SDHC has core_mem and hc_mem device memory and these memory |
| 1522 | * addresses do not fall within 1KB region. Hence, any update to |
| 1523 | * core_mem address space would require an mb() to ensure this gets |
| 1524 | * completed before its next update to registers within hc_mem. |
| 1525 | */ |
| 1526 | mb(); |
| 1527 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1528 | if (io_level & REQ_IO_HIGH) |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1529 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) & |
| 1530 | ~CORE_IO_PAD_PWR_SWITCH), |
| 1531 | host->ioaddr + CORE_VENDOR_SPEC); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1532 | else if (io_level & REQ_IO_LOW) |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1533 | writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) | |
| 1534 | CORE_IO_PAD_PWR_SWITCH), |
| 1535 | host->ioaddr + CORE_VENDOR_SPEC); |
| 1536 | mb(); |
| 1537 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1538 | pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n", |
| 1539 | mmc_hostname(msm_host->mmc), irq, ret, irq_ack); |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1540 | spin_lock_irqsave(&host->lock, flags); |
| 1541 | if (pwr_state) |
| 1542 | msm_host->curr_pwr_state = pwr_state; |
| 1543 | if (io_level) |
| 1544 | msm_host->curr_io_level = io_level; |
| 1545 | complete(&msm_host->pwr_irq_completion); |
| 1546 | spin_unlock_irqrestore(&host->lock, flags); |
| 1547 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1548 | return IRQ_HANDLED; |
| 1549 | } |
| 1550 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1551 | static ssize_t |
| 1552 | show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr, |
| 1553 | char *buf) |
| 1554 | { |
| 1555 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 1556 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1557 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1558 | |
| 1559 | return snprintf(buf, PAGE_SIZE, "%u\n", |
| 1560 | msm_host->msm_bus_vote.is_max_bw_needed); |
| 1561 | } |
| 1562 | |
| 1563 | static ssize_t |
| 1564 | store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr, |
| 1565 | const char *buf, size_t count) |
| 1566 | { |
| 1567 | struct sdhci_host *host = dev_get_drvdata(dev); |
| 1568 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1569 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1570 | uint32_t value; |
| 1571 | unsigned long flags; |
| 1572 | |
| 1573 | if (!kstrtou32(buf, 0, &value)) { |
| 1574 | spin_lock_irqsave(&host->lock, flags); |
| 1575 | msm_host->msm_bus_vote.is_max_bw_needed = !!value; |
| 1576 | spin_unlock_irqrestore(&host->lock, flags); |
| 1577 | } |
| 1578 | return count; |
| 1579 | } |
| 1580 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1581 | 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] | 1582 | { |
| 1583 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1584 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1585 | unsigned long flags; |
| 1586 | bool done = false; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1587 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1588 | spin_lock_irqsave(&host->lock, flags); |
| 1589 | pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n", |
| 1590 | mmc_hostname(host->mmc), __func__, req_type, |
| 1591 | msm_host->curr_pwr_state, msm_host->curr_io_level); |
| 1592 | if ((req_type & msm_host->curr_pwr_state) || |
| 1593 | (req_type & msm_host->curr_io_level)) |
| 1594 | done = true; |
| 1595 | spin_unlock_irqrestore(&host->lock, flags); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1596 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 1597 | /* |
| 1598 | * This is needed here to hanlde a case where IRQ gets |
| 1599 | * triggered even before this function is called so that |
| 1600 | * x->done counter of completion gets reset. Otherwise, |
| 1601 | * next call to wait_for_completion returns immediately |
| 1602 | * without actually waiting for the IRQ to be handled. |
| 1603 | */ |
| 1604 | if (done) |
| 1605 | init_completion(&msm_host->pwr_irq_completion); |
| 1606 | else |
| 1607 | wait_for_completion(&msm_host->pwr_irq_completion); |
| 1608 | |
| 1609 | pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc), |
| 1610 | __func__, req_type); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1611 | } |
| 1612 | |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1613 | static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable) |
| 1614 | { |
| 1615 | if (enable) |
| 1616 | writel_relaxed((readl_relaxed(host->ioaddr + |
| 1617 | CORE_DLL_CONFIG) | CORE_CDR_EN), |
| 1618 | host->ioaddr + CORE_DLL_CONFIG); |
| 1619 | else |
| 1620 | writel_relaxed((readl_relaxed(host->ioaddr + |
| 1621 | CORE_DLL_CONFIG) & ~CORE_CDR_EN), |
| 1622 | host->ioaddr + CORE_DLL_CONFIG); |
| 1623 | } |
| 1624 | |
Asutosh Das | 648f9d1 | 2013-01-10 21:11:04 +0530 | [diff] [blame] | 1625 | static unsigned int sdhci_msm_max_segs(void) |
| 1626 | { |
| 1627 | return SDHCI_MSM_MAX_SEGMENTS; |
| 1628 | } |
| 1629 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1630 | static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host) |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1631 | { |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1632 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1633 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1634 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1635 | return msm_host->pdata->sup_clk_table[0]; |
| 1636 | } |
| 1637 | |
| 1638 | static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host) |
| 1639 | { |
| 1640 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1641 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1642 | int max_clk_index = msm_host->pdata->sup_clk_cnt; |
| 1643 | |
| 1644 | return msm_host->pdata->sup_clk_table[max_clk_index - 1]; |
| 1645 | } |
| 1646 | |
| 1647 | static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host, |
| 1648 | u32 req_clk) |
| 1649 | { |
| 1650 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1651 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1652 | unsigned int sel_clk = -1; |
| 1653 | unsigned char cnt; |
| 1654 | |
| 1655 | if (req_clk < sdhci_msm_get_min_clock(host)) { |
| 1656 | sel_clk = sdhci_msm_get_min_clock(host); |
| 1657 | return sel_clk; |
| 1658 | } |
| 1659 | |
| 1660 | for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) { |
| 1661 | if (msm_host->pdata->sup_clk_table[cnt] > req_clk) { |
| 1662 | break; |
| 1663 | } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) { |
| 1664 | sel_clk = msm_host->pdata->sup_clk_table[cnt]; |
| 1665 | break; |
| 1666 | } else { |
| 1667 | sel_clk = msm_host->pdata->sup_clk_table[cnt]; |
| 1668 | } |
| 1669 | } |
| 1670 | return sel_clk; |
| 1671 | } |
| 1672 | |
| 1673 | static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable) |
| 1674 | { |
| 1675 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1676 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1677 | int rc = 0; |
| 1678 | |
| 1679 | if (enable && !atomic_read(&msm_host->clks_on)) { |
| 1680 | pr_debug("%s: request to enable clocks\n", |
| 1681 | mmc_hostname(host->mmc)); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1682 | |
| 1683 | sdhci_msm_bus_voting(host, 1); |
| 1684 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1685 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) { |
| 1686 | rc = clk_prepare_enable(msm_host->bus_clk); |
| 1687 | if (rc) { |
| 1688 | pr_err("%s: %s: failed to enable the bus-clock with error %d\n", |
| 1689 | mmc_hostname(host->mmc), __func__, rc); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1690 | goto remove_vote; |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1691 | } |
| 1692 | } |
| 1693 | if (!IS_ERR(msm_host->pclk)) { |
| 1694 | rc = clk_prepare_enable(msm_host->pclk); |
| 1695 | if (rc) { |
| 1696 | pr_err("%s: %s: failed to enable the pclk with error %d\n", |
| 1697 | mmc_hostname(host->mmc), __func__, rc); |
| 1698 | goto disable_bus_clk; |
| 1699 | } |
| 1700 | } |
| 1701 | rc = clk_prepare_enable(msm_host->clk); |
| 1702 | if (rc) { |
| 1703 | pr_err("%s: %s: failed to enable the host-clk with error %d\n", |
| 1704 | mmc_hostname(host->mmc), __func__, rc); |
| 1705 | goto disable_pclk; |
| 1706 | } |
| 1707 | mb(); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1708 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1709 | } else if (!enable && atomic_read(&msm_host->clks_on)) { |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1710 | pr_debug("%s: request to disable clocks\n", |
| 1711 | mmc_hostname(host->mmc)); |
| 1712 | sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL); |
| 1713 | mb(); |
| 1714 | clk_disable_unprepare(msm_host->clk); |
| 1715 | if (!IS_ERR(msm_host->pclk)) |
| 1716 | clk_disable_unprepare(msm_host->pclk); |
| 1717 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) |
| 1718 | clk_disable_unprepare(msm_host->bus_clk); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1719 | |
| 1720 | sdhci_msm_bus_voting(host, 0); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1721 | } |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1722 | atomic_set(&msm_host->clks_on, enable); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1723 | goto out; |
| 1724 | disable_pclk: |
| 1725 | if (!IS_ERR_OR_NULL(msm_host->pclk)) |
| 1726 | clk_disable_unprepare(msm_host->pclk); |
| 1727 | disable_bus_clk: |
| 1728 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) |
| 1729 | clk_disable_unprepare(msm_host->bus_clk); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1730 | remove_vote: |
| 1731 | if (msm_host->msm_bus_vote.client_handle) |
| 1732 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1733 | out: |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1734 | return rc; |
| 1735 | } |
| 1736 | |
| 1737 | static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock) |
| 1738 | { |
| 1739 | int rc; |
| 1740 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 1741 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 1742 | struct mmc_ios curr_ios = host->mmc->ios; |
| 1743 | u32 sup_clock, ddr_clock; |
| 1744 | |
| 1745 | if (!clock) { |
| 1746 | sdhci_msm_prepare_clocks(host, false); |
| 1747 | host->clock = clock; |
| 1748 | goto out; |
| 1749 | } |
| 1750 | |
| 1751 | rc = sdhci_msm_prepare_clocks(host, true); |
| 1752 | if (rc) |
| 1753 | goto out; |
| 1754 | |
| 1755 | sup_clock = sdhci_msm_get_sup_clk_rate(host, clock); |
| 1756 | if (curr_ios.timing == MMC_TIMING_UHS_DDR50) { |
| 1757 | /* |
| 1758 | * The SDHC requires internal clock frequency to be double the |
| 1759 | * actual clock that will be set for DDR mode. The controller |
| 1760 | * uses the faster clock(100MHz) for some of its parts and send |
| 1761 | * the actual required clock (50MHz) to the card. |
| 1762 | */ |
| 1763 | ddr_clock = clock * 2; |
| 1764 | sup_clock = sdhci_msm_get_sup_clk_rate(host, |
| 1765 | ddr_clock); |
| 1766 | } |
| 1767 | if (sup_clock != msm_host->clk_rate) { |
| 1768 | pr_debug("%s: %s: setting clk rate to %u\n", |
| 1769 | mmc_hostname(host->mmc), __func__, sup_clock); |
| 1770 | rc = clk_set_rate(msm_host->clk, sup_clock); |
| 1771 | if (rc) { |
| 1772 | pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n", |
| 1773 | mmc_hostname(host->mmc), __func__, |
| 1774 | sup_clock, rc); |
| 1775 | goto out; |
| 1776 | } |
| 1777 | msm_host->clk_rate = sup_clock; |
| 1778 | host->clock = clock; |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1779 | /* |
| 1780 | * Update the bus vote in case of frequency change due to |
| 1781 | * clock scaling. |
| 1782 | */ |
| 1783 | sdhci_msm_bus_voting(host, 1); |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1784 | } |
| 1785 | out: |
| 1786 | sdhci_set_clock(host, clock); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1787 | } |
| 1788 | |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 1789 | static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host, |
| 1790 | unsigned int uhs) |
| 1791 | { |
| 1792 | u16 ctrl_2; |
| 1793 | |
| 1794 | ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2); |
| 1795 | /* Select Bus Speed Mode for host */ |
| 1796 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; |
| 1797 | if (uhs == MMC_TIMING_MMC_HS200) |
| 1798 | ctrl_2 |= SDHCI_CTRL_UHS_SDR104; |
| 1799 | else if (uhs == MMC_TIMING_UHS_SDR12) |
| 1800 | ctrl_2 |= SDHCI_CTRL_UHS_SDR12; |
| 1801 | else if (uhs == MMC_TIMING_UHS_SDR25) |
| 1802 | ctrl_2 |= SDHCI_CTRL_UHS_SDR25; |
| 1803 | else if (uhs == MMC_TIMING_UHS_SDR50) |
| 1804 | ctrl_2 |= SDHCI_CTRL_UHS_SDR50; |
| 1805 | else if (uhs == MMC_TIMING_UHS_SDR104) |
| 1806 | ctrl_2 |= SDHCI_CTRL_UHS_SDR104; |
| 1807 | else if (uhs == MMC_TIMING_UHS_DDR50) |
| 1808 | ctrl_2 |= SDHCI_CTRL_UHS_DDR50; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1809 | /* |
| 1810 | * When clock frquency is less than 100MHz, the feedback clock must be |
| 1811 | * provided and DLL must not be used so that tuning can be skipped. To |
| 1812 | * provide feedback clock, the mode selection can be any value less |
| 1813 | * than 3'b011 in bits [2:0] of HOST CONTROL2 register. |
| 1814 | */ |
| 1815 | if (host->clock <= (100 * 1000 * 1000) && |
| 1816 | (uhs == MMC_TIMING_MMC_HS200 || |
| 1817 | uhs == MMC_TIMING_UHS_SDR104)) |
| 1818 | ctrl_2 &= ~SDHCI_CTRL_UHS_MASK; |
| 1819 | |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 1820 | sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2); |
| 1821 | |
| 1822 | } |
| 1823 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1824 | static struct sdhci_ops sdhci_msm_ops = { |
Sahitya Tummala | 1461343 | 2013-03-21 11:13:25 +0530 | [diff] [blame] | 1825 | .set_uhs_signaling = sdhci_msm_set_uhs_signaling, |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1826 | .check_power_status = sdhci_msm_check_power_status, |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 1827 | .platform_execute_tuning = sdhci_msm_execute_tuning, |
| 1828 | .toggle_cdr = sdhci_msm_toggle_cdr, |
Asutosh Das | 648f9d1 | 2013-01-10 21:11:04 +0530 | [diff] [blame] | 1829 | .get_max_segments = sdhci_msm_max_segs, |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1830 | .set_clock = sdhci_msm_set_clock, |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1831 | .get_min_clock = sdhci_msm_get_min_clock, |
| 1832 | .get_max_clock = sdhci_msm_get_max_clock, |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1833 | }; |
| 1834 | |
| 1835 | static int sdhci_msm_probe(struct platform_device *pdev) |
| 1836 | { |
| 1837 | struct sdhci_host *host; |
| 1838 | struct sdhci_pltfm_host *pltfm_host; |
| 1839 | struct sdhci_msm_host *msm_host; |
| 1840 | struct resource *core_memres = NULL; |
| 1841 | int ret = 0, pwr_irq = 0, dead = 0; |
Stephen Boyd | 8dce5c6 | 2013-04-24 14:19:46 -0700 | [diff] [blame] | 1842 | u16 host_version; |
Subhash Jadavani | 2813734 | 2013-05-14 17:46:43 +0530 | [diff] [blame] | 1843 | u32 pwr, irq_status, irq_ctl; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1844 | |
| 1845 | pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__); |
| 1846 | msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host), |
| 1847 | GFP_KERNEL); |
| 1848 | if (!msm_host) { |
| 1849 | ret = -ENOMEM; |
| 1850 | goto out; |
| 1851 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1852 | |
| 1853 | msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops; |
| 1854 | host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata, 0); |
| 1855 | if (IS_ERR(host)) { |
| 1856 | ret = PTR_ERR(host); |
| 1857 | goto out; |
| 1858 | } |
| 1859 | |
| 1860 | pltfm_host = sdhci_priv(host); |
| 1861 | pltfm_host->priv = msm_host; |
| 1862 | msm_host->mmc = host->mmc; |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 1863 | msm_host->pdev = pdev; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1864 | |
| 1865 | /* Extract platform data */ |
| 1866 | if (pdev->dev.of_node) { |
Venkat Gopalakrishnan | 270580a | 2013-03-11 12:17:57 -0700 | [diff] [blame] | 1867 | ret = of_alias_get_id(pdev->dev.of_node, "sdhc"); |
| 1868 | if (ret < 0) { |
| 1869 | dev_err(&pdev->dev, "Failed to get slot index %d\n", |
| 1870 | ret); |
| 1871 | goto pltfm_free; |
| 1872 | } |
| 1873 | if (disable_slots & (1 << (ret - 1))) { |
| 1874 | dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__, |
| 1875 | ret); |
| 1876 | ret = -ENODEV; |
| 1877 | goto pltfm_free; |
| 1878 | } |
| 1879 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1880 | msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev); |
| 1881 | if (!msm_host->pdata) { |
| 1882 | dev_err(&pdev->dev, "DT parsing error\n"); |
| 1883 | goto pltfm_free; |
| 1884 | } |
| 1885 | } else { |
| 1886 | dev_err(&pdev->dev, "No device tree node\n"); |
| 1887 | goto pltfm_free; |
| 1888 | } |
| 1889 | |
| 1890 | /* Setup Clocks */ |
| 1891 | |
| 1892 | /* Setup SDCC bus voter clock. */ |
| 1893 | msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk"); |
| 1894 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) { |
| 1895 | /* Vote for max. clk rate for max. performance */ |
| 1896 | ret = clk_set_rate(msm_host->bus_clk, INT_MAX); |
| 1897 | if (ret) |
| 1898 | goto pltfm_free; |
| 1899 | ret = clk_prepare_enable(msm_host->bus_clk); |
| 1900 | if (ret) |
| 1901 | goto pltfm_free; |
| 1902 | } |
| 1903 | |
| 1904 | /* Setup main peripheral bus clock */ |
| 1905 | msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk"); |
| 1906 | if (!IS_ERR(msm_host->pclk)) { |
| 1907 | ret = clk_prepare_enable(msm_host->pclk); |
| 1908 | if (ret) |
| 1909 | goto bus_clk_disable; |
| 1910 | } |
| 1911 | |
| 1912 | /* Setup SDC MMC clock */ |
| 1913 | msm_host->clk = devm_clk_get(&pdev->dev, "core_clk"); |
| 1914 | if (IS_ERR(msm_host->clk)) { |
| 1915 | ret = PTR_ERR(msm_host->clk); |
| 1916 | goto pclk_disable; |
| 1917 | } |
| 1918 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1919 | /* Set to the minimum supported clock frequency */ |
| 1920 | ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host)); |
| 1921 | if (ret) { |
| 1922 | dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret); |
Sahitya Tummala | 020ede0d | 2013-06-07 13:03:07 +0530 | [diff] [blame^] | 1923 | goto pclk_disable; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1924 | } |
Sahitya Tummala | 020ede0d | 2013-06-07 13:03:07 +0530 | [diff] [blame^] | 1925 | ret = clk_prepare_enable(msm_host->clk); |
| 1926 | if (ret) |
| 1927 | goto pclk_disable; |
| 1928 | |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1929 | msm_host->clk_rate = sdhci_msm_get_min_clock(host); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 1930 | atomic_set(&msm_host->clks_on, 1); |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 1931 | |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1932 | ret = sdhci_msm_bus_register(msm_host, pdev); |
| 1933 | if (ret) |
| 1934 | goto clk_disable; |
| 1935 | |
| 1936 | if (msm_host->msm_bus_vote.client_handle) |
| 1937 | INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work, |
| 1938 | sdhci_msm_bus_work); |
| 1939 | sdhci_msm_bus_voting(host, 1); |
| 1940 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1941 | /* Setup regulators */ |
| 1942 | ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true); |
| 1943 | if (ret) { |
| 1944 | dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 1945 | goto bus_unregister; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | /* Reset the core and Enable SDHC mode */ |
| 1949 | core_memres = platform_get_resource_byname(pdev, |
| 1950 | IORESOURCE_MEM, "core_mem"); |
| 1951 | msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start, |
| 1952 | resource_size(core_memres)); |
| 1953 | |
| 1954 | if (!msm_host->core_mem) { |
| 1955 | dev_err(&pdev->dev, "Failed to remap registers\n"); |
| 1956 | ret = -ENOMEM; |
| 1957 | goto vreg_deinit; |
| 1958 | } |
| 1959 | |
| 1960 | /* Set SW_RST bit in POWER register (Offset 0x0) */ |
Sahitya Tummala | 66b0fe3 | 2013-04-25 11:50:56 +0530 | [diff] [blame] | 1961 | writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) | |
| 1962 | CORE_SW_RST, msm_host->core_mem + CORE_POWER); |
| 1963 | /* |
| 1964 | * SW reset can take upto 10HCLK + 15MCLK cycles. |
| 1965 | * Calculating based on min clk rates (hclk = 27MHz, |
| 1966 | * mclk = 400KHz) it comes to ~40us. Let's poll for |
| 1967 | * max. 1ms for reset completion. |
| 1968 | */ |
| 1969 | ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER, |
| 1970 | pwr, !(pwr & CORE_SW_RST), 100, 10); |
| 1971 | |
| 1972 | if (ret) { |
| 1973 | dev_err(&pdev->dev, "reset failed (%d)\n", ret); |
| 1974 | goto vreg_deinit; |
| 1975 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 1976 | /* Set HC_MODE_EN bit in HC_MODE register */ |
| 1977 | writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE)); |
| 1978 | |
| 1979 | /* |
Subhash Jadavani | 2813734 | 2013-05-14 17:46:43 +0530 | [diff] [blame] | 1980 | * CORE_SW_RST above may trigger power irq if previous status of PWRCTL |
| 1981 | * was either BUS_ON or IO_HIGH_V. So before we enable the power irq |
| 1982 | * interrupt in GIC (by registering the interrupt handler), we need to |
| 1983 | * ensure that any pending power irq interrupt status is acknowledged |
| 1984 | * otherwise power irq interrupt handler would be fired prematurely. |
| 1985 | */ |
| 1986 | irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS); |
| 1987 | writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR)); |
| 1988 | irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL); |
| 1989 | if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF)) |
| 1990 | irq_ctl |= CORE_PWRCTL_BUS_SUCCESS; |
| 1991 | if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW)) |
| 1992 | irq_ctl |= CORE_PWRCTL_IO_SUCCESS; |
| 1993 | writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL)); |
| 1994 | /* |
| 1995 | * Ensure that above writes are propogated before interrupt enablement |
| 1996 | * in GIC. |
| 1997 | */ |
| 1998 | mb(); |
| 1999 | |
| 2000 | /* |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2001 | * Following are the deviations from SDHC spec v3.0 - |
| 2002 | * 1. Card detection is handled using separate GPIO. |
| 2003 | * 2. Bus power control is handled by interacting with PMIC. |
| 2004 | */ |
| 2005 | host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION; |
| 2006 | host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2007 | host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN; |
| 2008 | host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK; |
Sahitya Tummala | 87d43944 | 2013-04-12 11:49:11 +0530 | [diff] [blame] | 2009 | host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD; |
Sahitya Tummala | 314162c | 2013-04-12 12:11:20 +0530 | [diff] [blame] | 2010 | host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE; |
Sahitya Tummala | 7c9780d | 2013-04-12 11:59:25 +0530 | [diff] [blame] | 2011 | host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2012 | |
Stephen Boyd | 8dce5c6 | 2013-04-24 14:19:46 -0700 | [diff] [blame] | 2013 | host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION)); |
Venkat Gopalakrishnan | a58f91f | 2012-09-17 16:00:15 -0700 | [diff] [blame] | 2014 | dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n", |
| 2015 | host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >> |
| 2016 | SDHCI_VENDOR_VER_SHIFT)); |
| 2017 | if (((host_version & SDHCI_VENDOR_VER_MASK) >> |
| 2018 | SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) { |
| 2019 | /* |
| 2020 | * Add 40us delay in interrupt handler when |
| 2021 | * operating at initialization frequency(400KHz). |
| 2022 | */ |
| 2023 | host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR; |
| 2024 | /* |
| 2025 | * Set Software Reset for DAT line in Software |
| 2026 | * Reset Register (Bit 2). |
| 2027 | */ |
| 2028 | host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT; |
| 2029 | } |
| 2030 | |
| 2031 | /* Setup PWRCTL irq */ |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2032 | pwr_irq = platform_get_irq_byname(pdev, "pwr_irq"); |
| 2033 | if (pwr_irq < 0) { |
| 2034 | dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n", |
| 2035 | pwr_irq); |
| 2036 | goto vreg_deinit; |
| 2037 | } |
| 2038 | ret = devm_request_threaded_irq(&pdev->dev, pwr_irq, NULL, |
| 2039 | sdhci_msm_pwr_irq, IRQF_ONESHOT, |
Venkat Gopalakrishnan | 7944a37 | 2012-09-11 16:13:31 -0700 | [diff] [blame] | 2040 | dev_name(&pdev->dev), host); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2041 | if (ret) { |
| 2042 | dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n", |
| 2043 | pwr_irq, ret); |
| 2044 | goto vreg_deinit; |
| 2045 | } |
| 2046 | |
| 2047 | /* Enable pwr irq interrupts */ |
| 2048 | writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK)); |
| 2049 | |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2050 | #ifdef CONFIG_MMC_CLKGATE |
| 2051 | /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */ |
| 2052 | msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY; |
| 2053 | #endif |
| 2054 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2055 | /* Set host capabilities */ |
| 2056 | msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width; |
| 2057 | msm_host->mmc->caps |= msm_host->pdata->caps; |
Sahitya Tummala | 7bd9906 | 2013-02-28 12:21:58 +0530 | [diff] [blame] | 2058 | msm_host->mmc->caps |= MMC_CAP_HW_RESET; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2059 | msm_host->mmc->caps2 |= msm_host->pdata->caps2; |
| 2060 | msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR; |
| 2061 | msm_host->mmc->caps2 |= MMC_CAP2_PACKED_WR_CONTROL; |
Sahitya Tummala | 22dd336 | 2013-02-28 19:50:51 +0530 | [diff] [blame] | 2062 | msm_host->mmc->caps2 |= MMC_CAP2_CLK_SCALE; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2063 | |
| 2064 | if (msm_host->pdata->nonremovable) |
| 2065 | msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE; |
| 2066 | |
Sahitya Tummala | c6f48d4 | 2013-03-10 07:03:17 +0530 | [diff] [blame] | 2067 | host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us; |
| 2068 | |
Sahitya Tummala | 1f52eaa | 2013-03-20 19:24:01 +0530 | [diff] [blame] | 2069 | init_completion(&msm_host->pwr_irq_completion); |
| 2070 | |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 2071 | if (gpio_is_valid(msm_host->pdata->status_gpio)) { |
| 2072 | ret = mmc_gpio_request_cd(msm_host->mmc, |
| 2073 | msm_host->pdata->status_gpio, 0); |
| 2074 | if (ret) { |
| 2075 | dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n", |
| 2076 | __func__, ret); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2077 | goto vreg_deinit; |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 2078 | } |
| 2079 | } |
| 2080 | |
Sahitya Tummala | eaa2186 | 2013-03-20 19:34:59 +0530 | [diff] [blame] | 2081 | if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) { |
| 2082 | host->dma_mask = DMA_BIT_MASK(32); |
| 2083 | mmc_dev(host->mmc)->dma_mask = &host->dma_mask; |
| 2084 | } else { |
| 2085 | dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__); |
| 2086 | } |
| 2087 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2088 | ret = sdhci_add_host(host); |
| 2089 | if (ret) { |
| 2090 | dev_err(&pdev->dev, "Add host failed (%d)\n", ret); |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 2091 | goto vreg_deinit; |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2092 | } |
| 2093 | |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 2094 | msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw; |
| 2095 | msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw; |
| 2096 | sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr); |
| 2097 | msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw"; |
| 2098 | msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR; |
| 2099 | ret = device_create_file(&pdev->dev, |
| 2100 | &msm_host->msm_bus_vote.max_bus_bw); |
| 2101 | if (ret) |
| 2102 | goto remove_host; |
| 2103 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2104 | /* Successful initialization */ |
| 2105 | goto out; |
| 2106 | |
| 2107 | remove_host: |
| 2108 | dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff); |
| 2109 | sdhci_remove_host(host, dead); |
| 2110 | vreg_deinit: |
| 2111 | sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false); |
Sahitya Tummala | 79edc6a | 2013-05-23 15:59:22 +0530 | [diff] [blame] | 2112 | bus_unregister: |
| 2113 | if (msm_host->msm_bus_vote.client_handle) |
| 2114 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 2115 | sdhci_msm_bus_unregister(msm_host); |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2116 | clk_disable: |
| 2117 | if (!IS_ERR(msm_host->clk)) |
| 2118 | clk_disable_unprepare(msm_host->clk); |
| 2119 | pclk_disable: |
| 2120 | if (!IS_ERR(msm_host->pclk)) |
| 2121 | clk_disable_unprepare(msm_host->pclk); |
| 2122 | bus_clk_disable: |
| 2123 | if (!IS_ERR_OR_NULL(msm_host->bus_clk)) |
| 2124 | clk_disable_unprepare(msm_host->bus_clk); |
| 2125 | pltfm_free: |
| 2126 | sdhci_pltfm_free(pdev); |
| 2127 | out: |
| 2128 | pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__); |
| 2129 | return ret; |
| 2130 | } |
| 2131 | |
| 2132 | static int sdhci_msm_remove(struct platform_device *pdev) |
| 2133 | { |
| 2134 | struct sdhci_host *host = platform_get_drvdata(pdev); |
| 2135 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 2136 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 2137 | struct sdhci_msm_pltfm_data *pdata = msm_host->pdata; |
| 2138 | int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == |
| 2139 | 0xffffffff); |
| 2140 | |
| 2141 | pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 2142 | 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] | 2143 | sdhci_remove_host(host, dead); |
| 2144 | sdhci_pltfm_free(pdev); |
Sahitya Tummala | 581df13 | 2013-03-12 14:57:46 +0530 | [diff] [blame] | 2145 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2146 | sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false); |
Sahitya Tummala | a7f3c57 | 2013-01-11 11:30:45 +0530 | [diff] [blame] | 2147 | |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2148 | if (pdata->pin_data) |
| 2149 | sdhci_msm_setup_gpio(pdata, false); |
Sahitya Tummala | 8a3e818 | 2013-03-10 14:12:52 +0530 | [diff] [blame] | 2150 | |
| 2151 | if (msm_host->msm_bus_vote.client_handle) { |
| 2152 | sdhci_msm_bus_cancel_work_and_set_vote(host, 0); |
| 2153 | sdhci_msm_bus_unregister(msm_host); |
| 2154 | } |
Asutosh Das | 0ef2481 | 2012-12-18 16:14:02 +0530 | [diff] [blame] | 2155 | return 0; |
| 2156 | } |
| 2157 | |
| 2158 | static const struct of_device_id sdhci_msm_dt_match[] = { |
| 2159 | {.compatible = "qcom,sdhci-msm"}, |
| 2160 | }; |
| 2161 | MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match); |
| 2162 | |
| 2163 | static struct platform_driver sdhci_msm_driver = { |
| 2164 | .probe = sdhci_msm_probe, |
| 2165 | .remove = sdhci_msm_remove, |
| 2166 | .driver = { |
| 2167 | .name = "sdhci_msm", |
| 2168 | .owner = THIS_MODULE, |
| 2169 | .of_match_table = sdhci_msm_dt_match, |
| 2170 | }, |
| 2171 | }; |
| 2172 | |
| 2173 | module_platform_driver(sdhci_msm_driver); |
| 2174 | |
| 2175 | MODULE_DESCRIPTION("Qualcomm Technologies, Inc. Secure Digital Host Controller Interface driver"); |
| 2176 | MODULE_LICENSE("GPL v2"); |