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