usdhi6rol0.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905
  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 = 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 = 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 = devm_mmc_alloc_host(dev, sizeof(*host));
  1465. if (!mmc)
  1466. return -ENOMEM;
  1467. ret = mmc_regulator_get_supply(mmc);
  1468. if (ret)
  1469. return ret;
  1470. ret = mmc_of_parse(mmc);
  1471. if (ret < 0)
  1472. return ret;
  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. return PTR_ERR(host->pinctrl);
  1485. host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs");
  1486. host->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1487. if (IS_ERR(host->base))
  1488. return PTR_ERR(host->base);
  1489. host->clk = devm_clk_get(dev, NULL);
  1490. if (IS_ERR(host->clk))
  1491. return PTR_ERR(host->clk);
  1492. host->imclk = clk_get_rate(host->clk);
  1493. ret = clk_prepare_enable(host->clk);
  1494. if (ret < 0)
  1495. return ret;
  1496. version = usdhi6_read(host, USDHI6_VERSION);
  1497. if ((version & 0xfff) != 0xa0d) {
  1498. ret = -EPERM;
  1499. dev_err(dev, "Version not recognized %x\n", version);
  1500. goto e_clk_off;
  1501. }
  1502. dev_info(dev, "A USDHI6ROL0 SD host detected with %d ports\n",
  1503. usdhi6_read(host, USDHI6_SD_PORT_SEL) >> USDHI6_SD_PORT_SEL_PORTS_SHIFT);
  1504. usdhi6_mask_all(host);
  1505. if (irq_cd >= 0) {
  1506. ret = devm_request_irq(dev, irq_cd, usdhi6_cd, 0,
  1507. dev_name(dev), host);
  1508. if (ret < 0)
  1509. goto e_clk_off;
  1510. } else {
  1511. mmc->caps |= MMC_CAP_NEEDS_POLL;
  1512. }
  1513. ret = devm_request_threaded_irq(dev, irq_sd, usdhi6_sd, usdhi6_sd_bh, 0,
  1514. dev_name(dev), host);
  1515. if (ret < 0)
  1516. goto e_clk_off;
  1517. ret = devm_request_irq(dev, irq_sdio, usdhi6_sdio, 0,
  1518. dev_name(dev), host);
  1519. if (ret < 0)
  1520. goto e_clk_off;
  1521. INIT_DELAYED_WORK(&host->timeout_work, usdhi6_timeout_work);
  1522. usdhi6_dma_request(host, res->start);
  1523. mmc->ops = &usdhi6_ops;
  1524. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED |
  1525. MMC_CAP_SDIO_IRQ;
  1526. /* Set .max_segs to some random number. Feel free to adjust. */
  1527. mmc->max_segs = 32;
  1528. mmc->max_blk_size = 512;
  1529. mmc->max_req_size = PAGE_SIZE * mmc->max_segs;
  1530. mmc->max_blk_count = mmc->max_req_size / mmc->max_blk_size;
  1531. /*
  1532. * Setting .max_seg_size to 1 page would simplify our page-mapping code,
  1533. * But OTOH, having large segments makes DMA more efficient. We could
  1534. * check, whether we managed to get DMA and fall back to 1 page
  1535. * segments, but if we do manage to obtain DMA and then it fails at
  1536. * run-time and we fall back to PIO, we will continue getting large
  1537. * segments. So, we wouldn't be able to get rid of the code anyway.
  1538. */
  1539. mmc->max_seg_size = mmc->max_req_size;
  1540. if (!mmc->f_max)
  1541. mmc->f_max = host->imclk;
  1542. mmc->f_min = host->imclk / 512;
  1543. platform_set_drvdata(pdev, host);
  1544. ret = mmc_add_host(mmc);
  1545. if (ret < 0)
  1546. goto e_release_dma;
  1547. return 0;
  1548. e_release_dma:
  1549. usdhi6_dma_release(host);
  1550. e_clk_off:
  1551. clk_disable_unprepare(host->clk);
  1552. return ret;
  1553. }
  1554. static void usdhi6_remove(struct platform_device *pdev)
  1555. {
  1556. struct usdhi6_host *host = platform_get_drvdata(pdev);
  1557. mmc_remove_host(host->mmc);
  1558. usdhi6_mask_all(host);
  1559. cancel_delayed_work_sync(&host->timeout_work);
  1560. usdhi6_dma_release(host);
  1561. clk_disable_unprepare(host->clk);
  1562. }
  1563. static struct platform_driver usdhi6_driver = {
  1564. .probe = usdhi6_probe,
  1565. .remove = usdhi6_remove,
  1566. .driver = {
  1567. .name = "usdhi6rol0",
  1568. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1569. .of_match_table = usdhi6_of_match,
  1570. },
  1571. };
  1572. module_platform_driver(usdhi6_driver);
  1573. MODULE_DESCRIPTION("Renesas usdhi6rol0 SD/SDIO host driver");
  1574. MODULE_LICENSE("GPL v2");
  1575. MODULE_ALIAS("platform:usdhi6rol0");
  1576. MODULE_AUTHOR("Guennadi Liakhovetski <g.liakhovetski@gmx.de>");