usdhi6rol0.c 48 KB

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