cqhci.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  1. /* 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. #include <linux/delay.h>
  13. #include <linux/highmem.h>
  14. #include <linux/io.h>
  15. #include <linux/iopoll.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/ktime.h>
  22. #include <linux/mmc/mmc.h>
  23. #include <linux/mmc/host.h>
  24. #include <linux/mmc/card.h>
  25. #include "cqhci.h"
  26. #define DCMD_SLOT 31
  27. #define NUM_SLOTS 32
  28. struct cqhci_slot {
  29. struct mmc_request *mrq;
  30. unsigned int flags;
  31. #define CQHCI_EXTERNAL_TIMEOUT BIT(0)
  32. #define CQHCI_COMPLETED BIT(1)
  33. #define CQHCI_HOST_CRC BIT(2)
  34. #define CQHCI_HOST_TIMEOUT BIT(3)
  35. #define CQHCI_HOST_OTHER BIT(4)
  36. };
  37. static inline u8 *get_desc(struct cqhci_host *cq_host, u8 tag)
  38. {
  39. return cq_host->desc_base + (tag * cq_host->slot_sz);
  40. }
  41. static inline u8 *get_link_desc(struct cqhci_host *cq_host, u8 tag)
  42. {
  43. u8 *desc = get_desc(cq_host, tag);
  44. return desc + cq_host->task_desc_len;
  45. }
  46. static inline dma_addr_t get_trans_desc_dma(struct cqhci_host *cq_host, u8 tag)
  47. {
  48. return cq_host->trans_desc_dma_base +
  49. (cq_host->mmc->max_segs * tag *
  50. cq_host->trans_desc_len);
  51. }
  52. static inline u8 *get_trans_desc(struct cqhci_host *cq_host, u8 tag)
  53. {
  54. return cq_host->trans_desc_base +
  55. (cq_host->trans_desc_len * cq_host->mmc->max_segs * tag);
  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 descritors
  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 = cq_host->trans_desc_len * cq_host->mmc->max_segs *
  174. cq_host->mmc->cqe_qdepth;
  175. pr_debug("%s: cqhci: desc_size: %zu data_sz: %zu slot-sz: %d\n",
  176. mmc_hostname(cq_host->mmc), cq_host->desc_size, cq_host->data_size,
  177. cq_host->slot_sz);
  178. /*
  179. * allocate a dma-mapped chunk of memory for the descriptors
  180. * allocate a dma-mapped chunk of memory for link descriptors
  181. * setup each link-desc memory offset per slot-number to
  182. * the descriptor table.
  183. */
  184. cq_host->desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
  185. cq_host->desc_size,
  186. &cq_host->desc_dma_base,
  187. GFP_KERNEL);
  188. if (!cq_host->desc_base)
  189. return -ENOMEM;
  190. cq_host->trans_desc_base = dmam_alloc_coherent(mmc_dev(cq_host->mmc),
  191. cq_host->data_size,
  192. &cq_host->trans_desc_dma_base,
  193. GFP_KERNEL);
  194. if (!cq_host->trans_desc_base) {
  195. dmam_free_coherent(mmc_dev(cq_host->mmc), cq_host->desc_size,
  196. cq_host->desc_base,
  197. cq_host->desc_dma_base);
  198. cq_host->desc_base = NULL;
  199. cq_host->desc_dma_base = 0;
  200. return -ENOMEM;
  201. }
  202. pr_debug("%s: cqhci: desc-base: 0x%p trans-base: 0x%p\n desc_dma 0x%llx trans_dma: 0x%llx\n",
  203. mmc_hostname(cq_host->mmc), cq_host->desc_base, cq_host->trans_desc_base,
  204. (unsigned long long)cq_host->desc_dma_base,
  205. (unsigned long long)cq_host->trans_desc_dma_base);
  206. for (; i < (cq_host->num_slots); i++)
  207. setup_trans_desc(cq_host, i);
  208. return 0;
  209. }
  210. static void __cqhci_enable(struct cqhci_host *cq_host)
  211. {
  212. struct mmc_host *mmc = cq_host->mmc;
  213. u32 cqcfg;
  214. cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
  215. /* Configuration must not be changed while enabled */
  216. if (cqcfg & CQHCI_ENABLE) {
  217. cqcfg &= ~CQHCI_ENABLE;
  218. cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
  219. }
  220. cqcfg &= ~(CQHCI_DCMD | CQHCI_TASK_DESC_SZ);
  221. if (mmc->caps2 & MMC_CAP2_CQE_DCMD)
  222. cqcfg |= CQHCI_DCMD;
  223. if (cq_host->caps & CQHCI_TASK_DESC_SZ_128)
  224. cqcfg |= CQHCI_TASK_DESC_SZ;
  225. cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
  226. cqhci_writel(cq_host, lower_32_bits(cq_host->desc_dma_base),
  227. CQHCI_TDLBA);
  228. cqhci_writel(cq_host, upper_32_bits(cq_host->desc_dma_base),
  229. CQHCI_TDLBAU);
  230. cqhci_writel(cq_host, cq_host->rca, CQHCI_SSC2);
  231. cqhci_set_irqs(cq_host, 0);
  232. cqcfg |= CQHCI_ENABLE;
  233. cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
  234. mmc->cqe_on = true;
  235. if (cq_host->ops->enable)
  236. cq_host->ops->enable(mmc);
  237. /* Ensure all writes are done before interrupts are enabled */
  238. wmb();
  239. cqhci_set_irqs(cq_host, CQHCI_IS_MASK);
  240. cq_host->activated = true;
  241. }
  242. static void __cqhci_disable(struct cqhci_host *cq_host)
  243. {
  244. u32 cqcfg;
  245. cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
  246. cqcfg &= ~CQHCI_ENABLE;
  247. cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
  248. cq_host->mmc->cqe_on = false;
  249. cq_host->activated = false;
  250. }
  251. int cqhci_suspend(struct mmc_host *mmc)
  252. {
  253. struct cqhci_host *cq_host = mmc->cqe_private;
  254. if (cq_host->enabled)
  255. __cqhci_disable(cq_host);
  256. return 0;
  257. }
  258. EXPORT_SYMBOL(cqhci_suspend);
  259. int cqhci_resume(struct mmc_host *mmc)
  260. {
  261. /* Re-enable is done upon first request */
  262. return 0;
  263. }
  264. EXPORT_SYMBOL(cqhci_resume);
  265. static int cqhci_enable(struct mmc_host *mmc, struct mmc_card *card)
  266. {
  267. struct cqhci_host *cq_host = mmc->cqe_private;
  268. int err;
  269. if (cq_host->enabled)
  270. return 0;
  271. cq_host->rca = card->rca;
  272. err = cqhci_host_alloc_tdl(cq_host);
  273. if (err)
  274. return err;
  275. __cqhci_enable(cq_host);
  276. cq_host->enabled = true;
  277. #ifdef DEBUG
  278. cqhci_dumpregs(cq_host);
  279. #endif
  280. return 0;
  281. }
  282. /* CQHCI is idle and should halt immediately, so set a small timeout */
  283. #define CQHCI_OFF_TIMEOUT 100
  284. static u32 cqhci_read_ctl(struct cqhci_host *cq_host)
  285. {
  286. return cqhci_readl(cq_host, CQHCI_CTL);
  287. }
  288. static void cqhci_off(struct mmc_host *mmc)
  289. {
  290. struct cqhci_host *cq_host = mmc->cqe_private;
  291. u32 reg;
  292. int err;
  293. if (!cq_host->enabled || !mmc->cqe_on || cq_host->recovery_halt)
  294. return;
  295. if (cq_host->ops->disable)
  296. cq_host->ops->disable(mmc, false);
  297. cqhci_writel(cq_host, CQHCI_HALT, CQHCI_CTL);
  298. err = readx_poll_timeout(cqhci_read_ctl, cq_host, reg,
  299. reg & CQHCI_HALT, 0, CQHCI_OFF_TIMEOUT);
  300. if (err < 0)
  301. pr_err("%s: cqhci: CQE stuck on\n", mmc_hostname(mmc));
  302. else
  303. pr_debug("%s: cqhci: CQE off\n", mmc_hostname(mmc));
  304. mmc->cqe_on = false;
  305. }
  306. static void cqhci_disable(struct mmc_host *mmc)
  307. {
  308. struct cqhci_host *cq_host = mmc->cqe_private;
  309. if (!cq_host->enabled)
  310. return;
  311. cqhci_off(mmc);
  312. __cqhci_disable(cq_host);
  313. dmam_free_coherent(mmc_dev(mmc), cq_host->data_size,
  314. cq_host->trans_desc_base,
  315. cq_host->trans_desc_dma_base);
  316. dmam_free_coherent(mmc_dev(mmc), cq_host->desc_size,
  317. cq_host->desc_base,
  318. cq_host->desc_dma_base);
  319. cq_host->trans_desc_base = NULL;
  320. cq_host->desc_base = NULL;
  321. cq_host->enabled = false;
  322. }
  323. static void cqhci_prep_task_desc(struct mmc_request *mrq,
  324. u64 *data, bool intr)
  325. {
  326. u32 req_flags = mrq->data->flags;
  327. *data = CQHCI_VALID(1) |
  328. CQHCI_END(1) |
  329. CQHCI_INT(intr) |
  330. CQHCI_ACT(0x5) |
  331. CQHCI_FORCED_PROG(!!(req_flags & MMC_DATA_FORCED_PRG)) |
  332. CQHCI_DATA_TAG(!!(req_flags & MMC_DATA_DAT_TAG)) |
  333. CQHCI_DATA_DIR(!!(req_flags & MMC_DATA_READ)) |
  334. CQHCI_PRIORITY(!!(req_flags & MMC_DATA_PRIO)) |
  335. CQHCI_QBAR(!!(req_flags & MMC_DATA_QBR)) |
  336. CQHCI_REL_WRITE(!!(req_flags & MMC_DATA_REL_WR)) |
  337. CQHCI_BLK_COUNT(mrq->data->blocks) |
  338. CQHCI_BLK_ADDR((u64)mrq->data->blk_addr);
  339. pr_debug("%s: cqhci: tag %d task descriptor 0x016%llx\n",
  340. mmc_hostname(mrq->host), mrq->tag, (unsigned long long)*data);
  341. }
  342. static int cqhci_dma_map(struct mmc_host *host, struct mmc_request *mrq)
  343. {
  344. int sg_count;
  345. struct mmc_data *data = mrq->data;
  346. if (!data)
  347. return -EINVAL;
  348. sg_count = dma_map_sg(mmc_dev(host), data->sg,
  349. data->sg_len,
  350. (data->flags & MMC_DATA_WRITE) ?
  351. DMA_TO_DEVICE : DMA_FROM_DEVICE);
  352. if (!sg_count) {
  353. pr_err("%s: sg-len: %d\n", __func__, data->sg_len);
  354. return -ENOMEM;
  355. }
  356. return sg_count;
  357. }
  358. static void cqhci_set_tran_desc(u8 *desc, dma_addr_t addr, int len, bool end,
  359. bool dma64)
  360. {
  361. __le32 *attr = (__le32 __force *)desc;
  362. *attr = (CQHCI_VALID(1) |
  363. CQHCI_END(end ? 1 : 0) |
  364. CQHCI_INT(0) |
  365. CQHCI_ACT(0x4) |
  366. CQHCI_DAT_LENGTH(len));
  367. if (dma64) {
  368. __le64 *dataddr = (__le64 __force *)(desc + 4);
  369. dataddr[0] = cpu_to_le64(addr);
  370. } else {
  371. __le32 *dataddr = (__le32 __force *)(desc + 4);
  372. dataddr[0] = cpu_to_le32(addr);
  373. }
  374. }
  375. static int cqhci_prep_tran_desc(struct mmc_request *mrq,
  376. struct cqhci_host *cq_host, int tag)
  377. {
  378. struct mmc_data *data = mrq->data;
  379. int i, sg_count, len;
  380. bool end = false;
  381. bool dma64 = cq_host->dma64;
  382. dma_addr_t addr;
  383. u8 *desc;
  384. struct scatterlist *sg;
  385. sg_count = cqhci_dma_map(mrq->host, mrq);
  386. if (sg_count < 0) {
  387. pr_err("%s: %s: unable to map sg lists, %d\n",
  388. mmc_hostname(mrq->host), __func__, sg_count);
  389. return sg_count;
  390. }
  391. desc = get_trans_desc(cq_host, tag);
  392. for_each_sg(data->sg, sg, sg_count, i) {
  393. addr = sg_dma_address(sg);
  394. len = sg_dma_len(sg);
  395. if ((i+1) == sg_count)
  396. end = true;
  397. cqhci_set_tran_desc(desc, addr, len, end, dma64);
  398. desc += cq_host->trans_desc_len;
  399. }
  400. return 0;
  401. }
  402. static void cqhci_prep_dcmd_desc(struct mmc_host *mmc,
  403. struct mmc_request *mrq)
  404. {
  405. u64 *task_desc = NULL;
  406. u64 data = 0;
  407. u8 resp_type;
  408. u8 *desc;
  409. __le64 *dataddr;
  410. struct cqhci_host *cq_host = mmc->cqe_private;
  411. u8 timing;
  412. if (!(mrq->cmd->flags & MMC_RSP_PRESENT)) {
  413. resp_type = 0x0;
  414. timing = 0x1;
  415. } else {
  416. if (mrq->cmd->flags & MMC_RSP_R1B) {
  417. resp_type = 0x3;
  418. timing = 0x0;
  419. } else {
  420. resp_type = 0x2;
  421. timing = 0x1;
  422. }
  423. }
  424. task_desc = (__le64 __force *)get_desc(cq_host, cq_host->dcmd_slot);
  425. memset(task_desc, 0, cq_host->task_desc_len);
  426. data |= (CQHCI_VALID(1) |
  427. CQHCI_END(1) |
  428. CQHCI_INT(1) |
  429. CQHCI_QBAR(1) |
  430. CQHCI_ACT(0x5) |
  431. CQHCI_CMD_INDEX(mrq->cmd->opcode) |
  432. CQHCI_CMD_TIMING(timing) | CQHCI_RESP_TYPE(resp_type));
  433. *task_desc |= data;
  434. desc = (u8 *)task_desc;
  435. pr_debug("%s: cqhci: dcmd: cmd: %d timing: %d resp: %d\n",
  436. mmc_hostname(mmc), mrq->cmd->opcode, timing, resp_type);
  437. dataddr = (__le64 __force *)(desc + 4);
  438. dataddr[0] = cpu_to_le64((u64)mrq->cmd->arg);
  439. }
  440. static void cqhci_post_req(struct mmc_host *host, struct mmc_request *mrq)
  441. {
  442. struct mmc_data *data = mrq->data;
  443. if (data) {
  444. dma_unmap_sg(mmc_dev(host), data->sg, data->sg_len,
  445. (data->flags & MMC_DATA_READ) ?
  446. DMA_FROM_DEVICE : DMA_TO_DEVICE);
  447. }
  448. }
  449. static inline int cqhci_tag(struct mmc_request *mrq)
  450. {
  451. return mrq->cmd ? DCMD_SLOT : mrq->tag;
  452. }
  453. static int cqhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  454. {
  455. int err = 0;
  456. u64 data = 0;
  457. u64 *task_desc = NULL;
  458. int tag = cqhci_tag(mrq);
  459. struct cqhci_host *cq_host = mmc->cqe_private;
  460. unsigned long flags;
  461. if (!cq_host->enabled) {
  462. pr_err("%s: cqhci: not enabled\n", mmc_hostname(mmc));
  463. return -EINVAL;
  464. }
  465. /* First request after resume has to re-enable */
  466. if (!cq_host->activated)
  467. __cqhci_enable(cq_host);
  468. if (!mmc->cqe_on) {
  469. cqhci_writel(cq_host, 0, CQHCI_CTL);
  470. mmc->cqe_on = true;
  471. pr_debug("%s: cqhci: CQE on\n", mmc_hostname(mmc));
  472. if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT) {
  473. pr_err("%s: cqhci: CQE failed to exit halt state\n",
  474. mmc_hostname(mmc));
  475. }
  476. if (cq_host->ops->enable)
  477. cq_host->ops->enable(mmc);
  478. }
  479. if (mrq->data) {
  480. task_desc = (__le64 __force *)get_desc(cq_host, tag);
  481. cqhci_prep_task_desc(mrq, &data, 1);
  482. *task_desc = cpu_to_le64(data);
  483. err = cqhci_prep_tran_desc(mrq, cq_host, tag);
  484. if (err) {
  485. pr_err("%s: cqhci: failed to setup tx desc: %d\n",
  486. mmc_hostname(mmc), err);
  487. return err;
  488. }
  489. } else {
  490. cqhci_prep_dcmd_desc(mmc, mrq);
  491. }
  492. spin_lock_irqsave(&cq_host->lock, flags);
  493. if (cq_host->recovery_halt) {
  494. err = -EBUSY;
  495. goto out_unlock;
  496. }
  497. cq_host->slot[tag].mrq = mrq;
  498. cq_host->slot[tag].flags = 0;
  499. cq_host->qcnt += 1;
  500. /* Make sure descriptors are ready before ringing the doorbell */
  501. wmb();
  502. cqhci_writel(cq_host, 1 << tag, CQHCI_TDBR);
  503. if (!(cqhci_readl(cq_host, CQHCI_TDBR) & (1 << tag)))
  504. pr_debug("%s: cqhci: doorbell not set for tag %d\n",
  505. mmc_hostname(mmc), tag);
  506. out_unlock:
  507. spin_unlock_irqrestore(&cq_host->lock, flags);
  508. if (err)
  509. cqhci_post_req(mmc, mrq);
  510. return err;
  511. }
  512. static void cqhci_recovery_needed(struct mmc_host *mmc, struct mmc_request *mrq,
  513. bool notify)
  514. {
  515. struct cqhci_host *cq_host = mmc->cqe_private;
  516. if (!cq_host->recovery_halt) {
  517. cq_host->recovery_halt = true;
  518. pr_debug("%s: cqhci: recovery needed\n", mmc_hostname(mmc));
  519. wake_up(&cq_host->wait_queue);
  520. if (notify && mrq->recovery_notifier)
  521. mrq->recovery_notifier(mrq);
  522. }
  523. }
  524. static unsigned int cqhci_error_flags(int error1, int error2)
  525. {
  526. int error = error1 ? error1 : error2;
  527. switch (error) {
  528. case -EILSEQ:
  529. return CQHCI_HOST_CRC;
  530. case -ETIMEDOUT:
  531. return CQHCI_HOST_TIMEOUT;
  532. default:
  533. return CQHCI_HOST_OTHER;
  534. }
  535. }
  536. static void cqhci_error_irq(struct mmc_host *mmc, u32 status, int cmd_error,
  537. int data_error)
  538. {
  539. struct cqhci_host *cq_host = mmc->cqe_private;
  540. struct cqhci_slot *slot;
  541. u32 terri;
  542. int tag;
  543. spin_lock(&cq_host->lock);
  544. terri = cqhci_readl(cq_host, CQHCI_TERRI);
  545. pr_debug("%s: cqhci: error IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n",
  546. mmc_hostname(mmc), status, cmd_error, data_error, terri);
  547. /* Forget about errors when recovery has already been triggered */
  548. if (cq_host->recovery_halt)
  549. goto out_unlock;
  550. if (!cq_host->qcnt) {
  551. WARN_ONCE(1, "%s: cqhci: error when idle. IRQ status: 0x%08x cmd error %d data error %d TERRI: 0x%08x\n",
  552. mmc_hostname(mmc), status, cmd_error, data_error,
  553. terri);
  554. goto out_unlock;
  555. }
  556. if (CQHCI_TERRI_C_VALID(terri)) {
  557. tag = CQHCI_TERRI_C_TASK(terri);
  558. slot = &cq_host->slot[tag];
  559. if (slot->mrq) {
  560. slot->flags = cqhci_error_flags(cmd_error, data_error);
  561. cqhci_recovery_needed(mmc, slot->mrq, true);
  562. }
  563. }
  564. if (CQHCI_TERRI_D_VALID(terri)) {
  565. tag = CQHCI_TERRI_D_TASK(terri);
  566. slot = &cq_host->slot[tag];
  567. if (slot->mrq) {
  568. slot->flags = cqhci_error_flags(data_error, cmd_error);
  569. cqhci_recovery_needed(mmc, slot->mrq, true);
  570. }
  571. }
  572. if (!cq_host->recovery_halt) {
  573. /*
  574. * The only way to guarantee forward progress is to mark at
  575. * least one task in error, so if none is indicated, pick one.
  576. */
  577. for (tag = 0; tag < NUM_SLOTS; tag++) {
  578. slot = &cq_host->slot[tag];
  579. if (!slot->mrq)
  580. continue;
  581. slot->flags = cqhci_error_flags(data_error, cmd_error);
  582. cqhci_recovery_needed(mmc, slot->mrq, true);
  583. break;
  584. }
  585. }
  586. out_unlock:
  587. spin_unlock(&cq_host->lock);
  588. }
  589. static void cqhci_finish_mrq(struct mmc_host *mmc, unsigned int tag)
  590. {
  591. struct cqhci_host *cq_host = mmc->cqe_private;
  592. struct cqhci_slot *slot = &cq_host->slot[tag];
  593. struct mmc_request *mrq = slot->mrq;
  594. struct mmc_data *data;
  595. if (!mrq) {
  596. WARN_ONCE(1, "%s: cqhci: spurious TCN for tag %d\n",
  597. mmc_hostname(mmc), tag);
  598. return;
  599. }
  600. /* No completions allowed during recovery */
  601. if (cq_host->recovery_halt) {
  602. slot->flags |= CQHCI_COMPLETED;
  603. return;
  604. }
  605. slot->mrq = NULL;
  606. cq_host->qcnt -= 1;
  607. data = mrq->data;
  608. if (data) {
  609. if (data->error)
  610. data->bytes_xfered = 0;
  611. else
  612. data->bytes_xfered = data->blksz * data->blocks;
  613. }
  614. mmc_cqe_request_done(mmc, mrq);
  615. }
  616. irqreturn_t cqhci_irq(struct mmc_host *mmc, u32 intmask, int cmd_error,
  617. int data_error)
  618. {
  619. u32 status;
  620. unsigned long tag = 0, comp_status;
  621. struct cqhci_host *cq_host = mmc->cqe_private;
  622. status = cqhci_readl(cq_host, CQHCI_IS);
  623. cqhci_writel(cq_host, status, CQHCI_IS);
  624. pr_debug("%s: cqhci: IRQ status: 0x%08x\n", mmc_hostname(mmc), status);
  625. if ((status & CQHCI_IS_RED) || cmd_error || data_error)
  626. cqhci_error_irq(mmc, status, cmd_error, data_error);
  627. if (status & CQHCI_IS_TCC) {
  628. /* read TCN and complete the request */
  629. comp_status = cqhci_readl(cq_host, CQHCI_TCN);
  630. cqhci_writel(cq_host, comp_status, CQHCI_TCN);
  631. pr_debug("%s: cqhci: TCN: 0x%08lx\n",
  632. mmc_hostname(mmc), comp_status);
  633. spin_lock(&cq_host->lock);
  634. for_each_set_bit(tag, &comp_status, cq_host->num_slots) {
  635. /* complete the corresponding mrq */
  636. pr_debug("%s: cqhci: completing tag %lu\n",
  637. mmc_hostname(mmc), tag);
  638. cqhci_finish_mrq(mmc, tag);
  639. }
  640. if (cq_host->waiting_for_idle && !cq_host->qcnt) {
  641. cq_host->waiting_for_idle = false;
  642. wake_up(&cq_host->wait_queue);
  643. }
  644. spin_unlock(&cq_host->lock);
  645. }
  646. if (status & CQHCI_IS_TCL)
  647. wake_up(&cq_host->wait_queue);
  648. if (status & CQHCI_IS_HAC)
  649. wake_up(&cq_host->wait_queue);
  650. return IRQ_HANDLED;
  651. }
  652. EXPORT_SYMBOL(cqhci_irq);
  653. static bool cqhci_is_idle(struct cqhci_host *cq_host, int *ret)
  654. {
  655. unsigned long flags;
  656. bool is_idle;
  657. spin_lock_irqsave(&cq_host->lock, flags);
  658. is_idle = !cq_host->qcnt || cq_host->recovery_halt;
  659. *ret = cq_host->recovery_halt ? -EBUSY : 0;
  660. cq_host->waiting_for_idle = !is_idle;
  661. spin_unlock_irqrestore(&cq_host->lock, flags);
  662. return is_idle;
  663. }
  664. static int cqhci_wait_for_idle(struct mmc_host *mmc)
  665. {
  666. struct cqhci_host *cq_host = mmc->cqe_private;
  667. int ret;
  668. wait_event(cq_host->wait_queue, cqhci_is_idle(cq_host, &ret));
  669. return ret;
  670. }
  671. static bool cqhci_timeout(struct mmc_host *mmc, struct mmc_request *mrq,
  672. bool *recovery_needed)
  673. {
  674. struct cqhci_host *cq_host = mmc->cqe_private;
  675. int tag = cqhci_tag(mrq);
  676. struct cqhci_slot *slot = &cq_host->slot[tag];
  677. unsigned long flags;
  678. bool timed_out;
  679. spin_lock_irqsave(&cq_host->lock, flags);
  680. timed_out = slot->mrq == mrq;
  681. if (timed_out) {
  682. slot->flags |= CQHCI_EXTERNAL_TIMEOUT;
  683. cqhci_recovery_needed(mmc, mrq, false);
  684. *recovery_needed = cq_host->recovery_halt;
  685. }
  686. spin_unlock_irqrestore(&cq_host->lock, flags);
  687. if (timed_out) {
  688. pr_err("%s: cqhci: timeout for tag %d\n",
  689. mmc_hostname(mmc), tag);
  690. cqhci_dumpregs(cq_host);
  691. }
  692. return timed_out;
  693. }
  694. static bool cqhci_tasks_cleared(struct cqhci_host *cq_host)
  695. {
  696. return !(cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_CLEAR_ALL_TASKS);
  697. }
  698. static bool cqhci_clear_all_tasks(struct mmc_host *mmc, unsigned int timeout)
  699. {
  700. struct cqhci_host *cq_host = mmc->cqe_private;
  701. bool ret;
  702. u32 ctl;
  703. cqhci_set_irqs(cq_host, CQHCI_IS_TCL);
  704. ctl = cqhci_readl(cq_host, CQHCI_CTL);
  705. ctl |= CQHCI_CLEAR_ALL_TASKS;
  706. cqhci_writel(cq_host, ctl, CQHCI_CTL);
  707. wait_event_timeout(cq_host->wait_queue, cqhci_tasks_cleared(cq_host),
  708. msecs_to_jiffies(timeout) + 1);
  709. cqhci_set_irqs(cq_host, 0);
  710. ret = cqhci_tasks_cleared(cq_host);
  711. if (!ret)
  712. pr_debug("%s: cqhci: Failed to clear tasks\n",
  713. mmc_hostname(mmc));
  714. return ret;
  715. }
  716. static bool cqhci_halted(struct cqhci_host *cq_host)
  717. {
  718. return cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT;
  719. }
  720. static bool cqhci_halt(struct mmc_host *mmc, unsigned int timeout)
  721. {
  722. struct cqhci_host *cq_host = mmc->cqe_private;
  723. bool ret;
  724. u32 ctl;
  725. if (cqhci_halted(cq_host))
  726. return true;
  727. cqhci_set_irqs(cq_host, CQHCI_IS_HAC);
  728. ctl = cqhci_readl(cq_host, CQHCI_CTL);
  729. ctl |= CQHCI_HALT;
  730. cqhci_writel(cq_host, ctl, CQHCI_CTL);
  731. wait_event_timeout(cq_host->wait_queue, cqhci_halted(cq_host),
  732. msecs_to_jiffies(timeout) + 1);
  733. cqhci_set_irqs(cq_host, 0);
  734. ret = cqhci_halted(cq_host);
  735. if (!ret)
  736. pr_debug("%s: cqhci: Failed to halt\n", mmc_hostname(mmc));
  737. return ret;
  738. }
  739. /*
  740. * After halting we expect to be able to use the command line. We interpret the
  741. * failure to halt to mean the data lines might still be in use (and the upper
  742. * layers will need to send a STOP command), so we set the timeout based on a
  743. * generous command timeout.
  744. */
  745. #define CQHCI_START_HALT_TIMEOUT 5
  746. static void cqhci_recovery_start(struct mmc_host *mmc)
  747. {
  748. struct cqhci_host *cq_host = mmc->cqe_private;
  749. pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__);
  750. WARN_ON(!cq_host->recovery_halt);
  751. cqhci_halt(mmc, CQHCI_START_HALT_TIMEOUT);
  752. if (cq_host->ops->disable)
  753. cq_host->ops->disable(mmc, true);
  754. mmc->cqe_on = false;
  755. }
  756. static int cqhci_error_from_flags(unsigned int flags)
  757. {
  758. if (!flags)
  759. return 0;
  760. /* CRC errors might indicate re-tuning so prefer to report that */
  761. if (flags & CQHCI_HOST_CRC)
  762. return -EILSEQ;
  763. if (flags & (CQHCI_EXTERNAL_TIMEOUT | CQHCI_HOST_TIMEOUT))
  764. return -ETIMEDOUT;
  765. return -EIO;
  766. }
  767. static void cqhci_recover_mrq(struct cqhci_host *cq_host, unsigned int tag)
  768. {
  769. struct cqhci_slot *slot = &cq_host->slot[tag];
  770. struct mmc_request *mrq = slot->mrq;
  771. struct mmc_data *data;
  772. if (!mrq)
  773. return;
  774. slot->mrq = NULL;
  775. cq_host->qcnt -= 1;
  776. data = mrq->data;
  777. if (data) {
  778. data->bytes_xfered = 0;
  779. data->error = cqhci_error_from_flags(slot->flags);
  780. } else {
  781. mrq->cmd->error = cqhci_error_from_flags(slot->flags);
  782. }
  783. mmc_cqe_request_done(cq_host->mmc, mrq);
  784. }
  785. static void cqhci_recover_mrqs(struct cqhci_host *cq_host)
  786. {
  787. int i;
  788. for (i = 0; i < cq_host->num_slots; i++)
  789. cqhci_recover_mrq(cq_host, i);
  790. }
  791. /*
  792. * By now the command and data lines should be unused so there is no reason for
  793. * CQHCI to take a long time to halt, but if it doesn't halt there could be
  794. * problems clearing tasks, so be generous.
  795. */
  796. #define CQHCI_FINISH_HALT_TIMEOUT 20
  797. /* CQHCI could be expected to clear it's internal state pretty quickly */
  798. #define CQHCI_CLEAR_TIMEOUT 20
  799. static void cqhci_recovery_finish(struct mmc_host *mmc)
  800. {
  801. struct cqhci_host *cq_host = mmc->cqe_private;
  802. unsigned long flags;
  803. u32 cqcfg;
  804. bool ok;
  805. pr_debug("%s: cqhci: %s\n", mmc_hostname(mmc), __func__);
  806. WARN_ON(!cq_host->recovery_halt);
  807. ok = cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT);
  808. if (!cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT))
  809. ok = false;
  810. /*
  811. * The specification contradicts itself, by saying that tasks cannot be
  812. * cleared if CQHCI does not halt, but if CQHCI does not halt, it should
  813. * be disabled/re-enabled, but not to disable before clearing tasks.
  814. * Have a go anyway.
  815. */
  816. if (!ok) {
  817. pr_debug("%s: cqhci: disable / re-enable\n", mmc_hostname(mmc));
  818. cqcfg = cqhci_readl(cq_host, CQHCI_CFG);
  819. cqcfg &= ~CQHCI_ENABLE;
  820. cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
  821. cqcfg |= CQHCI_ENABLE;
  822. cqhci_writel(cq_host, cqcfg, CQHCI_CFG);
  823. /* Be sure that there are no tasks */
  824. ok = cqhci_halt(mmc, CQHCI_FINISH_HALT_TIMEOUT);
  825. if (!cqhci_clear_all_tasks(mmc, CQHCI_CLEAR_TIMEOUT))
  826. ok = false;
  827. WARN_ON(!ok);
  828. }
  829. cqhci_recover_mrqs(cq_host);
  830. WARN_ON(cq_host->qcnt);
  831. spin_lock_irqsave(&cq_host->lock, flags);
  832. cq_host->qcnt = 0;
  833. cq_host->recovery_halt = false;
  834. mmc->cqe_on = false;
  835. spin_unlock_irqrestore(&cq_host->lock, flags);
  836. /* Ensure all writes are done before interrupts are re-enabled */
  837. wmb();
  838. cqhci_writel(cq_host, CQHCI_IS_HAC | CQHCI_IS_TCL, CQHCI_IS);
  839. cqhci_set_irqs(cq_host, CQHCI_IS_MASK);
  840. pr_debug("%s: cqhci: recovery done\n", mmc_hostname(mmc));
  841. }
  842. static const struct mmc_cqe_ops cqhci_cqe_ops = {
  843. .cqe_enable = cqhci_enable,
  844. .cqe_disable = cqhci_disable,
  845. .cqe_request = cqhci_request,
  846. .cqe_post_req = cqhci_post_req,
  847. .cqe_off = cqhci_off,
  848. .cqe_wait_for_idle = cqhci_wait_for_idle,
  849. .cqe_timeout = cqhci_timeout,
  850. .cqe_recovery_start = cqhci_recovery_start,
  851. .cqe_recovery_finish = cqhci_recovery_finish,
  852. };
  853. struct cqhci_host *cqhci_pltfm_init(struct platform_device *pdev)
  854. {
  855. struct cqhci_host *cq_host;
  856. struct resource *cqhci_memres = NULL;
  857. /* check and setup CMDQ interface */
  858. cqhci_memres = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  859. "cqhci_mem");
  860. if (!cqhci_memres) {
  861. dev_dbg(&pdev->dev, "CMDQ not supported\n");
  862. return ERR_PTR(-EINVAL);
  863. }
  864. cq_host = devm_kzalloc(&pdev->dev, sizeof(*cq_host), GFP_KERNEL);
  865. if (!cq_host)
  866. return ERR_PTR(-ENOMEM);
  867. cq_host->mmio = devm_ioremap(&pdev->dev,
  868. cqhci_memres->start,
  869. resource_size(cqhci_memres));
  870. if (!cq_host->mmio) {
  871. dev_err(&pdev->dev, "failed to remap cqhci regs\n");
  872. return ERR_PTR(-EBUSY);
  873. }
  874. dev_dbg(&pdev->dev, "CMDQ ioremap: done\n");
  875. return cq_host;
  876. }
  877. EXPORT_SYMBOL(cqhci_pltfm_init);
  878. static unsigned int cqhci_ver_major(struct cqhci_host *cq_host)
  879. {
  880. return CQHCI_VER_MAJOR(cqhci_readl(cq_host, CQHCI_VER));
  881. }
  882. static unsigned int cqhci_ver_minor(struct cqhci_host *cq_host)
  883. {
  884. u32 ver = cqhci_readl(cq_host, CQHCI_VER);
  885. return CQHCI_VER_MINOR1(ver) * 10 + CQHCI_VER_MINOR2(ver);
  886. }
  887. int cqhci_init(struct cqhci_host *cq_host, struct mmc_host *mmc,
  888. bool dma64)
  889. {
  890. int err;
  891. cq_host->dma64 = dma64;
  892. cq_host->mmc = mmc;
  893. cq_host->mmc->cqe_private = cq_host;
  894. cq_host->num_slots = NUM_SLOTS;
  895. cq_host->dcmd_slot = DCMD_SLOT;
  896. mmc->cqe_ops = &cqhci_cqe_ops;
  897. mmc->cqe_qdepth = NUM_SLOTS;
  898. if (mmc->caps2 & MMC_CAP2_CQE_DCMD)
  899. mmc->cqe_qdepth -= 1;
  900. cq_host->slot = devm_kcalloc(mmc_dev(mmc), cq_host->num_slots,
  901. sizeof(*cq_host->slot), GFP_KERNEL);
  902. if (!cq_host->slot) {
  903. err = -ENOMEM;
  904. goto out_err;
  905. }
  906. spin_lock_init(&cq_host->lock);
  907. init_completion(&cq_host->halt_comp);
  908. init_waitqueue_head(&cq_host->wait_queue);
  909. pr_info("%s: CQHCI version %u.%02u\n",
  910. mmc_hostname(mmc), cqhci_ver_major(cq_host),
  911. cqhci_ver_minor(cq_host));
  912. return 0;
  913. out_err:
  914. pr_err("%s: CQHCI version %u.%02u failed to initialize, error %d\n",
  915. mmc_hostname(mmc), cqhci_ver_major(cq_host),
  916. cqhci_ver_minor(cq_host), err);
  917. return err;
  918. }
  919. EXPORT_SYMBOL(cqhci_init);
  920. MODULE_AUTHOR("Venkat Gopalakrishnan <venkatg@codeaurora.org>");
  921. MODULE_DESCRIPTION("Command Queue Host Controller Interface driver");
  922. MODULE_LICENSE("GPL v2");