cqhci-core.c 30 KB

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