blob: dac6b3002fd2816e5c19d006b8fda94bd37a9e0b [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>
Gilad Broner44445992015-09-29 16:05:39 +030027#include <linux/workqueue.h>
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -070028
29#include "cmdq_hci.h"
Gilad Broner44445992015-09-29 16:05:39 +030030#include "sdhci.h"
31#include "sdhci-msm.h"
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -070032
33#define DCMD_SLOT 31
34#define NUM_SLOTS 32
35
Asutosh Dasaa1e1c72015-05-21 17:22:10 +053036/* 1 sec */
37#define HALT_TIMEOUT_MS 1000
38
Ritesh Harjanib0280c22015-10-27 11:51:25 +053039static int cmdq_halt_poll(struct mmc_host *mmc);
Ritesh Harjani6217c042015-10-01 20:34:42 +053040static int cmdq_halt(struct mmc_host *mmc, bool halt);
Ritesh Harjanib0280c22015-10-27 11:51:25 +053041
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +030042#ifdef CONFIG_PM_RUNTIME
43static int cmdq_runtime_pm_get(struct cmdq_host *host)
44{
45 return pm_runtime_get_sync(host->mmc->parent);
46}
47static int cmdq_runtime_pm_put(struct cmdq_host *host)
48{
49 pm_runtime_mark_last_busy(host->mmc->parent);
50 return pm_runtime_put_autosuspend(host->mmc->parent);
51}
52#else
53static inline int cmdq_runtime_pm_get(struct cmdq_host *host)
54{
55 return 0;
56}
57static inline int cmdq_runtime_pm_put(struct cmdq_host *host)
58{
59 return 0;
60}
61#endif
Asutosh Das02e30862015-05-20 16:52:04 +053062static inline struct mmc_request *get_req_by_tag(struct cmdq_host *cq_host,
63 unsigned int tag)
64{
65 return cq_host->mrq_slot[tag];
66}
67
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -070068static inline u8 *get_desc(struct cmdq_host *cq_host, u8 tag)
69{
70 return cq_host->desc_base + (tag * cq_host->slot_sz);
71}
72
73static inline u8 *get_link_desc(struct cmdq_host *cq_host, u8 tag)
74{
75 u8 *desc = get_desc(cq_host, tag);
76
77 return desc + cq_host->task_desc_len;
78}
79
80static inline dma_addr_t get_trans_desc_dma(struct cmdq_host *cq_host, u8 tag)
81{
82 return cq_host->trans_desc_dma_base +
83 (cq_host->mmc->max_segs * tag *
84 cq_host->trans_desc_len);
85}
86
87static inline u8 *get_trans_desc(struct cmdq_host *cq_host, u8 tag)
88{
89 return cq_host->trans_desc_base +
90 (cq_host->trans_desc_len * cq_host->mmc->max_segs * tag);
91}
92
93static void setup_trans_desc(struct cmdq_host *cq_host, u8 tag)
94{
95 u8 *link_temp;
96 dma_addr_t trans_temp;
97
98 link_temp = get_link_desc(cq_host, tag);
99 trans_temp = get_trans_desc_dma(cq_host, tag);
100
101 memset(link_temp, 0, cq_host->link_desc_len);
102 if (cq_host->link_desc_len > 8)
103 *(link_temp + 8) = 0;
104
105 if (tag == DCMD_SLOT) {
106 *link_temp = VALID(0) | ACT(0) | END(1);
107 return;
108 }
109
110 *link_temp = VALID(1) | ACT(0x6) | END(0);
111
112 if (cq_host->dma64) {
113 __le64 *data_addr = (__le64 __force *)(link_temp + 4);
114 data_addr[0] = cpu_to_le64(trans_temp);
115 } else {
116 __le32 *data_addr = (__le32 __force *)(link_temp + 4);
117 data_addr[0] = cpu_to_le32(trans_temp);
118 }
119}
120
Ritesh Harjanib0280c22015-10-27 11:51:25 +0530121static void cmdq_set_halt_irq(struct cmdq_host *cq_host, bool enable)
122{
123 u32 ier;
124
125 ier = cmdq_readl(cq_host, CQISTE);
126 if (enable) {
127 cmdq_writel(cq_host, ier | HALT, CQISTE);
128 cmdq_writel(cq_host, ier | HALT, CQISGE);
129 } else {
130 cmdq_writel(cq_host, ier & ~HALT, CQISTE);
131 cmdq_writel(cq_host, ier & ~HALT, CQISGE);
132 }
133}
134
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700135static void cmdq_clear_set_irqs(struct cmdq_host *cq_host, u32 clear, u32 set)
136{
137 u32 ier;
138
139 ier = cmdq_readl(cq_host, CQISTE);
140 ier &= ~clear;
141 ier |= set;
142 cmdq_writel(cq_host, ier, CQISTE);
143 cmdq_writel(cq_host, ier, CQISGE);
144 /* ensure the writes are done */
145 mb();
146}
147
148
149#define DRV_NAME "cmdq-host"
150
Venkat Gopalakrishnane77c64d2015-09-28 18:53:18 -0700151static void cmdq_dump_task_history(struct cmdq_host *cq_host)
152{
153 int i;
154
155 if (likely(!cq_host->mmc->cmdq_thist_enabled))
156 return;
157
158 if (!cq_host->thist) {
159 pr_err("%s: %s: CMDQ task history buffer not allocated\n",
160 mmc_hostname(cq_host->mmc), __func__);
161 return;
162 }
163
164 pr_err("---- Circular Task History ----\n");
165 pr_err(DRV_NAME ": Last entry index: %d", cq_host->thist_idx - 1);
166
167 for (i = 0; i < cq_host->num_slots; i++) {
168 pr_err(DRV_NAME ": [%02d]%s Task: 0x%08x | Args: 0x%08x\n", i,
169 (cq_host->thist[i].is_dcmd) ? "DCMD" : "DATA",
170 lower_32_bits(cq_host->thist[i].task),
171 upper_32_bits(cq_host->thist[i].task));
172 }
173 pr_err("-------------------------\n");
174}
175
Ritesh Harjani6217c042015-10-01 20:34:42 +0530176static void cmdq_dump_adma_mem(struct cmdq_host *cq_host)
177{
178 struct mmc_host *mmc = cq_host->mmc;
179 dma_addr_t desc_dma;
180 int tag = 0;
181 unsigned long data_active_reqs =
182 mmc->cmdq_ctx.data_active_reqs;
183 unsigned long desc_size =
184 (cq_host->mmc->max_segs * cq_host->trans_desc_len);
185
186 for_each_set_bit(tag, &data_active_reqs, cq_host->num_slots) {
187 desc_dma = get_trans_desc_dma(cq_host, tag);
188 pr_err("%s: %s: tag = %d, trans_dma(phys) = %pad, trans_desc(virt) = 0x%p\n",
189 mmc_hostname(mmc), __func__, tag,
190 &desc_dma, get_trans_desc(cq_host, tag));
191 print_hex_dump(KERN_ERR, "cmdq-adma:", DUMP_PREFIX_ADDRESS,
192 32, 8, get_trans_desc(cq_host, tag),
193 (desc_size), false);
194 }
195}
196
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700197static void cmdq_dumpregs(struct cmdq_host *cq_host)
198{
199 struct mmc_host *mmc = cq_host->mmc;
200
Asutosh Das02e30862015-05-20 16:52:04 +0530201 pr_err(DRV_NAME ": ========== REGISTER DUMP (%s)==========\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700202 mmc_hostname(mmc));
203
Asutosh Das02e30862015-05-20 16:52:04 +0530204 pr_err(DRV_NAME ": Caps: 0x%08x | Version: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700205 cmdq_readl(cq_host, CQCAP),
206 cmdq_readl(cq_host, CQVER));
Asutosh Das02e30862015-05-20 16:52:04 +0530207 pr_err(DRV_NAME ": Queing config: 0x%08x | Queue Ctrl: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700208 cmdq_readl(cq_host, CQCFG),
209 cmdq_readl(cq_host, CQCTL));
Asutosh Das02e30862015-05-20 16:52:04 +0530210 pr_err(DRV_NAME ": Int stat: 0x%08x | Int enab: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700211 cmdq_readl(cq_host, CQIS),
212 cmdq_readl(cq_host, CQISTE));
Asutosh Das02e30862015-05-20 16:52:04 +0530213 pr_err(DRV_NAME ": Int sig: 0x%08x | Int Coal: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700214 cmdq_readl(cq_host, CQISGE),
215 cmdq_readl(cq_host, CQIC));
Asutosh Das02e30862015-05-20 16:52:04 +0530216 pr_err(DRV_NAME ": TDL base: 0x%08x | TDL up32: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700217 cmdq_readl(cq_host, CQTDLBA),
218 cmdq_readl(cq_host, CQTDLBAU));
Asutosh Das02e30862015-05-20 16:52:04 +0530219 pr_err(DRV_NAME ": Doorbell: 0x%08x | Comp Notif: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700220 cmdq_readl(cq_host, CQTDBR),
221 cmdq_readl(cq_host, CQTCN));
Asutosh Das02e30862015-05-20 16:52:04 +0530222 pr_err(DRV_NAME ": Dev queue: 0x%08x | Dev Pend: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700223 cmdq_readl(cq_host, CQDQS),
224 cmdq_readl(cq_host, CQDPT));
Asutosh Das02e30862015-05-20 16:52:04 +0530225 pr_err(DRV_NAME ": Task clr: 0x%08x | Send stat 1: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700226 cmdq_readl(cq_host, CQTCLR),
227 cmdq_readl(cq_host, CQSSC1));
Asutosh Das02e30862015-05-20 16:52:04 +0530228 pr_err(DRV_NAME ": Send stat 2: 0x%08x | DCMD resp: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700229 cmdq_readl(cq_host, CQSSC2),
230 cmdq_readl(cq_host, CQCRDCT));
Asutosh Das02e30862015-05-20 16:52:04 +0530231 pr_err(DRV_NAME ": Resp err mask: 0x%08x | Task err: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700232 cmdq_readl(cq_host, CQRMEM),
233 cmdq_readl(cq_host, CQTERRI));
Asutosh Das02e30862015-05-20 16:52:04 +0530234 pr_err(DRV_NAME ": Resp idx 0x%08x | Resp arg: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700235 cmdq_readl(cq_host, CQCRI),
236 cmdq_readl(cq_host, CQCRA));
Asutosh Dasc0ed9c42015-05-29 15:39:37 +0530237 pr_err(DRV_NAME": Vendor cfg 0x%08x\n",
238 cmdq_readl(cq_host, CQ_VENDOR_CFG));
Asutosh Das02e30862015-05-20 16:52:04 +0530239 pr_err(DRV_NAME ": ===========================================\n");
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700240
Venkat Gopalakrishnane77c64d2015-09-28 18:53:18 -0700241 cmdq_dump_task_history(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700242 if (cq_host->ops->dump_vendor_regs)
243 cq_host->ops->dump_vendor_regs(mmc);
244}
245
246/**
247 * The allocated descriptor table for task, link & transfer descritors
248 * looks like:
249 * |----------|
250 * |task desc | |->|----------|
251 * |----------| | |trans desc|
252 * |link desc-|->| |----------|
253 * |----------| .
254 * . .
255 * no. of slots max-segs
256 * . |----------|
257 * |----------|
258 * The idea here is to create the [task+trans] table and mark & point the
259 * link desc to the transfer desc table on a per slot basis.
260 */
261static int cmdq_host_alloc_tdl(struct cmdq_host *cq_host)
262{
263
264 size_t desc_size;
265 size_t data_size;
266 int i = 0;
267
268 /* task descriptor can be 64/128 bit irrespective of arch */
269 if (cq_host->caps & CMDQ_TASK_DESC_SZ_128) {
270 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) |
271 CQ_TASK_DESC_SZ, CQCFG);
272 cq_host->task_desc_len = 16;
273 } else {
274 cq_host->task_desc_len = 8;
275 }
276
277 /*
278 * 96 bits length of transfer desc instead of 128 bits which means
279 * ADMA would expect next valid descriptor at the 96th bit
280 * or 128th bit
281 */
282 if (cq_host->dma64) {
283 if (cq_host->quirks & CMDQ_QUIRK_SHORT_TXFR_DESC_SZ)
284 cq_host->trans_desc_len = 12;
285 else
286 cq_host->trans_desc_len = 16;
287 cq_host->link_desc_len = 16;
288 } else {
289 cq_host->trans_desc_len = 8;
290 cq_host->link_desc_len = 8;
291 }
292
293 /* total size of a slot: 1 task & 1 transfer (link) */
294 cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len;
295
296 desc_size = cq_host->slot_sz * cq_host->num_slots;
297
298 data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs *
299 (cq_host->num_slots - 1);
300
301 pr_info("%s: desc_size: %d data_sz: %d slot-sz: %d\n", __func__,
302 (int)desc_size, (int)data_size, cq_host->slot_sz);
303
304 /*
305 * allocate a dma-mapped chunk of memory for the descriptors
306 * allocate a dma-mapped chunk of memory for link descriptors
307 * setup each link-desc memory offset per slot-number to
308 * the descriptor table.
309 */
310 cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
311 desc_size,
312 &cq_host->desc_dma_base,
313 GFP_KERNEL);
314 cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
315 data_size,
316 &cq_host->trans_desc_dma_base,
317 GFP_KERNEL);
Venkat Gopalakrishnane77c64d2015-09-28 18:53:18 -0700318 cq_host->thist = devm_kzalloc(mmc_dev(cq_host->mmc),
319 (sizeof(*cq_host->thist) *
320 cq_host->num_slots),
321 GFP_KERNEL);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700322 if (!cq_host->desc_base || !cq_host->trans_desc_base)
323 return -ENOMEM;
324
325 pr_info("desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n",
326 cq_host->desc_base, cq_host->trans_desc_base,
327 (unsigned long long)cq_host->desc_dma_base,
328 (unsigned long long) cq_host->trans_desc_dma_base);
329
330 for (; i < (cq_host->num_slots); i++)
331 setup_trans_desc(cq_host, i);
332
333 return 0;
334}
335
336static int cmdq_enable(struct mmc_host *mmc)
337{
338 int err = 0;
339 u32 cqcfg;
340 bool dcmd_enable;
341 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
342
343 if (!cq_host || !mmc->card || !mmc_card_cmdq(mmc->card)) {
344 err = -EINVAL;
345 goto out;
346 }
347
348 if (cq_host->enabled)
349 goto out;
350
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300351 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700352 cqcfg = cmdq_readl(cq_host, CQCFG);
353 if (cqcfg & 0x1) {
354 pr_info("%s: %s: cq_host is already enabled\n",
355 mmc_hostname(mmc), __func__);
356 WARN_ON(1);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700357 goto pm_ref_count;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700358 }
359
360 if (cq_host->quirks & CMDQ_QUIRK_NO_DCMD)
361 dcmd_enable = false;
362 else
363 dcmd_enable = true;
364
365 cqcfg = ((cq_host->caps & CMDQ_TASK_DESC_SZ_128 ? CQ_TASK_DESC_SZ : 0) |
366 (dcmd_enable ? CQ_DCMD : 0));
367
368 cmdq_writel(cq_host, cqcfg, CQCFG);
369 /* enable CQ_HOST */
370 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) | CQ_ENABLE,
371 CQCFG);
372
373 if (!cq_host->desc_base ||
374 !cq_host->trans_desc_base) {
375 err = cmdq_host_alloc_tdl(cq_host);
376 if (err)
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700377 goto pm_ref_count;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700378 }
379
Konstantin Dorfman14c902d2015-06-11 11:33:23 +0300380 cmdq_writel(cq_host, lower_32_bits(cq_host->desc_dma_base), CQTDLBA);
381 cmdq_writel(cq_host, upper_32_bits(cq_host->desc_dma_base), CQTDLBAU);
382
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700383 /*
384 * disable all vendor interrupts
385 * enable CMDQ interrupts
386 * enable the vendor error interrupts
387 */
388 if (cq_host->ops->clear_set_irqs)
389 cq_host->ops->clear_set_irqs(mmc, true);
390
391 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
392
393 /* cq_host would use this rca to address the card */
394 cmdq_writel(cq_host, mmc->card->rca, CQSSC2);
395
396 /* send QSR at lesser intervals than the default */
Asutosh Das5b81f132015-10-06 09:53:33 +0530397 cmdq_writel(cq_host, SEND_QSR_INTERVAL, CQSSC1);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700398
Dov Levenglick2b678302015-07-01 14:24:20 +0300399 /* enable bkops exception indication */
Dov Levenglickaea348b2015-07-20 11:59:52 +0300400 if (mmc_card_configured_manual_bkops(mmc->card) &&
401 !mmc_card_configured_auto_bkops(mmc->card))
Dov Levenglick2b678302015-07-01 14:24:20 +0300402 cmdq_writel(cq_host, cmdq_readl(cq_host, CQRMEM) | CQ_EXCEPTION,
403 CQRMEM);
404
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700405 /* ensure the writes are done before enabling CQE */
406 mb();
407
408 cq_host->enabled = true;
Ritesh Harjanib0280c22015-10-27 11:51:25 +0530409 mmc_host_clr_cq_disable(mmc);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700410
411 if (cq_host->ops->set_block_size)
412 cq_host->ops->set_block_size(cq_host->mmc);
413
414 if (cq_host->ops->set_data_timeout)
415 cq_host->ops->set_data_timeout(mmc, 0xf);
416
417 if (cq_host->ops->clear_set_dumpregs)
418 cq_host->ops->clear_set_dumpregs(mmc, 1);
419
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530420 if (cq_host->ops->enhanced_strobe_mask)
421 cq_host->ops->enhanced_strobe_mask(mmc, true);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700422
423pm_ref_count:
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300424 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700425out:
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700426 return err;
427}
428
429static void cmdq_disable(struct mmc_host *mmc, bool soft)
430{
431 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
432
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300433 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700434 if (soft) {
435 cmdq_writel(cq_host, cmdq_readl(
436 cq_host, CQCFG) & ~(CQ_ENABLE),
437 CQCFG);
438 }
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530439 if (cq_host->ops->enhanced_strobe_mask)
440 cq_host->ops->enhanced_strobe_mask(mmc, false);
441
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300442 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700443 cq_host->enabled = false;
Ritesh Harjanib0280c22015-10-27 11:51:25 +0530444 mmc_host_set_cq_disable(mmc);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700445}
446
Asutosh Das02e30862015-05-20 16:52:04 +0530447static void cmdq_reset(struct mmc_host *mmc, bool soft)
448{
449 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
450 unsigned int cqcfg;
451 unsigned int tdlba;
452 unsigned int tdlbau;
453 unsigned int rca;
454 int ret;
455
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300456 cmdq_runtime_pm_get(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530457 cqcfg = cmdq_readl(cq_host, CQCFG);
458 tdlba = cmdq_readl(cq_host, CQTDLBA);
459 tdlbau = cmdq_readl(cq_host, CQTDLBAU);
460 rca = cmdq_readl(cq_host, CQSSC2);
461
462 cmdq_disable(mmc, true);
463
464 if (cq_host->ops->reset) {
465 ret = cq_host->ops->reset(mmc);
466 if (ret) {
467 pr_crit("%s: reset CMDQ controller: failed\n",
468 mmc_hostname(mmc));
469 BUG();
470 }
471 }
472
473 cmdq_writel(cq_host, tdlba, CQTDLBA);
474 cmdq_writel(cq_host, tdlbau, CQTDLBAU);
475
476 if (cq_host->ops->clear_set_irqs)
477 cq_host->ops->clear_set_irqs(mmc, true);
478
479 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
480
481 /* cq_host would use this rca to address the card */
482 cmdq_writel(cq_host, rca, CQSSC2);
483
484 /* ensure the writes are done before enabling CQE */
485 mb();
486
487 cmdq_writel(cq_host, cqcfg, CQCFG);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300488 cmdq_runtime_pm_put(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530489 cq_host->enabled = true;
Ritesh Harjanib0280c22015-10-27 11:51:25 +0530490 mmc_host_clr_cq_disable(mmc);
Asutosh Das02e30862015-05-20 16:52:04 +0530491}
492
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700493static void cmdq_prep_task_desc(struct mmc_request *mrq,
494 u64 *data, bool intr, bool qbr)
495{
496 struct mmc_cmdq_req *cmdq_req = mrq->cmdq_req;
497 u32 req_flags = cmdq_req->cmdq_req_flags;
498
499 pr_debug("%s: %s: data-tag: 0x%08x - dir: %d - prio: %d - cnt: 0x%08x - addr: 0x%llx\n",
500 mmc_hostname(mrq->host), __func__,
501 !!(req_flags & DAT_TAG), !!(req_flags & DIR),
502 !!(req_flags & PRIO), cmdq_req->data.blocks,
503 (u64)mrq->cmdq_req->blk_addr);
504
505 *data = VALID(1) |
506 END(1) |
507 INT(intr) |
508 ACT(0x5) |
509 FORCED_PROG(!!(req_flags & FORCED_PRG)) |
510 CONTEXT(mrq->cmdq_req->ctx_id) |
511 DATA_TAG(!!(req_flags & DAT_TAG)) |
512 DATA_DIR(!!(req_flags & DIR)) |
513 PRIORITY(!!(req_flags & PRIO)) |
514 QBAR(qbr) |
515 REL_WRITE(!!(req_flags & REL_WR)) |
516 BLK_COUNT(mrq->cmdq_req->data.blocks) |
517 BLK_ADDR((u64)mrq->cmdq_req->blk_addr);
518}
519
520static int cmdq_dma_map(struct mmc_host *host, struct mmc_request *mrq)
521{
522 int sg_count;
523 struct mmc_data *data = mrq->data;
524
525 if (!data)
526 return -EINVAL;
527
528 sg_count = dma_map_sg(mmc_dev(host), data->sg,
529 data->sg_len,
530 (data->flags & MMC_DATA_WRITE) ?
531 DMA_TO_DEVICE : DMA_FROM_DEVICE);
532 if (!sg_count) {
533 pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
534 return -ENOMEM;
535 }
536
537 return sg_count;
538}
539
Sahitya Tummala78a68e52015-09-30 15:55:41 +0530540static void cmdq_set_tran_desc(u8 *desc, dma_addr_t addr, int len,
541 bool end, bool is_dma64)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700542{
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700543 __le32 *attr = (__le32 __force *)desc;
544
545 *attr = (VALID(1) |
546 END(end ? 1 : 0) |
547 INT(0) |
548 ACT(0x4) |
549 DAT_LENGTH(len));
550
Sahitya Tummala78a68e52015-09-30 15:55:41 +0530551 if (is_dma64) {
552 __le64 *dataddr = (__le64 __force *)(desc + 4);
553
554 dataddr[0] = cpu_to_le64(addr);
555 } else {
556 __le32 *dataddr = (__le32 __force *)(desc + 4);
557
558 dataddr[0] = cpu_to_le32(addr);
559 }
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700560}
561
562static int cmdq_prep_tran_desc(struct mmc_request *mrq,
563 struct cmdq_host *cq_host, int tag)
564{
565 struct mmc_data *data = mrq->data;
566 int i, sg_count, len;
567 bool end = false;
568 dma_addr_t addr;
569 u8 *desc;
570 struct scatterlist *sg;
571
572 sg_count = cmdq_dma_map(mrq->host, mrq);
573 if (sg_count < 0) {
574 pr_err("%s: %s: unable to map sg lists, %d\n",
575 mmc_hostname(mrq->host), __func__, sg_count);
576 return sg_count;
577 }
578
579 desc = get_trans_desc(cq_host, tag);
580 memset(desc, 0, cq_host->trans_desc_len * cq_host->mmc->max_segs);
581
582 for_each_sg(data->sg, sg, sg_count, i) {
583 addr = sg_dma_address(sg);
584 len = sg_dma_len(sg);
585
586 if ((i+1) == sg_count)
587 end = true;
Sahitya Tummala78a68e52015-09-30 15:55:41 +0530588 cmdq_set_tran_desc(desc, addr, len, end, cq_host->dma64);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700589 desc += cq_host->trans_desc_len;
590 }
591
592 pr_debug("%s: req: 0x%p tag: %d calc_trans_des: 0x%p sg-cnt: %d\n",
593 __func__, mrq->req, tag, desc, sg_count);
594
595 return 0;
596}
597
Venkat Gopalakrishnane77c64d2015-09-28 18:53:18 -0700598static void cmdq_log_task_desc_history(struct cmdq_host *cq_host, u64 task,
599 bool is_dcmd)
600{
601 if (likely(!cq_host->mmc->cmdq_thist_enabled))
602 return;
603
604 if (!cq_host->thist) {
605 pr_err("%s: %s: CMDQ task history buffer not allocated\n",
606 mmc_hostname(cq_host->mmc), __func__);
607 return;
608 }
609
610 if (cq_host->thist_idx >= cq_host->num_slots)
611 cq_host->thist_idx = 0;
612
613 cq_host->thist[cq_host->thist_idx].is_dcmd = is_dcmd;
614 memcpy(&cq_host->thist[cq_host->thist_idx++].task,
615 &task, cq_host->task_desc_len);
616}
617
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700618static void cmdq_prep_dcmd_desc(struct mmc_host *mmc,
619 struct mmc_request *mrq)
620{
621 u64 *task_desc = NULL;
622 u64 data = 0;
623 u8 resp_type;
624 u8 *desc;
625 __le64 *dataddr;
626 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
627 u8 timing;
628
629 if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
630 resp_type = 0x0;
631 timing = 0x1;
632 } else {
Sahitya Tummala72bd8402015-05-29 13:27:38 +0530633 if (mrq->cmd->flags & MMC_RSP_BUSY) {
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700634 resp_type = 0x3;
635 timing = 0x0;
636 } else {
637 resp_type = 0x2;
638 timing = 0x1;
639 }
640 }
641
642 task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
643 memset(task_desc, 0, cq_host->task_desc_len);
644 data |= (VALID(1) |
645 END(1) |
646 INT(1) |
647 QBAR(1) |
648 ACT(0x5) |
649 CMD_INDEX(mrq->cmd->opcode) |
650 CMD_TIMING(timing) | RESP_TYPE(resp_type));
651 *task_desc |= data;
652 desc = (u8 *)task_desc;
653 pr_debug("cmdq: dcmd: cmd: %d timing: %d resp: %d\n",
654 mrq->cmd->opcode, timing, resp_type);
655 dataddr = (__le64 __force *)(desc + 4);
656 dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
Venkat Gopalakrishnane77c64d2015-09-28 18:53:18 -0700657 cmdq_log_task_desc_history(cq_host, *task_desc, true);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700658}
659
Gilad Broner44445992015-09-29 16:05:39 +0300660static void cmdq_pm_qos_vote(struct sdhci_host *host, struct mmc_request *mrq)
661{
662 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
663 struct sdhci_msm_host *msm_host = pltfm_host->priv;
664
665 sdhci_msm_pm_qos_cpu_vote(host,
666 msm_host->pdata->pm_qos_data.cmdq_latency, mrq->req->cpu);
667}
668
669static void cmdq_pm_qos_unvote(struct sdhci_host *host, struct mmc_request *mrq)
670{
671 /* use async as we're inside an atomic context (soft-irq) */
672 sdhci_msm_pm_qos_cpu_unvote(host, mrq->req->cpu, true);
673}
674
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700675static int cmdq_request(struct mmc_host *mmc, struct mmc_request *mrq)
676{
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300677 int err = 0;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700678 u64 data = 0;
679 u64 *task_desc = NULL;
680 u32 tag = mrq->cmdq_req->tag;
681 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Gilad Broner44445992015-09-29 16:05:39 +0300682 struct sdhci_host *host = mmc_priv(mmc);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700683
684 if (!cq_host->enabled) {
685 pr_err("%s: CMDQ host not enabled yet !!!\n",
686 mmc_hostname(mmc));
687 err = -EINVAL;
688 goto out;
689 }
690
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300691 cmdq_runtime_pm_get(cq_host);
692
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700693 if (mrq->cmdq_req->cmdq_req_flags & DCMD) {
694 cmdq_prep_dcmd_desc(mmc, mrq);
695 cq_host->mrq_slot[DCMD_SLOT] = mrq;
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700696 /* DCMD's are always issued on a fixed slot */
697 tag = DCMD_SLOT;
698 goto ring_doorbell;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700699 }
700
701 task_desc = (__le64 __force *)get_desc(cq_host, tag);
702
703 cmdq_prep_task_desc(mrq, &data, 1,
704 (mrq->cmdq_req->cmdq_req_flags & QBR));
705 *task_desc = cpu_to_le64(data);
Venkat Gopalakrishnane77c64d2015-09-28 18:53:18 -0700706 cmdq_log_task_desc_history(cq_host, *task_desc, false);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700707
708 err = cmdq_prep_tran_desc(mrq, cq_host, tag);
709 if (err) {
710 pr_err("%s: %s: failed to setup tx desc: %d\n",
711 mmc_hostname(mmc), __func__, err);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300712 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700713 }
714
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700715 cq_host->mrq_slot[tag] = mrq;
716 if (cq_host->ops->set_tranfer_params)
717 cq_host->ops->set_tranfer_params(mmc);
718
Gilad Broner44445992015-09-29 16:05:39 +0300719 /* PM QoS */
720 sdhci_msm_pm_qos_irq_vote(host);
721 cmdq_pm_qos_vote(host, mrq);
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700722ring_doorbell:
723 /* Ensure the task descriptor list is flushed before ringing doorbell */
724 wmb();
Venkat Gopalakrishnan7d53e832015-10-01 14:34:10 -0700725 if (cmdq_readl(cq_host, CQTDBR) & (1 << tag)) {
726 cmdq_dumpregs(cq_host);
727 BUG_ON(1);
728 }
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700729 cmdq_writel(cq_host, 1 << tag, CQTDBR);
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700730 /* Commit the doorbell write immediately */
731 wmb();
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700732
733out:
734 return err;
735}
736
737static void cmdq_finish_data(struct mmc_host *mmc, unsigned int tag)
738{
739 struct mmc_request *mrq;
740 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
741
Asutosh Das02e30862015-05-20 16:52:04 +0530742 mrq = get_req_by_tag(cq_host, tag);
Sahitya Tummala9549d562015-05-29 15:41:18 +0530743 if (tag == cq_host->dcmd_slot)
744 mrq->cmd->resp[0] = cmdq_readl(cq_host, CQCRDCT);
745
Asutosh Dasc0ed9c42015-05-29 15:39:37 +0530746 if (mrq->cmdq_req->cmdq_req_flags & DCMD)
747 cmdq_writel(cq_host, cmdq_readl(cq_host, CQ_VENDOR_CFG) |
Sahitya Tummalab9ed5612015-10-05 16:20:10 +0530748 CMDQ_SEND_STATUS_TRIGGER, CQ_VENDOR_CFG);
Konstantin Dorfman27af9a92015-08-02 17:06:18 +0300749
750 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700751 mrq->done(mrq);
752}
753
Asutosh Das02e30862015-05-20 16:52:04 +0530754irqreturn_t cmdq_irq(struct mmc_host *mmc, int err)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700755{
756 u32 status;
757 unsigned long tag = 0, comp_status;
758 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Asutosh Das02e30862015-05-20 16:52:04 +0530759 unsigned long err_info = 0;
760 struct mmc_request *mrq;
Ritesh Harjanib0280c22015-10-27 11:51:25 +0530761 int ret;
Ritesh Harjani6217c042015-10-01 20:34:42 +0530762 u32 dbr_set = 0;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700763
764 status = cmdq_readl(cq_host, CQIS);
765 cmdq_writel(cq_host, status, CQIS);
766
Asutosh Das02e30862015-05-20 16:52:04 +0530767 if (!status && !err)
768 return IRQ_NONE;
769
770 if (err || (status & CQIS_RED)) {
771 err_info = cmdq_readl(cq_host, CQTERRI);
772 pr_err("%s: err: %d status: 0x%08x task-err-info (0x%08lx)\n",
773 mmc_hostname(mmc), err, status, err_info);
774
Ritesh Harjanib0280c22015-10-27 11:51:25 +0530775 /*
776 * Need to halt CQE in case of error in interrupt context itself
777 * otherwise CQE may proceed with sending CMD to device even if
778 * CQE/card is in error state.
779 * CMDQ error handling will make sure that it is unhalted after
780 * handling all the errors.
781 */
782 ret = cmdq_halt_poll(mmc);
783 if (ret)
784 pr_err("%s: %s: halt failed ret=%d\n",
785 mmc_hostname(mmc), __func__, ret);
Asutosh Das02e30862015-05-20 16:52:04 +0530786 cmdq_dumpregs(cq_host);
787
Ritesh Harjani6217c042015-10-01 20:34:42 +0530788 if (!err_info) {
789 /*
790 * It may so happen sometimes for few errors(like ADMA)
791 * that HW cannot give CQTERRI info.
792 * Thus below is a HW WA for recovering from such
793 * scenario.
794 * - To halt/disable CQE and do reset_all.
795 * Since there is no way to know which tag would
796 * have caused such error, so check for any first
797 * bit set in doorbell and proceed with an error.
798 */
799 dbr_set = cmdq_readl(cq_host, CQTDBR);
800 if (!dbr_set) {
801 pr_err("%s: spurious/force error interrupt\n",
802 mmc_hostname(mmc));
803 cmdq_halt(mmc, false);
804 mmc_host_clr_halt(mmc);
805 return IRQ_HANDLED;
806 }
807
808 tag = ffs(dbr_set) - 1;
809 pr_err("%s: error tag selected: tag = %lu\n",
810 mmc_hostname(mmc), tag);
811 mrq = get_req_by_tag(cq_host, tag);
812 if (mrq->data)
813 mrq->data->error = err;
814 else
815 mrq->cmd->error = err;
816 /*
817 * Get ADMA descriptor memory in case of ADMA
818 * error for debug.
819 */
820 if (err == -EIO)
821 cmdq_dump_adma_mem(cq_host);
822 goto skip_cqterri;
823 }
824
Asutosh Das02e30862015-05-20 16:52:04 +0530825 if (err_info & CQ_RMEFV) {
826 tag = GET_CMD_ERR_TAG(err_info);
827 pr_err("%s: CMD err tag: %lu\n", __func__, tag);
828
829 mrq = get_req_by_tag(cq_host, tag);
830 /* CMD44/45/46/47 will not have a valid cmd */
831 if (mrq->cmd)
832 mrq->cmd->error = err;
833 else
834 mrq->data->error = err;
835 } else {
836 tag = GET_DAT_ERR_TAG(err_info);
837 pr_err("%s: Dat err tag: %lu\n", __func__, tag);
838 mrq = get_req_by_tag(cq_host, tag);
839 mrq->data->error = err;
840 }
841
Ritesh Harjani6217c042015-10-01 20:34:42 +0530842skip_cqterri:
843 /*
844 * If CQE halt fails then, disable CQE
845 * from processing any further requests
846 */
847 if (ret)
848 cmdq_disable(mmc, true);
849
Asutosh Das02e30862015-05-20 16:52:04 +0530850 /*
851 * CQE detected a response error from device
852 * In most cases, this would require a reset.
853 */
854 if (status & CQIS_RED) {
Dov Levenglick2b678302015-07-01 14:24:20 +0300855 /*
856 * will check if the RED error is due to a bkops
857 * exception once the queue is empty
858 */
859 BUG_ON(!mmc->card);
860 if (mmc_card_configured_manual_bkops(mmc->card) &&
861 !mmc_card_configured_auto_bkops(mmc->card))
862 mmc->card->bkops.needs_check = true;
863
Asutosh Das02e30862015-05-20 16:52:04 +0530864 mrq->cmdq_req->resp_err = true;
865 pr_err("%s: Response error (0x%08x) from card !!!",
Dov Levenglick2b678302015-07-01 14:24:20 +0300866 mmc_hostname(mmc), status);
Asutosh Das02e30862015-05-20 16:52:04 +0530867 } else {
868 mrq->cmdq_req->resp_idx = cmdq_readl(cq_host, CQCRI);
869 mrq->cmdq_req->resp_arg = cmdq_readl(cq_host, CQCRA);
870 }
871
Asutosh Das02e30862015-05-20 16:52:04 +0530872 cmdq_finish_data(mmc, tag);
873 }
874
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700875 if (status & CQIS_TCC) {
Konstantin Dorfmanaf1713c2015-10-06 13:25:45 +0300876 /* read CQTCN and complete the request */
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700877 comp_status = cmdq_readl(cq_host, CQTCN);
878 if (!comp_status)
879 goto out;
Konstantin Dorfmanaf1713c2015-10-06 13:25:45 +0300880 /*
881 * The CQTCN must be cleared before notifying req completion
882 * to upper layers to avoid missing completion notification
883 * of new requests with the same tag.
884 */
885 cmdq_writel(cq_host, comp_status, CQTCN);
886 /*
887 * A write memory barrier is necessary to guarantee that CQTCN
888 * gets cleared first before next doorbell for the same tag is
889 * set but that is already achieved by the barrier present
890 * before setting doorbell, hence one is not needed here.
891 */
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700892 for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
893 /* complete the corresponding mrq */
894 pr_debug("%s: completing tag -> %lu\n",
895 mmc_hostname(mmc), tag);
896 cmdq_finish_data(mmc, tag);
897 }
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700898 }
899
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530900 if (status & CQIS_HAC) {
Konstantin Dorfmanfa321072015-05-31 10:10:13 +0300901 if (cq_host->ops->post_cqe_halt)
902 cq_host->ops->post_cqe_halt(mmc);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530903 /* halt is completed, wakeup waiting thread */
904 complete(&cq_host->halt_comp);
905 }
906
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700907out:
908 return IRQ_HANDLED;
909}
910EXPORT_SYMBOL(cmdq_irq);
911
Ritesh Harjanib0280c22015-10-27 11:51:25 +0530912/* cmdq_halt_poll - Halting CQE using polling method.
913 * @mmc: struct mmc_host
914 * This is used mainly from interrupt context to halt
915 * CQE engine.
916 */
917static int cmdq_halt_poll(struct mmc_host *mmc)
918{
919 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
920 int retries = 100;
921
922 cmdq_set_halt_irq(cq_host, false);
923 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT, CQCTL);
924 while (retries) {
925 if (!(cmdq_readl(cq_host, CQCTL) & HALT)) {
926 udelay(5);
927 retries--;
928 continue;
929 } else {
930 if (cq_host->ops->post_cqe_halt)
931 cq_host->ops->post_cqe_halt(mmc);
932 /* halt done: re-enable legacy interrupts */
933 if (cq_host->ops->clear_set_irqs)
934 cq_host->ops->clear_set_irqs(mmc,
935 false);
936 mmc_host_set_halt(mmc);
937 break;
938 }
939 }
940 cmdq_set_halt_irq(cq_host, true);
941 return retries ? 0 : -ETIMEDOUT;
942}
943
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530944/* May sleep */
945static int cmdq_halt(struct mmc_host *mmc, bool halt)
946{
947 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300948 u32 ret = 0;
Ritesh Harjani442c60a2015-09-15 19:21:32 +0530949 int retries = 3;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530950
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300951 cmdq_runtime_pm_get(cq_host);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530952 if (halt) {
Ritesh Harjani442c60a2015-09-15 19:21:32 +0530953 while (retries) {
954 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT,
955 CQCTL);
956 ret = wait_for_completion_timeout(&cq_host->halt_comp,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530957 msecs_to_jiffies(HALT_TIMEOUT_MS));
Ritesh Harjani442c60a2015-09-15 19:21:32 +0530958 if (!ret && !(cmdq_readl(cq_host, CQCTL) & HALT)) {
959 retries--;
960 continue;
961 } else {
962 /* halt done: re-enable legacy interrupts */
963 if (cq_host->ops->clear_set_irqs)
964 cq_host->ops->clear_set_irqs(mmc,
965 false);
966 break;
967 }
968 }
Subhash Jadavani6a718e12015-10-19 17:25:22 -0700969 ret = retries ? 0 : -ETIMEDOUT;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530970 } else {
Asutosh Das3f730d12015-07-08 11:41:35 +0530971 if (cq_host->ops->set_data_timeout)
972 cq_host->ops->set_data_timeout(mmc, 0xf);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530973 if (cq_host->ops->clear_set_irqs)
974 cq_host->ops->clear_set_irqs(mmc, true);
975 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) & ~HALT,
976 CQCTL);
977 }
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300978 cmdq_runtime_pm_put(cq_host);
979 return ret;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530980}
981
Ritesh Harjani121cdec2015-09-27 21:51:01 +0530982static void cmdq_post_req(struct mmc_host *mmc, int tag, int err)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700983{
Ritesh Harjani121cdec2015-09-27 21:51:01 +0530984 struct cmdq_host *cq_host;
985 struct mmc_request *mrq;
986 struct mmc_data *data;
987 struct sdhci_host *sdhci_host = mmc_priv(mmc);
988
989 if (WARN_ON(!mmc))
990 return;
991
992 cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
993 mrq = get_req_by_tag(cq_host, tag);
994 data = mrq->data;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700995
996 if (data) {
997 data->error = err;
Ritesh Harjani121cdec2015-09-27 21:51:01 +0530998 dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700999 (data->flags & MMC_DATA_READ) ?
1000 DMA_FROM_DEVICE : DMA_TO_DEVICE);
1001 if (err)
1002 data->bytes_xfered = 0;
1003 else
1004 data->bytes_xfered = blk_rq_bytes(mrq->req);
Gilad Broner44445992015-09-29 16:05:39 +03001005
1006 /* we're in atomic context (soft-irq) so unvote async. */
1007 sdhci_msm_pm_qos_irq_unvote(sdhci_host, true);
1008 cmdq_pm_qos_unvote(sdhci_host, mrq);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -07001009 }
1010}
1011
Asutosh Dasfa8836b2015-03-02 23:14:05 +05301012static void cmdq_dumpstate(struct mmc_host *mmc)
1013{
1014 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +03001015 cmdq_runtime_pm_get(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05301016 cmdq_dumpregs(cq_host);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +03001017 cmdq_runtime_pm_put(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +05301018}
1019
Gilad Broner44445992015-09-29 16:05:39 +03001020static int cmdq_late_init(struct mmc_host *mmc)
1021{
1022 struct sdhci_host *host = mmc_priv(mmc);
1023 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1024 struct sdhci_msm_host *msm_host = pltfm_host->priv;
1025
1026 /*
1027 * TODO: This should basically move to something like "sdhci-cmdq-msm"
1028 * for msm specific implementation.
1029 */
1030 sdhci_msm_pm_qos_irq_init(host);
1031
1032 if (msm_host->pdata->pm_qos_data.cmdq_valid)
1033 sdhci_msm_pm_qos_cpu_init(host,
1034 msm_host->pdata->pm_qos_data.cmdq_latency);
1035 return 0;
1036}
1037
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -07001038static const struct mmc_cmdq_host_ops cmdq_host_ops = {
Gilad Broner44445992015-09-29 16:05:39 +03001039 .init = cmdq_late_init,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -07001040 .enable = cmdq_enable,
1041 .disable = cmdq_disable,
1042 .request = cmdq_request,
1043 .post_req = cmdq_post_req,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +05301044 .halt = cmdq_halt,
Asutosh Das02e30862015-05-20 16:52:04 +05301045 .reset = cmdq_reset,
Asutosh Dasfa8836b2015-03-02 23:14:05 +05301046 .dumpstate = cmdq_dumpstate,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -07001047};
1048
1049struct cmdq_host *cmdq_pltfm_init(struct platform_device *pdev)
1050{
1051 struct cmdq_host *cq_host;
1052 struct resource *cmdq_memres = NULL;
1053
1054 /* check and setup CMDQ interface */
1055 cmdq_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1056 "cmdq_mem");
1057 if (!cmdq_memres) {
1058 dev_dbg(&pdev->dev, "CMDQ not supported\n");
1059 return ERR_PTR(-EINVAL);
1060 }
1061
1062 cq_host = kzalloc(sizeof(*cq_host), GFP_KERNEL);
1063 if (!cq_host) {
1064 dev_err(&pdev->dev, "failed to allocate memory for CMDQ\n");
1065 return ERR_PTR(-ENOMEM);
1066 }
1067 cq_host->mmio = devm_ioremap(&pdev->dev,
1068 cmdq_memres->start,
1069 resource_size(cmdq_memres));
1070 if (!cq_host->mmio) {
1071 dev_err(&pdev->dev, "failed to remap cmdq regs\n");
1072 kfree(cq_host);
1073 return ERR_PTR(-EBUSY);
1074 }
1075 dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
1076
1077 return cq_host;
1078}
1079EXPORT_SYMBOL(cmdq_pltfm_init);
1080
1081int cmdq_init(struct cmdq_host *cq_host, struct mmc_host *mmc,
1082 bool dma64)
1083{
1084 int err = 0;
1085
1086 cq_host->dma64 = dma64;
1087 cq_host->mmc = mmc;
1088 cq_host->mmc->cmdq_private = cq_host;
1089
1090 cq_host->num_slots = NUM_SLOTS;
1091 cq_host->dcmd_slot = DCMD_SLOT;
1092
1093 mmc->cmdq_ops = &cmdq_host_ops;
Ritesh Harjanib0280c22015-10-27 11:51:25 +05301094 mmc->num_cq_slots = NUM_SLOTS;
1095 mmc->dcmd_cq_slot = DCMD_SLOT;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -07001096
1097 cq_host->mrq_slot = kzalloc(sizeof(cq_host->mrq_slot) *
1098 cq_host->num_slots, GFP_KERNEL);
1099 if (!cq_host->mrq_slot)
1100 return -ENOMEM;
1101
1102 init_completion(&cq_host->halt_comp);
1103 return err;
1104}
1105EXPORT_SYMBOL(cmdq_init);