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