dw_mmc.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042
  1. /*
  2. * Synopsys DesignWare Multimedia Card Interface driver
  3. * (Based on NXP driver for lpc 31xx)
  4. *
  5. * Copyright (C) 2009 NXP Semiconductors
  6. * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. */
  13. #include <linux/blkdev.h>
  14. #include <linux/clk.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/device.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/ioport.h>
  22. #include <linux/module.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/slab.h>
  26. #include <linux/stat.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/sd.h>
  33. #include <linux/mmc/sdio.h>
  34. #include <linux/mmc/dw_mmc.h>
  35. #include <linux/bitops.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <linux/of.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/mmc/slot-gpio.h>
  40. #include "dw_mmc.h"
  41. /* Common flag combinations */
  42. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
  43. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  44. SDMMC_INT_EBE)
  45. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  46. SDMMC_INT_RESP_ERR)
  47. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  48. DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
  49. #define DW_MCI_SEND_STATUS 1
  50. #define DW_MCI_RECV_STATUS 2
  51. #define DW_MCI_DMA_THRESHOLD 16
  52. #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
  53. #define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
  54. #ifdef CONFIG_MMC_DW_IDMAC
  55. #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
  56. SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
  57. SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
  58. SDMMC_IDMAC_INT_TI)
  59. struct idmac_desc_64addr {
  60. u32 des0; /* Control Descriptor */
  61. u32 des1; /* Reserved */
  62. u32 des2; /*Buffer sizes */
  63. #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
  64. ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
  65. ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
  66. u32 des3; /* Reserved */
  67. u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
  68. u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
  69. u32 des6; /* Lower 32-bits of Next Descriptor Address */
  70. u32 des7; /* Upper 32-bits of Next Descriptor Address */
  71. };
  72. struct idmac_desc {
  73. __le32 des0; /* Control Descriptor */
  74. #define IDMAC_DES0_DIC BIT(1)
  75. #define IDMAC_DES0_LD BIT(2)
  76. #define IDMAC_DES0_FD BIT(3)
  77. #define IDMAC_DES0_CH BIT(4)
  78. #define IDMAC_DES0_ER BIT(5)
  79. #define IDMAC_DES0_CES BIT(30)
  80. #define IDMAC_DES0_OWN BIT(31)
  81. __le32 des1; /* Buffer sizes */
  82. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  83. ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
  84. __le32 des2; /* buffer 1 physical address */
  85. __le32 des3; /* buffer 2 physical address */
  86. };
  87. #endif /* CONFIG_MMC_DW_IDMAC */
  88. static bool dw_mci_reset(struct dw_mci *host);
  89. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
  90. static int dw_mci_card_busy(struct mmc_host *mmc);
  91. #if defined(CONFIG_DEBUG_FS)
  92. static int dw_mci_req_show(struct seq_file *s, void *v)
  93. {
  94. struct dw_mci_slot *slot = s->private;
  95. struct mmc_request *mrq;
  96. struct mmc_command *cmd;
  97. struct mmc_command *stop;
  98. struct mmc_data *data;
  99. /* Make sure we get a consistent snapshot */
  100. spin_lock_bh(&slot->host->lock);
  101. mrq = slot->mrq;
  102. if (mrq) {
  103. cmd = mrq->cmd;
  104. data = mrq->data;
  105. stop = mrq->stop;
  106. if (cmd)
  107. seq_printf(s,
  108. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  109. cmd->opcode, cmd->arg, cmd->flags,
  110. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  111. cmd->resp[2], cmd->error);
  112. if (data)
  113. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  114. data->bytes_xfered, data->blocks,
  115. data->blksz, data->flags, data->error);
  116. if (stop)
  117. seq_printf(s,
  118. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  119. stop->opcode, stop->arg, stop->flags,
  120. stop->resp[0], stop->resp[1], stop->resp[2],
  121. stop->resp[2], stop->error);
  122. }
  123. spin_unlock_bh(&slot->host->lock);
  124. return 0;
  125. }
  126. static int dw_mci_req_open(struct inode *inode, struct file *file)
  127. {
  128. return single_open(file, dw_mci_req_show, inode->i_private);
  129. }
  130. static const struct file_operations dw_mci_req_fops = {
  131. .owner = THIS_MODULE,
  132. .open = dw_mci_req_open,
  133. .read = seq_read,
  134. .llseek = seq_lseek,
  135. .release = single_release,
  136. };
  137. static int dw_mci_regs_show(struct seq_file *s, void *v)
  138. {
  139. seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
  140. seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
  141. seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
  142. seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
  143. seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
  144. seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
  145. return 0;
  146. }
  147. static int dw_mci_regs_open(struct inode *inode, struct file *file)
  148. {
  149. return single_open(file, dw_mci_regs_show, inode->i_private);
  150. }
  151. static const struct file_operations dw_mci_regs_fops = {
  152. .owner = THIS_MODULE,
  153. .open = dw_mci_regs_open,
  154. .read = seq_read,
  155. .llseek = seq_lseek,
  156. .release = single_release,
  157. };
  158. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  159. {
  160. struct mmc_host *mmc = slot->mmc;
  161. struct dw_mci *host = slot->host;
  162. struct dentry *root;
  163. struct dentry *node;
  164. root = mmc->debugfs_root;
  165. if (!root)
  166. return;
  167. node = debugfs_create_file("regs", S_IRUSR, root, host,
  168. &dw_mci_regs_fops);
  169. if (!node)
  170. goto err;
  171. node = debugfs_create_file("req", S_IRUSR, root, slot,
  172. &dw_mci_req_fops);
  173. if (!node)
  174. goto err;
  175. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  176. if (!node)
  177. goto err;
  178. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  179. (u32 *)&host->pending_events);
  180. if (!node)
  181. goto err;
  182. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  183. (u32 *)&host->completed_events);
  184. if (!node)
  185. goto err;
  186. return;
  187. err:
  188. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  189. }
  190. #endif /* defined(CONFIG_DEBUG_FS) */
  191. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
  192. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  193. {
  194. struct mmc_data *data;
  195. struct dw_mci_slot *slot = mmc_priv(mmc);
  196. struct dw_mci *host = slot->host;
  197. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  198. u32 cmdr;
  199. cmd->error = -EINPROGRESS;
  200. cmdr = cmd->opcode;
  201. if (cmd->opcode == MMC_STOP_TRANSMISSION ||
  202. cmd->opcode == MMC_GO_IDLE_STATE ||
  203. cmd->opcode == MMC_GO_INACTIVE_STATE ||
  204. (cmd->opcode == SD_IO_RW_DIRECT &&
  205. ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
  206. cmdr |= SDMMC_CMD_STOP;
  207. else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
  208. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  209. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  210. u32 clk_en_a;
  211. /* Special bit makes CMD11 not die */
  212. cmdr |= SDMMC_CMD_VOLT_SWITCH;
  213. /* Change state to continue to handle CMD11 weirdness */
  214. WARN_ON(slot->host->state != STATE_SENDING_CMD);
  215. slot->host->state = STATE_SENDING_CMD11;
  216. /*
  217. * We need to disable low power mode (automatic clock stop)
  218. * while doing voltage switch so we don't confuse the card,
  219. * since stopping the clock is a specific part of the UHS
  220. * voltage change dance.
  221. *
  222. * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
  223. * unconditionally turned back on in dw_mci_setup_bus() if it's
  224. * ever called with a non-zero clock. That shouldn't happen
  225. * until the voltage change is all done.
  226. */
  227. clk_en_a = mci_readl(host, CLKENA);
  228. clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
  229. mci_writel(host, CLKENA, clk_en_a);
  230. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  231. SDMMC_CMD_PRV_DAT_WAIT, 0);
  232. }
  233. if (cmd->flags & MMC_RSP_PRESENT) {
  234. /* We expect a response, so set this bit */
  235. cmdr |= SDMMC_CMD_RESP_EXP;
  236. if (cmd->flags & MMC_RSP_136)
  237. cmdr |= SDMMC_CMD_RESP_LONG;
  238. }
  239. if (cmd->flags & MMC_RSP_CRC)
  240. cmdr |= SDMMC_CMD_RESP_CRC;
  241. data = cmd->data;
  242. if (data) {
  243. cmdr |= SDMMC_CMD_DAT_EXP;
  244. if (data->flags & MMC_DATA_STREAM)
  245. cmdr |= SDMMC_CMD_STRM_MODE;
  246. if (data->flags & MMC_DATA_WRITE)
  247. cmdr |= SDMMC_CMD_DAT_WR;
  248. }
  249. if (drv_data && drv_data->prepare_command)
  250. drv_data->prepare_command(slot->host, &cmdr);
  251. return cmdr;
  252. }
  253. static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
  254. {
  255. struct mmc_command *stop;
  256. u32 cmdr;
  257. if (!cmd->data)
  258. return 0;
  259. stop = &host->stop_abort;
  260. cmdr = cmd->opcode;
  261. memset(stop, 0, sizeof(struct mmc_command));
  262. if (cmdr == MMC_READ_SINGLE_BLOCK ||
  263. cmdr == MMC_READ_MULTIPLE_BLOCK ||
  264. cmdr == MMC_WRITE_BLOCK ||
  265. cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
  266. cmdr == MMC_SEND_TUNING_BLOCK ||
  267. cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
  268. stop->opcode = MMC_STOP_TRANSMISSION;
  269. stop->arg = 0;
  270. stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  271. } else if (cmdr == SD_IO_RW_EXTENDED) {
  272. stop->opcode = SD_IO_RW_DIRECT;
  273. stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
  274. ((cmd->arg >> 28) & 0x7);
  275. stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  276. } else {
  277. return 0;
  278. }
  279. cmdr = stop->opcode | SDMMC_CMD_STOP |
  280. SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
  281. return cmdr;
  282. }
  283. static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
  284. {
  285. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  286. /*
  287. * Databook says that before issuing a new data transfer command
  288. * we need to check to see if the card is busy. Data transfer commands
  289. * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
  290. *
  291. * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
  292. * expected.
  293. */
  294. if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
  295. !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
  296. while (mci_readl(host, STATUS) & SDMMC_STATUS_BUSY) {
  297. if (time_after(jiffies, timeout)) {
  298. /* Command will fail; we'll pass error then */
  299. dev_err(host->dev, "Busy; trying anyway\n");
  300. break;
  301. }
  302. udelay(10);
  303. }
  304. }
  305. }
  306. static void dw_mci_start_command(struct dw_mci *host,
  307. struct mmc_command *cmd, u32 cmd_flags)
  308. {
  309. host->cmd = cmd;
  310. dev_vdbg(host->dev,
  311. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  312. cmd->arg, cmd_flags);
  313. mci_writel(host, CMDARG, cmd->arg);
  314. wmb();
  315. dw_mci_wait_while_busy(host, cmd_flags);
  316. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  317. }
  318. static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
  319. {
  320. struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
  321. dw_mci_start_command(host, stop, host->stop_cmdr);
  322. }
  323. /* DMA interface functions */
  324. static void dw_mci_stop_dma(struct dw_mci *host)
  325. {
  326. if (host->using_dma) {
  327. host->dma_ops->stop(host);
  328. host->dma_ops->cleanup(host);
  329. }
  330. /* Data transfer was stopped by the interrupt handler */
  331. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  332. }
  333. static int dw_mci_get_dma_dir(struct mmc_data *data)
  334. {
  335. if (data->flags & MMC_DATA_WRITE)
  336. return DMA_TO_DEVICE;
  337. else
  338. return DMA_FROM_DEVICE;
  339. }
  340. #ifdef CONFIG_MMC_DW_IDMAC
  341. static void dw_mci_dma_cleanup(struct dw_mci *host)
  342. {
  343. struct mmc_data *data = host->data;
  344. if (data)
  345. if (!data->host_cookie)
  346. dma_unmap_sg(host->dev,
  347. data->sg,
  348. data->sg_len,
  349. dw_mci_get_dma_dir(data));
  350. }
  351. static void dw_mci_idmac_reset(struct dw_mci *host)
  352. {
  353. u32 bmod = mci_readl(host, BMOD);
  354. /* Software reset of DMA */
  355. bmod |= SDMMC_IDMAC_SWRESET;
  356. mci_writel(host, BMOD, bmod);
  357. }
  358. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  359. {
  360. u32 temp;
  361. /* Disable and reset the IDMAC interface */
  362. temp = mci_readl(host, CTRL);
  363. temp &= ~SDMMC_CTRL_USE_IDMAC;
  364. temp |= SDMMC_CTRL_DMA_RESET;
  365. mci_writel(host, CTRL, temp);
  366. /* Stop the IDMAC running */
  367. temp = mci_readl(host, BMOD);
  368. temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
  369. temp |= SDMMC_IDMAC_SWRESET;
  370. mci_writel(host, BMOD, temp);
  371. }
  372. static void dw_mci_idmac_complete_dma(struct dw_mci *host)
  373. {
  374. struct mmc_data *data = host->data;
  375. dev_vdbg(host->dev, "DMA complete\n");
  376. host->dma_ops->cleanup(host);
  377. /*
  378. * If the card was removed, data will be NULL. No point in trying to
  379. * send the stop command or waiting for NBUSY in this case.
  380. */
  381. if (data) {
  382. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  383. tasklet_schedule(&host->tasklet);
  384. }
  385. }
  386. static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
  387. unsigned int sg_len)
  388. {
  389. int i;
  390. if (host->dma_64bit_address == 1) {
  391. struct idmac_desc_64addr *desc = host->sg_cpu;
  392. for (i = 0; i < sg_len; i++, desc++) {
  393. unsigned int length = sg_dma_len(&data->sg[i]);
  394. u64 mem_addr = sg_dma_address(&data->sg[i]);
  395. /*
  396. * Set the OWN bit and disable interrupts for this
  397. * descriptor
  398. */
  399. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
  400. IDMAC_DES0_CH;
  401. /* Buffer length */
  402. IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, length);
  403. /* Physical address to DMA to/from */
  404. desc->des4 = mem_addr & 0xffffffff;
  405. desc->des5 = mem_addr >> 32;
  406. }
  407. /* Set first descriptor */
  408. desc = host->sg_cpu;
  409. desc->des0 |= IDMAC_DES0_FD;
  410. /* Set last descriptor */
  411. desc = host->sg_cpu + (i - 1) *
  412. sizeof(struct idmac_desc_64addr);
  413. desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  414. desc->des0 |= IDMAC_DES0_LD;
  415. } else {
  416. struct idmac_desc *desc = host->sg_cpu;
  417. for (i = 0; i < sg_len; i++, desc++) {
  418. unsigned int length = sg_dma_len(&data->sg[i]);
  419. u32 mem_addr = sg_dma_address(&data->sg[i]);
  420. /*
  421. * Set the OWN bit and disable interrupts for this
  422. * descriptor
  423. */
  424. desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
  425. IDMAC_DES0_DIC | IDMAC_DES0_CH);
  426. /* Buffer length */
  427. IDMAC_SET_BUFFER1_SIZE(desc, length);
  428. /* Physical address to DMA to/from */
  429. desc->des2 = cpu_to_le32(mem_addr);
  430. }
  431. /* Set first descriptor */
  432. desc = host->sg_cpu;
  433. desc->des0 |= cpu_to_le32(IDMAC_DES0_FD);
  434. /* Set last descriptor */
  435. desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
  436. desc->des0 &= cpu_to_le32(~(IDMAC_DES0_CH | IDMAC_DES0_DIC));
  437. desc->des0 |= cpu_to_le32(IDMAC_DES0_LD);
  438. }
  439. wmb();
  440. }
  441. static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  442. {
  443. u32 temp;
  444. dw_mci_translate_sglist(host, host->data, sg_len);
  445. /* Make sure to reset DMA in case we did PIO before this */
  446. dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
  447. dw_mci_idmac_reset(host);
  448. /* Select IDMAC interface */
  449. temp = mci_readl(host, CTRL);
  450. temp |= SDMMC_CTRL_USE_IDMAC;
  451. mci_writel(host, CTRL, temp);
  452. wmb();
  453. /* Enable the IDMAC */
  454. temp = mci_readl(host, BMOD);
  455. temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
  456. mci_writel(host, BMOD, temp);
  457. /* Start it running */
  458. mci_writel(host, PLDMND, 1);
  459. }
  460. static int dw_mci_idmac_init(struct dw_mci *host)
  461. {
  462. int i;
  463. if (host->dma_64bit_address == 1) {
  464. struct idmac_desc_64addr *p;
  465. /* Number of descriptors in the ring buffer */
  466. host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr);
  467. /* Forward link the descriptor list */
  468. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
  469. i++, p++) {
  470. p->des6 = (host->sg_dma +
  471. (sizeof(struct idmac_desc_64addr) *
  472. (i + 1))) & 0xffffffff;
  473. p->des7 = (u64)(host->sg_dma +
  474. (sizeof(struct idmac_desc_64addr) *
  475. (i + 1))) >> 32;
  476. /* Initialize reserved and buffer size fields to "0" */
  477. p->des1 = 0;
  478. p->des2 = 0;
  479. p->des3 = 0;
  480. }
  481. /* Set the last descriptor as the end-of-ring descriptor */
  482. p->des6 = host->sg_dma & 0xffffffff;
  483. p->des7 = (u64)host->sg_dma >> 32;
  484. p->des0 = IDMAC_DES0_ER;
  485. } else {
  486. struct idmac_desc *p;
  487. /* Number of descriptors in the ring buffer */
  488. host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
  489. /* Forward link the descriptor list */
  490. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++) {
  491. p->des3 = cpu_to_le32(host->sg_dma +
  492. (sizeof(struct idmac_desc) * (i + 1)));
  493. p->des1 = 0;
  494. }
  495. /* Set the last descriptor as the end-of-ring descriptor */
  496. p->des3 = cpu_to_le32(host->sg_dma);
  497. p->des0 = cpu_to_le32(IDMAC_DES0_ER);
  498. }
  499. dw_mci_idmac_reset(host);
  500. if (host->dma_64bit_address == 1) {
  501. /* Mask out interrupts - get Tx & Rx complete only */
  502. mci_writel(host, IDSTS64, IDMAC_INT_CLR);
  503. mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
  504. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  505. /* Set the descriptor base address */
  506. mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
  507. mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
  508. } else {
  509. /* Mask out interrupts - get Tx & Rx complete only */
  510. mci_writel(host, IDSTS, IDMAC_INT_CLR);
  511. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
  512. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  513. /* Set the descriptor base address */
  514. mci_writel(host, DBADDR, host->sg_dma);
  515. }
  516. return 0;
  517. }
  518. static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
  519. .init = dw_mci_idmac_init,
  520. .start = dw_mci_idmac_start_dma,
  521. .stop = dw_mci_idmac_stop_dma,
  522. .complete = dw_mci_idmac_complete_dma,
  523. .cleanup = dw_mci_dma_cleanup,
  524. };
  525. #endif /* CONFIG_MMC_DW_IDMAC */
  526. static int dw_mci_pre_dma_transfer(struct dw_mci *host,
  527. struct mmc_data *data,
  528. bool next)
  529. {
  530. struct scatterlist *sg;
  531. unsigned int i, sg_len;
  532. if (!next && data->host_cookie)
  533. return data->host_cookie;
  534. /*
  535. * We don't do DMA on "complex" transfers, i.e. with
  536. * non-word-aligned buffers or lengths. Also, we don't bother
  537. * with all the DMA setup overhead for short transfers.
  538. */
  539. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  540. return -EINVAL;
  541. if (data->blksz & 3)
  542. return -EINVAL;
  543. for_each_sg(data->sg, sg, data->sg_len, i) {
  544. if (sg->offset & 3 || sg->length & 3)
  545. return -EINVAL;
  546. }
  547. sg_len = dma_map_sg(host->dev,
  548. data->sg,
  549. data->sg_len,
  550. dw_mci_get_dma_dir(data));
  551. if (sg_len == 0)
  552. return -EINVAL;
  553. if (next)
  554. data->host_cookie = sg_len;
  555. return sg_len;
  556. }
  557. static void dw_mci_pre_req(struct mmc_host *mmc,
  558. struct mmc_request *mrq,
  559. bool is_first_req)
  560. {
  561. struct dw_mci_slot *slot = mmc_priv(mmc);
  562. struct mmc_data *data = mrq->data;
  563. if (!slot->host->use_dma || !data)
  564. return;
  565. if (data->host_cookie) {
  566. data->host_cookie = 0;
  567. return;
  568. }
  569. if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
  570. data->host_cookie = 0;
  571. }
  572. static void dw_mci_post_req(struct mmc_host *mmc,
  573. struct mmc_request *mrq,
  574. int err)
  575. {
  576. struct dw_mci_slot *slot = mmc_priv(mmc);
  577. struct mmc_data *data = mrq->data;
  578. if (!slot->host->use_dma || !data)
  579. return;
  580. if (data->host_cookie)
  581. dma_unmap_sg(slot->host->dev,
  582. data->sg,
  583. data->sg_len,
  584. dw_mci_get_dma_dir(data));
  585. data->host_cookie = 0;
  586. }
  587. static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
  588. {
  589. #ifdef CONFIG_MMC_DW_IDMAC
  590. unsigned int blksz = data->blksz;
  591. const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  592. u32 fifo_width = 1 << host->data_shift;
  593. u32 blksz_depth = blksz / fifo_width, fifoth_val;
  594. u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
  595. int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
  596. tx_wmark = (host->fifo_depth) / 2;
  597. tx_wmark_invers = host->fifo_depth - tx_wmark;
  598. /*
  599. * MSIZE is '1',
  600. * if blksz is not a multiple of the FIFO width
  601. */
  602. if (blksz % fifo_width) {
  603. msize = 0;
  604. rx_wmark = 1;
  605. goto done;
  606. }
  607. do {
  608. if (!((blksz_depth % mszs[idx]) ||
  609. (tx_wmark_invers % mszs[idx]))) {
  610. msize = idx;
  611. rx_wmark = mszs[idx] - 1;
  612. break;
  613. }
  614. } while (--idx > 0);
  615. /*
  616. * If idx is '0', it won't be tried
  617. * Thus, initial values are uesed
  618. */
  619. done:
  620. fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
  621. mci_writel(host, FIFOTH, fifoth_val);
  622. #endif
  623. }
  624. static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
  625. {
  626. unsigned int blksz = data->blksz;
  627. u32 blksz_depth, fifo_depth;
  628. u16 thld_size;
  629. WARN_ON(!(data->flags & MMC_DATA_READ));
  630. /*
  631. * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
  632. * in the FIFO region, so we really shouldn't access it).
  633. */
  634. if (host->verid < DW_MMC_240A)
  635. return;
  636. if (host->timing != MMC_TIMING_MMC_HS200 &&
  637. host->timing != MMC_TIMING_MMC_HS400 &&
  638. host->timing != MMC_TIMING_UHS_SDR104)
  639. goto disable;
  640. blksz_depth = blksz / (1 << host->data_shift);
  641. fifo_depth = host->fifo_depth;
  642. if (blksz_depth > fifo_depth)
  643. goto disable;
  644. /*
  645. * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
  646. * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
  647. * Currently just choose blksz.
  648. */
  649. thld_size = blksz;
  650. mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
  651. return;
  652. disable:
  653. mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
  654. }
  655. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  656. {
  657. unsigned long irqflags;
  658. int sg_len;
  659. u32 temp;
  660. host->using_dma = 0;
  661. /* If we don't have a channel, we can't do DMA */
  662. if (!host->use_dma)
  663. return -ENODEV;
  664. sg_len = dw_mci_pre_dma_transfer(host, data, 0);
  665. if (sg_len < 0) {
  666. host->dma_ops->stop(host);
  667. return sg_len;
  668. }
  669. host->using_dma = 1;
  670. dev_vdbg(host->dev,
  671. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  672. (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
  673. sg_len);
  674. /*
  675. * Decide the MSIZE and RX/TX Watermark.
  676. * If current block size is same with previous size,
  677. * no need to update fifoth.
  678. */
  679. if (host->prev_blksz != data->blksz)
  680. dw_mci_adjust_fifoth(host, data);
  681. /* Enable the DMA interface */
  682. temp = mci_readl(host, CTRL);
  683. temp |= SDMMC_CTRL_DMA_ENABLE;
  684. mci_writel(host, CTRL, temp);
  685. /* Disable RX/TX IRQs, let DMA handle it */
  686. spin_lock_irqsave(&host->irq_lock, irqflags);
  687. temp = mci_readl(host, INTMASK);
  688. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  689. mci_writel(host, INTMASK, temp);
  690. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  691. host->dma_ops->start(host, sg_len);
  692. return 0;
  693. }
  694. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  695. {
  696. unsigned long irqflags;
  697. u32 temp;
  698. data->error = -EINPROGRESS;
  699. WARN_ON(host->data);
  700. host->sg = NULL;
  701. host->data = data;
  702. if (data->flags & MMC_DATA_READ) {
  703. host->dir_status = DW_MCI_RECV_STATUS;
  704. dw_mci_ctrl_rd_thld(host, data);
  705. } else {
  706. host->dir_status = DW_MCI_SEND_STATUS;
  707. }
  708. if (dw_mci_submit_data_dma(host, data)) {
  709. int flags = SG_MITER_ATOMIC;
  710. if (host->data->flags & MMC_DATA_READ)
  711. flags |= SG_MITER_TO_SG;
  712. else
  713. flags |= SG_MITER_FROM_SG;
  714. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  715. host->sg = data->sg;
  716. host->part_buf_start = 0;
  717. host->part_buf_count = 0;
  718. mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
  719. spin_lock_irqsave(&host->irq_lock, irqflags);
  720. temp = mci_readl(host, INTMASK);
  721. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  722. mci_writel(host, INTMASK, temp);
  723. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  724. temp = mci_readl(host, CTRL);
  725. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  726. mci_writel(host, CTRL, temp);
  727. /*
  728. * Use the initial fifoth_val for PIO mode.
  729. * If next issued data may be transfered by DMA mode,
  730. * prev_blksz should be invalidated.
  731. */
  732. mci_writel(host, FIFOTH, host->fifoth_val);
  733. host->prev_blksz = 0;
  734. } else {
  735. /*
  736. * Keep the current block size.
  737. * It will be used to decide whether to update
  738. * fifoth register next time.
  739. */
  740. host->prev_blksz = data->blksz;
  741. }
  742. }
  743. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  744. {
  745. struct dw_mci *host = slot->host;
  746. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  747. unsigned int cmd_status = 0;
  748. mci_writel(host, CMDARG, arg);
  749. wmb();
  750. dw_mci_wait_while_busy(host, cmd);
  751. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  752. while (time_before(jiffies, timeout)) {
  753. cmd_status = mci_readl(host, CMD);
  754. if (!(cmd_status & SDMMC_CMD_START))
  755. return;
  756. }
  757. dev_err(&slot->mmc->class_dev,
  758. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  759. cmd, arg, cmd_status);
  760. }
  761. static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
  762. {
  763. struct dw_mci *host = slot->host;
  764. unsigned int clock = slot->clock;
  765. u32 div;
  766. u32 clk_en_a;
  767. u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
  768. /* We must continue to set bit 28 in CMD until the change is complete */
  769. if (host->state == STATE_WAITING_CMD11_DONE)
  770. sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
  771. if (!clock) {
  772. mci_writel(host, CLKENA, 0);
  773. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  774. } else if (clock != host->current_speed || force_clkinit) {
  775. div = host->bus_hz / clock;
  776. if (host->bus_hz % clock && host->bus_hz > clock)
  777. /*
  778. * move the + 1 after the divide to prevent
  779. * over-clocking the card.
  780. */
  781. div += 1;
  782. div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
  783. if ((clock << div) != slot->__clk_old || force_clkinit)
  784. dev_info(&slot->mmc->class_dev,
  785. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
  786. slot->id, host->bus_hz, clock,
  787. div ? ((host->bus_hz / div) >> 1) :
  788. host->bus_hz, div);
  789. /* disable clock */
  790. mci_writel(host, CLKENA, 0);
  791. mci_writel(host, CLKSRC, 0);
  792. /* inform CIU */
  793. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  794. /* set clock to desired speed */
  795. mci_writel(host, CLKDIV, div);
  796. /* inform CIU */
  797. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  798. /* enable clock; only low power if no SDIO */
  799. clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
  800. if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
  801. clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
  802. mci_writel(host, CLKENA, clk_en_a);
  803. /* inform CIU */
  804. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  805. /* keep the clock with reflecting clock dividor */
  806. slot->__clk_old = clock << div;
  807. }
  808. host->current_speed = clock;
  809. /* Set the current slot bus width */
  810. mci_writel(host, CTYPE, (slot->ctype << slot->id));
  811. }
  812. static void __dw_mci_start_request(struct dw_mci *host,
  813. struct dw_mci_slot *slot,
  814. struct mmc_command *cmd)
  815. {
  816. struct mmc_request *mrq;
  817. struct mmc_data *data;
  818. u32 cmdflags;
  819. mrq = slot->mrq;
  820. host->cur_slot = slot;
  821. host->mrq = mrq;
  822. host->pending_events = 0;
  823. host->completed_events = 0;
  824. host->cmd_status = 0;
  825. host->data_status = 0;
  826. host->dir_status = 0;
  827. data = cmd->data;
  828. if (data) {
  829. mci_writel(host, TMOUT, 0xFFFFFFFF);
  830. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  831. mci_writel(host, BLKSIZ, data->blksz);
  832. }
  833. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  834. /* this is the first command, send the initialization clock */
  835. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  836. cmdflags |= SDMMC_CMD_INIT;
  837. if (data) {
  838. dw_mci_submit_data(host, data);
  839. wmb();
  840. }
  841. dw_mci_start_command(host, cmd, cmdflags);
  842. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  843. unsigned long irqflags;
  844. /*
  845. * Databook says to fail after 2ms w/ no response, but evidence
  846. * shows that sometimes the cmd11 interrupt takes over 130ms.
  847. * We'll set to 500ms, plus an extra jiffy just in case jiffies
  848. * is just about to roll over.
  849. *
  850. * We do this whole thing under spinlock and only if the
  851. * command hasn't already completed (indicating the the irq
  852. * already ran so we don't want the timeout).
  853. */
  854. spin_lock_irqsave(&host->irq_lock, irqflags);
  855. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  856. mod_timer(&host->cmd11_timer,
  857. jiffies + msecs_to_jiffies(500) + 1);
  858. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  859. }
  860. if (mrq->stop)
  861. host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
  862. else
  863. host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
  864. }
  865. static void dw_mci_start_request(struct dw_mci *host,
  866. struct dw_mci_slot *slot)
  867. {
  868. struct mmc_request *mrq = slot->mrq;
  869. struct mmc_command *cmd;
  870. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  871. __dw_mci_start_request(host, slot, cmd);
  872. }
  873. /* must be called with host->lock held */
  874. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  875. struct mmc_request *mrq)
  876. {
  877. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  878. host->state);
  879. slot->mrq = mrq;
  880. if (host->state == STATE_WAITING_CMD11_DONE) {
  881. dev_warn(&slot->mmc->class_dev,
  882. "Voltage change didn't complete\n");
  883. /*
  884. * this case isn't expected to happen, so we can
  885. * either crash here or just try to continue on
  886. * in the closest possible state
  887. */
  888. host->state = STATE_IDLE;
  889. }
  890. if (host->state == STATE_IDLE) {
  891. host->state = STATE_SENDING_CMD;
  892. dw_mci_start_request(host, slot);
  893. } else {
  894. list_add_tail(&slot->queue_node, &host->queue);
  895. }
  896. }
  897. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  898. {
  899. struct dw_mci_slot *slot = mmc_priv(mmc);
  900. struct dw_mci *host = slot->host;
  901. WARN_ON(slot->mrq);
  902. /*
  903. * The check for card presence and queueing of the request must be
  904. * atomic, otherwise the card could be removed in between and the
  905. * request wouldn't fail until another card was inserted.
  906. */
  907. spin_lock_bh(&host->lock);
  908. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  909. spin_unlock_bh(&host->lock);
  910. mrq->cmd->error = -ENOMEDIUM;
  911. mmc_request_done(mmc, mrq);
  912. return;
  913. }
  914. dw_mci_queue_request(host, slot, mrq);
  915. spin_unlock_bh(&host->lock);
  916. }
  917. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  918. {
  919. struct dw_mci_slot *slot = mmc_priv(mmc);
  920. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  921. u32 regs;
  922. int ret;
  923. switch (ios->bus_width) {
  924. case MMC_BUS_WIDTH_4:
  925. slot->ctype = SDMMC_CTYPE_4BIT;
  926. break;
  927. case MMC_BUS_WIDTH_8:
  928. slot->ctype = SDMMC_CTYPE_8BIT;
  929. break;
  930. default:
  931. /* set default 1 bit mode */
  932. slot->ctype = SDMMC_CTYPE_1BIT;
  933. }
  934. regs = mci_readl(slot->host, UHS_REG);
  935. /* DDR mode set */
  936. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  937. ios->timing == MMC_TIMING_MMC_HS400)
  938. regs |= ((0x1 << slot->id) << 16);
  939. else
  940. regs &= ~((0x1 << slot->id) << 16);
  941. mci_writel(slot->host, UHS_REG, regs);
  942. slot->host->timing = ios->timing;
  943. /*
  944. * Use mirror of ios->clock to prevent race with mmc
  945. * core ios update when finding the minimum.
  946. */
  947. slot->clock = ios->clock;
  948. if (drv_data && drv_data->set_ios)
  949. drv_data->set_ios(slot->host, ios);
  950. switch (ios->power_mode) {
  951. case MMC_POWER_UP:
  952. if (!IS_ERR(mmc->supply.vmmc)) {
  953. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  954. ios->vdd);
  955. if (ret) {
  956. dev_err(slot->host->dev,
  957. "failed to enable vmmc regulator\n");
  958. /*return, if failed turn on vmmc*/
  959. return;
  960. }
  961. }
  962. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  963. regs = mci_readl(slot->host, PWREN);
  964. regs |= (1 << slot->id);
  965. mci_writel(slot->host, PWREN, regs);
  966. break;
  967. case MMC_POWER_ON:
  968. if (!slot->host->vqmmc_enabled) {
  969. if (!IS_ERR(mmc->supply.vqmmc)) {
  970. ret = regulator_enable(mmc->supply.vqmmc);
  971. if (ret < 0)
  972. dev_err(slot->host->dev,
  973. "failed to enable vqmmc\n");
  974. else
  975. slot->host->vqmmc_enabled = true;
  976. } else {
  977. /* Keep track so we don't reset again */
  978. slot->host->vqmmc_enabled = true;
  979. }
  980. /* Reset our state machine after powering on */
  981. dw_mci_ctrl_reset(slot->host,
  982. SDMMC_CTRL_ALL_RESET_FLAGS);
  983. }
  984. /* Adjust clock / bus width after power is up */
  985. dw_mci_setup_bus(slot, false);
  986. break;
  987. case MMC_POWER_OFF:
  988. /* Turn clock off before power goes down */
  989. dw_mci_setup_bus(slot, false);
  990. if (!IS_ERR(mmc->supply.vmmc))
  991. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  992. if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
  993. regulator_disable(mmc->supply.vqmmc);
  994. slot->host->vqmmc_enabled = false;
  995. regs = mci_readl(slot->host, PWREN);
  996. regs &= ~(1 << slot->id);
  997. mci_writel(slot->host, PWREN, regs);
  998. break;
  999. default:
  1000. break;
  1001. }
  1002. if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
  1003. slot->host->state = STATE_IDLE;
  1004. }
  1005. static int dw_mci_card_busy(struct mmc_host *mmc)
  1006. {
  1007. struct dw_mci_slot *slot = mmc_priv(mmc);
  1008. u32 status;
  1009. /*
  1010. * Check the busy bit which is low when DAT[3:0]
  1011. * (the data lines) are 0000
  1012. */
  1013. status = mci_readl(slot->host, STATUS);
  1014. return !!(status & SDMMC_STATUS_BUSY);
  1015. }
  1016. static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  1017. {
  1018. struct dw_mci_slot *slot = mmc_priv(mmc);
  1019. struct dw_mci *host = slot->host;
  1020. u32 uhs;
  1021. u32 v18 = SDMMC_UHS_18V << slot->id;
  1022. int min_uv, max_uv;
  1023. int ret;
  1024. /*
  1025. * Program the voltage. Note that some instances of dw_mmc may use
  1026. * the UHS_REG for this. For other instances (like exynos) the UHS_REG
  1027. * does no harm but you need to set the regulator directly. Try both.
  1028. */
  1029. uhs = mci_readl(host, UHS_REG);
  1030. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  1031. min_uv = 2700000;
  1032. max_uv = 3600000;
  1033. uhs &= ~v18;
  1034. } else {
  1035. min_uv = 1700000;
  1036. max_uv = 1950000;
  1037. uhs |= v18;
  1038. }
  1039. if (!IS_ERR(mmc->supply.vqmmc)) {
  1040. ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
  1041. if (ret) {
  1042. dev_dbg(&mmc->class_dev,
  1043. "Regulator set error %d: %d - %d\n",
  1044. ret, min_uv, max_uv);
  1045. return ret;
  1046. }
  1047. }
  1048. mci_writel(host, UHS_REG, uhs);
  1049. return 0;
  1050. }
  1051. static int dw_mci_get_ro(struct mmc_host *mmc)
  1052. {
  1053. int read_only;
  1054. struct dw_mci_slot *slot = mmc_priv(mmc);
  1055. int gpio_ro = mmc_gpio_get_ro(mmc);
  1056. /* Use platform get_ro function, else try on board write protect */
  1057. if ((slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT) ||
  1058. (slot->host->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT))
  1059. read_only = 0;
  1060. else if (!IS_ERR_VALUE(gpio_ro))
  1061. read_only = gpio_ro;
  1062. else
  1063. read_only =
  1064. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  1065. dev_dbg(&mmc->class_dev, "card is %s\n",
  1066. read_only ? "read-only" : "read-write");
  1067. return read_only;
  1068. }
  1069. static int dw_mci_get_cd(struct mmc_host *mmc)
  1070. {
  1071. int present;
  1072. struct dw_mci_slot *slot = mmc_priv(mmc);
  1073. struct dw_mci_board *brd = slot->host->pdata;
  1074. struct dw_mci *host = slot->host;
  1075. int gpio_cd = mmc_gpio_get_cd(mmc);
  1076. /* Use platform get_cd function, else try onboard card detect */
  1077. if ((brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION) ||
  1078. (mmc->caps & MMC_CAP_NONREMOVABLE))
  1079. present = 1;
  1080. else if (!IS_ERR_VALUE(gpio_cd))
  1081. present = gpio_cd;
  1082. else
  1083. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  1084. == 0 ? 1 : 0;
  1085. spin_lock_bh(&host->lock);
  1086. if (present) {
  1087. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1088. dev_dbg(&mmc->class_dev, "card is present\n");
  1089. } else {
  1090. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1091. dev_dbg(&mmc->class_dev, "card is not present\n");
  1092. }
  1093. spin_unlock_bh(&host->lock);
  1094. return present;
  1095. }
  1096. static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1097. {
  1098. struct dw_mci_slot *slot = mmc_priv(mmc);
  1099. struct dw_mci *host = slot->host;
  1100. /*
  1101. * Low power mode will stop the card clock when idle. According to the
  1102. * description of the CLKENA register we should disable low power mode
  1103. * for SDIO cards if we need SDIO interrupts to work.
  1104. */
  1105. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1106. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  1107. u32 clk_en_a_old;
  1108. u32 clk_en_a;
  1109. clk_en_a_old = mci_readl(host, CLKENA);
  1110. if (card->type == MMC_TYPE_SDIO ||
  1111. card->type == MMC_TYPE_SD_COMBO) {
  1112. set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1113. clk_en_a = clk_en_a_old & ~clken_low_pwr;
  1114. } else {
  1115. clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1116. clk_en_a = clk_en_a_old | clken_low_pwr;
  1117. }
  1118. if (clk_en_a != clk_en_a_old) {
  1119. mci_writel(host, CLKENA, clk_en_a);
  1120. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  1121. SDMMC_CMD_PRV_DAT_WAIT, 0);
  1122. }
  1123. }
  1124. }
  1125. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  1126. {
  1127. struct dw_mci_slot *slot = mmc_priv(mmc);
  1128. struct dw_mci *host = slot->host;
  1129. unsigned long irqflags;
  1130. u32 int_mask;
  1131. spin_lock_irqsave(&host->irq_lock, irqflags);
  1132. /* Enable/disable Slot Specific SDIO interrupt */
  1133. int_mask = mci_readl(host, INTMASK);
  1134. if (enb)
  1135. int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
  1136. else
  1137. int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
  1138. mci_writel(host, INTMASK, int_mask);
  1139. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1140. }
  1141. static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1142. {
  1143. struct dw_mci_slot *slot = mmc_priv(mmc);
  1144. struct dw_mci *host = slot->host;
  1145. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1146. int err = -ENOSYS;
  1147. if (drv_data && drv_data->execute_tuning)
  1148. err = drv_data->execute_tuning(slot);
  1149. return err;
  1150. }
  1151. static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
  1152. {
  1153. struct dw_mci_slot *slot = mmc_priv(mmc);
  1154. struct dw_mci *host = slot->host;
  1155. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1156. if (drv_data && drv_data->prepare_hs400_tuning)
  1157. return drv_data->prepare_hs400_tuning(host, ios);
  1158. return 0;
  1159. }
  1160. static const struct mmc_host_ops dw_mci_ops = {
  1161. .request = dw_mci_request,
  1162. .pre_req = dw_mci_pre_req,
  1163. .post_req = dw_mci_post_req,
  1164. .set_ios = dw_mci_set_ios,
  1165. .get_ro = dw_mci_get_ro,
  1166. .get_cd = dw_mci_get_cd,
  1167. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  1168. .execute_tuning = dw_mci_execute_tuning,
  1169. .card_busy = dw_mci_card_busy,
  1170. .start_signal_voltage_switch = dw_mci_switch_voltage,
  1171. .init_card = dw_mci_init_card,
  1172. .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
  1173. };
  1174. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  1175. __releases(&host->lock)
  1176. __acquires(&host->lock)
  1177. {
  1178. struct dw_mci_slot *slot;
  1179. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  1180. WARN_ON(host->cmd || host->data);
  1181. host->cur_slot->mrq = NULL;
  1182. host->mrq = NULL;
  1183. if (!list_empty(&host->queue)) {
  1184. slot = list_entry(host->queue.next,
  1185. struct dw_mci_slot, queue_node);
  1186. list_del(&slot->queue_node);
  1187. dev_vdbg(host->dev, "list not empty: %s is next\n",
  1188. mmc_hostname(slot->mmc));
  1189. host->state = STATE_SENDING_CMD;
  1190. dw_mci_start_request(host, slot);
  1191. } else {
  1192. dev_vdbg(host->dev, "list empty\n");
  1193. if (host->state == STATE_SENDING_CMD11)
  1194. host->state = STATE_WAITING_CMD11_DONE;
  1195. else
  1196. host->state = STATE_IDLE;
  1197. }
  1198. spin_unlock(&host->lock);
  1199. mmc_request_done(prev_mmc, mrq);
  1200. spin_lock(&host->lock);
  1201. }
  1202. static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  1203. {
  1204. u32 status = host->cmd_status;
  1205. host->cmd_status = 0;
  1206. /* Read the response from the card (up to 16 bytes) */
  1207. if (cmd->flags & MMC_RSP_PRESENT) {
  1208. if (cmd->flags & MMC_RSP_136) {
  1209. cmd->resp[3] = mci_readl(host, RESP0);
  1210. cmd->resp[2] = mci_readl(host, RESP1);
  1211. cmd->resp[1] = mci_readl(host, RESP2);
  1212. cmd->resp[0] = mci_readl(host, RESP3);
  1213. } else {
  1214. cmd->resp[0] = mci_readl(host, RESP0);
  1215. cmd->resp[1] = 0;
  1216. cmd->resp[2] = 0;
  1217. cmd->resp[3] = 0;
  1218. }
  1219. }
  1220. if (status & SDMMC_INT_RTO)
  1221. cmd->error = -ETIMEDOUT;
  1222. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  1223. cmd->error = -EILSEQ;
  1224. else if (status & SDMMC_INT_RESP_ERR)
  1225. cmd->error = -EIO;
  1226. else
  1227. cmd->error = 0;
  1228. if (cmd->error) {
  1229. /* newer ip versions need a delay between retries */
  1230. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  1231. mdelay(20);
  1232. }
  1233. return cmd->error;
  1234. }
  1235. static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
  1236. {
  1237. u32 status = host->data_status;
  1238. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1239. if (status & SDMMC_INT_DRTO) {
  1240. data->error = -ETIMEDOUT;
  1241. } else if (status & SDMMC_INT_DCRC) {
  1242. data->error = -EILSEQ;
  1243. } else if (status & SDMMC_INT_EBE) {
  1244. if (host->dir_status ==
  1245. DW_MCI_SEND_STATUS) {
  1246. /*
  1247. * No data CRC status was returned.
  1248. * The number of bytes transferred
  1249. * will be exaggerated in PIO mode.
  1250. */
  1251. data->bytes_xfered = 0;
  1252. data->error = -ETIMEDOUT;
  1253. } else if (host->dir_status ==
  1254. DW_MCI_RECV_STATUS) {
  1255. data->error = -EIO;
  1256. }
  1257. } else {
  1258. /* SDMMC_INT_SBE is included */
  1259. data->error = -EIO;
  1260. }
  1261. dev_dbg(host->dev, "data error, status 0x%08x\n", status);
  1262. /*
  1263. * After an error, there may be data lingering
  1264. * in the FIFO
  1265. */
  1266. dw_mci_reset(host);
  1267. } else {
  1268. data->bytes_xfered = data->blocks * data->blksz;
  1269. data->error = 0;
  1270. }
  1271. return data->error;
  1272. }
  1273. static void dw_mci_tasklet_func(unsigned long priv)
  1274. {
  1275. struct dw_mci *host = (struct dw_mci *)priv;
  1276. struct mmc_data *data;
  1277. struct mmc_command *cmd;
  1278. struct mmc_request *mrq;
  1279. enum dw_mci_state state;
  1280. enum dw_mci_state prev_state;
  1281. unsigned int err;
  1282. spin_lock(&host->lock);
  1283. state = host->state;
  1284. data = host->data;
  1285. mrq = host->mrq;
  1286. do {
  1287. prev_state = state;
  1288. switch (state) {
  1289. case STATE_IDLE:
  1290. case STATE_WAITING_CMD11_DONE:
  1291. break;
  1292. case STATE_SENDING_CMD11:
  1293. case STATE_SENDING_CMD:
  1294. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1295. &host->pending_events))
  1296. break;
  1297. cmd = host->cmd;
  1298. host->cmd = NULL;
  1299. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  1300. err = dw_mci_command_complete(host, cmd);
  1301. if (cmd == mrq->sbc && !err) {
  1302. prev_state = state = STATE_SENDING_CMD;
  1303. __dw_mci_start_request(host, host->cur_slot,
  1304. mrq->cmd);
  1305. goto unlock;
  1306. }
  1307. if (cmd->data && err) {
  1308. dw_mci_stop_dma(host);
  1309. send_stop_abort(host, data);
  1310. state = STATE_SENDING_STOP;
  1311. break;
  1312. }
  1313. if (!cmd->data || err) {
  1314. dw_mci_request_end(host, mrq);
  1315. goto unlock;
  1316. }
  1317. prev_state = state = STATE_SENDING_DATA;
  1318. /* fall through */
  1319. case STATE_SENDING_DATA:
  1320. /*
  1321. * We could get a data error and never a transfer
  1322. * complete so we'd better check for it here.
  1323. *
  1324. * Note that we don't really care if we also got a
  1325. * transfer complete; stopping the DMA and sending an
  1326. * abort won't hurt.
  1327. */
  1328. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1329. &host->pending_events)) {
  1330. dw_mci_stop_dma(host);
  1331. if (data->stop ||
  1332. !(host->data_status & (SDMMC_INT_DRTO |
  1333. SDMMC_INT_EBE)))
  1334. send_stop_abort(host, data);
  1335. state = STATE_DATA_ERROR;
  1336. break;
  1337. }
  1338. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1339. &host->pending_events))
  1340. break;
  1341. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  1342. /*
  1343. * Handle an EVENT_DATA_ERROR that might have shown up
  1344. * before the transfer completed. This might not have
  1345. * been caught by the check above because the interrupt
  1346. * could have gone off between the previous check and
  1347. * the check for transfer complete.
  1348. *
  1349. * Technically this ought not be needed assuming we
  1350. * get a DATA_COMPLETE eventually (we'll notice the
  1351. * error and end the request), but it shouldn't hurt.
  1352. *
  1353. * This has the advantage of sending the stop command.
  1354. */
  1355. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1356. &host->pending_events)) {
  1357. dw_mci_stop_dma(host);
  1358. if (data->stop ||
  1359. !(host->data_status & (SDMMC_INT_DRTO |
  1360. SDMMC_INT_EBE)))
  1361. send_stop_abort(host, data);
  1362. state = STATE_DATA_ERROR;
  1363. break;
  1364. }
  1365. prev_state = state = STATE_DATA_BUSY;
  1366. /* fall through */
  1367. case STATE_DATA_BUSY:
  1368. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  1369. &host->pending_events))
  1370. break;
  1371. host->data = NULL;
  1372. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  1373. err = dw_mci_data_complete(host, data);
  1374. if (!err) {
  1375. if (!data->stop || mrq->sbc) {
  1376. if (mrq->sbc && data->stop)
  1377. data->stop->error = 0;
  1378. dw_mci_request_end(host, mrq);
  1379. goto unlock;
  1380. }
  1381. /* stop command for open-ended transfer*/
  1382. if (data->stop)
  1383. send_stop_abort(host, data);
  1384. } else {
  1385. /*
  1386. * If we don't have a command complete now we'll
  1387. * never get one since we just reset everything;
  1388. * better end the request.
  1389. *
  1390. * If we do have a command complete we'll fall
  1391. * through to the SENDING_STOP command and
  1392. * everything will be peachy keen.
  1393. */
  1394. if (!test_bit(EVENT_CMD_COMPLETE,
  1395. &host->pending_events)) {
  1396. host->cmd = NULL;
  1397. dw_mci_request_end(host, mrq);
  1398. goto unlock;
  1399. }
  1400. }
  1401. /*
  1402. * If err has non-zero,
  1403. * stop-abort command has been already issued.
  1404. */
  1405. prev_state = state = STATE_SENDING_STOP;
  1406. /* fall through */
  1407. case STATE_SENDING_STOP:
  1408. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1409. &host->pending_events))
  1410. break;
  1411. /* CMD error in data command */
  1412. if (mrq->cmd->error && mrq->data)
  1413. dw_mci_reset(host);
  1414. host->cmd = NULL;
  1415. host->data = NULL;
  1416. if (mrq->stop)
  1417. dw_mci_command_complete(host, mrq->stop);
  1418. else
  1419. host->cmd_status = 0;
  1420. dw_mci_request_end(host, mrq);
  1421. goto unlock;
  1422. case STATE_DATA_ERROR:
  1423. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1424. &host->pending_events))
  1425. break;
  1426. state = STATE_DATA_BUSY;
  1427. break;
  1428. }
  1429. } while (state != prev_state);
  1430. host->state = state;
  1431. unlock:
  1432. spin_unlock(&host->lock);
  1433. }
  1434. /* push final bytes to part_buf, only use during push */
  1435. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1436. {
  1437. memcpy((void *)&host->part_buf, buf, cnt);
  1438. host->part_buf_count = cnt;
  1439. }
  1440. /* append bytes to part_buf, only use during push */
  1441. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1442. {
  1443. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  1444. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  1445. host->part_buf_count += cnt;
  1446. return cnt;
  1447. }
  1448. /* pull first bytes from part_buf, only use during pull */
  1449. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1450. {
  1451. cnt = min(cnt, (int)host->part_buf_count);
  1452. if (cnt) {
  1453. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  1454. cnt);
  1455. host->part_buf_count -= cnt;
  1456. host->part_buf_start += cnt;
  1457. }
  1458. return cnt;
  1459. }
  1460. /* pull final bytes from the part_buf, assuming it's just been filled */
  1461. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  1462. {
  1463. memcpy(buf, &host->part_buf, cnt);
  1464. host->part_buf_start = cnt;
  1465. host->part_buf_count = (1 << host->data_shift) - cnt;
  1466. }
  1467. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  1468. {
  1469. struct mmc_data *data = host->data;
  1470. int init_cnt = cnt;
  1471. /* try and push anything in the part_buf */
  1472. if (unlikely(host->part_buf_count)) {
  1473. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1474. buf += len;
  1475. cnt -= len;
  1476. if (host->part_buf_count == 2) {
  1477. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1478. host->part_buf_count = 0;
  1479. }
  1480. }
  1481. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1482. if (unlikely((unsigned long)buf & 0x1)) {
  1483. while (cnt >= 2) {
  1484. u16 aligned_buf[64];
  1485. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1486. int items = len >> 1;
  1487. int i;
  1488. /* memcpy from input buffer into aligned buffer */
  1489. memcpy(aligned_buf, buf, len);
  1490. buf += len;
  1491. cnt -= len;
  1492. /* push data from aligned buffer into fifo */
  1493. for (i = 0; i < items; ++i)
  1494. mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
  1495. }
  1496. } else
  1497. #endif
  1498. {
  1499. u16 *pdata = buf;
  1500. for (; cnt >= 2; cnt -= 2)
  1501. mci_fifo_writew(host->fifo_reg, *pdata++);
  1502. buf = pdata;
  1503. }
  1504. /* put anything remaining in the part_buf */
  1505. if (cnt) {
  1506. dw_mci_set_part_bytes(host, buf, cnt);
  1507. /* Push data if we have reached the expected data length */
  1508. if ((data->bytes_xfered + init_cnt) ==
  1509. (data->blksz * data->blocks))
  1510. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1511. }
  1512. }
  1513. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1514. {
  1515. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1516. if (unlikely((unsigned long)buf & 0x1)) {
  1517. while (cnt >= 2) {
  1518. /* pull data from fifo into aligned buffer */
  1519. u16 aligned_buf[64];
  1520. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1521. int items = len >> 1;
  1522. int i;
  1523. for (i = 0; i < items; ++i)
  1524. aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
  1525. /* memcpy from aligned buffer into output buffer */
  1526. memcpy(buf, aligned_buf, len);
  1527. buf += len;
  1528. cnt -= len;
  1529. }
  1530. } else
  1531. #endif
  1532. {
  1533. u16 *pdata = buf;
  1534. for (; cnt >= 2; cnt -= 2)
  1535. *pdata++ = mci_fifo_readw(host->fifo_reg);
  1536. buf = pdata;
  1537. }
  1538. if (cnt) {
  1539. host->part_buf16 = mci_fifo_readw(host->fifo_reg);
  1540. dw_mci_pull_final_bytes(host, buf, cnt);
  1541. }
  1542. }
  1543. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  1544. {
  1545. struct mmc_data *data = host->data;
  1546. int init_cnt = cnt;
  1547. /* try and push anything in the part_buf */
  1548. if (unlikely(host->part_buf_count)) {
  1549. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1550. buf += len;
  1551. cnt -= len;
  1552. if (host->part_buf_count == 4) {
  1553. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  1554. host->part_buf_count = 0;
  1555. }
  1556. }
  1557. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1558. if (unlikely((unsigned long)buf & 0x3)) {
  1559. while (cnt >= 4) {
  1560. u32 aligned_buf[32];
  1561. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1562. int items = len >> 2;
  1563. int i;
  1564. /* memcpy from input buffer into aligned buffer */
  1565. memcpy(aligned_buf, buf, len);
  1566. buf += len;
  1567. cnt -= len;
  1568. /* push data from aligned buffer into fifo */
  1569. for (i = 0; i < items; ++i)
  1570. mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
  1571. }
  1572. } else
  1573. #endif
  1574. {
  1575. u32 *pdata = buf;
  1576. for (; cnt >= 4; cnt -= 4)
  1577. mci_fifo_writel(host->fifo_reg, *pdata++);
  1578. buf = pdata;
  1579. }
  1580. /* put anything remaining in the part_buf */
  1581. if (cnt) {
  1582. dw_mci_set_part_bytes(host, buf, cnt);
  1583. /* Push data if we have reached the expected data length */
  1584. if ((data->bytes_xfered + init_cnt) ==
  1585. (data->blksz * data->blocks))
  1586. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  1587. }
  1588. }
  1589. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  1590. {
  1591. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1592. if (unlikely((unsigned long)buf & 0x3)) {
  1593. while (cnt >= 4) {
  1594. /* pull data from fifo into aligned buffer */
  1595. u32 aligned_buf[32];
  1596. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1597. int items = len >> 2;
  1598. int i;
  1599. for (i = 0; i < items; ++i)
  1600. aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
  1601. /* memcpy from aligned buffer into output buffer */
  1602. memcpy(buf, aligned_buf, len);
  1603. buf += len;
  1604. cnt -= len;
  1605. }
  1606. } else
  1607. #endif
  1608. {
  1609. u32 *pdata = buf;
  1610. for (; cnt >= 4; cnt -= 4)
  1611. *pdata++ = mci_fifo_readl(host->fifo_reg);
  1612. buf = pdata;
  1613. }
  1614. if (cnt) {
  1615. host->part_buf32 = mci_fifo_readl(host->fifo_reg);
  1616. dw_mci_pull_final_bytes(host, buf, cnt);
  1617. }
  1618. }
  1619. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1620. {
  1621. struct mmc_data *data = host->data;
  1622. int init_cnt = cnt;
  1623. /* try and push anything in the part_buf */
  1624. if (unlikely(host->part_buf_count)) {
  1625. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1626. buf += len;
  1627. cnt -= len;
  1628. if (host->part_buf_count == 8) {
  1629. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  1630. host->part_buf_count = 0;
  1631. }
  1632. }
  1633. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1634. if (unlikely((unsigned long)buf & 0x7)) {
  1635. while (cnt >= 8) {
  1636. u64 aligned_buf[16];
  1637. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1638. int items = len >> 3;
  1639. int i;
  1640. /* memcpy from input buffer into aligned buffer */
  1641. memcpy(aligned_buf, buf, len);
  1642. buf += len;
  1643. cnt -= len;
  1644. /* push data from aligned buffer into fifo */
  1645. for (i = 0; i < items; ++i)
  1646. mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
  1647. }
  1648. } else
  1649. #endif
  1650. {
  1651. u64 *pdata = buf;
  1652. for (; cnt >= 8; cnt -= 8)
  1653. mci_fifo_writeq(host->fifo_reg, *pdata++);
  1654. buf = pdata;
  1655. }
  1656. /* put anything remaining in the part_buf */
  1657. if (cnt) {
  1658. dw_mci_set_part_bytes(host, buf, cnt);
  1659. /* Push data if we have reached the expected data length */
  1660. if ((data->bytes_xfered + init_cnt) ==
  1661. (data->blksz * data->blocks))
  1662. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  1663. }
  1664. }
  1665. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1666. {
  1667. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1668. if (unlikely((unsigned long)buf & 0x7)) {
  1669. while (cnt >= 8) {
  1670. /* pull data from fifo into aligned buffer */
  1671. u64 aligned_buf[16];
  1672. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1673. int items = len >> 3;
  1674. int i;
  1675. for (i = 0; i < items; ++i)
  1676. aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
  1677. /* memcpy from aligned buffer into output buffer */
  1678. memcpy(buf, aligned_buf, len);
  1679. buf += len;
  1680. cnt -= len;
  1681. }
  1682. } else
  1683. #endif
  1684. {
  1685. u64 *pdata = buf;
  1686. for (; cnt >= 8; cnt -= 8)
  1687. *pdata++ = mci_fifo_readq(host->fifo_reg);
  1688. buf = pdata;
  1689. }
  1690. if (cnt) {
  1691. host->part_buf = mci_fifo_readq(host->fifo_reg);
  1692. dw_mci_pull_final_bytes(host, buf, cnt);
  1693. }
  1694. }
  1695. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  1696. {
  1697. int len;
  1698. /* get remaining partial bytes */
  1699. len = dw_mci_pull_part_bytes(host, buf, cnt);
  1700. if (unlikely(len == cnt))
  1701. return;
  1702. buf += len;
  1703. cnt -= len;
  1704. /* get the rest of the data */
  1705. host->pull_data(host, buf, cnt);
  1706. }
  1707. static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
  1708. {
  1709. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1710. void *buf;
  1711. unsigned int offset;
  1712. struct mmc_data *data = host->data;
  1713. int shift = host->data_shift;
  1714. u32 status;
  1715. unsigned int len;
  1716. unsigned int remain, fcnt;
  1717. do {
  1718. if (!sg_miter_next(sg_miter))
  1719. goto done;
  1720. host->sg = sg_miter->piter.sg;
  1721. buf = sg_miter->addr;
  1722. remain = sg_miter->length;
  1723. offset = 0;
  1724. do {
  1725. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  1726. << shift) + host->part_buf_count;
  1727. len = min(remain, fcnt);
  1728. if (!len)
  1729. break;
  1730. dw_mci_pull_data(host, (void *)(buf + offset), len);
  1731. data->bytes_xfered += len;
  1732. offset += len;
  1733. remain -= len;
  1734. } while (remain);
  1735. sg_miter->consumed = offset;
  1736. status = mci_readl(host, MINTSTS);
  1737. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1738. /* if the RXDR is ready read again */
  1739. } while ((status & SDMMC_INT_RXDR) ||
  1740. (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
  1741. if (!remain) {
  1742. if (!sg_miter_next(sg_miter))
  1743. goto done;
  1744. sg_miter->consumed = 0;
  1745. }
  1746. sg_miter_stop(sg_miter);
  1747. return;
  1748. done:
  1749. sg_miter_stop(sg_miter);
  1750. host->sg = NULL;
  1751. smp_wmb();
  1752. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1753. }
  1754. static void dw_mci_write_data_pio(struct dw_mci *host)
  1755. {
  1756. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1757. void *buf;
  1758. unsigned int offset;
  1759. struct mmc_data *data = host->data;
  1760. int shift = host->data_shift;
  1761. u32 status;
  1762. unsigned int len;
  1763. unsigned int fifo_depth = host->fifo_depth;
  1764. unsigned int remain, fcnt;
  1765. do {
  1766. if (!sg_miter_next(sg_miter))
  1767. goto done;
  1768. host->sg = sg_miter->piter.sg;
  1769. buf = sg_miter->addr;
  1770. remain = sg_miter->length;
  1771. offset = 0;
  1772. do {
  1773. fcnt = ((fifo_depth -
  1774. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  1775. << shift) - host->part_buf_count;
  1776. len = min(remain, fcnt);
  1777. if (!len)
  1778. break;
  1779. host->push_data(host, (void *)(buf + offset), len);
  1780. data->bytes_xfered += len;
  1781. offset += len;
  1782. remain -= len;
  1783. } while (remain);
  1784. sg_miter->consumed = offset;
  1785. status = mci_readl(host, MINTSTS);
  1786. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1787. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  1788. if (!remain) {
  1789. if (!sg_miter_next(sg_miter))
  1790. goto done;
  1791. sg_miter->consumed = 0;
  1792. }
  1793. sg_miter_stop(sg_miter);
  1794. return;
  1795. done:
  1796. sg_miter_stop(sg_miter);
  1797. host->sg = NULL;
  1798. smp_wmb();
  1799. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1800. }
  1801. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  1802. {
  1803. if (!host->cmd_status)
  1804. host->cmd_status = status;
  1805. smp_wmb();
  1806. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1807. tasklet_schedule(&host->tasklet);
  1808. }
  1809. static void dw_mci_handle_cd(struct dw_mci *host)
  1810. {
  1811. int i;
  1812. for (i = 0; i < host->num_slots; i++) {
  1813. struct dw_mci_slot *slot = host->slot[i];
  1814. if (!slot)
  1815. continue;
  1816. if (slot->mmc->ops->card_event)
  1817. slot->mmc->ops->card_event(slot->mmc);
  1818. mmc_detect_change(slot->mmc,
  1819. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1820. }
  1821. }
  1822. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  1823. {
  1824. struct dw_mci *host = dev_id;
  1825. u32 pending;
  1826. int i;
  1827. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  1828. /*
  1829. * DTO fix - version 2.10a and below, and only if internal DMA
  1830. * is configured.
  1831. */
  1832. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  1833. if (!pending &&
  1834. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  1835. pending |= SDMMC_INT_DATA_OVER;
  1836. }
  1837. if (pending) {
  1838. /* Check volt switch first, since it can look like an error */
  1839. if ((host->state == STATE_SENDING_CMD11) &&
  1840. (pending & SDMMC_INT_VOLT_SWITCH)) {
  1841. unsigned long irqflags;
  1842. mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
  1843. pending &= ~SDMMC_INT_VOLT_SWITCH;
  1844. /*
  1845. * Hold the lock; we know cmd11_timer can't be kicked
  1846. * off after the lock is released, so safe to delete.
  1847. */
  1848. spin_lock_irqsave(&host->irq_lock, irqflags);
  1849. dw_mci_cmd_interrupt(host, pending);
  1850. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1851. del_timer(&host->cmd11_timer);
  1852. }
  1853. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  1854. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  1855. host->cmd_status = pending;
  1856. smp_wmb();
  1857. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1858. }
  1859. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  1860. /* if there is an error report DATA_ERROR */
  1861. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  1862. host->data_status = pending;
  1863. smp_wmb();
  1864. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1865. tasklet_schedule(&host->tasklet);
  1866. }
  1867. if (pending & SDMMC_INT_DATA_OVER) {
  1868. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  1869. if (!host->data_status)
  1870. host->data_status = pending;
  1871. smp_wmb();
  1872. if (host->dir_status == DW_MCI_RECV_STATUS) {
  1873. if (host->sg != NULL)
  1874. dw_mci_read_data_pio(host, true);
  1875. }
  1876. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1877. tasklet_schedule(&host->tasklet);
  1878. }
  1879. if (pending & SDMMC_INT_RXDR) {
  1880. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1881. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  1882. dw_mci_read_data_pio(host, false);
  1883. }
  1884. if (pending & SDMMC_INT_TXDR) {
  1885. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1886. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  1887. dw_mci_write_data_pio(host);
  1888. }
  1889. if (pending & SDMMC_INT_CMD_DONE) {
  1890. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1891. dw_mci_cmd_interrupt(host, pending);
  1892. }
  1893. if (pending & SDMMC_INT_CD) {
  1894. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1895. dw_mci_handle_cd(host);
  1896. }
  1897. /* Handle SDIO Interrupts */
  1898. for (i = 0; i < host->num_slots; i++) {
  1899. struct dw_mci_slot *slot = host->slot[i];
  1900. if (!slot)
  1901. continue;
  1902. if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
  1903. mci_writel(host, RINTSTS,
  1904. SDMMC_INT_SDIO(slot->sdio_id));
  1905. mmc_signal_sdio_irq(slot->mmc);
  1906. }
  1907. }
  1908. }
  1909. #ifdef CONFIG_MMC_DW_IDMAC
  1910. /* Handle DMA interrupts */
  1911. if (host->dma_64bit_address == 1) {
  1912. pending = mci_readl(host, IDSTS64);
  1913. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1914. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
  1915. SDMMC_IDMAC_INT_RI);
  1916. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
  1917. host->dma_ops->complete(host);
  1918. }
  1919. } else {
  1920. pending = mci_readl(host, IDSTS);
  1921. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1922. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
  1923. SDMMC_IDMAC_INT_RI);
  1924. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1925. host->dma_ops->complete(host);
  1926. }
  1927. }
  1928. #endif
  1929. return IRQ_HANDLED;
  1930. }
  1931. #ifdef CONFIG_OF
  1932. /* given a slot id, find out the device node representing that slot */
  1933. static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
  1934. {
  1935. struct device_node *np;
  1936. const __be32 *addr;
  1937. int len;
  1938. if (!dev || !dev->of_node)
  1939. return NULL;
  1940. for_each_child_of_node(dev->of_node, np) {
  1941. addr = of_get_property(np, "reg", &len);
  1942. if (!addr || (len < sizeof(int)))
  1943. continue;
  1944. if (be32_to_cpup(addr) == slot)
  1945. return np;
  1946. }
  1947. return NULL;
  1948. }
  1949. static struct dw_mci_of_slot_quirks {
  1950. char *quirk;
  1951. int id;
  1952. } of_slot_quirks[] = {
  1953. {
  1954. .quirk = "disable-wp",
  1955. .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
  1956. },
  1957. };
  1958. static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
  1959. {
  1960. struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
  1961. int quirks = 0;
  1962. int idx;
  1963. /* get quirks */
  1964. for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
  1965. if (of_get_property(np, of_slot_quirks[idx].quirk, NULL)) {
  1966. dev_warn(dev, "Slot quirk %s is deprecated\n",
  1967. of_slot_quirks[idx].quirk);
  1968. quirks |= of_slot_quirks[idx].id;
  1969. }
  1970. return quirks;
  1971. }
  1972. #else /* CONFIG_OF */
  1973. static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
  1974. {
  1975. return 0;
  1976. }
  1977. #endif /* CONFIG_OF */
  1978. static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1979. {
  1980. struct mmc_host *mmc;
  1981. struct dw_mci_slot *slot;
  1982. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1983. int ctrl_id, ret;
  1984. u32 freq[2];
  1985. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
  1986. if (!mmc)
  1987. return -ENOMEM;
  1988. slot = mmc_priv(mmc);
  1989. slot->id = id;
  1990. slot->sdio_id = host->sdio_id0 + id;
  1991. slot->mmc = mmc;
  1992. slot->host = host;
  1993. host->slot[id] = slot;
  1994. slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
  1995. mmc->ops = &dw_mci_ops;
  1996. if (of_property_read_u32_array(host->dev->of_node,
  1997. "clock-freq-min-max", freq, 2)) {
  1998. mmc->f_min = DW_MCI_FREQ_MIN;
  1999. mmc->f_max = DW_MCI_FREQ_MAX;
  2000. } else {
  2001. mmc->f_min = freq[0];
  2002. mmc->f_max = freq[1];
  2003. }
  2004. /*if there are external regulators, get them*/
  2005. ret = mmc_regulator_get_supply(mmc);
  2006. if (ret == -EPROBE_DEFER)
  2007. goto err_host_allocated;
  2008. if (!mmc->ocr_avail)
  2009. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  2010. if (host->pdata->caps)
  2011. mmc->caps = host->pdata->caps;
  2012. if (host->pdata->pm_caps)
  2013. mmc->pm_caps = host->pdata->pm_caps;
  2014. if (host->dev->of_node) {
  2015. ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  2016. if (ctrl_id < 0)
  2017. ctrl_id = 0;
  2018. } else {
  2019. ctrl_id = to_platform_device(host->dev)->id;
  2020. }
  2021. if (drv_data && drv_data->caps)
  2022. mmc->caps |= drv_data->caps[ctrl_id];
  2023. if (host->pdata->caps2)
  2024. mmc->caps2 = host->pdata->caps2;
  2025. ret = mmc_of_parse(mmc);
  2026. if (ret)
  2027. goto err_host_allocated;
  2028. if (host->pdata->blk_settings) {
  2029. mmc->max_segs = host->pdata->blk_settings->max_segs;
  2030. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  2031. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  2032. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  2033. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  2034. } else {
  2035. /* Useful defaults if platform data is unset. */
  2036. #ifdef CONFIG_MMC_DW_IDMAC
  2037. mmc->max_segs = host->ring_size;
  2038. mmc->max_blk_size = 65536;
  2039. mmc->max_seg_size = 0x1000;
  2040. mmc->max_req_size = mmc->max_seg_size * host->ring_size;
  2041. mmc->max_blk_count = mmc->max_req_size / 512;
  2042. #else
  2043. mmc->max_segs = 64;
  2044. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  2045. mmc->max_blk_count = 512;
  2046. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  2047. mmc->max_seg_size = mmc->max_req_size;
  2048. #endif /* CONFIG_MMC_DW_IDMAC */
  2049. }
  2050. if (dw_mci_get_cd(mmc))
  2051. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  2052. else
  2053. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  2054. ret = mmc_add_host(mmc);
  2055. if (ret)
  2056. goto err_host_allocated;
  2057. #if defined(CONFIG_DEBUG_FS)
  2058. dw_mci_init_debugfs(slot);
  2059. #endif
  2060. return 0;
  2061. err_host_allocated:
  2062. mmc_free_host(mmc);
  2063. return ret;
  2064. }
  2065. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  2066. {
  2067. /* Debugfs stuff is cleaned up by mmc core */
  2068. mmc_remove_host(slot->mmc);
  2069. slot->host->slot[id] = NULL;
  2070. mmc_free_host(slot->mmc);
  2071. }
  2072. static void dw_mci_init_dma(struct dw_mci *host)
  2073. {
  2074. int addr_config;
  2075. /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */
  2076. addr_config = (mci_readl(host, HCON) >> 27) & 0x01;
  2077. if (addr_config == 1) {
  2078. /* host supports IDMAC in 64-bit address mode */
  2079. host->dma_64bit_address = 1;
  2080. dev_info(host->dev, "IDMAC supports 64-bit address mode.\n");
  2081. if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
  2082. dma_set_coherent_mask(host->dev, DMA_BIT_MASK(64));
  2083. } else {
  2084. /* host supports IDMAC in 32-bit address mode */
  2085. host->dma_64bit_address = 0;
  2086. dev_info(host->dev, "IDMAC supports 32-bit address mode.\n");
  2087. }
  2088. /* Alloc memory for sg translation */
  2089. host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
  2090. &host->sg_dma, GFP_KERNEL);
  2091. if (!host->sg_cpu) {
  2092. dev_err(host->dev, "%s: could not alloc DMA memory\n",
  2093. __func__);
  2094. goto no_dma;
  2095. }
  2096. /* Determine which DMA interface to use */
  2097. #ifdef CONFIG_MMC_DW_IDMAC
  2098. host->dma_ops = &dw_mci_idmac_ops;
  2099. dev_info(host->dev, "Using internal DMA controller.\n");
  2100. #endif
  2101. if (!host->dma_ops)
  2102. goto no_dma;
  2103. if (host->dma_ops->init && host->dma_ops->start &&
  2104. host->dma_ops->stop && host->dma_ops->cleanup) {
  2105. if (host->dma_ops->init(host)) {
  2106. dev_err(host->dev, "%s: Unable to initialize "
  2107. "DMA Controller.\n", __func__);
  2108. goto no_dma;
  2109. }
  2110. } else {
  2111. dev_err(host->dev, "DMA initialization not found.\n");
  2112. goto no_dma;
  2113. }
  2114. host->use_dma = 1;
  2115. return;
  2116. no_dma:
  2117. dev_info(host->dev, "Using PIO mode.\n");
  2118. host->use_dma = 0;
  2119. return;
  2120. }
  2121. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
  2122. {
  2123. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  2124. u32 ctrl;
  2125. ctrl = mci_readl(host, CTRL);
  2126. ctrl |= reset;
  2127. mci_writel(host, CTRL, ctrl);
  2128. /* wait till resets clear */
  2129. do {
  2130. ctrl = mci_readl(host, CTRL);
  2131. if (!(ctrl & reset))
  2132. return true;
  2133. } while (time_before(jiffies, timeout));
  2134. dev_err(host->dev,
  2135. "Timeout resetting block (ctrl reset %#x)\n",
  2136. ctrl & reset);
  2137. return false;
  2138. }
  2139. static bool dw_mci_reset(struct dw_mci *host)
  2140. {
  2141. u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
  2142. bool ret = false;
  2143. /*
  2144. * Reseting generates a block interrupt, hence setting
  2145. * the scatter-gather pointer to NULL.
  2146. */
  2147. if (host->sg) {
  2148. sg_miter_stop(&host->sg_miter);
  2149. host->sg = NULL;
  2150. }
  2151. if (host->use_dma)
  2152. flags |= SDMMC_CTRL_DMA_RESET;
  2153. if (dw_mci_ctrl_reset(host, flags)) {
  2154. /*
  2155. * In all cases we clear the RAWINTS register to clear any
  2156. * interrupts.
  2157. */
  2158. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2159. /* if using dma we wait for dma_req to clear */
  2160. if (host->use_dma) {
  2161. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  2162. u32 status;
  2163. do {
  2164. status = mci_readl(host, STATUS);
  2165. if (!(status & SDMMC_STATUS_DMA_REQ))
  2166. break;
  2167. cpu_relax();
  2168. } while (time_before(jiffies, timeout));
  2169. if (status & SDMMC_STATUS_DMA_REQ) {
  2170. dev_err(host->dev,
  2171. "%s: Timeout waiting for dma_req to "
  2172. "clear during reset\n", __func__);
  2173. goto ciu_out;
  2174. }
  2175. /* when using DMA next we reset the fifo again */
  2176. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
  2177. goto ciu_out;
  2178. }
  2179. } else {
  2180. /* if the controller reset bit did clear, then set clock regs */
  2181. if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
  2182. dev_err(host->dev, "%s: fifo/dma reset bits didn't "
  2183. "clear but ciu was reset, doing clock update\n",
  2184. __func__);
  2185. goto ciu_out;
  2186. }
  2187. }
  2188. #if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
  2189. /* It is also recommended that we reset and reprogram idmac */
  2190. dw_mci_idmac_reset(host);
  2191. #endif
  2192. ret = true;
  2193. ciu_out:
  2194. /* After a CTRL reset we need to have CIU set clock registers */
  2195. mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
  2196. return ret;
  2197. }
  2198. static void dw_mci_cmd11_timer(unsigned long arg)
  2199. {
  2200. struct dw_mci *host = (struct dw_mci *)arg;
  2201. if (host->state != STATE_SENDING_CMD11) {
  2202. dev_warn(host->dev, "Unexpected CMD11 timeout\n");
  2203. return;
  2204. }
  2205. host->cmd_status = SDMMC_INT_RTO;
  2206. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2207. tasklet_schedule(&host->tasklet);
  2208. }
  2209. #ifdef CONFIG_OF
  2210. static struct dw_mci_of_quirks {
  2211. char *quirk;
  2212. int id;
  2213. } of_quirks[] = {
  2214. {
  2215. .quirk = "broken-cd",
  2216. .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
  2217. }, {
  2218. .quirk = "disable-wp",
  2219. .id = DW_MCI_QUIRK_NO_WRITE_PROTECT,
  2220. },
  2221. };
  2222. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2223. {
  2224. struct dw_mci_board *pdata;
  2225. struct device *dev = host->dev;
  2226. struct device_node *np = dev->of_node;
  2227. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2228. int idx, ret;
  2229. u32 clock_frequency;
  2230. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2231. if (!pdata)
  2232. return ERR_PTR(-ENOMEM);
  2233. /* find out number of slots supported */
  2234. if (of_property_read_u32(dev->of_node, "num-slots",
  2235. &pdata->num_slots)) {
  2236. dev_info(dev, "num-slots property not found, "
  2237. "assuming 1 slot is available\n");
  2238. pdata->num_slots = 1;
  2239. }
  2240. /* get quirks */
  2241. for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
  2242. if (of_get_property(np, of_quirks[idx].quirk, NULL))
  2243. pdata->quirks |= of_quirks[idx].id;
  2244. if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
  2245. dev_info(dev, "fifo-depth property not found, using "
  2246. "value of FIFOTH register as default\n");
  2247. of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
  2248. if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
  2249. pdata->bus_hz = clock_frequency;
  2250. if (drv_data && drv_data->parse_dt) {
  2251. ret = drv_data->parse_dt(host);
  2252. if (ret)
  2253. return ERR_PTR(ret);
  2254. }
  2255. if (of_find_property(np, "supports-highspeed", NULL))
  2256. pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2257. return pdata;
  2258. }
  2259. #else /* CONFIG_OF */
  2260. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2261. {
  2262. return ERR_PTR(-EINVAL);
  2263. }
  2264. #endif /* CONFIG_OF */
  2265. static void dw_mci_enable_cd(struct dw_mci *host)
  2266. {
  2267. struct dw_mci_board *brd = host->pdata;
  2268. unsigned long irqflags;
  2269. u32 temp;
  2270. int i;
  2271. /* No need for CD if broken card detection */
  2272. if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  2273. return;
  2274. /* No need for CD if all slots have a non-error GPIO */
  2275. for (i = 0; i < host->num_slots; i++) {
  2276. struct dw_mci_slot *slot = host->slot[i];
  2277. if (IS_ERR_VALUE(mmc_gpio_get_cd(slot->mmc)))
  2278. break;
  2279. }
  2280. if (i == host->num_slots)
  2281. return;
  2282. spin_lock_irqsave(&host->irq_lock, irqflags);
  2283. temp = mci_readl(host, INTMASK);
  2284. temp |= SDMMC_INT_CD;
  2285. mci_writel(host, INTMASK, temp);
  2286. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2287. }
  2288. int dw_mci_probe(struct dw_mci *host)
  2289. {
  2290. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2291. int width, i, ret = 0;
  2292. u32 fifo_size;
  2293. int init_slots = 0;
  2294. if (!host->pdata) {
  2295. host->pdata = dw_mci_parse_dt(host);
  2296. if (IS_ERR(host->pdata)) {
  2297. dev_err(host->dev, "platform data not available\n");
  2298. return -EINVAL;
  2299. }
  2300. }
  2301. if (host->pdata->num_slots > 1) {
  2302. dev_err(host->dev,
  2303. "Platform data must supply num_slots.\n");
  2304. return -ENODEV;
  2305. }
  2306. host->biu_clk = devm_clk_get(host->dev, "biu");
  2307. if (IS_ERR(host->biu_clk)) {
  2308. dev_dbg(host->dev, "biu clock not available\n");
  2309. } else {
  2310. ret = clk_prepare_enable(host->biu_clk);
  2311. if (ret) {
  2312. dev_err(host->dev, "failed to enable biu clock\n");
  2313. return ret;
  2314. }
  2315. }
  2316. host->ciu_clk = devm_clk_get(host->dev, "ciu");
  2317. if (IS_ERR(host->ciu_clk)) {
  2318. dev_dbg(host->dev, "ciu clock not available\n");
  2319. host->bus_hz = host->pdata->bus_hz;
  2320. } else {
  2321. ret = clk_prepare_enable(host->ciu_clk);
  2322. if (ret) {
  2323. dev_err(host->dev, "failed to enable ciu clock\n");
  2324. goto err_clk_biu;
  2325. }
  2326. if (host->pdata->bus_hz) {
  2327. ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
  2328. if (ret)
  2329. dev_warn(host->dev,
  2330. "Unable to set bus rate to %uHz\n",
  2331. host->pdata->bus_hz);
  2332. }
  2333. host->bus_hz = clk_get_rate(host->ciu_clk);
  2334. }
  2335. if (!host->bus_hz) {
  2336. dev_err(host->dev,
  2337. "Platform data must supply bus speed\n");
  2338. ret = -ENODEV;
  2339. goto err_clk_ciu;
  2340. }
  2341. if (drv_data && drv_data->init) {
  2342. ret = drv_data->init(host);
  2343. if (ret) {
  2344. dev_err(host->dev,
  2345. "implementation specific init failed\n");
  2346. goto err_clk_ciu;
  2347. }
  2348. }
  2349. if (drv_data && drv_data->setup_clock) {
  2350. ret = drv_data->setup_clock(host);
  2351. if (ret) {
  2352. dev_err(host->dev,
  2353. "implementation specific clock setup failed\n");
  2354. goto err_clk_ciu;
  2355. }
  2356. }
  2357. setup_timer(&host->cmd11_timer,
  2358. dw_mci_cmd11_timer, (unsigned long)host);
  2359. host->quirks = host->pdata->quirks;
  2360. spin_lock_init(&host->lock);
  2361. spin_lock_init(&host->irq_lock);
  2362. INIT_LIST_HEAD(&host->queue);
  2363. /*
  2364. * Get the host data width - this assumes that HCON has been set with
  2365. * the correct values.
  2366. */
  2367. i = (mci_readl(host, HCON) >> 7) & 0x7;
  2368. if (!i) {
  2369. host->push_data = dw_mci_push_data16;
  2370. host->pull_data = dw_mci_pull_data16;
  2371. width = 16;
  2372. host->data_shift = 1;
  2373. } else if (i == 2) {
  2374. host->push_data = dw_mci_push_data64;
  2375. host->pull_data = dw_mci_pull_data64;
  2376. width = 64;
  2377. host->data_shift = 3;
  2378. } else {
  2379. /* Check for a reserved value, and warn if it is */
  2380. WARN((i != 1),
  2381. "HCON reports a reserved host data width!\n"
  2382. "Defaulting to 32-bit access.\n");
  2383. host->push_data = dw_mci_push_data32;
  2384. host->pull_data = dw_mci_pull_data32;
  2385. width = 32;
  2386. host->data_shift = 2;
  2387. }
  2388. /* Reset all blocks */
  2389. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
  2390. return -ENODEV;
  2391. host->dma_ops = host->pdata->dma_ops;
  2392. dw_mci_init_dma(host);
  2393. /* Clear the interrupts for the host controller */
  2394. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2395. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2396. /* Put in max timeout */
  2397. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2398. /*
  2399. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  2400. * Tx Mark = fifo_size / 2 DMA Size = 8
  2401. */
  2402. if (!host->pdata->fifo_depth) {
  2403. /*
  2404. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  2405. * have been overwritten by the bootloader, just like we're
  2406. * about to do, so if you know the value for your hardware, you
  2407. * should put it in the platform data.
  2408. */
  2409. fifo_size = mci_readl(host, FIFOTH);
  2410. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  2411. } else {
  2412. fifo_size = host->pdata->fifo_depth;
  2413. }
  2414. host->fifo_depth = fifo_size;
  2415. host->fifoth_val =
  2416. SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
  2417. mci_writel(host, FIFOTH, host->fifoth_val);
  2418. /* disable clock to CIU */
  2419. mci_writel(host, CLKENA, 0);
  2420. mci_writel(host, CLKSRC, 0);
  2421. /*
  2422. * In 2.40a spec, Data offset is changed.
  2423. * Need to check the version-id and set data-offset for DATA register.
  2424. */
  2425. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  2426. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  2427. if (host->verid < DW_MMC_240A)
  2428. host->fifo_reg = host->regs + DATA_OFFSET;
  2429. else
  2430. host->fifo_reg = host->regs + DATA_240A_OFFSET;
  2431. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  2432. ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
  2433. host->irq_flags, "dw-mci", host);
  2434. if (ret)
  2435. goto err_dmaunmap;
  2436. if (host->pdata->num_slots)
  2437. host->num_slots = host->pdata->num_slots;
  2438. else
  2439. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  2440. /*
  2441. * Enable interrupts for command done, data over, data empty,
  2442. * receive ready and error such as transmit, receive timeout, crc error
  2443. */
  2444. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2445. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2446. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2447. DW_MCI_ERROR_FLAGS);
  2448. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  2449. dev_info(host->dev, "DW MMC controller at irq %d, "
  2450. "%d bit host data width, "
  2451. "%u deep fifo\n",
  2452. host->irq, width, fifo_size);
  2453. /* We need at least one slot to succeed */
  2454. for (i = 0; i < host->num_slots; i++) {
  2455. ret = dw_mci_init_slot(host, i);
  2456. if (ret)
  2457. dev_dbg(host->dev, "slot %d init failed\n", i);
  2458. else
  2459. init_slots++;
  2460. }
  2461. if (init_slots) {
  2462. dev_info(host->dev, "%d slots initialized\n", init_slots);
  2463. } else {
  2464. dev_dbg(host->dev, "attempted to initialize %d slots, "
  2465. "but failed on all\n", host->num_slots);
  2466. goto err_dmaunmap;
  2467. }
  2468. /* Now that slots are all setup, we can enable card detect */
  2469. dw_mci_enable_cd(host);
  2470. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  2471. dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
  2472. return 0;
  2473. err_dmaunmap:
  2474. if (host->use_dma && host->dma_ops->exit)
  2475. host->dma_ops->exit(host);
  2476. err_clk_ciu:
  2477. if (!IS_ERR(host->ciu_clk))
  2478. clk_disable_unprepare(host->ciu_clk);
  2479. err_clk_biu:
  2480. if (!IS_ERR(host->biu_clk))
  2481. clk_disable_unprepare(host->biu_clk);
  2482. return ret;
  2483. }
  2484. EXPORT_SYMBOL(dw_mci_probe);
  2485. void dw_mci_remove(struct dw_mci *host)
  2486. {
  2487. int i;
  2488. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2489. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2490. for (i = 0; i < host->num_slots; i++) {
  2491. dev_dbg(host->dev, "remove slot %d\n", i);
  2492. if (host->slot[i])
  2493. dw_mci_cleanup_slot(host->slot[i], i);
  2494. }
  2495. /* disable clock to CIU */
  2496. mci_writel(host, CLKENA, 0);
  2497. mci_writel(host, CLKSRC, 0);
  2498. if (host->use_dma && host->dma_ops->exit)
  2499. host->dma_ops->exit(host);
  2500. if (!IS_ERR(host->ciu_clk))
  2501. clk_disable_unprepare(host->ciu_clk);
  2502. if (!IS_ERR(host->biu_clk))
  2503. clk_disable_unprepare(host->biu_clk);
  2504. }
  2505. EXPORT_SYMBOL(dw_mci_remove);
  2506. #ifdef CONFIG_PM_SLEEP
  2507. /*
  2508. * TODO: we should probably disable the clock to the card in the suspend path.
  2509. */
  2510. int dw_mci_suspend(struct dw_mci *host)
  2511. {
  2512. return 0;
  2513. }
  2514. EXPORT_SYMBOL(dw_mci_suspend);
  2515. int dw_mci_resume(struct dw_mci *host)
  2516. {
  2517. int i, ret;
  2518. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2519. ret = -ENODEV;
  2520. return ret;
  2521. }
  2522. if (host->use_dma && host->dma_ops->init)
  2523. host->dma_ops->init(host);
  2524. /*
  2525. * Restore the initial value at FIFOTH register
  2526. * And Invalidate the prev_blksz with zero
  2527. */
  2528. mci_writel(host, FIFOTH, host->fifoth_val);
  2529. host->prev_blksz = 0;
  2530. /* Put in max timeout */
  2531. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2532. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2533. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2534. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2535. DW_MCI_ERROR_FLAGS);
  2536. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2537. for (i = 0; i < host->num_slots; i++) {
  2538. struct dw_mci_slot *slot = host->slot[i];
  2539. if (!slot)
  2540. continue;
  2541. if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
  2542. dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
  2543. dw_mci_setup_bus(slot, true);
  2544. }
  2545. }
  2546. /* Now that slots are all setup, we can enable card detect */
  2547. dw_mci_enable_cd(host);
  2548. return 0;
  2549. }
  2550. EXPORT_SYMBOL(dw_mci_resume);
  2551. #endif /* CONFIG_PM_SLEEP */
  2552. static int __init dw_mci_init(void)
  2553. {
  2554. pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
  2555. return 0;
  2556. }
  2557. static void __exit dw_mci_exit(void)
  2558. {
  2559. }
  2560. module_init(dw_mci_init);
  2561. module_exit(dw_mci_exit);
  2562. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  2563. MODULE_AUTHOR("NXP Semiconductor VietNam");
  2564. MODULE_AUTHOR("Imagination Technologies Ltd");
  2565. MODULE_LICENSE("GPL v2");