xj | b04a402 | 2021-11-25 15:01:52 +0800 | [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/ktime.h> |
| 22 | |
| 23 | #include <linux/mmc/mmc.h> |
| 24 | #include <linux/mmc/host.h> |
| 25 | #include <linux/mmc/card.h> |
| 26 | |
| 27 | #include "cqhci.h" |
| 28 | #include "dbg.h" |
| 29 | #define DCMD_SLOT 31 |
| 30 | #define NUM_SLOTS 32 |
| 31 | |
| 32 | struct cqhci_slot { |
| 33 | struct mmc_request *mrq; |
| 34 | unsigned int flags; |
| 35 | #define CQHCI_EXTERNAL_TIMEOUT BIT(0) |
| 36 | #define CQHCI_COMPLETED BIT(1) |
| 37 | #define CQHCI_HOST_CRC BIT(2) |
| 38 | #define CQHCI_HOST_TIMEOUT BIT(3) |
| 39 | #define CQHCI_HOST_OTHER BIT(4) |
| 40 | }; |
| 41 | |
| 42 | static inline u8 *get_desc(struct cqhci_host *cq_host, u8 tag) |
| 43 | { |
| 44 | return cq_host->desc_base + (tag * cq_host->slot_sz); |
| 45 | } |
| 46 | |
| 47 | static inline u8 *get_link_desc(struct cqhci_host *cq_host, u8 tag) |
| 48 | { |
| 49 | u8 *desc = get_desc(cq_host, tag); |
| 50 | |
| 51 | return desc + cq_host->task_desc_len; |
| 52 | } |
| 53 | |
| 54 | static inline dma_addr_t get_trans_desc_dma(struct cqhci_host *cq_host, u8 tag) |
| 55 | { |
| 56 | return cq_host->trans_desc_dma_base + |
| 57 | (cq_host->mmc->max_segs * tag * |
| 58 | cq_host->trans_desc_len); |
| 59 | } |
| 60 | |
| 61 | static inline u8 *get_trans_desc(struct cqhci_host *cq_host, u8 tag) |
| 62 | { |
| 63 | return cq_host->trans_desc_base + |
| 64 | (cq_host->trans_desc_len * cq_host->mmc->max_segs * tag); |
| 65 | } |
| 66 | |
| 67 | static void setup_trans_desc(struct cqhci_host *cq_host, u8 tag) |
| 68 | { |
| 69 | u8 *link_temp; |
| 70 | dma_addr_t trans_temp; |
| 71 | |
| 72 | link_temp = get_link_desc(cq_host, tag); |
| 73 | trans_temp = get_trans_desc_dma(cq_host, tag); |
| 74 | |
| 75 | memset(link_temp, 0, cq_host->link_desc_len); |
| 76 | if (cq_host->link_desc_len > 8) |
| 77 | *(link_temp + 8) = 0; |
| 78 | |
| 79 | if (tag == DCMD_SLOT && (cq_host->mmc->caps2 & MMC_CAP2_CQE_DCMD)) { |
| 80 | *link_temp = CQHCI_VALID(0) | CQHCI_ACT(0) | CQHCI_END(1); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | *link_temp = CQHCI_VALID(1) | CQHCI_ACT(0x6) | CQHCI_END(0); |
| 85 | |
| 86 | if (cq_host->dma64) { |
| 87 | __le64 *data_addr = (__le64 __force *)(link_temp + 4); |
| 88 | |
| 89 | data_addr[0] = cpu_to_le64(trans_temp); |
| 90 | } else { |
| 91 | __le32 *data_addr = (__le32 __force *)(link_temp + 4); |
| 92 | |
| 93 | data_addr[0] = cpu_to_le32(trans_temp); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | static void cqhci_set_irqs(struct cqhci_host *cq_host, u32 set) |
| 98 | { |
| 99 | cqhci_writel(cq_host, set, CQHCI_ISTE); |
| 100 | cqhci_writel(cq_host, set, CQHCI_ISGE); |
| 101 | } |
| 102 | |
| 103 | #define DRV_NAME "cqhci" |
| 104 | |
| 105 | #define CQHCI_DUMP(f, x...) \ |
| 106 | pr_err("%s: " DRV_NAME ": " f, mmc_hostname(mmc), ## x) |
| 107 | |
| 108 | static void cqhci_dumpregs(struct cqhci_host *cq_host) |
| 109 | { |
| 110 | struct mmc_host *mmc = cq_host->mmc; |
| 111 | |
| 112 | CQHCI_DUMP("============ CQHCI REGISTER DUMP ===========\n"); |
| 113 | |
| 114 | CQHCI_DUMP("Caps: 0x%08x | Version: 0x%08x\n", |
| 115 | cqhci_readl(cq_host, CQHCI_CAP), |
| 116 | cqhci_readl(cq_host, CQHCI_VER)); |
| 117 | CQHCI_DUMP("Config: 0x%08x | Control: 0x%08x\n", |
| 118 | cqhci_readl(cq_host, CQHCI_CFG), |
| 119 | cqhci_readl(cq_host, CQHCI_CTL)); |
| 120 | CQHCI_DUMP("Int stat: 0x%08x | Int enab: 0x%08x\n", |
| 121 | cqhci_readl(cq_host, CQHCI_IS), |
| 122 | cqhci_readl(cq_host, CQHCI_ISTE)); |
| 123 | CQHCI_DUMP("Int sig: 0x%08x | Int Coal: 0x%08x\n", |
| 124 | cqhci_readl(cq_host, CQHCI_ISGE), |
| 125 | cqhci_readl(cq_host, CQHCI_IC)); |
| 126 | CQHCI_DUMP("TDL base: 0x%08x | TDL up32: 0x%08x\n", |
| 127 | cqhci_readl(cq_host, CQHCI_TDLBA), |
| 128 | cqhci_readl(cq_host, CQHCI_TDLBAU)); |
| 129 | CQHCI_DUMP("Doorbell: 0x%08x | TCN: 0x%08x\n", |
| 130 | cqhci_readl(cq_host, CQHCI_TDBR), |
| 131 | cqhci_readl(cq_host, CQHCI_TCN)); |
| 132 | CQHCI_DUMP("Dev queue: 0x%08x | Dev Pend: 0x%08x\n", |
| 133 | cqhci_readl(cq_host, CQHCI_DQS), |
| 134 | cqhci_readl(cq_host, CQHCI_DPT)); |
| 135 | CQHCI_DUMP("Task clr: 0x%08x | SSC1: 0x%08x\n", |
| 136 | cqhci_readl(cq_host, CQHCI_TCLR), |
| 137 | cqhci_readl(cq_host, CQHCI_SSC1)); |
| 138 | CQHCI_DUMP("SSC2: 0x%08x | DCMD rsp: 0x%08x\n", |
| 139 | cqhci_readl(cq_host, CQHCI_SSC2), |
| 140 | cqhci_readl(cq_host, CQHCI_CRDCT)); |
| 141 | CQHCI_DUMP("RED mask: 0x%08x | TERRI: 0x%08x\n", |
| 142 | cqhci_readl(cq_host, CQHCI_RMEM), |
| 143 | cqhci_readl(cq_host, CQHCI_TERRI)); |
| 144 | CQHCI_DUMP("Resp idx: 0x%08x | Resp arg: 0x%08x\n", |
| 145 | cqhci_readl(cq_host, CQHCI_CRI), |
| 146 | cqhci_readl(cq_host, CQHCI_CRA)); |
| 147 | |
| 148 | if (cq_host->ops->dumpregs) |
| 149 | cq_host->ops->dumpregs(mmc); |
| 150 | else |
| 151 | CQHCI_DUMP(": ===========================================\n"); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * The allocated descriptor table for task, link & transfer descritors |
| 156 | * looks like: |
| 157 | * |----------| |
| 158 | * |task desc | |->|----------| |
| 159 | * |----------| | |trans desc| |
| 160 | * |link desc-|->| |----------| |
| 161 | * |----------| . |
| 162 | * . . |
| 163 | * no. of slots max-segs |
| 164 | * . |----------| |
| 165 | * |----------| |
| 166 | * The idea here is to create the [task+trans] table and mark & point the |
| 167 | * link desc to the transfer desc table on a per slot basis. |
| 168 | */ |
| 169 | static int cqhci_host_alloc_tdl(struct cqhci_host *cq_host) |
| 170 | { |
| 171 | int i = 0; |
| 172 | |
| 173 | /* task descriptor can be 64/128 bit irrespective of arch */ |
| 174 | if (cq_host->caps & CQHCI_TASK_DESC_SZ_128) { |
| 175 | cqhci_writel(cq_host, cqhci_readl(cq_host, CQHCI_CFG) | |
| 176 | CQHCI_TASK_DESC_SZ, CQHCI_CFG); |
| 177 | cq_host->task_desc_len = 16; |
| 178 | } else { |
| 179 | cq_host->task_desc_len = 8; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * 96 bits length of transfer desc instead of 128 bits which means |
| 184 | * ADMA would expect next valid descriptor at the 96th bit |
| 185 | * or 128th bit |
| 186 | */ |
| 187 | if (cq_host->dma64) { |
| 188 | if (cq_host->quirks & CQHCI_QUIRK_SHORT_TXFR_DESC_SZ) |
| 189 | cq_host->trans_desc_len = 12; |
| 190 | else |
| 191 | cq_host->trans_desc_len = 16; |
| 192 | cq_host->link_desc_len = 16; |
| 193 | } else { |
| 194 | cq_host->trans_desc_len = 8; |
| 195 | cq_host->link_desc_len = 8; |
| 196 | } |
| 197 | |
| 198 | /* total size of a slot: 1 task & 1 transfer (link) */ |
| 199 | cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len; |
| 200 | |
| 201 | cq_host->desc_size = cq_host->slot_sz * cq_host->num_slots; |
| 202 | |
| 203 | cq_host->data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs * |
| 204 | cq_host->mmc->cqe_qdepth; |
| 205 | |
| 206 | pr_debug("%s: cqhci: desc_size: %zu data_sz: %zu slot-sz: %d\n", |
| 207 | mmc_hostname(cq_host->mmc), cq_host->desc_size, cq_host->data_size, |
| 208 | cq_host->slot_sz); |
| 209 | |
| 210 | /* |
| 211 | * allocate a dma-mapped chunk of memory for the descriptors |
| 212 | * allocate a dma-mapped chunk of memory for link descriptors |
| 213 | * setup each link-desc memory offset per slot-number to |
| 214 | * the descriptor table. |
| 215 | */ |
| 216 | cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc), |
| 217 | cq_host->desc_size, |
| 218 | &cq_host->desc_dma_base, |
| 219 | GFP_KERNEL); |
| 220 | if (!cq_host->desc_base) |
| 221 | return -ENOMEM; |
| 222 | |
| 223 | cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc), |
| 224 | cq_host->data_size, |
| 225 | &cq_host->trans_desc_dma_base, |
| 226 | GFP_KERNEL); |
| 227 | if (!cq_host->trans_desc_base) { |
| 228 | dmam_free_coherent(mmc_dev(cq_host->mmc), cq_host->desc_size, |
| 229 | cq_host->desc_base, |
| 230 | cq_host->desc_dma_base); |
| 231 | cq_host->desc_base = NULL; |
| 232 | cq_host->desc_dma_base = 0; |
| 233 | return -ENOMEM; |
| 234 | } |
| 235 | |
| 236 | pr_debug("%s: cqhci: desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n", |
| 237 | mmc_hostname(cq_host->mmc), cq_host->desc_base, cq_host->trans_desc_base, |
| 238 | (unsigned long long)cq_host->desc_dma_base, |
| 239 | (unsigned long long)cq_host->trans_desc_dma_base); |
| 240 | |
| 241 | for (; i < (cq_host->num_slots); i++) |
| 242 | setup_trans_desc(cq_host, i); |
| 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static void __cqhci_enable(struct cqhci_host *cq_host) |
| 248 | { |
| 249 | struct mmc_host *mmc = cq_host->mmc; |
| 250 | u32 cqcfg; |
| 251 | |
| 252 | cqcfg = cqhci_readl(cq_host, CQHCI_CFG); |
| 253 | |
| 254 | /* Configuration must not be changed while enabled */ |
| 255 | if (cqcfg & CQHCI_ENABLE) { |
| 256 | cqcfg &= ~CQHCI_ENABLE; |
| 257 | cqhci_writel(cq_host, cqcfg, CQHCI_CFG); |
| 258 | } |
| 259 | |
| 260 | cqcfg &= ~(CQHCI_DCMD | CQHCI_TASK_DESC_SZ); |
| 261 | |
| 262 | if (mmc->caps2 & MMC_CAP2_CQE_DCMD) |
| 263 | cqcfg |= CQHCI_DCMD; |
| 264 | |
| 265 | if (cq_host->caps & CQHCI_TASK_DESC_SZ_128) |
| 266 | cqcfg |= CQHCI_TASK_DESC_SZ; |
| 267 | |
| 268 | cqhci_writel(cq_host, cqcfg, CQHCI_CFG); |
| 269 | |
| 270 | cqhci_writel(cq_host, lower_32_bits(cq_host->desc_dma_base), |
| 271 | CQHCI_TDLBA); |
| 272 | cqhci_writel(cq_host, upper_32_bits(cq_host->desc_dma_base), |
| 273 | CQHCI_TDLBAU); |
| 274 | |
| 275 | cqhci_writel(cq_host, cq_host->rca, CQHCI_SSC2); |
| 276 | |
| 277 | cqhci_set_irqs(cq_host, 0); |
| 278 | |
| 279 | cqcfg |= CQHCI_ENABLE; |
| 280 | |
| 281 | cqhci_writel(cq_host, cqcfg, CQHCI_CFG); |
| 282 | |
| 283 | if (cq_host->ops->enable) |
| 284 | cq_host->ops->enable(mmc); |
| 285 | |
| 286 | /* Ensure all writes are done before interrupts are enabled */ |
| 287 | wmb(); |
| 288 | |
| 289 | cqhci_set_irqs(cq_host, CQHCI_IS_MASK); |
| 290 | |
| 291 | cq_host->activated = true; |
| 292 | } |
| 293 | |
| 294 | static void __cqhci_disable(struct cqhci_host *cq_host) |
| 295 | { |
| 296 | u32 cqcfg; |
| 297 | |
| 298 | cqcfg = cqhci_readl(cq_host, CQHCI_CFG); |
| 299 | cqcfg &= ~CQHCI_ENABLE; |
| 300 | cqhci_writel(cq_host, cqcfg, CQHCI_CFG); |
| 301 | |
| 302 | cq_host->activated = false; |
| 303 | } |
| 304 | |
| 305 | int cqhci_suspend(struct mmc_host *mmc) |
| 306 | { |
| 307 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 308 | |
| 309 | if (cq_host->enabled) |
| 310 | __cqhci_disable(cq_host); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | EXPORT_SYMBOL(cqhci_suspend); |
| 315 | |
| 316 | int cqhci_resume(struct mmc_host *mmc) |
| 317 | { |
| 318 | /* Re-enable is done upon first request */ |
| 319 | return 0; |
| 320 | } |
| 321 | EXPORT_SYMBOL(cqhci_resume); |
| 322 | |
| 323 | static int cqhci_enable(struct mmc_host *mmc, struct mmc_card *card) |
| 324 | { |
| 325 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 326 | int err; |
| 327 | |
| 328 | if (cq_host->enabled) |
| 329 | return 0; |
| 330 | |
| 331 | cq_host->rca = card->rca; |
| 332 | |
| 333 | err = cqhci_host_alloc_tdl(cq_host); |
| 334 | if (err) |
| 335 | return err; |
| 336 | |
| 337 | __cqhci_enable(cq_host); |
| 338 | |
| 339 | cq_host->enabled = true; |
| 340 | |
| 341 | #ifdef DEBUG |
| 342 | cqhci_dumpregs(cq_host); |
| 343 | #endif |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* CQHCI is idle and should halt immediately, so set a small timeout */ |
| 348 | #define CQHCI_OFF_TIMEOUT 100 |
| 349 | |
| 350 | static void cqhci_off(struct mmc_host *mmc) |
| 351 | { |
| 352 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 353 | ktime_t timeout; |
| 354 | bool timed_out; |
| 355 | u32 reg; |
| 356 | |
| 357 | if (!cq_host->enabled || !mmc->cqe_on || cq_host->recovery_halt) |
| 358 | return; |
| 359 | |
| 360 | if (cq_host->ops->disable) |
| 361 | cq_host->ops->disable(mmc, false); |
| 362 | |
| 363 | cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL); |
| 364 | |
| 365 | timeout = ktime_add_us(ktime_get(), CQHCI_OFF_TIMEOUT); |
| 366 | while (1) { |
| 367 | timed_out = ktime_compare(ktime_get(), timeout) > 0; |
| 368 | reg = cqhci_readl(cq_host, CQHCI_CTL); |
| 369 | if ((reg & CQHCI_HALT) || timed_out) |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | if (timed_out) |
| 374 | pr_err("%s: cqhci: CQE stuck on\n", mmc_hostname(mmc)); |
| 375 | else |
| 376 | pr_debug("%s: cqhci: CQE off\n", mmc_hostname(mmc)); |
| 377 | |
| 378 | /* |
| 379 | * MTK PATCH: need disable cqhci for legacy cmds coz legacy cmds using |
| 380 | * GPD DMA and it can only work when CQHCI disable. |
| 381 | */ |
| 382 | if (cq_host->quirks & CQHCI_QUIRK_DIS_BEFORE_NON_CQ_CMD) { |
| 383 | reg = cqhci_readl(cq_host, CQHCI_CFG); |
| 384 | reg &= ~CQHCI_ENABLE; |
| 385 | cqhci_writel(cq_host, reg, CQHCI_CFG); |
| 386 | } |
| 387 | |
| 388 | mmc->cqe_on = false; |
| 389 | } |
| 390 | |
| 391 | static void cqhci_disable(struct mmc_host *mmc) |
| 392 | { |
| 393 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 394 | |
| 395 | if (!cq_host->enabled) |
| 396 | return; |
| 397 | |
| 398 | cqhci_off(mmc); |
| 399 | |
| 400 | __cqhci_disable(cq_host); |
| 401 | |
| 402 | dmam_free_coherent(mmc_dev(mmc), cq_host->data_size, |
| 403 | cq_host->trans_desc_base, |
| 404 | cq_host->trans_desc_dma_base); |
| 405 | |
| 406 | dmam_free_coherent(mmc_dev(mmc), cq_host->desc_size, |
| 407 | cq_host->desc_base, |
| 408 | cq_host->desc_dma_base); |
| 409 | |
| 410 | cq_host->trans_desc_base = NULL; |
| 411 | cq_host->desc_base = NULL; |
| 412 | |
| 413 | cq_host->enabled = false; |
| 414 | } |
| 415 | |
| 416 | static void cqhci_prep_task_desc(struct mmc_request *mrq, |
| 417 | u64 *data, bool intr) |
| 418 | { |
| 419 | u32 req_flags = mrq->data->flags; |
| 420 | |
| 421 | *data = CQHCI_VALID(1) | |
| 422 | CQHCI_END(1) | |
| 423 | CQHCI_INT(intr) | |
| 424 | CQHCI_ACT(0x5) | |
| 425 | CQHCI_FORCED_PROG(!!(req_flags & MMC_DATA_FORCED_PRG)) | |
| 426 | CQHCI_DATA_TAG(!!(req_flags & MMC_DATA_DAT_TAG)) | |
| 427 | CQHCI_DATA_DIR(!!(req_flags & MMC_DATA_READ)) | |
| 428 | CQHCI_PRIORITY(!!(req_flags & MMC_DATA_PRIO)) | |
| 429 | CQHCI_QBAR(!!(req_flags & MMC_DATA_QBR)) | |
| 430 | CQHCI_REL_WRITE(!!(req_flags & MMC_DATA_REL_WR)) | |
| 431 | CQHCI_BLK_COUNT(mrq->data->blocks) | |
| 432 | CQHCI_BLK_ADDR((u64)mrq->data->blk_addr); |
| 433 | |
| 434 | pr_debug("%s: cqhci: tag %d task descriptor 0x016%llx\n", |
| 435 | mmc_hostname(mrq->host), mrq->tag, (unsigned long long)*data); |
| 436 | } |
| 437 | |
| 438 | static int cqhci_dma_map(struct mmc_host *host, struct mmc_request *mrq) |
| 439 | { |
| 440 | int sg_count; |
| 441 | struct mmc_data *data = mrq->data; |
| 442 | |
| 443 | if (!data) |
| 444 | return -EINVAL; |
| 445 | |
| 446 | sg_count = dma_map_sg(mmc_dev(host), data->sg, |
| 447 | data->sg_len, |
| 448 | (data->flags & MMC_DATA_WRITE) ? |
| 449 | DMA_TO_DEVICE : DMA_FROM_DEVICE); |
| 450 | if (!sg_count) { |
| 451 | pr_err("%s: sg-len: %d\n", __func__, data->sg_len); |
| 452 | return -ENOMEM; |
| 453 | } |
| 454 | |
| 455 | return sg_count; |
| 456 | } |
| 457 | |
| 458 | static void cqhci_set_tran_desc(u8 *desc, dma_addr_t addr, int len, bool end, |
| 459 | bool dma64) |
| 460 | { |
| 461 | __le32 *attr = (__le32 __force *)desc; |
| 462 | |
| 463 | *attr = (CQHCI_VALID(1) | |
| 464 | CQHCI_END(end ? 1 : 0) | |
| 465 | CQHCI_INT(0) | |
| 466 | CQHCI_ACT(0x4) | |
| 467 | CQHCI_DAT_LENGTH(len)); |
| 468 | |
| 469 | if (dma64) { |
| 470 | __le64 *dataddr = (__le64 __force *)(desc + 4); |
| 471 | |
| 472 | dataddr[0] = cpu_to_le64(addr); |
| 473 | } else { |
| 474 | __le32 *dataddr = (__le32 __force *)(desc + 4); |
| 475 | |
| 476 | dataddr[0] = cpu_to_le32(addr); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | static int cqhci_prep_tran_desc(struct mmc_request *mrq, |
| 481 | struct cqhci_host *cq_host, int tag) |
| 482 | { |
| 483 | struct mmc_data *data = mrq->data; |
| 484 | int i, sg_count, len; |
| 485 | bool end = false; |
| 486 | bool dma64 = cq_host->dma64; |
| 487 | dma_addr_t addr; |
| 488 | u8 *desc; |
| 489 | struct scatterlist *sg; |
| 490 | |
| 491 | sg_count = cqhci_dma_map(mrq->host, mrq); |
| 492 | if (sg_count < 0) { |
| 493 | pr_err("%s: %s: unable to map sg lists, %d\n", |
| 494 | mmc_hostname(mrq->host), __func__, sg_count); |
| 495 | return sg_count; |
| 496 | } |
| 497 | |
| 498 | desc = get_trans_desc(cq_host, tag); |
| 499 | |
| 500 | for_each_sg(data->sg, sg, sg_count, i) { |
| 501 | addr = sg_dma_address(sg); |
| 502 | len = sg_dma_len(sg); |
| 503 | |
| 504 | if ((i+1) == sg_count) |
| 505 | end = true; |
| 506 | cqhci_set_tran_desc(desc, addr, len, end, dma64); |
| 507 | desc += cq_host->trans_desc_len; |
| 508 | } |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static void cqhci_prep_dcmd_desc(struct mmc_host *mmc, |
| 514 | struct mmc_request *mrq) |
| 515 | { |
| 516 | u64 *task_desc = NULL; |
| 517 | u64 data = 0; |
| 518 | u8 resp_type; |
| 519 | u8 *desc; |
| 520 | __le64 *dataddr; |
| 521 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 522 | u8 timing; |
| 523 | |
| 524 | if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) { |
| 525 | resp_type = 0x0; |
| 526 | timing = 0x1; |
| 527 | } else { |
| 528 | if (mrq->cmd->flags & MMC_RSP_R1B) { |
| 529 | resp_type = 0x3; |
| 530 | timing = 0x0; |
| 531 | } else { |
| 532 | resp_type = 0x2; |
| 533 | timing = 0x1; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot); |
| 538 | memset(task_desc, 0, cq_host->task_desc_len); |
| 539 | data |= (CQHCI_VALID(1) | |
| 540 | CQHCI_END(1) | |
| 541 | CQHCI_INT(1) | |
| 542 | CQHCI_QBAR(1) | |
| 543 | CQHCI_ACT(0x5) | |
| 544 | CQHCI_CMD_INDEX(mrq->cmd->opcode) | |
| 545 | CQHCI_CMD_TIMING(timing) | CQHCI_RESP_TYPE(resp_type)); |
| 546 | *task_desc |= data; |
| 547 | desc = (u8 *)task_desc; |
| 548 | pr_debug("%s: cqhci: dcmd: cmd: %d timing: %d resp: %d\n", |
| 549 | mmc_hostname(mmc), mrq->cmd->opcode, timing, resp_type); |
| 550 | dataddr = (__le64 __force *)(desc + 4); |
| 551 | dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg); |
| 552 | |
| 553 | } |
| 554 | |
| 555 | static void cqhci_post_req(struct mmc_host *host, struct mmc_request *mrq) |
| 556 | { |
| 557 | struct mmc_data *data = mrq->data; |
| 558 | |
| 559 | if (data) { |
| 560 | dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len, |
| 561 | (data->flags & MMC_DATA_READ) ? |
| 562 | DMA_FROM_DEVICE : DMA_TO_DEVICE); |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | static inline int cqhci_tag(struct mmc_request *mrq) |
| 567 | { |
| 568 | return mrq->cmd ? DCMD_SLOT : mrq->tag; |
| 569 | } |
| 570 | |
| 571 | static int cqhci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 572 | { |
| 573 | int err = 0; |
| 574 | u64 data = 0; |
| 575 | u64 *task_desc = NULL; |
| 576 | int tag = cqhci_tag(mrq); |
| 577 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 578 | unsigned long flags; |
| 579 | u32 reg; |
| 580 | |
| 581 | if (!cq_host->enabled) { |
| 582 | pr_err("%s: cqhci: not enabled\n", mmc_hostname(mmc)); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
| 586 | /* First request after resume has to re-enable */ |
| 587 | if (!cq_host->activated) |
| 588 | __cqhci_enable(cq_host); |
| 589 | |
| 590 | if (!mmc->cqe_on) { |
| 591 | /* MTK PATCH: need enable cqhci after issue legacy cmds */ |
| 592 | if (cq_host->quirks & CQHCI_QUIRK_DIS_BEFORE_NON_CQ_CMD) { |
| 593 | reg = cqhci_readl(cq_host, CQHCI_CFG); |
| 594 | reg |= CQHCI_ENABLE; |
| 595 | cqhci_writel(cq_host, reg, CQHCI_CFG); |
| 596 | } |
| 597 | |
| 598 | cqhci_writel(cq_host, 0, CQHCI_CTL); |
| 599 | mmc->cqe_on = true; |
| 600 | pr_debug("%s: cqhci: CQE on\n", mmc_hostname(mmc)); |
| 601 | if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT) { |
| 602 | pr_err("%s: cqhci: CQE failed to exit halt state\n", |
| 603 | mmc_hostname(mmc)); |
| 604 | } |
| 605 | if (cq_host->ops->enable) |
| 606 | cq_host->ops->enable(mmc); |
| 607 | } |
| 608 | |
| 609 | if (mrq->data) { |
| 610 | task_desc = (__le64 __force *)get_desc(cq_host, tag); |
| 611 | cqhci_prep_task_desc(mrq, &data, 1); |
| 612 | *task_desc = cpu_to_le64(data); |
| 613 | err = cqhci_prep_tran_desc(mrq, cq_host, tag); |
| 614 | if (err) { |
| 615 | pr_err("%s: cqhci: failed to setup tx desc: %d\n", |
| 616 | mmc_hostname(mmc), err); |
| 617 | return err; |
| 618 | } |
| 619 | #if MTK_MMC_DEBUG |
| 620 | dbg_add_host_log(mmc, MAGIC_CQHCI_DBG_TYPE, |
| 621 | MAGIC_CQHCI_DBG_NUM_L + tag, |
| 622 | lower_32_bits(*task_desc)); |
| 623 | dbg_add_host_log(mmc, MAGIC_CQHCI_DBG_TYPE, |
| 624 | MAGIC_CQHCI_DBG_NUM_U + tag, |
| 625 | upper_32_bits(*task_desc)); |
| 626 | #endif |
| 627 | } else { |
| 628 | cqhci_prep_dcmd_desc(mmc, mrq); |
| 629 | #if MTK_MMC_DEBUG |
| 630 | dbg_add_host_log(mmc, MAGIC_CQHCI_DBG_TYPE_DCMD, mrq->cmd->opcode, |
| 631 | mrq->cmd->arg); |
| 632 | #endif |
| 633 | } |
| 634 | |
| 635 | spin_lock_irqsave(&cq_host->lock, flags); |
| 636 | |
| 637 | if (cq_host->recovery_halt) { |
| 638 | err = -EBUSY; |
| 639 | goto out_unlock; |
| 640 | } |
| 641 | |
| 642 | cq_host->slot[tag].mrq = mrq; |
| 643 | cq_host->slot[tag].flags = 0; |
| 644 | |
| 645 | cq_host->qcnt += 1; |
| 646 | /* Make sure descriptors are ready before ringing the doorbell */ |
| 647 | wmb(); |
| 648 | cqhci_writel(cq_host, 1 << tag, CQHCI_TDBR); |
| 649 | |
| 650 | if (!(cqhci_readl(cq_host, CQHCI_TDBR) & (1 << tag))) |
| 651 | pr_debug("%s: cqhci: doorbell not set for tag %d\n", |
| 652 | mmc_hostname(mmc), tag); |
| 653 | out_unlock: |
| 654 | spin_unlock_irqrestore(&cq_host->lock, flags); |
| 655 | |
| 656 | if (err) |
| 657 | cqhci_post_req(mmc, mrq); |
| 658 | |
| 659 | return err; |
| 660 | } |
| 661 | |
| 662 | static void cqhci_recovery_needed(struct mmc_host *mmc, struct mmc_request *mrq, |
| 663 | bool notify) |
| 664 | { |
| 665 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 666 | |
| 667 | if (!cq_host->recovery_halt) { |
| 668 | cq_host->recovery_halt = true; |
| 669 | pr_debug("%s: cqhci: recovery needed\n", mmc_hostname(mmc)); |
| 670 | wake_up(&cq_host->wait_queue); |
| 671 | if (notify && mrq->recovery_notifier) |
| 672 | mrq->recovery_notifier(mrq); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static unsigned int cqhci_error_flags(int error1, int error2) |
| 677 | { |
| 678 | int error = error1 ? error1 : error2; |
| 679 | |
| 680 | switch (error) { |
| 681 | case -EILSEQ: |
| 682 | return CQHCI_HOST_CRC; |
| 683 | case -ETIMEDOUT: |
| 684 | return CQHCI_HOST_TIMEOUT; |
| 685 | default: |
| 686 | return CQHCI_HOST_OTHER; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | static void cqhci_error_irq(struct mmc_host *mmc, u32 status, int cmd_error, |
| 691 | int data_error) |
| 692 | { |
| 693 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 694 | struct cqhci_slot *slot; |
| 695 | u32 terri; |
| 696 | int tag; |
| 697 | |
| 698 | spin_lock(&cq_host->lock); |
| 699 | |
| 700 | terri = cqhci_readl(cq_host, CQHCI_TERRI); |
| 701 | |
| 702 | pr_debug("%s: cqhci: error IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n", |
| 703 | mmc_hostname(mmc), status, cmd_error, data_error, terri); |
| 704 | |
| 705 | /* Forget about errors when recovery has already been triggered */ |
| 706 | if (cq_host->recovery_halt) |
| 707 | goto out_unlock; |
| 708 | |
| 709 | if (!cq_host->qcnt) { |
| 710 | WARN_ONCE(1, "%s: cqhci: error when idle. IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n", |
| 711 | mmc_hostname(mmc), status, cmd_error, data_error, |
| 712 | terri); |
| 713 | goto out_unlock; |
| 714 | } |
| 715 | |
| 716 | if (CQHCI_TERRI_C_VALID(terri)) { |
| 717 | tag = CQHCI_TERRI_C_TASK(terri); |
| 718 | slot = &cq_host->slot[tag]; |
| 719 | if (slot->mrq) { |
| 720 | slot->flags = cqhci_error_flags(cmd_error, data_error); |
| 721 | cqhci_recovery_needed(mmc, slot->mrq, true); |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | if (CQHCI_TERRI_D_VALID(terri)) { |
| 726 | tag = CQHCI_TERRI_D_TASK(terri); |
| 727 | slot = &cq_host->slot[tag]; |
| 728 | if (slot->mrq) { |
| 729 | slot->flags = cqhci_error_flags(data_error, cmd_error); |
| 730 | cqhci_recovery_needed(mmc, slot->mrq, true); |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | if (!cq_host->recovery_halt) { |
| 735 | /* |
| 736 | * The only way to guarantee forward progress is to mark at |
| 737 | * least one task in error, so if none is indicated, pick one. |
| 738 | */ |
| 739 | for (tag = 0; tag < NUM_SLOTS; tag++) { |
| 740 | slot = &cq_host->slot[tag]; |
| 741 | if (!slot->mrq) |
| 742 | continue; |
| 743 | slot->flags = cqhci_error_flags(data_error, cmd_error); |
| 744 | cqhci_recovery_needed(mmc, slot->mrq, true); |
| 745 | break; |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | out_unlock: |
| 750 | spin_unlock(&cq_host->lock); |
| 751 | } |
| 752 | |
| 753 | static void cqhci_finish_mrq(struct mmc_host *mmc, unsigned int tag) |
| 754 | { |
| 755 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 756 | struct cqhci_slot *slot = &cq_host->slot[tag]; |
| 757 | struct mmc_request *mrq = slot->mrq; |
| 758 | struct mmc_data *data; |
| 759 | |
| 760 | if (!mrq) { |
| 761 | WARN_ONCE(1, "%s: cqhci: spurious TCN for tag %d\n", |
| 762 | mmc_hostname(mmc), tag); |
| 763 | return; |
| 764 | } |
| 765 | |
| 766 | /* No completions allowed during recovery */ |
| 767 | if (cq_host->recovery_halt) { |
| 768 | slot->flags |= CQHCI_COMPLETED; |
| 769 | return; |
| 770 | } |
| 771 | |
| 772 | slot->mrq = NULL; |
| 773 | |
| 774 | cq_host->qcnt -= 1; |
| 775 | |
| 776 | data = mrq->data; |
| 777 | if (data) { |
| 778 | if (data->error) |
| 779 | data->bytes_xfered = 0; |
| 780 | else |
| 781 | data->bytes_xfered = data->blksz * data->blocks; |
| 782 | } |
| 783 | #if MTK_MMC_DEBUG |
| 784 | if (tag != DCMD_SLOT) |
| 785 | dbg_add_host_log(mmc, MAGIC_CQHCI_DBG_TYPE, |
| 786 | MAGIC_CQHCI_DBG_NUM_RI + tag, |
| 787 | cqhci_readl(cq_host, CQHCI_CRA)); |
| 788 | else |
| 789 | dbg_add_host_log(mmc, (MAGIC_CQHCI_DBG_TYPE_DCMD + 1), |
| 790 | mrq->cmd->opcode, |
| 791 | mrq->cmd->resp[0]); |
| 792 | #endif |
| 793 | mmc_cqe_request_done(mmc, mrq); |
| 794 | } |
| 795 | |
| 796 | irqreturn_t cqhci_irq(struct mmc_host *mmc, u32 intmask, int cmd_error, |
| 797 | int data_error) |
| 798 | { |
| 799 | u32 status; |
| 800 | unsigned long tag = 0, comp_status; |
| 801 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 802 | |
| 803 | status = cqhci_readl(cq_host, CQHCI_IS); |
| 804 | cqhci_writel(cq_host, status, CQHCI_IS); |
| 805 | |
| 806 | pr_debug("%s: cqhci: IRQ status: 0x%08x\n", mmc_hostname(mmc), status); |
| 807 | |
| 808 | if ((status & CQHCI_IS_RED) || cmd_error || data_error) |
| 809 | cqhci_error_irq(mmc, status, cmd_error, data_error); |
| 810 | |
| 811 | if (status & CQHCI_IS_TCC) { |
| 812 | /* read TCN and complete the request */ |
| 813 | comp_status = cqhci_readl(cq_host, CQHCI_TCN); |
| 814 | cqhci_writel(cq_host, comp_status, CQHCI_TCN); |
| 815 | pr_debug("%s: cqhci: TCN: 0x%08lx\n", |
| 816 | mmc_hostname(mmc), comp_status); |
| 817 | |
| 818 | spin_lock(&cq_host->lock); |
| 819 | |
| 820 | for_each_set_bit(tag, &comp_status, cq_host->num_slots) { |
| 821 | /* complete the corresponding mrq */ |
| 822 | pr_debug("%s: cqhci: completing tag %lu\n", |
| 823 | mmc_hostname(mmc), tag); |
| 824 | cqhci_finish_mrq(mmc, tag); |
| 825 | } |
| 826 | |
| 827 | if (cq_host->waiting_for_idle && !cq_host->qcnt) { |
| 828 | cq_host->waiting_for_idle = false; |
| 829 | wake_up(&cq_host->wait_queue); |
| 830 | } |
| 831 | |
| 832 | spin_unlock(&cq_host->lock); |
| 833 | } |
| 834 | |
| 835 | if (status & CQHCI_IS_TCL) |
| 836 | wake_up(&cq_host->wait_queue); |
| 837 | |
| 838 | if (status & CQHCI_IS_HAC) |
| 839 | wake_up(&cq_host->wait_queue); |
| 840 | |
| 841 | return IRQ_HANDLED; |
| 842 | } |
| 843 | EXPORT_SYMBOL(cqhci_irq); |
| 844 | |
| 845 | static bool cqhci_is_idle(struct cqhci_host *cq_host, int *ret) |
| 846 | { |
| 847 | unsigned long flags; |
| 848 | bool is_idle; |
| 849 | |
| 850 | spin_lock_irqsave(&cq_host->lock, flags); |
| 851 | is_idle = !cq_host->qcnt || cq_host->recovery_halt; |
| 852 | *ret = cq_host->recovery_halt ? -EBUSY : 0; |
| 853 | cq_host->waiting_for_idle = !is_idle; |
| 854 | spin_unlock_irqrestore(&cq_host->lock, flags); |
| 855 | |
| 856 | return is_idle; |
| 857 | } |
| 858 | |
| 859 | static int cqhci_wait_for_idle(struct mmc_host *mmc) |
| 860 | { |
| 861 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 862 | int ret; |
| 863 | |
| 864 | wait_event(cq_host->wait_queue, cqhci_is_idle(cq_host, &ret)); |
| 865 | |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | static bool cqhci_timeout(struct mmc_host *mmc, struct mmc_request *mrq, |
| 870 | bool *recovery_needed) |
| 871 | { |
| 872 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 873 | int tag = cqhci_tag(mrq); |
| 874 | struct cqhci_slot *slot = &cq_host->slot[tag]; |
| 875 | unsigned long flags; |
| 876 | bool timed_out; |
| 877 | |
| 878 | spin_lock_irqsave(&cq_host->lock, flags); |
| 879 | timed_out = slot->mrq == mrq; |
| 880 | if (timed_out) { |
| 881 | slot->flags |= CQHCI_EXTERNAL_TIMEOUT; |
| 882 | cqhci_recovery_needed(mmc, mrq, false); |
| 883 | *recovery_needed = cq_host->recovery_halt; |
| 884 | } |
| 885 | spin_unlock_irqrestore(&cq_host->lock, flags); |
| 886 | |
| 887 | if (timed_out) { |
| 888 | pr_err("%s: cqhci: timeout for tag %d\n", |
| 889 | mmc_hostname(mmc), tag); |
| 890 | cqhci_dumpregs(cq_host); |
| 891 | } |
| 892 | |
| 893 | return timed_out; |
| 894 | } |
| 895 | |
| 896 | static bool cqhci_tasks_cleared(struct cqhci_host *cq_host) |
| 897 | { |
| 898 | return !(cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_CLEAR_ALL_TASKS); |
| 899 | } |
| 900 | |
| 901 | static bool cqhci_clear_all_tasks(struct mmc_host *mmc, unsigned int timeout) |
| 902 | { |
| 903 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 904 | bool ret; |
| 905 | u32 ctl; |
| 906 | |
| 907 | cqhci_set_irqs(cq_host, CQHCI_IS_TCL); |
| 908 | |
| 909 | ctl = cqhci_readl(cq_host, CQHCI_CTL); |
| 910 | ctl |= CQHCI_CLEAR_ALL_TASKS; |
| 911 | cqhci_writel(cq_host, ctl, CQHCI_CTL); |
| 912 | |
| 913 | wait_event_timeout(cq_host->wait_queue, cqhci_tasks_cleared(cq_host), |
| 914 | msecs_to_jiffies(timeout) + 1); |
| 915 | |
| 916 | cqhci_set_irqs(cq_host, 0); |
| 917 | |
| 918 | ret = cqhci_tasks_cleared(cq_host); |
| 919 | |
| 920 | if (!ret) |
| 921 | pr_debug("%s: cqhci: Failed to clear tasks\n", |
| 922 | mmc_hostname(mmc)); |
| 923 | |
| 924 | return ret; |
| 925 | } |
| 926 | |
| 927 | static bool cqhci_halted(struct cqhci_host *cq_host) |
| 928 | { |
| 929 | return cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT; |
| 930 | } |
| 931 | |
| 932 | static bool cqhci_halt(struct mmc_host *mmc, unsigned int timeout) |
| 933 | { |
| 934 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 935 | bool ret; |
| 936 | u32 ctl; |
| 937 | |
| 938 | if (cqhci_halted(cq_host)) |
| 939 | return true; |
| 940 | |
| 941 | cqhci_set_irqs(cq_host, CQHCI_IS_HAC); |
| 942 | |
| 943 | ctl = cqhci_readl(cq_host, CQHCI_CTL); |
| 944 | ctl |= CQHCI_HALT; |
| 945 | cqhci_writel(cq_host, ctl, CQHCI_CTL); |
| 946 | |
| 947 | wait_event_timeout(cq_host->wait_queue, cqhci_halted(cq_host), |
| 948 | msecs_to_jiffies(timeout) + 1); |
| 949 | |
| 950 | cqhci_set_irqs(cq_host, 0); |
| 951 | |
| 952 | ret = cqhci_halted(cq_host); |
| 953 | |
| 954 | if (!ret) |
| 955 | pr_debug("%s: cqhci: Failed to halt\n", mmc_hostname(mmc)); |
| 956 | |
| 957 | return ret; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * After halting we expect to be able to use the command line. We interpret the |
| 962 | * failure to halt to mean the data lines might still be in use (and the upper |
| 963 | * layers will need to send a STOP command), so we set the timeout based on a |
| 964 | * generous command timeout. |
| 965 | */ |
| 966 | #define CQHCI_START_HALT_TIMEOUT 5 |
| 967 | |
| 968 | static void cqhci_recovery_start(struct mmc_host *mmc) |
| 969 | { |
| 970 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 971 | |
| 972 | pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__); |
| 973 | |
| 974 | WARN_ON(!cq_host->recovery_halt); |
| 975 | |
| 976 | cqhci_halt(mmc, CQHCI_START_HALT_TIMEOUT); |
| 977 | |
| 978 | if (cq_host->ops->disable) |
| 979 | cq_host->ops->disable(mmc, true); |
| 980 | |
| 981 | mmc->cqe_on = false; |
| 982 | } |
| 983 | |
| 984 | static int cqhci_error_from_flags(unsigned int flags) |
| 985 | { |
| 986 | if (!flags) |
| 987 | return 0; |
| 988 | |
| 989 | /* CRC errors might indicate re-tuning so prefer to report that */ |
| 990 | if (flags & CQHCI_HOST_CRC) |
| 991 | return -EILSEQ; |
| 992 | |
| 993 | if (flags & (CQHCI_EXTERNAL_TIMEOUT | CQHCI_HOST_TIMEOUT)) |
| 994 | return -ETIMEDOUT; |
| 995 | |
| 996 | return -EIO; |
| 997 | } |
| 998 | |
| 999 | static void cqhci_recover_mrq(struct cqhci_host *cq_host, unsigned int tag) |
| 1000 | { |
| 1001 | struct cqhci_slot *slot = &cq_host->slot[tag]; |
| 1002 | struct mmc_request *mrq = slot->mrq; |
| 1003 | struct mmc_data *data; |
| 1004 | |
| 1005 | if (!mrq) |
| 1006 | return; |
| 1007 | |
| 1008 | slot->mrq = NULL; |
| 1009 | |
| 1010 | cq_host->qcnt -= 1; |
| 1011 | |
| 1012 | data = mrq->data; |
| 1013 | if (data) { |
| 1014 | data->bytes_xfered = 0; |
| 1015 | data->error = cqhci_error_from_flags(slot->flags); |
| 1016 | } else { |
| 1017 | mrq->cmd->error = cqhci_error_from_flags(slot->flags); |
| 1018 | } |
| 1019 | |
| 1020 | mmc_cqe_request_done(cq_host->mmc, mrq); |
| 1021 | } |
| 1022 | |
| 1023 | static void cqhci_recover_mrqs(struct cqhci_host *cq_host) |
| 1024 | { |
| 1025 | int i; |
| 1026 | |
| 1027 | for (i = 0; i < cq_host->num_slots; i++) |
| 1028 | cqhci_recover_mrq(cq_host, i); |
| 1029 | } |
| 1030 | |
| 1031 | /* |
| 1032 | * By now the command and data lines should be unused so there is no reason for |
| 1033 | * CQHCI to take a long time to halt, but if it doesn't halt there could be |
| 1034 | * problems clearing tasks, so be generous. |
| 1035 | */ |
| 1036 | #define CQHCI_FINISH_HALT_TIMEOUT 20 |
| 1037 | |
| 1038 | /* CQHCI could be expected to clear it's internal state pretty quickly */ |
| 1039 | #define CQHCI_CLEAR_TIMEOUT 20 |
| 1040 | |
| 1041 | static void cqhci_recovery_finish(struct mmc_host *mmc) |
| 1042 | { |
| 1043 | struct cqhci_host *cq_host = mmc->cqe_private; |
| 1044 | unsigned long flags; |
| 1045 | u32 cqcfg; |
| 1046 | bool ok; |
| 1047 | |
| 1048 | pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__); |
| 1049 | |
| 1050 | WARN_ON(!cq_host->recovery_halt); |
| 1051 | |
| 1052 | ok = cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT); |
| 1053 | |
| 1054 | if (!cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT)) |
| 1055 | ok = false; |
| 1056 | |
| 1057 | /* |
| 1058 | * The specification contradicts itself, by saying that tasks cannot be |
| 1059 | * cleared if CQHCI does not halt, but if CQHCI does not halt, it should |
| 1060 | * be disabled/re-enabled, but not to disable before clearing tasks. |
| 1061 | * Have a go anyway. |
| 1062 | */ |
| 1063 | if (!ok) { |
| 1064 | pr_debug("%s: cqhci: disable / re-enable\n", mmc_hostname(mmc)); |
| 1065 | cqcfg = cqhci_readl(cq_host, CQHCI_CFG); |
| 1066 | cqcfg &= ~CQHCI_ENABLE; |
| 1067 | cqhci_writel(cq_host, cqcfg, CQHCI_CFG); |
| 1068 | cqcfg |= CQHCI_ENABLE; |
| 1069 | cqhci_writel(cq_host, cqcfg, CQHCI_CFG); |
| 1070 | /* Be sure that there are no tasks */ |
| 1071 | ok = cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT); |
| 1072 | if (!cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT)) |
| 1073 | ok = false; |
| 1074 | WARN_ON(!ok); |
| 1075 | } |
| 1076 | |
| 1077 | cqhci_recover_mrqs(cq_host); |
| 1078 | |
| 1079 | WARN_ON(cq_host->qcnt); |
| 1080 | |
| 1081 | spin_lock_irqsave(&cq_host->lock, flags); |
| 1082 | cq_host->qcnt = 0; |
| 1083 | cq_host->recovery_halt = false; |
| 1084 | mmc->cqe_on = false; |
| 1085 | spin_unlock_irqrestore(&cq_host->lock, flags); |
| 1086 | |
| 1087 | /* Ensure all writes are done before interrupts are re-enabled */ |
| 1088 | wmb(); |
| 1089 | |
| 1090 | cqhci_writel(cq_host, CQHCI_IS_HAC | CQHCI_IS_TCL, CQHCI_IS); |
| 1091 | |
| 1092 | cqhci_set_irqs(cq_host, CQHCI_IS_MASK); |
| 1093 | |
| 1094 | pr_debug("%s: cqhci: recovery done\n", mmc_hostname(mmc)); |
| 1095 | } |
| 1096 | |
| 1097 | static const struct mmc_cqe_ops cqhci_cqe_ops = { |
| 1098 | .cqe_enable = cqhci_enable, |
| 1099 | .cqe_disable = cqhci_disable, |
| 1100 | .cqe_request = cqhci_request, |
| 1101 | .cqe_post_req = cqhci_post_req, |
| 1102 | .cqe_off = cqhci_off, |
| 1103 | .cqe_wait_for_idle = cqhci_wait_for_idle, |
| 1104 | .cqe_timeout = cqhci_timeout, |
| 1105 | .cqe_recovery_start = cqhci_recovery_start, |
| 1106 | .cqe_recovery_finish = cqhci_recovery_finish, |
| 1107 | }; |
| 1108 | |
| 1109 | struct cqhci_host *cqhci_pltfm_init(struct platform_device *pdev) |
| 1110 | { |
| 1111 | struct cqhci_host *cq_host; |
| 1112 | struct resource *cqhci_memres = NULL; |
| 1113 | |
| 1114 | /* check and setup CMDQ interface */ |
| 1115 | cqhci_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM, |
| 1116 | "cqhci_mem"); |
| 1117 | if (!cqhci_memres) { |
| 1118 | dev_dbg(&pdev->dev, "CMDQ not supported\n"); |
| 1119 | return ERR_PTR(-EINVAL); |
| 1120 | } |
| 1121 | |
| 1122 | cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL); |
| 1123 | if (!cq_host) |
| 1124 | return ERR_PTR(-ENOMEM); |
| 1125 | cq_host->mmio = devm_ioremap(&pdev->dev, |
| 1126 | cqhci_memres->start, |
| 1127 | resource_size(cqhci_memres)); |
| 1128 | if (!cq_host->mmio) { |
| 1129 | dev_err(&pdev->dev, "failed to remap cqhci regs\n"); |
| 1130 | return ERR_PTR(-EBUSY); |
| 1131 | } |
| 1132 | dev_dbg(&pdev->dev, "CMDQ ioremap: done\n"); |
| 1133 | |
| 1134 | return cq_host; |
| 1135 | } |
| 1136 | EXPORT_SYMBOL(cqhci_pltfm_init); |
| 1137 | |
| 1138 | static unsigned int cqhci_ver_major(struct cqhci_host *cq_host) |
| 1139 | { |
| 1140 | return CQHCI_VER_MAJOR(cqhci_readl(cq_host, CQHCI_VER)); |
| 1141 | } |
| 1142 | |
| 1143 | static unsigned int cqhci_ver_minor(struct cqhci_host *cq_host) |
| 1144 | { |
| 1145 | u32 ver = cqhci_readl(cq_host, CQHCI_VER); |
| 1146 | |
| 1147 | return CQHCI_VER_MINOR1(ver) * 10 + CQHCI_VER_MINOR2(ver); |
| 1148 | } |
| 1149 | |
| 1150 | int cqhci_init(struct cqhci_host *cq_host, struct mmc_host *mmc, |
| 1151 | bool dma64) |
| 1152 | { |
| 1153 | int err; |
| 1154 | |
| 1155 | cq_host->dma64 = dma64; |
| 1156 | cq_host->mmc = mmc; |
| 1157 | cq_host->mmc->cqe_private = cq_host; |
| 1158 | |
| 1159 | cq_host->num_slots = NUM_SLOTS; |
| 1160 | cq_host->dcmd_slot = DCMD_SLOT; |
| 1161 | |
| 1162 | mmc->cqe_ops = &cqhci_cqe_ops; |
| 1163 | |
| 1164 | mmc->cqe_qdepth = NUM_SLOTS; |
| 1165 | if (mmc->caps2 & MMC_CAP2_CQE_DCMD) |
| 1166 | mmc->cqe_qdepth -= 1; |
| 1167 | |
| 1168 | cq_host->slot = devm_kcalloc(mmc_dev(mmc), cq_host->num_slots, |
| 1169 | sizeof(*cq_host->slot), GFP_KERNEL); |
| 1170 | if (!cq_host->slot) { |
| 1171 | err = -ENOMEM; |
| 1172 | goto out_err; |
| 1173 | } |
| 1174 | |
| 1175 | spin_lock_init(&cq_host->lock); |
| 1176 | |
| 1177 | init_completion(&cq_host->halt_comp); |
| 1178 | init_waitqueue_head(&cq_host->wait_queue); |
| 1179 | |
| 1180 | pr_info("%s: CQHCI version %u.%02u\n", |
| 1181 | mmc_hostname(mmc), cqhci_ver_major(cq_host), |
| 1182 | cqhci_ver_minor(cq_host)); |
| 1183 | |
| 1184 | return 0; |
| 1185 | |
| 1186 | out_err: |
| 1187 | pr_err("%s: CQHCI version %u.%02u failed to initialize, error %d\n", |
| 1188 | mmc_hostname(mmc), cqhci_ver_major(cq_host), |
| 1189 | cqhci_ver_minor(cq_host), err); |
| 1190 | return err; |
| 1191 | } |
| 1192 | EXPORT_SYMBOL(cqhci_init); |
| 1193 | |
| 1194 | MODULE_AUTHOR("Venkat Gopalakrishnan <venkatg@codeaurora.org>"); |
| 1195 | MODULE_DESCRIPTION("Command Queue Host Controller Interface driver"); |
| 1196 | MODULE_LICENSE("GPL v2"); |