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