mtk-sd.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730
  1. /*
  2. * Copyright (c) 2014-2015 MediaTek Inc.
  3. * Author: Chaotian.Jing <chaotian.jing@mediatek.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <linux/module.h>
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/ioport.h>
  19. #include <linux/irq.h>
  20. #include <linux/of_address.h>
  21. #include <linux/of_irq.h>
  22. #include <linux/of_gpio.h>
  23. #include <linux/pinctrl/consumer.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/pm.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/regulator/consumer.h>
  28. #include <linux/slab.h>
  29. #include <linux/spinlock.h>
  30. #include <linux/mmc/card.h>
  31. #include <linux/mmc/core.h>
  32. #include <linux/mmc/host.h>
  33. #include <linux/mmc/mmc.h>
  34. #include <linux/mmc/sd.h>
  35. #include <linux/mmc/sdio.h>
  36. #include <linux/mmc/slot-gpio.h>
  37. #define MAX_BD_NUM 1024
  38. /*--------------------------------------------------------------------------*/
  39. /* Common Definition */
  40. /*--------------------------------------------------------------------------*/
  41. #define MSDC_BUS_1BITS 0x0
  42. #define MSDC_BUS_4BITS 0x1
  43. #define MSDC_BUS_8BITS 0x2
  44. #define MSDC_BURST_64B 0x6
  45. /*--------------------------------------------------------------------------*/
  46. /* Register Offset */
  47. /*--------------------------------------------------------------------------*/
  48. #define MSDC_CFG 0x0
  49. #define MSDC_IOCON 0x04
  50. #define MSDC_PS 0x08
  51. #define MSDC_INT 0x0c
  52. #define MSDC_INTEN 0x10
  53. #define MSDC_FIFOCS 0x14
  54. #define SDC_CFG 0x30
  55. #define SDC_CMD 0x34
  56. #define SDC_ARG 0x38
  57. #define SDC_STS 0x3c
  58. #define SDC_RESP0 0x40
  59. #define SDC_RESP1 0x44
  60. #define SDC_RESP2 0x48
  61. #define SDC_RESP3 0x4c
  62. #define SDC_BLK_NUM 0x50
  63. #define EMMC_IOCON 0x7c
  64. #define SDC_ACMD_RESP 0x80
  65. #define MSDC_DMA_SA 0x90
  66. #define MSDC_DMA_CTRL 0x98
  67. #define MSDC_DMA_CFG 0x9c
  68. #define MSDC_PATCH_BIT 0xb0
  69. #define MSDC_PATCH_BIT1 0xb4
  70. #define MSDC_PAD_TUNE 0xec
  71. #define PAD_DS_TUNE 0x188
  72. #define EMMC50_CFG0 0x208
  73. /*--------------------------------------------------------------------------*/
  74. /* Register Mask */
  75. /*--------------------------------------------------------------------------*/
  76. /* MSDC_CFG mask */
  77. #define MSDC_CFG_MODE (0x1 << 0) /* RW */
  78. #define MSDC_CFG_CKPDN (0x1 << 1) /* RW */
  79. #define MSDC_CFG_RST (0x1 << 2) /* RW */
  80. #define MSDC_CFG_PIO (0x1 << 3) /* RW */
  81. #define MSDC_CFG_CKDRVEN (0x1 << 4) /* RW */
  82. #define MSDC_CFG_BV18SDT (0x1 << 5) /* RW */
  83. #define MSDC_CFG_BV18PSS (0x1 << 6) /* R */
  84. #define MSDC_CFG_CKSTB (0x1 << 7) /* R */
  85. #define MSDC_CFG_CKDIV (0xff << 8) /* RW */
  86. #define MSDC_CFG_CKMOD (0x3 << 16) /* RW */
  87. #define MSDC_CFG_HS400_CK_MODE (0x1 << 18) /* RW */
  88. /* MSDC_IOCON mask */
  89. #define MSDC_IOCON_SDR104CKS (0x1 << 0) /* RW */
  90. #define MSDC_IOCON_RSPL (0x1 << 1) /* RW */
  91. #define MSDC_IOCON_DSPL (0x1 << 2) /* RW */
  92. #define MSDC_IOCON_DDLSEL (0x1 << 3) /* RW */
  93. #define MSDC_IOCON_DDR50CKD (0x1 << 4) /* RW */
  94. #define MSDC_IOCON_DSPLSEL (0x1 << 5) /* RW */
  95. #define MSDC_IOCON_W_DSPL (0x1 << 8) /* RW */
  96. #define MSDC_IOCON_D0SPL (0x1 << 16) /* RW */
  97. #define MSDC_IOCON_D1SPL (0x1 << 17) /* RW */
  98. #define MSDC_IOCON_D2SPL (0x1 << 18) /* RW */
  99. #define MSDC_IOCON_D3SPL (0x1 << 19) /* RW */
  100. #define MSDC_IOCON_D4SPL (0x1 << 20) /* RW */
  101. #define MSDC_IOCON_D5SPL (0x1 << 21) /* RW */
  102. #define MSDC_IOCON_D6SPL (0x1 << 22) /* RW */
  103. #define MSDC_IOCON_D7SPL (0x1 << 23) /* RW */
  104. #define MSDC_IOCON_RISCSZ (0x3 << 24) /* RW */
  105. /* MSDC_PS mask */
  106. #define MSDC_PS_CDEN (0x1 << 0) /* RW */
  107. #define MSDC_PS_CDSTS (0x1 << 1) /* R */
  108. #define MSDC_PS_CDDEBOUNCE (0xf << 12) /* RW */
  109. #define MSDC_PS_DAT (0xff << 16) /* R */
  110. #define MSDC_PS_CMD (0x1 << 24) /* R */
  111. #define MSDC_PS_WP (0x1 << 31) /* R */
  112. /* MSDC_INT mask */
  113. #define MSDC_INT_MMCIRQ (0x1 << 0) /* W1C */
  114. #define MSDC_INT_CDSC (0x1 << 1) /* W1C */
  115. #define MSDC_INT_ACMDRDY (0x1 << 3) /* W1C */
  116. #define MSDC_INT_ACMDTMO (0x1 << 4) /* W1C */
  117. #define MSDC_INT_ACMDCRCERR (0x1 << 5) /* W1C */
  118. #define MSDC_INT_DMAQ_EMPTY (0x1 << 6) /* W1C */
  119. #define MSDC_INT_SDIOIRQ (0x1 << 7) /* W1C */
  120. #define MSDC_INT_CMDRDY (0x1 << 8) /* W1C */
  121. #define MSDC_INT_CMDTMO (0x1 << 9) /* W1C */
  122. #define MSDC_INT_RSPCRCERR (0x1 << 10) /* W1C */
  123. #define MSDC_INT_CSTA (0x1 << 11) /* R */
  124. #define MSDC_INT_XFER_COMPL (0x1 << 12) /* W1C */
  125. #define MSDC_INT_DXFER_DONE (0x1 << 13) /* W1C */
  126. #define MSDC_INT_DATTMO (0x1 << 14) /* W1C */
  127. #define MSDC_INT_DATCRCERR (0x1 << 15) /* W1C */
  128. #define MSDC_INT_ACMD19_DONE (0x1 << 16) /* W1C */
  129. #define MSDC_INT_DMA_BDCSERR (0x1 << 17) /* W1C */
  130. #define MSDC_INT_DMA_GPDCSERR (0x1 << 18) /* W1C */
  131. #define MSDC_INT_DMA_PROTECT (0x1 << 19) /* W1C */
  132. /* MSDC_INTEN mask */
  133. #define MSDC_INTEN_MMCIRQ (0x1 << 0) /* RW */
  134. #define MSDC_INTEN_CDSC (0x1 << 1) /* RW */
  135. #define MSDC_INTEN_ACMDRDY (0x1 << 3) /* RW */
  136. #define MSDC_INTEN_ACMDTMO (0x1 << 4) /* RW */
  137. #define MSDC_INTEN_ACMDCRCERR (0x1 << 5) /* RW */
  138. #define MSDC_INTEN_DMAQ_EMPTY (0x1 << 6) /* RW */
  139. #define MSDC_INTEN_SDIOIRQ (0x1 << 7) /* RW */
  140. #define MSDC_INTEN_CMDRDY (0x1 << 8) /* RW */
  141. #define MSDC_INTEN_CMDTMO (0x1 << 9) /* RW */
  142. #define MSDC_INTEN_RSPCRCERR (0x1 << 10) /* RW */
  143. #define MSDC_INTEN_CSTA (0x1 << 11) /* RW */
  144. #define MSDC_INTEN_XFER_COMPL (0x1 << 12) /* RW */
  145. #define MSDC_INTEN_DXFER_DONE (0x1 << 13) /* RW */
  146. #define MSDC_INTEN_DATTMO (0x1 << 14) /* RW */
  147. #define MSDC_INTEN_DATCRCERR (0x1 << 15) /* RW */
  148. #define MSDC_INTEN_ACMD19_DONE (0x1 << 16) /* RW */
  149. #define MSDC_INTEN_DMA_BDCSERR (0x1 << 17) /* RW */
  150. #define MSDC_INTEN_DMA_GPDCSERR (0x1 << 18) /* RW */
  151. #define MSDC_INTEN_DMA_PROTECT (0x1 << 19) /* RW */
  152. /* MSDC_FIFOCS mask */
  153. #define MSDC_FIFOCS_RXCNT (0xff << 0) /* R */
  154. #define MSDC_FIFOCS_TXCNT (0xff << 16) /* R */
  155. #define MSDC_FIFOCS_CLR (0x1 << 31) /* RW */
  156. /* SDC_CFG mask */
  157. #define SDC_CFG_SDIOINTWKUP (0x1 << 0) /* RW */
  158. #define SDC_CFG_INSWKUP (0x1 << 1) /* RW */
  159. #define SDC_CFG_BUSWIDTH (0x3 << 16) /* RW */
  160. #define SDC_CFG_SDIO (0x1 << 19) /* RW */
  161. #define SDC_CFG_SDIOIDE (0x1 << 20) /* RW */
  162. #define SDC_CFG_INTATGAP (0x1 << 21) /* RW */
  163. #define SDC_CFG_DTOC (0xff << 24) /* RW */
  164. /* SDC_STS mask */
  165. #define SDC_STS_SDCBUSY (0x1 << 0) /* RW */
  166. #define SDC_STS_CMDBUSY (0x1 << 1) /* RW */
  167. #define SDC_STS_SWR_COMPL (0x1 << 31) /* RW */
  168. /* MSDC_DMA_CTRL mask */
  169. #define MSDC_DMA_CTRL_START (0x1 << 0) /* W */
  170. #define MSDC_DMA_CTRL_STOP (0x1 << 1) /* W */
  171. #define MSDC_DMA_CTRL_RESUME (0x1 << 2) /* W */
  172. #define MSDC_DMA_CTRL_MODE (0x1 << 8) /* RW */
  173. #define MSDC_DMA_CTRL_LASTBUF (0x1 << 10) /* RW */
  174. #define MSDC_DMA_CTRL_BRUSTSZ (0x7 << 12) /* RW */
  175. /* MSDC_DMA_CFG mask */
  176. #define MSDC_DMA_CFG_STS (0x1 << 0) /* R */
  177. #define MSDC_DMA_CFG_DECSEN (0x1 << 1) /* RW */
  178. #define MSDC_DMA_CFG_AHBHPROT2 (0x2 << 8) /* RW */
  179. #define MSDC_DMA_CFG_ACTIVEEN (0x2 << 12) /* RW */
  180. #define MSDC_DMA_CFG_CS12B16B (0x1 << 16) /* RW */
  181. /* MSDC_PATCH_BIT mask */
  182. #define MSDC_PATCH_BIT_ODDSUPP (0x1 << 1) /* RW */
  183. #define MSDC_INT_DAT_LATCH_CK_SEL (0x7 << 7)
  184. #define MSDC_CKGEN_MSDC_DLY_SEL (0x1f << 10)
  185. #define MSDC_PATCH_BIT_IODSSEL (0x1 << 16) /* RW */
  186. #define MSDC_PATCH_BIT_IOINTSEL (0x1 << 17) /* RW */
  187. #define MSDC_PATCH_BIT_BUSYDLY (0xf << 18) /* RW */
  188. #define MSDC_PATCH_BIT_WDOD (0xf << 22) /* RW */
  189. #define MSDC_PATCH_BIT_IDRTSEL (0x1 << 26) /* RW */
  190. #define MSDC_PATCH_BIT_CMDFSEL (0x1 << 27) /* RW */
  191. #define MSDC_PATCH_BIT_INTDLSEL (0x1 << 28) /* RW */
  192. #define MSDC_PATCH_BIT_SPCPUSH (0x1 << 29) /* RW */
  193. #define MSDC_PATCH_BIT_DECRCTMO (0x1 << 30) /* RW */
  194. #define MSDC_PAD_TUNE_DATRRDLY (0x1f << 8) /* RW */
  195. #define MSDC_PAD_TUNE_CMDRDLY (0x1f << 16) /* RW */
  196. #define PAD_DS_TUNE_DLY1 (0x1f << 2) /* RW */
  197. #define PAD_DS_TUNE_DLY2 (0x1f << 7) /* RW */
  198. #define PAD_DS_TUNE_DLY3 (0x1f << 12) /* RW */
  199. #define EMMC50_CFG_PADCMD_LATCHCK (0x1 << 0) /* RW */
  200. #define EMMC50_CFG_CRCSTS_EDGE (0x1 << 3) /* RW */
  201. #define EMMC50_CFG_CFCSTS_SEL (0x1 << 4) /* RW */
  202. #define REQ_CMD_EIO (0x1 << 0)
  203. #define REQ_CMD_TMO (0x1 << 1)
  204. #define REQ_DAT_ERR (0x1 << 2)
  205. #define REQ_STOP_EIO (0x1 << 3)
  206. #define REQ_STOP_TMO (0x1 << 4)
  207. #define REQ_CMD_BUSY (0x1 << 5)
  208. #define MSDC_PREPARE_FLAG (0x1 << 0)
  209. #define MSDC_ASYNC_FLAG (0x1 << 1)
  210. #define MSDC_MMAP_FLAG (0x1 << 2)
  211. #define MTK_MMC_AUTOSUSPEND_DELAY 50
  212. #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */
  213. #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */
  214. #define PAD_DELAY_MAX 32 /* PAD delay cells */
  215. /*--------------------------------------------------------------------------*/
  216. /* Descriptor Structure */
  217. /*--------------------------------------------------------------------------*/
  218. struct mt_gpdma_desc {
  219. u32 gpd_info;
  220. #define GPDMA_DESC_HWO (0x1 << 0)
  221. #define GPDMA_DESC_BDP (0x1 << 1)
  222. #define GPDMA_DESC_CHECKSUM (0xff << 8) /* bit8 ~ bit15 */
  223. #define GPDMA_DESC_INT (0x1 << 16)
  224. u32 next;
  225. u32 ptr;
  226. u32 gpd_data_len;
  227. #define GPDMA_DESC_BUFLEN (0xffff) /* bit0 ~ bit15 */
  228. #define GPDMA_DESC_EXTLEN (0xff << 16) /* bit16 ~ bit23 */
  229. u32 arg;
  230. u32 blknum;
  231. u32 cmd;
  232. };
  233. struct mt_bdma_desc {
  234. u32 bd_info;
  235. #define BDMA_DESC_EOL (0x1 << 0)
  236. #define BDMA_DESC_CHECKSUM (0xff << 8) /* bit8 ~ bit15 */
  237. #define BDMA_DESC_BLKPAD (0x1 << 17)
  238. #define BDMA_DESC_DWPAD (0x1 << 18)
  239. u32 next;
  240. u32 ptr;
  241. u32 bd_data_len;
  242. #define BDMA_DESC_BUFLEN (0xffff) /* bit0 ~ bit15 */
  243. };
  244. struct msdc_dma {
  245. struct scatterlist *sg; /* I/O scatter list */
  246. struct mt_gpdma_desc *gpd; /* pointer to gpd array */
  247. struct mt_bdma_desc *bd; /* pointer to bd array */
  248. dma_addr_t gpd_addr; /* the physical address of gpd array */
  249. dma_addr_t bd_addr; /* the physical address of bd array */
  250. };
  251. struct msdc_save_para {
  252. u32 msdc_cfg;
  253. u32 iocon;
  254. u32 sdc_cfg;
  255. u32 pad_tune;
  256. u32 patch_bit0;
  257. u32 patch_bit1;
  258. u32 pad_ds_tune;
  259. u32 emmc50_cfg0;
  260. };
  261. struct msdc_tune_para {
  262. u32 iocon;
  263. u32 pad_tune;
  264. };
  265. struct msdc_delay_phase {
  266. u8 maxlen;
  267. u8 start;
  268. u8 final_phase;
  269. };
  270. struct msdc_host {
  271. struct device *dev;
  272. struct mmc_host *mmc; /* mmc structure */
  273. int cmd_rsp;
  274. spinlock_t lock;
  275. struct mmc_request *mrq;
  276. struct mmc_command *cmd;
  277. struct mmc_data *data;
  278. int error;
  279. void __iomem *base; /* host base address */
  280. struct msdc_dma dma; /* dma channel */
  281. u64 dma_mask;
  282. u32 timeout_ns; /* data timeout ns */
  283. u32 timeout_clks; /* data timeout clks */
  284. struct pinctrl *pinctrl;
  285. struct pinctrl_state *pins_default;
  286. struct pinctrl_state *pins_uhs;
  287. struct delayed_work req_timeout;
  288. int irq; /* host interrupt */
  289. struct clk *src_clk; /* msdc source clock */
  290. struct clk *h_clk; /* msdc h_clk */
  291. u32 mclk; /* mmc subsystem clock frequency */
  292. u32 src_clk_freq; /* source clock frequency */
  293. u32 sclk; /* SD/MS bus clock frequency */
  294. unsigned char timing;
  295. bool vqmmc_enabled;
  296. u32 hs400_ds_delay;
  297. bool hs400_mode; /* current eMMC will run at hs400 mode */
  298. struct msdc_save_para save_para; /* used when gate HCLK */
  299. struct msdc_tune_para def_tune_para; /* default tune setting */
  300. struct msdc_tune_para saved_tune_para; /* tune result of CMD21/CMD19 */
  301. };
  302. static void sdr_set_bits(void __iomem *reg, u32 bs)
  303. {
  304. u32 val = readl(reg);
  305. val |= bs;
  306. writel(val, reg);
  307. }
  308. static void sdr_clr_bits(void __iomem *reg, u32 bs)
  309. {
  310. u32 val = readl(reg);
  311. val &= ~bs;
  312. writel(val, reg);
  313. }
  314. static void sdr_set_field(void __iomem *reg, u32 field, u32 val)
  315. {
  316. unsigned int tv = readl(reg);
  317. tv &= ~field;
  318. tv |= ((val) << (ffs((unsigned int)field) - 1));
  319. writel(tv, reg);
  320. }
  321. static void sdr_get_field(void __iomem *reg, u32 field, u32 *val)
  322. {
  323. unsigned int tv = readl(reg);
  324. *val = ((tv & field) >> (ffs((unsigned int)field) - 1));
  325. }
  326. static void msdc_reset_hw(struct msdc_host *host)
  327. {
  328. u32 val;
  329. sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST);
  330. while (readl(host->base + MSDC_CFG) & MSDC_CFG_RST)
  331. cpu_relax();
  332. sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR);
  333. while (readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_CLR)
  334. cpu_relax();
  335. val = readl(host->base + MSDC_INT);
  336. writel(val, host->base + MSDC_INT);
  337. }
  338. static void msdc_cmd_next(struct msdc_host *host,
  339. struct mmc_request *mrq, struct mmc_command *cmd);
  340. static const u32 cmd_ints_mask = MSDC_INTEN_CMDRDY | MSDC_INTEN_RSPCRCERR |
  341. MSDC_INTEN_CMDTMO | MSDC_INTEN_ACMDRDY |
  342. MSDC_INTEN_ACMDCRCERR | MSDC_INTEN_ACMDTMO;
  343. static const u32 data_ints_mask = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO |
  344. MSDC_INTEN_DATCRCERR | MSDC_INTEN_DMA_BDCSERR |
  345. MSDC_INTEN_DMA_GPDCSERR | MSDC_INTEN_DMA_PROTECT;
  346. static u8 msdc_dma_calcs(u8 *buf, u32 len)
  347. {
  348. u32 i, sum = 0;
  349. for (i = 0; i < len; i++)
  350. sum += buf[i];
  351. return 0xff - (u8) sum;
  352. }
  353. static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
  354. struct mmc_data *data)
  355. {
  356. unsigned int j, dma_len;
  357. dma_addr_t dma_address;
  358. u32 dma_ctrl;
  359. struct scatterlist *sg;
  360. struct mt_gpdma_desc *gpd;
  361. struct mt_bdma_desc *bd;
  362. sg = data->sg;
  363. gpd = dma->gpd;
  364. bd = dma->bd;
  365. /* modify gpd */
  366. gpd->gpd_info |= GPDMA_DESC_HWO;
  367. gpd->gpd_info |= GPDMA_DESC_BDP;
  368. /* need to clear first. use these bits to calc checksum */
  369. gpd->gpd_info &= ~GPDMA_DESC_CHECKSUM;
  370. gpd->gpd_info |= msdc_dma_calcs((u8 *) gpd, 16) << 8;
  371. /* modify bd */
  372. for_each_sg(data->sg, sg, data->sg_count, j) {
  373. dma_address = sg_dma_address(sg);
  374. dma_len = sg_dma_len(sg);
  375. /* init bd */
  376. bd[j].bd_info &= ~BDMA_DESC_BLKPAD;
  377. bd[j].bd_info &= ~BDMA_DESC_DWPAD;
  378. bd[j].ptr = (u32)dma_address;
  379. bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN;
  380. bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN);
  381. if (j == data->sg_count - 1) /* the last bd */
  382. bd[j].bd_info |= BDMA_DESC_EOL;
  383. else
  384. bd[j].bd_info &= ~BDMA_DESC_EOL;
  385. /* checksume need to clear first */
  386. bd[j].bd_info &= ~BDMA_DESC_CHECKSUM;
  387. bd[j].bd_info |= msdc_dma_calcs((u8 *)(&bd[j]), 16) << 8;
  388. }
  389. sdr_set_field(host->base + MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1);
  390. dma_ctrl = readl_relaxed(host->base + MSDC_DMA_CTRL);
  391. dma_ctrl &= ~(MSDC_DMA_CTRL_BRUSTSZ | MSDC_DMA_CTRL_MODE);
  392. dma_ctrl |= (MSDC_BURST_64B << 12 | 1 << 8);
  393. writel_relaxed(dma_ctrl, host->base + MSDC_DMA_CTRL);
  394. writel((u32)dma->gpd_addr, host->base + MSDC_DMA_SA);
  395. }
  396. static void msdc_prepare_data(struct msdc_host *host, struct mmc_request *mrq)
  397. {
  398. struct mmc_data *data = mrq->data;
  399. if (!(data->host_cookie & MSDC_PREPARE_FLAG)) {
  400. bool read = (data->flags & MMC_DATA_READ) != 0;
  401. data->host_cookie |= MSDC_PREPARE_FLAG;
  402. data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len,
  403. read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  404. }
  405. }
  406. static void msdc_unprepare_data(struct msdc_host *host, struct mmc_request *mrq)
  407. {
  408. struct mmc_data *data = mrq->data;
  409. if (data->host_cookie & MSDC_ASYNC_FLAG)
  410. return;
  411. if (data->host_cookie & MSDC_PREPARE_FLAG) {
  412. bool read = (data->flags & MMC_DATA_READ) != 0;
  413. dma_unmap_sg(host->dev, data->sg, data->sg_len,
  414. read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
  415. data->host_cookie &= ~MSDC_PREPARE_FLAG;
  416. }
  417. }
  418. /* clock control primitives */
  419. static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
  420. {
  421. u32 timeout, clk_ns;
  422. u32 mode = 0;
  423. host->timeout_ns = ns;
  424. host->timeout_clks = clks;
  425. if (host->sclk == 0) {
  426. timeout = 0;
  427. } else {
  428. clk_ns = 1000000000UL / host->sclk;
  429. timeout = (ns + clk_ns - 1) / clk_ns + clks;
  430. /* in 1048576 sclk cycle unit */
  431. timeout = (timeout + (0x1 << 20) - 1) >> 20;
  432. sdr_get_field(host->base + MSDC_CFG, MSDC_CFG_CKMOD, &mode);
  433. /*DDR mode will double the clk cycles for data timeout */
  434. timeout = mode >= 2 ? timeout * 2 : timeout;
  435. timeout = timeout > 1 ? timeout - 1 : 0;
  436. timeout = timeout > 255 ? 255 : timeout;
  437. }
  438. sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, timeout);
  439. }
  440. static void msdc_gate_clock(struct msdc_host *host)
  441. {
  442. clk_disable_unprepare(host->src_clk);
  443. clk_disable_unprepare(host->h_clk);
  444. }
  445. static void msdc_ungate_clock(struct msdc_host *host)
  446. {
  447. clk_prepare_enable(host->h_clk);
  448. clk_prepare_enable(host->src_clk);
  449. while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB))
  450. cpu_relax();
  451. }
  452. static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz)
  453. {
  454. u32 mode;
  455. u32 flags;
  456. u32 div;
  457. u32 sclk;
  458. if (!hz) {
  459. dev_dbg(host->dev, "set mclk to 0\n");
  460. host->mclk = 0;
  461. sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
  462. return;
  463. }
  464. flags = readl(host->base + MSDC_INTEN);
  465. sdr_clr_bits(host->base + MSDC_INTEN, flags);
  466. sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE);
  467. if (timing == MMC_TIMING_UHS_DDR50 ||
  468. timing == MMC_TIMING_MMC_DDR52 ||
  469. timing == MMC_TIMING_MMC_HS400) {
  470. if (timing == MMC_TIMING_MMC_HS400)
  471. mode = 0x3;
  472. else
  473. mode = 0x2; /* ddr mode and use divisor */
  474. if (hz >= (host->src_clk_freq >> 2)) {
  475. div = 0; /* mean div = 1/4 */
  476. sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
  477. } else {
  478. div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2);
  479. sclk = (host->src_clk_freq >> 2) / div;
  480. div = (div >> 1);
  481. }
  482. if (timing == MMC_TIMING_MMC_HS400 &&
  483. hz >= (host->src_clk_freq >> 1)) {
  484. sdr_set_bits(host->base + MSDC_CFG,
  485. MSDC_CFG_HS400_CK_MODE);
  486. sclk = host->src_clk_freq >> 1;
  487. div = 0; /* div is ignore when bit18 is set */
  488. }
  489. } else if (hz >= host->src_clk_freq) {
  490. mode = 0x1; /* no divisor */
  491. div = 0;
  492. sclk = host->src_clk_freq;
  493. } else {
  494. mode = 0x0; /* use divisor */
  495. if (hz >= (host->src_clk_freq >> 1)) {
  496. div = 0; /* mean div = 1/2 */
  497. sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
  498. } else {
  499. div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2);
  500. sclk = (host->src_clk_freq >> 2) / div;
  501. }
  502. }
  503. sdr_set_field(host->base + MSDC_CFG, MSDC_CFG_CKMOD | MSDC_CFG_CKDIV,
  504. (mode << 8) | div);
  505. sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
  506. while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB))
  507. cpu_relax();
  508. host->sclk = sclk;
  509. host->mclk = hz;
  510. host->timing = timing;
  511. /* need because clk changed. */
  512. msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
  513. sdr_set_bits(host->base + MSDC_INTEN, flags);
  514. /*
  515. * mmc_select_hs400() will drop to 50Mhz and High speed mode,
  516. * tune result of hs200/200Mhz is not suitable for 50Mhz
  517. */
  518. if (host->sclk <= 52000000) {
  519. writel(host->def_tune_para.iocon, host->base + MSDC_IOCON);
  520. writel(host->def_tune_para.pad_tune, host->base + MSDC_PAD_TUNE);
  521. } else {
  522. writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON);
  523. writel(host->saved_tune_para.pad_tune, host->base + MSDC_PAD_TUNE);
  524. }
  525. dev_dbg(host->dev, "sclk: %d, timing: %d\n", host->sclk, timing);
  526. }
  527. static inline u32 msdc_cmd_find_resp(struct msdc_host *host,
  528. struct mmc_request *mrq, struct mmc_command *cmd)
  529. {
  530. u32 resp;
  531. switch (mmc_resp_type(cmd)) {
  532. /* Actually, R1, R5, R6, R7 are the same */
  533. case MMC_RSP_R1:
  534. resp = 0x1;
  535. break;
  536. case MMC_RSP_R1B:
  537. resp = 0x7;
  538. break;
  539. case MMC_RSP_R2:
  540. resp = 0x2;
  541. break;
  542. case MMC_RSP_R3:
  543. resp = 0x3;
  544. break;
  545. case MMC_RSP_NONE:
  546. default:
  547. resp = 0x0;
  548. break;
  549. }
  550. return resp;
  551. }
  552. static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
  553. struct mmc_request *mrq, struct mmc_command *cmd)
  554. {
  555. /* rawcmd :
  556. * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
  557. * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
  558. */
  559. u32 opcode = cmd->opcode;
  560. u32 resp = msdc_cmd_find_resp(host, mrq, cmd);
  561. u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7);
  562. host->cmd_rsp = resp;
  563. if ((opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int) -1) ||
  564. opcode == MMC_STOP_TRANSMISSION)
  565. rawcmd |= (0x1 << 14);
  566. else if (opcode == SD_SWITCH_VOLTAGE)
  567. rawcmd |= (0x1 << 30);
  568. else if (opcode == SD_APP_SEND_SCR ||
  569. opcode == SD_APP_SEND_NUM_WR_BLKS ||
  570. (opcode == SD_SWITCH && mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
  571. (opcode == SD_APP_SD_STATUS && mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
  572. (opcode == MMC_SEND_EXT_CSD && mmc_cmd_type(cmd) == MMC_CMD_ADTC))
  573. rawcmd |= (0x1 << 11);
  574. if (cmd->data) {
  575. struct mmc_data *data = cmd->data;
  576. if (mmc_op_multi(opcode)) {
  577. if (mmc_card_mmc(host->mmc->card) && mrq->sbc &&
  578. !(mrq->sbc->arg & 0xFFFF0000))
  579. rawcmd |= 0x2 << 28; /* AutoCMD23 */
  580. }
  581. rawcmd |= ((data->blksz & 0xFFF) << 16);
  582. if (data->flags & MMC_DATA_WRITE)
  583. rawcmd |= (0x1 << 13);
  584. if (data->blocks > 1)
  585. rawcmd |= (0x2 << 11);
  586. else
  587. rawcmd |= (0x1 << 11);
  588. /* Always use dma mode */
  589. sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO);
  590. if (host->timeout_ns != data->timeout_ns ||
  591. host->timeout_clks != data->timeout_clks)
  592. msdc_set_timeout(host, data->timeout_ns,
  593. data->timeout_clks);
  594. writel(data->blocks, host->base + SDC_BLK_NUM);
  595. }
  596. return rawcmd;
  597. }
  598. static void msdc_start_data(struct msdc_host *host, struct mmc_request *mrq,
  599. struct mmc_command *cmd, struct mmc_data *data)
  600. {
  601. bool read;
  602. WARN_ON(host->data);
  603. host->data = data;
  604. read = data->flags & MMC_DATA_READ;
  605. mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
  606. msdc_dma_setup(host, &host->dma, data);
  607. sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask);
  608. sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
  609. dev_dbg(host->dev, "DMA start\n");
  610. dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n",
  611. __func__, cmd->opcode, data->blocks, read);
  612. }
  613. static int msdc_auto_cmd_done(struct msdc_host *host, int events,
  614. struct mmc_command *cmd)
  615. {
  616. u32 *rsp = cmd->resp;
  617. rsp[0] = readl(host->base + SDC_ACMD_RESP);
  618. if (events & MSDC_INT_ACMDRDY) {
  619. cmd->error = 0;
  620. } else {
  621. msdc_reset_hw(host);
  622. if (events & MSDC_INT_ACMDCRCERR) {
  623. cmd->error = -EILSEQ;
  624. host->error |= REQ_STOP_EIO;
  625. } else if (events & MSDC_INT_ACMDTMO) {
  626. cmd->error = -ETIMEDOUT;
  627. host->error |= REQ_STOP_TMO;
  628. }
  629. dev_err(host->dev,
  630. "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n",
  631. __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error);
  632. }
  633. return cmd->error;
  634. }
  635. static void msdc_track_cmd_data(struct msdc_host *host,
  636. struct mmc_command *cmd, struct mmc_data *data)
  637. {
  638. if (host->error)
  639. dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n",
  640. __func__, cmd->opcode, cmd->arg, host->error);
  641. }
  642. static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq)
  643. {
  644. unsigned long flags;
  645. bool ret;
  646. ret = cancel_delayed_work(&host->req_timeout);
  647. if (!ret) {
  648. /* delay work already running */
  649. return;
  650. }
  651. spin_lock_irqsave(&host->lock, flags);
  652. host->mrq = NULL;
  653. spin_unlock_irqrestore(&host->lock, flags);
  654. msdc_track_cmd_data(host, mrq->cmd, mrq->data);
  655. if (mrq->data)
  656. msdc_unprepare_data(host, mrq);
  657. mmc_request_done(host->mmc, mrq);
  658. }
  659. /* returns true if command is fully handled; returns false otherwise */
  660. static bool msdc_cmd_done(struct msdc_host *host, int events,
  661. struct mmc_request *mrq, struct mmc_command *cmd)
  662. {
  663. bool done = false;
  664. bool sbc_error;
  665. unsigned long flags;
  666. u32 *rsp = cmd->resp;
  667. if (mrq->sbc && cmd == mrq->cmd &&
  668. (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR
  669. | MSDC_INT_ACMDTMO)))
  670. msdc_auto_cmd_done(host, events, mrq->sbc);
  671. sbc_error = mrq->sbc && mrq->sbc->error;
  672. if (!sbc_error && !(events & (MSDC_INT_CMDRDY
  673. | MSDC_INT_RSPCRCERR
  674. | MSDC_INT_CMDTMO)))
  675. return done;
  676. spin_lock_irqsave(&host->lock, flags);
  677. done = !host->cmd;
  678. host->cmd = NULL;
  679. spin_unlock_irqrestore(&host->lock, flags);
  680. if (done)
  681. return true;
  682. sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask);
  683. if (cmd->flags & MMC_RSP_PRESENT) {
  684. if (cmd->flags & MMC_RSP_136) {
  685. rsp[0] = readl(host->base + SDC_RESP3);
  686. rsp[1] = readl(host->base + SDC_RESP2);
  687. rsp[2] = readl(host->base + SDC_RESP1);
  688. rsp[3] = readl(host->base + SDC_RESP0);
  689. } else {
  690. rsp[0] = readl(host->base + SDC_RESP0);
  691. }
  692. }
  693. if (!sbc_error && !(events & MSDC_INT_CMDRDY)) {
  694. if (cmd->opcode != MMC_SEND_TUNING_BLOCK &&
  695. cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)
  696. /*
  697. * should not clear fifo/interrupt as the tune data
  698. * may have alreay come.
  699. */
  700. msdc_reset_hw(host);
  701. if (events & MSDC_INT_RSPCRCERR) {
  702. cmd->error = -EILSEQ;
  703. host->error |= REQ_CMD_EIO;
  704. } else if (events & MSDC_INT_CMDTMO) {
  705. cmd->error = -ETIMEDOUT;
  706. host->error |= REQ_CMD_TMO;
  707. }
  708. }
  709. if (cmd->error)
  710. dev_dbg(host->dev,
  711. "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n",
  712. __func__, cmd->opcode, cmd->arg, rsp[0],
  713. cmd->error);
  714. msdc_cmd_next(host, mrq, cmd);
  715. return true;
  716. }
  717. /* It is the core layer's responsibility to ensure card status
  718. * is correct before issue a request. but host design do below
  719. * checks recommended.
  720. */
  721. static inline bool msdc_cmd_is_ready(struct msdc_host *host,
  722. struct mmc_request *mrq, struct mmc_command *cmd)
  723. {
  724. /* The max busy time we can endure is 20ms */
  725. unsigned long tmo = jiffies + msecs_to_jiffies(20);
  726. while ((readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) &&
  727. time_before(jiffies, tmo))
  728. cpu_relax();
  729. if (readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) {
  730. dev_err(host->dev, "CMD bus busy detected\n");
  731. host->error |= REQ_CMD_BUSY;
  732. msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd);
  733. return false;
  734. }
  735. if (mmc_resp_type(cmd) == MMC_RSP_R1B || cmd->data) {
  736. tmo = jiffies + msecs_to_jiffies(20);
  737. /* R1B or with data, should check SDCBUSY */
  738. while ((readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) &&
  739. time_before(jiffies, tmo))
  740. cpu_relax();
  741. if (readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) {
  742. dev_err(host->dev, "Controller busy detected\n");
  743. host->error |= REQ_CMD_BUSY;
  744. msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd);
  745. return false;
  746. }
  747. }
  748. return true;
  749. }
  750. static void msdc_start_command(struct msdc_host *host,
  751. struct mmc_request *mrq, struct mmc_command *cmd)
  752. {
  753. u32 rawcmd;
  754. WARN_ON(host->cmd);
  755. host->cmd = cmd;
  756. if (!msdc_cmd_is_ready(host, mrq, cmd))
  757. return;
  758. if ((readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16 ||
  759. readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) {
  760. dev_err(host->dev, "TX/RX FIFO non-empty before start of IO. Reset\n");
  761. msdc_reset_hw(host);
  762. }
  763. cmd->error = 0;
  764. rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd);
  765. mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
  766. sdr_set_bits(host->base + MSDC_INTEN, cmd_ints_mask);
  767. writel(cmd->arg, host->base + SDC_ARG);
  768. writel(rawcmd, host->base + SDC_CMD);
  769. }
  770. static void msdc_cmd_next(struct msdc_host *host,
  771. struct mmc_request *mrq, struct mmc_command *cmd)
  772. {
  773. if ((cmd->error &&
  774. !(cmd->error == -EILSEQ &&
  775. (cmd->opcode == MMC_SEND_TUNING_BLOCK ||
  776. cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) ||
  777. (mrq->sbc && mrq->sbc->error))
  778. msdc_request_done(host, mrq);
  779. else if (cmd == mrq->sbc)
  780. msdc_start_command(host, mrq, mrq->cmd);
  781. else if (!cmd->data)
  782. msdc_request_done(host, mrq);
  783. else
  784. msdc_start_data(host, mrq, cmd, cmd->data);
  785. }
  786. static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
  787. {
  788. struct msdc_host *host = mmc_priv(mmc);
  789. host->error = 0;
  790. WARN_ON(host->mrq);
  791. host->mrq = mrq;
  792. if (mrq->data)
  793. msdc_prepare_data(host, mrq);
  794. /* if SBC is required, we have HW option and SW option.
  795. * if HW option is enabled, and SBC does not have "special" flags,
  796. * use HW option, otherwise use SW option
  797. */
  798. if (mrq->sbc && (!mmc_card_mmc(mmc->card) ||
  799. (mrq->sbc->arg & 0xFFFF0000)))
  800. msdc_start_command(host, mrq, mrq->sbc);
  801. else
  802. msdc_start_command(host, mrq, mrq->cmd);
  803. }
  804. static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  805. bool is_first_req)
  806. {
  807. struct msdc_host *host = mmc_priv(mmc);
  808. struct mmc_data *data = mrq->data;
  809. if (!data)
  810. return;
  811. msdc_prepare_data(host, mrq);
  812. data->host_cookie |= MSDC_ASYNC_FLAG;
  813. }
  814. static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  815. int err)
  816. {
  817. struct msdc_host *host = mmc_priv(mmc);
  818. struct mmc_data *data;
  819. data = mrq->data;
  820. if (!data)
  821. return;
  822. if (data->host_cookie) {
  823. data->host_cookie &= ~MSDC_ASYNC_FLAG;
  824. msdc_unprepare_data(host, mrq);
  825. }
  826. }
  827. static void msdc_data_xfer_next(struct msdc_host *host,
  828. struct mmc_request *mrq, struct mmc_data *data)
  829. {
  830. if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error &&
  831. !mrq->sbc)
  832. msdc_start_command(host, mrq, mrq->stop);
  833. else
  834. msdc_request_done(host, mrq);
  835. }
  836. static bool msdc_data_xfer_done(struct msdc_host *host, u32 events,
  837. struct mmc_request *mrq, struct mmc_data *data)
  838. {
  839. struct mmc_command *stop = data->stop;
  840. unsigned long flags;
  841. bool done;
  842. unsigned int check_data = events &
  843. (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO
  844. | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR
  845. | MSDC_INT_DMA_PROTECT);
  846. spin_lock_irqsave(&host->lock, flags);
  847. done = !host->data;
  848. if (check_data)
  849. host->data = NULL;
  850. spin_unlock_irqrestore(&host->lock, flags);
  851. if (done)
  852. return true;
  853. if (check_data || (stop && stop->error)) {
  854. dev_dbg(host->dev, "DMA status: 0x%8X\n",
  855. readl(host->base + MSDC_DMA_CFG));
  856. sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP,
  857. 1);
  858. while (readl(host->base + MSDC_DMA_CFG) & MSDC_DMA_CFG_STS)
  859. cpu_relax();
  860. sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask);
  861. dev_dbg(host->dev, "DMA stop\n");
  862. if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) {
  863. data->bytes_xfered = data->blocks * data->blksz;
  864. } else {
  865. dev_dbg(host->dev, "interrupt events: %x\n", events);
  866. msdc_reset_hw(host);
  867. host->error |= REQ_DAT_ERR;
  868. data->bytes_xfered = 0;
  869. if (events & MSDC_INT_DATTMO)
  870. data->error = -ETIMEDOUT;
  871. else if (events & MSDC_INT_DATCRCERR)
  872. data->error = -EILSEQ;
  873. dev_dbg(host->dev, "%s: cmd=%d; blocks=%d",
  874. __func__, mrq->cmd->opcode, data->blocks);
  875. dev_dbg(host->dev, "data_error=%d xfer_size=%d\n",
  876. (int)data->error, data->bytes_xfered);
  877. }
  878. msdc_data_xfer_next(host, mrq, data);
  879. done = true;
  880. }
  881. return done;
  882. }
  883. static void msdc_set_buswidth(struct msdc_host *host, u32 width)
  884. {
  885. u32 val = readl(host->base + SDC_CFG);
  886. val &= ~SDC_CFG_BUSWIDTH;
  887. switch (width) {
  888. default:
  889. case MMC_BUS_WIDTH_1:
  890. val |= (MSDC_BUS_1BITS << 16);
  891. break;
  892. case MMC_BUS_WIDTH_4:
  893. val |= (MSDC_BUS_4BITS << 16);
  894. break;
  895. case MMC_BUS_WIDTH_8:
  896. val |= (MSDC_BUS_8BITS << 16);
  897. break;
  898. }
  899. writel(val, host->base + SDC_CFG);
  900. dev_dbg(host->dev, "Bus Width = %d", width);
  901. }
  902. static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios)
  903. {
  904. struct msdc_host *host = mmc_priv(mmc);
  905. int ret = 0;
  906. if (!IS_ERR(mmc->supply.vqmmc)) {
  907. if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330 &&
  908. ios->signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
  909. dev_err(host->dev, "Unsupported signal voltage!\n");
  910. return -EINVAL;
  911. }
  912. ret = mmc_regulator_set_vqmmc(mmc, ios);
  913. if (ret) {
  914. dev_dbg(host->dev, "Regulator set error %d (%d)\n",
  915. ret, ios->signal_voltage);
  916. } else {
  917. /* Apply different pinctrl settings for different signal voltage */
  918. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
  919. pinctrl_select_state(host->pinctrl, host->pins_uhs);
  920. else
  921. pinctrl_select_state(host->pinctrl, host->pins_default);
  922. }
  923. }
  924. return ret;
  925. }
  926. static int msdc_card_busy(struct mmc_host *mmc)
  927. {
  928. struct msdc_host *host = mmc_priv(mmc);
  929. u32 status = readl(host->base + MSDC_PS);
  930. /* check if any pin between dat[0:3] is low */
  931. if (((status >> 16) & 0xf) != 0xf)
  932. return 1;
  933. return 0;
  934. }
  935. static void msdc_request_timeout(struct work_struct *work)
  936. {
  937. struct msdc_host *host = container_of(work, struct msdc_host,
  938. req_timeout.work);
  939. /* simulate HW timeout status */
  940. dev_err(host->dev, "%s: aborting cmd/data/mrq\n", __func__);
  941. if (host->mrq) {
  942. dev_err(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__,
  943. host->mrq, host->mrq->cmd->opcode);
  944. if (host->cmd) {
  945. dev_err(host->dev, "%s: aborting cmd=%d\n",
  946. __func__, host->cmd->opcode);
  947. msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq,
  948. host->cmd);
  949. } else if (host->data) {
  950. dev_err(host->dev, "%s: abort data: cmd%d; %d blocks\n",
  951. __func__, host->mrq->cmd->opcode,
  952. host->data->blocks);
  953. msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq,
  954. host->data);
  955. }
  956. }
  957. }
  958. static irqreturn_t msdc_irq(int irq, void *dev_id)
  959. {
  960. struct msdc_host *host = (struct msdc_host *) dev_id;
  961. while (true) {
  962. unsigned long flags;
  963. struct mmc_request *mrq;
  964. struct mmc_command *cmd;
  965. struct mmc_data *data;
  966. u32 events, event_mask;
  967. spin_lock_irqsave(&host->lock, flags);
  968. events = readl(host->base + MSDC_INT);
  969. event_mask = readl(host->base + MSDC_INTEN);
  970. /* clear interrupts */
  971. writel(events & event_mask, host->base + MSDC_INT);
  972. mrq = host->mrq;
  973. cmd = host->cmd;
  974. data = host->data;
  975. spin_unlock_irqrestore(&host->lock, flags);
  976. if (!(events & event_mask))
  977. break;
  978. if (!mrq) {
  979. dev_err(host->dev,
  980. "%s: MRQ=NULL; events=%08X; event_mask=%08X\n",
  981. __func__, events, event_mask);
  982. WARN_ON(1);
  983. break;
  984. }
  985. dev_dbg(host->dev, "%s: events=%08X\n", __func__, events);
  986. if (cmd)
  987. msdc_cmd_done(host, events, mrq, cmd);
  988. else if (data)
  989. msdc_data_xfer_done(host, events, mrq, data);
  990. }
  991. return IRQ_HANDLED;
  992. }
  993. static void msdc_init_hw(struct msdc_host *host)
  994. {
  995. u32 val;
  996. /* Configure to MMC/SD mode, clock free running */
  997. sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN);
  998. /* Reset */
  999. msdc_reset_hw(host);
  1000. /* Disable card detection */
  1001. sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN);
  1002. /* Disable and clear all interrupts */
  1003. writel(0, host->base + MSDC_INTEN);
  1004. val = readl(host->base + MSDC_INT);
  1005. writel(val, host->base + MSDC_INT);
  1006. writel(0, host->base + MSDC_PAD_TUNE);
  1007. writel(0, host->base + MSDC_IOCON);
  1008. sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0);
  1009. writel(0x403c0046, host->base + MSDC_PATCH_BIT);
  1010. sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_CKGEN_MSDC_DLY_SEL, 1);
  1011. writel(0xffff0089, host->base + MSDC_PATCH_BIT1);
  1012. sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL);
  1013. /* Configure to enable SDIO mode.
  1014. * it's must otherwise sdio cmd5 failed
  1015. */
  1016. sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO);
  1017. /* disable detect SDIO device interrupt function */
  1018. sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
  1019. /* Configure to default data timeout */
  1020. sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3);
  1021. host->def_tune_para.iocon = readl(host->base + MSDC_IOCON);
  1022. host->def_tune_para.pad_tune = readl(host->base + MSDC_PAD_TUNE);
  1023. dev_dbg(host->dev, "init hardware done!");
  1024. }
  1025. static void msdc_deinit_hw(struct msdc_host *host)
  1026. {
  1027. u32 val;
  1028. /* Disable and clear all interrupts */
  1029. writel(0, host->base + MSDC_INTEN);
  1030. val = readl(host->base + MSDC_INT);
  1031. writel(val, host->base + MSDC_INT);
  1032. }
  1033. /* init gpd and bd list in msdc_drv_probe */
  1034. static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
  1035. {
  1036. struct mt_gpdma_desc *gpd = dma->gpd;
  1037. struct mt_bdma_desc *bd = dma->bd;
  1038. int i;
  1039. memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2);
  1040. gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */
  1041. gpd->ptr = (u32)dma->bd_addr; /* physical address */
  1042. /* gpd->next is must set for desc DMA
  1043. * That's why must alloc 2 gpd structure.
  1044. */
  1045. gpd->next = (u32)dma->gpd_addr + sizeof(struct mt_gpdma_desc);
  1046. memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM);
  1047. for (i = 0; i < (MAX_BD_NUM - 1); i++)
  1048. bd[i].next = (u32)dma->bd_addr + sizeof(*bd) * (i + 1);
  1049. }
  1050. static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1051. {
  1052. struct msdc_host *host = mmc_priv(mmc);
  1053. int ret;
  1054. msdc_set_buswidth(host, ios->bus_width);
  1055. /* Suspend/Resume will do power off/on */
  1056. switch (ios->power_mode) {
  1057. case MMC_POWER_UP:
  1058. if (!IS_ERR(mmc->supply.vmmc)) {
  1059. msdc_init_hw(host);
  1060. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  1061. ios->vdd);
  1062. if (ret) {
  1063. dev_err(host->dev, "Failed to set vmmc power!\n");
  1064. return;
  1065. }
  1066. }
  1067. break;
  1068. case MMC_POWER_ON:
  1069. if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
  1070. ret = regulator_enable(mmc->supply.vqmmc);
  1071. if (ret)
  1072. dev_err(host->dev, "Failed to set vqmmc power!\n");
  1073. else
  1074. host->vqmmc_enabled = true;
  1075. }
  1076. break;
  1077. case MMC_POWER_OFF:
  1078. if (!IS_ERR(mmc->supply.vmmc))
  1079. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1080. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
  1081. regulator_disable(mmc->supply.vqmmc);
  1082. host->vqmmc_enabled = false;
  1083. }
  1084. break;
  1085. default:
  1086. break;
  1087. }
  1088. if (host->mclk != ios->clock || host->timing != ios->timing)
  1089. msdc_set_mclk(host, ios->timing, ios->clock);
  1090. }
  1091. static u32 test_delay_bit(u32 delay, u32 bit)
  1092. {
  1093. bit %= PAD_DELAY_MAX;
  1094. return delay & (1 << bit);
  1095. }
  1096. static int get_delay_len(u32 delay, u32 start_bit)
  1097. {
  1098. int i;
  1099. for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) {
  1100. if (test_delay_bit(delay, start_bit + i) == 0)
  1101. return i;
  1102. }
  1103. return PAD_DELAY_MAX - start_bit;
  1104. }
  1105. static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay)
  1106. {
  1107. int start = 0, len = 0;
  1108. int start_final = 0, len_final = 0;
  1109. u8 final_phase = 0xff;
  1110. struct msdc_delay_phase delay_phase = { 0, };
  1111. if (delay == 0) {
  1112. dev_err(host->dev, "phase error: [map:%x]\n", delay);
  1113. delay_phase.final_phase = final_phase;
  1114. return delay_phase;
  1115. }
  1116. while (start < PAD_DELAY_MAX) {
  1117. len = get_delay_len(delay, start);
  1118. if (len_final < len) {
  1119. start_final = start;
  1120. len_final = len;
  1121. }
  1122. start += len ? len : 1;
  1123. if (len >= 8 && start_final < 4)
  1124. break;
  1125. }
  1126. /* The rule is that to find the smallest delay cell */
  1127. if (start_final == 0)
  1128. final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX;
  1129. else
  1130. final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX;
  1131. dev_info(host->dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n",
  1132. delay, len_final, final_phase);
  1133. delay_phase.maxlen = len_final;
  1134. delay_phase.start = start_final;
  1135. delay_phase.final_phase = final_phase;
  1136. return delay_phase;
  1137. }
  1138. static int msdc_tune_response(struct mmc_host *mmc, u32 opcode)
  1139. {
  1140. struct msdc_host *host = mmc_priv(mmc);
  1141. u32 rise_delay = 0, fall_delay = 0;
  1142. struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,};
  1143. u8 final_delay, final_maxlen;
  1144. int cmd_err;
  1145. int i;
  1146. sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
  1147. for (i = 0 ; i < PAD_DELAY_MAX; i++) {
  1148. sdr_set_field(host->base + MSDC_PAD_TUNE,
  1149. MSDC_PAD_TUNE_CMDRDLY, i);
  1150. mmc_send_tuning(mmc, opcode, &cmd_err);
  1151. if (!cmd_err)
  1152. rise_delay |= (1 << i);
  1153. }
  1154. final_rise_delay = get_best_delay(host, rise_delay);
  1155. /* if rising edge has enough margin, then do not scan falling edge */
  1156. if (final_rise_delay.maxlen >= 10 ||
  1157. (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
  1158. goto skip_fall;
  1159. sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
  1160. for (i = 0; i < PAD_DELAY_MAX; i++) {
  1161. sdr_set_field(host->base + MSDC_PAD_TUNE,
  1162. MSDC_PAD_TUNE_CMDRDLY, i);
  1163. mmc_send_tuning(mmc, opcode, &cmd_err);
  1164. if (!cmd_err)
  1165. fall_delay |= (1 << i);
  1166. }
  1167. final_fall_delay = get_best_delay(host, fall_delay);
  1168. skip_fall:
  1169. final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
  1170. if (final_maxlen == final_rise_delay.maxlen) {
  1171. sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
  1172. sdr_set_field(host->base + MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRDLY,
  1173. final_rise_delay.final_phase);
  1174. final_delay = final_rise_delay.final_phase;
  1175. } else {
  1176. sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
  1177. sdr_set_field(host->base + MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRDLY,
  1178. final_fall_delay.final_phase);
  1179. final_delay = final_fall_delay.final_phase;
  1180. }
  1181. return final_delay == 0xff ? -EIO : 0;
  1182. }
  1183. static int msdc_tune_data(struct mmc_host *mmc, u32 opcode)
  1184. {
  1185. struct msdc_host *host = mmc_priv(mmc);
  1186. u32 rise_delay = 0, fall_delay = 0;
  1187. struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,};
  1188. u8 final_delay, final_maxlen;
  1189. int i, ret;
  1190. sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
  1191. sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
  1192. for (i = 0 ; i < PAD_DELAY_MAX; i++) {
  1193. sdr_set_field(host->base + MSDC_PAD_TUNE,
  1194. MSDC_PAD_TUNE_DATRRDLY, i);
  1195. ret = mmc_send_tuning(mmc, opcode, NULL);
  1196. if (!ret)
  1197. rise_delay |= (1 << i);
  1198. }
  1199. final_rise_delay = get_best_delay(host, rise_delay);
  1200. /* if rising edge has enough margin, then do not scan falling edge */
  1201. if (final_rise_delay.maxlen >= 10 ||
  1202. (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
  1203. goto skip_fall;
  1204. sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
  1205. sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
  1206. for (i = 0; i < PAD_DELAY_MAX; i++) {
  1207. sdr_set_field(host->base + MSDC_PAD_TUNE,
  1208. MSDC_PAD_TUNE_DATRRDLY, i);
  1209. ret = mmc_send_tuning(mmc, opcode, NULL);
  1210. if (!ret)
  1211. fall_delay |= (1 << i);
  1212. }
  1213. final_fall_delay = get_best_delay(host, fall_delay);
  1214. skip_fall:
  1215. final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
  1216. if (final_maxlen == final_rise_delay.maxlen) {
  1217. sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
  1218. sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
  1219. sdr_set_field(host->base + MSDC_PAD_TUNE,
  1220. MSDC_PAD_TUNE_DATRRDLY,
  1221. final_rise_delay.final_phase);
  1222. final_delay = final_rise_delay.final_phase;
  1223. } else {
  1224. sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
  1225. sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
  1226. sdr_set_field(host->base + MSDC_PAD_TUNE,
  1227. MSDC_PAD_TUNE_DATRRDLY,
  1228. final_fall_delay.final_phase);
  1229. final_delay = final_fall_delay.final_phase;
  1230. }
  1231. return final_delay == 0xff ? -EIO : 0;
  1232. }
  1233. static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1234. {
  1235. struct msdc_host *host = mmc_priv(mmc);
  1236. int ret;
  1237. ret = msdc_tune_response(mmc, opcode);
  1238. if (ret == -EIO) {
  1239. dev_err(host->dev, "Tune response fail!\n");
  1240. return ret;
  1241. }
  1242. if (host->hs400_mode == false) {
  1243. ret = msdc_tune_data(mmc, opcode);
  1244. if (ret == -EIO)
  1245. dev_err(host->dev, "Tune data fail!\n");
  1246. }
  1247. host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON);
  1248. host->saved_tune_para.pad_tune = readl(host->base + MSDC_PAD_TUNE);
  1249. return ret;
  1250. }
  1251. static int msdc_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
  1252. {
  1253. struct msdc_host *host = mmc_priv(mmc);
  1254. host->hs400_mode = true;
  1255. writel(host->hs400_ds_delay, host->base + PAD_DS_TUNE);
  1256. return 0;
  1257. }
  1258. static void msdc_hw_reset(struct mmc_host *mmc)
  1259. {
  1260. struct msdc_host *host = mmc_priv(mmc);
  1261. sdr_set_bits(host->base + EMMC_IOCON, 1);
  1262. udelay(10); /* 10us is enough */
  1263. sdr_clr_bits(host->base + EMMC_IOCON, 1);
  1264. }
  1265. static struct mmc_host_ops mt_msdc_ops = {
  1266. .post_req = msdc_post_req,
  1267. .pre_req = msdc_pre_req,
  1268. .request = msdc_ops_request,
  1269. .set_ios = msdc_ops_set_ios,
  1270. .get_ro = mmc_gpio_get_ro,
  1271. .start_signal_voltage_switch = msdc_ops_switch_volt,
  1272. .card_busy = msdc_card_busy,
  1273. .execute_tuning = msdc_execute_tuning,
  1274. .prepare_hs400_tuning = msdc_prepare_hs400_tuning,
  1275. .hw_reset = msdc_hw_reset,
  1276. };
  1277. static int msdc_drv_probe(struct platform_device *pdev)
  1278. {
  1279. struct mmc_host *mmc;
  1280. struct msdc_host *host;
  1281. struct resource *res;
  1282. int ret;
  1283. if (!pdev->dev.of_node) {
  1284. dev_err(&pdev->dev, "No DT found\n");
  1285. return -EINVAL;
  1286. }
  1287. /* Allocate MMC host for this device */
  1288. mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
  1289. if (!mmc)
  1290. return -ENOMEM;
  1291. host = mmc_priv(mmc);
  1292. ret = mmc_of_parse(mmc);
  1293. if (ret)
  1294. goto host_free;
  1295. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1296. host->base = devm_ioremap_resource(&pdev->dev, res);
  1297. if (IS_ERR(host->base)) {
  1298. ret = PTR_ERR(host->base);
  1299. goto host_free;
  1300. }
  1301. ret = mmc_regulator_get_supply(mmc);
  1302. if (ret == -EPROBE_DEFER)
  1303. goto host_free;
  1304. host->src_clk = devm_clk_get(&pdev->dev, "source");
  1305. if (IS_ERR(host->src_clk)) {
  1306. ret = PTR_ERR(host->src_clk);
  1307. goto host_free;
  1308. }
  1309. host->h_clk = devm_clk_get(&pdev->dev, "hclk");
  1310. if (IS_ERR(host->h_clk)) {
  1311. ret = PTR_ERR(host->h_clk);
  1312. goto host_free;
  1313. }
  1314. host->irq = platform_get_irq(pdev, 0);
  1315. if (host->irq < 0) {
  1316. ret = -EINVAL;
  1317. goto host_free;
  1318. }
  1319. host->pinctrl = devm_pinctrl_get(&pdev->dev);
  1320. if (IS_ERR(host->pinctrl)) {
  1321. ret = PTR_ERR(host->pinctrl);
  1322. dev_err(&pdev->dev, "Cannot find pinctrl!\n");
  1323. goto host_free;
  1324. }
  1325. host->pins_default = pinctrl_lookup_state(host->pinctrl, "default");
  1326. if (IS_ERR(host->pins_default)) {
  1327. ret = PTR_ERR(host->pins_default);
  1328. dev_err(&pdev->dev, "Cannot find pinctrl default!\n");
  1329. goto host_free;
  1330. }
  1331. host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs");
  1332. if (IS_ERR(host->pins_uhs)) {
  1333. ret = PTR_ERR(host->pins_uhs);
  1334. dev_err(&pdev->dev, "Cannot find pinctrl uhs!\n");
  1335. goto host_free;
  1336. }
  1337. if (!of_property_read_u32(pdev->dev.of_node, "hs400-ds-delay",
  1338. &host->hs400_ds_delay))
  1339. dev_dbg(&pdev->dev, "hs400-ds-delay: %x\n",
  1340. host->hs400_ds_delay);
  1341. host->dev = &pdev->dev;
  1342. host->mmc = mmc;
  1343. host->src_clk_freq = clk_get_rate(host->src_clk);
  1344. /* Set host parameters to mmc */
  1345. mmc->ops = &mt_msdc_ops;
  1346. mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 255);
  1347. mmc->caps |= MMC_CAP_ERASE | MMC_CAP_CMD23;
  1348. /* MMC core transfer sizes tunable parameters */
  1349. mmc->max_segs = MAX_BD_NUM;
  1350. mmc->max_seg_size = BDMA_DESC_BUFLEN;
  1351. mmc->max_blk_size = 2048;
  1352. mmc->max_req_size = 512 * 1024;
  1353. mmc->max_blk_count = mmc->max_req_size / 512;
  1354. host->dma_mask = DMA_BIT_MASK(32);
  1355. mmc_dev(mmc)->dma_mask = &host->dma_mask;
  1356. host->timeout_clks = 3 * 1048576;
  1357. host->dma.gpd = dma_alloc_coherent(&pdev->dev,
  1358. 2 * sizeof(struct mt_gpdma_desc),
  1359. &host->dma.gpd_addr, GFP_KERNEL);
  1360. host->dma.bd = dma_alloc_coherent(&pdev->dev,
  1361. MAX_BD_NUM * sizeof(struct mt_bdma_desc),
  1362. &host->dma.bd_addr, GFP_KERNEL);
  1363. if (!host->dma.gpd || !host->dma.bd) {
  1364. ret = -ENOMEM;
  1365. goto release_mem;
  1366. }
  1367. msdc_init_gpd_bd(host, &host->dma);
  1368. INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout);
  1369. spin_lock_init(&host->lock);
  1370. platform_set_drvdata(pdev, mmc);
  1371. msdc_ungate_clock(host);
  1372. msdc_init_hw(host);
  1373. ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq,
  1374. IRQF_TRIGGER_LOW | IRQF_ONESHOT, pdev->name, host);
  1375. if (ret)
  1376. goto release;
  1377. pm_runtime_set_active(host->dev);
  1378. pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY);
  1379. pm_runtime_use_autosuspend(host->dev);
  1380. pm_runtime_enable(host->dev);
  1381. ret = mmc_add_host(mmc);
  1382. if (ret)
  1383. goto end;
  1384. return 0;
  1385. end:
  1386. pm_runtime_disable(host->dev);
  1387. release:
  1388. platform_set_drvdata(pdev, NULL);
  1389. msdc_deinit_hw(host);
  1390. msdc_gate_clock(host);
  1391. release_mem:
  1392. if (host->dma.gpd)
  1393. dma_free_coherent(&pdev->dev,
  1394. 2 * sizeof(struct mt_gpdma_desc),
  1395. host->dma.gpd, host->dma.gpd_addr);
  1396. if (host->dma.bd)
  1397. dma_free_coherent(&pdev->dev,
  1398. MAX_BD_NUM * sizeof(struct mt_bdma_desc),
  1399. host->dma.bd, host->dma.bd_addr);
  1400. host_free:
  1401. mmc_free_host(mmc);
  1402. return ret;
  1403. }
  1404. static int msdc_drv_remove(struct platform_device *pdev)
  1405. {
  1406. struct mmc_host *mmc;
  1407. struct msdc_host *host;
  1408. mmc = platform_get_drvdata(pdev);
  1409. host = mmc_priv(mmc);
  1410. pm_runtime_get_sync(host->dev);
  1411. platform_set_drvdata(pdev, NULL);
  1412. mmc_remove_host(host->mmc);
  1413. msdc_deinit_hw(host);
  1414. msdc_gate_clock(host);
  1415. pm_runtime_disable(host->dev);
  1416. pm_runtime_put_noidle(host->dev);
  1417. dma_free_coherent(&pdev->dev,
  1418. sizeof(struct mt_gpdma_desc),
  1419. host->dma.gpd, host->dma.gpd_addr);
  1420. dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc),
  1421. host->dma.bd, host->dma.bd_addr);
  1422. mmc_free_host(host->mmc);
  1423. return 0;
  1424. }
  1425. #ifdef CONFIG_PM
  1426. static void msdc_save_reg(struct msdc_host *host)
  1427. {
  1428. host->save_para.msdc_cfg = readl(host->base + MSDC_CFG);
  1429. host->save_para.iocon = readl(host->base + MSDC_IOCON);
  1430. host->save_para.sdc_cfg = readl(host->base + SDC_CFG);
  1431. host->save_para.pad_tune = readl(host->base + MSDC_PAD_TUNE);
  1432. host->save_para.patch_bit0 = readl(host->base + MSDC_PATCH_BIT);
  1433. host->save_para.patch_bit1 = readl(host->base + MSDC_PATCH_BIT1);
  1434. host->save_para.pad_ds_tune = readl(host->base + PAD_DS_TUNE);
  1435. host->save_para.emmc50_cfg0 = readl(host->base + EMMC50_CFG0);
  1436. }
  1437. static void msdc_restore_reg(struct msdc_host *host)
  1438. {
  1439. writel(host->save_para.msdc_cfg, host->base + MSDC_CFG);
  1440. writel(host->save_para.iocon, host->base + MSDC_IOCON);
  1441. writel(host->save_para.sdc_cfg, host->base + SDC_CFG);
  1442. writel(host->save_para.pad_tune, host->base + MSDC_PAD_TUNE);
  1443. writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT);
  1444. writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1);
  1445. writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE);
  1446. writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0);
  1447. }
  1448. static int msdc_runtime_suspend(struct device *dev)
  1449. {
  1450. struct mmc_host *mmc = dev_get_drvdata(dev);
  1451. struct msdc_host *host = mmc_priv(mmc);
  1452. msdc_save_reg(host);
  1453. msdc_gate_clock(host);
  1454. return 0;
  1455. }
  1456. static int msdc_runtime_resume(struct device *dev)
  1457. {
  1458. struct mmc_host *mmc = dev_get_drvdata(dev);
  1459. struct msdc_host *host = mmc_priv(mmc);
  1460. msdc_ungate_clock(host);
  1461. msdc_restore_reg(host);
  1462. return 0;
  1463. }
  1464. #endif
  1465. static const struct dev_pm_ops msdc_dev_pm_ops = {
  1466. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1467. pm_runtime_force_resume)
  1468. SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL)
  1469. };
  1470. static const struct of_device_id msdc_of_ids[] = {
  1471. { .compatible = "mediatek,mt8135-mmc", },
  1472. {}
  1473. };
  1474. static struct platform_driver mt_msdc_driver = {
  1475. .probe = msdc_drv_probe,
  1476. .remove = msdc_drv_remove,
  1477. .driver = {
  1478. .name = "mtk-msdc",
  1479. .of_match_table = msdc_of_ids,
  1480. .pm = &msdc_dev_pm_ops,
  1481. },
  1482. };
  1483. module_platform_driver(mt_msdc_driver);
  1484. MODULE_LICENSE("GPL v2");
  1485. MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver");