blob: b53d900a3e26a04152365f5d9335fcca64e9c7d2 [file] [log] [blame]
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -07001/* Copyright (c) 2015, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#include <linux/delay.h>
14#include <linux/highmem.h>
15#include <linux/io.h>
16#include <linux/module.h>
17#include <linux/dma-mapping.h>
18#include <linux/slab.h>
19#include <linux/scatterlist.h>
20#include <linux/platform_device.h>
21#include <linux/blkdev.h>
22
23#include <linux/mmc/mmc.h>
24#include <linux/mmc/host.h>
25#include <linux/mmc/card.h>
26
27#include "cmdq_hci.h"
28
29#define DCMD_SLOT 31
30#define NUM_SLOTS 32
31
Asutosh Dasaa1e1c72015-05-21 17:22:10 +053032/* 1 sec */
33#define HALT_TIMEOUT_MS 1000
34
Asutosh Das02e30862015-05-20 16:52:04 +053035static inline struct mmc_request *get_req_by_tag(struct cmdq_host *cq_host,
36 unsigned int tag)
37{
38 return cq_host->mrq_slot[tag];
39}
40
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -070041static inline u8 *get_desc(struct cmdq_host *cq_host, u8 tag)
42{
43 return cq_host->desc_base + (tag * cq_host->slot_sz);
44}
45
46static inline u8 *get_link_desc(struct cmdq_host *cq_host, u8 tag)
47{
48 u8 *desc = get_desc(cq_host, tag);
49
50 return desc + cq_host->task_desc_len;
51}
52
53static inline dma_addr_t get_trans_desc_dma(struct cmdq_host *cq_host, u8 tag)
54{
55 return cq_host->trans_desc_dma_base +
56 (cq_host->mmc->max_segs * tag *
57 cq_host->trans_desc_len);
58}
59
60static inline u8 *get_trans_desc(struct cmdq_host *cq_host, u8 tag)
61{
62 return cq_host->trans_desc_base +
63 (cq_host->trans_desc_len * cq_host->mmc->max_segs * tag);
64}
65
66static void setup_trans_desc(struct cmdq_host *cq_host, u8 tag)
67{
68 u8 *link_temp;
69 dma_addr_t trans_temp;
70
71 link_temp = get_link_desc(cq_host, tag);
72 trans_temp = get_trans_desc_dma(cq_host, tag);
73
74 memset(link_temp, 0, cq_host->link_desc_len);
75 if (cq_host->link_desc_len > 8)
76 *(link_temp + 8) = 0;
77
78 if (tag == DCMD_SLOT) {
79 *link_temp = VALID(0) | ACT(0) | END(1);
80 return;
81 }
82
83 *link_temp = VALID(1) | ACT(0x6) | END(0);
84
85 if (cq_host->dma64) {
86 __le64 *data_addr = (__le64 __force *)(link_temp + 4);
87 data_addr[0] = cpu_to_le64(trans_temp);
88 } else {
89 __le32 *data_addr = (__le32 __force *)(link_temp + 4);
90 data_addr[0] = cpu_to_le32(trans_temp);
91 }
92}
93
94static void cmdq_clear_set_irqs(struct cmdq_host *cq_host, u32 clear, u32 set)
95{
96 u32 ier;
97
98 ier = cmdq_readl(cq_host, CQISTE);
99 ier &= ~clear;
100 ier |= set;
101 cmdq_writel(cq_host, ier, CQISTE);
102 cmdq_writel(cq_host, ier, CQISGE);
103 /* ensure the writes are done */
104 mb();
105}
106
107
108#define DRV_NAME "cmdq-host"
109
110static void cmdq_dump_debug_ram(struct cmdq_host *cq_host)
111{
112 int i = 0;
113
114 pr_err("---- Debug RAM dump ----\n");
115 pr_err(DRV_NAME ": Debug RAM wrap-around: 0x%08x | Debug RAM overlap: 0x%08x\n",
116 cmdq_readl(cq_host, CQ_CMD_DBG_RAM_WA),
117 cmdq_readl(cq_host, CQ_CMD_DBG_RAM_OL));
118
119 while (i < 16) {
120 pr_err(DRV_NAME ": Debug RAM dump [%d]: 0x%08x\n", i,
121 cmdq_readl(cq_host, CQ_CMD_DBG_RAM + (0x4 * i)));
122 i++;
123 }
124 pr_err("-------------------------\n");
125}
126
127static void cmdq_dumpregs(struct cmdq_host *cq_host)
128{
129 struct mmc_host *mmc = cq_host->mmc;
130
Asutosh Das02e30862015-05-20 16:52:04 +0530131 pr_err(DRV_NAME ": ========== REGISTER DUMP (%s)==========\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700132 mmc_hostname(mmc));
133
Asutosh Das02e30862015-05-20 16:52:04 +0530134 pr_err(DRV_NAME ": Caps: 0x%08x | Version: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700135 cmdq_readl(cq_host, CQCAP),
136 cmdq_readl(cq_host, CQVER));
Asutosh Das02e30862015-05-20 16:52:04 +0530137 pr_err(DRV_NAME ": Queing config: 0x%08x | Queue Ctrl: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700138 cmdq_readl(cq_host, CQCFG),
139 cmdq_readl(cq_host, CQCTL));
Asutosh Das02e30862015-05-20 16:52:04 +0530140 pr_err(DRV_NAME ": Int stat: 0x%08x | Int enab: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700141 cmdq_readl(cq_host, CQIS),
142 cmdq_readl(cq_host, CQISTE));
Asutosh Das02e30862015-05-20 16:52:04 +0530143 pr_err(DRV_NAME ": Int sig: 0x%08x | Int Coal: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700144 cmdq_readl(cq_host, CQISGE),
145 cmdq_readl(cq_host, CQIC));
Asutosh Das02e30862015-05-20 16:52:04 +0530146 pr_err(DRV_NAME ": TDL base: 0x%08x | TDL up32: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700147 cmdq_readl(cq_host, CQTDLBA),
148 cmdq_readl(cq_host, CQTDLBAU));
Asutosh Das02e30862015-05-20 16:52:04 +0530149 pr_err(DRV_NAME ": Doorbell: 0x%08x | Comp Notif: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700150 cmdq_readl(cq_host, CQTDBR),
151 cmdq_readl(cq_host, CQTCN));
Asutosh Das02e30862015-05-20 16:52:04 +0530152 pr_err(DRV_NAME ": Dev queue: 0x%08x | Dev Pend: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700153 cmdq_readl(cq_host, CQDQS),
154 cmdq_readl(cq_host, CQDPT));
Asutosh Das02e30862015-05-20 16:52:04 +0530155 pr_err(DRV_NAME ": Task clr: 0x%08x | Send stat 1: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700156 cmdq_readl(cq_host, CQTCLR),
157 cmdq_readl(cq_host, CQSSC1));
Asutosh Das02e30862015-05-20 16:52:04 +0530158 pr_err(DRV_NAME ": Send stat 2: 0x%08x | DCMD resp: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700159 cmdq_readl(cq_host, CQSSC2),
160 cmdq_readl(cq_host, CQCRDCT));
Asutosh Das02e30862015-05-20 16:52:04 +0530161 pr_err(DRV_NAME ": Resp err mask: 0x%08x | Task err: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700162 cmdq_readl(cq_host, CQRMEM),
163 cmdq_readl(cq_host, CQTERRI));
Asutosh Das02e30862015-05-20 16:52:04 +0530164 pr_err(DRV_NAME ": Resp idx 0x%08x | Resp arg: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700165 cmdq_readl(cq_host, CQCRI),
166 cmdq_readl(cq_host, CQCRA));
Asutosh Das02e30862015-05-20 16:52:04 +0530167 pr_err(DRV_NAME ": ===========================================\n");
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700168
169 cmdq_dump_debug_ram(cq_host);
170 if (cq_host->ops->dump_vendor_regs)
171 cq_host->ops->dump_vendor_regs(mmc);
172}
173
174/**
175 * The allocated descriptor table for task, link & transfer descritors
176 * looks like:
177 * |----------|
178 * |task desc | |->|----------|
179 * |----------| | |trans desc|
180 * |link desc-|->| |----------|
181 * |----------| .
182 * . .
183 * no. of slots max-segs
184 * . |----------|
185 * |----------|
186 * The idea here is to create the [task+trans] table and mark & point the
187 * link desc to the transfer desc table on a per slot basis.
188 */
189static int cmdq_host_alloc_tdl(struct cmdq_host *cq_host)
190{
191
192 size_t desc_size;
193 size_t data_size;
194 int i = 0;
195
196 /* task descriptor can be 64/128 bit irrespective of arch */
197 if (cq_host->caps & CMDQ_TASK_DESC_SZ_128) {
198 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) |
199 CQ_TASK_DESC_SZ, CQCFG);
200 cq_host->task_desc_len = 16;
201 } else {
202 cq_host->task_desc_len = 8;
203 }
204
205 /*
206 * 96 bits length of transfer desc instead of 128 bits which means
207 * ADMA would expect next valid descriptor at the 96th bit
208 * or 128th bit
209 */
210 if (cq_host->dma64) {
211 if (cq_host->quirks & CMDQ_QUIRK_SHORT_TXFR_DESC_SZ)
212 cq_host->trans_desc_len = 12;
213 else
214 cq_host->trans_desc_len = 16;
215 cq_host->link_desc_len = 16;
216 } else {
217 cq_host->trans_desc_len = 8;
218 cq_host->link_desc_len = 8;
219 }
220
221 /* total size of a slot: 1 task & 1 transfer (link) */
222 cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len;
223
224 desc_size = cq_host->slot_sz * cq_host->num_slots;
225
226 data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs *
227 (cq_host->num_slots - 1);
228
229 pr_info("%s: desc_size: %d data_sz: %d slot-sz: %d\n", __func__,
230 (int)desc_size, (int)data_size, cq_host->slot_sz);
231
232 /*
233 * allocate a dma-mapped chunk of memory for the descriptors
234 * allocate a dma-mapped chunk of memory for link descriptors
235 * setup each link-desc memory offset per slot-number to
236 * the descriptor table.
237 */
238 cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
239 desc_size,
240 &cq_host->desc_dma_base,
241 GFP_KERNEL);
242 cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
243 data_size,
244 &cq_host->trans_desc_dma_base,
245 GFP_KERNEL);
246 if (!cq_host->desc_base || !cq_host->trans_desc_base)
247 return -ENOMEM;
248
249 pr_info("desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n",
250 cq_host->desc_base, cq_host->trans_desc_base,
251 (unsigned long long)cq_host->desc_dma_base,
252 (unsigned long long) cq_host->trans_desc_dma_base);
253
254 for (; i < (cq_host->num_slots); i++)
255 setup_trans_desc(cq_host, i);
256
257 return 0;
258}
259
260static int cmdq_enable(struct mmc_host *mmc)
261{
262 int err = 0;
263 u32 cqcfg;
264 bool dcmd_enable;
265 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
266
267 if (!cq_host || !mmc->card || !mmc_card_cmdq(mmc->card)) {
268 err = -EINVAL;
269 goto out;
270 }
271
272 if (cq_host->enabled)
273 goto out;
274
275 cqcfg = cmdq_readl(cq_host, CQCFG);
276 if (cqcfg & 0x1) {
277 pr_info("%s: %s: cq_host is already enabled\n",
278 mmc_hostname(mmc), __func__);
279 WARN_ON(1);
280 goto out;
281 }
282
283 if (cq_host->quirks & CMDQ_QUIRK_NO_DCMD)
284 dcmd_enable = false;
285 else
286 dcmd_enable = true;
287
288 cqcfg = ((cq_host->caps & CMDQ_TASK_DESC_SZ_128 ? CQ_TASK_DESC_SZ : 0) |
289 (dcmd_enable ? CQ_DCMD : 0));
290
291 cmdq_writel(cq_host, cqcfg, CQCFG);
292 /* enable CQ_HOST */
293 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) | CQ_ENABLE,
294 CQCFG);
295
296 if (!cq_host->desc_base ||
297 !cq_host->trans_desc_base) {
298 err = cmdq_host_alloc_tdl(cq_host);
299 if (err)
300 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700301 }
302
Konstantin Dorfman14c902d2015-06-11 11:33:23 +0300303 cmdq_writel(cq_host, lower_32_bits(cq_host->desc_dma_base), CQTDLBA);
304 cmdq_writel(cq_host, upper_32_bits(cq_host->desc_dma_base), CQTDLBAU);
305
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700306 /*
307 * disable all vendor interrupts
308 * enable CMDQ interrupts
309 * enable the vendor error interrupts
310 */
311 if (cq_host->ops->clear_set_irqs)
312 cq_host->ops->clear_set_irqs(mmc, true);
313
314 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
315
316 /* cq_host would use this rca to address the card */
317 cmdq_writel(cq_host, mmc->card->rca, CQSSC2);
318
319 /* send QSR at lesser intervals than the default */
320 cmdq_writel(cq_host, cmdq_readl(cq_host, CQSSC1) | SEND_QSR_INTERVAL,
321 CQSSC1);
322
323 /* ensure the writes are done before enabling CQE */
324 mb();
325
326 cq_host->enabled = true;
327
328 if (cq_host->ops->set_block_size)
329 cq_host->ops->set_block_size(cq_host->mmc);
330
331 if (cq_host->ops->set_data_timeout)
332 cq_host->ops->set_data_timeout(mmc, 0xf);
333
334 if (cq_host->ops->clear_set_dumpregs)
335 cq_host->ops->clear_set_dumpregs(mmc, 1);
336
337out:
338 return err;
339}
340
341static void cmdq_disable(struct mmc_host *mmc, bool soft)
342{
343 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
344
345 if (soft) {
346 cmdq_writel(cq_host, cmdq_readl(
347 cq_host, CQCFG) & ~(CQ_ENABLE),
348 CQCFG);
349 }
350
351 cq_host->enabled = false;
352}
353
Asutosh Das02e30862015-05-20 16:52:04 +0530354static void cmdq_reset(struct mmc_host *mmc, bool soft)
355{
356 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
357 unsigned int cqcfg;
358 unsigned int tdlba;
359 unsigned int tdlbau;
360 unsigned int rca;
361 int ret;
362
363 cqcfg = cmdq_readl(cq_host, CQCFG);
364 tdlba = cmdq_readl(cq_host, CQTDLBA);
365 tdlbau = cmdq_readl(cq_host, CQTDLBAU);
366 rca = cmdq_readl(cq_host, CQSSC2);
367
368 cmdq_disable(mmc, true);
369
370 if (cq_host->ops->reset) {
371 ret = cq_host->ops->reset(mmc);
372 if (ret) {
373 pr_crit("%s: reset CMDQ controller: failed\n",
374 mmc_hostname(mmc));
375 BUG();
376 }
377 }
378
379 cmdq_writel(cq_host, tdlba, CQTDLBA);
380 cmdq_writel(cq_host, tdlbau, CQTDLBAU);
381
382 if (cq_host->ops->clear_set_irqs)
383 cq_host->ops->clear_set_irqs(mmc, true);
384
385 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
386
387 /* cq_host would use this rca to address the card */
388 cmdq_writel(cq_host, rca, CQSSC2);
389
390 /* ensure the writes are done before enabling CQE */
391 mb();
392
393 cmdq_writel(cq_host, cqcfg, CQCFG);
394 cq_host->enabled = true;
395}
396
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700397static void cmdq_prep_task_desc(struct mmc_request *mrq,
398 u64 *data, bool intr, bool qbr)
399{
400 struct mmc_cmdq_req *cmdq_req = mrq->cmdq_req;
401 u32 req_flags = cmdq_req->cmdq_req_flags;
402
403 pr_debug("%s: %s: data-tag: 0x%08x - dir: %d - prio: %d - cnt: 0x%08x - addr: 0x%llx\n",
404 mmc_hostname(mrq->host), __func__,
405 !!(req_flags & DAT_TAG), !!(req_flags & DIR),
406 !!(req_flags & PRIO), cmdq_req->data.blocks,
407 (u64)mrq->cmdq_req->blk_addr);
408
409 *data = VALID(1) |
410 END(1) |
411 INT(intr) |
412 ACT(0x5) |
413 FORCED_PROG(!!(req_flags & FORCED_PRG)) |
414 CONTEXT(mrq->cmdq_req->ctx_id) |
415 DATA_TAG(!!(req_flags & DAT_TAG)) |
416 DATA_DIR(!!(req_flags & DIR)) |
417 PRIORITY(!!(req_flags & PRIO)) |
418 QBAR(qbr) |
419 REL_WRITE(!!(req_flags & REL_WR)) |
420 BLK_COUNT(mrq->cmdq_req->data.blocks) |
421 BLK_ADDR((u64)mrq->cmdq_req->blk_addr);
422}
423
424static int cmdq_dma_map(struct mmc_host *host, struct mmc_request *mrq)
425{
426 int sg_count;
427 struct mmc_data *data = mrq->data;
428
429 if (!data)
430 return -EINVAL;
431
432 sg_count = dma_map_sg(mmc_dev(host), data->sg,
433 data->sg_len,
434 (data->flags & MMC_DATA_WRITE) ?
435 DMA_TO_DEVICE : DMA_FROM_DEVICE);
436 if (!sg_count) {
437 pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
438 return -ENOMEM;
439 }
440
441 return sg_count;
442}
443
444static void cmdq_set_tran_desc(u8 *desc,
445 dma_addr_t addr, int len, bool end)
446{
447 __le64 *dataddr = (__le64 __force *)(desc + 4);
448 __le32 *attr = (__le32 __force *)desc;
449
450 *attr = (VALID(1) |
451 END(end ? 1 : 0) |
452 INT(0) |
453 ACT(0x4) |
454 DAT_LENGTH(len));
455
456 dataddr[0] = cpu_to_le64(addr);
457}
458
459static int cmdq_prep_tran_desc(struct mmc_request *mrq,
460 struct cmdq_host *cq_host, int tag)
461{
462 struct mmc_data *data = mrq->data;
463 int i, sg_count, len;
464 bool end = false;
465 dma_addr_t addr;
466 u8 *desc;
467 struct scatterlist *sg;
468
469 sg_count = cmdq_dma_map(mrq->host, mrq);
470 if (sg_count < 0) {
471 pr_err("%s: %s: unable to map sg lists, %d\n",
472 mmc_hostname(mrq->host), __func__, sg_count);
473 return sg_count;
474 }
475
476 desc = get_trans_desc(cq_host, tag);
477 memset(desc, 0, cq_host->trans_desc_len * cq_host->mmc->max_segs);
478
479 for_each_sg(data->sg, sg, sg_count, i) {
480 addr = sg_dma_address(sg);
481 len = sg_dma_len(sg);
482
483 if ((i+1) == sg_count)
484 end = true;
485 cmdq_set_tran_desc(desc, addr, len, end);
486 desc += cq_host->trans_desc_len;
487 }
488
489 pr_debug("%s: req: 0x%p tag: %d calc_trans_des: 0x%p sg-cnt: %d\n",
490 __func__, mrq->req, tag, desc, sg_count);
491
492 return 0;
493}
494
495static void cmdq_prep_dcmd_desc(struct mmc_host *mmc,
496 struct mmc_request *mrq)
497{
498 u64 *task_desc = NULL;
499 u64 data = 0;
500 u8 resp_type;
501 u8 *desc;
502 __le64 *dataddr;
503 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
504 u8 timing;
505
506 if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
507 resp_type = 0x0;
508 timing = 0x1;
509 } else {
Sahitya Tummala72bd8402015-05-29 13:27:38 +0530510 if (mrq->cmd->flags & MMC_RSP_BUSY) {
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700511 resp_type = 0x3;
512 timing = 0x0;
513 } else {
514 resp_type = 0x2;
515 timing = 0x1;
516 }
517 }
518
519 task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
520 memset(task_desc, 0, cq_host->task_desc_len);
521 data |= (VALID(1) |
522 END(1) |
523 INT(1) |
524 QBAR(1) |
525 ACT(0x5) |
526 CMD_INDEX(mrq->cmd->opcode) |
527 CMD_TIMING(timing) | RESP_TYPE(resp_type));
528 *task_desc |= data;
529 desc = (u8 *)task_desc;
530 pr_debug("cmdq: dcmd: cmd: %d timing: %d resp: %d\n",
531 mrq->cmd->opcode, timing, resp_type);
532 dataddr = (__le64 __force *)(desc + 4);
533 dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
534
535}
536
537static int cmdq_request(struct mmc_host *mmc, struct mmc_request *mrq)
538{
539 int err;
540 u64 data = 0;
541 u64 *task_desc = NULL;
542 u32 tag = mrq->cmdq_req->tag;
543 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
544
545 if (!cq_host->enabled) {
546 pr_err("%s: CMDQ host not enabled yet !!!\n",
547 mmc_hostname(mmc));
548 err = -EINVAL;
549 goto out;
550 }
551
552 if (mrq->cmdq_req->cmdq_req_flags & DCMD) {
553 cmdq_prep_dcmd_desc(mmc, mrq);
554 cq_host->mrq_slot[DCMD_SLOT] = mrq;
555 cmdq_writel(cq_host, 1 << DCMD_SLOT, CQTDBR);
556 return 0;
557 }
558
559 task_desc = (__le64 __force *)get_desc(cq_host, tag);
560
561 cmdq_prep_task_desc(mrq, &data, 1,
562 (mrq->cmdq_req->cmdq_req_flags & QBR));
563 *task_desc = cpu_to_le64(data);
564
565 err = cmdq_prep_tran_desc(mrq, cq_host, tag);
566 if (err) {
567 pr_err("%s: %s: failed to setup tx desc: %d\n",
568 mmc_hostname(mmc), __func__, err);
569 return err;
570 }
571
572 BUG_ON(cmdq_readl(cq_host, CQTDBR) & (1 << tag));
573
574 cq_host->mrq_slot[tag] = mrq;
575 if (cq_host->ops->set_tranfer_params)
576 cq_host->ops->set_tranfer_params(mmc);
577
578 cmdq_writel(cq_host, 1 << tag, CQTDBR);
579
580out:
581 return err;
582}
583
584static void cmdq_finish_data(struct mmc_host *mmc, unsigned int tag)
585{
586 struct mmc_request *mrq;
587 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
588
Asutosh Das02e30862015-05-20 16:52:04 +0530589 mrq = get_req_by_tag(cq_host, tag);
Sahitya Tummala9549d562015-05-29 15:41:18 +0530590 if (tag == cq_host->dcmd_slot)
591 mrq->cmd->resp[0] = cmdq_readl(cq_host, CQCRDCT);
592
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700593 mrq->done(mrq);
594}
595
Asutosh Das02e30862015-05-20 16:52:04 +0530596irqreturn_t cmdq_irq(struct mmc_host *mmc, int err)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700597{
598 u32 status;
599 unsigned long tag = 0, comp_status;
600 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Asutosh Das02e30862015-05-20 16:52:04 +0530601 unsigned long err_info = 0;
602 struct mmc_request *mrq;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700603
604 status = cmdq_readl(cq_host, CQIS);
605 cmdq_writel(cq_host, status, CQIS);
606
Asutosh Das02e30862015-05-20 16:52:04 +0530607 if (!status && !err)
608 return IRQ_NONE;
609
610 if (err || (status & CQIS_RED)) {
611 err_info = cmdq_readl(cq_host, CQTERRI);
612 pr_err("%s: err: %d status: 0x%08x task-err-info (0x%08lx)\n",
613 mmc_hostname(mmc), err, status, err_info);
614
615 cmdq_dumpregs(cq_host);
616
617 if (err_info & CQ_RMEFV) {
618 tag = GET_CMD_ERR_TAG(err_info);
619 pr_err("%s: CMD err tag: %lu\n", __func__, tag);
620
621 mrq = get_req_by_tag(cq_host, tag);
622 /* CMD44/45/46/47 will not have a valid cmd */
623 if (mrq->cmd)
624 mrq->cmd->error = err;
625 else
626 mrq->data->error = err;
627 } else {
628 tag = GET_DAT_ERR_TAG(err_info);
629 pr_err("%s: Dat err tag: %lu\n", __func__, tag);
630 mrq = get_req_by_tag(cq_host, tag);
631 mrq->data->error = err;
632 }
633
634 tag = 0;
635 /*
636 * CQE detected a response error from device
637 * In most cases, this would require a reset.
638 */
639 if (status & CQIS_RED) {
640 mrq->cmdq_req->resp_err = true;
641 pr_err("%s: Response error (0x%08x) from card !!!",
642 mmc_hostname(mmc), status);
643 } else {
644 mrq->cmdq_req->resp_idx = cmdq_readl(cq_host, CQCRI);
645 mrq->cmdq_req->resp_arg = cmdq_readl(cq_host, CQCRA);
646 }
647
648 mmc->err_mrq = mrq;
649 cmdq_finish_data(mmc, tag);
650 }
651
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700652 if (status & CQIS_TCC) {
653 /* read QCTCN and complete the request */
654 comp_status = cmdq_readl(cq_host, CQTCN);
655 if (!comp_status)
656 goto out;
657
658 for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
659 /* complete the corresponding mrq */
660 pr_debug("%s: completing tag -> %lu\n",
661 mmc_hostname(mmc), tag);
662 cmdq_finish_data(mmc, tag);
663 }
664 cmdq_writel(cq_host, comp_status, CQTCN);
665 }
666
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530667 if (status & CQIS_HAC) {
668 /* halt is completed, wakeup waiting thread */
669 complete(&cq_host->halt_comp);
670 }
671
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700672out:
673 return IRQ_HANDLED;
674}
675EXPORT_SYMBOL(cmdq_irq);
676
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530677/* May sleep */
678static int cmdq_halt(struct mmc_host *mmc, bool halt)
679{
680 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
681 u32 val;
682
683 if (halt) {
684 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT,
685 CQCTL);
686 val = wait_for_completion_timeout(&cq_host->halt_comp,
687 msecs_to_jiffies(HALT_TIMEOUT_MS));
688 /* halt done: re-enable legacy interrupts */
689 if (cq_host->ops->clear_set_irqs)
690 cq_host->ops->clear_set_irqs(mmc, false);
691
692 return val ? 0 : -ETIMEDOUT;
693 } else {
694 if (cq_host->ops->clear_set_irqs)
695 cq_host->ops->clear_set_irqs(mmc, true);
696 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) & ~HALT,
697 CQCTL);
698 }
699
700 return 0;
701}
702
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700703static void cmdq_post_req(struct mmc_host *host, struct mmc_request *mrq,
704 int err)
705{
706 struct mmc_data *data = mrq->data;
707
708 if (data) {
709 data->error = err;
710 dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len,
711 (data->flags & MMC_DATA_READ) ?
712 DMA_FROM_DEVICE : DMA_TO_DEVICE);
713 if (err)
714 data->bytes_xfered = 0;
715 else
716 data->bytes_xfered = blk_rq_bytes(mrq->req);
717 }
718}
719
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530720static void cmdq_dumpstate(struct mmc_host *mmc)
721{
722 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
723
724 cmdq_dumpregs(cq_host);
725}
726
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700727static const struct mmc_cmdq_host_ops cmdq_host_ops = {
728 .enable = cmdq_enable,
729 .disable = cmdq_disable,
730 .request = cmdq_request,
731 .post_req = cmdq_post_req,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530732 .halt = cmdq_halt,
Asutosh Das02e30862015-05-20 16:52:04 +0530733 .reset = cmdq_reset,
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530734 .dumpstate = cmdq_dumpstate,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700735};
736
737struct cmdq_host *cmdq_pltfm_init(struct platform_device *pdev)
738{
739 struct cmdq_host *cq_host;
740 struct resource *cmdq_memres = NULL;
741
742 /* check and setup CMDQ interface */
743 cmdq_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
744 "cmdq_mem");
745 if (!cmdq_memres) {
746 dev_dbg(&pdev->dev, "CMDQ not supported\n");
747 return ERR_PTR(-EINVAL);
748 }
749
750 cq_host = kzalloc(sizeof(*cq_host), GFP_KERNEL);
751 if (!cq_host) {
752 dev_err(&pdev->dev, "failed to allocate memory for CMDQ\n");
753 return ERR_PTR(-ENOMEM);
754 }
755 cq_host->mmio = devm_ioremap(&pdev->dev,
756 cmdq_memres->start,
757 resource_size(cmdq_memres));
758 if (!cq_host->mmio) {
759 dev_err(&pdev->dev, "failed to remap cmdq regs\n");
760 kfree(cq_host);
761 return ERR_PTR(-EBUSY);
762 }
763 dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
764
765 return cq_host;
766}
767EXPORT_SYMBOL(cmdq_pltfm_init);
768
769int cmdq_init(struct cmdq_host *cq_host, struct mmc_host *mmc,
770 bool dma64)
771{
772 int err = 0;
773
774 cq_host->dma64 = dma64;
775 cq_host->mmc = mmc;
776 cq_host->mmc->cmdq_private = cq_host;
777
778 cq_host->num_slots = NUM_SLOTS;
779 cq_host->dcmd_slot = DCMD_SLOT;
780
781 mmc->cmdq_ops = &cmdq_host_ops;
782
783 cq_host->mrq_slot = kzalloc(sizeof(cq_host->mrq_slot) *
784 cq_host->num_slots, GFP_KERNEL);
785 if (!cq_host->mrq_slot)
786 return -ENOMEM;
787
788 init_completion(&cq_host->halt_comp);
789 return err;
790}
791EXPORT_SYMBOL(cmdq_init);