• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2015, The Linux Foundation. All rights reserved.
3  */
4 
5 #include <linux/delay.h>
6 #include <linux/highmem.h>
7 #include <linux/io.h>
8 #include <linux/iopoll.h>
9 #include <linux/module.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/slab.h>
12 #include <linux/scatterlist.h>
13 #include <linux/platform_device.h>
14 #include <linux/ktime.h>
15 
16 #include <linux/mmc/mmc.h>
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/card.h>
19 
20 #include "cqhci.h"
21 #include "cqhci-crypto.h"
22 
23 #define DCMD_SLOT 31
24 #define NUM_SLOTS 32
25 
26 struct cqhci_slot {
27 	struct mmc_request *mrq;
28 	unsigned int flags;
29 #define CQHCI_EXTERNAL_TIMEOUT	BIT(0)
30 #define CQHCI_COMPLETED		BIT(1)
31 #define CQHCI_HOST_CRC		BIT(2)
32 #define CQHCI_HOST_TIMEOUT	BIT(3)
33 #define CQHCI_HOST_OTHER	BIT(4)
34 };
35 
get_desc(struct cqhci_host * cq_host,u8 tag)36 static inline u8 *get_desc(struct cqhci_host *cq_host, u8 tag)
37 {
38 	return cq_host->desc_base + (tag * cq_host->slot_sz);
39 }
40 
get_link_desc(struct cqhci_host * cq_host,u8 tag)41 static inline u8 *get_link_desc(struct cqhci_host *cq_host, u8 tag)
42 {
43 	u8 *desc = get_desc(cq_host, tag);
44 
45 	return desc + cq_host->task_desc_len;
46 }
47 
get_trans_desc_dma(struct cqhci_host * cq_host,u8 tag)48 static inline dma_addr_t get_trans_desc_dma(struct cqhci_host *cq_host, u8 tag)
49 {
50 	return cq_host->trans_desc_dma_base +
51 		(cq_host->mmc->max_segs * tag *
52 		 cq_host->trans_desc_len);
53 }
54 
get_trans_desc(struct cqhci_host * cq_host,u8 tag)55 static inline u8 *get_trans_desc(struct cqhci_host *cq_host, u8 tag)
56 {
57 	return cq_host->trans_desc_base +
58 		(cq_host->trans_desc_len * cq_host->mmc->max_segs * tag);
59 }
60 
setup_trans_desc(struct cqhci_host * cq_host,u8 tag)61 static void setup_trans_desc(struct cqhci_host *cq_host, u8 tag)
62 {
63 	u8 *link_temp;
64 	dma_addr_t trans_temp;
65 
66 	link_temp = get_link_desc(cq_host, tag);
67 	trans_temp = get_trans_desc_dma(cq_host, tag);
68 
69 	memset(link_temp, 0, cq_host->link_desc_len);
70 	if (cq_host->link_desc_len > 8)
71 		*(link_temp + 8) = 0;
72 
73 	if (tag == DCMD_SLOT && (cq_host->mmc->caps2 & MMC_CAP2_CQE_DCMD)) {
74 		*link_temp = CQHCI_VALID(0) | CQHCI_ACT(0) | CQHCI_END(1);
75 		return;
76 	}
77 
78 	*link_temp = CQHCI_VALID(1) | CQHCI_ACT(0x6) | CQHCI_END(0);
79 
80 	if (cq_host->dma64) {
81 		__le64 *data_addr = (__le64 __force *)(link_temp + 4);
82 
83 		data_addr[0] = cpu_to_le64(trans_temp);
84 	} else {
85 		__le32 *data_addr = (__le32 __force *)(link_temp + 4);
86 
87 		data_addr[0] = cpu_to_le32(trans_temp);
88 	}
89 }
90 
cqhci_set_irqs(struct cqhci_host * cq_host,u32 set)91 static void cqhci_set_irqs(struct cqhci_host *cq_host, u32 set)
92 {
93 	cqhci_writel(cq_host, set, CQHCI_ISTE);
94 	cqhci_writel(cq_host, set, CQHCI_ISGE);
95 }
96 
97 #define DRV_NAME "cqhci"
98 
99 #define CQHCI_DUMP(f, x...) \
100 	pr_err("%s: " DRV_NAME ": " f, mmc_hostname(mmc), ## x)
101 
cqhci_dumpregs(struct cqhci_host * cq_host)102 static void cqhci_dumpregs(struct cqhci_host *cq_host)
103 {
104 	struct mmc_host *mmc = cq_host->mmc;
105 
106 	CQHCI_DUMP("============ CQHCI REGISTER DUMP ===========\n");
107 
108 	CQHCI_DUMP("Caps:      0x%08x | Version:  0x%08x\n",
109 		   cqhci_readl(cq_host, CQHCI_CAP),
110 		   cqhci_readl(cq_host, CQHCI_VER));
111 	CQHCI_DUMP("Config:    0x%08x | Control:  0x%08x\n",
112 		   cqhci_readl(cq_host, CQHCI_CFG),
113 		   cqhci_readl(cq_host, CQHCI_CTL));
114 	CQHCI_DUMP("Int stat:  0x%08x | Int enab: 0x%08x\n",
115 		   cqhci_readl(cq_host, CQHCI_IS),
116 		   cqhci_readl(cq_host, CQHCI_ISTE));
117 	CQHCI_DUMP("Int sig:   0x%08x | Int Coal: 0x%08x\n",
118 		   cqhci_readl(cq_host, CQHCI_ISGE),
119 		   cqhci_readl(cq_host, CQHCI_IC));
120 	CQHCI_DUMP("TDL base:  0x%08x | TDL up32: 0x%08x\n",
121 		   cqhci_readl(cq_host, CQHCI_TDLBA),
122 		   cqhci_readl(cq_host, CQHCI_TDLBAU));
123 	CQHCI_DUMP("Doorbell:  0x%08x | TCN:      0x%08x\n",
124 		   cqhci_readl(cq_host, CQHCI_TDBR),
125 		   cqhci_readl(cq_host, CQHCI_TCN));
126 	CQHCI_DUMP("Dev queue: 0x%08x | Dev Pend: 0x%08x\n",
127 		   cqhci_readl(cq_host, CQHCI_DQS),
128 		   cqhci_readl(cq_host, CQHCI_DPT));
129 	CQHCI_DUMP("Task clr:  0x%08x | SSC1:     0x%08x\n",
130 		   cqhci_readl(cq_host, CQHCI_TCLR),
131 		   cqhci_readl(cq_host, CQHCI_SSC1));
132 	CQHCI_DUMP("SSC2:      0x%08x | DCMD rsp: 0x%08x\n",
133 		   cqhci_readl(cq_host, CQHCI_SSC2),
134 		   cqhci_readl(cq_host, CQHCI_CRDCT));
135 	CQHCI_DUMP("RED mask:  0x%08x | TERRI:    0x%08x\n",
136 		   cqhci_readl(cq_host, CQHCI_RMEM),
137 		   cqhci_readl(cq_host, CQHCI_TERRI));
138 	CQHCI_DUMP("Resp idx:  0x%08x | Resp arg: 0x%08x\n",
139 		   cqhci_readl(cq_host, CQHCI_CRI),
140 		   cqhci_readl(cq_host, CQHCI_CRA));
141 
142 	if (cq_host->ops->dumpregs)
143 		cq_host->ops->dumpregs(mmc);
144 	else
145 		CQHCI_DUMP(": ===========================================\n");
146 }
147 
148 /*
149  * The allocated descriptor table for task, link & transfer descritors
150  * looks like:
151  * |----------|
152  * |task desc |  |->|----------|
153  * |----------|  |  |trans desc|
154  * |link desc-|->|  |----------|
155  * |----------|          .
156  *      .                .
157  *  no. of slots      max-segs
158  *      .           |----------|
159  * |----------|
160  * The idea here is to create the [task+trans] table and mark & point the
161  * link desc to the transfer desc table on a per slot basis.
162  */
cqhci_host_alloc_tdl(struct cqhci_host * cq_host)163 static int cqhci_host_alloc_tdl(struct cqhci_host *cq_host)
164 {
165 	int i = 0;
166 
167 	/* task descriptor can be 64/128 bit irrespective of arch */
168 	if (cq_host->caps & CQHCI_TASK_DESC_SZ_128) {
169 		cqhci_writel(cq_host, cqhci_readl(cq_host, CQHCI_CFG) |
170 			       CQHCI_TASK_DESC_SZ, CQHCI_CFG);
171 		cq_host->task_desc_len = 16;
172 	} else {
173 		cq_host->task_desc_len = 8;
174 	}
175 
176 	/*
177 	 * 96 bits length of transfer desc instead of 128 bits which means
178 	 * ADMA would expect next valid descriptor at the 96th bit
179 	 * or 128th bit
180 	 */
181 	if (cq_host->dma64) {
182 		if (cq_host->quirks & CQHCI_QUIRK_SHORT_TXFR_DESC_SZ)
183 			cq_host->trans_desc_len = 12;
184 		else
185 			cq_host->trans_desc_len = 16;
186 		cq_host->link_desc_len = 16;
187 	} else {
188 		cq_host->trans_desc_len = 8;
189 		cq_host->link_desc_len = 8;
190 	}
191 
192 	/* total size of a slot: 1 task & 1 transfer (link) */
193 	cq_host->slot_sz = cq_host->task_desc_len + cq_host->link_desc_len;
194 
195 	cq_host->desc_size = cq_host->slot_sz * cq_host->num_slots;
196 
197 	cq_host->data_size = cq_host->trans_desc_len * cq_host->mmc->max_segs *
198 		cq_host->mmc->cqe_qdepth;
199 
200 	pr_debug("%s: cqhci: desc_size: %zu data_sz: %zu slot-sz: %d\n",
201 		 mmc_hostname(cq_host->mmc), cq_host->desc_size, cq_host->data_size,
202 		 cq_host->slot_sz);
203 
204 	/*
205 	 * allocate a dma-mapped chunk of memory for the descriptors
206 	 * allocate a dma-mapped chunk of memory for link descriptors
207 	 * setup each link-desc memory offset per slot-number to
208 	 * the descriptor table.
209 	 */
210 	cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
211 						 cq_host->desc_size,
212 						 &cq_host->desc_dma_base,
213 						 GFP_KERNEL);
214 	if (!cq_host->desc_base)
215 		return -ENOMEM;
216 
217 	cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
218 					      cq_host->data_size,
219 					      &cq_host->trans_desc_dma_base,
220 					      GFP_KERNEL);
221 	if (!cq_host->trans_desc_base) {
222 		dmam_free_coherent(mmc_dev(cq_host->mmc), cq_host->desc_size,
223 				   cq_host->desc_base,
224 				   cq_host->desc_dma_base);
225 		cq_host->desc_base = NULL;
226 		cq_host->desc_dma_base = 0;
227 		return -ENOMEM;
228 	}
229 
230 	pr_debug("%s: cqhci: desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n",
231 		 mmc_hostname(cq_host->mmc), cq_host->desc_base, cq_host->trans_desc_base,
232 		(unsigned long long)cq_host->desc_dma_base,
233 		(unsigned long long)cq_host->trans_desc_dma_base);
234 
235 	for (; i < (cq_host->num_slots); i++)
236 		setup_trans_desc(cq_host, i);
237 
238 	return 0;
239 }
240 
__cqhci_enable(struct cqhci_host * cq_host)241 static void __cqhci_enable(struct cqhci_host *cq_host)
242 {
243 	struct mmc_host *mmc = cq_host->mmc;
244 	u32 cqcfg;
245 
246 	cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
247 
248 	/* Configuration must not be changed while enabled */
249 	if (cqcfg & CQHCI_ENABLE) {
250 		cqcfg &= ~CQHCI_ENABLE;
251 		cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
252 	}
253 
254 	cqcfg &= ~(CQHCI_DCMD | CQHCI_TASK_DESC_SZ);
255 
256 	if (mmc->caps2 & MMC_CAP2_CQE_DCMD)
257 		cqcfg |= CQHCI_DCMD;
258 
259 	if (cq_host->caps & CQHCI_TASK_DESC_SZ_128)
260 		cqcfg |= CQHCI_TASK_DESC_SZ;
261 
262 	if (mmc->caps2 & MMC_CAP2_CRYPTO)
263 		cqcfg |= CQHCI_CRYPTO_GENERAL_ENABLE;
264 
265 	cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
266 
267 	cqhci_writel(cq_host, lower_32_bits(cq_host->desc_dma_base),
268 		     CQHCI_TDLBA);
269 	cqhci_writel(cq_host, upper_32_bits(cq_host->desc_dma_base),
270 		     CQHCI_TDLBAU);
271 
272 	cqhci_writel(cq_host, cq_host->rca, CQHCI_SSC2);
273 
274 	cqhci_set_irqs(cq_host, 0);
275 
276 	cqcfg |= CQHCI_ENABLE;
277 
278 	cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
279 
280 	if (cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT)
281 		cqhci_writel(cq_host, 0, CQHCI_CTL);
282 
283 	mmc->cqe_on = true;
284 
285 	if (cq_host->ops->enable)
286 		cq_host->ops->enable(mmc);
287 
288 	/* Ensure all writes are done before interrupts are enabled */
289 	wmb();
290 
291 	cqhci_set_irqs(cq_host, CQHCI_IS_MASK);
292 
293 	cq_host->activated = true;
294 }
295 
__cqhci_disable(struct cqhci_host * cq_host)296 static void __cqhci_disable(struct cqhci_host *cq_host)
297 {
298 	u32 cqcfg;
299 
300 	cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
301 	cqcfg &= ~CQHCI_ENABLE;
302 	cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
303 
304 	cq_host->mmc->cqe_on = false;
305 
306 	cq_host->activated = false;
307 }
308 
cqhci_deactivate(struct mmc_host * mmc)309 int cqhci_deactivate(struct mmc_host *mmc)
310 {
311 	struct cqhci_host *cq_host = mmc->cqe_private;
312 
313 	if (cq_host->enabled && cq_host->activated)
314 		__cqhci_disable(cq_host);
315 
316 	return 0;
317 }
318 EXPORT_SYMBOL(cqhci_deactivate);
319 
cqhci_resume(struct mmc_host * mmc)320 int cqhci_resume(struct mmc_host *mmc)
321 {
322 	/* Re-enable is done upon first request */
323 	return 0;
324 }
325 EXPORT_SYMBOL(cqhci_resume);
326 
cqhci_enable(struct mmc_host * mmc,struct mmc_card * card)327 static int cqhci_enable(struct mmc_host *mmc, struct mmc_card *card)
328 {
329 	struct cqhci_host *cq_host = mmc->cqe_private;
330 	int err;
331 
332 	if (!card->ext_csd.cmdq_en)
333 		return -EINVAL;
334 
335 	if (cq_host->enabled)
336 		return 0;
337 
338 	cq_host->rca = card->rca;
339 
340 	err = cqhci_host_alloc_tdl(cq_host);
341 	if (err) {
342 		pr_err("%s: Failed to enable CQE, error %d\n",
343 		       mmc_hostname(mmc), err);
344 		return err;
345 	}
346 
347 	__cqhci_enable(cq_host);
348 
349 	cq_host->enabled = true;
350 
351 #ifdef DEBUG
352 	cqhci_dumpregs(cq_host);
353 #endif
354 	return 0;
355 }
356 
357 /* CQHCI is idle and should halt immediately, so set a small timeout */
358 #define CQHCI_OFF_TIMEOUT 100
359 
cqhci_read_ctl(struct cqhci_host * cq_host)360 static u32 cqhci_read_ctl(struct cqhci_host *cq_host)
361 {
362 	return cqhci_readl(cq_host, CQHCI_CTL);
363 }
364 
cqhci_off(struct mmc_host * mmc)365 static void cqhci_off(struct mmc_host *mmc)
366 {
367 	struct cqhci_host *cq_host = mmc->cqe_private;
368 	u32 reg;
369 	int err;
370 
371 	if (!cq_host->enabled || !mmc->cqe_on || cq_host->recovery_halt)
372 		return;
373 
374 	if (cq_host->ops->disable)
375 		cq_host->ops->disable(mmc, false);
376 
377 	cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
378 
379 	err = readx_poll_timeout(cqhci_read_ctl, cq_host, reg,
380 				 reg & CQHCI_HALT, 0, CQHCI_OFF_TIMEOUT);
381 	if (err < 0)
382 		pr_err("%s: cqhci: CQE stuck on\n", mmc_hostname(mmc));
383 	else
384 		pr_debug("%s: cqhci: CQE off\n", mmc_hostname(mmc));
385 
386 	if (cq_host->ops->post_disable)
387 		cq_host->ops->post_disable(mmc);
388 
389 	mmc->cqe_on = false;
390 }
391 
cqhci_disable(struct mmc_host * mmc)392 static void cqhci_disable(struct mmc_host *mmc)
393 {
394 	struct cqhci_host *cq_host = mmc->cqe_private;
395 
396 	if (!cq_host->enabled)
397 		return;
398 
399 	cqhci_off(mmc);
400 
401 	__cqhci_disable(cq_host);
402 
403 	dmam_free_coherent(mmc_dev(mmc), cq_host->data_size,
404 			   cq_host->trans_desc_base,
405 			   cq_host->trans_desc_dma_base);
406 
407 	dmam_free_coherent(mmc_dev(mmc), cq_host->desc_size,
408 			   cq_host->desc_base,
409 			   cq_host->desc_dma_base);
410 
411 	cq_host->trans_desc_base = NULL;
412 	cq_host->desc_base = NULL;
413 
414 	cq_host->enabled = false;
415 }
416 
cqhci_prep_task_desc(struct mmc_request * mrq,struct cqhci_host * cq_host,int tag)417 static void cqhci_prep_task_desc(struct mmc_request *mrq,
418 				 struct cqhci_host *cq_host, int tag)
419 {
420 	__le64 *task_desc = (__le64 __force *)get_desc(cq_host, tag);
421 	u32 req_flags = mrq->data->flags;
422 	u64 desc0;
423 
424 	desc0 = CQHCI_VALID(1) |
425 		CQHCI_END(1) |
426 		CQHCI_INT(1) |
427 		CQHCI_ACT(0x5) |
428 		CQHCI_FORCED_PROG(!!(req_flags & MMC_DATA_FORCED_PRG)) |
429 		CQHCI_DATA_TAG(!!(req_flags & MMC_DATA_DAT_TAG)) |
430 		CQHCI_DATA_DIR(!!(req_flags & MMC_DATA_READ)) |
431 		CQHCI_PRIORITY(!!(req_flags & MMC_DATA_PRIO)) |
432 		CQHCI_QBAR(!!(req_flags & MMC_DATA_QBR)) |
433 		CQHCI_REL_WRITE(!!(req_flags & MMC_DATA_REL_WR)) |
434 		CQHCI_BLK_COUNT(mrq->data->blocks) |
435 		CQHCI_BLK_ADDR((u64)mrq->data->blk_addr);
436 
437 	task_desc[0] = cpu_to_le64(desc0);
438 
439 	if (cq_host->caps & CQHCI_TASK_DESC_SZ_128) {
440 		u64 desc1 = cqhci_crypto_prep_task_desc(mrq);
441 
442 		task_desc[1] = cpu_to_le64(desc1);
443 
444 		pr_debug("%s: cqhci: tag %d task descriptor 0x%016llx%016llx\n",
445 			 mmc_hostname(mrq->host), mrq->tag, desc1, desc0);
446 	} else {
447 		pr_debug("%s: cqhci: tag %d task descriptor 0x%016llx\n",
448 			 mmc_hostname(mrq->host), mrq->tag, desc0);
449 	}
450 }
451 
cqhci_dma_map(struct mmc_host * host,struct mmc_request * mrq)452 static int cqhci_dma_map(struct mmc_host *host, struct mmc_request *mrq)
453 {
454 	int sg_count;
455 	struct mmc_data *data = mrq->data;
456 
457 	if (!data)
458 		return -EINVAL;
459 
460 	sg_count = dma_map_sg(mmc_dev(host), data->sg,
461 			      data->sg_len,
462 			      (data->flags & MMC_DATA_WRITE) ?
463 			      DMA_TO_DEVICE : DMA_FROM_DEVICE);
464 	if (!sg_count) {
465 		pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
466 		return -ENOMEM;
467 	}
468 
469 	return sg_count;
470 }
471 
cqhci_set_tran_desc(u8 * desc,dma_addr_t addr,int len,bool end,bool dma64)472 static void cqhci_set_tran_desc(u8 *desc, dma_addr_t addr, int len, bool end,
473 				bool dma64)
474 {
475 	__le32 *attr = (__le32 __force *)desc;
476 
477 	*attr = (CQHCI_VALID(1) |
478 		 CQHCI_END(end ? 1 : 0) |
479 		 CQHCI_INT(0) |
480 		 CQHCI_ACT(0x4) |
481 		 CQHCI_DAT_LENGTH(len));
482 
483 	if (dma64) {
484 		__le64 *dataddr = (__le64 __force *)(desc + 4);
485 
486 		dataddr[0] = cpu_to_le64(addr);
487 	} else {
488 		__le32 *dataddr = (__le32 __force *)(desc + 4);
489 
490 		dataddr[0] = cpu_to_le32(addr);
491 	}
492 }
493 
cqhci_prep_tran_desc(struct mmc_request * mrq,struct cqhci_host * cq_host,int tag)494 static int cqhci_prep_tran_desc(struct mmc_request *mrq,
495 			       struct cqhci_host *cq_host, int tag)
496 {
497 	struct mmc_data *data = mrq->data;
498 	int i, sg_count, len;
499 	bool end = false;
500 	bool dma64 = cq_host->dma64;
501 	dma_addr_t addr;
502 	u8 *desc;
503 	struct scatterlist *sg;
504 
505 	sg_count = cqhci_dma_map(mrq->host, mrq);
506 	if (sg_count < 0) {
507 		pr_err("%s: %s: unable to map sg lists, %d\n",
508 				mmc_hostname(mrq->host), __func__, sg_count);
509 		return sg_count;
510 	}
511 
512 	desc = get_trans_desc(cq_host, tag);
513 
514 	for_each_sg(data->sg, sg, sg_count, i) {
515 		addr = sg_dma_address(sg);
516 		len = sg_dma_len(sg);
517 
518 		if ((i+1) == sg_count)
519 			end = true;
520 		cqhci_set_tran_desc(desc, addr, len, end, dma64);
521 		desc += cq_host->trans_desc_len;
522 	}
523 
524 	return 0;
525 }
526 
cqhci_prep_dcmd_desc(struct mmc_host * mmc,struct mmc_request * mrq)527 static void cqhci_prep_dcmd_desc(struct mmc_host *mmc,
528 				   struct mmc_request *mrq)
529 {
530 	u64 *task_desc = NULL;
531 	u64 data = 0;
532 	u8 resp_type;
533 	u8 *desc;
534 	__le64 *dataddr;
535 	struct cqhci_host *cq_host = mmc->cqe_private;
536 	u8 timing;
537 
538 	if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
539 		resp_type = 0x0;
540 		timing = 0x1;
541 	} else {
542 		if (mrq->cmd->flags & MMC_RSP_R1B) {
543 			resp_type = 0x3;
544 			timing = 0x0;
545 		} else {
546 			resp_type = 0x2;
547 			timing = 0x1;
548 		}
549 	}
550 
551 	task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
552 	memset(task_desc, 0, cq_host->task_desc_len);
553 	data |= (CQHCI_VALID(1) |
554 		 CQHCI_END(1) |
555 		 CQHCI_INT(1) |
556 		 CQHCI_QBAR(1) |
557 		 CQHCI_ACT(0x5) |
558 		 CQHCI_CMD_INDEX(mrq->cmd->opcode) |
559 		 CQHCI_CMD_TIMING(timing) | CQHCI_RESP_TYPE(resp_type));
560 	if (cq_host->ops->update_dcmd_desc)
561 		cq_host->ops->update_dcmd_desc(mmc, mrq, &data);
562 	*task_desc |= data;
563 	desc = (u8 *)task_desc;
564 	pr_debug("%s: cqhci: dcmd: cmd: %d timing: %d resp: %d\n",
565 		 mmc_hostname(mmc), mrq->cmd->opcode, timing, resp_type);
566 	dataddr = (__le64 __force *)(desc + 4);
567 	dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
568 
569 }
570 
cqhci_post_req(struct mmc_host * host,struct mmc_request * mrq)571 static void cqhci_post_req(struct mmc_host *host, struct mmc_request *mrq)
572 {
573 	struct mmc_data *data = mrq->data;
574 
575 	if (data) {
576 		dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len,
577 			     (data->flags & MMC_DATA_READ) ?
578 			     DMA_FROM_DEVICE : DMA_TO_DEVICE);
579 	}
580 }
581 
cqhci_tag(struct mmc_request * mrq)582 static inline int cqhci_tag(struct mmc_request *mrq)
583 {
584 	return mrq->cmd ? DCMD_SLOT : mrq->tag;
585 }
586 
cqhci_request(struct mmc_host * mmc,struct mmc_request * mrq)587 static int cqhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
588 {
589 	int err = 0;
590 	int tag = cqhci_tag(mrq);
591 	struct cqhci_host *cq_host = mmc->cqe_private;
592 	unsigned long flags;
593 
594 	if (!cq_host->enabled) {
595 		pr_err("%s: cqhci: not enabled\n", mmc_hostname(mmc));
596 		return -EINVAL;
597 	}
598 
599 	/* First request after resume has to re-enable */
600 	if (!cq_host->activated)
601 		__cqhci_enable(cq_host);
602 
603 	if (!mmc->cqe_on) {
604 		if (cq_host->ops->pre_enable)
605 			cq_host->ops->pre_enable(mmc);
606 
607 		cqhci_writel(cq_host, 0, CQHCI_CTL);
608 		mmc->cqe_on = true;
609 		pr_debug("%s: cqhci: CQE on\n", mmc_hostname(mmc));
610 		if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT) {
611 			pr_err("%s: cqhci: CQE failed to exit halt state\n",
612 			       mmc_hostname(mmc));
613 		}
614 		if (cq_host->ops->enable)
615 			cq_host->ops->enable(mmc);
616 	}
617 
618 	if (mrq->data) {
619 		cqhci_prep_task_desc(mrq, cq_host, tag);
620 
621 		err = cqhci_prep_tran_desc(mrq, cq_host, tag);
622 		if (err) {
623 			pr_err("%s: cqhci: failed to setup tx desc: %d\n",
624 			       mmc_hostname(mmc), err);
625 			return err;
626 		}
627 	} else {
628 		cqhci_prep_dcmd_desc(mmc, mrq);
629 	}
630 
631 	spin_lock_irqsave(&cq_host->lock, flags);
632 
633 	if (cq_host->recovery_halt) {
634 		err = -EBUSY;
635 		goto out_unlock;
636 	}
637 
638 	cq_host->slot[tag].mrq = mrq;
639 	cq_host->slot[tag].flags = 0;
640 
641 	cq_host->qcnt += 1;
642 	/* Make sure descriptors are ready before ringing the doorbell */
643 	wmb();
644 	cqhci_writel(cq_host, 1 << tag, CQHCI_TDBR);
645 	if (!(cqhci_readl(cq_host, CQHCI_TDBR) & (1 << tag)))
646 		pr_debug("%s: cqhci: doorbell not set for tag %d\n",
647 			 mmc_hostname(mmc), tag);
648 out_unlock:
649 	spin_unlock_irqrestore(&cq_host->lock, flags);
650 
651 	if (err)
652 		cqhci_post_req(mmc, mrq);
653 
654 	return err;
655 }
656 
cqhci_recovery_needed(struct mmc_host * mmc,struct mmc_request * mrq,bool notify)657 static void cqhci_recovery_needed(struct mmc_host *mmc, struct mmc_request *mrq,
658 				  bool notify)
659 {
660 	struct cqhci_host *cq_host = mmc->cqe_private;
661 
662 	if (!cq_host->recovery_halt) {
663 		cq_host->recovery_halt = true;
664 		pr_debug("%s: cqhci: recovery needed\n", mmc_hostname(mmc));
665 		wake_up(&cq_host->wait_queue);
666 		if (notify && mrq->recovery_notifier)
667 			mrq->recovery_notifier(mrq);
668 	}
669 }
670 
cqhci_error_flags(int error1,int error2)671 static unsigned int cqhci_error_flags(int error1, int error2)
672 {
673 	int error = error1 ? error1 : error2;
674 
675 	switch (error) {
676 	case -EILSEQ:
677 		return CQHCI_HOST_CRC;
678 	case -ETIMEDOUT:
679 		return CQHCI_HOST_TIMEOUT;
680 	default:
681 		return CQHCI_HOST_OTHER;
682 	}
683 }
684 
cqhci_error_irq(struct mmc_host * mmc,u32 status,int cmd_error,int data_error)685 static void cqhci_error_irq(struct mmc_host *mmc, u32 status, int cmd_error,
686 			    int data_error)
687 {
688 	struct cqhci_host *cq_host = mmc->cqe_private;
689 	struct cqhci_slot *slot;
690 	u32 terri;
691 	u32 tdpe;
692 	int tag;
693 
694 	spin_lock(&cq_host->lock);
695 
696 	terri = cqhci_readl(cq_host, CQHCI_TERRI);
697 
698 	pr_debug("%s: cqhci: error IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n",
699 		 mmc_hostname(mmc), status, cmd_error, data_error, terri);
700 
701 	/* Forget about errors when recovery has already been triggered */
702 	if (cq_host->recovery_halt)
703 		goto out_unlock;
704 
705 	if (!cq_host->qcnt) {
706 		WARN_ONCE(1, "%s: cqhci: error when idle. IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n",
707 			  mmc_hostname(mmc), status, cmd_error, data_error,
708 			  terri);
709 		goto out_unlock;
710 	}
711 
712 	if (CQHCI_TERRI_C_VALID(terri)) {
713 		tag = CQHCI_TERRI_C_TASK(terri);
714 		slot = &cq_host->slot[tag];
715 		if (slot->mrq) {
716 			slot->flags = cqhci_error_flags(cmd_error, data_error);
717 			cqhci_recovery_needed(mmc, slot->mrq, true);
718 		}
719 	}
720 
721 	if (CQHCI_TERRI_D_VALID(terri)) {
722 		tag = CQHCI_TERRI_D_TASK(terri);
723 		slot = &cq_host->slot[tag];
724 		if (slot->mrq) {
725 			slot->flags = cqhci_error_flags(data_error, cmd_error);
726 			cqhci_recovery_needed(mmc, slot->mrq, true);
727 		}
728 	}
729 
730 	/*
731 	 * Handle ICCE ("Invalid Crypto Configuration Error").  This should
732 	 * never happen, since the block layer ensures that all crypto-enabled
733 	 * I/O requests have a valid keyslot before they reach the driver.
734 	 *
735 	 * Note that GCE ("General Crypto Error") is different; it already got
736 	 * handled above by checking TERRI.
737 	 */
738 	if (status & CQHCI_IS_ICCE) {
739 		tdpe = cqhci_readl(cq_host, CQHCI_TDPE);
740 		WARN_ONCE(1,
741 			  "%s: cqhci: invalid crypto configuration error. IRQ status: 0x%08x TDPE: 0x%08x\n",
742 			  mmc_hostname(mmc), status, tdpe);
743 		while (tdpe != 0) {
744 			tag = __ffs(tdpe);
745 			tdpe &= ~(1 << tag);
746 			slot = &cq_host->slot[tag];
747 			if (!slot->mrq)
748 				continue;
749 			slot->flags = cqhci_error_flags(data_error, cmd_error);
750 			cqhci_recovery_needed(mmc, slot->mrq, true);
751 		}
752 	}
753 
754 	if (!cq_host->recovery_halt) {
755 		/*
756 		 * The only way to guarantee forward progress is to mark at
757 		 * least one task in error, so if none is indicated, pick one.
758 		 */
759 		for (tag = 0; tag < NUM_SLOTS; tag++) {
760 			slot = &cq_host->slot[tag];
761 			if (!slot->mrq)
762 				continue;
763 			slot->flags = cqhci_error_flags(data_error, cmd_error);
764 			cqhci_recovery_needed(mmc, slot->mrq, true);
765 			break;
766 		}
767 	}
768 
769 out_unlock:
770 	spin_unlock(&cq_host->lock);
771 }
772 
cqhci_finish_mrq(struct mmc_host * mmc,unsigned int tag)773 static void cqhci_finish_mrq(struct mmc_host *mmc, unsigned int tag)
774 {
775 	struct cqhci_host *cq_host = mmc->cqe_private;
776 	struct cqhci_slot *slot = &cq_host->slot[tag];
777 	struct mmc_request *mrq = slot->mrq;
778 	struct mmc_data *data;
779 
780 	if (!mrq) {
781 		WARN_ONCE(1, "%s: cqhci: spurious TCN for tag %d\n",
782 			  mmc_hostname(mmc), tag);
783 		return;
784 	}
785 
786 	/* No completions allowed during recovery */
787 	if (cq_host->recovery_halt) {
788 		slot->flags |= CQHCI_COMPLETED;
789 		return;
790 	}
791 
792 	slot->mrq = NULL;
793 
794 	cq_host->qcnt -= 1;
795 
796 	data = mrq->data;
797 	if (data) {
798 		if (data->error)
799 			data->bytes_xfered = 0;
800 		else
801 			data->bytes_xfered = data->blksz * data->blocks;
802 	}
803 
804 	mmc_cqe_request_done(mmc, mrq);
805 }
806 
cqhci_irq(struct mmc_host * mmc,u32 intmask,int cmd_error,int data_error)807 irqreturn_t cqhci_irq(struct mmc_host *mmc, u32 intmask, int cmd_error,
808 		      int data_error)
809 {
810 	u32 status;
811 	unsigned long tag = 0, comp_status;
812 	struct cqhci_host *cq_host = mmc->cqe_private;
813 
814 	status = cqhci_readl(cq_host, CQHCI_IS);
815 	cqhci_writel(cq_host, status, CQHCI_IS);
816 
817 	pr_debug("%s: cqhci: IRQ status: 0x%08x\n", mmc_hostname(mmc), status);
818 
819 	if ((status & (CQHCI_IS_RED | CQHCI_IS_GCE | CQHCI_IS_ICCE)) ||
820 	    cmd_error || data_error)
821 		cqhci_error_irq(mmc, status, cmd_error, data_error);
822 
823 	if (status & CQHCI_IS_TCC) {
824 		/* read TCN and complete the request */
825 		comp_status = cqhci_readl(cq_host, CQHCI_TCN);
826 		cqhci_writel(cq_host, comp_status, CQHCI_TCN);
827 		pr_debug("%s: cqhci: TCN: 0x%08lx\n",
828 			 mmc_hostname(mmc), comp_status);
829 
830 		spin_lock(&cq_host->lock);
831 
832 		for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
833 			/* complete the corresponding mrq */
834 			pr_debug("%s: cqhci: completing tag %lu\n",
835 				 mmc_hostname(mmc), tag);
836 			cqhci_finish_mrq(mmc, tag);
837 		}
838 
839 		if (cq_host->waiting_for_idle && !cq_host->qcnt) {
840 			cq_host->waiting_for_idle = false;
841 			wake_up(&cq_host->wait_queue);
842 		}
843 
844 		spin_unlock(&cq_host->lock);
845 	}
846 
847 	if (status & CQHCI_IS_TCL)
848 		wake_up(&cq_host->wait_queue);
849 
850 	if (status & CQHCI_IS_HAC)
851 		wake_up(&cq_host->wait_queue);
852 
853 	return IRQ_HANDLED;
854 }
855 EXPORT_SYMBOL(cqhci_irq);
856 
cqhci_is_idle(struct cqhci_host * cq_host,int * ret)857 static bool cqhci_is_idle(struct cqhci_host *cq_host, int *ret)
858 {
859 	unsigned long flags;
860 	bool is_idle;
861 
862 	spin_lock_irqsave(&cq_host->lock, flags);
863 	is_idle = !cq_host->qcnt || cq_host->recovery_halt;
864 	*ret = cq_host->recovery_halt ? -EBUSY : 0;
865 	cq_host->waiting_for_idle = !is_idle;
866 	spin_unlock_irqrestore(&cq_host->lock, flags);
867 
868 	return is_idle;
869 }
870 
cqhci_wait_for_idle(struct mmc_host * mmc)871 static int cqhci_wait_for_idle(struct mmc_host *mmc)
872 {
873 	struct cqhci_host *cq_host = mmc->cqe_private;
874 	int ret;
875 
876 	wait_event(cq_host->wait_queue, cqhci_is_idle(cq_host, &ret));
877 
878 	return ret;
879 }
880 
cqhci_timeout(struct mmc_host * mmc,struct mmc_request * mrq,bool * recovery_needed)881 static bool cqhci_timeout(struct mmc_host *mmc, struct mmc_request *mrq,
882 			  bool *recovery_needed)
883 {
884 	struct cqhci_host *cq_host = mmc->cqe_private;
885 	int tag = cqhci_tag(mrq);
886 	struct cqhci_slot *slot = &cq_host->slot[tag];
887 	unsigned long flags;
888 	bool timed_out;
889 
890 	spin_lock_irqsave(&cq_host->lock, flags);
891 	timed_out = slot->mrq == mrq;
892 	if (timed_out) {
893 		slot->flags |= CQHCI_EXTERNAL_TIMEOUT;
894 		cqhci_recovery_needed(mmc, mrq, false);
895 		*recovery_needed = cq_host->recovery_halt;
896 	}
897 	spin_unlock_irqrestore(&cq_host->lock, flags);
898 
899 	if (timed_out) {
900 		pr_err("%s: cqhci: timeout for tag %d\n",
901 		       mmc_hostname(mmc), tag);
902 		cqhci_dumpregs(cq_host);
903 	}
904 
905 	return timed_out;
906 }
907 
cqhci_tasks_cleared(struct cqhci_host * cq_host)908 static bool cqhci_tasks_cleared(struct cqhci_host *cq_host)
909 {
910 	return !(cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_CLEAR_ALL_TASKS);
911 }
912 
cqhci_clear_all_tasks(struct mmc_host * mmc,unsigned int timeout)913 static bool cqhci_clear_all_tasks(struct mmc_host *mmc, unsigned int timeout)
914 {
915 	struct cqhci_host *cq_host = mmc->cqe_private;
916 	bool ret;
917 	u32 ctl;
918 
919 	cqhci_set_irqs(cq_host, CQHCI_IS_TCL);
920 
921 	ctl = cqhci_readl(cq_host, CQHCI_CTL);
922 	ctl |= CQHCI_CLEAR_ALL_TASKS;
923 	cqhci_writel(cq_host, ctl, CQHCI_CTL);
924 
925 	wait_event_timeout(cq_host->wait_queue, cqhci_tasks_cleared(cq_host),
926 			   msecs_to_jiffies(timeout) + 1);
927 
928 	cqhci_set_irqs(cq_host, 0);
929 
930 	ret = cqhci_tasks_cleared(cq_host);
931 
932 	if (!ret)
933 		pr_warn("%s: cqhci: Failed to clear tasks\n",
934 			mmc_hostname(mmc));
935 
936 	return ret;
937 }
938 
cqhci_halted(struct cqhci_host * cq_host)939 static bool cqhci_halted(struct cqhci_host *cq_host)
940 {
941 	return cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT;
942 }
943 
cqhci_halt(struct mmc_host * mmc,unsigned int timeout)944 static bool cqhci_halt(struct mmc_host *mmc, unsigned int timeout)
945 {
946 	struct cqhci_host *cq_host = mmc->cqe_private;
947 	bool ret;
948 	u32 ctl;
949 
950 	if (cqhci_halted(cq_host))
951 		return true;
952 
953 	cqhci_set_irqs(cq_host, CQHCI_IS_HAC);
954 
955 	ctl = cqhci_readl(cq_host, CQHCI_CTL);
956 	ctl |= CQHCI_HALT;
957 	cqhci_writel(cq_host, ctl, CQHCI_CTL);
958 
959 	wait_event_timeout(cq_host->wait_queue, cqhci_halted(cq_host),
960 			   msecs_to_jiffies(timeout) + 1);
961 
962 	cqhci_set_irqs(cq_host, 0);
963 
964 	ret = cqhci_halted(cq_host);
965 
966 	if (!ret)
967 		pr_warn("%s: cqhci: Failed to halt\n", mmc_hostname(mmc));
968 
969 	return ret;
970 }
971 
972 /*
973  * After halting we expect to be able to use the command line. We interpret the
974  * failure to halt to mean the data lines might still be in use (and the upper
975  * layers will need to send a STOP command), however failing to halt complicates
976  * the recovery, so set a timeout that would reasonably allow I/O to complete.
977  */
978 #define CQHCI_START_HALT_TIMEOUT	500
979 
cqhci_recovery_start(struct mmc_host * mmc)980 static void cqhci_recovery_start(struct mmc_host *mmc)
981 {
982 	struct cqhci_host *cq_host = mmc->cqe_private;
983 
984 	pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__);
985 
986 	WARN_ON(!cq_host->recovery_halt);
987 
988 	cqhci_halt(mmc, CQHCI_START_HALT_TIMEOUT);
989 
990 	if (cq_host->ops->disable)
991 		cq_host->ops->disable(mmc, true);
992 
993 	mmc->cqe_on = false;
994 }
995 
cqhci_error_from_flags(unsigned int flags)996 static int cqhci_error_from_flags(unsigned int flags)
997 {
998 	if (!flags)
999 		return 0;
1000 
1001 	/* CRC errors might indicate re-tuning so prefer to report that */
1002 	if (flags & CQHCI_HOST_CRC)
1003 		return -EILSEQ;
1004 
1005 	if (flags & (CQHCI_EXTERNAL_TIMEOUT | CQHCI_HOST_TIMEOUT))
1006 		return -ETIMEDOUT;
1007 
1008 	return -EIO;
1009 }
1010 
cqhci_recover_mrq(struct cqhci_host * cq_host,unsigned int tag)1011 static void cqhci_recover_mrq(struct cqhci_host *cq_host, unsigned int tag)
1012 {
1013 	struct cqhci_slot *slot = &cq_host->slot[tag];
1014 	struct mmc_request *mrq = slot->mrq;
1015 	struct mmc_data *data;
1016 
1017 	if (!mrq)
1018 		return;
1019 
1020 	slot->mrq = NULL;
1021 
1022 	cq_host->qcnt -= 1;
1023 
1024 	data = mrq->data;
1025 	if (data) {
1026 		data->bytes_xfered = 0;
1027 		data->error = cqhci_error_from_flags(slot->flags);
1028 	} else {
1029 		mrq->cmd->error = cqhci_error_from_flags(slot->flags);
1030 	}
1031 
1032 	mmc_cqe_request_done(cq_host->mmc, mrq);
1033 }
1034 
cqhci_recover_mrqs(struct cqhci_host * cq_host)1035 static void cqhci_recover_mrqs(struct cqhci_host *cq_host)
1036 {
1037 	int i;
1038 
1039 	for (i = 0; i < cq_host->num_slots; i++)
1040 		cqhci_recover_mrq(cq_host, i);
1041 }
1042 
1043 /*
1044  * By now the command and data lines should be unused so there is no reason for
1045  * CQHCI to take a long time to halt, but if it doesn't halt there could be
1046  * problems clearing tasks, so be generous.
1047  */
1048 #define CQHCI_FINISH_HALT_TIMEOUT	20
1049 
1050 /* CQHCI could be expected to clear it's internal state pretty quickly */
1051 #define CQHCI_CLEAR_TIMEOUT		20
1052 
cqhci_recovery_finish(struct mmc_host * mmc)1053 static void cqhci_recovery_finish(struct mmc_host *mmc)
1054 {
1055 	struct cqhci_host *cq_host = mmc->cqe_private;
1056 	unsigned long flags;
1057 	u32 cqcfg;
1058 	bool ok;
1059 
1060 	pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__);
1061 
1062 	WARN_ON(!cq_host->recovery_halt);
1063 
1064 	ok = cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT);
1065 
1066 	/*
1067 	 * The specification contradicts itself, by saying that tasks cannot be
1068 	 * cleared if CQHCI does not halt, but if CQHCI does not halt, it should
1069 	 * be disabled/re-enabled, but not to disable before clearing tasks.
1070 	 * Have a go anyway.
1071 	 */
1072 	if (!cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT))
1073 		ok = false;
1074 
1075 	/* Disable to make sure tasks really are cleared */
1076 	cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
1077 	cqcfg &= ~CQHCI_ENABLE;
1078 	cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
1079 
1080 	cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
1081 	cqcfg |= CQHCI_ENABLE;
1082 	cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
1083 
1084 	cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT);
1085 
1086 	if (!ok)
1087 		cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT);
1088 
1089 	cqhci_recover_mrqs(cq_host);
1090 
1091 	WARN_ON(cq_host->qcnt);
1092 
1093 	spin_lock_irqsave(&cq_host->lock, flags);
1094 	cq_host->qcnt = 0;
1095 	cq_host->recovery_halt = false;
1096 	mmc->cqe_on = false;
1097 	spin_unlock_irqrestore(&cq_host->lock, flags);
1098 
1099 	/* Ensure all writes are done before interrupts are re-enabled */
1100 	wmb();
1101 
1102 	cqhci_writel(cq_host, CQHCI_IS_HAC | CQHCI_IS_TCL, CQHCI_IS);
1103 
1104 	cqhci_set_irqs(cq_host, CQHCI_IS_MASK);
1105 
1106 	pr_debug("%s: cqhci: recovery done\n", mmc_hostname(mmc));
1107 }
1108 
1109 static const struct mmc_cqe_ops cqhci_cqe_ops = {
1110 	.cqe_enable = cqhci_enable,
1111 	.cqe_disable = cqhci_disable,
1112 	.cqe_request = cqhci_request,
1113 	.cqe_post_req = cqhci_post_req,
1114 	.cqe_off = cqhci_off,
1115 	.cqe_wait_for_idle = cqhci_wait_for_idle,
1116 	.cqe_timeout = cqhci_timeout,
1117 	.cqe_recovery_start = cqhci_recovery_start,
1118 	.cqe_recovery_finish = cqhci_recovery_finish,
1119 };
1120 
cqhci_pltfm_init(struct platform_device * pdev)1121 struct cqhci_host *cqhci_pltfm_init(struct platform_device *pdev)
1122 {
1123 	struct cqhci_host *cq_host;
1124 	struct resource *cqhci_memres = NULL;
1125 
1126 	/* check and setup CMDQ interface */
1127 	cqhci_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1128 						   "cqhci");
1129 	if (!cqhci_memres) {
1130 		dev_dbg(&pdev->dev, "CMDQ not supported\n");
1131 		return ERR_PTR(-EINVAL);
1132 	}
1133 
1134 	cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
1135 	if (!cq_host)
1136 		return ERR_PTR(-ENOMEM);
1137 	cq_host->mmio = devm_ioremap(&pdev->dev,
1138 				     cqhci_memres->start,
1139 				     resource_size(cqhci_memres));
1140 	if (!cq_host->mmio) {
1141 		dev_err(&pdev->dev, "failed to remap cqhci regs\n");
1142 		return ERR_PTR(-EBUSY);
1143 	}
1144 	dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
1145 
1146 	return cq_host;
1147 }
1148 EXPORT_SYMBOL(cqhci_pltfm_init);
1149 
cqhci_ver_major(struct cqhci_host * cq_host)1150 static unsigned int cqhci_ver_major(struct cqhci_host *cq_host)
1151 {
1152 	return CQHCI_VER_MAJOR(cqhci_readl(cq_host, CQHCI_VER));
1153 }
1154 
cqhci_ver_minor(struct cqhci_host * cq_host)1155 static unsigned int cqhci_ver_minor(struct cqhci_host *cq_host)
1156 {
1157 	u32 ver = cqhci_readl(cq_host, CQHCI_VER);
1158 
1159 	return CQHCI_VER_MINOR1(ver) * 10 + CQHCI_VER_MINOR2(ver);
1160 }
1161 
cqhci_init(struct cqhci_host * cq_host,struct mmc_host * mmc,bool dma64)1162 int cqhci_init(struct cqhci_host *cq_host, struct mmc_host *mmc,
1163 	      bool dma64)
1164 {
1165 	int err;
1166 
1167 	cq_host->dma64 = dma64;
1168 	cq_host->mmc = mmc;
1169 	cq_host->mmc->cqe_private = cq_host;
1170 
1171 	cq_host->num_slots = NUM_SLOTS;
1172 	cq_host->dcmd_slot = DCMD_SLOT;
1173 
1174 	mmc->cqe_ops = &cqhci_cqe_ops;
1175 
1176 	mmc->cqe_qdepth = NUM_SLOTS;
1177 	if (mmc->caps2 & MMC_CAP2_CQE_DCMD)
1178 		mmc->cqe_qdepth -= 1;
1179 
1180 	cq_host->slot = devm_kcalloc(mmc_dev(mmc), cq_host->num_slots,
1181 				     sizeof(*cq_host->slot), GFP_KERNEL);
1182 	if (!cq_host->slot) {
1183 		err = -ENOMEM;
1184 		goto out_err;
1185 	}
1186 
1187 	err = cqhci_crypto_init(cq_host);
1188 	if (err) {
1189 		pr_err("%s: CQHCI crypto initialization failed\n",
1190 		       mmc_hostname(mmc));
1191 		goto out_err;
1192 	}
1193 
1194 	spin_lock_init(&cq_host->lock);
1195 
1196 	init_completion(&cq_host->halt_comp);
1197 	init_waitqueue_head(&cq_host->wait_queue);
1198 
1199 	pr_info("%s: CQHCI version %u.%02u\n",
1200 		mmc_hostname(mmc), cqhci_ver_major(cq_host),
1201 		cqhci_ver_minor(cq_host));
1202 
1203 	return 0;
1204 
1205 out_err:
1206 	pr_err("%s: CQHCI version %u.%02u failed to initialize, error %d\n",
1207 	       mmc_hostname(mmc), cqhci_ver_major(cq_host),
1208 	       cqhci_ver_minor(cq_host), err);
1209 	return err;
1210 }
1211 EXPORT_SYMBOL(cqhci_init);
1212 
1213 MODULE_AUTHOR("Venkat Gopalakrishnan <venkatg@codeaurora.org>");
1214 MODULE_DESCRIPTION("Command Queue Host Controller Interface driver");
1215 MODULE_LICENSE("GPL v2");
1216