blob: aeb18d269b7c1938300e6d74ab6e7210f604d4ab [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
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +030039#ifdef CONFIG_PM_RUNTIME
40static int cmdq_runtime_pm_get(struct cmdq_host *host)
41{
42 return pm_runtime_get_sync(host->mmc->parent);
43}
44static int cmdq_runtime_pm_put(struct cmdq_host *host)
45{
46 pm_runtime_mark_last_busy(host->mmc->parent);
47 return pm_runtime_put_autosuspend(host->mmc->parent);
48}
49#else
50static inline int cmdq_runtime_pm_get(struct cmdq_host *host)
51{
52 return 0;
53}
54static inline int cmdq_runtime_pm_put(struct cmdq_host *host)
55{
56 return 0;
57}
58#endif
Asutosh Das02e30862015-05-20 16:52:04 +053059static inline struct mmc_request *get_req_by_tag(struct cmdq_host *cq_host,
60 unsigned int tag)
61{
62 return cq_host->mrq_slot[tag];
63}
64
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -070065static inline u8 *get_desc(struct cmdq_host *cq_host, u8 tag)
66{
67 return cq_host->desc_base + (tag * cq_host->slot_sz);
68}
69
70static inline u8 *get_link_desc(struct cmdq_host *cq_host, u8 tag)
71{
72 u8 *desc = get_desc(cq_host, tag);
73
74 return desc + cq_host->task_desc_len;
75}
76
77static inline dma_addr_t get_trans_desc_dma(struct cmdq_host *cq_host, u8 tag)
78{
79 return cq_host->trans_desc_dma_base +
80 (cq_host->mmc->max_segs * tag *
81 cq_host->trans_desc_len);
82}
83
84static inline u8 *get_trans_desc(struct cmdq_host *cq_host, u8 tag)
85{
86 return cq_host->trans_desc_base +
87 (cq_host->trans_desc_len * cq_host->mmc->max_segs * tag);
88}
89
90static void setup_trans_desc(struct cmdq_host *cq_host, u8 tag)
91{
92 u8 *link_temp;
93 dma_addr_t trans_temp;
94
95 link_temp = get_link_desc(cq_host, tag);
96 trans_temp = get_trans_desc_dma(cq_host, tag);
97
98 memset(link_temp, 0, cq_host->link_desc_len);
99 if (cq_host->link_desc_len > 8)
100 *(link_temp + 8) = 0;
101
102 if (tag == DCMD_SLOT) {
103 *link_temp = VALID(0) | ACT(0) | END(1);
104 return;
105 }
106
107 *link_temp = VALID(1) | ACT(0x6) | END(0);
108
109 if (cq_host->dma64) {
110 __le64 *data_addr = (__le64 __force *)(link_temp + 4);
111 data_addr[0] = cpu_to_le64(trans_temp);
112 } else {
113 __le32 *data_addr = (__le32 __force *)(link_temp + 4);
114 data_addr[0] = cpu_to_le32(trans_temp);
115 }
116}
117
118static void cmdq_clear_set_irqs(struct cmdq_host *cq_host, u32 clear, u32 set)
119{
120 u32 ier;
121
122 ier = cmdq_readl(cq_host, CQISTE);
123 ier &= ~clear;
124 ier |= set;
125 cmdq_writel(cq_host, ier, CQISTE);
126 cmdq_writel(cq_host, ier, CQISGE);
127 /* ensure the writes are done */
128 mb();
129}
130
131
132#define DRV_NAME "cmdq-host"
133
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700134static void cmdq_dumpregs(struct cmdq_host *cq_host)
135{
136 struct mmc_host *mmc = cq_host->mmc;
137
Asutosh Das02e30862015-05-20 16:52:04 +0530138 pr_err(DRV_NAME ": ========== REGISTER DUMP (%s)==========\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700139 mmc_hostname(mmc));
140
Asutosh Das02e30862015-05-20 16:52:04 +0530141 pr_err(DRV_NAME ": Caps: 0x%08x | Version: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700142 cmdq_readl(cq_host, CQCAP),
143 cmdq_readl(cq_host, CQVER));
Asutosh Das02e30862015-05-20 16:52:04 +0530144 pr_err(DRV_NAME ": Queing config: 0x%08x | Queue Ctrl: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700145 cmdq_readl(cq_host, CQCFG),
146 cmdq_readl(cq_host, CQCTL));
Asutosh Das02e30862015-05-20 16:52:04 +0530147 pr_err(DRV_NAME ": Int stat: 0x%08x | Int enab: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700148 cmdq_readl(cq_host, CQIS),
149 cmdq_readl(cq_host, CQISTE));
Asutosh Das02e30862015-05-20 16:52:04 +0530150 pr_err(DRV_NAME ": Int sig: 0x%08x | Int Coal: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700151 cmdq_readl(cq_host, CQISGE),
152 cmdq_readl(cq_host, CQIC));
Asutosh Das02e30862015-05-20 16:52:04 +0530153 pr_err(DRV_NAME ": TDL base: 0x%08x | TDL up32: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700154 cmdq_readl(cq_host, CQTDLBA),
155 cmdq_readl(cq_host, CQTDLBAU));
Asutosh Das02e30862015-05-20 16:52:04 +0530156 pr_err(DRV_NAME ": Doorbell: 0x%08x | Comp Notif: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700157 cmdq_readl(cq_host, CQTDBR),
158 cmdq_readl(cq_host, CQTCN));
Asutosh Das02e30862015-05-20 16:52:04 +0530159 pr_err(DRV_NAME ": Dev queue: 0x%08x | Dev Pend: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700160 cmdq_readl(cq_host, CQDQS),
161 cmdq_readl(cq_host, CQDPT));
Asutosh Das02e30862015-05-20 16:52:04 +0530162 pr_err(DRV_NAME ": Task clr: 0x%08x | Send stat 1: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700163 cmdq_readl(cq_host, CQTCLR),
164 cmdq_readl(cq_host, CQSSC1));
Asutosh Das02e30862015-05-20 16:52:04 +0530165 pr_err(DRV_NAME ": Send stat 2: 0x%08x | DCMD resp: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700166 cmdq_readl(cq_host, CQSSC2),
167 cmdq_readl(cq_host, CQCRDCT));
Asutosh Das02e30862015-05-20 16:52:04 +0530168 pr_err(DRV_NAME ": Resp err mask: 0x%08x | Task err: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700169 cmdq_readl(cq_host, CQRMEM),
170 cmdq_readl(cq_host, CQTERRI));
Asutosh Das02e30862015-05-20 16:52:04 +0530171 pr_err(DRV_NAME ": Resp idx 0x%08x | Resp arg: 0x%08x\n",
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700172 cmdq_readl(cq_host, CQCRI),
173 cmdq_readl(cq_host, CQCRA));
Asutosh Dasc0ed9c42015-05-29 15:39:37 +0530174 pr_err(DRV_NAME": Vendor cfg 0x%08x\n",
175 cmdq_readl(cq_host, CQ_VENDOR_CFG));
Asutosh Das02e30862015-05-20 16:52:04 +0530176 pr_err(DRV_NAME ": ===========================================\n");
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700177
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700178 if (cq_host->ops->dump_vendor_regs)
179 cq_host->ops->dump_vendor_regs(mmc);
180}
181
182/**
183 * The allocated descriptor table for task, link & transfer descritors
184 * looks like:
185 * |----------|
186 * |task desc | |->|----------|
187 * |----------| | |trans desc|
188 * |link desc-|->| |----------|
189 * |----------| .
190 * . .
191 * no. of slots max-segs
192 * . |----------|
193 * |----------|
194 * The idea here is to create the [task+trans] table and mark & point the
195 * link desc to the transfer desc table on a per slot basis.
196 */
197static int cmdq_host_alloc_tdl(struct cmdq_host *cq_host)
198{
199
200 size_t desc_size;
201 size_t data_size;
202 int i = 0;
203
204 /* task descriptor can be 64/128 bit irrespective of arch */
205 if (cq_host->caps & CMDQ_TASK_DESC_SZ_128) {
206 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) |
207 CQ_TASK_DESC_SZ, CQCFG);
208 cq_host->task_desc_len = 16;
209 } else {
210 cq_host->task_desc_len = 8;
211 }
212
213 /*
214 * 96 bits length of transfer desc instead of 128 bits which means
215 * ADMA would expect next valid descriptor at the 96th bit
216 * or 128th bit
217 */
218 if (cq_host->dma64) {
219 if (cq_host->quirks & CMDQ_QUIRK_SHORT_TXFR_DESC_SZ)
220 cq_host->trans_desc_len = 12;
221 else
222 cq_host->trans_desc_len = 16;
223 cq_host->link_desc_len = 16;
224 } else {
225 cq_host->trans_desc_len = 8;
226 cq_host->link_desc_len = 8;
227 }
228
229 /* total size of a slot: 1 task & 1 transfer (link) */
230 cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len;
231
232 desc_size = cq_host->slot_sz * cq_host->num_slots;
233
234 data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs *
235 (cq_host->num_slots - 1);
236
237 pr_info("%s: desc_size: %d data_sz: %d slot-sz: %d\n", __func__,
238 (int)desc_size, (int)data_size, cq_host->slot_sz);
239
240 /*
241 * allocate a dma-mapped chunk of memory for the descriptors
242 * allocate a dma-mapped chunk of memory for link descriptors
243 * setup each link-desc memory offset per slot-number to
244 * the descriptor table.
245 */
246 cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
247 desc_size,
248 &cq_host->desc_dma_base,
249 GFP_KERNEL);
250 cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
251 data_size,
252 &cq_host->trans_desc_dma_base,
253 GFP_KERNEL);
254 if (!cq_host->desc_base || !cq_host->trans_desc_base)
255 return -ENOMEM;
256
257 pr_info("desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n",
258 cq_host->desc_base, cq_host->trans_desc_base,
259 (unsigned long long)cq_host->desc_dma_base,
260 (unsigned long long) cq_host->trans_desc_dma_base);
261
262 for (; i < (cq_host->num_slots); i++)
263 setup_trans_desc(cq_host, i);
264
265 return 0;
266}
267
268static int cmdq_enable(struct mmc_host *mmc)
269{
270 int err = 0;
271 u32 cqcfg;
272 bool dcmd_enable;
273 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
274
275 if (!cq_host || !mmc->card || !mmc_card_cmdq(mmc->card)) {
276 err = -EINVAL;
277 goto out;
278 }
279
280 if (cq_host->enabled)
281 goto out;
282
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300283 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700284 cqcfg = cmdq_readl(cq_host, CQCFG);
285 if (cqcfg & 0x1) {
286 pr_info("%s: %s: cq_host is already enabled\n",
287 mmc_hostname(mmc), __func__);
288 WARN_ON(1);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700289 goto pm_ref_count;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700290 }
291
292 if (cq_host->quirks & CMDQ_QUIRK_NO_DCMD)
293 dcmd_enable = false;
294 else
295 dcmd_enable = true;
296
297 cqcfg = ((cq_host->caps & CMDQ_TASK_DESC_SZ_128 ? CQ_TASK_DESC_SZ : 0) |
298 (dcmd_enable ? CQ_DCMD : 0));
299
300 cmdq_writel(cq_host, cqcfg, CQCFG);
301 /* enable CQ_HOST */
302 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCFG) | CQ_ENABLE,
303 CQCFG);
304
305 if (!cq_host->desc_base ||
306 !cq_host->trans_desc_base) {
307 err = cmdq_host_alloc_tdl(cq_host);
308 if (err)
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700309 goto pm_ref_count;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700310 }
311
Konstantin Dorfman14c902d2015-06-11 11:33:23 +0300312 cmdq_writel(cq_host, lower_32_bits(cq_host->desc_dma_base), CQTDLBA);
313 cmdq_writel(cq_host, upper_32_bits(cq_host->desc_dma_base), CQTDLBAU);
314
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700315 /*
316 * disable all vendor interrupts
317 * enable CMDQ interrupts
318 * enable the vendor error interrupts
319 */
320 if (cq_host->ops->clear_set_irqs)
321 cq_host->ops->clear_set_irqs(mmc, true);
322
323 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
324
325 /* cq_host would use this rca to address the card */
326 cmdq_writel(cq_host, mmc->card->rca, CQSSC2);
327
328 /* send QSR at lesser intervals than the default */
329 cmdq_writel(cq_host, cmdq_readl(cq_host, CQSSC1) | SEND_QSR_INTERVAL,
330 CQSSC1);
331
Dov Levenglick2b678302015-07-01 14:24:20 +0300332 /* enable bkops exception indication */
Dov Levenglickaea348b2015-07-20 11:59:52 +0300333 if (mmc_card_configured_manual_bkops(mmc->card) &&
334 !mmc_card_configured_auto_bkops(mmc->card))
Dov Levenglick2b678302015-07-01 14:24:20 +0300335 cmdq_writel(cq_host, cmdq_readl(cq_host, CQRMEM) | CQ_EXCEPTION,
336 CQRMEM);
337
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700338 /* ensure the writes are done before enabling CQE */
339 mb();
340
341 cq_host->enabled = true;
342
343 if (cq_host->ops->set_block_size)
344 cq_host->ops->set_block_size(cq_host->mmc);
345
346 if (cq_host->ops->set_data_timeout)
347 cq_host->ops->set_data_timeout(mmc, 0xf);
348
349 if (cq_host->ops->clear_set_dumpregs)
350 cq_host->ops->clear_set_dumpregs(mmc, 1);
351
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530352 if (cq_host->ops->enhanced_strobe_mask)
353 cq_host->ops->enhanced_strobe_mask(mmc, true);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700354
355pm_ref_count:
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300356 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan632b13b2015-08-24 14:36:59 -0700357out:
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700358 return err;
359}
360
361static void cmdq_disable(struct mmc_host *mmc, bool soft)
362{
363 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
364
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300365 cmdq_runtime_pm_get(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700366 if (soft) {
367 cmdq_writel(cq_host, cmdq_readl(
368 cq_host, CQCFG) & ~(CQ_ENABLE),
369 CQCFG);
370 }
Ritesh Harjani6b2ea572015-07-15 13:23:05 +0530371 if (cq_host->ops->enhanced_strobe_mask)
372 cq_host->ops->enhanced_strobe_mask(mmc, false);
373
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300374 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700375 cq_host->enabled = false;
376}
377
Asutosh Das02e30862015-05-20 16:52:04 +0530378static void cmdq_reset(struct mmc_host *mmc, bool soft)
379{
380 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
381 unsigned int cqcfg;
382 unsigned int tdlba;
383 unsigned int tdlbau;
384 unsigned int rca;
385 int ret;
386
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300387 cmdq_runtime_pm_get(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530388 cqcfg = cmdq_readl(cq_host, CQCFG);
389 tdlba = cmdq_readl(cq_host, CQTDLBA);
390 tdlbau = cmdq_readl(cq_host, CQTDLBAU);
391 rca = cmdq_readl(cq_host, CQSSC2);
392
393 cmdq_disable(mmc, true);
394
395 if (cq_host->ops->reset) {
396 ret = cq_host->ops->reset(mmc);
397 if (ret) {
398 pr_crit("%s: reset CMDQ controller: failed\n",
399 mmc_hostname(mmc));
400 BUG();
401 }
402 }
403
404 cmdq_writel(cq_host, tdlba, CQTDLBA);
405 cmdq_writel(cq_host, tdlbau, CQTDLBAU);
406
407 if (cq_host->ops->clear_set_irqs)
408 cq_host->ops->clear_set_irqs(mmc, true);
409
410 cmdq_clear_set_irqs(cq_host, 0x0, CQ_INT_ALL);
411
412 /* cq_host would use this rca to address the card */
413 cmdq_writel(cq_host, rca, CQSSC2);
414
415 /* ensure the writes are done before enabling CQE */
416 mb();
417
418 cmdq_writel(cq_host, cqcfg, CQCFG);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300419 cmdq_runtime_pm_put(cq_host);
Asutosh Das02e30862015-05-20 16:52:04 +0530420 cq_host->enabled = true;
421}
422
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700423static void cmdq_prep_task_desc(struct mmc_request *mrq,
424 u64 *data, bool intr, bool qbr)
425{
426 struct mmc_cmdq_req *cmdq_req = mrq->cmdq_req;
427 u32 req_flags = cmdq_req->cmdq_req_flags;
428
429 pr_debug("%s: %s: data-tag: 0x%08x - dir: %d - prio: %d - cnt: 0x%08x - addr: 0x%llx\n",
430 mmc_hostname(mrq->host), __func__,
431 !!(req_flags & DAT_TAG), !!(req_flags & DIR),
432 !!(req_flags & PRIO), cmdq_req->data.blocks,
433 (u64)mrq->cmdq_req->blk_addr);
434
435 *data = VALID(1) |
436 END(1) |
437 INT(intr) |
438 ACT(0x5) |
439 FORCED_PROG(!!(req_flags & FORCED_PRG)) |
440 CONTEXT(mrq->cmdq_req->ctx_id) |
441 DATA_TAG(!!(req_flags & DAT_TAG)) |
442 DATA_DIR(!!(req_flags & DIR)) |
443 PRIORITY(!!(req_flags & PRIO)) |
444 QBAR(qbr) |
445 REL_WRITE(!!(req_flags & REL_WR)) |
446 BLK_COUNT(mrq->cmdq_req->data.blocks) |
447 BLK_ADDR((u64)mrq->cmdq_req->blk_addr);
448}
449
450static int cmdq_dma_map(struct mmc_host *host, struct mmc_request *mrq)
451{
452 int sg_count;
453 struct mmc_data *data = mrq->data;
454
455 if (!data)
456 return -EINVAL;
457
458 sg_count = dma_map_sg(mmc_dev(host), data->sg,
459 data->sg_len,
460 (data->flags & MMC_DATA_WRITE) ?
461 DMA_TO_DEVICE : DMA_FROM_DEVICE);
462 if (!sg_count) {
463 pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
464 return -ENOMEM;
465 }
466
467 return sg_count;
468}
469
Sahitya Tummala78a68e52015-09-30 15:55:41 +0530470static void cmdq_set_tran_desc(u8 *desc, dma_addr_t addr, int len,
471 bool end, bool is_dma64)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700472{
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700473 __le32 *attr = (__le32 __force *)desc;
474
475 *attr = (VALID(1) |
476 END(end ? 1 : 0) |
477 INT(0) |
478 ACT(0x4) |
479 DAT_LENGTH(len));
480
Sahitya Tummala78a68e52015-09-30 15:55:41 +0530481 if (is_dma64) {
482 __le64 *dataddr = (__le64 __force *)(desc + 4);
483
484 dataddr[0] = cpu_to_le64(addr);
485 } else {
486 __le32 *dataddr = (__le32 __force *)(desc + 4);
487
488 dataddr[0] = cpu_to_le32(addr);
489 }
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700490}
491
492static int cmdq_prep_tran_desc(struct mmc_request *mrq,
493 struct cmdq_host *cq_host, int tag)
494{
495 struct mmc_data *data = mrq->data;
496 int i, sg_count, len;
497 bool end = false;
498 dma_addr_t addr;
499 u8 *desc;
500 struct scatterlist *sg;
501
502 sg_count = cmdq_dma_map(mrq->host, mrq);
503 if (sg_count < 0) {
504 pr_err("%s: %s: unable to map sg lists, %d\n",
505 mmc_hostname(mrq->host), __func__, sg_count);
506 return sg_count;
507 }
508
509 desc = get_trans_desc(cq_host, tag);
510 memset(desc, 0, cq_host->trans_desc_len * cq_host->mmc->max_segs);
511
512 for_each_sg(data->sg, sg, sg_count, i) {
513 addr = sg_dma_address(sg);
514 len = sg_dma_len(sg);
515
516 if ((i+1) == sg_count)
517 end = true;
Sahitya Tummala78a68e52015-09-30 15:55:41 +0530518 cmdq_set_tran_desc(desc, addr, len, end, cq_host->dma64);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700519 desc += cq_host->trans_desc_len;
520 }
521
522 pr_debug("%s: req: 0x%p tag: %d calc_trans_des: 0x%p sg-cnt: %d\n",
523 __func__, mrq->req, tag, desc, sg_count);
524
525 return 0;
526}
527
528static void cmdq_prep_dcmd_desc(struct mmc_host *mmc,
529 struct mmc_request *mrq)
530{
531 u64 *task_desc = NULL;
532 u64 data = 0;
533 u8 resp_type;
534 u8 *desc;
535 __le64 *dataddr;
536 struct cmdq_host *cq_host = mmc_cmdq_private(mmc);
537 u8 timing;
538
539 if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
540 resp_type = 0x0;
541 timing = 0x1;
542 } else {
Sahitya Tummala72bd8402015-05-29 13:27:38 +0530543 if (mrq->cmd->flags & MMC_RSP_BUSY) {
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700544 resp_type = 0x3;
545 timing = 0x0;
546 } else {
547 resp_type = 0x2;
548 timing = 0x1;
549 }
550 }
551
552 task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
553 memset(task_desc, 0, cq_host->task_desc_len);
554 data |= (VALID(1) |
555 END(1) |
556 INT(1) |
557 QBAR(1) |
558 ACT(0x5) |
559 CMD_INDEX(mrq->cmd->opcode) |
560 CMD_TIMING(timing) | RESP_TYPE(resp_type));
561 *task_desc |= data;
562 desc = (u8 *)task_desc;
563 pr_debug("cmdq: dcmd: cmd: %d timing: %d resp: %d\n",
564 mrq->cmd->opcode, timing, resp_type);
565 dataddr = (__le64 __force *)(desc + 4);
566 dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
567
568}
569
Gilad Broner44445992015-09-29 16:05:39 +0300570static void cmdq_pm_qos_vote(struct sdhci_host *host, struct mmc_request *mrq)
571{
572 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
573 struct sdhci_msm_host *msm_host = pltfm_host->priv;
574
575 sdhci_msm_pm_qos_cpu_vote(host,
576 msm_host->pdata->pm_qos_data.cmdq_latency, mrq->req->cpu);
577}
578
579static void cmdq_pm_qos_unvote(struct sdhci_host *host, struct mmc_request *mrq)
580{
581 /* use async as we're inside an atomic context (soft-irq) */
582 sdhci_msm_pm_qos_cpu_unvote(host, mrq->req->cpu, true);
583}
584
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700585static int cmdq_request(struct mmc_host *mmc, struct mmc_request *mrq)
586{
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300587 int err = 0;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700588 u64 data = 0;
589 u64 *task_desc = NULL;
590 u32 tag = mrq->cmdq_req->tag;
591 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Gilad Broner44445992015-09-29 16:05:39 +0300592 struct sdhci_host *host = mmc_priv(mmc);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700593
594 if (!cq_host->enabled) {
595 pr_err("%s: CMDQ host not enabled yet !!!\n",
596 mmc_hostname(mmc));
597 err = -EINVAL;
598 goto out;
599 }
600
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300601 cmdq_runtime_pm_get(cq_host);
602
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700603 if (mrq->cmdq_req->cmdq_req_flags & DCMD) {
604 cmdq_prep_dcmd_desc(mmc, mrq);
605 cq_host->mrq_slot[DCMD_SLOT] = mrq;
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700606 /* DCMD's are always issued on a fixed slot */
607 tag = DCMD_SLOT;
608 goto ring_doorbell;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700609 }
610
611 task_desc = (__le64 __force *)get_desc(cq_host, tag);
612
613 cmdq_prep_task_desc(mrq, &data, 1,
614 (mrq->cmdq_req->cmdq_req_flags & QBR));
615 *task_desc = cpu_to_le64(data);
616
617 err = cmdq_prep_tran_desc(mrq, cq_host, tag);
618 if (err) {
619 pr_err("%s: %s: failed to setup tx desc: %d\n",
620 mmc_hostname(mmc), __func__, err);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300621 goto out;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700622 }
623
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700624 cq_host->mrq_slot[tag] = mrq;
625 if (cq_host->ops->set_tranfer_params)
626 cq_host->ops->set_tranfer_params(mmc);
627
Gilad Broner44445992015-09-29 16:05:39 +0300628 /* PM QoS */
629 sdhci_msm_pm_qos_irq_vote(host);
630 cmdq_pm_qos_vote(host, mrq);
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700631ring_doorbell:
632 /* Ensure the task descriptor list is flushed before ringing doorbell */
633 wmb();
Venkat Gopalakrishnan7d53e832015-10-01 14:34:10 -0700634 if (cmdq_readl(cq_host, CQTDBR) & (1 << tag)) {
635 cmdq_dumpregs(cq_host);
636 BUG_ON(1);
637 }
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700638 cmdq_writel(cq_host, 1 << tag, CQTDBR);
Venkat Gopalakrishnanf1329ce2015-08-10 14:55:23 -0700639 /* Commit the doorbell write immediately */
640 wmb();
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700641
642out:
643 return err;
644}
645
646static void cmdq_finish_data(struct mmc_host *mmc, unsigned int tag)
647{
648 struct mmc_request *mrq;
649 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
650
Asutosh Das02e30862015-05-20 16:52:04 +0530651 mrq = get_req_by_tag(cq_host, tag);
Sahitya Tummala9549d562015-05-29 15:41:18 +0530652 if (tag == cq_host->dcmd_slot)
653 mrq->cmd->resp[0] = cmdq_readl(cq_host, CQCRDCT);
654
Asutosh Dasc0ed9c42015-05-29 15:39:37 +0530655 if (mrq->cmdq_req->cmdq_req_flags & DCMD)
656 cmdq_writel(cq_host, cmdq_readl(cq_host, CQ_VENDOR_CFG) |
Sahitya Tummalab9ed5612015-10-05 16:20:10 +0530657 CMDQ_SEND_STATUS_TRIGGER, CQ_VENDOR_CFG);
Konstantin Dorfman27af9a92015-08-02 17:06:18 +0300658
659 cmdq_runtime_pm_put(cq_host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700660 mrq->done(mrq);
661}
662
Asutosh Das02e30862015-05-20 16:52:04 +0530663irqreturn_t cmdq_irq(struct mmc_host *mmc, int err)
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700664{
665 u32 status;
666 unsigned long tag = 0, comp_status;
667 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Asutosh Das02e30862015-05-20 16:52:04 +0530668 unsigned long err_info = 0;
669 struct mmc_request *mrq;
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700670
671 status = cmdq_readl(cq_host, CQIS);
672 cmdq_writel(cq_host, status, CQIS);
673
Asutosh Das02e30862015-05-20 16:52:04 +0530674 if (!status && !err)
675 return IRQ_NONE;
676
677 if (err || (status & CQIS_RED)) {
678 err_info = cmdq_readl(cq_host, CQTERRI);
679 pr_err("%s: err: %d status: 0x%08x task-err-info (0x%08lx)\n",
680 mmc_hostname(mmc), err, status, err_info);
681
682 cmdq_dumpregs(cq_host);
683
684 if (err_info & CQ_RMEFV) {
685 tag = GET_CMD_ERR_TAG(err_info);
686 pr_err("%s: CMD err tag: %lu\n", __func__, tag);
687
688 mrq = get_req_by_tag(cq_host, tag);
689 /* CMD44/45/46/47 will not have a valid cmd */
690 if (mrq->cmd)
691 mrq->cmd->error = err;
692 else
693 mrq->data->error = err;
694 } else {
695 tag = GET_DAT_ERR_TAG(err_info);
696 pr_err("%s: Dat err tag: %lu\n", __func__, tag);
697 mrq = get_req_by_tag(cq_host, tag);
698 mrq->data->error = err;
699 }
700
Asutosh Das02e30862015-05-20 16:52:04 +0530701 /*
702 * CQE detected a response error from device
703 * In most cases, this would require a reset.
704 */
705 if (status & CQIS_RED) {
Dov Levenglick2b678302015-07-01 14:24:20 +0300706 /*
707 * will check if the RED error is due to a bkops
708 * exception once the queue is empty
709 */
710 BUG_ON(!mmc->card);
711 if (mmc_card_configured_manual_bkops(mmc->card) &&
712 !mmc_card_configured_auto_bkops(mmc->card))
713 mmc->card->bkops.needs_check = true;
714
Asutosh Das02e30862015-05-20 16:52:04 +0530715 mrq->cmdq_req->resp_err = true;
716 pr_err("%s: Response error (0x%08x) from card !!!",
Dov Levenglick2b678302015-07-01 14:24:20 +0300717 mmc_hostname(mmc), status);
Asutosh Das02e30862015-05-20 16:52:04 +0530718 } else {
719 mrq->cmdq_req->resp_idx = cmdq_readl(cq_host, CQCRI);
720 mrq->cmdq_req->resp_arg = cmdq_readl(cq_host, CQCRA);
721 }
722
723 mmc->err_mrq = mrq;
724 cmdq_finish_data(mmc, tag);
725 }
726
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700727 if (status & CQIS_TCC) {
728 /* read QCTCN and complete the request */
729 comp_status = cmdq_readl(cq_host, CQTCN);
730 if (!comp_status)
731 goto out;
732
733 for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
734 /* complete the corresponding mrq */
735 pr_debug("%s: completing tag -> %lu\n",
736 mmc_hostname(mmc), tag);
737 cmdq_finish_data(mmc, tag);
738 }
739 cmdq_writel(cq_host, comp_status, CQTCN);
740 }
741
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530742 if (status & CQIS_HAC) {
Konstantin Dorfmanfa321072015-05-31 10:10:13 +0300743 if (cq_host->ops->post_cqe_halt)
744 cq_host->ops->post_cqe_halt(mmc);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530745 /* halt is completed, wakeup waiting thread */
746 complete(&cq_host->halt_comp);
747 }
748
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700749out:
750 return IRQ_HANDLED;
751}
752EXPORT_SYMBOL(cmdq_irq);
753
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530754/* May sleep */
755static int cmdq_halt(struct mmc_host *mmc, bool halt)
756{
757 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300758 u32 ret = 0;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530759
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300760 cmdq_runtime_pm_get(cq_host);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530761 if (halt) {
762 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) | HALT,
763 CQCTL);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300764 ret = wait_for_completion_timeout(&cq_host->halt_comp,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530765 msecs_to_jiffies(HALT_TIMEOUT_MS));
766 /* halt done: re-enable legacy interrupts */
767 if (cq_host->ops->clear_set_irqs)
768 cq_host->ops->clear_set_irqs(mmc, false);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300769 ret = ret ? 0 : -ETIMEDOUT;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530770 } else {
Asutosh Das3f730d12015-07-08 11:41:35 +0530771 if (cq_host->ops->set_data_timeout)
772 cq_host->ops->set_data_timeout(mmc, 0xf);
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530773 if (cq_host->ops->clear_set_irqs)
774 cq_host->ops->clear_set_irqs(mmc, true);
775 cmdq_writel(cq_host, cmdq_readl(cq_host, CQCTL) & ~HALT,
776 CQCTL);
777 }
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300778 cmdq_runtime_pm_put(cq_host);
779 return ret;
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530780}
781
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700782static void cmdq_post_req(struct mmc_host *host, struct mmc_request *mrq,
783 int err)
784{
785 struct mmc_data *data = mrq->data;
Gilad Broner44445992015-09-29 16:05:39 +0300786 struct sdhci_host *sdhci_host = mmc_priv(host);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700787
788 if (data) {
789 data->error = err;
790 dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len,
791 (data->flags & MMC_DATA_READ) ?
792 DMA_FROM_DEVICE : DMA_TO_DEVICE);
793 if (err)
794 data->bytes_xfered = 0;
795 else
796 data->bytes_xfered = blk_rq_bytes(mrq->req);
Gilad Broner44445992015-09-29 16:05:39 +0300797
798 /* we're in atomic context (soft-irq) so unvote async. */
799 sdhci_msm_pm_qos_irq_unvote(sdhci_host, true);
800 cmdq_pm_qos_unvote(sdhci_host, mrq);
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700801 }
802}
803
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530804static void cmdq_dumpstate(struct mmc_host *mmc)
805{
806 struct cmdq_host *cq_host = (struct cmdq_host *)mmc_cmdq_private(mmc);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300807 cmdq_runtime_pm_get(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530808 cmdq_dumpregs(cq_host);
Konstantin Dorfman4d40cf22015-06-11 11:41:53 +0300809 cmdq_runtime_pm_put(cq_host);
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530810}
811
Gilad Broner44445992015-09-29 16:05:39 +0300812static int cmdq_late_init(struct mmc_host *mmc)
813{
814 struct sdhci_host *host = mmc_priv(mmc);
815 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
816 struct sdhci_msm_host *msm_host = pltfm_host->priv;
817
818 /*
819 * TODO: This should basically move to something like "sdhci-cmdq-msm"
820 * for msm specific implementation.
821 */
822 sdhci_msm_pm_qos_irq_init(host);
823
824 if (msm_host->pdata->pm_qos_data.cmdq_valid)
825 sdhci_msm_pm_qos_cpu_init(host,
826 msm_host->pdata->pm_qos_data.cmdq_latency);
827 return 0;
828}
829
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700830static const struct mmc_cmdq_host_ops cmdq_host_ops = {
Gilad Broner44445992015-09-29 16:05:39 +0300831 .init = cmdq_late_init,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700832 .enable = cmdq_enable,
833 .disable = cmdq_disable,
834 .request = cmdq_request,
835 .post_req = cmdq_post_req,
Asutosh Dasaa1e1c72015-05-21 17:22:10 +0530836 .halt = cmdq_halt,
Asutosh Das02e30862015-05-20 16:52:04 +0530837 .reset = cmdq_reset,
Asutosh Dasfa8836b2015-03-02 23:14:05 +0530838 .dumpstate = cmdq_dumpstate,
Venkat Gopalakrishnan0225ff92015-05-29 17:25:46 -0700839};
840
841struct cmdq_host *cmdq_pltfm_init(struct platform_device *pdev)
842{
843 struct cmdq_host *cq_host;
844 struct resource *cmdq_memres = NULL;
845
846 /* check and setup CMDQ interface */
847 cmdq_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
848 "cmdq_mem");
849 if (!cmdq_memres) {
850 dev_dbg(&pdev->dev, "CMDQ not supported\n");
851 return ERR_PTR(-EINVAL);
852 }
853
854 cq_host = kzalloc(sizeof(*cq_host), GFP_KERNEL);
855 if (!cq_host) {
856 dev_err(&pdev->dev, "failed to allocate memory for CMDQ\n");
857 return ERR_PTR(-ENOMEM);
858 }
859 cq_host->mmio = devm_ioremap(&pdev->dev,
860 cmdq_memres->start,
861 resource_size(cmdq_memres));
862 if (!cq_host->mmio) {
863 dev_err(&pdev->dev, "failed to remap cmdq regs\n");
864 kfree(cq_host);
865 return ERR_PTR(-EBUSY);
866 }
867 dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
868
869 return cq_host;
870}
871EXPORT_SYMBOL(cmdq_pltfm_init);
872
873int cmdq_init(struct cmdq_host *cq_host, struct mmc_host *mmc,
874 bool dma64)
875{
876 int err = 0;
877
878 cq_host->dma64 = dma64;
879 cq_host->mmc = mmc;
880 cq_host->mmc->cmdq_private = cq_host;
881
882 cq_host->num_slots = NUM_SLOTS;
883 cq_host->dcmd_slot = DCMD_SLOT;
884
885 mmc->cmdq_ops = &cmdq_host_ops;
886
887 cq_host->mrq_slot = kzalloc(sizeof(cq_host->mrq_slot) *
888 cq_host->num_slots, GFP_KERNEL);
889 if (!cq_host->mrq_slot)
890 return -ENOMEM;
891
892 init_completion(&cq_host->halt_comp);
893 return err;
894}
895EXPORT_SYMBOL(cmdq_init);