blob: 73bb2236882c21e83f9389dd47ecf400a5764225 [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 Dasc0ed9c42015-05-29 15:39:37 +0530188 pr_err(DRV_NAME": Vendor cfg 0x%08x\n",
189 cmdq_readl(cq_host, CQ_VENDOR_CFG));
Asutosh Das02e30862015-05-20 16:52:04 +0530190 pr_err(DRV_NAME ": ===========================================\n");
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700191
192 cmdq_dump_debug_ram(cq_host);
193 if (cq_host->ops->dump_vendor_regs)
194 cq_host->ops->dump_vendor_regs(mmc);
195}
196
197/**
198 * The allocated descriptor table for task, link & transfer descritors
199 * looks like:
200 * |----------|
201 * |task desc | |->|----------|
202 * |----------| | |trans desc|
203 * |link desc-|->| |----------|
204 * |----------| .
205 * . .
206 * no. of slots max-segs
207 * . |----------|
208 * |----------|
209 * The idea here is to create the [task+trans] table and mark & point the
210 * link desc to the transfer desc table on a per slot basis.
211 */
212static int cmdq_host_alloc_tdl(struct cmdq_host *cq_host)
213{
214
215 size_t desc_size;
216 size_t data_size;
217 int i = 0;
218
219 /* task descriptor can be 64/128 bit irrespective of arch */
220 if (cq_host->caps & CMDQ_TASK_DESC_SZ_128) {
221 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) |
222 CQ_TASK_DESC_SZ, CQCFG);
223 cq_host->task_desc_len = 16;
224 } else {
225 cq_host->task_desc_len = 8;
226 }
227
228 /*
229 * 96 bits length of transfer desc instead of 128 bits which means
230 * ADMA would expect next valid descriptor at the 96th bit
231 * or 128th bit
232 */
233 if (cq_host->dma64) {
234 if (cq_host->quirks & CMDQ_QUIRK_SHORT_TXFR_DESC_SZ)
235 cq_host->trans_desc_len = 12;
236 else
237 cq_host->trans_desc_len = 16;
238 cq_host->link_desc_len = 16;
239 } else {
240 cq_host->trans_desc_len = 8;
241 cq_host->link_desc_len = 8;
242 }
243
244 /* total size of a slot: 1 task & 1 transfer (link) */
245 cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len;
246
247 desc_size = cq_host->slot_sz * cq_host->num_slots;
248
249 data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs *
250 (cq_host->num_slots - 1);
251
252 pr_info("%s: desc_size: %d data_sz: %d slot-sz: %d\n", __func__,
253 (int)desc_size, (int)data_size, cq_host->slot_sz);
254
255 /*
256 * allocate a dma-mapped chunk of memory for the descriptors
257 * allocate a dma-mapped chunk of memory for link descriptors
258 * setup each link-desc memory offset per slot-number to
259 * the descriptor table.
260 */
261 cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
262 desc_size,
263 &cq_host->desc_dma_base,
264 GFP_KERNEL);
265 cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
266 data_size,
267 &cq_host->trans_desc_dma_base,
268 GFP_KERNEL);
269 if (!cq_host->desc_base || !cq_host->trans_desc_base)
270 return -ENOMEM;
271
272 pr_info("desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n",
273 cq_host->desc_base, cq_host->trans_desc_base,
274 (unsigned long long)cq_host->desc_dma_base,
275 (unsigned long long) cq_host->trans_desc_dma_base);
276
277 for (; i < (cq_host->num_slots); i++)
278 setup_trans_desc(cq_host, i);
279
280 return 0;
281}
282
283static int cmdq_enable(struct mmc_host *mmc)
284{
285 int err = 0;
286 u32 cqcfg;
287 bool dcmd_enable;
288 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
289
290 if (!cq_host || !mmc->card || !mmc_card_cmdq(mmc->card)) {
291 err = -EINVAL;
292 goto out;
293 }
294
295 if (cq_host->enabled)
296 goto out;
297
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300298 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700299 cqcfg = cmdq_readl(cq_host, CQCFG);
300 if (cqcfg & 0x1) {
301 pr_info("%s: %s: cq_host is already enabled\n",
302 mmc_hostname(mmc), __func__);
303 WARN_ON(1);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700304 goto pm_ref_count;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700305 }
306
307 if (cq_host->quirks & CMDQ_QUIRK_NO_DCMD)
308 dcmd_enable = false;
309 else
310 dcmd_enable = true;
311
312 cqcfg = ((cq_host->caps & CMDQ_TASK_DESC_SZ_128 ? CQ_TASK_DESC_SZ : 0) |
313 (dcmd_enable ? CQ_DCMD : 0));
314
315 cmdq_writel(cq_host, cqcfg, CQCFG);
316 /* enable CQ_HOST */
317 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) | CQ_ENABLE,
318 CQCFG);
319
320 if (!cq_host->desc_base ||
321 !cq_host->trans_desc_base) {
322 err = cmdq_host_alloc_tdl(cq_host);
323 if (err)
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700324 goto pm_ref_count;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700325 }
326
Konstantin Dorfman14c902d2015-06-11 11:33:23 +0300327 cmdq_writel(cq_host, lower_32_bits(cq_host->desc_dma_base), CQTDLBA);
328 cmdq_writel(cq_host, upper_32_bits(cq_host->desc_dma_base), CQTDLBAU);
329
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700330 /*
331 * disable all vendor interrupts
332 * enable CMDQ interrupts
333 * enable the vendor error interrupts
334 */
335 if (cq_host->ops->clear_set_irqs)
336 cq_host->ops->clear_set_irqs(mmc, true);
337
338 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
339
340 /* cq_host would use this rca to address the card */
341 cmdq_writel(cq_host, mmc->card->rca, CQSSC2);
342
343 /* send QSR at lesser intervals than the default */
344 cmdq_writel(cq_host, cmdq_readl(cq_host, CQSSC1) | SEND_QSR_INTERVAL,
345 CQSSC1);
346
Dov Levenglick2b678302015-07-01 14:24:20 +0300347 /* enable bkops exception indication */
Dov Levenglickaea348b2015-07-20 11:59:52 +0300348 if (mmc_card_configured_manual_bkops(mmc->card) &&
349 !mmc_card_configured_auto_bkops(mmc->card))
Dov Levenglick2b678302015-07-01 14:24:20 +0300350 cmdq_writel(cq_host, cmdq_readl(cq_host, CQRMEM) | CQ_EXCEPTION,
351 CQRMEM);
352
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700353 /* ensure the writes are done before enabling CQE */
354 mb();
355
356 cq_host->enabled = true;
357
358 if (cq_host->ops->set_block_size)
359 cq_host->ops->set_block_size(cq_host->mmc);
360
361 if (cq_host->ops->set_data_timeout)
362 cq_host->ops->set_data_timeout(mmc, 0xf);
363
364 if (cq_host->ops->clear_set_dumpregs)
365 cq_host->ops->clear_set_dumpregs(mmc, 1);
366
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530367 if (cq_host->ops->enhanced_strobe_mask)
368 cq_host->ops->enhanced_strobe_mask(mmc, true);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700369
370pm_ref_count:
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300371 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700372out:
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700373 return err;
374}
375
376static void cmdq_disable(struct mmc_host *mmc, bool soft)
377{
378 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
379
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300380 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700381 if (soft) {
382 cmdq_writel(cq_host, cmdq_readl(
383 cq_host, CQCFG) & ~(CQ_ENABLE),
384 CQCFG);
385 }
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530386 if (cq_host->ops->enhanced_strobe_mask)
387 cq_host->ops->enhanced_strobe_mask(mmc, false);
388
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300389 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700390 cq_host->enabled = false;
391}
392
Asutosh Das02e30862015-05-20 16:52:04 +0530393static void cmdq_reset(struct mmc_host *mmc, bool soft)
394{
395 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
396 unsigned int cqcfg;
397 unsigned int tdlba;
398 unsigned int tdlbau;
399 unsigned int rca;
400 int ret;
401
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300402 cmdq_runtime_pm_get(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530403 cqcfg = cmdq_readl(cq_host, CQCFG);
404 tdlba = cmdq_readl(cq_host, CQTDLBA);
405 tdlbau = cmdq_readl(cq_host, CQTDLBAU);
406 rca = cmdq_readl(cq_host, CQSSC2);
407
408 cmdq_disable(mmc, true);
409
410 if (cq_host->ops->reset) {
411 ret = cq_host->ops->reset(mmc);
412 if (ret) {
413 pr_crit("%s: reset CMDQ controller: failed\n",
414 mmc_hostname(mmc));
415 BUG();
416 }
417 }
418
419 cmdq_writel(cq_host, tdlba, CQTDLBA);
420 cmdq_writel(cq_host, tdlbau, CQTDLBAU);
421
422 if (cq_host->ops->clear_set_irqs)
423 cq_host->ops->clear_set_irqs(mmc, true);
424
425 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
426
427 /* cq_host would use this rca to address the card */
428 cmdq_writel(cq_host, rca, CQSSC2);
429
430 /* ensure the writes are done before enabling CQE */
431 mb();
432
433 cmdq_writel(cq_host, cqcfg, CQCFG);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300434 cmdq_runtime_pm_put(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530435 cq_host->enabled = true;
436}
437
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700438static void cmdq_prep_task_desc(struct mmc_request *mrq,
439 u64 *data, bool intr, bool qbr)
440{
441 struct mmc_cmdq_req *cmdq_req = mrq->cmdq_req;
442 u32 req_flags = cmdq_req->cmdq_req_flags;
443
444 pr_debug("%s: %s: data-tag: 0x%08x - dir: %d - prio: %d - cnt: 0x%08x - addr: 0x%llx\n",
445 mmc_hostname(mrq->host), __func__,
446 !!(req_flags & DAT_TAG), !!(req_flags & DIR),
447 !!(req_flags & PRIO), cmdq_req->data.blocks,
448 (u64)mrq->cmdq_req->blk_addr);
449
450 *data = VALID(1) |
451 END(1) |
452 INT(intr) |
453 ACT(0x5) |
454 FORCED_PROG(!!(req_flags & FORCED_PRG)) |
455 CONTEXT(mrq->cmdq_req->ctx_id) |
456 DATA_TAG(!!(req_flags & DAT_TAG)) |
457 DATA_DIR(!!(req_flags & DIR)) |
458 PRIORITY(!!(req_flags & PRIO)) |
459 QBAR(qbr) |
460 REL_WRITE(!!(req_flags & REL_WR)) |
461 BLK_COUNT(mrq->cmdq_req->data.blocks) |
462 BLK_ADDR((u64)mrq->cmdq_req->blk_addr);
463}
464
465static int cmdq_dma_map(struct mmc_host *host, struct mmc_request *mrq)
466{
467 int sg_count;
468 struct mmc_data *data = mrq->data;
469
470 if (!data)
471 return -EINVAL;
472
473 sg_count = dma_map_sg(mmc_dev(host), data->sg,
474 data->sg_len,
475 (data->flags & MMC_DATA_WRITE) ?
476 DMA_TO_DEVICE : DMA_FROM_DEVICE);
477 if (!sg_count) {
478 pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
479 return -ENOMEM;
480 }
481
482 return sg_count;
483}
484
485static void cmdq_set_tran_desc(u8 *desc,
486 dma_addr_t addr, int len, bool end)
487{
488 __le64 *dataddr = (__le64 __force *)(desc + 4);
489 __le32 *attr = (__le32 __force *)desc;
490
491 *attr = (VALID(1) |
492 END(end ? 1 : 0) |
493 INT(0) |
494 ACT(0x4) |
495 DAT_LENGTH(len));
496
497 dataddr[0] = cpu_to_le64(addr);
498}
499
500static int cmdq_prep_tran_desc(struct mmc_request *mrq,
501 struct cmdq_host *cq_host, int tag)
502{
503 struct mmc_data *data = mrq->data;
504 int i, sg_count, len;
505 bool end = false;
506 dma_addr_t addr;
507 u8 *desc;
508 struct scatterlist *sg;
509
510 sg_count = cmdq_dma_map(mrq->host, mrq);
511 if (sg_count < 0) {
512 pr_err("%s: %s: unable to map sg lists, %d\n",
513 mmc_hostname(mrq->host), __func__, sg_count);
514 return sg_count;
515 }
516
517 desc = get_trans_desc(cq_host, tag);
518 memset(desc, 0, cq_host->trans_desc_len * cq_host->mmc->max_segs);
519
520 for_each_sg(data->sg, sg, sg_count, i) {
521 addr = sg_dma_address(sg);
522 len = sg_dma_len(sg);
523
524 if ((i+1) == sg_count)
525 end = true;
526 cmdq_set_tran_desc(desc, addr, len, end);
527 desc += cq_host->trans_desc_len;
528 }
529
530 pr_debug("%s: req: 0x%p tag: %d calc_trans_des: 0x%p sg-cnt: %d\n",
531 __func__, mrq->req, tag, desc, sg_count);
532
533 return 0;
534}
535
536static void cmdq_prep_dcmd_desc(struct mmc_host *mmc,
537 struct mmc_request *mrq)
538{
539 u64 *task_desc = NULL;
540 u64 data = 0;
541 u8 resp_type;
542 u8 *desc;
543 __le64 *dataddr;
544 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
545 u8 timing;
546
547 if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
548 resp_type = 0x0;
549 timing = 0x1;
550 } else {
Sahitya Tummala72bd8402015-05-29 13:27:38 +0530551 if (mrq->cmd->flags & MMC_RSP_BUSY) {
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700552 resp_type = 0x3;
553 timing = 0x0;
554 } else {
555 resp_type = 0x2;
556 timing = 0x1;
557 }
558 }
559
560 task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
561 memset(task_desc, 0, cq_host->task_desc_len);
562 data |= (VALID(1) |
563 END(1) |
564 INT(1) |
565 QBAR(1) |
566 ACT(0x5) |
567 CMD_INDEX(mrq->cmd->opcode) |
568 CMD_TIMING(timing) | RESP_TYPE(resp_type));
569 *task_desc |= data;
570 desc = (u8 *)task_desc;
571 pr_debug("cmdq: dcmd: cmd: %d timing: %d resp: %d\n",
572 mrq->cmd->opcode, timing, resp_type);
573 dataddr = (__le64 __force *)(desc + 4);
574 dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
575
576}
577
578static int cmdq_request(struct mmc_host *mmc, struct mmc_request *mrq)
579{
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300580 int err = 0;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700581 u64 data = 0;
582 u64 *task_desc = NULL;
583 u32 tag = mrq->cmdq_req->tag;
584 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
585
586 if (!cq_host->enabled) {
587 pr_err("%s: CMDQ host not enabled yet !!!\n",
588 mmc_hostname(mmc));
589 err = -EINVAL;
590 goto out;
591 }
592
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300593 cmdq_runtime_pm_get(cq_host);
594
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700595 if (mrq->cmdq_req->cmdq_req_flags & DCMD) {
596 cmdq_prep_dcmd_desc(mmc, mrq);
597 cq_host->mrq_slot[DCMD_SLOT] = mrq;
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700598 /* DCMD's are always issued on a fixed slot */
599 tag = DCMD_SLOT;
600 goto ring_doorbell;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700601 }
602
603 task_desc = (__le64 __force *)get_desc(cq_host, tag);
604
605 cmdq_prep_task_desc(mrq, &data, 1,
606 (mrq->cmdq_req->cmdq_req_flags & QBR));
607 *task_desc = cpu_to_le64(data);
608
609 err = cmdq_prep_tran_desc(mrq, cq_host, tag);
610 if (err) {
611 pr_err("%s: %s: failed to setup tx desc: %d\n",
612 mmc_hostname(mmc), __func__, err);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300613 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700614 }
615
616 BUG_ON(cmdq_readl(cq_host, CQTDBR) & (1 << tag));
617
618 cq_host->mrq_slot[tag] = mrq;
619 if (cq_host->ops->set_tranfer_params)
620 cq_host->ops->set_tranfer_params(mmc);
621
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700622ring_doorbell:
623 /* Ensure the task descriptor list is flushed before ringing doorbell */
624 wmb();
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700625 cmdq_writel(cq_host, 1 << tag, CQTDBR);
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700626 /* Commit the doorbell write immediately */
627 wmb();
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700628
629out:
630 return err;
631}
632
633static void cmdq_finish_data(struct mmc_host *mmc, unsigned int tag)
634{
635 struct mmc_request *mrq;
636 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
637
Asutosh Das02e30862015-05-20 16:52:04 +0530638 mrq = get_req_by_tag(cq_host, tag);
Sahitya Tummala9549d562015-05-29 15:41:18 +0530639 if (tag == cq_host->dcmd_slot)
640 mrq->cmd->resp[0] = cmdq_readl(cq_host, CQCRDCT);
641
Asutosh Dasc0ed9c42015-05-29 15:39:37 +0530642 if (mrq->cmdq_req->cmdq_req_flags & DCMD)
643 cmdq_writel(cq_host, cmdq_readl(cq_host, CQ_VENDOR_CFG) |
644 CMDQ_SEND_STATUS_TRIGGER, CQCTL);
Konstantin Dorfman27af9a92015-08-02 17:06:18 +0300645
646 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700647 mrq->done(mrq);
648}
649
Asutosh Das02e30862015-05-20 16:52:04 +0530650irqreturn_t cmdq_irq(struct mmc_host *mmc, int err)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700651{
652 u32 status;
653 unsigned long tag = 0, comp_status;
654 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Asutosh Das02e30862015-05-20 16:52:04 +0530655 unsigned long err_info = 0;
656 struct mmc_request *mrq;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700657
658 status = cmdq_readl(cq_host, CQIS);
659 cmdq_writel(cq_host, status, CQIS);
660
Asutosh Das02e30862015-05-20 16:52:04 +0530661 if (!status && !err)
662 return IRQ_NONE;
663
664 if (err || (status & CQIS_RED)) {
665 err_info = cmdq_readl(cq_host, CQTERRI);
666 pr_err("%s: err: %d status: 0x%08x task-err-info (0x%08lx)\n",
667 mmc_hostname(mmc), err, status, err_info);
668
669 cmdq_dumpregs(cq_host);
670
671 if (err_info & CQ_RMEFV) {
672 tag = GET_CMD_ERR_TAG(err_info);
673 pr_err("%s: CMD err tag: %lu\n", __func__, tag);
674
675 mrq = get_req_by_tag(cq_host, tag);
676 /* CMD44/45/46/47 will not have a valid cmd */
677 if (mrq->cmd)
678 mrq->cmd->error = err;
679 else
680 mrq->data->error = err;
681 } else {
682 tag = GET_DAT_ERR_TAG(err_info);
683 pr_err("%s: Dat err tag: %lu\n", __func__, tag);
684 mrq = get_req_by_tag(cq_host, tag);
685 mrq->data->error = err;
686 }
687
688 tag = 0;
689 /*
690 * CQE detected a response error from device
691 * In most cases, this would require a reset.
692 */
693 if (status & CQIS_RED) {
Dov Levenglick2b678302015-07-01 14:24:20 +0300694 /*
695 * will check if the RED error is due to a bkops
696 * exception once the queue is empty
697 */
698 BUG_ON(!mmc->card);
699 if (mmc_card_configured_manual_bkops(mmc->card) &&
700 !mmc_card_configured_auto_bkops(mmc->card))
701 mmc->card->bkops.needs_check = true;
702
Asutosh Das02e30862015-05-20 16:52:04 +0530703 mrq->cmdq_req->resp_err = true;
704 pr_err("%s: Response error (0x%08x) from card !!!",
Dov Levenglick2b678302015-07-01 14:24:20 +0300705 mmc_hostname(mmc), status);
Asutosh Das02e30862015-05-20 16:52:04 +0530706 } else {
707 mrq->cmdq_req->resp_idx = cmdq_readl(cq_host, CQCRI);
708 mrq->cmdq_req->resp_arg = cmdq_readl(cq_host, CQCRA);
709 }
710
711 mmc->err_mrq = mrq;
712 cmdq_finish_data(mmc, tag);
713 }
714
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700715 if (status & CQIS_TCC) {
716 /* read QCTCN and complete the request */
717 comp_status = cmdq_readl(cq_host, CQTCN);
718 if (!comp_status)
719 goto out;
720
721 for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
722 /* complete the corresponding mrq */
723 pr_debug("%s: completing tag -> %lu\n",
724 mmc_hostname(mmc), tag);
725 cmdq_finish_data(mmc, tag);
726 }
727 cmdq_writel(cq_host, comp_status, CQTCN);
728 }
729
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530730 if (status & CQIS_HAC) {
Konstantin Dorfmanfa321072015-05-31 10:10:13 +0300731 if (cq_host->ops->post_cqe_halt)
732 cq_host->ops->post_cqe_halt(mmc);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530733 /* halt is completed, wakeup waiting thread */
734 complete(&cq_host->halt_comp);
735 }
736
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700737out:
738 return IRQ_HANDLED;
739}
740EXPORT_SYMBOL(cmdq_irq);
741
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530742/* May sleep */
743static int cmdq_halt(struct mmc_host *mmc, bool halt)
744{
745 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300746 u32 ret = 0;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530747
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300748 cmdq_runtime_pm_get(cq_host);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530749 if (halt) {
750 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT,
751 CQCTL);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300752 ret = wait_for_completion_timeout(&cq_host->halt_comp,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530753 msecs_to_jiffies(HALT_TIMEOUT_MS));
754 /* halt done: re-enable legacy interrupts */
755 if (cq_host->ops->clear_set_irqs)
756 cq_host->ops->clear_set_irqs(mmc, false);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300757 ret = ret ? 0 : -ETIMEDOUT;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530758 } else {
Asutosh Das3f730d12015-07-08 11:41:35 +0530759 if (cq_host->ops->set_data_timeout)
760 cq_host->ops->set_data_timeout(mmc, 0xf);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530761 if (cq_host->ops->clear_set_irqs)
762 cq_host->ops->clear_set_irqs(mmc, true);
763 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) & ~HALT,
764 CQCTL);
765 }
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300766 cmdq_runtime_pm_put(cq_host);
767 return ret;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530768}
769
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700770static void cmdq_post_req(struct mmc_host *host, struct mmc_request *mrq,
771 int err)
772{
773 struct mmc_data *data = mrq->data;
774
775 if (data) {
776 data->error = err;
777 dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len,
778 (data->flags & MMC_DATA_READ) ?
779 DMA_FROM_DEVICE : DMA_TO_DEVICE);
780 if (err)
781 data->bytes_xfered = 0;
782 else
783 data->bytes_xfered = blk_rq_bytes(mrq->req);
784 }
785}
786
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530787static void cmdq_dumpstate(struct mmc_host *mmc)
788{
789 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300790 cmdq_runtime_pm_get(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530791 cmdq_dumpregs(cq_host);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300792 cmdq_runtime_pm_put(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530793}
794
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700795static const struct mmc_cmdq_host_ops cmdq_host_ops = {
796 .enable = cmdq_enable,
797 .disable = cmdq_disable,
798 .request = cmdq_request,
799 .post_req = cmdq_post_req,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530800 .halt = cmdq_halt,
Asutosh Das02e30862015-05-20 16:52:04 +0530801 .reset = cmdq_reset,
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530802 .dumpstate = cmdq_dumpstate,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700803};
804
805struct cmdq_host *cmdq_pltfm_init(struct platform_device *pdev)
806{
807 struct cmdq_host *cq_host;
808 struct resource *cmdq_memres = NULL;
809
810 /* check and setup CMDQ interface */
811 cmdq_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
812 "cmdq_mem");
813 if (!cmdq_memres) {
814 dev_dbg(&pdev->dev, "CMDQ not supported\n");
815 return ERR_PTR(-EINVAL);
816 }
817
818 cq_host = kzalloc(sizeof(*cq_host), GFP_KERNEL);
819 if (!cq_host) {
820 dev_err(&pdev->dev, "failed to allocate memory for CMDQ\n");
821 return ERR_PTR(-ENOMEM);
822 }
823 cq_host->mmio = devm_ioremap(&pdev->dev,
824 cmdq_memres->start,
825 resource_size(cmdq_memres));
826 if (!cq_host->mmio) {
827 dev_err(&pdev->dev, "failed to remap cmdq regs\n");
828 kfree(cq_host);
829 return ERR_PTR(-EBUSY);
830 }
831 dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
832
833 return cq_host;
834}
835EXPORT_SYMBOL(cmdq_pltfm_init);
836
837int cmdq_init(struct cmdq_host *cq_host, struct mmc_host *mmc,
838 bool dma64)
839{
840 int err = 0;
841
842 cq_host->dma64 = dma64;
843 cq_host->mmc = mmc;
844 cq_host->mmc->cmdq_private = cq_host;
845
846 cq_host->num_slots = NUM_SLOTS;
847 cq_host->dcmd_slot = DCMD_SLOT;
848
849 mmc->cmdq_ops = &cmdq_host_ops;
850
851 cq_host->mrq_slot = kzalloc(sizeof(cq_host->mrq_slot) *
852 cq_host->num_slots, GFP_KERNEL);
853 if (!cq_host->mrq_slot)
854 return -ENOMEM;
855
856 init_completion(&cq_host->halt_comp);
857 return err;
858}
859EXPORT_SYMBOL(cmdq_init);