Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/highmem.h> |
| 15 | #include <linux/io.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/scatterlist.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/blkdev.h> |
| 22 | |
| 23 | #include <linux/mmc/mmc.h> |
| 24 | #include <linux/mmc/host.h> |
| 25 | #include <linux/mmc/card.h> |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 27 | #include <linux/workqueue.h> |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 28 | |
| 29 | #include "cmdq_hci.h" |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 30 | #include "sdhci.h" |
| 31 | #include "sdhci-msm.h" |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 32 | |
| 33 | #define DCMD_SLOT 31 |
| 34 | #define NUM_SLOTS 32 |
| 35 | |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 36 | /* 1 sec */ |
| 37 | #define HALT_TIMEOUT_MS 1000 |
| 38 | |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 39 | #ifdef CONFIG_PM_RUNTIME |
| 40 | static int cmdq_runtime_pm_get(struct cmdq_host *host) |
| 41 | { |
| 42 | return pm_runtime_get_sync(host->mmc->parent); |
| 43 | } |
| 44 | static int cmdq_runtime_pm_put(struct cmdq_host *host) |
| 45 | { |
| 46 | pm_runtime_mark_last_busy(host->mmc->parent); |
| 47 | return pm_runtime_put_autosuspend(host->mmc->parent); |
| 48 | } |
| 49 | #else |
| 50 | static inline int cmdq_runtime_pm_get(struct cmdq_host *host) |
| 51 | { |
| 52 | return 0; |
| 53 | } |
| 54 | static inline int cmdq_runtime_pm_put(struct cmdq_host *host) |
| 55 | { |
| 56 | return 0; |
| 57 | } |
| 58 | #endif |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 59 | static inline struct mmc_request *get_req_by_tag(struct cmdq_host *cq_host, |
| 60 | unsigned int tag) |
| 61 | { |
| 62 | return cq_host->mrq_slot[tag]; |
| 63 | } |
| 64 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 65 | static inline u8 *get_desc(struct cmdq_host *cq_host, u8 tag) |
| 66 | { |
| 67 | return cq_host->desc_base + (tag * cq_host->slot_sz); |
| 68 | } |
| 69 | |
| 70 | static inline u8 *get_link_desc(struct cmdq_host *cq_host, u8 tag) |
| 71 | { |
| 72 | u8 *desc = get_desc(cq_host, tag); |
| 73 | |
| 74 | return desc + cq_host->task_desc_len; |
| 75 | } |
| 76 | |
| 77 | static inline dma_addr_t get_trans_desc_dma(struct cmdq_host *cq_host, u8 tag) |
| 78 | { |
| 79 | return cq_host->trans_desc_dma_base + |
| 80 | (cq_host->mmc->max_segs * tag * |
| 81 | cq_host->trans_desc_len); |
| 82 | } |
| 83 | |
| 84 | static inline u8 *get_trans_desc(struct cmdq_host *cq_host, u8 tag) |
| 85 | { |
| 86 | return cq_host->trans_desc_base + |
| 87 | (cq_host->trans_desc_len * cq_host->mmc->max_segs * tag); |
| 88 | } |
| 89 | |
| 90 | static void setup_trans_desc(struct cmdq_host *cq_host, u8 tag) |
| 91 | { |
| 92 | u8 *link_temp; |
| 93 | dma_addr_t trans_temp; |
| 94 | |
| 95 | link_temp = get_link_desc(cq_host, tag); |
| 96 | trans_temp = get_trans_desc_dma(cq_host, tag); |
| 97 | |
| 98 | memset(link_temp, 0, cq_host->link_desc_len); |
| 99 | if (cq_host->link_desc_len > 8) |
| 100 | *(link_temp + 8) = 0; |
| 101 | |
| 102 | if (tag == DCMD_SLOT) { |
| 103 | *link_temp = VALID(0) | ACT(0) | END(1); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | *link_temp = VALID(1) | ACT(0x6) | END(0); |
| 108 | |
| 109 | if (cq_host->dma64) { |
| 110 | __le64 *data_addr = (__le64 __force *)(link_temp + 4); |
| 111 | data_addr[0] = cpu_to_le64(trans_temp); |
| 112 | } else { |
| 113 | __le32 *data_addr = (__le32 __force *)(link_temp + 4); |
| 114 | data_addr[0] = cpu_to_le32(trans_temp); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static void cmdq_clear_set_irqs(struct cmdq_host *cq_host, u32 clear, u32 set) |
| 119 | { |
| 120 | u32 ier; |
| 121 | |
| 122 | ier = cmdq_readl(cq_host, CQISTE); |
| 123 | ier &= ~clear; |
| 124 | ier |= set; |
| 125 | cmdq_writel(cq_host, ier, CQISTE); |
| 126 | cmdq_writel(cq_host, ier, CQISGE); |
| 127 | /* ensure the writes are done */ |
| 128 | mb(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | #define DRV_NAME "cmdq-host" |
| 133 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 134 | static void cmdq_dumpregs(struct cmdq_host *cq_host) |
| 135 | { |
| 136 | struct mmc_host *mmc = cq_host->mmc; |
| 137 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 138 | pr_err(DRV_NAME ": ========== REGISTER DUMP (%s)==========\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 139 | mmc_hostname(mmc)); |
| 140 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 141 | pr_err(DRV_NAME ": Caps: 0x%08x | Version: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 142 | cmdq_readl(cq_host, CQCAP), |
| 143 | cmdq_readl(cq_host, CQVER)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 144 | pr_err(DRV_NAME ": Queing config: 0x%08x | Queue Ctrl: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 145 | cmdq_readl(cq_host, CQCFG), |
| 146 | cmdq_readl(cq_host, CQCTL)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 147 | pr_err(DRV_NAME ": Int stat: 0x%08x | Int enab: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 148 | cmdq_readl(cq_host, CQIS), |
| 149 | cmdq_readl(cq_host, CQISTE)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 150 | pr_err(DRV_NAME ": Int sig: 0x%08x | Int Coal: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 151 | cmdq_readl(cq_host, CQISGE), |
| 152 | cmdq_readl(cq_host, CQIC)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 153 | pr_err(DRV_NAME ": TDL base: 0x%08x | TDL up32: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 154 | cmdq_readl(cq_host, CQTDLBA), |
| 155 | cmdq_readl(cq_host, CQTDLBAU)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 156 | pr_err(DRV_NAME ": Doorbell: 0x%08x | Comp Notif: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 157 | cmdq_readl(cq_host, CQTDBR), |
| 158 | cmdq_readl(cq_host, CQTCN)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 159 | pr_err(DRV_NAME ": Dev queue: 0x%08x | Dev Pend: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 160 | cmdq_readl(cq_host, CQDQS), |
| 161 | cmdq_readl(cq_host, CQDPT)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 162 | pr_err(DRV_NAME ": Task clr: 0x%08x | Send stat 1: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 163 | cmdq_readl(cq_host, CQTCLR), |
| 164 | cmdq_readl(cq_host, CQSSC1)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 165 | pr_err(DRV_NAME ": Send stat 2: 0x%08x | DCMD resp: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 166 | cmdq_readl(cq_host, CQSSC2), |
| 167 | cmdq_readl(cq_host, CQCRDCT)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 168 | pr_err(DRV_NAME ": Resp err mask: 0x%08x | Task err: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 169 | cmdq_readl(cq_host, CQRMEM), |
| 170 | cmdq_readl(cq_host, CQTERRI)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 171 | pr_err(DRV_NAME ": Resp idx 0x%08x | Resp arg: 0x%08x\n", |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 172 | cmdq_readl(cq_host, CQCRI), |
| 173 | cmdq_readl(cq_host, CQCRA)); |
Asutosh Das | c0ed9c4 | 2015-05-29 15:39:37 +0530 | [diff] [blame] | 174 | pr_err(DRV_NAME": Vendor cfg 0x%08x\n", |
| 175 | cmdq_readl(cq_host, CQ_VENDOR_CFG)); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 176 | pr_err(DRV_NAME ": ===========================================\n"); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 177 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 178 | if (cq_host->ops->dump_vendor_regs) |
| 179 | cq_host->ops->dump_vendor_regs(mmc); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * The allocated descriptor table for task, link & transfer descritors |
| 184 | * looks like: |
| 185 | * |----------| |
| 186 | * |task desc | |->|----------| |
| 187 | * |----------| | |trans desc| |
| 188 | * |link desc-|->| |----------| |
| 189 | * |----------| . |
| 190 | * . . |
| 191 | * no. of slots max-segs |
| 192 | * . |----------| |
| 193 | * |----------| |
| 194 | * The idea here is to create the [task+trans] table and mark & point the |
| 195 | * link desc to the transfer desc table on a per slot basis. |
| 196 | */ |
| 197 | static int cmdq_host_alloc_tdl(struct cmdq_host *cq_host) |
| 198 | { |
| 199 | |
| 200 | size_t desc_size; |
| 201 | size_t data_size; |
| 202 | int i = 0; |
| 203 | |
| 204 | /* task descriptor can be 64/128 bit irrespective of arch */ |
| 205 | if (cq_host->caps & CMDQ_TASK_DESC_SZ_128) { |
| 206 | cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) | |
| 207 | CQ_TASK_DESC_SZ, CQCFG); |
| 208 | cq_host->task_desc_len = 16; |
| 209 | } else { |
| 210 | cq_host->task_desc_len = 8; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * 96 bits length of transfer desc instead of 128 bits which means |
| 215 | * ADMA would expect next valid descriptor at the 96th bit |
| 216 | * or 128th bit |
| 217 | */ |
| 218 | if (cq_host->dma64) { |
| 219 | if (cq_host->quirks & CMDQ_QUIRK_SHORT_TXFR_DESC_SZ) |
| 220 | cq_host->trans_desc_len = 12; |
| 221 | else |
| 222 | cq_host->trans_desc_len = 16; |
| 223 | cq_host->link_desc_len = 16; |
| 224 | } else { |
| 225 | cq_host->trans_desc_len = 8; |
| 226 | cq_host->link_desc_len = 8; |
| 227 | } |
| 228 | |
| 229 | /* total size of a slot: 1 task & 1 transfer (link) */ |
| 230 | cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len; |
| 231 | |
| 232 | desc_size = cq_host->slot_sz * cq_host->num_slots; |
| 233 | |
| 234 | data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs * |
| 235 | (cq_host->num_slots - 1); |
| 236 | |
| 237 | pr_info("%s: desc_size: %d data_sz: %d slot-sz: %d\n", __func__, |
| 238 | (int)desc_size, (int)data_size, cq_host->slot_sz); |
| 239 | |
| 240 | /* |
| 241 | * allocate a dma-mapped chunk of memory for the descriptors |
| 242 | * allocate a dma-mapped chunk of memory for link descriptors |
| 243 | * setup each link-desc memory offset per slot-number to |
| 244 | * the descriptor table. |
| 245 | */ |
| 246 | cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc), |
| 247 | desc_size, |
| 248 | &cq_host->desc_dma_base, |
| 249 | GFP_KERNEL); |
| 250 | cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc), |
| 251 | data_size, |
| 252 | &cq_host->trans_desc_dma_base, |
| 253 | GFP_KERNEL); |
| 254 | if (!cq_host->desc_base || !cq_host->trans_desc_base) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | pr_info("desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n", |
| 258 | cq_host->desc_base, cq_host->trans_desc_base, |
| 259 | (unsigned long long)cq_host->desc_dma_base, |
| 260 | (unsigned long long) cq_host->trans_desc_dma_base); |
| 261 | |
| 262 | for (; i < (cq_host->num_slots); i++) |
| 263 | setup_trans_desc(cq_host, i); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int cmdq_enable(struct mmc_host *mmc) |
| 269 | { |
| 270 | int err = 0; |
| 271 | u32 cqcfg; |
| 272 | bool dcmd_enable; |
| 273 | struct cmdq_host *cq_host = mmc_cmdq_private(mmc); |
| 274 | |
| 275 | if (!cq_host || !mmc->card || !mmc_card_cmdq(mmc->card)) { |
| 276 | err = -EINVAL; |
| 277 | goto out; |
| 278 | } |
| 279 | |
| 280 | if (cq_host->enabled) |
| 281 | goto out; |
| 282 | |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 283 | cmdq_runtime_pm_get(cq_host); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 284 | cqcfg = cmdq_readl(cq_host, CQCFG); |
| 285 | if (cqcfg & 0x1) { |
| 286 | pr_info("%s: %s: cq_host is already enabled\n", |
| 287 | mmc_hostname(mmc), __func__); |
| 288 | WARN_ON(1); |
Venkat Gopalakrishnan | 632b13b | 2015-08-24 14:36:59 -0700 | [diff] [blame] | 289 | goto pm_ref_count; |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | if (cq_host->quirks & CMDQ_QUIRK_NO_DCMD) |
| 293 | dcmd_enable = false; |
| 294 | else |
| 295 | dcmd_enable = true; |
| 296 | |
| 297 | cqcfg = ((cq_host->caps & CMDQ_TASK_DESC_SZ_128 ? CQ_TASK_DESC_SZ : 0) | |
| 298 | (dcmd_enable ? CQ_DCMD : 0)); |
| 299 | |
| 300 | cmdq_writel(cq_host, cqcfg, CQCFG); |
| 301 | /* enable CQ_HOST */ |
| 302 | cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) | CQ_ENABLE, |
| 303 | CQCFG); |
| 304 | |
| 305 | if (!cq_host->desc_base || |
| 306 | !cq_host->trans_desc_base) { |
| 307 | err = cmdq_host_alloc_tdl(cq_host); |
| 308 | if (err) |
Venkat Gopalakrishnan | 632b13b | 2015-08-24 14:36:59 -0700 | [diff] [blame] | 309 | goto pm_ref_count; |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 310 | } |
| 311 | |
Konstantin Dorfman | 14c902d | 2015-06-11 11:33:23 +0300 | [diff] [blame] | 312 | cmdq_writel(cq_host, lower_32_bits(cq_host->desc_dma_base), CQTDLBA); |
| 313 | cmdq_writel(cq_host, upper_32_bits(cq_host->desc_dma_base), CQTDLBAU); |
| 314 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 315 | /* |
| 316 | * disable all vendor interrupts |
| 317 | * enable CMDQ interrupts |
| 318 | * enable the vendor error interrupts |
| 319 | */ |
| 320 | if (cq_host->ops->clear_set_irqs) |
| 321 | cq_host->ops->clear_set_irqs(mmc, true); |
| 322 | |
| 323 | cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL); |
| 324 | |
| 325 | /* cq_host would use this rca to address the card */ |
| 326 | cmdq_writel(cq_host, mmc->card->rca, CQSSC2); |
| 327 | |
| 328 | /* send QSR at lesser intervals than the default */ |
| 329 | cmdq_writel(cq_host, cmdq_readl(cq_host, CQSSC1) | SEND_QSR_INTERVAL, |
| 330 | CQSSC1); |
| 331 | |
Dov Levenglick | 2b67830 | 2015-07-01 14:24:20 +0300 | [diff] [blame] | 332 | /* enable bkops exception indication */ |
Dov Levenglick | aea348b | 2015-07-20 11:59:52 +0300 | [diff] [blame] | 333 | if (mmc_card_configured_manual_bkops(mmc->card) && |
| 334 | !mmc_card_configured_auto_bkops(mmc->card)) |
Dov Levenglick | 2b67830 | 2015-07-01 14:24:20 +0300 | [diff] [blame] | 335 | cmdq_writel(cq_host, cmdq_readl(cq_host, CQRMEM) | CQ_EXCEPTION, |
| 336 | CQRMEM); |
| 337 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 338 | /* ensure the writes are done before enabling CQE */ |
| 339 | mb(); |
| 340 | |
| 341 | cq_host->enabled = true; |
| 342 | |
| 343 | if (cq_host->ops->set_block_size) |
| 344 | cq_host->ops->set_block_size(cq_host->mmc); |
| 345 | |
| 346 | if (cq_host->ops->set_data_timeout) |
| 347 | cq_host->ops->set_data_timeout(mmc, 0xf); |
| 348 | |
| 349 | if (cq_host->ops->clear_set_dumpregs) |
| 350 | cq_host->ops->clear_set_dumpregs(mmc, 1); |
| 351 | |
Ritesh Harjani | 6b2ea57 | 2015-07-15 13:23:05 +0530 | [diff] [blame] | 352 | if (cq_host->ops->enhanced_strobe_mask) |
| 353 | cq_host->ops->enhanced_strobe_mask(mmc, true); |
Venkat Gopalakrishnan | 632b13b | 2015-08-24 14:36:59 -0700 | [diff] [blame] | 354 | |
| 355 | pm_ref_count: |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 356 | cmdq_runtime_pm_put(cq_host); |
Venkat Gopalakrishnan | 632b13b | 2015-08-24 14:36:59 -0700 | [diff] [blame] | 357 | out: |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 358 | return err; |
| 359 | } |
| 360 | |
| 361 | static void cmdq_disable(struct mmc_host *mmc, bool soft) |
| 362 | { |
| 363 | struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc); |
| 364 | |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 365 | cmdq_runtime_pm_get(cq_host); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 366 | if (soft) { |
| 367 | cmdq_writel(cq_host, cmdq_readl( |
| 368 | cq_host, CQCFG) & ~(CQ_ENABLE), |
| 369 | CQCFG); |
| 370 | } |
Ritesh Harjani | 6b2ea57 | 2015-07-15 13:23:05 +0530 | [diff] [blame] | 371 | if (cq_host->ops->enhanced_strobe_mask) |
| 372 | cq_host->ops->enhanced_strobe_mask(mmc, false); |
| 373 | |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 374 | cmdq_runtime_pm_put(cq_host); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 375 | cq_host->enabled = false; |
| 376 | } |
| 377 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 378 | static void cmdq_reset(struct mmc_host *mmc, bool soft) |
| 379 | { |
| 380 | struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc); |
| 381 | unsigned int cqcfg; |
| 382 | unsigned int tdlba; |
| 383 | unsigned int tdlbau; |
| 384 | unsigned int rca; |
| 385 | int ret; |
| 386 | |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 387 | cmdq_runtime_pm_get(cq_host); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 388 | cqcfg = cmdq_readl(cq_host, CQCFG); |
| 389 | tdlba = cmdq_readl(cq_host, CQTDLBA); |
| 390 | tdlbau = cmdq_readl(cq_host, CQTDLBAU); |
| 391 | rca = cmdq_readl(cq_host, CQSSC2); |
| 392 | |
| 393 | cmdq_disable(mmc, true); |
| 394 | |
| 395 | if (cq_host->ops->reset) { |
| 396 | ret = cq_host->ops->reset(mmc); |
| 397 | if (ret) { |
| 398 | pr_crit("%s: reset CMDQ controller: failed\n", |
| 399 | mmc_hostname(mmc)); |
| 400 | BUG(); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | cmdq_writel(cq_host, tdlba, CQTDLBA); |
| 405 | cmdq_writel(cq_host, tdlbau, CQTDLBAU); |
| 406 | |
| 407 | if (cq_host->ops->clear_set_irqs) |
| 408 | cq_host->ops->clear_set_irqs(mmc, true); |
| 409 | |
| 410 | cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL); |
| 411 | |
| 412 | /* cq_host would use this rca to address the card */ |
| 413 | cmdq_writel(cq_host, rca, CQSSC2); |
| 414 | |
| 415 | /* ensure the writes are done before enabling CQE */ |
| 416 | mb(); |
| 417 | |
| 418 | cmdq_writel(cq_host, cqcfg, CQCFG); |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 419 | cmdq_runtime_pm_put(cq_host); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 420 | cq_host->enabled = true; |
| 421 | } |
| 422 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 423 | static void cmdq_prep_task_desc(struct mmc_request *mrq, |
| 424 | u64 *data, bool intr, bool qbr) |
| 425 | { |
| 426 | struct mmc_cmdq_req *cmdq_req = mrq->cmdq_req; |
| 427 | u32 req_flags = cmdq_req->cmdq_req_flags; |
| 428 | |
| 429 | pr_debug("%s: %s: data-tag: 0x%08x - dir: %d - prio: %d - cnt: 0x%08x - addr: 0x%llx\n", |
| 430 | mmc_hostname(mrq->host), __func__, |
| 431 | !!(req_flags & DAT_TAG), !!(req_flags & DIR), |
| 432 | !!(req_flags & PRIO), cmdq_req->data.blocks, |
| 433 | (u64)mrq->cmdq_req->blk_addr); |
| 434 | |
| 435 | *data = VALID(1) | |
| 436 | END(1) | |
| 437 | INT(intr) | |
| 438 | ACT(0x5) | |
| 439 | FORCED_PROG(!!(req_flags & FORCED_PRG)) | |
| 440 | CONTEXT(mrq->cmdq_req->ctx_id) | |
| 441 | DATA_TAG(!!(req_flags & DAT_TAG)) | |
| 442 | DATA_DIR(!!(req_flags & DIR)) | |
| 443 | PRIORITY(!!(req_flags & PRIO)) | |
| 444 | QBAR(qbr) | |
| 445 | REL_WRITE(!!(req_flags & REL_WR)) | |
| 446 | BLK_COUNT(mrq->cmdq_req->data.blocks) | |
| 447 | BLK_ADDR((u64)mrq->cmdq_req->blk_addr); |
| 448 | } |
| 449 | |
| 450 | static int cmdq_dma_map(struct mmc_host *host, struct mmc_request *mrq) |
| 451 | { |
| 452 | int sg_count; |
| 453 | struct mmc_data *data = mrq->data; |
| 454 | |
| 455 | if (!data) |
| 456 | return -EINVAL; |
| 457 | |
| 458 | sg_count = dma_map_sg(mmc_dev(host), data->sg, |
| 459 | data->sg_len, |
| 460 | (data->flags & MMC_DATA_WRITE) ? |
| 461 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 462 | if (!sg_count) { |
| 463 | pr_err("%s: sg-len: %d\n", __func__, data->sg_len); |
| 464 | return -ENOMEM; |
| 465 | } |
| 466 | |
| 467 | return sg_count; |
| 468 | } |
| 469 | |
Sahitya Tummala | 78a68e5 | 2015-09-30 15:55:41 +0530 | [diff] [blame^] | 470 | static void cmdq_set_tran_desc(u8 *desc, dma_addr_t addr, int len, |
| 471 | bool end, bool is_dma64) |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 472 | { |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 473 | __le32 *attr = (__le32 __force *)desc; |
| 474 | |
| 475 | *attr = (VALID(1) | |
| 476 | END(end ? 1 : 0) | |
| 477 | INT(0) | |
| 478 | ACT(0x4) | |
| 479 | DAT_LENGTH(len)); |
| 480 | |
Sahitya Tummala | 78a68e5 | 2015-09-30 15:55:41 +0530 | [diff] [blame^] | 481 | if (is_dma64) { |
| 482 | __le64 *dataddr = (__le64 __force *)(desc + 4); |
| 483 | |
| 484 | dataddr[0] = cpu_to_le64(addr); |
| 485 | } else { |
| 486 | __le32 *dataddr = (__le32 __force *)(desc + 4); |
| 487 | |
| 488 | dataddr[0] = cpu_to_le32(addr); |
| 489 | } |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static int cmdq_prep_tran_desc(struct mmc_request *mrq, |
| 493 | struct cmdq_host *cq_host, int tag) |
| 494 | { |
| 495 | struct mmc_data *data = mrq->data; |
| 496 | int i, sg_count, len; |
| 497 | bool end = false; |
| 498 | dma_addr_t addr; |
| 499 | u8 *desc; |
| 500 | struct scatterlist *sg; |
| 501 | |
| 502 | sg_count = cmdq_dma_map(mrq->host, mrq); |
| 503 | if (sg_count < 0) { |
| 504 | pr_err("%s: %s: unable to map sg lists, %d\n", |
| 505 | mmc_hostname(mrq->host), __func__, sg_count); |
| 506 | return sg_count; |
| 507 | } |
| 508 | |
| 509 | desc = get_trans_desc(cq_host, tag); |
| 510 | memset(desc, 0, cq_host->trans_desc_len * cq_host->mmc->max_segs); |
| 511 | |
| 512 | for_each_sg(data->sg, sg, sg_count, i) { |
| 513 | addr = sg_dma_address(sg); |
| 514 | len = sg_dma_len(sg); |
| 515 | |
| 516 | if ((i+1) == sg_count) |
| 517 | end = true; |
Sahitya Tummala | 78a68e5 | 2015-09-30 15:55:41 +0530 | [diff] [blame^] | 518 | cmdq_set_tran_desc(desc, addr, len, end, cq_host->dma64); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 519 | desc += cq_host->trans_desc_len; |
| 520 | } |
| 521 | |
| 522 | pr_debug("%s: req: 0x%p tag: %d calc_trans_des: 0x%p sg-cnt: %d\n", |
| 523 | __func__, mrq->req, tag, desc, sg_count); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static void cmdq_prep_dcmd_desc(struct mmc_host *mmc, |
| 529 | struct mmc_request *mrq) |
| 530 | { |
| 531 | u64 *task_desc = NULL; |
| 532 | u64 data = 0; |
| 533 | u8 resp_type; |
| 534 | u8 *desc; |
| 535 | __le64 *dataddr; |
| 536 | struct cmdq_host *cq_host = mmc_cmdq_private(mmc); |
| 537 | u8 timing; |
| 538 | |
| 539 | if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) { |
| 540 | resp_type = 0x0; |
| 541 | timing = 0x1; |
| 542 | } else { |
Sahitya Tummala | 72bd840 | 2015-05-29 13:27:38 +0530 | [diff] [blame] | 543 | if (mrq->cmd->flags & MMC_RSP_BUSY) { |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 544 | resp_type = 0x3; |
| 545 | timing = 0x0; |
| 546 | } else { |
| 547 | resp_type = 0x2; |
| 548 | timing = 0x1; |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot); |
| 553 | memset(task_desc, 0, cq_host->task_desc_len); |
| 554 | data |= (VALID(1) | |
| 555 | END(1) | |
| 556 | INT(1) | |
| 557 | QBAR(1) | |
| 558 | ACT(0x5) | |
| 559 | CMD_INDEX(mrq->cmd->opcode) | |
| 560 | CMD_TIMING(timing) | RESP_TYPE(resp_type)); |
| 561 | *task_desc |= data; |
| 562 | desc = (u8 *)task_desc; |
| 563 | pr_debug("cmdq: dcmd: cmd: %d timing: %d resp: %d\n", |
| 564 | mrq->cmd->opcode, timing, resp_type); |
| 565 | dataddr = (__le64 __force *)(desc + 4); |
| 566 | dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg); |
| 567 | |
| 568 | } |
| 569 | |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 570 | static void cmdq_pm_qos_vote(struct sdhci_host *host, struct mmc_request *mrq) |
| 571 | { |
| 572 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 573 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 574 | |
| 575 | sdhci_msm_pm_qos_cpu_vote(host, |
| 576 | msm_host->pdata->pm_qos_data.cmdq_latency, mrq->req->cpu); |
| 577 | } |
| 578 | |
| 579 | static void cmdq_pm_qos_unvote(struct sdhci_host *host, struct mmc_request *mrq) |
| 580 | { |
| 581 | /* use async as we're inside an atomic context (soft-irq) */ |
| 582 | sdhci_msm_pm_qos_cpu_unvote(host, mrq->req->cpu, true); |
| 583 | } |
| 584 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 585 | static int cmdq_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 586 | { |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 587 | int err = 0; |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 588 | u64 data = 0; |
| 589 | u64 *task_desc = NULL; |
| 590 | u32 tag = mrq->cmdq_req->tag; |
| 591 | struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc); |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 592 | struct sdhci_host *host = mmc_priv(mmc); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 593 | |
| 594 | if (!cq_host->enabled) { |
| 595 | pr_err("%s: CMDQ host not enabled yet !!!\n", |
| 596 | mmc_hostname(mmc)); |
| 597 | err = -EINVAL; |
| 598 | goto out; |
| 599 | } |
| 600 | |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 601 | cmdq_runtime_pm_get(cq_host); |
| 602 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 603 | if (mrq->cmdq_req->cmdq_req_flags & DCMD) { |
| 604 | cmdq_prep_dcmd_desc(mmc, mrq); |
| 605 | cq_host->mrq_slot[DCMD_SLOT] = mrq; |
Venkat Gopalakrishnan | f1329ce | 2015-08-10 14:55:23 -0700 | [diff] [blame] | 606 | /* DCMD's are always issued on a fixed slot */ |
| 607 | tag = DCMD_SLOT; |
| 608 | goto ring_doorbell; |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | task_desc = (__le64 __force *)get_desc(cq_host, tag); |
| 612 | |
| 613 | cmdq_prep_task_desc(mrq, &data, 1, |
| 614 | (mrq->cmdq_req->cmdq_req_flags & QBR)); |
| 615 | *task_desc = cpu_to_le64(data); |
| 616 | |
| 617 | err = cmdq_prep_tran_desc(mrq, cq_host, tag); |
| 618 | if (err) { |
| 619 | pr_err("%s: %s: failed to setup tx desc: %d\n", |
| 620 | mmc_hostname(mmc), __func__, err); |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 621 | goto out; |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | BUG_ON(cmdq_readl(cq_host, CQTDBR) & (1 << tag)); |
| 625 | |
| 626 | cq_host->mrq_slot[tag] = mrq; |
| 627 | if (cq_host->ops->set_tranfer_params) |
| 628 | cq_host->ops->set_tranfer_params(mmc); |
| 629 | |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 630 | /* PM QoS */ |
| 631 | sdhci_msm_pm_qos_irq_vote(host); |
| 632 | cmdq_pm_qos_vote(host, mrq); |
Venkat Gopalakrishnan | f1329ce | 2015-08-10 14:55:23 -0700 | [diff] [blame] | 633 | ring_doorbell: |
| 634 | /* Ensure the task descriptor list is flushed before ringing doorbell */ |
| 635 | wmb(); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 636 | cmdq_writel(cq_host, 1 << tag, CQTDBR); |
Venkat Gopalakrishnan | f1329ce | 2015-08-10 14:55:23 -0700 | [diff] [blame] | 637 | /* Commit the doorbell write immediately */ |
| 638 | wmb(); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 639 | |
| 640 | out: |
| 641 | return err; |
| 642 | } |
| 643 | |
| 644 | static void cmdq_finish_data(struct mmc_host *mmc, unsigned int tag) |
| 645 | { |
| 646 | struct mmc_request *mrq; |
| 647 | struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc); |
| 648 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 649 | mrq = get_req_by_tag(cq_host, tag); |
Sahitya Tummala | 9549d56 | 2015-05-29 15:41:18 +0530 | [diff] [blame] | 650 | if (tag == cq_host->dcmd_slot) |
| 651 | mrq->cmd->resp[0] = cmdq_readl(cq_host, CQCRDCT); |
| 652 | |
Asutosh Das | c0ed9c4 | 2015-05-29 15:39:37 +0530 | [diff] [blame] | 653 | if (mrq->cmdq_req->cmdq_req_flags & DCMD) |
| 654 | cmdq_writel(cq_host, cmdq_readl(cq_host, CQ_VENDOR_CFG) | |
| 655 | CMDQ_SEND_STATUS_TRIGGER, CQCTL); |
Konstantin Dorfman | 27af9a9 | 2015-08-02 17:06:18 +0300 | [diff] [blame] | 656 | |
| 657 | cmdq_runtime_pm_put(cq_host); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 658 | mrq->done(mrq); |
| 659 | } |
| 660 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 661 | irqreturn_t cmdq_irq(struct mmc_host *mmc, int err) |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 662 | { |
| 663 | u32 status; |
| 664 | unsigned long tag = 0, comp_status; |
| 665 | struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 666 | unsigned long err_info = 0; |
| 667 | struct mmc_request *mrq; |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 668 | |
| 669 | status = cmdq_readl(cq_host, CQIS); |
| 670 | cmdq_writel(cq_host, status, CQIS); |
| 671 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 672 | if (!status && !err) |
| 673 | return IRQ_NONE; |
| 674 | |
| 675 | if (err || (status & CQIS_RED)) { |
| 676 | err_info = cmdq_readl(cq_host, CQTERRI); |
| 677 | pr_err("%s: err: %d status: 0x%08x task-err-info (0x%08lx)\n", |
| 678 | mmc_hostname(mmc), err, status, err_info); |
| 679 | |
| 680 | cmdq_dumpregs(cq_host); |
| 681 | |
| 682 | if (err_info & CQ_RMEFV) { |
| 683 | tag = GET_CMD_ERR_TAG(err_info); |
| 684 | pr_err("%s: CMD err tag: %lu\n", __func__, tag); |
| 685 | |
| 686 | mrq = get_req_by_tag(cq_host, tag); |
| 687 | /* CMD44/45/46/47 will not have a valid cmd */ |
| 688 | if (mrq->cmd) |
| 689 | mrq->cmd->error = err; |
| 690 | else |
| 691 | mrq->data->error = err; |
| 692 | } else { |
| 693 | tag = GET_DAT_ERR_TAG(err_info); |
| 694 | pr_err("%s: Dat err tag: %lu\n", __func__, tag); |
| 695 | mrq = get_req_by_tag(cq_host, tag); |
| 696 | mrq->data->error = err; |
| 697 | } |
| 698 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 699 | /* |
| 700 | * CQE detected a response error from device |
| 701 | * In most cases, this would require a reset. |
| 702 | */ |
| 703 | if (status & CQIS_RED) { |
Dov Levenglick | 2b67830 | 2015-07-01 14:24:20 +0300 | [diff] [blame] | 704 | /* |
| 705 | * will check if the RED error is due to a bkops |
| 706 | * exception once the queue is empty |
| 707 | */ |
| 708 | BUG_ON(!mmc->card); |
| 709 | if (mmc_card_configured_manual_bkops(mmc->card) && |
| 710 | !mmc_card_configured_auto_bkops(mmc->card)) |
| 711 | mmc->card->bkops.needs_check = true; |
| 712 | |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 713 | mrq->cmdq_req->resp_err = true; |
| 714 | pr_err("%s: Response error (0x%08x) from card !!!", |
Dov Levenglick | 2b67830 | 2015-07-01 14:24:20 +0300 | [diff] [blame] | 715 | mmc_hostname(mmc), status); |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 716 | } else { |
| 717 | mrq->cmdq_req->resp_idx = cmdq_readl(cq_host, CQCRI); |
| 718 | mrq->cmdq_req->resp_arg = cmdq_readl(cq_host, CQCRA); |
| 719 | } |
| 720 | |
| 721 | mmc->err_mrq = mrq; |
| 722 | cmdq_finish_data(mmc, tag); |
| 723 | } |
| 724 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 725 | if (status & CQIS_TCC) { |
| 726 | /* read QCTCN and complete the request */ |
| 727 | comp_status = cmdq_readl(cq_host, CQTCN); |
| 728 | if (!comp_status) |
| 729 | goto out; |
| 730 | |
| 731 | for_each_set_bit(tag, &comp_status, cq_host->num_slots) { |
| 732 | /* complete the corresponding mrq */ |
| 733 | pr_debug("%s: completing tag -> %lu\n", |
| 734 | mmc_hostname(mmc), tag); |
| 735 | cmdq_finish_data(mmc, tag); |
| 736 | } |
| 737 | cmdq_writel(cq_host, comp_status, CQTCN); |
| 738 | } |
| 739 | |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 740 | if (status & CQIS_HAC) { |
Konstantin Dorfman | fa32107 | 2015-05-31 10:10:13 +0300 | [diff] [blame] | 741 | if (cq_host->ops->post_cqe_halt) |
| 742 | cq_host->ops->post_cqe_halt(mmc); |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 743 | /* halt is completed, wakeup waiting thread */ |
| 744 | complete(&cq_host->halt_comp); |
| 745 | } |
| 746 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 747 | out: |
| 748 | return IRQ_HANDLED; |
| 749 | } |
| 750 | EXPORT_SYMBOL(cmdq_irq); |
| 751 | |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 752 | /* May sleep */ |
| 753 | static int cmdq_halt(struct mmc_host *mmc, bool halt) |
| 754 | { |
| 755 | struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc); |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 756 | u32 ret = 0; |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 757 | |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 758 | cmdq_runtime_pm_get(cq_host); |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 759 | if (halt) { |
| 760 | cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT, |
| 761 | CQCTL); |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 762 | ret = wait_for_completion_timeout(&cq_host->halt_comp, |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 763 | msecs_to_jiffies(HALT_TIMEOUT_MS)); |
| 764 | /* halt done: re-enable legacy interrupts */ |
| 765 | if (cq_host->ops->clear_set_irqs) |
| 766 | cq_host->ops->clear_set_irqs(mmc, false); |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 767 | ret = ret ? 0 : -ETIMEDOUT; |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 768 | } else { |
Asutosh Das | 3f730d1 | 2015-07-08 11:41:35 +0530 | [diff] [blame] | 769 | if (cq_host->ops->set_data_timeout) |
| 770 | cq_host->ops->set_data_timeout(mmc, 0xf); |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 771 | if (cq_host->ops->clear_set_irqs) |
| 772 | cq_host->ops->clear_set_irqs(mmc, true); |
| 773 | cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) & ~HALT, |
| 774 | CQCTL); |
| 775 | } |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 776 | cmdq_runtime_pm_put(cq_host); |
| 777 | return ret; |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 778 | } |
| 779 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 780 | static void cmdq_post_req(struct mmc_host *host, struct mmc_request *mrq, |
| 781 | int err) |
| 782 | { |
| 783 | struct mmc_data *data = mrq->data; |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 784 | struct sdhci_host *sdhci_host = mmc_priv(host); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 785 | |
| 786 | if (data) { |
| 787 | data->error = err; |
| 788 | dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len, |
| 789 | (data->flags & MMC_DATA_READ) ? |
| 790 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 791 | if (err) |
| 792 | data->bytes_xfered = 0; |
| 793 | else |
| 794 | data->bytes_xfered = blk_rq_bytes(mrq->req); |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 795 | |
| 796 | /* we're in atomic context (soft-irq) so unvote async. */ |
| 797 | sdhci_msm_pm_qos_irq_unvote(sdhci_host, true); |
| 798 | cmdq_pm_qos_unvote(sdhci_host, mrq); |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
Asutosh Das | fa8836b | 2015-03-02 23:14:05 +0530 | [diff] [blame] | 802 | static void cmdq_dumpstate(struct mmc_host *mmc) |
| 803 | { |
| 804 | struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc); |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 805 | cmdq_runtime_pm_get(cq_host); |
Asutosh Das | fa8836b | 2015-03-02 23:14:05 +0530 | [diff] [blame] | 806 | cmdq_dumpregs(cq_host); |
Konstantin Dorfman | 4d40cf2 | 2015-06-11 11:41:53 +0300 | [diff] [blame] | 807 | cmdq_runtime_pm_put(cq_host); |
Asutosh Das | fa8836b | 2015-03-02 23:14:05 +0530 | [diff] [blame] | 808 | } |
| 809 | |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 810 | static int cmdq_late_init(struct mmc_host *mmc) |
| 811 | { |
| 812 | struct sdhci_host *host = mmc_priv(mmc); |
| 813 | struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host); |
| 814 | struct sdhci_msm_host *msm_host = pltfm_host->priv; |
| 815 | |
| 816 | /* |
| 817 | * TODO: This should basically move to something like "sdhci-cmdq-msm" |
| 818 | * for msm specific implementation. |
| 819 | */ |
| 820 | sdhci_msm_pm_qos_irq_init(host); |
| 821 | |
| 822 | if (msm_host->pdata->pm_qos_data.cmdq_valid) |
| 823 | sdhci_msm_pm_qos_cpu_init(host, |
| 824 | msm_host->pdata->pm_qos_data.cmdq_latency); |
| 825 | return 0; |
| 826 | } |
| 827 | |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 828 | static const struct mmc_cmdq_host_ops cmdq_host_ops = { |
Gilad Broner | 4444599 | 2015-09-29 16:05:39 +0300 | [diff] [blame] | 829 | .init = cmdq_late_init, |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 830 | .enable = cmdq_enable, |
| 831 | .disable = cmdq_disable, |
| 832 | .request = cmdq_request, |
| 833 | .post_req = cmdq_post_req, |
Asutosh Das | aa1e1c7 | 2015-05-21 17:22:10 +0530 | [diff] [blame] | 834 | .halt = cmdq_halt, |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 835 | .reset = cmdq_reset, |
Asutosh Das | fa8836b | 2015-03-02 23:14:05 +0530 | [diff] [blame] | 836 | .dumpstate = cmdq_dumpstate, |
Venkat Gopalakrishnan | 0225ff9 | 2015-05-29 17:25:46 -0700 | [diff] [blame] | 837 | }; |
| 838 | |
| 839 | struct cmdq_host *cmdq_pltfm_init(struct platform_device *pdev) |
| 840 | { |
| 841 | struct cmdq_host *cq_host; |
| 842 | struct resource *cmdq_memres = NULL; |
| 843 | |
| 844 | /* check and setup CMDQ interface */ |
| 845 | cmdq_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 846 | "cmdq_mem"); |
| 847 | if (!cmdq_memres) { |
| 848 | dev_dbg(&pdev->dev, "CMDQ not supported\n"); |
| 849 | return ERR_PTR(-EINVAL); |
| 850 | } |
| 851 | |
| 852 | cq_host = kzalloc(sizeof(*cq_host), GFP_KERNEL); |
| 853 | if (!cq_host) { |
| 854 | dev_err(&pdev->dev, "failed to allocate memory for CMDQ\n"); |
| 855 | return ERR_PTR(-ENOMEM); |
| 856 | } |
| 857 | cq_host->mmio = devm_ioremap(&pdev->dev, |
| 858 | cmdq_memres->start, |
| 859 | resource_size(cmdq_memres)); |
| 860 | if (!cq_host->mmio) { |
| 861 | dev_err(&pdev->dev, "failed to remap cmdq regs\n"); |
| 862 | kfree(cq_host); |
| 863 | return ERR_PTR(-EBUSY); |
| 864 | } |
| 865 | dev_dbg(&pdev->dev, "CMDQ ioremap: done\n"); |
| 866 | |
| 867 | return cq_host; |
| 868 | } |
| 869 | EXPORT_SYMBOL(cmdq_pltfm_init); |
| 870 | |
| 871 | int cmdq_init(struct cmdq_host *cq_host, struct mmc_host *mmc, |
| 872 | bool dma64) |
| 873 | { |
| 874 | int err = 0; |
| 875 | |
| 876 | cq_host->dma64 = dma64; |
| 877 | cq_host->mmc = mmc; |
| 878 | cq_host->mmc->cmdq_private = cq_host; |
| 879 | |
| 880 | cq_host->num_slots = NUM_SLOTS; |
| 881 | cq_host->dcmd_slot = DCMD_SLOT; |
| 882 | |
| 883 | mmc->cmdq_ops = &cmdq_host_ops; |
| 884 | |
| 885 | cq_host->mrq_slot = kzalloc(sizeof(cq_host->mrq_slot) * |
| 886 | cq_host->num_slots, GFP_KERNEL); |
| 887 | if (!cq_host->mrq_slot) |
| 888 | return -ENOMEM; |
| 889 | |
| 890 | init_completion(&cq_host->halt_comp); |
| 891 | return err; |
| 892 | } |
| 893 | EXPORT_SYMBOL(cmdq_init); |