usdhi6rol0.c 48 KB

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