Merge branch 'x86-mds-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.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/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
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         mmc->cqe_on = true;
284
285         if (cq_host->ops->enable)
286                 cq_host->ops->enable(mmc);
287
288         /* Ensure all writes are done before interrupts are enabled */
289         wmb();
290
291         cqhci_set_irqs(cq_host, CQHCI_IS_MASK);
292
293         cq_host->activated = true;
294 }
295
296 static void __cqhci_disable(struct cqhci_host *cq_host)
297 {
298         u32 cqcfg;
299
300         cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
301         cqcfg &= ~CQHCI_ENABLE;
302         cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
303
304         cq_host->mmc->cqe_on = false;
305
306         cq_host->activated = false;
307 }
308
309 int cqhci_suspend(struct mmc_host *mmc)
310 {
311         struct cqhci_host *cq_host = mmc->cqe_private;
312
313         if (cq_host->enabled)
314                 __cqhci_disable(cq_host);
315
316         return 0;
317 }
318 EXPORT_SYMBOL(cqhci_suspend);
319
320 int cqhci_resume(struct mmc_host *mmc)
321 {
322         /* Re-enable is done upon first request */
323         return 0;
324 }
325 EXPORT_SYMBOL(cqhci_resume);
326
327 static int cqhci_enable(struct mmc_host *mmc, struct mmc_card *card)
328 {
329         struct cqhci_host *cq_host = mmc->cqe_private;
330         int err;
331
332         if (cq_host->enabled)
333                 return 0;
334
335         cq_host->rca = card->rca;
336
337         err = cqhci_host_alloc_tdl(cq_host);
338         if (err)
339                 return err;
340
341         __cqhci_enable(cq_host);
342
343         cq_host->enabled = true;
344
345 #ifdef DEBUG
346         cqhci_dumpregs(cq_host);
347 #endif
348         return 0;
349 }
350
351 /* CQHCI is idle and should halt immediately, so set a small timeout */
352 #define CQHCI_OFF_TIMEOUT 100
353
354 static void cqhci_off(struct mmc_host *mmc)
355 {
356         struct cqhci_host *cq_host = mmc->cqe_private;
357         ktime_t timeout;
358         bool timed_out;
359         u32 reg;
360
361         if (!cq_host->enabled || !mmc->cqe_on || cq_host->recovery_halt)
362                 return;
363
364         if (cq_host->ops->disable)
365                 cq_host->ops->disable(mmc, false);
366
367         cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
368
369         timeout = ktime_add_us(ktime_get(), CQHCI_OFF_TIMEOUT);
370         while (1) {
371                 timed_out = ktime_compare(ktime_get(), timeout) > 0;
372                 reg = cqhci_readl(cq_host, CQHCI_CTL);
373                 if ((reg & CQHCI_HALT) || timed_out)
374                         break;
375         }
376
377         if (timed_out)
378                 pr_err("%s: cqhci: CQE stuck on\n", mmc_hostname(mmc));
379         else
380                 pr_debug("%s: cqhci: CQE off\n", mmc_hostname(mmc));
381
382         mmc->cqe_on = false;
383 }
384
385 static void cqhci_disable(struct mmc_host *mmc)
386 {
387         struct cqhci_host *cq_host = mmc->cqe_private;
388
389         if (!cq_host->enabled)
390                 return;
391
392         cqhci_off(mmc);
393
394         __cqhci_disable(cq_host);
395
396         dmam_free_coherent(mmc_dev(mmc), cq_host->data_size,
397                            cq_host->trans_desc_base,
398                            cq_host->trans_desc_dma_base);
399
400         dmam_free_coherent(mmc_dev(mmc), cq_host->desc_size,
401                            cq_host->desc_base,
402                            cq_host->desc_dma_base);
403
404         cq_host->trans_desc_base = NULL;
405         cq_host->desc_base = NULL;
406
407         cq_host->enabled = false;
408 }
409
410 static void cqhci_prep_task_desc(struct mmc_request *mrq,
411                                         u64 *data, bool intr)
412 {
413         u32 req_flags = mrq->data->flags;
414
415         *data = CQHCI_VALID(1) |
416                 CQHCI_END(1) |
417                 CQHCI_INT(intr) |
418                 CQHCI_ACT(0x5) |
419                 CQHCI_FORCED_PROG(!!(req_flags & MMC_DATA_FORCED_PRG)) |
420                 CQHCI_DATA_TAG(!!(req_flags & MMC_DATA_DAT_TAG)) |
421                 CQHCI_DATA_DIR(!!(req_flags & MMC_DATA_READ)) |
422                 CQHCI_PRIORITY(!!(req_flags & MMC_DATA_PRIO)) |
423                 CQHCI_QBAR(!!(req_flags & MMC_DATA_QBR)) |
424                 CQHCI_REL_WRITE(!!(req_flags & MMC_DATA_REL_WR)) |
425                 CQHCI_BLK_COUNT(mrq->data->blocks) |
426                 CQHCI_BLK_ADDR((u64)mrq->data->blk_addr);
427
428         pr_debug("%s: cqhci: tag %d task descriptor 0x016%llx\n",
429                  mmc_hostname(mrq->host), mrq->tag, (unsigned long long)*data);
430 }
431
432 static int cqhci_dma_map(struct mmc_host *host, struct mmc_request *mrq)
433 {
434         int sg_count;
435         struct mmc_data *data = mrq->data;
436
437         if (!data)
438                 return -EINVAL;
439
440         sg_count = dma_map_sg(mmc_dev(host), data->sg,
441                               data->sg_len,
442                               (data->flags & MMC_DATA_WRITE) ?
443                               DMA_TO_DEVICE : DMA_FROM_DEVICE);
444         if (!sg_count) {
445                 pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
446                 return -ENOMEM;
447         }
448
449         return sg_count;
450 }
451
452 static void cqhci_set_tran_desc(u8 *desc, dma_addr_t addr, int len, bool end,
453                                 bool dma64)
454 {
455         __le32 *attr = (__le32 __force *)desc;
456
457         *attr = (CQHCI_VALID(1) |
458                  CQHCI_END(end ? 1 : 0) |
459                  CQHCI_INT(0) |
460                  CQHCI_ACT(0x4) |
461                  CQHCI_DAT_LENGTH(len));
462
463         if (dma64) {
464                 __le64 *dataddr = (__le64 __force *)(desc + 4);
465
466                 dataddr[0] = cpu_to_le64(addr);
467         } else {
468                 __le32 *dataddr = (__le32 __force *)(desc + 4);
469
470                 dataddr[0] = cpu_to_le32(addr);
471         }
472 }
473
474 static int cqhci_prep_tran_desc(struct mmc_request *mrq,
475                                struct cqhci_host *cq_host, int tag)
476 {
477         struct mmc_data *data = mrq->data;
478         int i, sg_count, len;
479         bool end = false;
480         bool dma64 = cq_host->dma64;
481         dma_addr_t addr;
482         u8 *desc;
483         struct scatterlist *sg;
484
485         sg_count = cqhci_dma_map(mrq->host, mrq);
486         if (sg_count < 0) {
487                 pr_err("%s: %s: unable to map sg lists, %d\n",
488                                 mmc_hostname(mrq->host), __func__, sg_count);
489                 return sg_count;
490         }
491
492         desc = get_trans_desc(cq_host, tag);
493
494         for_each_sg(data->sg, sg, sg_count, i) {
495                 addr = sg_dma_address(sg);
496                 len = sg_dma_len(sg);
497
498                 if ((i+1) == sg_count)
499                         end = true;
500                 cqhci_set_tran_desc(desc, addr, len, end, dma64);
501                 desc += cq_host->trans_desc_len;
502         }
503
504         return 0;
505 }
506
507 static void cqhci_prep_dcmd_desc(struct mmc_host *mmc,
508                                    struct mmc_request *mrq)
509 {
510         u64 *task_desc = NULL;
511         u64 data = 0;
512         u8 resp_type;
513         u8 *desc;
514         __le64 *dataddr;
515         struct cqhci_host *cq_host = mmc->cqe_private;
516         u8 timing;
517
518         if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
519                 resp_type = 0x0;
520                 timing = 0x1;
521         } else {
522                 if (mrq->cmd->flags & MMC_RSP_R1B) {
523                         resp_type = 0x3;
524                         timing = 0x0;
525                 } else {
526                         resp_type = 0x2;
527                         timing = 0x1;
528                 }
529         }
530
531         task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
532         memset(task_desc, 0, cq_host->task_desc_len);
533         data |= (CQHCI_VALID(1) |
534                  CQHCI_END(1) |
535                  CQHCI_INT(1) |
536                  CQHCI_QBAR(1) |
537                  CQHCI_ACT(0x5) |
538                  CQHCI_CMD_INDEX(mrq->cmd->opcode) |
539                  CQHCI_CMD_TIMING(timing) | CQHCI_RESP_TYPE(resp_type));
540         if (cq_host->ops->update_dcmd_desc)
541                 cq_host->ops->update_dcmd_desc(mmc, mrq, &data);
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
623         cqhci_writel(cq_host, 1 << tag, CQHCI_TDBR);
624         if (!(cqhci_readl(cq_host, CQHCI_TDBR) & (1 << tag)))
625                 pr_debug("%s: cqhci: doorbell not set for tag %d\n",
626                          mmc_hostname(mmc), tag);
627 out_unlock:
628         spin_unlock_irqrestore(&cq_host->lock, flags);
629
630         if (err)
631                 cqhci_post_req(mmc, mrq);
632
633         return err;
634 }
635
636 static void cqhci_recovery_needed(struct mmc_host *mmc, struct mmc_request *mrq,
637                                   bool notify)
638 {
639         struct cqhci_host *cq_host = mmc->cqe_private;
640
641         if (!cq_host->recovery_halt) {
642                 cq_host->recovery_halt = true;
643                 pr_debug("%s: cqhci: recovery needed\n", mmc_hostname(mmc));
644                 wake_up(&cq_host->wait_queue);
645                 if (notify && mrq->recovery_notifier)
646                         mrq->recovery_notifier(mrq);
647         }
648 }
649
650 static unsigned int cqhci_error_flags(int error1, int error2)
651 {
652         int error = error1 ? error1 : error2;
653
654         switch (error) {
655         case -EILSEQ:
656                 return CQHCI_HOST_CRC;
657         case -ETIMEDOUT:
658                 return CQHCI_HOST_TIMEOUT;
659         default:
660                 return CQHCI_HOST_OTHER;
661         }
662 }
663
664 static void cqhci_error_irq(struct mmc_host *mmc, u32 status, int cmd_error,
665                             int data_error)
666 {
667         struct cqhci_host *cq_host = mmc->cqe_private;
668         struct cqhci_slot *slot;
669         u32 terri;
670         int tag;
671
672         spin_lock(&cq_host->lock);
673
674         terri = cqhci_readl(cq_host, CQHCI_TERRI);
675
676         pr_debug("%s: cqhci: error IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n",
677                  mmc_hostname(mmc), status, cmd_error, data_error, terri);
678
679         /* Forget about errors when recovery has already been triggered */
680         if (cq_host->recovery_halt)
681                 goto out_unlock;
682
683         if (!cq_host->qcnt) {
684                 WARN_ONCE(1, "%s: cqhci: error when idle. IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n",
685                           mmc_hostname(mmc), status, cmd_error, data_error,
686                           terri);
687                 goto out_unlock;
688         }
689
690         if (CQHCI_TERRI_C_VALID(terri)) {
691                 tag = CQHCI_TERRI_C_TASK(terri);
692                 slot = &cq_host->slot[tag];
693                 if (slot->mrq) {
694                         slot->flags = cqhci_error_flags(cmd_error, data_error);
695                         cqhci_recovery_needed(mmc, slot->mrq, true);
696                 }
697         }
698
699         if (CQHCI_TERRI_D_VALID(terri)) {
700                 tag = CQHCI_TERRI_D_TASK(terri);
701                 slot = &cq_host->slot[tag];
702                 if (slot->mrq) {
703                         slot->flags = cqhci_error_flags(data_error, cmd_error);
704                         cqhci_recovery_needed(mmc, slot->mrq, true);
705                 }
706         }
707
708         if (!cq_host->recovery_halt) {
709                 /*
710                  * The only way to guarantee forward progress is to mark at
711                  * least one task in error, so if none is indicated, pick one.
712                  */
713                 for (tag = 0; tag < NUM_SLOTS; tag++) {
714                         slot = &cq_host->slot[tag];
715                         if (!slot->mrq)
716                                 continue;
717                         slot->flags = cqhci_error_flags(data_error, cmd_error);
718                         cqhci_recovery_needed(mmc, slot->mrq, true);
719                         break;
720                 }
721         }
722
723 out_unlock:
724         spin_unlock(&cq_host->lock);
725 }
726
727 static void cqhci_finish_mrq(struct mmc_host *mmc, unsigned int tag)
728 {
729         struct cqhci_host *cq_host = mmc->cqe_private;
730         struct cqhci_slot *slot = &cq_host->slot[tag];
731         struct mmc_request *mrq = slot->mrq;
732         struct mmc_data *data;
733
734         if (!mrq) {
735                 WARN_ONCE(1, "%s: cqhci: spurious TCN for tag %d\n",
736                           mmc_hostname(mmc), tag);
737                 return;
738         }
739
740         /* No completions allowed during recovery */
741         if (cq_host->recovery_halt) {
742                 slot->flags |= CQHCI_COMPLETED;
743                 return;
744         }
745
746         slot->mrq = NULL;
747
748         cq_host->qcnt -= 1;
749
750         data = mrq->data;
751         if (data) {
752                 if (data->error)
753                         data->bytes_xfered = 0;
754                 else
755                         data->bytes_xfered = data->blksz * data->blocks;
756         }
757
758         mmc_cqe_request_done(mmc, mrq);
759 }
760
761 irqreturn_t cqhci_irq(struct mmc_host *mmc, u32 intmask, int cmd_error,
762                       int data_error)
763 {
764         u32 status;
765         unsigned long tag = 0, comp_status;
766         struct cqhci_host *cq_host = mmc->cqe_private;
767
768         status = cqhci_readl(cq_host, CQHCI_IS);
769         cqhci_writel(cq_host, status, CQHCI_IS);
770
771         pr_debug("%s: cqhci: IRQ status: 0x%08x\n", mmc_hostname(mmc), status);
772
773         if ((status & CQHCI_IS_RED) || cmd_error || data_error)
774                 cqhci_error_irq(mmc, status, cmd_error, data_error);
775
776         if (status & CQHCI_IS_TCC) {
777                 /* read TCN and complete the request */
778                 comp_status = cqhci_readl(cq_host, CQHCI_TCN);
779                 cqhci_writel(cq_host, comp_status, CQHCI_TCN);
780                 pr_debug("%s: cqhci: TCN: 0x%08lx\n",
781                          mmc_hostname(mmc), comp_status);
782
783                 spin_lock(&cq_host->lock);
784
785                 for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
786                         /* complete the corresponding mrq */
787                         pr_debug("%s: cqhci: completing tag %lu\n",
788                                  mmc_hostname(mmc), tag);
789                         cqhci_finish_mrq(mmc, tag);
790                 }
791
792                 if (cq_host->waiting_for_idle && !cq_host->qcnt) {
793                         cq_host->waiting_for_idle = false;
794                         wake_up(&cq_host->wait_queue);
795                 }
796
797                 spin_unlock(&cq_host->lock);
798         }
799
800         if (status & CQHCI_IS_TCL)
801                 wake_up(&cq_host->wait_queue);
802
803         if (status & CQHCI_IS_HAC)
804                 wake_up(&cq_host->wait_queue);
805
806         return IRQ_HANDLED;
807 }
808 EXPORT_SYMBOL(cqhci_irq);
809
810 static bool cqhci_is_idle(struct cqhci_host *cq_host, int *ret)
811 {
812         unsigned long flags;
813         bool is_idle;
814
815         spin_lock_irqsave(&cq_host->lock, flags);
816         is_idle = !cq_host->qcnt || cq_host->recovery_halt;
817         *ret = cq_host->recovery_halt ? -EBUSY : 0;
818         cq_host->waiting_for_idle = !is_idle;
819         spin_unlock_irqrestore(&cq_host->lock, flags);
820
821         return is_idle;
822 }
823
824 static int cqhci_wait_for_idle(struct mmc_host *mmc)
825 {
826         struct cqhci_host *cq_host = mmc->cqe_private;
827         int ret;
828
829         wait_event(cq_host->wait_queue, cqhci_is_idle(cq_host, &ret));
830
831         return ret;
832 }
833
834 static bool cqhci_timeout(struct mmc_host *mmc, struct mmc_request *mrq,
835                           bool *recovery_needed)
836 {
837         struct cqhci_host *cq_host = mmc->cqe_private;
838         int tag = cqhci_tag(mrq);
839         struct cqhci_slot *slot = &cq_host->slot[tag];
840         unsigned long flags;
841         bool timed_out;
842
843         spin_lock_irqsave(&cq_host->lock, flags);
844         timed_out = slot->mrq == mrq;
845         if (timed_out) {
846                 slot->flags |= CQHCI_EXTERNAL_TIMEOUT;
847                 cqhci_recovery_needed(mmc, mrq, false);
848                 *recovery_needed = cq_host->recovery_halt;
849         }
850         spin_unlock_irqrestore(&cq_host->lock, flags);
851
852         if (timed_out) {
853                 pr_err("%s: cqhci: timeout for tag %d\n",
854                        mmc_hostname(mmc), tag);
855                 cqhci_dumpregs(cq_host);
856         }
857
858         return timed_out;
859 }
860
861 static bool cqhci_tasks_cleared(struct cqhci_host *cq_host)
862 {
863         return !(cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_CLEAR_ALL_TASKS);
864 }
865
866 static bool cqhci_clear_all_tasks(struct mmc_host *mmc, unsigned int timeout)
867 {
868         struct cqhci_host *cq_host = mmc->cqe_private;
869         bool ret;
870         u32 ctl;
871
872         cqhci_set_irqs(cq_host, CQHCI_IS_TCL);
873
874         ctl = cqhci_readl(cq_host, CQHCI_CTL);
875         ctl |= CQHCI_CLEAR_ALL_TASKS;
876         cqhci_writel(cq_host, ctl, CQHCI_CTL);
877
878         wait_event_timeout(cq_host->wait_queue, cqhci_tasks_cleared(cq_host),
879                            msecs_to_jiffies(timeout) + 1);
880
881         cqhci_set_irqs(cq_host, 0);
882
883         ret = cqhci_tasks_cleared(cq_host);
884
885         if (!ret)
886                 pr_debug("%s: cqhci: Failed to clear tasks\n",
887                          mmc_hostname(mmc));
888
889         return ret;
890 }
891
892 static bool cqhci_halted(struct cqhci_host *cq_host)
893 {
894         return cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT;
895 }
896
897 static bool cqhci_halt(struct mmc_host *mmc, unsigned int timeout)
898 {
899         struct cqhci_host *cq_host = mmc->cqe_private;
900         bool ret;
901         u32 ctl;
902
903         if (cqhci_halted(cq_host))
904                 return true;
905
906         cqhci_set_irqs(cq_host, CQHCI_IS_HAC);
907
908         ctl = cqhci_readl(cq_host, CQHCI_CTL);
909         ctl |= CQHCI_HALT;
910         cqhci_writel(cq_host, ctl, CQHCI_CTL);
911
912         wait_event_timeout(cq_host->wait_queue, cqhci_halted(cq_host),
913                            msecs_to_jiffies(timeout) + 1);
914
915         cqhci_set_irqs(cq_host, 0);
916
917         ret = cqhci_halted(cq_host);
918
919         if (!ret)
920                 pr_debug("%s: cqhci: Failed to halt\n", mmc_hostname(mmc));
921
922         return ret;
923 }
924
925 /*
926  * After halting we expect to be able to use the command line. We interpret the
927  * failure to halt to mean the data lines might still be in use (and the upper
928  * layers will need to send a STOP command), so we set the timeout based on a
929  * generous command timeout.
930  */
931 #define CQHCI_START_HALT_TIMEOUT        5
932
933 static void cqhci_recovery_start(struct mmc_host *mmc)
934 {
935         struct cqhci_host *cq_host = mmc->cqe_private;
936
937         pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__);
938
939         WARN_ON(!cq_host->recovery_halt);
940
941         cqhci_halt(mmc, CQHCI_START_HALT_TIMEOUT);
942
943         if (cq_host->ops->disable)
944                 cq_host->ops->disable(mmc, true);
945
946         mmc->cqe_on = false;
947 }
948
949 static int cqhci_error_from_flags(unsigned int flags)
950 {
951         if (!flags)
952                 return 0;
953
954         /* CRC errors might indicate re-tuning so prefer to report that */
955         if (flags & CQHCI_HOST_CRC)
956                 return -EILSEQ;
957
958         if (flags & (CQHCI_EXTERNAL_TIMEOUT | CQHCI_HOST_TIMEOUT))
959                 return -ETIMEDOUT;
960
961         return -EIO;
962 }
963
964 static void cqhci_recover_mrq(struct cqhci_host *cq_host, unsigned int tag)
965 {
966         struct cqhci_slot *slot = &cq_host->slot[tag];
967         struct mmc_request *mrq = slot->mrq;
968         struct mmc_data *data;
969
970         if (!mrq)
971                 return;
972
973         slot->mrq = NULL;
974
975         cq_host->qcnt -= 1;
976
977         data = mrq->data;
978         if (data) {
979                 data->bytes_xfered = 0;
980                 data->error = cqhci_error_from_flags(slot->flags);
981         } else {
982                 mrq->cmd->error = cqhci_error_from_flags(slot->flags);
983         }
984
985         mmc_cqe_request_done(cq_host->mmc, mrq);
986 }
987
988 static void cqhci_recover_mrqs(struct cqhci_host *cq_host)
989 {
990         int i;
991
992         for (i = 0; i < cq_host->num_slots; i++)
993                 cqhci_recover_mrq(cq_host, i);
994 }
995
996 /*
997  * By now the command and data lines should be unused so there is no reason for
998  * CQHCI to take a long time to halt, but if it doesn't halt there could be
999  * problems clearing tasks, so be generous.
1000  */
1001 #define CQHCI_FINISH_HALT_TIMEOUT       20
1002
1003 /* CQHCI could be expected to clear it's internal state pretty quickly */
1004 #define CQHCI_CLEAR_TIMEOUT             20
1005
1006 static void cqhci_recovery_finish(struct mmc_host *mmc)
1007 {
1008         struct cqhci_host *cq_host = mmc->cqe_private;
1009         unsigned long flags;
1010         u32 cqcfg;
1011         bool ok;
1012
1013         pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__);
1014
1015         WARN_ON(!cq_host->recovery_halt);
1016
1017         ok = cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT);
1018
1019         if (!cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT))
1020                 ok = false;
1021
1022         /*
1023          * The specification contradicts itself, by saying that tasks cannot be
1024          * cleared if CQHCI does not halt, but if CQHCI does not halt, it should
1025          * be disabled/re-enabled, but not to disable before clearing tasks.
1026          * Have a go anyway.
1027          */
1028         if (!ok) {
1029                 pr_debug("%s: cqhci: disable / re-enable\n", mmc_hostname(mmc));
1030                 cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
1031                 cqcfg &= ~CQHCI_ENABLE;
1032                 cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
1033                 cqcfg |= CQHCI_ENABLE;
1034                 cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
1035                 /* Be sure that there are no tasks */
1036                 ok = cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT);
1037                 if (!cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT))
1038                         ok = false;
1039                 WARN_ON(!ok);
1040         }
1041
1042         cqhci_recover_mrqs(cq_host);
1043
1044         WARN_ON(cq_host->qcnt);
1045
1046         spin_lock_irqsave(&cq_host->lock, flags);
1047         cq_host->qcnt = 0;
1048         cq_host->recovery_halt = false;
1049         mmc->cqe_on = false;
1050         spin_unlock_irqrestore(&cq_host->lock, flags);
1051
1052         /* Ensure all writes are done before interrupts are re-enabled */
1053         wmb();
1054
1055         cqhci_writel(cq_host, CQHCI_IS_HAC | CQHCI_IS_TCL, CQHCI_IS);
1056
1057         cqhci_set_irqs(cq_host, CQHCI_IS_MASK);
1058
1059         pr_debug("%s: cqhci: recovery done\n", mmc_hostname(mmc));
1060 }
1061
1062 static const struct mmc_cqe_ops cqhci_cqe_ops = {
1063         .cqe_enable = cqhci_enable,
1064         .cqe_disable = cqhci_disable,
1065         .cqe_request = cqhci_request,
1066         .cqe_post_req = cqhci_post_req,
1067         .cqe_off = cqhci_off,
1068         .cqe_wait_for_idle = cqhci_wait_for_idle,
1069         .cqe_timeout = cqhci_timeout,
1070         .cqe_recovery_start = cqhci_recovery_start,
1071         .cqe_recovery_finish = cqhci_recovery_finish,
1072 };
1073
1074 struct cqhci_host *cqhci_pltfm_init(struct platform_device *pdev)
1075 {
1076         struct cqhci_host *cq_host;
1077         struct resource *cqhci_memres = NULL;
1078
1079         /* check and setup CMDQ interface */
1080         cqhci_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1081                                                    "cqhci_mem");
1082         if (!cqhci_memres) {
1083                 dev_dbg(&pdev->dev, "CMDQ not supported\n");
1084                 return ERR_PTR(-EINVAL);
1085         }
1086
1087         cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1088         if (!cq_host)
1089                 return ERR_PTR(-ENOMEM);
1090         cq_host->mmio = devm_ioremap(&pdev->dev,
1091                                      cqhci_memres->start,
1092                                      resource_size(cqhci_memres));
1093         if (!cq_host->mmio) {
1094                 dev_err(&pdev->dev, "failed to remap cqhci regs\n");
1095                 return ERR_PTR(-EBUSY);
1096         }
1097         dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
1098
1099         return cq_host;
1100 }
1101 EXPORT_SYMBOL(cqhci_pltfm_init);
1102
1103 static unsigned int cqhci_ver_major(struct cqhci_host *cq_host)
1104 {
1105         return CQHCI_VER_MAJOR(cqhci_readl(cq_host, CQHCI_VER));
1106 }
1107
1108 static unsigned int cqhci_ver_minor(struct cqhci_host *cq_host)
1109 {
1110         u32 ver = cqhci_readl(cq_host, CQHCI_VER);
1111
1112         return CQHCI_VER_MINOR1(ver) * 10 + CQHCI_VER_MINOR2(ver);
1113 }
1114
1115 int cqhci_init(struct cqhci_host *cq_host, struct mmc_host *mmc,
1116               bool dma64)
1117 {
1118         int err;
1119
1120         cq_host->dma64 = dma64;
1121         cq_host->mmc = mmc;
1122         cq_host->mmc->cqe_private = cq_host;
1123
1124         cq_host->num_slots = NUM_SLOTS;
1125         cq_host->dcmd_slot = DCMD_SLOT;
1126
1127         mmc->cqe_ops = &cqhci_cqe_ops;
1128
1129         mmc->cqe_qdepth = NUM_SLOTS;
1130         if (mmc->caps2 & MMC_CAP2_CQE_DCMD)
1131                 mmc->cqe_qdepth -= 1;
1132
1133         cq_host->slot = devm_kcalloc(mmc_dev(mmc), cq_host->num_slots,
1134                                      sizeof(*cq_host->slot), GFP_KERNEL);
1135         if (!cq_host->slot) {
1136                 err = -ENOMEM;
1137                 goto out_err;
1138         }
1139
1140         spin_lock_init(&cq_host->lock);
1141
1142         init_completion(&cq_host->halt_comp);
1143         init_waitqueue_head(&cq_host->wait_queue);
1144
1145         pr_info("%s: CQHCI version %u.%02u\n",
1146                 mmc_hostname(mmc), cqhci_ver_major(cq_host),
1147                 cqhci_ver_minor(cq_host));
1148
1149         return 0;
1150
1151 out_err:
1152         pr_err("%s: CQHCI version %u.%02u failed to initialize, error %d\n",
1153                mmc_hostname(mmc), cqhci_ver_major(cq_host),
1154                cqhci_ver_minor(cq_host), err);
1155         return err;
1156 }
1157 EXPORT_SYMBOL(cqhci_init);
1158
1159 MODULE_AUTHOR("Venkat Gopalakrishnan <venkatg@codeaurora.org>");
1160 MODULE_DESCRIPTION("Command Queue Host Controller Interface driver");
1161 MODULE_LICENSE("GPL v2");