usdhi6rol0.c 47 KB

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