usdhi6rol0.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2013-2014 Renesas Electronics Europe Ltd.
  4. * Author: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
  5. */
  6. #include <linux/clk.h>
  7. #include <linux/delay.h>
  8. #include <linux/device.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/highmem.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/io.h>
  14. #include <linux/log2.h>
  15. #include <linux/mmc/host.h>
  16. #include <linux/mmc/mmc.h>
  17. #include <linux/mmc/sd.h>
  18. #include <linux/mmc/sdio.h>
  19. #include <linux/module.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/pinctrl/consumer.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/string.h>
  25. #include <linux/time.h>
  26. #include <linux/virtio.h>
  27. #include <linux/workqueue.h>
  28. #define USDHI6_SD_CMD 0x0000
  29. #define USDHI6_SD_PORT_SEL 0x0004
  30. #define USDHI6_SD_ARG 0x0008
  31. #define USDHI6_SD_STOP 0x0010
  32. #define USDHI6_SD_SECCNT 0x0014
  33. #define USDHI6_SD_RSP10 0x0018
  34. #define USDHI6_SD_RSP32 0x0020
  35. #define USDHI6_SD_RSP54 0x0028
  36. #define USDHI6_SD_RSP76 0x0030
  37. #define USDHI6_SD_INFO1 0x0038
  38. #define USDHI6_SD_INFO2 0x003c
  39. #define USDHI6_SD_INFO1_MASK 0x0040
  40. #define USDHI6_SD_INFO2_MASK 0x0044
  41. #define USDHI6_SD_CLK_CTRL 0x0048
  42. #define USDHI6_SD_SIZE 0x004c
  43. #define USDHI6_SD_OPTION 0x0050
  44. #define USDHI6_SD_ERR_STS1 0x0058
  45. #define USDHI6_SD_ERR_STS2 0x005c
  46. #define USDHI6_SD_BUF0 0x0060
  47. #define USDHI6_SDIO_MODE 0x0068
  48. #define USDHI6_SDIO_INFO1 0x006c
  49. #define USDHI6_SDIO_INFO1_MASK 0x0070
  50. #define USDHI6_CC_EXT_MODE 0x01b0
  51. #define USDHI6_SOFT_RST 0x01c0
  52. #define USDHI6_VERSION 0x01c4
  53. #define USDHI6_HOST_MODE 0x01c8
  54. #define USDHI6_SDIF_MODE 0x01cc
  55. #define USDHI6_SD_CMD_APP 0x0040
  56. #define USDHI6_SD_CMD_MODE_RSP_AUTO 0x0000
  57. #define USDHI6_SD_CMD_MODE_RSP_NONE 0x0300
  58. #define USDHI6_SD_CMD_MODE_RSP_R1 0x0400 /* Also R5, R6, R7 */
  59. #define USDHI6_SD_CMD_MODE_RSP_R1B 0x0500 /* R1b */
  60. #define USDHI6_SD_CMD_MODE_RSP_R2 0x0600
  61. #define USDHI6_SD_CMD_MODE_RSP_R3 0x0700 /* Also R4 */
  62. #define USDHI6_SD_CMD_DATA 0x0800
  63. #define USDHI6_SD_CMD_READ 0x1000
  64. #define USDHI6_SD_CMD_MULTI 0x2000
  65. #define USDHI6_SD_CMD_CMD12_AUTO_OFF 0x4000
  66. #define USDHI6_CC_EXT_MODE_SDRW BIT(1)
  67. #define USDHI6_SD_INFO1_RSP_END BIT(0)
  68. #define USDHI6_SD_INFO1_ACCESS_END BIT(2)
  69. #define USDHI6_SD_INFO1_CARD_OUT BIT(3)
  70. #define USDHI6_SD_INFO1_CARD_IN BIT(4)
  71. #define USDHI6_SD_INFO1_CD BIT(5)
  72. #define USDHI6_SD_INFO1_WP BIT(7)
  73. #define USDHI6_SD_INFO1_D3_CARD_OUT BIT(8)
  74. #define USDHI6_SD_INFO1_D3_CARD_IN BIT(9)
  75. #define USDHI6_SD_INFO2_CMD_ERR BIT(0)
  76. #define USDHI6_SD_INFO2_CRC_ERR BIT(1)
  77. #define USDHI6_SD_INFO2_END_ERR BIT(2)
  78. #define USDHI6_SD_INFO2_TOUT BIT(3)
  79. #define USDHI6_SD_INFO2_IWA_ERR BIT(4)
  80. #define USDHI6_SD_INFO2_IRA_ERR BIT(5)
  81. #define USDHI6_SD_INFO2_RSP_TOUT BIT(6)
  82. #define USDHI6_SD_INFO2_SDDAT0 BIT(7)
  83. #define USDHI6_SD_INFO2_BRE BIT(8)
  84. #define USDHI6_SD_INFO2_BWE BIT(9)
  85. #define USDHI6_SD_INFO2_SCLKDIVEN BIT(13)
  86. #define USDHI6_SD_INFO2_CBSY BIT(14)
  87. #define USDHI6_SD_INFO2_ILA BIT(15)
  88. #define USDHI6_SD_INFO1_CARD_INSERT (USDHI6_SD_INFO1_CARD_IN | USDHI6_SD_INFO1_D3_CARD_IN)
  89. #define USDHI6_SD_INFO1_CARD_EJECT (USDHI6_SD_INFO1_CARD_OUT | USDHI6_SD_INFO1_D3_CARD_OUT)
  90. #define USDHI6_SD_INFO1_CARD (USDHI6_SD_INFO1_CARD_INSERT | USDHI6_SD_INFO1_CARD_EJECT)
  91. #define USDHI6_SD_INFO1_CARD_CD (USDHI6_SD_INFO1_CARD_IN | USDHI6_SD_INFO1_CARD_OUT)
  92. #define USDHI6_SD_INFO2_ERR (USDHI6_SD_INFO2_CMD_ERR | \
  93. USDHI6_SD_INFO2_CRC_ERR | USDHI6_SD_INFO2_END_ERR | \
  94. USDHI6_SD_INFO2_TOUT | USDHI6_SD_INFO2_IWA_ERR | \
  95. USDHI6_SD_INFO2_IRA_ERR | USDHI6_SD_INFO2_RSP_TOUT | \
  96. USDHI6_SD_INFO2_ILA)
  97. #define USDHI6_SD_INFO1_IRQ (USDHI6_SD_INFO1_RSP_END | USDHI6_SD_INFO1_ACCESS_END | \
  98. USDHI6_SD_INFO1_CARD)
  99. #define USDHI6_SD_INFO2_IRQ (USDHI6_SD_INFO2_ERR | USDHI6_SD_INFO2_BRE | \
  100. USDHI6_SD_INFO2_BWE | 0x0800 | USDHI6_SD_INFO2_ILA)
  101. #define USDHI6_SD_CLK_CTRL_SCLKEN BIT(8)
  102. #define USDHI6_SD_STOP_STP BIT(0)
  103. #define USDHI6_SD_STOP_SEC BIT(8)
  104. #define USDHI6_SDIO_INFO1_IOIRQ BIT(0)
  105. #define USDHI6_SDIO_INFO1_EXPUB52 BIT(14)
  106. #define USDHI6_SDIO_INFO1_EXWT BIT(15)
  107. #define USDHI6_SD_ERR_STS1_CRC_NO_ERROR BIT(13)
  108. #define USDHI6_SOFT_RST_RESERVED (BIT(1) | BIT(2))
  109. #define USDHI6_SOFT_RST_RESET BIT(0)
  110. #define USDHI6_SD_OPTION_TIMEOUT_SHIFT 4
  111. #define USDHI6_SD_OPTION_TIMEOUT_MASK (0xf << USDHI6_SD_OPTION_TIMEOUT_SHIFT)
  112. #define USDHI6_SD_OPTION_WIDTH_1 BIT(15)
  113. #define USDHI6_SD_PORT_SEL_PORTS_SHIFT 8
  114. #define USDHI6_SD_CLK_CTRL_DIV_MASK 0xff
  115. #define USDHI6_SDIO_INFO1_IRQ (USDHI6_SDIO_INFO1_IOIRQ | 3 | \
  116. USDHI6_SDIO_INFO1_EXPUB52 | USDHI6_SDIO_INFO1_EXWT)
  117. #define USDHI6_MIN_DMA 64
  118. #define USDHI6_REQ_TIMEOUT_MS 4000
  119. enum usdhi6_wait_for {
  120. USDHI6_WAIT_FOR_REQUEST,
  121. USDHI6_WAIT_FOR_CMD,
  122. USDHI6_WAIT_FOR_MREAD,
  123. USDHI6_WAIT_FOR_MWRITE,
  124. USDHI6_WAIT_FOR_READ,
  125. USDHI6_WAIT_FOR_WRITE,
  126. USDHI6_WAIT_FOR_DATA_END,
  127. USDHI6_WAIT_FOR_STOP,
  128. USDHI6_WAIT_FOR_DMA,
  129. };
  130. struct usdhi6_page {
  131. struct page *page;
  132. void *mapped; /* mapped page */
  133. };
  134. struct usdhi6_host {
  135. struct mmc_host *mmc;
  136. struct mmc_request *mrq;
  137. void __iomem *base;
  138. struct clk *clk;
  139. /* SG memory handling */
  140. /* Common for multiple and single block requests */
  141. struct usdhi6_page pg; /* current page from an SG */
  142. void *blk_page; /* either a mapped page, or the bounce buffer */
  143. size_t offset; /* offset within a page, including sg->offset */
  144. /* Blocks, crossing a page boundary */
  145. size_t head_len;
  146. struct usdhi6_page head_pg;
  147. /* A bounce buffer for unaligned blocks or blocks, crossing a page boundary */
  148. struct scatterlist bounce_sg;
  149. u8 bounce_buf[512];
  150. /* Multiple block requests only */
  151. struct scatterlist *sg; /* current SG segment */
  152. int page_idx; /* page index within an SG segment */
  153. enum usdhi6_wait_for wait;
  154. u32 status_mask;
  155. u32 status2_mask;
  156. u32 sdio_mask;
  157. u32 io_error;
  158. u32 irq_status;
  159. unsigned long imclk;
  160. unsigned long rate;
  161. bool app_cmd;
  162. /* Timeout handling */
  163. struct delayed_work timeout_work;
  164. unsigned long timeout;
  165. /* DMA support */
  166. struct dma_chan *chan_rx;
  167. struct dma_chan *chan_tx;
  168. bool dma_active;
  169. /* Pin control */
  170. struct pinctrl *pinctrl;
  171. struct pinctrl_state *pins_uhs;
  172. };
  173. /* I/O primitives */
  174. static void usdhi6_write(struct usdhi6_host *host, u32 reg, u32 data)
  175. {
  176. iowrite32(data, host->base + reg);
  177. dev_vdbg(mmc_dev(host->mmc), "%s(0x%p + 0x%x) = 0x%x\n", __func__,
  178. host->base, reg, data);
  179. }
  180. static void usdhi6_write16(struct usdhi6_host *host, u32 reg, u16 data)
  181. {
  182. iowrite16(data, host->base + reg);
  183. dev_vdbg(mmc_dev(host->mmc), "%s(0x%p + 0x%x) = 0x%x\n", __func__,
  184. host->base, reg, data);
  185. }
  186. static u32 usdhi6_read(struct usdhi6_host *host, u32 reg)
  187. {
  188. u32 data = ioread32(host->base + reg);
  189. dev_vdbg(mmc_dev(host->mmc), "%s(0x%p + 0x%x) = 0x%x\n", __func__,
  190. host->base, reg, data);
  191. return data;
  192. }
  193. static u16 usdhi6_read16(struct usdhi6_host *host, u32 reg)
  194. {
  195. u16 data = ioread16(host->base + reg);
  196. dev_vdbg(mmc_dev(host->mmc), "%s(0x%p + 0x%x) = 0x%x\n", __func__,
  197. host->base, reg, data);
  198. return data;
  199. }
  200. static void usdhi6_irq_enable(struct usdhi6_host *host, u32 info1, u32 info2)
  201. {
  202. host->status_mask = USDHI6_SD_INFO1_IRQ & ~info1;
  203. host->status2_mask = USDHI6_SD_INFO2_IRQ & ~info2;
  204. usdhi6_write(host, USDHI6_SD_INFO1_MASK, host->status_mask);
  205. usdhi6_write(host, USDHI6_SD_INFO2_MASK, host->status2_mask);
  206. }
  207. static void usdhi6_wait_for_resp(struct usdhi6_host *host)
  208. {
  209. usdhi6_irq_enable(host, USDHI6_SD_INFO1_RSP_END |
  210. USDHI6_SD_INFO1_ACCESS_END | USDHI6_SD_INFO1_CARD_CD,
  211. USDHI6_SD_INFO2_ERR);
  212. }
  213. static void usdhi6_wait_for_brwe(struct usdhi6_host *host, bool read)
  214. {
  215. usdhi6_irq_enable(host, USDHI6_SD_INFO1_ACCESS_END |
  216. USDHI6_SD_INFO1_CARD_CD, USDHI6_SD_INFO2_ERR |
  217. (read ? USDHI6_SD_INFO2_BRE : USDHI6_SD_INFO2_BWE));
  218. }
  219. static void usdhi6_only_cd(struct usdhi6_host *host)
  220. {
  221. /* Mask all except card hotplug */
  222. usdhi6_irq_enable(host, USDHI6_SD_INFO1_CARD_CD, 0);
  223. }
  224. static void usdhi6_mask_all(struct usdhi6_host *host)
  225. {
  226. usdhi6_irq_enable(host, 0, 0);
  227. }
  228. static int usdhi6_error_code(struct usdhi6_host *host)
  229. {
  230. u32 err;
  231. usdhi6_write(host, USDHI6_SD_STOP, USDHI6_SD_STOP_STP);
  232. if (host->io_error &
  233. (USDHI6_SD_INFO2_RSP_TOUT | USDHI6_SD_INFO2_TOUT)) {
  234. u32 rsp54 = usdhi6_read(host, USDHI6_SD_RSP54);
  235. int opc = host->mrq ? host->mrq->cmd->opcode : -1;
  236. err = usdhi6_read(host, USDHI6_SD_ERR_STS2);
  237. /* Response timeout is often normal, don't spam the log */
  238. if (host->wait == USDHI6_WAIT_FOR_CMD)
  239. dev_dbg(mmc_dev(host->mmc),
  240. "T-out sts 0x%x, resp 0x%x, state %u, CMD%d\n",
  241. err, rsp54, host->wait, opc);
  242. else
  243. dev_warn(mmc_dev(host->mmc),
  244. "T-out sts 0x%x, resp 0x%x, state %u, CMD%d\n",
  245. err, rsp54, host->wait, opc);
  246. return -ETIMEDOUT;
  247. }
  248. err = usdhi6_read(host, USDHI6_SD_ERR_STS1);
  249. if (err != USDHI6_SD_ERR_STS1_CRC_NO_ERROR)
  250. dev_warn(mmc_dev(host->mmc), "Err sts 0x%x, state %u, CMD%d\n",
  251. err, host->wait, host->mrq ? host->mrq->cmd->opcode : -1);
  252. if (host->io_error & USDHI6_SD_INFO2_ILA)
  253. return -EILSEQ;
  254. return -EIO;
  255. }
  256. /* Scatter-Gather management */
  257. /*
  258. * In PIO mode we have to map each page separately, using kmap(). That way
  259. * adjacent pages are mapped to non-adjacent virtual addresses. That's why we
  260. * have to use a bounce buffer for blocks, crossing page boundaries. Such blocks
  261. * have been observed with an SDIO WiFi card (b43 driver).
  262. */
  263. static void usdhi6_blk_bounce(struct usdhi6_host *host,
  264. struct scatterlist *sg)
  265. {
  266. struct mmc_data *data = host->mrq->data;
  267. size_t blk_head = host->head_len;
  268. dev_dbg(mmc_dev(host->mmc), "%s(): CMD%u of %u SG: %ux%u @ 0x%x\n",
  269. __func__, host->mrq->cmd->opcode, data->sg_len,
  270. data->blksz, data->blocks, sg->offset);
  271. host->head_pg.page = host->pg.page;
  272. host->head_pg.mapped = host->pg.mapped;
  273. host->pg.page = nth_page(host->pg.page, 1);
  274. host->pg.mapped = kmap(host->pg.page);
  275. host->blk_page = host->bounce_buf;
  276. host->offset = 0;
  277. if (data->flags & MMC_DATA_READ)
  278. return;
  279. memcpy(host->bounce_buf, host->head_pg.mapped + PAGE_SIZE - blk_head,
  280. blk_head);
  281. memcpy(host->bounce_buf + blk_head, host->pg.mapped,
  282. data->blksz - blk_head);
  283. }
  284. /* Only called for multiple block IO */
  285. static void usdhi6_sg_prep(struct usdhi6_host *host)
  286. {
  287. struct mmc_request *mrq = host->mrq;
  288. struct mmc_data *data = mrq->data;
  289. usdhi6_write(host, USDHI6_SD_SECCNT, data->blocks);
  290. host->sg = data->sg;
  291. /* TODO: if we always map, this is redundant */
  292. host->offset = host->sg->offset;
  293. }
  294. /* Map the first page in an SG segment: common for multiple and single block IO */
  295. static void *usdhi6_sg_map(struct usdhi6_host *host)
  296. {
  297. struct mmc_data *data = host->mrq->data;
  298. struct scatterlist *sg = data->sg_len > 1 ? host->sg : data->sg;
  299. size_t head = PAGE_SIZE - sg->offset;
  300. size_t blk_head = head % data->blksz;
  301. WARN(host->pg.page, "%p not properly unmapped!\n", host->pg.page);
  302. if (WARN(sg_dma_len(sg) % data->blksz,
  303. "SG size %u isn't a multiple of block size %u\n",
  304. sg_dma_len(sg), data->blksz))
  305. return NULL;
  306. host->pg.page = sg_page(sg);
  307. host->pg.mapped = kmap(host->pg.page);
  308. host->offset = sg->offset;
  309. /*
  310. * Block size must be a power of 2 for multi-block transfers,
  311. * therefore blk_head is equal for all pages in this SG
  312. */
  313. host->head_len = blk_head;
  314. if (head < data->blksz)
  315. /*
  316. * The first block in the SG crosses a page boundary.
  317. * Max blksz = 512, so blocks can only span 2 pages
  318. */
  319. usdhi6_blk_bounce(host, sg);
  320. else
  321. host->blk_page = host->pg.mapped;
  322. dev_dbg(mmc_dev(host->mmc), "Mapped %p (%lx) at %p + %u for CMD%u @ 0x%p\n",
  323. host->pg.page, page_to_pfn(host->pg.page), host->pg.mapped,
  324. sg->offset, host->mrq->cmd->opcode, host->mrq);
  325. return host->blk_page + host->offset;
  326. }
  327. /* Unmap the current page: common for multiple and single block IO */
  328. static void usdhi6_sg_unmap(struct usdhi6_host *host, bool force)
  329. {
  330. struct mmc_data *data = host->mrq->data;
  331. struct page *page = host->head_pg.page;
  332. if (page) {
  333. /* Previous block was cross-page boundary */
  334. struct scatterlist *sg = data->sg_len > 1 ?
  335. host->sg : data->sg;
  336. size_t blk_head = host->head_len;
  337. if (!data->error && data->flags & MMC_DATA_READ) {
  338. memcpy(host->head_pg.mapped + PAGE_SIZE - blk_head,
  339. host->bounce_buf, blk_head);
  340. memcpy(host->pg.mapped, host->bounce_buf + blk_head,
  341. data->blksz - blk_head);
  342. }
  343. flush_dcache_page(page);
  344. kunmap(page);
  345. host->head_pg.page = NULL;
  346. if (!force && sg_dma_len(sg) + sg->offset >
  347. (host->page_idx << PAGE_SHIFT) + data->blksz - blk_head)
  348. /* More blocks in this SG, don't unmap the next page */
  349. return;
  350. }
  351. page = host->pg.page;
  352. if (!page)
  353. return;
  354. flush_dcache_page(page);
  355. kunmap(page);
  356. host->pg.page = NULL;
  357. }
  358. /* Called from MMC_WRITE_MULTIPLE_BLOCK or MMC_READ_MULTIPLE_BLOCK */
  359. static void usdhi6_sg_advance(struct usdhi6_host *host)
  360. {
  361. struct mmc_data *data = host->mrq->data;
  362. size_t done, total;
  363. /* New offset: set at the end of the previous block */
  364. if (host->head_pg.page) {
  365. /* Finished a cross-page block, jump to the new page */
  366. host->page_idx++;
  367. host->offset = data->blksz - host->head_len;
  368. host->blk_page = host->pg.mapped;
  369. usdhi6_sg_unmap(host, false);
  370. } else {
  371. host->offset += data->blksz;
  372. /* The completed block didn't cross a page boundary */
  373. if (host->offset == PAGE_SIZE) {
  374. /* If required, we'll map the page below */
  375. host->offset = 0;
  376. host->page_idx++;
  377. }
  378. }
  379. /*
  380. * Now host->blk_page + host->offset point at the end of our last block
  381. * and host->page_idx is the index of the page, in which our new block
  382. * is located, if any
  383. */
  384. done = (host->page_idx << PAGE_SHIFT) + host->offset;
  385. total = host->sg->offset + sg_dma_len(host->sg);
  386. dev_dbg(mmc_dev(host->mmc), "%s(): %zu of %zu @ %zu\n", __func__,
  387. done, total, host->offset);
  388. if (done < total && host->offset) {
  389. /* More blocks in this page */
  390. if (host->offset + data->blksz > PAGE_SIZE)
  391. /* We approached at a block, that spans 2 pages */
  392. usdhi6_blk_bounce(host, host->sg);
  393. return;
  394. }
  395. /* Finished current page or an SG segment */
  396. usdhi6_sg_unmap(host, false);
  397. if (done == total) {
  398. /*
  399. * End of an SG segment or the complete SG: jump to the next
  400. * segment, we'll map it later in usdhi6_blk_read() or
  401. * usdhi6_blk_write()
  402. */
  403. struct scatterlist *next = sg_next(host->sg);
  404. host->page_idx = 0;
  405. if (!next)
  406. host->wait = USDHI6_WAIT_FOR_DATA_END;
  407. host->sg = next;
  408. if (WARN(next && sg_dma_len(next) % data->blksz,
  409. "SG size %u isn't a multiple of block size %u\n",
  410. sg_dma_len(next), data->blksz))
  411. data->error = -EINVAL;
  412. return;
  413. }
  414. /* We cannot get here after crossing a page border */
  415. /* Next page in the same SG */
  416. host->pg.page = nth_page(sg_page(host->sg), host->page_idx);
  417. host->pg.mapped = kmap(host->pg.page);
  418. host->blk_page = host->pg.mapped;
  419. dev_dbg(mmc_dev(host->mmc), "Mapped %p (%lx) at %p for CMD%u @ 0x%p\n",
  420. host->pg.page, page_to_pfn(host->pg.page), host->pg.mapped,
  421. host->mrq->cmd->opcode, host->mrq);
  422. }
  423. /* DMA handling */
  424. static void usdhi6_dma_release(struct usdhi6_host *host)
  425. {
  426. host->dma_active = false;
  427. if (host->chan_tx) {
  428. struct dma_chan *chan = host->chan_tx;
  429. host->chan_tx = NULL;
  430. dma_release_channel(chan);
  431. }
  432. if (host->chan_rx) {
  433. struct dma_chan *chan = host->chan_rx;
  434. host->chan_rx = NULL;
  435. dma_release_channel(chan);
  436. }
  437. }
  438. static void usdhi6_dma_stop_unmap(struct usdhi6_host *host)
  439. {
  440. struct mmc_data *data = host->mrq->data;
  441. if (!host->dma_active)
  442. return;
  443. usdhi6_write(host, USDHI6_CC_EXT_MODE, 0);
  444. host->dma_active = false;
  445. if (data->flags & MMC_DATA_READ)
  446. dma_unmap_sg(host->chan_rx->device->dev, data->sg,
  447. data->sg_len, DMA_FROM_DEVICE);
  448. else
  449. dma_unmap_sg(host->chan_tx->device->dev, data->sg,
  450. data->sg_len, DMA_TO_DEVICE);
  451. }
  452. static void usdhi6_dma_complete(void *arg)
  453. {
  454. struct usdhi6_host *host = arg;
  455. struct mmc_request *mrq = host->mrq;
  456. if (WARN(!mrq || !mrq->data, "%s: NULL data in DMA completion for %p!\n",
  457. dev_name(mmc_dev(host->mmc)), mrq))
  458. return;
  459. dev_dbg(mmc_dev(host->mmc), "%s(): CMD%u DMA completed\n", __func__,
  460. mrq->cmd->opcode);
  461. usdhi6_dma_stop_unmap(host);
  462. usdhi6_wait_for_brwe(host, mrq->data->flags & MMC_DATA_READ);
  463. }
  464. static int usdhi6_dma_setup(struct usdhi6_host *host, struct dma_chan *chan,
  465. enum dma_transfer_direction dir)
  466. {
  467. struct mmc_data *data = host->mrq->data;
  468. struct scatterlist *sg = data->sg;
  469. struct dma_async_tx_descriptor *desc = NULL;
  470. dma_cookie_t cookie = -EINVAL;
  471. enum dma_data_direction data_dir;
  472. int ret;
  473. switch (dir) {
  474. case DMA_MEM_TO_DEV:
  475. data_dir = DMA_TO_DEVICE;
  476. break;
  477. case DMA_DEV_TO_MEM:
  478. data_dir = DMA_FROM_DEVICE;
  479. break;
  480. default:
  481. return -EINVAL;
  482. }
  483. ret = dma_map_sg(chan->device->dev, sg, data->sg_len, data_dir);
  484. if (ret > 0) {
  485. host->dma_active = true;
  486. desc = dmaengine_prep_slave_sg(chan, sg, ret, dir,
  487. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  488. }
  489. if (desc) {
  490. desc->callback = usdhi6_dma_complete;
  491. desc->callback_param = host;
  492. cookie = dmaengine_submit(desc);
  493. }
  494. dev_dbg(mmc_dev(host->mmc), "%s(): mapped %d -> %d, cookie %d @ %p\n",
  495. __func__, data->sg_len, ret, cookie, desc);
  496. if (cookie < 0) {
  497. /* DMA failed, fall back to PIO */
  498. if (ret >= 0)
  499. ret = cookie;
  500. usdhi6_dma_release(host);
  501. dev_warn(mmc_dev(host->mmc),
  502. "DMA failed: %d, falling back to PIO\n", ret);
  503. }
  504. return cookie;
  505. }
  506. static int usdhi6_dma_start(struct usdhi6_host *host)
  507. {
  508. if (!host->chan_rx || !host->chan_tx)
  509. return -ENODEV;
  510. if (host->mrq->data->flags & MMC_DATA_READ)
  511. return usdhi6_dma_setup(host, host->chan_rx, DMA_DEV_TO_MEM);
  512. return usdhi6_dma_setup(host, host->chan_tx, DMA_MEM_TO_DEV);
  513. }
  514. static void usdhi6_dma_kill(struct usdhi6_host *host)
  515. {
  516. struct mmc_data *data = host->mrq->data;
  517. dev_dbg(mmc_dev(host->mmc), "%s(): SG of %u: %ux%u\n",
  518. __func__, data->sg_len, data->blocks, data->blksz);
  519. /* Abort DMA */
  520. if (data->flags & MMC_DATA_READ)
  521. dmaengine_terminate_sync(host->chan_rx);
  522. else
  523. dmaengine_terminate_sync(host->chan_tx);
  524. }
  525. static void usdhi6_dma_check_error(struct usdhi6_host *host)
  526. {
  527. struct mmc_data *data = host->mrq->data;
  528. dev_dbg(mmc_dev(host->mmc), "%s(): IO error %d, status 0x%x\n",
  529. __func__, host->io_error, usdhi6_read(host, USDHI6_SD_INFO1));
  530. if (host->io_error) {
  531. data->error = usdhi6_error_code(host);
  532. data->bytes_xfered = 0;
  533. usdhi6_dma_kill(host);
  534. usdhi6_dma_release(host);
  535. dev_warn(mmc_dev(host->mmc),
  536. "DMA failed: %d, falling back to PIO\n", data->error);
  537. return;
  538. }
  539. /*
  540. * The datasheet tells us to check a response from the card, whereas
  541. * responses only come after the command phase, not after the data
  542. * phase. Let's check anyway.
  543. */
  544. if (host->irq_status & USDHI6_SD_INFO1_RSP_END)
  545. dev_warn(mmc_dev(host->mmc), "Unexpected response received!\n");
  546. }
  547. static void usdhi6_dma_kick(struct usdhi6_host *host)
  548. {
  549. if (host->mrq->data->flags & MMC_DATA_READ)
  550. dma_async_issue_pending(host->chan_rx);
  551. else
  552. dma_async_issue_pending(host->chan_tx);
  553. }
  554. static void usdhi6_dma_request(struct usdhi6_host *host, phys_addr_t start)
  555. {
  556. struct dma_slave_config cfg = {
  557. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  558. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  559. };
  560. int ret;
  561. host->chan_tx = dma_request_chan(mmc_dev(host->mmc), "tx");
  562. dev_dbg(mmc_dev(host->mmc), "%s: TX: got channel %p\n", __func__,
  563. host->chan_tx);
  564. if (IS_ERR(host->chan_tx)) {
  565. host->chan_tx = NULL;
  566. return;
  567. }
  568. cfg.direction = DMA_MEM_TO_DEV;
  569. cfg.dst_addr = start + USDHI6_SD_BUF0;
  570. cfg.dst_maxburst = 128; /* 128 words * 4 bytes = 512 bytes */
  571. cfg.src_addr = 0;
  572. ret = dmaengine_slave_config(host->chan_tx, &cfg);
  573. if (ret < 0)
  574. goto e_release_tx;
  575. host->chan_rx = dma_request_chan(mmc_dev(host->mmc), "rx");
  576. dev_dbg(mmc_dev(host->mmc), "%s: RX: got channel %p\n", __func__,
  577. host->chan_rx);
  578. if (IS_ERR(host->chan_rx)) {
  579. host->chan_rx = NULL;
  580. goto e_release_tx;
  581. }
  582. cfg.direction = DMA_DEV_TO_MEM;
  583. cfg.src_addr = cfg.dst_addr;
  584. cfg.src_maxburst = 128; /* 128 words * 4 bytes = 512 bytes */
  585. cfg.dst_addr = 0;
  586. ret = dmaengine_slave_config(host->chan_rx, &cfg);
  587. if (ret < 0)
  588. goto e_release_rx;
  589. return;
  590. e_release_rx:
  591. dma_release_channel(host->chan_rx);
  592. host->chan_rx = NULL;
  593. e_release_tx:
  594. dma_release_channel(host->chan_tx);
  595. host->chan_tx = NULL;
  596. }
  597. /* API helpers */
  598. static void usdhi6_clk_set(struct usdhi6_host *host, struct mmc_ios *ios)
  599. {
  600. unsigned long rate = ios->clock;
  601. u32 val;
  602. unsigned int i;
  603. for (i = 1000; i; i--) {
  604. if (usdhi6_read(host, USDHI6_SD_INFO2) & USDHI6_SD_INFO2_SCLKDIVEN)
  605. break;
  606. usleep_range(10, 100);
  607. }
  608. if (!i) {
  609. dev_err(mmc_dev(host->mmc), "SD bus busy, clock set aborted\n");
  610. return;
  611. }
  612. val = usdhi6_read(host, USDHI6_SD_CLK_CTRL) & ~USDHI6_SD_CLK_CTRL_DIV_MASK;
  613. if (rate) {
  614. unsigned long new_rate;
  615. if (host->imclk <= rate) {
  616. if (ios->timing != MMC_TIMING_UHS_DDR50) {
  617. /* Cannot have 1-to-1 clock in DDR mode */
  618. new_rate = host->imclk;
  619. val |= 0xff;
  620. } else {
  621. new_rate = host->imclk / 2;
  622. }
  623. } else {
  624. unsigned long div =
  625. roundup_pow_of_two(DIV_ROUND_UP(host->imclk, rate));
  626. val |= div >> 2;
  627. new_rate = host->imclk / div;
  628. }
  629. if (host->rate == new_rate)
  630. return;
  631. host->rate = new_rate;
  632. dev_dbg(mmc_dev(host->mmc), "target %lu, div %u, set %lu\n",
  633. rate, (val & 0xff) << 2, new_rate);
  634. }
  635. /*
  636. * if old or new rate is equal to input rate, have to switch the clock
  637. * off before changing and on after
  638. */
  639. if (host->imclk == rate || host->imclk == host->rate || !rate)
  640. usdhi6_write(host, USDHI6_SD_CLK_CTRL,
  641. val & ~USDHI6_SD_CLK_CTRL_SCLKEN);
  642. if (!rate) {
  643. host->rate = 0;
  644. return;
  645. }
  646. usdhi6_write(host, USDHI6_SD_CLK_CTRL, val);
  647. if (host->imclk == rate || host->imclk == host->rate ||
  648. !(val & USDHI6_SD_CLK_CTRL_SCLKEN))
  649. usdhi6_write(host, USDHI6_SD_CLK_CTRL,
  650. val | USDHI6_SD_CLK_CTRL_SCLKEN);
  651. }
  652. static void usdhi6_set_power(struct usdhi6_host *host, struct mmc_ios *ios)
  653. {
  654. struct mmc_host *mmc = host->mmc;
  655. if (!IS_ERR(mmc->supply.vmmc))
  656. /* Errors ignored... */
  657. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  658. ios->power_mode ? ios->vdd : 0);
  659. }
  660. static int usdhi6_reset(struct usdhi6_host *host)
  661. {
  662. int i;
  663. usdhi6_write(host, USDHI6_SOFT_RST, USDHI6_SOFT_RST_RESERVED);
  664. cpu_relax();
  665. usdhi6_write(host, USDHI6_SOFT_RST, USDHI6_SOFT_RST_RESERVED | USDHI6_SOFT_RST_RESET);
  666. for (i = 1000; i; i--)
  667. if (usdhi6_read(host, USDHI6_SOFT_RST) & USDHI6_SOFT_RST_RESET)
  668. break;
  669. return i ? 0 : -ETIMEDOUT;
  670. }
  671. static void usdhi6_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  672. {
  673. struct usdhi6_host *host = mmc_priv(mmc);
  674. u32 option, mode;
  675. int ret;
  676. dev_dbg(mmc_dev(mmc), "%uHz, OCR: %u, power %u, bus-width %u, timing %u\n",
  677. ios->clock, ios->vdd, ios->power_mode, ios->bus_width, ios->timing);
  678. switch (ios->power_mode) {
  679. case MMC_POWER_OFF:
  680. usdhi6_set_power(host, ios);
  681. usdhi6_only_cd(host);
  682. break;
  683. case MMC_POWER_UP:
  684. /*
  685. * We only also touch USDHI6_SD_OPTION from .request(), which
  686. * cannot race with MMC_POWER_UP
  687. */
  688. ret = usdhi6_reset(host);
  689. if (ret < 0) {
  690. dev_err(mmc_dev(mmc), "Cannot reset the interface!\n");
  691. } else {
  692. usdhi6_set_power(host, ios);
  693. usdhi6_only_cd(host);
  694. }
  695. break;
  696. case MMC_POWER_ON:
  697. option = usdhi6_read(host, USDHI6_SD_OPTION);
  698. /*
  699. * The eMMC standard only allows 4 or 8 bits in the DDR mode,
  700. * the same probably holds for SD cards. We check here anyway,
  701. * since the datasheet explicitly requires 4 bits for DDR.
  702. */
  703. if (ios->bus_width == MMC_BUS_WIDTH_1) {
  704. if (ios->timing == MMC_TIMING_UHS_DDR50)
  705. dev_err(mmc_dev(mmc),
  706. "4 bits are required for DDR\n");
  707. option |= USDHI6_SD_OPTION_WIDTH_1;
  708. mode = 0;
  709. } else {
  710. option &= ~USDHI6_SD_OPTION_WIDTH_1;
  711. mode = ios->timing == MMC_TIMING_UHS_DDR50;
  712. }
  713. usdhi6_write(host, USDHI6_SD_OPTION, option);
  714. usdhi6_write(host, USDHI6_SDIF_MODE, mode);
  715. break;
  716. }
  717. if (host->rate != ios->clock)
  718. usdhi6_clk_set(host, ios);
  719. }
  720. /* This is data timeout. Response timeout is fixed to 640 clock cycles */
  721. static void usdhi6_timeout_set(struct usdhi6_host *host)
  722. {
  723. struct mmc_request *mrq = host->mrq;
  724. u32 val;
  725. unsigned long ticks;
  726. if (!mrq->data)
  727. ticks = host->rate / 1000 * mrq->cmd->busy_timeout;
  728. else
  729. ticks = host->rate / 1000000 * (mrq->data->timeout_ns / 1000) +
  730. mrq->data->timeout_clks;
  731. if (!ticks || ticks > 1 << 27)
  732. /* Max timeout */
  733. val = 14;
  734. else if (ticks < 1 << 13)
  735. /* Min timeout */
  736. val = 0;
  737. else
  738. val = order_base_2(ticks) - 13;
  739. dev_dbg(mmc_dev(host->mmc), "Set %s timeout %lu ticks @ %lu Hz\n",
  740. mrq->data ? "data" : "cmd", ticks, host->rate);
  741. /* Timeout Counter mask: 0xf0 */
  742. usdhi6_write(host, USDHI6_SD_OPTION, (val << USDHI6_SD_OPTION_TIMEOUT_SHIFT) |
  743. (usdhi6_read(host, USDHI6_SD_OPTION) & ~USDHI6_SD_OPTION_TIMEOUT_MASK));
  744. }
  745. static void usdhi6_request_done(struct usdhi6_host *host)
  746. {
  747. struct mmc_request *mrq = host->mrq;
  748. struct mmc_data *data = mrq->data;
  749. if (WARN(host->pg.page || host->head_pg.page,
  750. "Page %p or %p not unmapped: wait %u, CMD%d(%c) @ +0x%zx %ux%u in SG%u!\n",
  751. host->pg.page, host->head_pg.page, host->wait, mrq->cmd->opcode,
  752. data ? (data->flags & MMC_DATA_READ ? 'R' : 'W') : '-',
  753. data ? host->offset : 0, data ? data->blocks : 0,
  754. data ? data->blksz : 0, data ? data->sg_len : 0))
  755. usdhi6_sg_unmap(host, true);
  756. if (mrq->cmd->error ||
  757. (data && data->error) ||
  758. (mrq->stop && mrq->stop->error))
  759. dev_dbg(mmc_dev(host->mmc), "%s(CMD%d: %ux%u): err %d %d %d\n",
  760. __func__, mrq->cmd->opcode, data ? data->blocks : 0,
  761. data ? data->blksz : 0,
  762. mrq->cmd->error,
  763. data ? data->error : 1,
  764. mrq->stop ? mrq->stop->error : 1);
  765. /* Disable DMA */
  766. usdhi6_write(host, USDHI6_CC_EXT_MODE, 0);
  767. host->wait = USDHI6_WAIT_FOR_REQUEST;
  768. host->mrq = NULL;
  769. mmc_request_done(host->mmc, mrq);
  770. }
  771. static int usdhi6_cmd_flags(struct usdhi6_host *host)
  772. {
  773. struct mmc_request *mrq = host->mrq;
  774. struct mmc_command *cmd = mrq->cmd;
  775. u16 opc = cmd->opcode;
  776. if (host->app_cmd) {
  777. host->app_cmd = false;
  778. opc |= USDHI6_SD_CMD_APP;
  779. }
  780. if (mrq->data) {
  781. opc |= USDHI6_SD_CMD_DATA;
  782. if (mrq->data->flags & MMC_DATA_READ)
  783. opc |= USDHI6_SD_CMD_READ;
  784. if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  785. cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
  786. (cmd->opcode == SD_IO_RW_EXTENDED &&
  787. mrq->data->blocks > 1)) {
  788. opc |= USDHI6_SD_CMD_MULTI;
  789. if (!mrq->stop)
  790. opc |= USDHI6_SD_CMD_CMD12_AUTO_OFF;
  791. }
  792. switch (mmc_resp_type(cmd)) {
  793. case MMC_RSP_NONE:
  794. opc |= USDHI6_SD_CMD_MODE_RSP_NONE;
  795. break;
  796. case MMC_RSP_R1:
  797. opc |= USDHI6_SD_CMD_MODE_RSP_R1;
  798. break;
  799. case MMC_RSP_R1B:
  800. opc |= USDHI6_SD_CMD_MODE_RSP_R1B;
  801. break;
  802. case MMC_RSP_R2:
  803. opc |= USDHI6_SD_CMD_MODE_RSP_R2;
  804. break;
  805. case MMC_RSP_R3:
  806. opc |= USDHI6_SD_CMD_MODE_RSP_R3;
  807. break;
  808. default:
  809. dev_warn(mmc_dev(host->mmc),
  810. "Unknown response type %d\n",
  811. mmc_resp_type(cmd));
  812. return -EINVAL;
  813. }
  814. }
  815. return opc;
  816. }
  817. static int usdhi6_rq_start(struct usdhi6_host *host)
  818. {
  819. struct mmc_request *mrq = host->mrq;
  820. struct mmc_command *cmd = mrq->cmd;
  821. struct mmc_data *data = mrq->data;
  822. int opc = usdhi6_cmd_flags(host);
  823. int i;
  824. if (opc < 0)
  825. return opc;
  826. for (i = 1000; i; i--) {
  827. if (!(usdhi6_read(host, USDHI6_SD_INFO2) & USDHI6_SD_INFO2_CBSY))
  828. break;
  829. usleep_range(10, 100);
  830. }
  831. if (!i) {
  832. dev_dbg(mmc_dev(host->mmc), "Command active, request aborted\n");
  833. return -EAGAIN;
  834. }
  835. if (data) {
  836. bool use_dma;
  837. int ret = 0;
  838. host->page_idx = 0;
  839. if (cmd->opcode == SD_IO_RW_EXTENDED && data->blocks > 1) {
  840. switch (data->blksz) {
  841. case 512:
  842. break;
  843. case 32:
  844. case 64:
  845. case 128:
  846. case 256:
  847. if (mrq->stop)
  848. ret = -EINVAL;
  849. break;
  850. default:
  851. ret = -EINVAL;
  852. }
  853. } else if ((cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  854. cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) &&
  855. data->blksz != 512) {
  856. ret = -EINVAL;
  857. }
  858. if (ret < 0) {
  859. dev_warn(mmc_dev(host->mmc), "%s(): %u blocks of %u bytes\n",
  860. __func__, data->blocks, data->blksz);
  861. return -EINVAL;
  862. }
  863. if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  864. cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
  865. (cmd->opcode == SD_IO_RW_EXTENDED &&
  866. data->blocks > 1))
  867. usdhi6_sg_prep(host);
  868. usdhi6_write(host, USDHI6_SD_SIZE, data->blksz);
  869. if ((data->blksz >= USDHI6_MIN_DMA ||
  870. data->blocks > 1) &&
  871. (data->blksz % 4 ||
  872. data->sg->offset % 4))
  873. dev_dbg(mmc_dev(host->mmc),
  874. "Bad SG of %u: %ux%u @ %u\n", data->sg_len,
  875. data->blksz, data->blocks, data->sg->offset);
  876. /* Enable DMA for USDHI6_MIN_DMA bytes or more */
  877. use_dma = data->blksz >= USDHI6_MIN_DMA &&
  878. !(data->blksz % 4) &&
  879. usdhi6_dma_start(host) >= DMA_MIN_COOKIE;
  880. if (use_dma)
  881. usdhi6_write(host, USDHI6_CC_EXT_MODE, USDHI6_CC_EXT_MODE_SDRW);
  882. dev_dbg(mmc_dev(host->mmc),
  883. "%s(): request opcode %u, %u blocks of %u bytes in %u segments, %s %s @+0x%x%s\n",
  884. __func__, cmd->opcode, data->blocks, data->blksz,
  885. data->sg_len, use_dma ? "DMA" : "PIO",
  886. data->flags & MMC_DATA_READ ? "read" : "write",
  887. data->sg->offset, mrq->stop ? " + stop" : "");
  888. } else {
  889. dev_dbg(mmc_dev(host->mmc), "%s(): request opcode %u\n",
  890. __func__, cmd->opcode);
  891. }
  892. /* We have to get a command completion interrupt with DMA too */
  893. usdhi6_wait_for_resp(host);
  894. host->wait = USDHI6_WAIT_FOR_CMD;
  895. schedule_delayed_work(&host->timeout_work, host->timeout);
  896. /* SEC bit is required to enable block counting by the core */
  897. usdhi6_write(host, USDHI6_SD_STOP,
  898. data && data->blocks > 1 ? USDHI6_SD_STOP_SEC : 0);
  899. usdhi6_write(host, USDHI6_SD_ARG, cmd->arg);
  900. /* Kick command execution */
  901. usdhi6_write(host, USDHI6_SD_CMD, opc);
  902. return 0;
  903. }
  904. static void usdhi6_request(struct mmc_host *mmc, struct mmc_request *mrq)
  905. {
  906. struct usdhi6_host *host = mmc_priv(mmc);
  907. int ret;
  908. cancel_delayed_work_sync(&host->timeout_work);
  909. host->mrq = mrq;
  910. host->sg = NULL;
  911. usdhi6_timeout_set(host);
  912. ret = usdhi6_rq_start(host);
  913. if (ret < 0) {
  914. mrq->cmd->error = ret;
  915. usdhi6_request_done(host);
  916. }
  917. }
  918. static int usdhi6_get_cd(struct mmc_host *mmc)
  919. {
  920. struct usdhi6_host *host = mmc_priv(mmc);
  921. /* Read is atomic, no need to lock */
  922. u32 status = usdhi6_read(host, USDHI6_SD_INFO1) & USDHI6_SD_INFO1_CD;
  923. /*
  924. * level status.CD CD_ACTIVE_HIGH card present
  925. * 1 0 0 0
  926. * 1 0 1 1
  927. * 0 1 0 1
  928. * 0 1 1 0
  929. */
  930. return !status ^ !(mmc->caps2 & MMC_CAP2_CD_ACTIVE_HIGH);
  931. }
  932. static int usdhi6_get_ro(struct mmc_host *mmc)
  933. {
  934. struct usdhi6_host *host = mmc_priv(mmc);
  935. /* No locking as above */
  936. u32 status = usdhi6_read(host, USDHI6_SD_INFO1) & USDHI6_SD_INFO1_WP;
  937. /*
  938. * level status.WP RO_ACTIVE_HIGH card read-only
  939. * 1 0 0 0
  940. * 1 0 1 1
  941. * 0 1 0 1
  942. * 0 1 1 0
  943. */
  944. return !status ^ !(mmc->caps2 & MMC_CAP2_RO_ACTIVE_HIGH);
  945. }
  946. static void usdhi6_enable_sdio_irq(struct mmc_host *mmc, int enable)
  947. {
  948. struct usdhi6_host *host = mmc_priv(mmc);
  949. dev_dbg(mmc_dev(mmc), "%s(): %sable\n", __func__, enable ? "en" : "dis");
  950. if (enable) {
  951. host->sdio_mask = USDHI6_SDIO_INFO1_IRQ & ~USDHI6_SDIO_INFO1_IOIRQ;
  952. usdhi6_write(host, USDHI6_SDIO_INFO1_MASK, host->sdio_mask);
  953. usdhi6_write(host, USDHI6_SDIO_MODE, 1);
  954. } else {
  955. usdhi6_write(host, USDHI6_SDIO_MODE, 0);
  956. usdhi6_write(host, USDHI6_SDIO_INFO1_MASK, USDHI6_SDIO_INFO1_IRQ);
  957. host->sdio_mask = USDHI6_SDIO_INFO1_IRQ;
  958. }
  959. }
  960. static int usdhi6_set_pinstates(struct usdhi6_host *host, int voltage)
  961. {
  962. if (IS_ERR(host->pins_uhs))
  963. return 0;
  964. switch (voltage) {
  965. case MMC_SIGNAL_VOLTAGE_180:
  966. case MMC_SIGNAL_VOLTAGE_120:
  967. return pinctrl_select_state(host->pinctrl,
  968. host->pins_uhs);
  969. default:
  970. return pinctrl_select_default_state(mmc_dev(host->mmc));
  971. }
  972. }
  973. static int usdhi6_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  974. {
  975. int ret;
  976. ret = mmc_regulator_set_vqmmc(mmc, ios);
  977. if (ret < 0)
  978. return ret;
  979. ret = usdhi6_set_pinstates(mmc_priv(mmc), ios->signal_voltage);
  980. if (ret)
  981. dev_warn_once(mmc_dev(mmc),
  982. "Failed to set pinstate err=%d\n", ret);
  983. return ret;
  984. }
  985. static int usdhi6_card_busy(struct mmc_host *mmc)
  986. {
  987. struct usdhi6_host *host = mmc_priv(mmc);
  988. u32 tmp = usdhi6_read(host, USDHI6_SD_INFO2);
  989. /* Card is busy if it is pulling dat[0] low */
  990. return !(tmp & USDHI6_SD_INFO2_SDDAT0);
  991. }
  992. static const struct mmc_host_ops usdhi6_ops = {
  993. .request = usdhi6_request,
  994. .set_ios = usdhi6_set_ios,
  995. .get_cd = usdhi6_get_cd,
  996. .get_ro = usdhi6_get_ro,
  997. .enable_sdio_irq = usdhi6_enable_sdio_irq,
  998. .start_signal_voltage_switch = usdhi6_sig_volt_switch,
  999. .card_busy = usdhi6_card_busy,
  1000. };
  1001. /* State machine handlers */
  1002. static void usdhi6_resp_cmd12(struct usdhi6_host *host)
  1003. {
  1004. struct mmc_command *cmd = host->mrq->stop;
  1005. cmd->resp[0] = usdhi6_read(host, USDHI6_SD_RSP10);
  1006. }
  1007. static void usdhi6_resp_read(struct usdhi6_host *host)
  1008. {
  1009. struct mmc_command *cmd = host->mrq->cmd;
  1010. u32 *rsp = cmd->resp, tmp = 0;
  1011. int i;
  1012. /*
  1013. * RSP10 39-8
  1014. * RSP32 71-40
  1015. * RSP54 103-72
  1016. * RSP76 127-104
  1017. * R2-type response:
  1018. * resp[0] = r[127..96]
  1019. * resp[1] = r[95..64]
  1020. * resp[2] = r[63..32]
  1021. * resp[3] = r[31..0]
  1022. * Other responses:
  1023. * resp[0] = r[39..8]
  1024. */
  1025. if (mmc_resp_type(cmd) == MMC_RSP_NONE)
  1026. return;
  1027. if (!(host->irq_status & USDHI6_SD_INFO1_RSP_END)) {
  1028. dev_err(mmc_dev(host->mmc),
  1029. "CMD%d: response expected but is missing!\n", cmd->opcode);
  1030. return;
  1031. }
  1032. if (mmc_resp_type(cmd) & MMC_RSP_136)
  1033. for (i = 0; i < 4; i++) {
  1034. if (i)
  1035. rsp[3 - i] = tmp >> 24;
  1036. tmp = usdhi6_read(host, USDHI6_SD_RSP10 + i * 8);
  1037. rsp[3 - i] |= tmp << 8;
  1038. }
  1039. else if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  1040. cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)
  1041. /* Read RSP54 to avoid conflict with auto CMD12 */
  1042. rsp[0] = usdhi6_read(host, USDHI6_SD_RSP54);
  1043. else
  1044. rsp[0] = usdhi6_read(host, USDHI6_SD_RSP10);
  1045. dev_dbg(mmc_dev(host->mmc), "Response 0x%x\n", rsp[0]);
  1046. }
  1047. static int usdhi6_blk_read(struct usdhi6_host *host)
  1048. {
  1049. struct mmc_data *data = host->mrq->data;
  1050. u32 *p;
  1051. int i, rest;
  1052. if (host->io_error) {
  1053. data->error = usdhi6_error_code(host);
  1054. goto error;
  1055. }
  1056. if (host->pg.page) {
  1057. p = host->blk_page + host->offset;
  1058. } else {
  1059. p = usdhi6_sg_map(host);
  1060. if (!p) {
  1061. data->error = -ENOMEM;
  1062. goto error;
  1063. }
  1064. }
  1065. for (i = 0; i < data->blksz / 4; i++, p++)
  1066. *p = usdhi6_read(host, USDHI6_SD_BUF0);
  1067. rest = data->blksz % 4;
  1068. for (i = 0; i < (rest + 1) / 2; i++) {
  1069. u16 d = usdhi6_read16(host, USDHI6_SD_BUF0);
  1070. ((u8 *)p)[2 * i] = ((u8 *)&d)[0];
  1071. if (rest > 1 && !i)
  1072. ((u8 *)p)[2 * i + 1] = ((u8 *)&d)[1];
  1073. }
  1074. return 0;
  1075. error:
  1076. dev_dbg(mmc_dev(host->mmc), "%s(): %d\n", __func__, data->error);
  1077. host->wait = USDHI6_WAIT_FOR_REQUEST;
  1078. return data->error;
  1079. }
  1080. static int usdhi6_blk_write(struct usdhi6_host *host)
  1081. {
  1082. struct mmc_data *data = host->mrq->data;
  1083. u32 *p;
  1084. int i, rest;
  1085. if (host->io_error) {
  1086. data->error = usdhi6_error_code(host);
  1087. goto error;
  1088. }
  1089. if (host->pg.page) {
  1090. p = host->blk_page + host->offset;
  1091. } else {
  1092. p = usdhi6_sg_map(host);
  1093. if (!p) {
  1094. data->error = -ENOMEM;
  1095. goto error;
  1096. }
  1097. }
  1098. for (i = 0; i < data->blksz / 4; i++, p++)
  1099. usdhi6_write(host, USDHI6_SD_BUF0, *p);
  1100. rest = data->blksz % 4;
  1101. for (i = 0; i < (rest + 1) / 2; i++) {
  1102. u16 d;
  1103. ((u8 *)&d)[0] = ((u8 *)p)[2 * i];
  1104. if (rest > 1 && !i)
  1105. ((u8 *)&d)[1] = ((u8 *)p)[2 * i + 1];
  1106. else
  1107. ((u8 *)&d)[1] = 0;
  1108. usdhi6_write16(host, USDHI6_SD_BUF0, d);
  1109. }
  1110. return 0;
  1111. error:
  1112. dev_dbg(mmc_dev(host->mmc), "%s(): %d\n", __func__, data->error);
  1113. host->wait = USDHI6_WAIT_FOR_REQUEST;
  1114. return data->error;
  1115. }
  1116. static int usdhi6_stop_cmd(struct usdhi6_host *host)
  1117. {
  1118. struct mmc_request *mrq = host->mrq;
  1119. switch (mrq->cmd->opcode) {
  1120. case MMC_READ_MULTIPLE_BLOCK:
  1121. case MMC_WRITE_MULTIPLE_BLOCK:
  1122. if (mrq->stop->opcode == MMC_STOP_TRANSMISSION) {
  1123. host->wait = USDHI6_WAIT_FOR_STOP;
  1124. return 0;
  1125. }
  1126. fallthrough; /* Unsupported STOP command */
  1127. default:
  1128. dev_err(mmc_dev(host->mmc),
  1129. "unsupported stop CMD%d for CMD%d\n",
  1130. mrq->stop->opcode, mrq->cmd->opcode);
  1131. mrq->stop->error = -EOPNOTSUPP;
  1132. }
  1133. return -EOPNOTSUPP;
  1134. }
  1135. static bool usdhi6_end_cmd(struct usdhi6_host *host)
  1136. {
  1137. struct mmc_request *mrq = host->mrq;
  1138. struct mmc_command *cmd = mrq->cmd;
  1139. if (host->io_error) {
  1140. cmd->error = usdhi6_error_code(host);
  1141. return false;
  1142. }
  1143. usdhi6_resp_read(host);
  1144. if (!mrq->data)
  1145. return false;
  1146. if (host->dma_active) {
  1147. usdhi6_dma_kick(host);
  1148. if (!mrq->stop)
  1149. host->wait = USDHI6_WAIT_FOR_DMA;
  1150. else if (usdhi6_stop_cmd(host) < 0)
  1151. return false;
  1152. } else if (mrq->data->flags & MMC_DATA_READ) {
  1153. if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  1154. (cmd->opcode == SD_IO_RW_EXTENDED &&
  1155. mrq->data->blocks > 1))
  1156. host->wait = USDHI6_WAIT_FOR_MREAD;
  1157. else
  1158. host->wait = USDHI6_WAIT_FOR_READ;
  1159. } else {
  1160. if (cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK ||
  1161. (cmd->opcode == SD_IO_RW_EXTENDED &&
  1162. mrq->data->blocks > 1))
  1163. host->wait = USDHI6_WAIT_FOR_MWRITE;
  1164. else
  1165. host->wait = USDHI6_WAIT_FOR_WRITE;
  1166. }
  1167. return true;
  1168. }
  1169. static bool usdhi6_read_block(struct usdhi6_host *host)
  1170. {
  1171. /* ACCESS_END IRQ is already unmasked */
  1172. int ret = usdhi6_blk_read(host);
  1173. /*
  1174. * Have to force unmapping both pages: the single block could have been
  1175. * cross-page, in which case for single-block IO host->page_idx == 0.
  1176. * So, if we don't force, the second page won't be unmapped.
  1177. */
  1178. usdhi6_sg_unmap(host, true);
  1179. if (ret < 0)
  1180. return false;
  1181. host->wait = USDHI6_WAIT_FOR_DATA_END;
  1182. return true;
  1183. }
  1184. static bool usdhi6_mread_block(struct usdhi6_host *host)
  1185. {
  1186. int ret = usdhi6_blk_read(host);
  1187. if (ret < 0)
  1188. return false;
  1189. usdhi6_sg_advance(host);
  1190. return !host->mrq->data->error &&
  1191. (host->wait != USDHI6_WAIT_FOR_DATA_END || !host->mrq->stop);
  1192. }
  1193. static bool usdhi6_write_block(struct usdhi6_host *host)
  1194. {
  1195. int ret = usdhi6_blk_write(host);
  1196. /* See comment in usdhi6_read_block() */
  1197. usdhi6_sg_unmap(host, true);
  1198. if (ret < 0)
  1199. return false;
  1200. host->wait = USDHI6_WAIT_FOR_DATA_END;
  1201. return true;
  1202. }
  1203. static bool usdhi6_mwrite_block(struct usdhi6_host *host)
  1204. {
  1205. int ret = usdhi6_blk_write(host);
  1206. if (ret < 0)
  1207. return false;
  1208. usdhi6_sg_advance(host);
  1209. return !host->mrq->data->error &&
  1210. (host->wait != USDHI6_WAIT_FOR_DATA_END || !host->mrq->stop);
  1211. }
  1212. /* Interrupt & timeout handlers */
  1213. static irqreturn_t usdhi6_sd_bh(int irq, void *dev_id)
  1214. {
  1215. struct usdhi6_host *host = dev_id;
  1216. struct mmc_request *mrq;
  1217. struct mmc_command *cmd;
  1218. struct mmc_data *data;
  1219. bool io_wait = false;
  1220. cancel_delayed_work_sync(&host->timeout_work);
  1221. mrq = host->mrq;
  1222. if (!mrq)
  1223. return IRQ_HANDLED;
  1224. cmd = mrq->cmd;
  1225. data = mrq->data;
  1226. switch (host->wait) {
  1227. case USDHI6_WAIT_FOR_REQUEST:
  1228. /* We're too late, the timeout has already kicked in */
  1229. return IRQ_HANDLED;
  1230. case USDHI6_WAIT_FOR_CMD:
  1231. /* Wait for data? */
  1232. io_wait = usdhi6_end_cmd(host);
  1233. break;
  1234. case USDHI6_WAIT_FOR_MREAD:
  1235. /* Wait for more data? */
  1236. io_wait = usdhi6_mread_block(host);
  1237. break;
  1238. case USDHI6_WAIT_FOR_READ:
  1239. /* Wait for data end? */
  1240. io_wait = usdhi6_read_block(host);
  1241. break;
  1242. case USDHI6_WAIT_FOR_MWRITE:
  1243. /* Wait data to write? */
  1244. io_wait = usdhi6_mwrite_block(host);
  1245. break;
  1246. case USDHI6_WAIT_FOR_WRITE:
  1247. /* Wait for data end? */
  1248. io_wait = usdhi6_write_block(host);
  1249. break;
  1250. case USDHI6_WAIT_FOR_DMA:
  1251. usdhi6_dma_check_error(host);
  1252. break;
  1253. case USDHI6_WAIT_FOR_STOP:
  1254. usdhi6_write(host, USDHI6_SD_STOP, 0);
  1255. if (host->io_error) {
  1256. int ret = usdhi6_error_code(host);
  1257. if (mrq->stop)
  1258. mrq->stop->error = ret;
  1259. else
  1260. mrq->data->error = ret;
  1261. dev_warn(mmc_dev(host->mmc), "%s(): %d\n", __func__, ret);
  1262. break;
  1263. }
  1264. usdhi6_resp_cmd12(host);
  1265. mrq->stop->error = 0;
  1266. break;
  1267. case USDHI6_WAIT_FOR_DATA_END:
  1268. if (host->io_error) {
  1269. mrq->data->error = usdhi6_error_code(host);
  1270. dev_warn(mmc_dev(host->mmc), "%s(): %d\n", __func__,
  1271. mrq->data->error);
  1272. }
  1273. break;
  1274. default:
  1275. cmd->error = -EFAULT;
  1276. dev_err(mmc_dev(host->mmc), "Invalid state %u\n", host->wait);
  1277. usdhi6_request_done(host);
  1278. return IRQ_HANDLED;
  1279. }
  1280. if (io_wait) {
  1281. schedule_delayed_work(&host->timeout_work, host->timeout);
  1282. /* Wait for more data or ACCESS_END */
  1283. if (!host->dma_active)
  1284. usdhi6_wait_for_brwe(host, mrq->data->flags & MMC_DATA_READ);
  1285. return IRQ_HANDLED;
  1286. }
  1287. if (!cmd->error) {
  1288. if (data) {
  1289. if (!data->error) {
  1290. if (host->wait != USDHI6_WAIT_FOR_STOP &&
  1291. host->mrq->stop &&
  1292. !host->mrq->stop->error &&
  1293. !usdhi6_stop_cmd(host)) {
  1294. /* Sending STOP */
  1295. usdhi6_wait_for_resp(host);
  1296. schedule_delayed_work(&host->timeout_work,
  1297. host->timeout);
  1298. return IRQ_HANDLED;
  1299. }
  1300. data->bytes_xfered = data->blocks * data->blksz;
  1301. } else {
  1302. /* Data error: might need to unmap the last page */
  1303. dev_warn(mmc_dev(host->mmc), "%s(): data error %d\n",
  1304. __func__, data->error);
  1305. usdhi6_sg_unmap(host, true);
  1306. }
  1307. } else if (cmd->opcode == MMC_APP_CMD) {
  1308. host->app_cmd = true;
  1309. }
  1310. }
  1311. usdhi6_request_done(host);
  1312. return IRQ_HANDLED;
  1313. }
  1314. static irqreturn_t usdhi6_sd(int irq, void *dev_id)
  1315. {
  1316. struct usdhi6_host *host = dev_id;
  1317. u16 status, status2, error;
  1318. status = usdhi6_read(host, USDHI6_SD_INFO1) & ~host->status_mask &
  1319. ~USDHI6_SD_INFO1_CARD;
  1320. status2 = usdhi6_read(host, USDHI6_SD_INFO2) & ~host->status2_mask;
  1321. usdhi6_only_cd(host);
  1322. dev_dbg(mmc_dev(host->mmc),
  1323. "IRQ status = 0x%08x, status2 = 0x%08x\n", status, status2);
  1324. if (!status && !status2)
  1325. return IRQ_NONE;
  1326. error = status2 & USDHI6_SD_INFO2_ERR;
  1327. /* Ack / clear interrupts */
  1328. if (USDHI6_SD_INFO1_IRQ & status)
  1329. usdhi6_write(host, USDHI6_SD_INFO1,
  1330. 0xffff & ~(USDHI6_SD_INFO1_IRQ & status));
  1331. if (USDHI6_SD_INFO2_IRQ & status2) {
  1332. if (error)
  1333. /* In error cases BWE and BRE aren't cleared automatically */
  1334. status2 |= USDHI6_SD_INFO2_BWE | USDHI6_SD_INFO2_BRE;
  1335. usdhi6_write(host, USDHI6_SD_INFO2,
  1336. 0xffff & ~(USDHI6_SD_INFO2_IRQ & status2));
  1337. }
  1338. host->io_error = error;
  1339. host->irq_status = status;
  1340. if (error) {
  1341. /* Don't pollute the log with unsupported command timeouts */
  1342. if (host->wait != USDHI6_WAIT_FOR_CMD ||
  1343. error != USDHI6_SD_INFO2_RSP_TOUT)
  1344. dev_warn(mmc_dev(host->mmc),
  1345. "%s(): INFO2 error bits 0x%08x\n",
  1346. __func__, error);
  1347. else
  1348. dev_dbg(mmc_dev(host->mmc),
  1349. "%s(): INFO2 error bits 0x%08x\n",
  1350. __func__, error);
  1351. }
  1352. return IRQ_WAKE_THREAD;
  1353. }
  1354. static irqreturn_t usdhi6_sdio(int irq, void *dev_id)
  1355. {
  1356. struct usdhi6_host *host = dev_id;
  1357. u32 status = usdhi6_read(host, USDHI6_SDIO_INFO1) & ~host->sdio_mask;
  1358. dev_dbg(mmc_dev(host->mmc), "%s(): status 0x%x\n", __func__, status);
  1359. if (!status)
  1360. return IRQ_NONE;
  1361. usdhi6_write(host, USDHI6_SDIO_INFO1, ~status);
  1362. mmc_signal_sdio_irq(host->mmc);
  1363. return IRQ_HANDLED;
  1364. }
  1365. static irqreturn_t usdhi6_cd(int irq, void *dev_id)
  1366. {
  1367. struct usdhi6_host *host = dev_id;
  1368. struct mmc_host *mmc = host->mmc;
  1369. u16 status;
  1370. /* We're only interested in hotplug events here */
  1371. status = usdhi6_read(host, USDHI6_SD_INFO1) & ~host->status_mask &
  1372. USDHI6_SD_INFO1_CARD;
  1373. if (!status)
  1374. return IRQ_NONE;
  1375. /* Ack */
  1376. usdhi6_write(host, USDHI6_SD_INFO1, ~status);
  1377. if (!work_pending(&mmc->detect.work) &&
  1378. (((status & USDHI6_SD_INFO1_CARD_INSERT) &&
  1379. !mmc->card) ||
  1380. ((status & USDHI6_SD_INFO1_CARD_EJECT) &&
  1381. mmc->card)))
  1382. mmc_detect_change(mmc, msecs_to_jiffies(100));
  1383. return IRQ_HANDLED;
  1384. }
  1385. /*
  1386. * Actually this should not be needed, if the built-in timeout works reliably in
  1387. * the both PIO cases and DMA never fails. But if DMA does fail, a timeout
  1388. * handler might be the only way to catch the error.
  1389. */
  1390. static void usdhi6_timeout_work(struct work_struct *work)
  1391. {
  1392. struct delayed_work *d = to_delayed_work(work);
  1393. struct usdhi6_host *host = container_of(d, struct usdhi6_host, timeout_work);
  1394. struct mmc_request *mrq = host->mrq;
  1395. struct mmc_data *data = mrq ? mrq->data : NULL;
  1396. struct scatterlist *sg;
  1397. dev_warn(mmc_dev(host->mmc),
  1398. "%s timeout wait %u CMD%d: IRQ 0x%08x:0x%08x, last IRQ 0x%08x\n",
  1399. host->dma_active ? "DMA" : "PIO",
  1400. host->wait, mrq ? mrq->cmd->opcode : -1,
  1401. usdhi6_read(host, USDHI6_SD_INFO1),
  1402. usdhi6_read(host, USDHI6_SD_INFO2), host->irq_status);
  1403. if (host->dma_active) {
  1404. usdhi6_dma_kill(host);
  1405. usdhi6_dma_stop_unmap(host);
  1406. }
  1407. switch (host->wait) {
  1408. default:
  1409. dev_err(mmc_dev(host->mmc), "Invalid state %u\n", host->wait);
  1410. fallthrough; /* mrq can be NULL, but is impossible */
  1411. case USDHI6_WAIT_FOR_CMD:
  1412. usdhi6_error_code(host);
  1413. if (mrq)
  1414. mrq->cmd->error = -ETIMEDOUT;
  1415. break;
  1416. case USDHI6_WAIT_FOR_STOP:
  1417. usdhi6_error_code(host);
  1418. mrq->stop->error = -ETIMEDOUT;
  1419. break;
  1420. case USDHI6_WAIT_FOR_DMA:
  1421. case USDHI6_WAIT_FOR_MREAD:
  1422. case USDHI6_WAIT_FOR_MWRITE:
  1423. case USDHI6_WAIT_FOR_READ:
  1424. case USDHI6_WAIT_FOR_WRITE:
  1425. sg = host->sg ?: data->sg;
  1426. dev_dbg(mmc_dev(host->mmc),
  1427. "%c: page #%u @ +0x%zx %ux%u in SG%u. Current SG %u bytes @ %u\n",
  1428. data->flags & MMC_DATA_READ ? 'R' : 'W', host->page_idx,
  1429. host->offset, data->blocks, data->blksz, data->sg_len,
  1430. sg_dma_len(sg), sg->offset);
  1431. usdhi6_sg_unmap(host, true);
  1432. fallthrough; /* page unmapped in USDHI6_WAIT_FOR_DATA_END */
  1433. case USDHI6_WAIT_FOR_DATA_END:
  1434. usdhi6_error_code(host);
  1435. data->error = -ETIMEDOUT;
  1436. }
  1437. if (mrq)
  1438. usdhi6_request_done(host);
  1439. }
  1440. /* Probe / release */
  1441. static const struct of_device_id usdhi6_of_match[] = {
  1442. {.compatible = "renesas,usdhi6rol0"},
  1443. {}
  1444. };
  1445. MODULE_DEVICE_TABLE(of, usdhi6_of_match);
  1446. static int usdhi6_probe(struct platform_device *pdev)
  1447. {
  1448. struct device *dev = &pdev->dev;
  1449. struct mmc_host *mmc;
  1450. struct usdhi6_host *host;
  1451. struct resource *res;
  1452. int irq_cd, irq_sd, irq_sdio;
  1453. u32 version;
  1454. int ret;
  1455. if (!dev->of_node)
  1456. return -ENODEV;
  1457. irq_cd = platform_get_irq_byname(pdev, "card detect");
  1458. irq_sd = platform_get_irq_byname(pdev, "data");
  1459. irq_sdio = platform_get_irq_byname(pdev, "SDIO");
  1460. if (irq_sd < 0)
  1461. return irq_sd;
  1462. if (irq_sdio < 0)
  1463. return irq_sdio;
  1464. mmc = mmc_alloc_host(sizeof(struct usdhi6_host), dev);
  1465. if (!mmc)
  1466. return -ENOMEM;
  1467. ret = mmc_regulator_get_supply(mmc);
  1468. if (ret)
  1469. goto e_free_mmc;
  1470. ret = mmc_of_parse(mmc);
  1471. if (ret < 0)
  1472. goto e_free_mmc;
  1473. host = mmc_priv(mmc);
  1474. host->mmc = mmc;
  1475. host->wait = USDHI6_WAIT_FOR_REQUEST;
  1476. host->timeout = msecs_to_jiffies(USDHI6_REQ_TIMEOUT_MS);
  1477. /*
  1478. * We use a fixed timeout of 4s, hence inform the core about it. A
  1479. * future improvement should instead respect the cmd->busy_timeout.
  1480. */
  1481. mmc->max_busy_timeout = USDHI6_REQ_TIMEOUT_MS;
  1482. host->pinctrl = devm_pinctrl_get(&pdev->dev);
  1483. if (IS_ERR(host->pinctrl)) {
  1484. ret = PTR_ERR(host->pinctrl);
  1485. goto e_free_mmc;
  1486. }
  1487. host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs");
  1488. host->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1489. if (IS_ERR(host->base)) {
  1490. ret = PTR_ERR(host->base);
  1491. goto e_free_mmc;
  1492. }
  1493. host->clk = devm_clk_get(dev, NULL);
  1494. if (IS_ERR(host->clk)) {
  1495. ret = PTR_ERR(host->clk);
  1496. goto e_free_mmc;
  1497. }
  1498. host->imclk = clk_get_rate(host->clk);
  1499. ret = clk_prepare_enable(host->clk);
  1500. if (ret < 0)
  1501. goto e_free_mmc;
  1502. version = usdhi6_read(host, USDHI6_VERSION);
  1503. if ((version & 0xfff) != 0xa0d) {
  1504. ret = -EPERM;
  1505. dev_err(dev, "Version not recognized %x\n", version);
  1506. goto e_clk_off;
  1507. }
  1508. dev_info(dev, "A USDHI6ROL0 SD host detected with %d ports\n",
  1509. usdhi6_read(host, USDHI6_SD_PORT_SEL) >> USDHI6_SD_PORT_SEL_PORTS_SHIFT);
  1510. usdhi6_mask_all(host);
  1511. if (irq_cd >= 0) {
  1512. ret = devm_request_irq(dev, irq_cd, usdhi6_cd, 0,
  1513. dev_name(dev), host);
  1514. if (ret < 0)
  1515. goto e_clk_off;
  1516. } else {
  1517. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1518. }
  1519. ret = devm_request_threaded_irq(dev, irq_sd, usdhi6_sd, usdhi6_sd_bh, 0,
  1520. dev_name(dev), host);
  1521. if (ret < 0)
  1522. goto e_clk_off;
  1523. ret = devm_request_irq(dev, irq_sdio, usdhi6_sdio, 0,
  1524. dev_name(dev), host);
  1525. if (ret < 0)
  1526. goto e_clk_off;
  1527. INIT_DELAYED_WORK(&host->timeout_work, usdhi6_timeout_work);
  1528. usdhi6_dma_request(host, res->start);
  1529. mmc->ops = &usdhi6_ops;
  1530. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  1531. MMC_CAP_SDIO_IRQ;
  1532. /* Set .max_segs to some random number. Feel free to adjust. */
  1533. mmc->max_segs = 32;
  1534. mmc->max_blk_size = 512;
  1535. mmc->max_req_size = PAGE_SIZE * mmc->max_segs;
  1536. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1537. /*
  1538. * Setting .max_seg_size to 1 page would simplify our page-mapping code,
  1539. * But OTOH, having large segments makes DMA more efficient. We could
  1540. * check, whether we managed to get DMA and fall back to 1 page
  1541. * segments, but if we do manage to obtain DMA and then it fails at
  1542. * run-time and we fall back to PIO, we will continue getting large
  1543. * segments. So, we wouldn't be able to get rid of the code anyway.
  1544. */
  1545. mmc->max_seg_size = mmc->max_req_size;
  1546. if (!mmc->f_max)
  1547. mmc->f_max = host->imclk;
  1548. mmc->f_min = host->imclk / 512;
  1549. platform_set_drvdata(pdev, host);
  1550. ret = mmc_add_host(mmc);
  1551. if (ret < 0)
  1552. goto e_release_dma;
  1553. return 0;
  1554. e_release_dma:
  1555. usdhi6_dma_release(host);
  1556. e_clk_off:
  1557. clk_disable_unprepare(host->clk);
  1558. e_free_mmc:
  1559. mmc_free_host(mmc);
  1560. return ret;
  1561. }
  1562. static void usdhi6_remove(struct platform_device *pdev)
  1563. {
  1564. struct usdhi6_host *host = platform_get_drvdata(pdev);
  1565. mmc_remove_host(host->mmc);
  1566. usdhi6_mask_all(host);
  1567. cancel_delayed_work_sync(&host->timeout_work);
  1568. usdhi6_dma_release(host);
  1569. clk_disable_unprepare(host->clk);
  1570. mmc_free_host(host->mmc);
  1571. }
  1572. static struct platform_driver usdhi6_driver = {
  1573. .probe = usdhi6_probe,
  1574. .remove_new = usdhi6_remove,
  1575. .driver = {
  1576. .name = "usdhi6rol0",
  1577. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1578. .of_match_table = usdhi6_of_match,
  1579. },
  1580. };
  1581. module_platform_driver(usdhi6_driver);
  1582. MODULE_DESCRIPTION("Renesas usdhi6rol0 SD/SDIO host driver");
  1583. MODULE_LICENSE("GPL v2");
  1584. MODULE_ALIAS("platform:usdhi6rol0");
  1585. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");