blob: 66979c24b0fb99d412222d3bee6c1847f0a4f9f3 [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>
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +030026#include <linux/pm_runtime.h>
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -070027
28#include "cmdq_hci.h"
29
30#define DCMD_SLOT 31
31#define NUM_SLOTS 32
32
Asutosh Dasaa1e1c72015-05-21 17:22:10 +053033/* 1 sec */
34#define HALT_TIMEOUT_MS 1000
35
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +030036#ifdef CONFIG_PM_RUNTIME
37static int cmdq_runtime_pm_get(struct cmdq_host *host)
38{
39 return pm_runtime_get_sync(host->mmc->parent);
40}
41static int cmdq_runtime_pm_put(struct cmdq_host *host)
42{
43 pm_runtime_mark_last_busy(host->mmc->parent);
44 return pm_runtime_put_autosuspend(host->mmc->parent);
45}
46#else
47static inline int cmdq_runtime_pm_get(struct cmdq_host *host)
48{
49 return 0;
50}
51static inline int cmdq_runtime_pm_put(struct cmdq_host *host)
52{
53 return 0;
54}
55#endif
Asutosh Das02e30862015-05-20 16:52:04 +053056static inline struct mmc_request *get_req_by_tag(struct cmdq_host *cq_host,
57 unsigned int tag)
58{
59 return cq_host->mrq_slot[tag];
60}
61
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -070062static inline u8 *get_desc(struct cmdq_host *cq_host, u8 tag)
63{
64 return cq_host->desc_base + (tag * cq_host->slot_sz);
65}
66
67static inline u8 *get_link_desc(struct cmdq_host *cq_host, u8 tag)
68{
69 u8 *desc = get_desc(cq_host, tag);
70
71 return desc + cq_host->task_desc_len;
72}
73
74static inline dma_addr_t get_trans_desc_dma(struct cmdq_host *cq_host, u8 tag)
75{
76 return cq_host->trans_desc_dma_base +
77 (cq_host->mmc->max_segs * tag *
78 cq_host->trans_desc_len);
79}
80
81static inline u8 *get_trans_desc(struct cmdq_host *cq_host, u8 tag)
82{
83 return cq_host->trans_desc_base +
84 (cq_host->trans_desc_len * cq_host->mmc->max_segs * tag);
85}
86
87static void setup_trans_desc(struct cmdq_host *cq_host, u8 tag)
88{
89 u8 *link_temp;
90 dma_addr_t trans_temp;
91
92 link_temp = get_link_desc(cq_host, tag);
93 trans_temp = get_trans_desc_dma(cq_host, tag);
94
95 memset(link_temp, 0, cq_host->link_desc_len);
96 if (cq_host->link_desc_len > 8)
97 *(link_temp + 8) = 0;
98
99 if (tag == DCMD_SLOT) {
100 *link_temp = VALID(0) | ACT(0) | END(1);
101 return;
102 }
103
104 *link_temp = VALID(1) | ACT(0x6) | END(0);
105
106 if (cq_host->dma64) {
107 __le64 *data_addr = (__le64 __force *)(link_temp + 4);
108 data_addr[0] = cpu_to_le64(trans_temp);
109 } else {
110 __le32 *data_addr = (__le32 __force *)(link_temp + 4);
111 data_addr[0] = cpu_to_le32(trans_temp);
112 }
113}
114
115static void cmdq_clear_set_irqs(struct cmdq_host *cq_host, u32 clear, u32 set)
116{
117 u32 ier;
118
119 ier = cmdq_readl(cq_host, CQISTE);
120 ier &= ~clear;
121 ier |= set;
122 cmdq_writel(cq_host, ier, CQISTE);
123 cmdq_writel(cq_host, ier, CQISGE);
124 /* ensure the writes are done */
125 mb();
126}
127
128
129#define DRV_NAME "cmdq-host"
130
131static void cmdq_dump_debug_ram(struct cmdq_host *cq_host)
132{
133 int i = 0;
134
135 pr_err("---- Debug RAM dump ----\n");
136 pr_err(DRV_NAME ": Debug RAM wrap-around: 0x%08x | Debug RAM overlap: 0x%08x\n",
137 cmdq_readl(cq_host, CQ_CMD_DBG_RAM_WA),
138 cmdq_readl(cq_host, CQ_CMD_DBG_RAM_OL));
139
140 while (i < 16) {
141 pr_err(DRV_NAME ": Debug RAM dump [%d]: 0x%08x\n", i,
142 cmdq_readl(cq_host, CQ_CMD_DBG_RAM + (0x4 * i)));
143 i++;
144 }
145 pr_err("-------------------------\n");
146}
147
148static void cmdq_dumpregs(struct cmdq_host *cq_host)
149{
150 struct mmc_host *mmc = cq_host->mmc;
151
Asutosh Das02e30862015-05-20 16:52:04 +0530152 pr_err(DRV_NAME ": ========== REGISTER DUMP (%s)==========\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700153 mmc_hostname(mmc));
154
Asutosh Das02e30862015-05-20 16:52:04 +0530155 pr_err(DRV_NAME ": Caps: 0x%08x | Version: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700156 cmdq_readl(cq_host, CQCAP),
157 cmdq_readl(cq_host, CQVER));
Asutosh Das02e30862015-05-20 16:52:04 +0530158 pr_err(DRV_NAME ": Queing config: 0x%08x | Queue Ctrl: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700159 cmdq_readl(cq_host, CQCFG),
160 cmdq_readl(cq_host, CQCTL));
Asutosh Das02e30862015-05-20 16:52:04 +0530161 pr_err(DRV_NAME ": Int stat: 0x%08x | Int enab: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700162 cmdq_readl(cq_host, CQIS),
163 cmdq_readl(cq_host, CQISTE));
Asutosh Das02e30862015-05-20 16:52:04 +0530164 pr_err(DRV_NAME ": Int sig: 0x%08x | Int Coal: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700165 cmdq_readl(cq_host, CQISGE),
166 cmdq_readl(cq_host, CQIC));
Asutosh Das02e30862015-05-20 16:52:04 +0530167 pr_err(DRV_NAME ": TDL base: 0x%08x | TDL up32: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700168 cmdq_readl(cq_host, CQTDLBA),
169 cmdq_readl(cq_host, CQTDLBAU));
Asutosh Das02e30862015-05-20 16:52:04 +0530170 pr_err(DRV_NAME ": Doorbell: 0x%08x | Comp Notif: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700171 cmdq_readl(cq_host, CQTDBR),
172 cmdq_readl(cq_host, CQTCN));
Asutosh Das02e30862015-05-20 16:52:04 +0530173 pr_err(DRV_NAME ": Dev queue: 0x%08x | Dev Pend: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700174 cmdq_readl(cq_host, CQDQS),
175 cmdq_readl(cq_host, CQDPT));
Asutosh Das02e30862015-05-20 16:52:04 +0530176 pr_err(DRV_NAME ": Task clr: 0x%08x | Send stat 1: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700177 cmdq_readl(cq_host, CQTCLR),
178 cmdq_readl(cq_host, CQSSC1));
Asutosh Das02e30862015-05-20 16:52:04 +0530179 pr_err(DRV_NAME ": Send stat 2: 0x%08x | DCMD resp: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700180 cmdq_readl(cq_host, CQSSC2),
181 cmdq_readl(cq_host, CQCRDCT));
Asutosh Das02e30862015-05-20 16:52:04 +0530182 pr_err(DRV_NAME ": Resp err mask: 0x%08x | Task err: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700183 cmdq_readl(cq_host, CQRMEM),
184 cmdq_readl(cq_host, CQTERRI));
Asutosh Das02e30862015-05-20 16:52:04 +0530185 pr_err(DRV_NAME ": Resp idx 0x%08x | Resp arg: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700186 cmdq_readl(cq_host, CQCRI),
187 cmdq_readl(cq_host, CQCRA));
Asutosh Das02e30862015-05-20 16:52:04 +0530188 pr_err(DRV_NAME ": ===========================================\n");
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700189
190 cmdq_dump_debug_ram(cq_host);
191 if (cq_host->ops->dump_vendor_regs)
192 cq_host->ops->dump_vendor_regs(mmc);
193}
194
195/**
196 * The allocated descriptor table for task, link & transfer descritors
197 * looks like:
198 * |----------|
199 * |task desc | |->|----------|
200 * |----------| | |trans desc|
201 * |link desc-|->| |----------|
202 * |----------| .
203 * . .
204 * no. of slots max-segs
205 * . |----------|
206 * |----------|
207 * The idea here is to create the [task+trans] table and mark & point the
208 * link desc to the transfer desc table on a per slot basis.
209 */
210static int cmdq_host_alloc_tdl(struct cmdq_host *cq_host)
211{
212
213 size_t desc_size;
214 size_t data_size;
215 int i = 0;
216
217 /* task descriptor can be 64/128 bit irrespective of arch */
218 if (cq_host->caps & CMDQ_TASK_DESC_SZ_128) {
219 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) |
220 CQ_TASK_DESC_SZ, CQCFG);
221 cq_host->task_desc_len = 16;
222 } else {
223 cq_host->task_desc_len = 8;
224 }
225
226 /*
227 * 96 bits length of transfer desc instead of 128 bits which means
228 * ADMA would expect next valid descriptor at the 96th bit
229 * or 128th bit
230 */
231 if (cq_host->dma64) {
232 if (cq_host->quirks & CMDQ_QUIRK_SHORT_TXFR_DESC_SZ)
233 cq_host->trans_desc_len = 12;
234 else
235 cq_host->trans_desc_len = 16;
236 cq_host->link_desc_len = 16;
237 } else {
238 cq_host->trans_desc_len = 8;
239 cq_host->link_desc_len = 8;
240 }
241
242 /* total size of a slot: 1 task & 1 transfer (link) */
243 cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len;
244
245 desc_size = cq_host->slot_sz * cq_host->num_slots;
246
247 data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs *
248 (cq_host->num_slots - 1);
249
250 pr_info("%s: desc_size: %d data_sz: %d slot-sz: %d\n", __func__,
251 (int)desc_size, (int)data_size, cq_host->slot_sz);
252
253 /*
254 * allocate a dma-mapped chunk of memory for the descriptors
255 * allocate a dma-mapped chunk of memory for link descriptors
256 * setup each link-desc memory offset per slot-number to
257 * the descriptor table.
258 */
259 cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
260 desc_size,
261 &cq_host->desc_dma_base,
262 GFP_KERNEL);
263 cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
264 data_size,
265 &cq_host->trans_desc_dma_base,
266 GFP_KERNEL);
267 if (!cq_host->desc_base || !cq_host->trans_desc_base)
268 return -ENOMEM;
269
270 pr_info("desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n",
271 cq_host->desc_base, cq_host->trans_desc_base,
272 (unsigned long long)cq_host->desc_dma_base,
273 (unsigned long long) cq_host->trans_desc_dma_base);
274
275 for (; i < (cq_host->num_slots); i++)
276 setup_trans_desc(cq_host, i);
277
278 return 0;
279}
280
281static int cmdq_enable(struct mmc_host *mmc)
282{
283 int err = 0;
284 u32 cqcfg;
285 bool dcmd_enable;
286 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
287
288 if (!cq_host || !mmc->card || !mmc_card_cmdq(mmc->card)) {
289 err = -EINVAL;
290 goto out;
291 }
292
293 if (cq_host->enabled)
294 goto out;
295
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300296 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700297 cqcfg = cmdq_readl(cq_host, CQCFG);
298 if (cqcfg & 0x1) {
299 pr_info("%s: %s: cq_host is already enabled\n",
300 mmc_hostname(mmc), __func__);
301 WARN_ON(1);
302 goto out;
303 }
304
305 if (cq_host->quirks & CMDQ_QUIRK_NO_DCMD)
306 dcmd_enable = false;
307 else
308 dcmd_enable = true;
309
310 cqcfg = ((cq_host->caps & CMDQ_TASK_DESC_SZ_128 ? CQ_TASK_DESC_SZ : 0) |
311 (dcmd_enable ? CQ_DCMD : 0));
312
313 cmdq_writel(cq_host, cqcfg, CQCFG);
314 /* enable CQ_HOST */
315 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) | CQ_ENABLE,
316 CQCFG);
317
318 if (!cq_host->desc_base ||
319 !cq_host->trans_desc_base) {
320 err = cmdq_host_alloc_tdl(cq_host);
321 if (err)
322 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700323 }
324
Konstantin Dorfman14c902d2015-06-11 11:33:23 +0300325 cmdq_writel(cq_host, lower_32_bits(cq_host->desc_dma_base), CQTDLBA);
326 cmdq_writel(cq_host, upper_32_bits(cq_host->desc_dma_base), CQTDLBAU);
327
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700328 /*
329 * disable all vendor interrupts
330 * enable CMDQ interrupts
331 * enable the vendor error interrupts
332 */
333 if (cq_host->ops->clear_set_irqs)
334 cq_host->ops->clear_set_irqs(mmc, true);
335
336 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
337
338 /* cq_host would use this rca to address the card */
339 cmdq_writel(cq_host, mmc->card->rca, CQSSC2);
340
341 /* send QSR at lesser intervals than the default */
342 cmdq_writel(cq_host, cmdq_readl(cq_host, CQSSC1) | SEND_QSR_INTERVAL,
343 CQSSC1);
344
Dov Levenglick2b678302015-07-01 14:24:20 +0300345 /* enable bkops exception indication */
Dov Levenglickaea348b2015-07-20 11:59:52 +0300346 if (mmc_card_configured_manual_bkops(mmc->card) &&
347 !mmc_card_configured_auto_bkops(mmc->card))
Dov Levenglick2b678302015-07-01 14:24:20 +0300348 cmdq_writel(cq_host, cmdq_readl(cq_host, CQRMEM) | CQ_EXCEPTION,
349 CQRMEM);
350
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700351 /* ensure the writes are done before enabling CQE */
352 mb();
353
354 cq_host->enabled = true;
355
356 if (cq_host->ops->set_block_size)
357 cq_host->ops->set_block_size(cq_host->mmc);
358
359 if (cq_host->ops->set_data_timeout)
360 cq_host->ops->set_data_timeout(mmc, 0xf);
361
362 if (cq_host->ops->clear_set_dumpregs)
363 cq_host->ops->clear_set_dumpregs(mmc, 1);
364
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530365 if (cq_host->ops->enhanced_strobe_mask)
366 cq_host->ops->enhanced_strobe_mask(mmc, true);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700367out:
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300368 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700369 return err;
370}
371
372static void cmdq_disable(struct mmc_host *mmc, bool soft)
373{
374 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
375
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300376 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700377 if (soft) {
378 cmdq_writel(cq_host, cmdq_readl(
379 cq_host, CQCFG) & ~(CQ_ENABLE),
380 CQCFG);
381 }
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530382 if (cq_host->ops->enhanced_strobe_mask)
383 cq_host->ops->enhanced_strobe_mask(mmc, false);
384
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300385 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700386 cq_host->enabled = false;
387}
388
Asutosh Das02e30862015-05-20 16:52:04 +0530389static void cmdq_reset(struct mmc_host *mmc, bool soft)
390{
391 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
392 unsigned int cqcfg;
393 unsigned int tdlba;
394 unsigned int tdlbau;
395 unsigned int rca;
396 int ret;
397
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300398 cmdq_runtime_pm_get(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530399 cqcfg = cmdq_readl(cq_host, CQCFG);
400 tdlba = cmdq_readl(cq_host, CQTDLBA);
401 tdlbau = cmdq_readl(cq_host, CQTDLBAU);
402 rca = cmdq_readl(cq_host, CQSSC2);
403
404 cmdq_disable(mmc, true);
405
406 if (cq_host->ops->reset) {
407 ret = cq_host->ops->reset(mmc);
408 if (ret) {
409 pr_crit("%s: reset CMDQ controller: failed\n",
410 mmc_hostname(mmc));
411 BUG();
412 }
413 }
414
415 cmdq_writel(cq_host, tdlba, CQTDLBA);
416 cmdq_writel(cq_host, tdlbau, CQTDLBAU);
417
418 if (cq_host->ops->clear_set_irqs)
419 cq_host->ops->clear_set_irqs(mmc, true);
420
421 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
422
423 /* cq_host would use this rca to address the card */
424 cmdq_writel(cq_host, rca, CQSSC2);
425
426 /* ensure the writes are done before enabling CQE */
427 mb();
428
429 cmdq_writel(cq_host, cqcfg, CQCFG);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300430 cmdq_runtime_pm_put(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530431 cq_host->enabled = true;
432}
433
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700434static void cmdq_prep_task_desc(struct mmc_request *mrq,
435 u64 *data, bool intr, bool qbr)
436{
437 struct mmc_cmdq_req *cmdq_req = mrq->cmdq_req;
438 u32 req_flags = cmdq_req->cmdq_req_flags;
439
440 pr_debug("%s: %s: data-tag: 0x%08x - dir: %d - prio: %d - cnt: 0x%08x - addr: 0x%llx\n",
441 mmc_hostname(mrq->host), __func__,
442 !!(req_flags & DAT_TAG), !!(req_flags & DIR),
443 !!(req_flags & PRIO), cmdq_req->data.blocks,
444 (u64)mrq->cmdq_req->blk_addr);
445
446 *data = VALID(1) |
447 END(1) |
448 INT(intr) |
449 ACT(0x5) |
450 FORCED_PROG(!!(req_flags & FORCED_PRG)) |
451 CONTEXT(mrq->cmdq_req->ctx_id) |
452 DATA_TAG(!!(req_flags & DAT_TAG)) |
453 DATA_DIR(!!(req_flags & DIR)) |
454 PRIORITY(!!(req_flags & PRIO)) |
455 QBAR(qbr) |
456 REL_WRITE(!!(req_flags & REL_WR)) |
457 BLK_COUNT(mrq->cmdq_req->data.blocks) |
458 BLK_ADDR((u64)mrq->cmdq_req->blk_addr);
459}
460
461static int cmdq_dma_map(struct mmc_host *host, struct mmc_request *mrq)
462{
463 int sg_count;
464 struct mmc_data *data = mrq->data;
465
466 if (!data)
467 return -EINVAL;
468
469 sg_count = dma_map_sg(mmc_dev(host), data->sg,
470 data->sg_len,
471 (data->flags & MMC_DATA_WRITE) ?
472 DMA_TO_DEVICE : DMA_FROM_DEVICE);
473 if (!sg_count) {
474 pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
475 return -ENOMEM;
476 }
477
478 return sg_count;
479}
480
481static void cmdq_set_tran_desc(u8 *desc,
482 dma_addr_t addr, int len, bool end)
483{
484 __le64 *dataddr = (__le64 __force *)(desc + 4);
485 __le32 *attr = (__le32 __force *)desc;
486
487 *attr = (VALID(1) |
488 END(end ? 1 : 0) |
489 INT(0) |
490 ACT(0x4) |
491 DAT_LENGTH(len));
492
493 dataddr[0] = cpu_to_le64(addr);
494}
495
496static int cmdq_prep_tran_desc(struct mmc_request *mrq,
497 struct cmdq_host *cq_host, int tag)
498{
499 struct mmc_data *data = mrq->data;
500 int i, sg_count, len;
501 bool end = false;
502 dma_addr_t addr;
503 u8 *desc;
504 struct scatterlist *sg;
505
506 sg_count = cmdq_dma_map(mrq->host, mrq);
507 if (sg_count < 0) {
508 pr_err("%s: %s: unable to map sg lists, %d\n",
509 mmc_hostname(mrq->host), __func__, sg_count);
510 return sg_count;
511 }
512
513 desc = get_trans_desc(cq_host, tag);
514 memset(desc, 0, cq_host->trans_desc_len * cq_host->mmc->max_segs);
515
516 for_each_sg(data->sg, sg, sg_count, i) {
517 addr = sg_dma_address(sg);
518 len = sg_dma_len(sg);
519
520 if ((i+1) == sg_count)
521 end = true;
522 cmdq_set_tran_desc(desc, addr, len, end);
523 desc += cq_host->trans_desc_len;
524 }
525
526 pr_debug("%s: req: 0x%p tag: %d calc_trans_des: 0x%p sg-cnt: %d\n",
527 __func__, mrq->req, tag, desc, sg_count);
528
529 return 0;
530}
531
532static void cmdq_prep_dcmd_desc(struct mmc_host *mmc,
533 struct mmc_request *mrq)
534{
535 u64 *task_desc = NULL;
536 u64 data = 0;
537 u8 resp_type;
538 u8 *desc;
539 __le64 *dataddr;
540 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
541 u8 timing;
542
543 if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
544 resp_type = 0x0;
545 timing = 0x1;
546 } else {
Sahitya Tummala72bd8402015-05-29 13:27:38 +0530547 if (mrq->cmd->flags & MMC_RSP_BUSY) {
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700548 resp_type = 0x3;
549 timing = 0x0;
550 } else {
551 resp_type = 0x2;
552 timing = 0x1;
553 }
554 }
555
556 task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
557 memset(task_desc, 0, cq_host->task_desc_len);
558 data |= (VALID(1) |
559 END(1) |
560 INT(1) |
561 QBAR(1) |
562 ACT(0x5) |
563 CMD_INDEX(mrq->cmd->opcode) |
564 CMD_TIMING(timing) | RESP_TYPE(resp_type));
565 *task_desc |= data;
566 desc = (u8 *)task_desc;
567 pr_debug("cmdq: dcmd: cmd: %d timing: %d resp: %d\n",
568 mrq->cmd->opcode, timing, resp_type);
569 dataddr = (__le64 __force *)(desc + 4);
570 dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
571
572}
573
574static int cmdq_request(struct mmc_host *mmc, struct mmc_request *mrq)
575{
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300576 int err = 0;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700577 u64 data = 0;
578 u64 *task_desc = NULL;
579 u32 tag = mrq->cmdq_req->tag;
580 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
581
582 if (!cq_host->enabled) {
583 pr_err("%s: CMDQ host not enabled yet !!!\n",
584 mmc_hostname(mmc));
585 err = -EINVAL;
586 goto out;
587 }
588
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300589 cmdq_runtime_pm_get(cq_host);
590
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700591 if (mrq->cmdq_req->cmdq_req_flags & DCMD) {
592 cmdq_prep_dcmd_desc(mmc, mrq);
593 cq_host->mrq_slot[DCMD_SLOT] = mrq;
594 cmdq_writel(cq_host, 1 << DCMD_SLOT, CQTDBR);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300595 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700596 }
597
598 task_desc = (__le64 __force *)get_desc(cq_host, tag);
599
600 cmdq_prep_task_desc(mrq, &data, 1,
601 (mrq->cmdq_req->cmdq_req_flags & QBR));
602 *task_desc = cpu_to_le64(data);
603
604 err = cmdq_prep_tran_desc(mrq, cq_host, tag);
605 if (err) {
606 pr_err("%s: %s: failed to setup tx desc: %d\n",
607 mmc_hostname(mmc), __func__, err);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300608 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700609 }
610
611 BUG_ON(cmdq_readl(cq_host, CQTDBR) & (1 << tag));
612
613 cq_host->mrq_slot[tag] = mrq;
614 if (cq_host->ops->set_tranfer_params)
615 cq_host->ops->set_tranfer_params(mmc);
616
617 cmdq_writel(cq_host, 1 << tag, CQTDBR);
618
619out:
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300620 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700621 return err;
622}
623
624static void cmdq_finish_data(struct mmc_host *mmc, unsigned int tag)
625{
626 struct mmc_request *mrq;
627 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
628
Asutosh Das02e30862015-05-20 16:52:04 +0530629 mrq = get_req_by_tag(cq_host, tag);
Sahitya Tummala9549d562015-05-29 15:41:18 +0530630 if (tag == cq_host->dcmd_slot)
631 mrq->cmd->resp[0] = cmdq_readl(cq_host, CQCRDCT);
632
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700633 mrq->done(mrq);
634}
635
Asutosh Das02e30862015-05-20 16:52:04 +0530636irqreturn_t cmdq_irq(struct mmc_host *mmc, int err)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700637{
638 u32 status;
639 unsigned long tag = 0, comp_status;
640 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Asutosh Das02e30862015-05-20 16:52:04 +0530641 unsigned long err_info = 0;
642 struct mmc_request *mrq;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700643
644 status = cmdq_readl(cq_host, CQIS);
645 cmdq_writel(cq_host, status, CQIS);
646
Asutosh Das02e30862015-05-20 16:52:04 +0530647 if (!status && !err)
648 return IRQ_NONE;
649
650 if (err || (status & CQIS_RED)) {
651 err_info = cmdq_readl(cq_host, CQTERRI);
652 pr_err("%s: err: %d status: 0x%08x task-err-info (0x%08lx)\n",
653 mmc_hostname(mmc), err, status, err_info);
654
655 cmdq_dumpregs(cq_host);
656
657 if (err_info & CQ_RMEFV) {
658 tag = GET_CMD_ERR_TAG(err_info);
659 pr_err("%s: CMD err tag: %lu\n", __func__, tag);
660
661 mrq = get_req_by_tag(cq_host, tag);
662 /* CMD44/45/46/47 will not have a valid cmd */
663 if (mrq->cmd)
664 mrq->cmd->error = err;
665 else
666 mrq->data->error = err;
667 } else {
668 tag = GET_DAT_ERR_TAG(err_info);
669 pr_err("%s: Dat err tag: %lu\n", __func__, tag);
670 mrq = get_req_by_tag(cq_host, tag);
671 mrq->data->error = err;
672 }
673
674 tag = 0;
675 /*
676 * CQE detected a response error from device
677 * In most cases, this would require a reset.
678 */
679 if (status & CQIS_RED) {
Dov Levenglick2b678302015-07-01 14:24:20 +0300680 /*
681 * will check if the RED error is due to a bkops
682 * exception once the queue is empty
683 */
684 BUG_ON(!mmc->card);
685 if (mmc_card_configured_manual_bkops(mmc->card) &&
686 !mmc_card_configured_auto_bkops(mmc->card))
687 mmc->card->bkops.needs_check = true;
688
Asutosh Das02e30862015-05-20 16:52:04 +0530689 mrq->cmdq_req->resp_err = true;
690 pr_err("%s: Response error (0x%08x) from card !!!",
Dov Levenglick2b678302015-07-01 14:24:20 +0300691 mmc_hostname(mmc), status);
Asutosh Das02e30862015-05-20 16:52:04 +0530692 } else {
693 mrq->cmdq_req->resp_idx = cmdq_readl(cq_host, CQCRI);
694 mrq->cmdq_req->resp_arg = cmdq_readl(cq_host, CQCRA);
695 }
696
697 mmc->err_mrq = mrq;
698 cmdq_finish_data(mmc, tag);
699 }
700
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700701 if (status & CQIS_TCC) {
702 /* read QCTCN and complete the request */
703 comp_status = cmdq_readl(cq_host, CQTCN);
704 if (!comp_status)
705 goto out;
706
707 for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
708 /* complete the corresponding mrq */
709 pr_debug("%s: completing tag -> %lu\n",
710 mmc_hostname(mmc), tag);
711 cmdq_finish_data(mmc, tag);
712 }
713 cmdq_writel(cq_host, comp_status, CQTCN);
714 }
715
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530716 if (status & CQIS_HAC) {
Konstantin Dorfmanfa321072015-05-31 10:10:13 +0300717 if (cq_host->ops->post_cqe_halt)
718 cq_host->ops->post_cqe_halt(mmc);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530719 /* halt is completed, wakeup waiting thread */
720 complete(&cq_host->halt_comp);
721 }
722
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700723out:
724 return IRQ_HANDLED;
725}
726EXPORT_SYMBOL(cmdq_irq);
727
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530728/* May sleep */
729static int cmdq_halt(struct mmc_host *mmc, bool halt)
730{
731 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300732 u32 ret = 0;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530733
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300734 cmdq_runtime_pm_get(cq_host);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530735 if (halt) {
736 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT,
737 CQCTL);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300738 ret = wait_for_completion_timeout(&cq_host->halt_comp,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530739 msecs_to_jiffies(HALT_TIMEOUT_MS));
740 /* halt done: re-enable legacy interrupts */
741 if (cq_host->ops->clear_set_irqs)
742 cq_host->ops->clear_set_irqs(mmc, false);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300743 ret = ret ? 0 : -ETIMEDOUT;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530744 } else {
Asutosh Das3f730d12015-07-08 11:41:35 +0530745 if (cq_host->ops->set_data_timeout)
746 cq_host->ops->set_data_timeout(mmc, 0xf);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530747 if (cq_host->ops->clear_set_irqs)
748 cq_host->ops->clear_set_irqs(mmc, true);
749 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) & ~HALT,
750 CQCTL);
751 }
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300752 cmdq_runtime_pm_put(cq_host);
753 return ret;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530754}
755
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700756static void cmdq_post_req(struct mmc_host *host, struct mmc_request *mrq,
757 int err)
758{
759 struct mmc_data *data = mrq->data;
760
761 if (data) {
762 data->error = err;
763 dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len,
764 (data->flags & MMC_DATA_READ) ?
765 DMA_FROM_DEVICE : DMA_TO_DEVICE);
766 if (err)
767 data->bytes_xfered = 0;
768 else
769 data->bytes_xfered = blk_rq_bytes(mrq->req);
770 }
771}
772
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530773static void cmdq_dumpstate(struct mmc_host *mmc)
774{
775 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300776 cmdq_runtime_pm_get(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530777 cmdq_dumpregs(cq_host);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300778 cmdq_runtime_pm_put(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530779}
780
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700781static const struct mmc_cmdq_host_ops cmdq_host_ops = {
782 .enable = cmdq_enable,
783 .disable = cmdq_disable,
784 .request = cmdq_request,
785 .post_req = cmdq_post_req,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530786 .halt = cmdq_halt,
Asutosh Das02e30862015-05-20 16:52:04 +0530787 .reset = cmdq_reset,
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530788 .dumpstate = cmdq_dumpstate,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700789};
790
791struct cmdq_host *cmdq_pltfm_init(struct platform_device *pdev)
792{
793 struct cmdq_host *cq_host;
794 struct resource *cmdq_memres = NULL;
795
796 /* check and setup CMDQ interface */
797 cmdq_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
798 "cmdq_mem");
799 if (!cmdq_memres) {
800 dev_dbg(&pdev->dev, "CMDQ not supported\n");
801 return ERR_PTR(-EINVAL);
802 }
803
804 cq_host = kzalloc(sizeof(*cq_host), GFP_KERNEL);
805 if (!cq_host) {
806 dev_err(&pdev->dev, "failed to allocate memory for CMDQ\n");
807 return ERR_PTR(-ENOMEM);
808 }
809 cq_host->mmio = devm_ioremap(&pdev->dev,
810 cmdq_memres->start,
811 resource_size(cmdq_memres));
812 if (!cq_host->mmio) {
813 dev_err(&pdev->dev, "failed to remap cmdq regs\n");
814 kfree(cq_host);
815 return ERR_PTR(-EBUSY);
816 }
817 dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
818
819 return cq_host;
820}
821EXPORT_SYMBOL(cmdq_pltfm_init);
822
823int cmdq_init(struct cmdq_host *cq_host, struct mmc_host *mmc,
824 bool dma64)
825{
826 int err = 0;
827
828 cq_host->dma64 = dma64;
829 cq_host->mmc = mmc;
830 cq_host->mmc->cmdq_private = cq_host;
831
832 cq_host->num_slots = NUM_SLOTS;
833 cq_host->dcmd_slot = DCMD_SLOT;
834
835 mmc->cmdq_ops = &cmdq_host_ops;
836
837 cq_host->mrq_slot = kzalloc(sizeof(cq_host->mrq_slot) *
838 cq_host->num_slots, GFP_KERNEL);
839 if (!cq_host->mrq_slot)
840 return -ENOMEM;
841
842 init_completion(&cq_host->halt_comp);
843 return err;
844}
845EXPORT_SYMBOL(cmdq_init);