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