blob: cbc0e5700e79a6e459efb967c94b8e536e2d47f9 [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);
304 goto out;
305 }
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)
324 goto out;
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 Gopalakrishnan0225ff92015-05-29 17:25:46 -0700369out:
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300370 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700371 return err;
372}
373
374static void cmdq_disable(struct mmc_host *mmc, bool soft)
375{
376 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
377
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300378 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700379 if (soft) {
380 cmdq_writel(cq_host, cmdq_readl(
381 cq_host, CQCFG) & ~(CQ_ENABLE),
382 CQCFG);
383 }
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530384 if (cq_host->ops->enhanced_strobe_mask)
385 cq_host->ops->enhanced_strobe_mask(mmc, false);
386
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300387 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700388 cq_host->enabled = false;
389}
390
Asutosh Das02e30862015-05-20 16:52:04 +0530391static void cmdq_reset(struct mmc_host *mmc, bool soft)
392{
393 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
394 unsigned int cqcfg;
395 unsigned int tdlba;
396 unsigned int tdlbau;
397 unsigned int rca;
398 int ret;
399
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300400 cmdq_runtime_pm_get(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530401 cqcfg = cmdq_readl(cq_host, CQCFG);
402 tdlba = cmdq_readl(cq_host, CQTDLBA);
403 tdlbau = cmdq_readl(cq_host, CQTDLBAU);
404 rca = cmdq_readl(cq_host, CQSSC2);
405
406 cmdq_disable(mmc, true);
407
408 if (cq_host->ops->reset) {
409 ret = cq_host->ops->reset(mmc);
410 if (ret) {
411 pr_crit("%s: reset CMDQ controller: failed\n",
412 mmc_hostname(mmc));
413 BUG();
414 }
415 }
416
417 cmdq_writel(cq_host, tdlba, CQTDLBA);
418 cmdq_writel(cq_host, tdlbau, CQTDLBAU);
419
420 if (cq_host->ops->clear_set_irqs)
421 cq_host->ops->clear_set_irqs(mmc, true);
422
423 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
424
425 /* cq_host would use this rca to address the card */
426 cmdq_writel(cq_host, rca, CQSSC2);
427
428 /* ensure the writes are done before enabling CQE */
429 mb();
430
431 cmdq_writel(cq_host, cqcfg, CQCFG);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300432 cmdq_runtime_pm_put(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530433 cq_host->enabled = true;
434}
435
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700436static void cmdq_prep_task_desc(struct mmc_request *mrq,
437 u64 *data, bool intr, bool qbr)
438{
439 struct mmc_cmdq_req *cmdq_req = mrq->cmdq_req;
440 u32 req_flags = cmdq_req->cmdq_req_flags;
441
442 pr_debug("%s: %s: data-tag: 0x%08x - dir: %d - prio: %d - cnt: 0x%08x - addr: 0x%llx\n",
443 mmc_hostname(mrq->host), __func__,
444 !!(req_flags & DAT_TAG), !!(req_flags & DIR),
445 !!(req_flags & PRIO), cmdq_req->data.blocks,
446 (u64)mrq->cmdq_req->blk_addr);
447
448 *data = VALID(1) |
449 END(1) |
450 INT(intr) |
451 ACT(0x5) |
452 FORCED_PROG(!!(req_flags & FORCED_PRG)) |
453 CONTEXT(mrq->cmdq_req->ctx_id) |
454 DATA_TAG(!!(req_flags & DAT_TAG)) |
455 DATA_DIR(!!(req_flags & DIR)) |
456 PRIORITY(!!(req_flags & PRIO)) |
457 QBAR(qbr) |
458 REL_WRITE(!!(req_flags & REL_WR)) |
459 BLK_COUNT(mrq->cmdq_req->data.blocks) |
460 BLK_ADDR((u64)mrq->cmdq_req->blk_addr);
461}
462
463static int cmdq_dma_map(struct mmc_host *host, struct mmc_request *mrq)
464{
465 int sg_count;
466 struct mmc_data *data = mrq->data;
467
468 if (!data)
469 return -EINVAL;
470
471 sg_count = dma_map_sg(mmc_dev(host), data->sg,
472 data->sg_len,
473 (data->flags & MMC_DATA_WRITE) ?
474 DMA_TO_DEVICE : DMA_FROM_DEVICE);
475 if (!sg_count) {
476 pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
477 return -ENOMEM;
478 }
479
480 return sg_count;
481}
482
483static void cmdq_set_tran_desc(u8 *desc,
484 dma_addr_t addr, int len, bool end)
485{
486 __le64 *dataddr = (__le64 __force *)(desc + 4);
487 __le32 *attr = (__le32 __force *)desc;
488
489 *attr = (VALID(1) |
490 END(end ? 1 : 0) |
491 INT(0) |
492 ACT(0x4) |
493 DAT_LENGTH(len));
494
495 dataddr[0] = cpu_to_le64(addr);
496}
497
498static int cmdq_prep_tran_desc(struct mmc_request *mrq,
499 struct cmdq_host *cq_host, int tag)
500{
501 struct mmc_data *data = mrq->data;
502 int i, sg_count, len;
503 bool end = false;
504 dma_addr_t addr;
505 u8 *desc;
506 struct scatterlist *sg;
507
508 sg_count = cmdq_dma_map(mrq->host, mrq);
509 if (sg_count < 0) {
510 pr_err("%s: %s: unable to map sg lists, %d\n",
511 mmc_hostname(mrq->host), __func__, sg_count);
512 return sg_count;
513 }
514
515 desc = get_trans_desc(cq_host, tag);
516 memset(desc, 0, cq_host->trans_desc_len * cq_host->mmc->max_segs);
517
518 for_each_sg(data->sg, sg, sg_count, i) {
519 addr = sg_dma_address(sg);
520 len = sg_dma_len(sg);
521
522 if ((i+1) == sg_count)
523 end = true;
524 cmdq_set_tran_desc(desc, addr, len, end);
525 desc += cq_host->trans_desc_len;
526 }
527
528 pr_debug("%s: req: 0x%p tag: %d calc_trans_des: 0x%p sg-cnt: %d\n",
529 __func__, mrq->req, tag, desc, sg_count);
530
531 return 0;
532}
533
534static void cmdq_prep_dcmd_desc(struct mmc_host *mmc,
535 struct mmc_request *mrq)
536{
537 u64 *task_desc = NULL;
538 u64 data = 0;
539 u8 resp_type;
540 u8 *desc;
541 __le64 *dataddr;
542 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
543 u8 timing;
544
545 if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
546 resp_type = 0x0;
547 timing = 0x1;
548 } else {
Sahitya Tummala72bd8402015-05-29 13:27:38 +0530549 if (mrq->cmd->flags & MMC_RSP_BUSY) {
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700550 resp_type = 0x3;
551 timing = 0x0;
552 } else {
553 resp_type = 0x2;
554 timing = 0x1;
555 }
556 }
557
558 task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
559 memset(task_desc, 0, cq_host->task_desc_len);
560 data |= (VALID(1) |
561 END(1) |
562 INT(1) |
563 QBAR(1) |
564 ACT(0x5) |
565 CMD_INDEX(mrq->cmd->opcode) |
566 CMD_TIMING(timing) | RESP_TYPE(resp_type));
567 *task_desc |= data;
568 desc = (u8 *)task_desc;
569 pr_debug("cmdq: dcmd: cmd: %d timing: %d resp: %d\n",
570 mrq->cmd->opcode, timing, resp_type);
571 dataddr = (__le64 __force *)(desc + 4);
572 dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
573
574}
575
576static int cmdq_request(struct mmc_host *mmc, struct mmc_request *mrq)
577{
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300578 int err = 0;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700579 u64 data = 0;
580 u64 *task_desc = NULL;
581 u32 tag = mrq->cmdq_req->tag;
582 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
583
584 if (!cq_host->enabled) {
585 pr_err("%s: CMDQ host not enabled yet !!!\n",
586 mmc_hostname(mmc));
587 err = -EINVAL;
588 goto out;
589 }
590
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300591 cmdq_runtime_pm_get(cq_host);
592
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700593 if (mrq->cmdq_req->cmdq_req_flags & DCMD) {
594 cmdq_prep_dcmd_desc(mmc, mrq);
595 cq_host->mrq_slot[DCMD_SLOT] = mrq;
596 cmdq_writel(cq_host, 1 << DCMD_SLOT, CQTDBR);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300597 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700598 }
599
600 task_desc = (__le64 __force *)get_desc(cq_host, tag);
601
602 cmdq_prep_task_desc(mrq, &data, 1,
603 (mrq->cmdq_req->cmdq_req_flags & QBR));
604 *task_desc = cpu_to_le64(data);
605
606 err = cmdq_prep_tran_desc(mrq, cq_host, tag);
607 if (err) {
608 pr_err("%s: %s: failed to setup tx desc: %d\n",
609 mmc_hostname(mmc), __func__, err);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300610 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700611 }
612
613 BUG_ON(cmdq_readl(cq_host, CQTDBR) & (1 << tag));
614
615 cq_host->mrq_slot[tag] = mrq;
616 if (cq_host->ops->set_tranfer_params)
617 cq_host->ops->set_tranfer_params(mmc);
618
619 cmdq_writel(cq_host, 1 << tag, CQTDBR);
620
621out:
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300622 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700623 return err;
624}
625
626static void cmdq_finish_data(struct mmc_host *mmc, unsigned int tag)
627{
628 struct mmc_request *mrq;
629 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
630
Asutosh Das02e30862015-05-20 16:52:04 +0530631 mrq = get_req_by_tag(cq_host, tag);
Sahitya Tummala9549d562015-05-29 15:41:18 +0530632 if (tag == cq_host->dcmd_slot)
633 mrq->cmd->resp[0] = cmdq_readl(cq_host, CQCRDCT);
634
Asutosh Dasc0ed9c42015-05-29 15:39:37 +0530635 if (mrq->cmdq_req->cmdq_req_flags & DCMD)
636 cmdq_writel(cq_host, cmdq_readl(cq_host, CQ_VENDOR_CFG) |
637 CMDQ_SEND_STATUS_TRIGGER, CQCTL);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700638 mrq->done(mrq);
639}
640
Asutosh Das02e30862015-05-20 16:52:04 +0530641irqreturn_t cmdq_irq(struct mmc_host *mmc, int err)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700642{
643 u32 status;
644 unsigned long tag = 0, comp_status;
645 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Asutosh Das02e30862015-05-20 16:52:04 +0530646 unsigned long err_info = 0;
647 struct mmc_request *mrq;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700648
649 status = cmdq_readl(cq_host, CQIS);
650 cmdq_writel(cq_host, status, CQIS);
651
Asutosh Das02e30862015-05-20 16:52:04 +0530652 if (!status && !err)
653 return IRQ_NONE;
654
655 if (err || (status & CQIS_RED)) {
656 err_info = cmdq_readl(cq_host, CQTERRI);
657 pr_err("%s: err: %d status: 0x%08x task-err-info (0x%08lx)\n",
658 mmc_hostname(mmc), err, status, err_info);
659
660 cmdq_dumpregs(cq_host);
661
662 if (err_info & CQ_RMEFV) {
663 tag = GET_CMD_ERR_TAG(err_info);
664 pr_err("%s: CMD err tag: %lu\n", __func__, tag);
665
666 mrq = get_req_by_tag(cq_host, tag);
667 /* CMD44/45/46/47 will not have a valid cmd */
668 if (mrq->cmd)
669 mrq->cmd->error = err;
670 else
671 mrq->data->error = err;
672 } else {
673 tag = GET_DAT_ERR_TAG(err_info);
674 pr_err("%s: Dat err tag: %lu\n", __func__, tag);
675 mrq = get_req_by_tag(cq_host, tag);
676 mrq->data->error = err;
677 }
678
679 tag = 0;
680 /*
681 * CQE detected a response error from device
682 * In most cases, this would require a reset.
683 */
684 if (status & CQIS_RED) {
Dov Levenglick2b678302015-07-01 14:24:20 +0300685 /*
686 * will check if the RED error is due to a bkops
687 * exception once the queue is empty
688 */
689 BUG_ON(!mmc->card);
690 if (mmc_card_configured_manual_bkops(mmc->card) &&
691 !mmc_card_configured_auto_bkops(mmc->card))
692 mmc->card->bkops.needs_check = true;
693
Asutosh Das02e30862015-05-20 16:52:04 +0530694 mrq->cmdq_req->resp_err = true;
695 pr_err("%s: Response error (0x%08x) from card !!!",
Dov Levenglick2b678302015-07-01 14:24:20 +0300696 mmc_hostname(mmc), status);
Asutosh Das02e30862015-05-20 16:52:04 +0530697 } else {
698 mrq->cmdq_req->resp_idx = cmdq_readl(cq_host, CQCRI);
699 mrq->cmdq_req->resp_arg = cmdq_readl(cq_host, CQCRA);
700 }
701
702 mmc->err_mrq = mrq;
703 cmdq_finish_data(mmc, tag);
704 }
705
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700706 if (status & CQIS_TCC) {
707 /* read QCTCN and complete the request */
708 comp_status = cmdq_readl(cq_host, CQTCN);
709 if (!comp_status)
710 goto out;
711
712 for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
713 /* complete the corresponding mrq */
714 pr_debug("%s: completing tag -> %lu\n",
715 mmc_hostname(mmc), tag);
716 cmdq_finish_data(mmc, tag);
717 }
718 cmdq_writel(cq_host, comp_status, CQTCN);
719 }
720
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530721 if (status & CQIS_HAC) {
Konstantin Dorfmanfa321072015-05-31 10:10:13 +0300722 if (cq_host->ops->post_cqe_halt)
723 cq_host->ops->post_cqe_halt(mmc);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530724 /* halt is completed, wakeup waiting thread */
725 complete(&cq_host->halt_comp);
726 }
727
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700728out:
729 return IRQ_HANDLED;
730}
731EXPORT_SYMBOL(cmdq_irq);
732
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530733/* May sleep */
734static int cmdq_halt(struct mmc_host *mmc, bool halt)
735{
736 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300737 u32 ret = 0;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530738
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300739 cmdq_runtime_pm_get(cq_host);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530740 if (halt) {
741 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT,
742 CQCTL);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300743 ret = wait_for_completion_timeout(&cq_host->halt_comp,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530744 msecs_to_jiffies(HALT_TIMEOUT_MS));
745 /* halt done: re-enable legacy interrupts */
746 if (cq_host->ops->clear_set_irqs)
747 cq_host->ops->clear_set_irqs(mmc, false);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300748 ret = ret ? 0 : -ETIMEDOUT;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530749 } else {
Asutosh Das3f730d12015-07-08 11:41:35 +0530750 if (cq_host->ops->set_data_timeout)
751 cq_host->ops->set_data_timeout(mmc, 0xf);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530752 if (cq_host->ops->clear_set_irqs)
753 cq_host->ops->clear_set_irqs(mmc, true);
754 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) & ~HALT,
755 CQCTL);
756 }
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300757 cmdq_runtime_pm_put(cq_host);
758 return ret;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530759}
760
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700761static void cmdq_post_req(struct mmc_host *host, struct mmc_request *mrq,
762 int err)
763{
764 struct mmc_data *data = mrq->data;
765
766 if (data) {
767 data->error = err;
768 dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len,
769 (data->flags & MMC_DATA_READ) ?
770 DMA_FROM_DEVICE : DMA_TO_DEVICE);
771 if (err)
772 data->bytes_xfered = 0;
773 else
774 data->bytes_xfered = blk_rq_bytes(mrq->req);
775 }
776}
777
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530778static void cmdq_dumpstate(struct mmc_host *mmc)
779{
780 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300781 cmdq_runtime_pm_get(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530782 cmdq_dumpregs(cq_host);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300783 cmdq_runtime_pm_put(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530784}
785
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700786static const struct mmc_cmdq_host_ops cmdq_host_ops = {
787 .enable = cmdq_enable,
788 .disable = cmdq_disable,
789 .request = cmdq_request,
790 .post_req = cmdq_post_req,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530791 .halt = cmdq_halt,
Asutosh Das02e30862015-05-20 16:52:04 +0530792 .reset = cmdq_reset,
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530793 .dumpstate = cmdq_dumpstate,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700794};
795
796struct cmdq_host *cmdq_pltfm_init(struct platform_device *pdev)
797{
798 struct cmdq_host *cq_host;
799 struct resource *cmdq_memres = NULL;
800
801 /* check and setup CMDQ interface */
802 cmdq_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
803 "cmdq_mem");
804 if (!cmdq_memres) {
805 dev_dbg(&pdev->dev, "CMDQ not supported\n");
806 return ERR_PTR(-EINVAL);
807 }
808
809 cq_host = kzalloc(sizeof(*cq_host), GFP_KERNEL);
810 if (!cq_host) {
811 dev_err(&pdev->dev, "failed to allocate memory for CMDQ\n");
812 return ERR_PTR(-ENOMEM);
813 }
814 cq_host->mmio = devm_ioremap(&pdev->dev,
815 cmdq_memres->start,
816 resource_size(cmdq_memres));
817 if (!cq_host->mmio) {
818 dev_err(&pdev->dev, "failed to remap cmdq regs\n");
819 kfree(cq_host);
820 return ERR_PTR(-EBUSY);
821 }
822 dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
823
824 return cq_host;
825}
826EXPORT_SYMBOL(cmdq_pltfm_init);
827
828int cmdq_init(struct cmdq_host *cq_host, struct mmc_host *mmc,
829 bool dma64)
830{
831 int err = 0;
832
833 cq_host->dma64 = dma64;
834 cq_host->mmc = mmc;
835 cq_host->mmc->cmdq_private = cq_host;
836
837 cq_host->num_slots = NUM_SLOTS;
838 cq_host->dcmd_slot = DCMD_SLOT;
839
840 mmc->cmdq_ops = &cmdq_host_ops;
841
842 cq_host->mrq_slot = kzalloc(sizeof(cq_host->mrq_slot) *
843 cq_host->num_slots, GFP_KERNEL);
844 if (!cq_host->mrq_slot)
845 return -ENOMEM;
846
847 init_completion(&cq_host->halt_comp);
848 return err;
849}
850EXPORT_SYMBOL(cmdq_init);