dw_mmc.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synopsys DesignWare Multimedia Card Interface driver
  4. * (Based on NXP driver for lpc 31xx)
  5. *
  6. * Copyright (C) 2009 NXP Semiconductors
  7. * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
  8. */
  9. #include <linux/blkdev.h>
  10. #include <linux/clk.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/ioport.h>
  19. #include <linux/ktime.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/prandom.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/bitops.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/of.h>
  37. #include <linux/of_gpio.h>
  38. #include <linux/mmc/slot-gpio.h>
  39. #include "dw_mmc.h"
  40. /* Common flag combinations */
  41. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
  42. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  43. SDMMC_INT_EBE | SDMMC_INT_HLE)
  44. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  45. SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
  46. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  47. DW_MCI_CMD_ERROR_FLAGS)
  48. #define DW_MCI_SEND_STATUS 1
  49. #define DW_MCI_RECV_STATUS 2
  50. #define DW_MCI_DMA_THRESHOLD 16
  51. #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
  52. #define DW_MCI_FREQ_MIN 100000 /* unit: HZ */
  53. #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
  54. SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
  55. SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
  56. SDMMC_IDMAC_INT_TI)
  57. #define DESC_RING_BUF_SZ PAGE_SIZE
  58. struct idmac_desc_64addr {
  59. u32 des0; /* Control Descriptor */
  60. #define IDMAC_OWN_CLR64(x) \
  61. !((x) & cpu_to_le32(IDMAC_DES0_OWN))
  62. u32 des1; /* Reserved */
  63. u32 des2; /*Buffer sizes */
  64. #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
  65. ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
  66. ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
  67. u32 des3; /* Reserved */
  68. u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
  69. u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
  70. u32 des6; /* Lower 32-bits of Next Descriptor Address */
  71. u32 des7; /* Upper 32-bits of Next Descriptor Address */
  72. };
  73. struct idmac_desc {
  74. __le32 des0; /* Control Descriptor */
  75. #define IDMAC_DES0_DIC BIT(1)
  76. #define IDMAC_DES0_LD BIT(2)
  77. #define IDMAC_DES0_FD BIT(3)
  78. #define IDMAC_DES0_CH BIT(4)
  79. #define IDMAC_DES0_ER BIT(5)
  80. #define IDMAC_DES0_CES BIT(30)
  81. #define IDMAC_DES0_OWN BIT(31)
  82. __le32 des1; /* Buffer sizes */
  83. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  84. ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
  85. __le32 des2; /* buffer 1 physical address */
  86. __le32 des3; /* buffer 2 physical address */
  87. };
  88. /* Each descriptor can transfer up to 4KB of data in chained mode */
  89. #define DW_MCI_DESC_DATA_LENGTH 0x1000
  90. #if defined(CONFIG_DEBUG_FS)
  91. static int dw_mci_req_show(struct seq_file *s, void *v)
  92. {
  93. struct dw_mci_slot *slot = s->private;
  94. struct mmc_request *mrq;
  95. struct mmc_command *cmd;
  96. struct mmc_command *stop;
  97. struct mmc_data *data;
  98. /* Make sure we get a consistent snapshot */
  99. spin_lock_bh(&slot->host->lock);
  100. mrq = slot->mrq;
  101. if (mrq) {
  102. cmd = mrq->cmd;
  103. data = mrq->data;
  104. stop = mrq->stop;
  105. if (cmd)
  106. seq_printf(s,
  107. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  108. cmd->opcode, cmd->arg, cmd->flags,
  109. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  110. cmd->resp[2], cmd->error);
  111. if (data)
  112. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  113. data->bytes_xfered, data->blocks,
  114. data->blksz, data->flags, data->error);
  115. if (stop)
  116. seq_printf(s,
  117. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  118. stop->opcode, stop->arg, stop->flags,
  119. stop->resp[0], stop->resp[1], stop->resp[2],
  120. stop->resp[2], stop->error);
  121. }
  122. spin_unlock_bh(&slot->host->lock);
  123. return 0;
  124. }
  125. DEFINE_SHOW_ATTRIBUTE(dw_mci_req);
  126. static int dw_mci_regs_show(struct seq_file *s, void *v)
  127. {
  128. struct dw_mci *host = s->private;
  129. pm_runtime_get_sync(host->dev);
  130. seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
  131. seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
  132. seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
  133. seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
  134. seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
  135. seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
  136. pm_runtime_put_autosuspend(host->dev);
  137. return 0;
  138. }
  139. DEFINE_SHOW_ATTRIBUTE(dw_mci_regs);
  140. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  141. {
  142. struct mmc_host *mmc = slot->mmc;
  143. struct dw_mci *host = slot->host;
  144. struct dentry *root;
  145. root = mmc->debugfs_root;
  146. if (!root)
  147. return;
  148. debugfs_create_file("regs", S_IRUSR, root, host, &dw_mci_regs_fops);
  149. debugfs_create_file("req", S_IRUSR, root, slot, &dw_mci_req_fops);
  150. debugfs_create_u32("state", S_IRUSR, root, &host->state);
  151. debugfs_create_xul("pending_events", S_IRUSR, root,
  152. &host->pending_events);
  153. debugfs_create_xul("completed_events", S_IRUSR, root,
  154. &host->completed_events);
  155. #ifdef CONFIG_FAULT_INJECTION
  156. fault_create_debugfs_attr("fail_data_crc", root, &host->fail_data_crc);
  157. #endif
  158. }
  159. #endif /* defined(CONFIG_DEBUG_FS) */
  160. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
  161. {
  162. u32 ctrl;
  163. ctrl = mci_readl(host, CTRL);
  164. ctrl |= reset;
  165. mci_writel(host, CTRL, ctrl);
  166. /* wait till resets clear */
  167. if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
  168. !(ctrl & reset),
  169. 1, 500 * USEC_PER_MSEC)) {
  170. dev_err(host->dev,
  171. "Timeout resetting block (ctrl reset %#x)\n",
  172. ctrl & reset);
  173. return false;
  174. }
  175. return true;
  176. }
  177. static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
  178. {
  179. u32 status;
  180. /*
  181. * Databook says that before issuing a new data transfer command
  182. * we need to check to see if the card is busy. Data transfer commands
  183. * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
  184. *
  185. * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
  186. * expected.
  187. */
  188. if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
  189. !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
  190. if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
  191. status,
  192. !(status & SDMMC_STATUS_BUSY),
  193. 10, 500 * USEC_PER_MSEC))
  194. dev_err(host->dev, "Busy; trying anyway\n");
  195. }
  196. }
  197. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  198. {
  199. struct dw_mci *host = slot->host;
  200. unsigned int cmd_status = 0;
  201. mci_writel(host, CMDARG, arg);
  202. wmb(); /* drain writebuffer */
  203. dw_mci_wait_while_busy(host, cmd);
  204. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  205. if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
  206. !(cmd_status & SDMMC_CMD_START),
  207. 1, 500 * USEC_PER_MSEC))
  208. dev_err(&slot->mmc->class_dev,
  209. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  210. cmd, arg, cmd_status);
  211. }
  212. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  213. {
  214. struct dw_mci_slot *slot = mmc_priv(mmc);
  215. struct dw_mci *host = slot->host;
  216. u32 cmdr;
  217. cmd->error = -EINPROGRESS;
  218. cmdr = cmd->opcode;
  219. if (cmd->opcode == MMC_STOP_TRANSMISSION ||
  220. cmd->opcode == MMC_GO_IDLE_STATE ||
  221. cmd->opcode == MMC_GO_INACTIVE_STATE ||
  222. (cmd->opcode == SD_IO_RW_DIRECT &&
  223. ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
  224. cmdr |= SDMMC_CMD_STOP;
  225. else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
  226. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  227. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  228. u32 clk_en_a;
  229. /* Special bit makes CMD11 not die */
  230. cmdr |= SDMMC_CMD_VOLT_SWITCH;
  231. /* Change state to continue to handle CMD11 weirdness */
  232. WARN_ON(slot->host->state != STATE_SENDING_CMD);
  233. slot->host->state = STATE_SENDING_CMD11;
  234. /*
  235. * We need to disable low power mode (automatic clock stop)
  236. * while doing voltage switch so we don't confuse the card,
  237. * since stopping the clock is a specific part of the UHS
  238. * voltage change dance.
  239. *
  240. * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
  241. * unconditionally turned back on in dw_mci_setup_bus() if it's
  242. * ever called with a non-zero clock. That shouldn't happen
  243. * until the voltage change is all done.
  244. */
  245. clk_en_a = mci_readl(host, CLKENA);
  246. clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
  247. mci_writel(host, CLKENA, clk_en_a);
  248. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  249. SDMMC_CMD_PRV_DAT_WAIT, 0);
  250. }
  251. if (cmd->flags & MMC_RSP_PRESENT) {
  252. /* We expect a response, so set this bit */
  253. cmdr |= SDMMC_CMD_RESP_EXP;
  254. if (cmd->flags & MMC_RSP_136)
  255. cmdr |= SDMMC_CMD_RESP_LONG;
  256. }
  257. if (cmd->flags & MMC_RSP_CRC)
  258. cmdr |= SDMMC_CMD_RESP_CRC;
  259. if (cmd->data) {
  260. cmdr |= SDMMC_CMD_DAT_EXP;
  261. if (cmd->data->flags & MMC_DATA_WRITE)
  262. cmdr |= SDMMC_CMD_DAT_WR;
  263. }
  264. if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
  265. cmdr |= SDMMC_CMD_USE_HOLD_REG;
  266. return cmdr;
  267. }
  268. static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
  269. {
  270. struct mmc_command *stop;
  271. u32 cmdr;
  272. if (!cmd->data)
  273. return 0;
  274. stop = &host->stop_abort;
  275. cmdr = cmd->opcode;
  276. memset(stop, 0, sizeof(struct mmc_command));
  277. if (cmdr == MMC_READ_SINGLE_BLOCK ||
  278. cmdr == MMC_READ_MULTIPLE_BLOCK ||
  279. cmdr == MMC_WRITE_BLOCK ||
  280. cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
  281. cmdr == MMC_SEND_TUNING_BLOCK ||
  282. cmdr == MMC_SEND_TUNING_BLOCK_HS200 ||
  283. cmdr == MMC_GEN_CMD) {
  284. stop->opcode = MMC_STOP_TRANSMISSION;
  285. stop->arg = 0;
  286. stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  287. } else if (cmdr == SD_IO_RW_EXTENDED) {
  288. stop->opcode = SD_IO_RW_DIRECT;
  289. stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
  290. ((cmd->arg >> 28) & 0x7);
  291. stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  292. } else {
  293. return 0;
  294. }
  295. cmdr = stop->opcode | SDMMC_CMD_STOP |
  296. SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
  297. if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags))
  298. cmdr |= SDMMC_CMD_USE_HOLD_REG;
  299. return cmdr;
  300. }
  301. static inline void dw_mci_set_cto(struct dw_mci *host)
  302. {
  303. unsigned int cto_clks;
  304. unsigned int cto_div;
  305. unsigned int cto_ms;
  306. unsigned long irqflags;
  307. cto_clks = mci_readl(host, TMOUT) & 0xff;
  308. cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
  309. if (cto_div == 0)
  310. cto_div = 1;
  311. cto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * cto_clks * cto_div,
  312. host->bus_hz);
  313. /* add a bit spare time */
  314. cto_ms += 10;
  315. /*
  316. * The durations we're working with are fairly short so we have to be
  317. * extra careful about synchronization here. Specifically in hardware a
  318. * command timeout is _at most_ 5.1 ms, so that means we expect an
  319. * interrupt (either command done or timeout) to come rather quickly
  320. * after the mci_writel. ...but just in case we have a long interrupt
  321. * latency let's add a bit of paranoia.
  322. *
  323. * In general we'll assume that at least an interrupt will be asserted
  324. * in hardware by the time the cto_timer runs. ...and if it hasn't
  325. * been asserted in hardware by that time then we'll assume it'll never
  326. * come.
  327. */
  328. spin_lock_irqsave(&host->irq_lock, irqflags);
  329. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  330. mod_timer(&host->cto_timer,
  331. jiffies + msecs_to_jiffies(cto_ms) + 1);
  332. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  333. }
  334. static void dw_mci_start_command(struct dw_mci *host,
  335. struct mmc_command *cmd, u32 cmd_flags)
  336. {
  337. host->cmd = cmd;
  338. dev_vdbg(host->dev,
  339. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  340. cmd->arg, cmd_flags);
  341. mci_writel(host, CMDARG, cmd->arg);
  342. wmb(); /* drain writebuffer */
  343. dw_mci_wait_while_busy(host, cmd_flags);
  344. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  345. /* response expected command only */
  346. if (cmd_flags & SDMMC_CMD_RESP_EXP)
  347. dw_mci_set_cto(host);
  348. }
  349. static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
  350. {
  351. struct mmc_command *stop = &host->stop_abort;
  352. dw_mci_start_command(host, stop, host->stop_cmdr);
  353. }
  354. /* DMA interface functions */
  355. static void dw_mci_stop_dma(struct dw_mci *host)
  356. {
  357. if (host->using_dma) {
  358. host->dma_ops->stop(host);
  359. host->dma_ops->cleanup(host);
  360. }
  361. /* Data transfer was stopped by the interrupt handler */
  362. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  363. }
  364. static void dw_mci_dma_cleanup(struct dw_mci *host)
  365. {
  366. struct mmc_data *data = host->data;
  367. if (data && data->host_cookie == COOKIE_MAPPED) {
  368. dma_unmap_sg(host->dev,
  369. data->sg,
  370. data->sg_len,
  371. mmc_get_dma_dir(data));
  372. data->host_cookie = COOKIE_UNMAPPED;
  373. }
  374. }
  375. static void dw_mci_idmac_reset(struct dw_mci *host)
  376. {
  377. u32 bmod = mci_readl(host, BMOD);
  378. /* Software reset of DMA */
  379. bmod |= SDMMC_IDMAC_SWRESET;
  380. mci_writel(host, BMOD, bmod);
  381. }
  382. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  383. {
  384. u32 temp;
  385. /* Disable and reset the IDMAC interface */
  386. temp = mci_readl(host, CTRL);
  387. temp &= ~SDMMC_CTRL_USE_IDMAC;
  388. temp |= SDMMC_CTRL_DMA_RESET;
  389. mci_writel(host, CTRL, temp);
  390. /* Stop the IDMAC running */
  391. temp = mci_readl(host, BMOD);
  392. temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
  393. temp |= SDMMC_IDMAC_SWRESET;
  394. mci_writel(host, BMOD, temp);
  395. }
  396. static void dw_mci_dmac_complete_dma(void *arg)
  397. {
  398. struct dw_mci *host = arg;
  399. struct mmc_data *data = host->data;
  400. dev_vdbg(host->dev, "DMA complete\n");
  401. if ((host->use_dma == TRANS_MODE_EDMAC) &&
  402. data && (data->flags & MMC_DATA_READ))
  403. /* Invalidate cache after read */
  404. dma_sync_sg_for_cpu(mmc_dev(host->slot->mmc),
  405. data->sg,
  406. data->sg_len,
  407. DMA_FROM_DEVICE);
  408. host->dma_ops->cleanup(host);
  409. /*
  410. * If the card was removed, data will be NULL. No point in trying to
  411. * send the stop command or waiting for NBUSY in this case.
  412. */
  413. if (data) {
  414. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  415. tasklet_schedule(&host->tasklet);
  416. }
  417. }
  418. static int dw_mci_idmac_init(struct dw_mci *host)
  419. {
  420. int i;
  421. if (host->dma_64bit_address == 1) {
  422. struct idmac_desc_64addr *p;
  423. /* Number of descriptors in the ring buffer */
  424. host->ring_size =
  425. DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
  426. /* Forward link the descriptor list */
  427. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
  428. i++, p++) {
  429. p->des6 = (host->sg_dma +
  430. (sizeof(struct idmac_desc_64addr) *
  431. (i + 1))) & 0xffffffff;
  432. p->des7 = (u64)(host->sg_dma +
  433. (sizeof(struct idmac_desc_64addr) *
  434. (i + 1))) >> 32;
  435. /* Initialize reserved and buffer size fields to "0" */
  436. p->des0 = 0;
  437. p->des1 = 0;
  438. p->des2 = 0;
  439. p->des3 = 0;
  440. }
  441. /* Set the last descriptor as the end-of-ring descriptor */
  442. p->des6 = host->sg_dma & 0xffffffff;
  443. p->des7 = (u64)host->sg_dma >> 32;
  444. p->des0 = IDMAC_DES0_ER;
  445. } else {
  446. struct idmac_desc *p;
  447. /* Number of descriptors in the ring buffer */
  448. host->ring_size =
  449. DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
  450. /* Forward link the descriptor list */
  451. for (i = 0, p = host->sg_cpu;
  452. i < host->ring_size - 1;
  453. i++, p++) {
  454. p->des3 = cpu_to_le32(host->sg_dma +
  455. (sizeof(struct idmac_desc) * (i + 1)));
  456. p->des0 = 0;
  457. p->des1 = 0;
  458. }
  459. /* Set the last descriptor as the end-of-ring descriptor */
  460. p->des3 = cpu_to_le32(host->sg_dma);
  461. p->des0 = cpu_to_le32(IDMAC_DES0_ER);
  462. }
  463. dw_mci_idmac_reset(host);
  464. if (host->dma_64bit_address == 1) {
  465. /* Mask out interrupts - get Tx & Rx complete only */
  466. mci_writel(host, IDSTS64, IDMAC_INT_CLR);
  467. mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
  468. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  469. /* Set the descriptor base address */
  470. mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
  471. mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
  472. } else {
  473. /* Mask out interrupts - get Tx & Rx complete only */
  474. mci_writel(host, IDSTS, IDMAC_INT_CLR);
  475. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
  476. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  477. /* Set the descriptor base address */
  478. mci_writel(host, DBADDR, host->sg_dma);
  479. }
  480. return 0;
  481. }
  482. static inline int dw_mci_prepare_desc64(struct dw_mci *host,
  483. struct mmc_data *data,
  484. unsigned int sg_len)
  485. {
  486. unsigned int desc_len;
  487. struct idmac_desc_64addr *desc_first, *desc_last, *desc;
  488. u32 val;
  489. int i;
  490. desc_first = desc_last = desc = host->sg_cpu;
  491. for (i = 0; i < sg_len; i++) {
  492. unsigned int length = sg_dma_len(&data->sg[i]);
  493. u64 mem_addr = sg_dma_address(&data->sg[i]);
  494. for ( ; length ; desc++) {
  495. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  496. length : DW_MCI_DESC_DATA_LENGTH;
  497. length -= desc_len;
  498. /*
  499. * Wait for the former clear OWN bit operation
  500. * of IDMAC to make sure that this descriptor
  501. * isn't still owned by IDMAC as IDMAC's write
  502. * ops and CPU's read ops are asynchronous.
  503. */
  504. if (readl_poll_timeout_atomic(&desc->des0, val,
  505. !(val & IDMAC_DES0_OWN),
  506. 10, 100 * USEC_PER_MSEC))
  507. goto err_own_bit;
  508. /*
  509. * Set the OWN bit and disable interrupts
  510. * for this descriptor
  511. */
  512. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
  513. IDMAC_DES0_CH;
  514. /* Buffer length */
  515. IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
  516. /* Physical address to DMA to/from */
  517. desc->des4 = mem_addr & 0xffffffff;
  518. desc->des5 = mem_addr >> 32;
  519. /* Update physical address for the next desc */
  520. mem_addr += desc_len;
  521. /* Save pointer to the last descriptor */
  522. desc_last = desc;
  523. }
  524. }
  525. /* Set first descriptor */
  526. desc_first->des0 |= IDMAC_DES0_FD;
  527. /* Set last descriptor */
  528. desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  529. desc_last->des0 |= IDMAC_DES0_LD;
  530. return 0;
  531. err_own_bit:
  532. /* restore the descriptor chain as it's polluted */
  533. dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
  534. memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
  535. dw_mci_idmac_init(host);
  536. return -EINVAL;
  537. }
  538. static inline int dw_mci_prepare_desc32(struct dw_mci *host,
  539. struct mmc_data *data,
  540. unsigned int sg_len)
  541. {
  542. unsigned int desc_len;
  543. struct idmac_desc *desc_first, *desc_last, *desc;
  544. u32 val;
  545. int i;
  546. desc_first = desc_last = desc = host->sg_cpu;
  547. for (i = 0; i < sg_len; i++) {
  548. unsigned int length = sg_dma_len(&data->sg[i]);
  549. u32 mem_addr = sg_dma_address(&data->sg[i]);
  550. for ( ; length ; desc++) {
  551. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  552. length : DW_MCI_DESC_DATA_LENGTH;
  553. length -= desc_len;
  554. /*
  555. * Wait for the former clear OWN bit operation
  556. * of IDMAC to make sure that this descriptor
  557. * isn't still owned by IDMAC as IDMAC's write
  558. * ops and CPU's read ops are asynchronous.
  559. */
  560. if (readl_poll_timeout_atomic(&desc->des0, val,
  561. IDMAC_OWN_CLR64(val),
  562. 10,
  563. 100 * USEC_PER_MSEC))
  564. goto err_own_bit;
  565. /*
  566. * Set the OWN bit and disable interrupts
  567. * for this descriptor
  568. */
  569. desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
  570. IDMAC_DES0_DIC |
  571. IDMAC_DES0_CH);
  572. /* Buffer length */
  573. IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
  574. /* Physical address to DMA to/from */
  575. desc->des2 = cpu_to_le32(mem_addr);
  576. /* Update physical address for the next desc */
  577. mem_addr += desc_len;
  578. /* Save pointer to the last descriptor */
  579. desc_last = desc;
  580. }
  581. }
  582. /* Set first descriptor */
  583. desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
  584. /* Set last descriptor */
  585. desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
  586. IDMAC_DES0_DIC));
  587. desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
  588. return 0;
  589. err_own_bit:
  590. /* restore the descriptor chain as it's polluted */
  591. dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
  592. memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
  593. dw_mci_idmac_init(host);
  594. return -EINVAL;
  595. }
  596. static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  597. {
  598. u32 temp;
  599. int ret;
  600. if (host->dma_64bit_address == 1)
  601. ret = dw_mci_prepare_desc64(host, host->data, sg_len);
  602. else
  603. ret = dw_mci_prepare_desc32(host, host->data, sg_len);
  604. if (ret)
  605. goto out;
  606. /* drain writebuffer */
  607. wmb();
  608. /* Make sure to reset DMA in case we did PIO before this */
  609. dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
  610. dw_mci_idmac_reset(host);
  611. /* Select IDMAC interface */
  612. temp = mci_readl(host, CTRL);
  613. temp |= SDMMC_CTRL_USE_IDMAC;
  614. mci_writel(host, CTRL, temp);
  615. /* drain writebuffer */
  616. wmb();
  617. /* Enable the IDMAC */
  618. temp = mci_readl(host, BMOD);
  619. temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
  620. mci_writel(host, BMOD, temp);
  621. /* Start it running */
  622. mci_writel(host, PLDMND, 1);
  623. out:
  624. return ret;
  625. }
  626. static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
  627. .init = dw_mci_idmac_init,
  628. .start = dw_mci_idmac_start_dma,
  629. .stop = dw_mci_idmac_stop_dma,
  630. .complete = dw_mci_dmac_complete_dma,
  631. .cleanup = dw_mci_dma_cleanup,
  632. };
  633. static void dw_mci_edmac_stop_dma(struct dw_mci *host)
  634. {
  635. dmaengine_terminate_async(host->dms->ch);
  636. }
  637. static int dw_mci_edmac_start_dma(struct dw_mci *host,
  638. unsigned int sg_len)
  639. {
  640. struct dma_slave_config cfg;
  641. struct dma_async_tx_descriptor *desc = NULL;
  642. struct scatterlist *sgl = host->data->sg;
  643. static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  644. u32 sg_elems = host->data->sg_len;
  645. u32 fifoth_val;
  646. u32 fifo_offset = host->fifo_reg - host->regs;
  647. int ret = 0;
  648. /* Set external dma config: burst size, burst width */
  649. memset(&cfg, 0, sizeof(cfg));
  650. cfg.dst_addr = host->phy_regs + fifo_offset;
  651. cfg.src_addr = cfg.dst_addr;
  652. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  653. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  654. /* Match burst msize with external dma config */
  655. fifoth_val = mci_readl(host, FIFOTH);
  656. cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
  657. cfg.src_maxburst = cfg.dst_maxburst;
  658. if (host->data->flags & MMC_DATA_WRITE)
  659. cfg.direction = DMA_MEM_TO_DEV;
  660. else
  661. cfg.direction = DMA_DEV_TO_MEM;
  662. ret = dmaengine_slave_config(host->dms->ch, &cfg);
  663. if (ret) {
  664. dev_err(host->dev, "Failed to config edmac.\n");
  665. return -EBUSY;
  666. }
  667. desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
  668. sg_len, cfg.direction,
  669. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  670. if (!desc) {
  671. dev_err(host->dev, "Can't prepare slave sg.\n");
  672. return -EBUSY;
  673. }
  674. /* Set dw_mci_dmac_complete_dma as callback */
  675. desc->callback = dw_mci_dmac_complete_dma;
  676. desc->callback_param = (void *)host;
  677. dmaengine_submit(desc);
  678. /* Flush cache before write */
  679. if (host->data->flags & MMC_DATA_WRITE)
  680. dma_sync_sg_for_device(mmc_dev(host->slot->mmc), sgl,
  681. sg_elems, DMA_TO_DEVICE);
  682. dma_async_issue_pending(host->dms->ch);
  683. return 0;
  684. }
  685. static int dw_mci_edmac_init(struct dw_mci *host)
  686. {
  687. /* Request external dma channel */
  688. host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
  689. if (!host->dms)
  690. return -ENOMEM;
  691. host->dms->ch = dma_request_chan(host->dev, "rx-tx");
  692. if (IS_ERR(host->dms->ch)) {
  693. int ret = PTR_ERR(host->dms->ch);
  694. dev_err(host->dev, "Failed to get external DMA channel.\n");
  695. kfree(host->dms);
  696. host->dms = NULL;
  697. return ret;
  698. }
  699. return 0;
  700. }
  701. static void dw_mci_edmac_exit(struct dw_mci *host)
  702. {
  703. if (host->dms) {
  704. if (host->dms->ch) {
  705. dma_release_channel(host->dms->ch);
  706. host->dms->ch = NULL;
  707. }
  708. kfree(host->dms);
  709. host->dms = NULL;
  710. }
  711. }
  712. static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
  713. .init = dw_mci_edmac_init,
  714. .exit = dw_mci_edmac_exit,
  715. .start = dw_mci_edmac_start_dma,
  716. .stop = dw_mci_edmac_stop_dma,
  717. .complete = dw_mci_dmac_complete_dma,
  718. .cleanup = dw_mci_dma_cleanup,
  719. };
  720. static int dw_mci_pre_dma_transfer(struct dw_mci *host,
  721. struct mmc_data *data,
  722. int cookie)
  723. {
  724. struct scatterlist *sg;
  725. unsigned int i, sg_len;
  726. if (data->host_cookie == COOKIE_PRE_MAPPED)
  727. return data->sg_len;
  728. /*
  729. * We don't do DMA on "complex" transfers, i.e. with
  730. * non-word-aligned buffers or lengths. Also, we don't bother
  731. * with all the DMA setup overhead for short transfers.
  732. */
  733. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  734. return -EINVAL;
  735. if (data->blksz & 3)
  736. return -EINVAL;
  737. for_each_sg(data->sg, sg, data->sg_len, i) {
  738. if (sg->offset & 3 || sg->length & 3)
  739. return -EINVAL;
  740. }
  741. sg_len = dma_map_sg(host->dev,
  742. data->sg,
  743. data->sg_len,
  744. mmc_get_dma_dir(data));
  745. if (sg_len == 0)
  746. return -EINVAL;
  747. data->host_cookie = cookie;
  748. return sg_len;
  749. }
  750. static void dw_mci_pre_req(struct mmc_host *mmc,
  751. struct mmc_request *mrq)
  752. {
  753. struct dw_mci_slot *slot = mmc_priv(mmc);
  754. struct mmc_data *data = mrq->data;
  755. if (!slot->host->use_dma || !data)
  756. return;
  757. /* This data might be unmapped at this time */
  758. data->host_cookie = COOKIE_UNMAPPED;
  759. if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
  760. COOKIE_PRE_MAPPED) < 0)
  761. data->host_cookie = COOKIE_UNMAPPED;
  762. }
  763. static void dw_mci_post_req(struct mmc_host *mmc,
  764. struct mmc_request *mrq,
  765. int err)
  766. {
  767. struct dw_mci_slot *slot = mmc_priv(mmc);
  768. struct mmc_data *data = mrq->data;
  769. if (!slot->host->use_dma || !data)
  770. return;
  771. if (data->host_cookie != COOKIE_UNMAPPED)
  772. dma_unmap_sg(slot->host->dev,
  773. data->sg,
  774. data->sg_len,
  775. mmc_get_dma_dir(data));
  776. data->host_cookie = COOKIE_UNMAPPED;
  777. }
  778. static int dw_mci_get_cd(struct mmc_host *mmc)
  779. {
  780. int present;
  781. struct dw_mci_slot *slot = mmc_priv(mmc);
  782. struct dw_mci *host = slot->host;
  783. int gpio_cd = mmc_gpio_get_cd(mmc);
  784. /* Use platform get_cd function, else try onboard card detect */
  785. if (((mmc->caps & MMC_CAP_NEEDS_POLL)
  786. || !mmc_card_is_removable(mmc))) {
  787. present = 1;
  788. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  789. if (mmc->caps & MMC_CAP_NEEDS_POLL) {
  790. dev_info(&mmc->class_dev,
  791. "card is polling.\n");
  792. } else {
  793. dev_info(&mmc->class_dev,
  794. "card is non-removable.\n");
  795. }
  796. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  797. }
  798. return present;
  799. } else if (gpio_cd >= 0)
  800. present = gpio_cd;
  801. else
  802. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  803. == 0 ? 1 : 0;
  804. spin_lock_bh(&host->lock);
  805. if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
  806. dev_dbg(&mmc->class_dev, "card is present\n");
  807. else if (!present &&
  808. !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
  809. dev_dbg(&mmc->class_dev, "card is not present\n");
  810. spin_unlock_bh(&host->lock);
  811. return present;
  812. }
  813. static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
  814. {
  815. unsigned int blksz = data->blksz;
  816. static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  817. u32 fifo_width = 1 << host->data_shift;
  818. u32 blksz_depth = blksz / fifo_width, fifoth_val;
  819. u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
  820. int idx = ARRAY_SIZE(mszs) - 1;
  821. /* pio should ship this scenario */
  822. if (!host->use_dma)
  823. return;
  824. tx_wmark = (host->fifo_depth) / 2;
  825. tx_wmark_invers = host->fifo_depth - tx_wmark;
  826. /*
  827. * MSIZE is '1',
  828. * if blksz is not a multiple of the FIFO width
  829. */
  830. if (blksz % fifo_width)
  831. goto done;
  832. do {
  833. if (!((blksz_depth % mszs[idx]) ||
  834. (tx_wmark_invers % mszs[idx]))) {
  835. msize = idx;
  836. rx_wmark = mszs[idx] - 1;
  837. break;
  838. }
  839. } while (--idx > 0);
  840. /*
  841. * If idx is '0', it won't be tried
  842. * Thus, initial values are uesed
  843. */
  844. done:
  845. fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
  846. mci_writel(host, FIFOTH, fifoth_val);
  847. }
  848. static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
  849. {
  850. unsigned int blksz = data->blksz;
  851. u32 blksz_depth, fifo_depth;
  852. u16 thld_size;
  853. u8 enable;
  854. /*
  855. * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
  856. * in the FIFO region, so we really shouldn't access it).
  857. */
  858. if (host->verid < DW_MMC_240A ||
  859. (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
  860. return;
  861. /*
  862. * Card write Threshold is introduced since 2.80a
  863. * It's used when HS400 mode is enabled.
  864. */
  865. if (data->flags & MMC_DATA_WRITE &&
  866. host->timing != MMC_TIMING_MMC_HS400)
  867. goto disable;
  868. if (data->flags & MMC_DATA_WRITE)
  869. enable = SDMMC_CARD_WR_THR_EN;
  870. else
  871. enable = SDMMC_CARD_RD_THR_EN;
  872. if (host->timing != MMC_TIMING_MMC_HS200 &&
  873. host->timing != MMC_TIMING_UHS_SDR104 &&
  874. host->timing != MMC_TIMING_MMC_HS400)
  875. goto disable;
  876. blksz_depth = blksz / (1 << host->data_shift);
  877. fifo_depth = host->fifo_depth;
  878. if (blksz_depth > fifo_depth)
  879. goto disable;
  880. /*
  881. * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
  882. * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
  883. * Currently just choose blksz.
  884. */
  885. thld_size = blksz;
  886. mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
  887. return;
  888. disable:
  889. mci_writel(host, CDTHRCTL, 0);
  890. }
  891. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  892. {
  893. unsigned long irqflags;
  894. int sg_len;
  895. u32 temp;
  896. host->using_dma = 0;
  897. /* If we don't have a channel, we can't do DMA */
  898. if (!host->use_dma)
  899. return -ENODEV;
  900. sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
  901. if (sg_len < 0) {
  902. host->dma_ops->stop(host);
  903. return sg_len;
  904. }
  905. host->using_dma = 1;
  906. if (host->use_dma == TRANS_MODE_IDMAC)
  907. dev_vdbg(host->dev,
  908. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  909. (unsigned long)host->sg_cpu,
  910. (unsigned long)host->sg_dma,
  911. sg_len);
  912. /*
  913. * Decide the MSIZE and RX/TX Watermark.
  914. * If current block size is same with previous size,
  915. * no need to update fifoth.
  916. */
  917. if (host->prev_blksz != data->blksz)
  918. dw_mci_adjust_fifoth(host, data);
  919. /* Enable the DMA interface */
  920. temp = mci_readl(host, CTRL);
  921. temp |= SDMMC_CTRL_DMA_ENABLE;
  922. mci_writel(host, CTRL, temp);
  923. /* Disable RX/TX IRQs, let DMA handle it */
  924. spin_lock_irqsave(&host->irq_lock, irqflags);
  925. temp = mci_readl(host, INTMASK);
  926. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  927. mci_writel(host, INTMASK, temp);
  928. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  929. if (host->dma_ops->start(host, sg_len)) {
  930. host->dma_ops->stop(host);
  931. /* We can't do DMA, try PIO for this one */
  932. dev_dbg(host->dev,
  933. "%s: fall back to PIO mode for current transfer\n",
  934. __func__);
  935. return -ENODEV;
  936. }
  937. return 0;
  938. }
  939. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  940. {
  941. unsigned long irqflags;
  942. int flags = SG_MITER_ATOMIC;
  943. u32 temp;
  944. data->error = -EINPROGRESS;
  945. WARN_ON(host->data);
  946. host->sg = NULL;
  947. host->data = data;
  948. if (data->flags & MMC_DATA_READ)
  949. host->dir_status = DW_MCI_RECV_STATUS;
  950. else
  951. host->dir_status = DW_MCI_SEND_STATUS;
  952. dw_mci_ctrl_thld(host, data);
  953. if (dw_mci_submit_data_dma(host, data)) {
  954. if (host->data->flags & MMC_DATA_READ)
  955. flags |= SG_MITER_TO_SG;
  956. else
  957. flags |= SG_MITER_FROM_SG;
  958. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  959. host->sg = data->sg;
  960. host->part_buf_start = 0;
  961. host->part_buf_count = 0;
  962. mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
  963. spin_lock_irqsave(&host->irq_lock, irqflags);
  964. temp = mci_readl(host, INTMASK);
  965. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  966. mci_writel(host, INTMASK, temp);
  967. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  968. temp = mci_readl(host, CTRL);
  969. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  970. mci_writel(host, CTRL, temp);
  971. /*
  972. * Use the initial fifoth_val for PIO mode. If wm_algined
  973. * is set, we set watermark same as data size.
  974. * If next issued data may be transfered by DMA mode,
  975. * prev_blksz should be invalidated.
  976. */
  977. if (host->wm_aligned)
  978. dw_mci_adjust_fifoth(host, data);
  979. else
  980. mci_writel(host, FIFOTH, host->fifoth_val);
  981. host->prev_blksz = 0;
  982. } else {
  983. /*
  984. * Keep the current block size.
  985. * It will be used to decide whether to update
  986. * fifoth register next time.
  987. */
  988. host->prev_blksz = data->blksz;
  989. }
  990. }
  991. static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
  992. {
  993. struct dw_mci *host = slot->host;
  994. unsigned int clock = slot->clock;
  995. u32 div;
  996. u32 clk_en_a;
  997. u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
  998. /* We must continue to set bit 28 in CMD until the change is complete */
  999. if (host->state == STATE_WAITING_CMD11_DONE)
  1000. sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
  1001. slot->mmc->actual_clock = 0;
  1002. if (!clock) {
  1003. mci_writel(host, CLKENA, 0);
  1004. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1005. } else if (clock != host->current_speed || force_clkinit) {
  1006. div = host->bus_hz / clock;
  1007. if (host->bus_hz % clock && host->bus_hz > clock)
  1008. /*
  1009. * move the + 1 after the divide to prevent
  1010. * over-clocking the card.
  1011. */
  1012. div += 1;
  1013. div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
  1014. if ((clock != slot->__clk_old &&
  1015. !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
  1016. force_clkinit) {
  1017. /* Silent the verbose log if calling from PM context */
  1018. if (!force_clkinit)
  1019. dev_info(&slot->mmc->class_dev,
  1020. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
  1021. slot->id, host->bus_hz, clock,
  1022. div ? ((host->bus_hz / div) >> 1) :
  1023. host->bus_hz, div);
  1024. /*
  1025. * If card is polling, display the message only
  1026. * one time at boot time.
  1027. */
  1028. if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
  1029. slot->mmc->f_min == clock)
  1030. set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
  1031. }
  1032. /* disable clock */
  1033. mci_writel(host, CLKENA, 0);
  1034. mci_writel(host, CLKSRC, 0);
  1035. /* inform CIU */
  1036. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1037. /* set clock to desired speed */
  1038. mci_writel(host, CLKDIV, div);
  1039. /* inform CIU */
  1040. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1041. /* enable clock; only low power if no SDIO */
  1042. clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
  1043. if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
  1044. clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
  1045. mci_writel(host, CLKENA, clk_en_a);
  1046. /* inform CIU */
  1047. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1048. /* keep the last clock value that was requested from core */
  1049. slot->__clk_old = clock;
  1050. slot->mmc->actual_clock = div ? ((host->bus_hz / div) >> 1) :
  1051. host->bus_hz;
  1052. }
  1053. host->current_speed = clock;
  1054. /* Set the current slot bus width */
  1055. mci_writel(host, CTYPE, (slot->ctype << slot->id));
  1056. }
  1057. static void dw_mci_set_data_timeout(struct dw_mci *host,
  1058. unsigned int timeout_ns)
  1059. {
  1060. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1061. u32 clk_div, tmout;
  1062. u64 tmp;
  1063. if (drv_data && drv_data->set_data_timeout)
  1064. return drv_data->set_data_timeout(host, timeout_ns);
  1065. clk_div = (mci_readl(host, CLKDIV) & 0xFF) * 2;
  1066. if (clk_div == 0)
  1067. clk_div = 1;
  1068. tmp = DIV_ROUND_UP_ULL((u64)timeout_ns * host->bus_hz, NSEC_PER_SEC);
  1069. tmp = DIV_ROUND_UP_ULL(tmp, clk_div);
  1070. /* TMOUT[7:0] (RESPONSE_TIMEOUT) */
  1071. tmout = 0xFF; /* Set maximum */
  1072. /* TMOUT[31:8] (DATA_TIMEOUT) */
  1073. if (!tmp || tmp > 0xFFFFFF)
  1074. tmout |= (0xFFFFFF << 8);
  1075. else
  1076. tmout |= (tmp & 0xFFFFFF) << 8;
  1077. mci_writel(host, TMOUT, tmout);
  1078. dev_dbg(host->dev, "timeout_ns: %u => TMOUT[31:8]: %#08x",
  1079. timeout_ns, tmout >> 8);
  1080. }
  1081. static void __dw_mci_start_request(struct dw_mci *host,
  1082. struct dw_mci_slot *slot,
  1083. struct mmc_command *cmd)
  1084. {
  1085. struct mmc_request *mrq;
  1086. struct mmc_data *data;
  1087. u32 cmdflags;
  1088. mrq = slot->mrq;
  1089. host->mrq = mrq;
  1090. host->pending_events = 0;
  1091. host->completed_events = 0;
  1092. host->cmd_status = 0;
  1093. host->data_status = 0;
  1094. host->dir_status = 0;
  1095. data = cmd->data;
  1096. if (data) {
  1097. dw_mci_set_data_timeout(host, data->timeout_ns);
  1098. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  1099. mci_writel(host, BLKSIZ, data->blksz);
  1100. }
  1101. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  1102. /* this is the first command, send the initialization clock */
  1103. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  1104. cmdflags |= SDMMC_CMD_INIT;
  1105. if (data) {
  1106. dw_mci_submit_data(host, data);
  1107. wmb(); /* drain writebuffer */
  1108. }
  1109. dw_mci_start_command(host, cmd, cmdflags);
  1110. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  1111. unsigned long irqflags;
  1112. /*
  1113. * Databook says to fail after 2ms w/ no response, but evidence
  1114. * shows that sometimes the cmd11 interrupt takes over 130ms.
  1115. * We'll set to 500ms, plus an extra jiffy just in case jiffies
  1116. * is just about to roll over.
  1117. *
  1118. * We do this whole thing under spinlock and only if the
  1119. * command hasn't already completed (indicating the the irq
  1120. * already ran so we don't want the timeout).
  1121. */
  1122. spin_lock_irqsave(&host->irq_lock, irqflags);
  1123. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  1124. mod_timer(&host->cmd11_timer,
  1125. jiffies + msecs_to_jiffies(500) + 1);
  1126. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1127. }
  1128. host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
  1129. }
  1130. static void dw_mci_start_request(struct dw_mci *host,
  1131. struct dw_mci_slot *slot)
  1132. {
  1133. struct mmc_request *mrq = slot->mrq;
  1134. struct mmc_command *cmd;
  1135. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  1136. __dw_mci_start_request(host, slot, cmd);
  1137. }
  1138. /* must be called with host->lock held */
  1139. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  1140. struct mmc_request *mrq)
  1141. {
  1142. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  1143. host->state);
  1144. slot->mrq = mrq;
  1145. if (host->state == STATE_WAITING_CMD11_DONE) {
  1146. dev_warn(&slot->mmc->class_dev,
  1147. "Voltage change didn't complete\n");
  1148. /*
  1149. * this case isn't expected to happen, so we can
  1150. * either crash here or just try to continue on
  1151. * in the closest possible state
  1152. */
  1153. host->state = STATE_IDLE;
  1154. }
  1155. if (host->state == STATE_IDLE) {
  1156. host->state = STATE_SENDING_CMD;
  1157. dw_mci_start_request(host, slot);
  1158. } else {
  1159. list_add_tail(&slot->queue_node, &host->queue);
  1160. }
  1161. }
  1162. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1163. {
  1164. struct dw_mci_slot *slot = mmc_priv(mmc);
  1165. struct dw_mci *host = slot->host;
  1166. WARN_ON(slot->mrq);
  1167. /*
  1168. * The check for card presence and queueing of the request must be
  1169. * atomic, otherwise the card could be removed in between and the
  1170. * request wouldn't fail until another card was inserted.
  1171. */
  1172. if (!dw_mci_get_cd(mmc)) {
  1173. mrq->cmd->error = -ENOMEDIUM;
  1174. mmc_request_done(mmc, mrq);
  1175. return;
  1176. }
  1177. spin_lock_bh(&host->lock);
  1178. dw_mci_queue_request(host, slot, mrq);
  1179. spin_unlock_bh(&host->lock);
  1180. }
  1181. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1182. {
  1183. struct dw_mci_slot *slot = mmc_priv(mmc);
  1184. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  1185. u32 regs;
  1186. int ret;
  1187. switch (ios->bus_width) {
  1188. case MMC_BUS_WIDTH_4:
  1189. slot->ctype = SDMMC_CTYPE_4BIT;
  1190. break;
  1191. case MMC_BUS_WIDTH_8:
  1192. slot->ctype = SDMMC_CTYPE_8BIT;
  1193. break;
  1194. default:
  1195. /* set default 1 bit mode */
  1196. slot->ctype = SDMMC_CTYPE_1BIT;
  1197. }
  1198. regs = mci_readl(slot->host, UHS_REG);
  1199. /* DDR mode set */
  1200. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  1201. ios->timing == MMC_TIMING_UHS_DDR50 ||
  1202. ios->timing == MMC_TIMING_MMC_HS400)
  1203. regs |= ((0x1 << slot->id) << 16);
  1204. else
  1205. regs &= ~((0x1 << slot->id) << 16);
  1206. mci_writel(slot->host, UHS_REG, regs);
  1207. slot->host->timing = ios->timing;
  1208. /*
  1209. * Use mirror of ios->clock to prevent race with mmc
  1210. * core ios update when finding the minimum.
  1211. */
  1212. slot->clock = ios->clock;
  1213. if (drv_data && drv_data->set_ios)
  1214. drv_data->set_ios(slot->host, ios);
  1215. switch (ios->power_mode) {
  1216. case MMC_POWER_UP:
  1217. if (!IS_ERR(mmc->supply.vmmc)) {
  1218. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  1219. ios->vdd);
  1220. if (ret) {
  1221. dev_err(slot->host->dev,
  1222. "failed to enable vmmc regulator\n");
  1223. /*return, if failed turn on vmmc*/
  1224. return;
  1225. }
  1226. }
  1227. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  1228. regs = mci_readl(slot->host, PWREN);
  1229. regs |= (1 << slot->id);
  1230. mci_writel(slot->host, PWREN, regs);
  1231. break;
  1232. case MMC_POWER_ON:
  1233. if (!slot->host->vqmmc_enabled) {
  1234. if (!IS_ERR(mmc->supply.vqmmc)) {
  1235. ret = regulator_enable(mmc->supply.vqmmc);
  1236. if (ret < 0)
  1237. dev_err(slot->host->dev,
  1238. "failed to enable vqmmc\n");
  1239. else
  1240. slot->host->vqmmc_enabled = true;
  1241. } else {
  1242. /* Keep track so we don't reset again */
  1243. slot->host->vqmmc_enabled = true;
  1244. }
  1245. /* Reset our state machine after powering on */
  1246. dw_mci_ctrl_reset(slot->host,
  1247. SDMMC_CTRL_ALL_RESET_FLAGS);
  1248. }
  1249. /* Adjust clock / bus width after power is up */
  1250. dw_mci_setup_bus(slot, false);
  1251. break;
  1252. case MMC_POWER_OFF:
  1253. /* Turn clock off before power goes down */
  1254. dw_mci_setup_bus(slot, false);
  1255. if (!IS_ERR(mmc->supply.vmmc))
  1256. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1257. if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
  1258. regulator_disable(mmc->supply.vqmmc);
  1259. slot->host->vqmmc_enabled = false;
  1260. regs = mci_readl(slot->host, PWREN);
  1261. regs &= ~(1 << slot->id);
  1262. mci_writel(slot->host, PWREN, regs);
  1263. break;
  1264. default:
  1265. break;
  1266. }
  1267. if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
  1268. slot->host->state = STATE_IDLE;
  1269. }
  1270. static int dw_mci_card_busy(struct mmc_host *mmc)
  1271. {
  1272. struct dw_mci_slot *slot = mmc_priv(mmc);
  1273. u32 status;
  1274. /*
  1275. * Check the busy bit which is low when DAT[3:0]
  1276. * (the data lines) are 0000
  1277. */
  1278. status = mci_readl(slot->host, STATUS);
  1279. return !!(status & SDMMC_STATUS_BUSY);
  1280. }
  1281. static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  1282. {
  1283. struct dw_mci_slot *slot = mmc_priv(mmc);
  1284. struct dw_mci *host = slot->host;
  1285. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1286. u32 uhs;
  1287. u32 v18 = SDMMC_UHS_18V << slot->id;
  1288. int ret;
  1289. if (drv_data && drv_data->switch_voltage)
  1290. return drv_data->switch_voltage(mmc, ios);
  1291. /*
  1292. * Program the voltage. Note that some instances of dw_mmc may use
  1293. * the UHS_REG for this. For other instances (like exynos) the UHS_REG
  1294. * does no harm but you need to set the regulator directly. Try both.
  1295. */
  1296. uhs = mci_readl(host, UHS_REG);
  1297. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  1298. uhs &= ~v18;
  1299. else
  1300. uhs |= v18;
  1301. if (!IS_ERR(mmc->supply.vqmmc)) {
  1302. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1303. if (ret < 0) {
  1304. dev_dbg(&mmc->class_dev,
  1305. "Regulator set error %d - %s V\n",
  1306. ret, uhs & v18 ? "1.8" : "3.3");
  1307. return ret;
  1308. }
  1309. }
  1310. mci_writel(host, UHS_REG, uhs);
  1311. return 0;
  1312. }
  1313. static int dw_mci_get_ro(struct mmc_host *mmc)
  1314. {
  1315. int read_only;
  1316. struct dw_mci_slot *slot = mmc_priv(mmc);
  1317. int gpio_ro = mmc_gpio_get_ro(mmc);
  1318. /* Use platform get_ro function, else try on board write protect */
  1319. if (gpio_ro >= 0)
  1320. read_only = gpio_ro;
  1321. else
  1322. read_only =
  1323. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  1324. dev_dbg(&mmc->class_dev, "card is %s\n",
  1325. read_only ? "read-only" : "read-write");
  1326. return read_only;
  1327. }
  1328. static void dw_mci_hw_reset(struct mmc_host *mmc)
  1329. {
  1330. struct dw_mci_slot *slot = mmc_priv(mmc);
  1331. struct dw_mci *host = slot->host;
  1332. int reset;
  1333. if (host->use_dma == TRANS_MODE_IDMAC)
  1334. dw_mci_idmac_reset(host);
  1335. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
  1336. SDMMC_CTRL_FIFO_RESET))
  1337. return;
  1338. /*
  1339. * According to eMMC spec, card reset procedure:
  1340. * tRstW >= 1us: RST_n pulse width
  1341. * tRSCA >= 200us: RST_n to Command time
  1342. * tRSTH >= 1us: RST_n high period
  1343. */
  1344. reset = mci_readl(host, RST_N);
  1345. reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
  1346. mci_writel(host, RST_N, reset);
  1347. usleep_range(1, 2);
  1348. reset |= SDMMC_RST_HWACTIVE << slot->id;
  1349. mci_writel(host, RST_N, reset);
  1350. usleep_range(200, 300);
  1351. }
  1352. static void dw_mci_prepare_sdio_irq(struct dw_mci_slot *slot, bool prepare)
  1353. {
  1354. struct dw_mci *host = slot->host;
  1355. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  1356. u32 clk_en_a_old;
  1357. u32 clk_en_a;
  1358. /*
  1359. * Low power mode will stop the card clock when idle. According to the
  1360. * description of the CLKENA register we should disable low power mode
  1361. * for SDIO cards if we need SDIO interrupts to work.
  1362. */
  1363. clk_en_a_old = mci_readl(host, CLKENA);
  1364. if (prepare) {
  1365. set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1366. clk_en_a = clk_en_a_old & ~clken_low_pwr;
  1367. } else {
  1368. clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1369. clk_en_a = clk_en_a_old | clken_low_pwr;
  1370. }
  1371. if (clk_en_a != clk_en_a_old) {
  1372. mci_writel(host, CLKENA, clk_en_a);
  1373. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT,
  1374. 0);
  1375. }
  1376. }
  1377. static void __dw_mci_enable_sdio_irq(struct dw_mci_slot *slot, int enb)
  1378. {
  1379. struct dw_mci *host = slot->host;
  1380. unsigned long irqflags;
  1381. u32 int_mask;
  1382. spin_lock_irqsave(&host->irq_lock, irqflags);
  1383. /* Enable/disable Slot Specific SDIO interrupt */
  1384. int_mask = mci_readl(host, INTMASK);
  1385. if (enb)
  1386. int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
  1387. else
  1388. int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
  1389. mci_writel(host, INTMASK, int_mask);
  1390. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1391. }
  1392. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  1393. {
  1394. struct dw_mci_slot *slot = mmc_priv(mmc);
  1395. struct dw_mci *host = slot->host;
  1396. dw_mci_prepare_sdio_irq(slot, enb);
  1397. __dw_mci_enable_sdio_irq(slot, enb);
  1398. /* Avoid runtime suspending the device when SDIO IRQ is enabled */
  1399. if (enb)
  1400. pm_runtime_get_noresume(host->dev);
  1401. else
  1402. pm_runtime_put_noidle(host->dev);
  1403. }
  1404. static void dw_mci_ack_sdio_irq(struct mmc_host *mmc)
  1405. {
  1406. struct dw_mci_slot *slot = mmc_priv(mmc);
  1407. __dw_mci_enable_sdio_irq(slot, 1);
  1408. }
  1409. static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1410. {
  1411. struct dw_mci_slot *slot = mmc_priv(mmc);
  1412. struct dw_mci *host = slot->host;
  1413. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1414. int err = -EINVAL;
  1415. if (drv_data && drv_data->execute_tuning)
  1416. err = drv_data->execute_tuning(slot, opcode);
  1417. return err;
  1418. }
  1419. static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
  1420. struct mmc_ios *ios)
  1421. {
  1422. struct dw_mci_slot *slot = mmc_priv(mmc);
  1423. struct dw_mci *host = slot->host;
  1424. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1425. if (drv_data && drv_data->prepare_hs400_tuning)
  1426. return drv_data->prepare_hs400_tuning(host, ios);
  1427. return 0;
  1428. }
  1429. static bool dw_mci_reset(struct dw_mci *host)
  1430. {
  1431. u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
  1432. bool ret = false;
  1433. u32 status = 0;
  1434. /*
  1435. * Resetting generates a block interrupt, hence setting
  1436. * the scatter-gather pointer to NULL.
  1437. */
  1438. if (host->sg) {
  1439. sg_miter_stop(&host->sg_miter);
  1440. host->sg = NULL;
  1441. }
  1442. if (host->use_dma)
  1443. flags |= SDMMC_CTRL_DMA_RESET;
  1444. if (dw_mci_ctrl_reset(host, flags)) {
  1445. /*
  1446. * In all cases we clear the RAWINTS
  1447. * register to clear any interrupts.
  1448. */
  1449. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1450. if (!host->use_dma) {
  1451. ret = true;
  1452. goto ciu_out;
  1453. }
  1454. /* Wait for dma_req to be cleared */
  1455. if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
  1456. status,
  1457. !(status & SDMMC_STATUS_DMA_REQ),
  1458. 1, 500 * USEC_PER_MSEC)) {
  1459. dev_err(host->dev,
  1460. "%s: Timeout waiting for dma_req to be cleared\n",
  1461. __func__);
  1462. goto ciu_out;
  1463. }
  1464. /* when using DMA next we reset the fifo again */
  1465. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
  1466. goto ciu_out;
  1467. } else {
  1468. /* if the controller reset bit did clear, then set clock regs */
  1469. if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
  1470. dev_err(host->dev,
  1471. "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
  1472. __func__);
  1473. goto ciu_out;
  1474. }
  1475. }
  1476. if (host->use_dma == TRANS_MODE_IDMAC)
  1477. /* It is also required that we reinit idmac */
  1478. dw_mci_idmac_init(host);
  1479. ret = true;
  1480. ciu_out:
  1481. /* After a CTRL reset we need to have CIU set clock registers */
  1482. mci_send_cmd(host->slot, SDMMC_CMD_UPD_CLK, 0);
  1483. return ret;
  1484. }
  1485. static const struct mmc_host_ops dw_mci_ops = {
  1486. .request = dw_mci_request,
  1487. .pre_req = dw_mci_pre_req,
  1488. .post_req = dw_mci_post_req,
  1489. .set_ios = dw_mci_set_ios,
  1490. .get_ro = dw_mci_get_ro,
  1491. .get_cd = dw_mci_get_cd,
  1492. .card_hw_reset = dw_mci_hw_reset,
  1493. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  1494. .ack_sdio_irq = dw_mci_ack_sdio_irq,
  1495. .execute_tuning = dw_mci_execute_tuning,
  1496. .card_busy = dw_mci_card_busy,
  1497. .start_signal_voltage_switch = dw_mci_switch_voltage,
  1498. .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
  1499. };
  1500. #ifdef CONFIG_FAULT_INJECTION
  1501. static enum hrtimer_restart dw_mci_fault_timer(struct hrtimer *t)
  1502. {
  1503. struct dw_mci *host = container_of(t, struct dw_mci, fault_timer);
  1504. unsigned long flags;
  1505. spin_lock_irqsave(&host->irq_lock, flags);
  1506. /*
  1507. * Only inject an error if we haven't already got an error or data over
  1508. * interrupt.
  1509. */
  1510. if (!host->data_status) {
  1511. host->data_status = SDMMC_INT_DCRC;
  1512. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1513. tasklet_schedule(&host->tasklet);
  1514. }
  1515. spin_unlock_irqrestore(&host->irq_lock, flags);
  1516. return HRTIMER_NORESTART;
  1517. }
  1518. static void dw_mci_start_fault_timer(struct dw_mci *host)
  1519. {
  1520. struct mmc_data *data = host->data;
  1521. if (!data || data->blocks <= 1)
  1522. return;
  1523. if (!should_fail(&host->fail_data_crc, 1))
  1524. return;
  1525. /*
  1526. * Try to inject the error at random points during the data transfer.
  1527. */
  1528. hrtimer_start(&host->fault_timer,
  1529. ms_to_ktime(prandom_u32_max(25)),
  1530. HRTIMER_MODE_REL);
  1531. }
  1532. static void dw_mci_stop_fault_timer(struct dw_mci *host)
  1533. {
  1534. hrtimer_cancel(&host->fault_timer);
  1535. }
  1536. static void dw_mci_init_fault(struct dw_mci *host)
  1537. {
  1538. host->fail_data_crc = (struct fault_attr) FAULT_ATTR_INITIALIZER;
  1539. hrtimer_init(&host->fault_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1540. host->fault_timer.function = dw_mci_fault_timer;
  1541. }
  1542. #else
  1543. static void dw_mci_init_fault(struct dw_mci *host)
  1544. {
  1545. }
  1546. static void dw_mci_start_fault_timer(struct dw_mci *host)
  1547. {
  1548. }
  1549. static void dw_mci_stop_fault_timer(struct dw_mci *host)
  1550. {
  1551. }
  1552. #endif
  1553. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  1554. __releases(&host->lock)
  1555. __acquires(&host->lock)
  1556. {
  1557. struct dw_mci_slot *slot;
  1558. struct mmc_host *prev_mmc = host->slot->mmc;
  1559. WARN_ON(host->cmd || host->data);
  1560. host->slot->mrq = NULL;
  1561. host->mrq = NULL;
  1562. if (!list_empty(&host->queue)) {
  1563. slot = list_entry(host->queue.next,
  1564. struct dw_mci_slot, queue_node);
  1565. list_del(&slot->queue_node);
  1566. dev_vdbg(host->dev, "list not empty: %s is next\n",
  1567. mmc_hostname(slot->mmc));
  1568. host->state = STATE_SENDING_CMD;
  1569. dw_mci_start_request(host, slot);
  1570. } else {
  1571. dev_vdbg(host->dev, "list empty\n");
  1572. if (host->state == STATE_SENDING_CMD11)
  1573. host->state = STATE_WAITING_CMD11_DONE;
  1574. else
  1575. host->state = STATE_IDLE;
  1576. }
  1577. spin_unlock(&host->lock);
  1578. mmc_request_done(prev_mmc, mrq);
  1579. spin_lock(&host->lock);
  1580. }
  1581. static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  1582. {
  1583. u32 status = host->cmd_status;
  1584. host->cmd_status = 0;
  1585. /* Read the response from the card (up to 16 bytes) */
  1586. if (cmd->flags & MMC_RSP_PRESENT) {
  1587. if (cmd->flags & MMC_RSP_136) {
  1588. cmd->resp[3] = mci_readl(host, RESP0);
  1589. cmd->resp[2] = mci_readl(host, RESP1);
  1590. cmd->resp[1] = mci_readl(host, RESP2);
  1591. cmd->resp[0] = mci_readl(host, RESP3);
  1592. } else {
  1593. cmd->resp[0] = mci_readl(host, RESP0);
  1594. cmd->resp[1] = 0;
  1595. cmd->resp[2] = 0;
  1596. cmd->resp[3] = 0;
  1597. }
  1598. }
  1599. if (status & SDMMC_INT_RTO)
  1600. cmd->error = -ETIMEDOUT;
  1601. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  1602. cmd->error = -EILSEQ;
  1603. else if (status & SDMMC_INT_RESP_ERR)
  1604. cmd->error = -EIO;
  1605. else
  1606. cmd->error = 0;
  1607. return cmd->error;
  1608. }
  1609. static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
  1610. {
  1611. u32 status = host->data_status;
  1612. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1613. if (status & SDMMC_INT_DRTO) {
  1614. data->error = -ETIMEDOUT;
  1615. } else if (status & SDMMC_INT_DCRC) {
  1616. data->error = -EILSEQ;
  1617. } else if (status & SDMMC_INT_EBE) {
  1618. if (host->dir_status ==
  1619. DW_MCI_SEND_STATUS) {
  1620. /*
  1621. * No data CRC status was returned.
  1622. * The number of bytes transferred
  1623. * will be exaggerated in PIO mode.
  1624. */
  1625. data->bytes_xfered = 0;
  1626. data->error = -ETIMEDOUT;
  1627. } else if (host->dir_status ==
  1628. DW_MCI_RECV_STATUS) {
  1629. data->error = -EILSEQ;
  1630. }
  1631. } else {
  1632. /* SDMMC_INT_SBE is included */
  1633. data->error = -EILSEQ;
  1634. }
  1635. dev_dbg(host->dev, "data error, status 0x%08x\n", status);
  1636. /*
  1637. * After an error, there may be data lingering
  1638. * in the FIFO
  1639. */
  1640. dw_mci_reset(host);
  1641. } else {
  1642. data->bytes_xfered = data->blocks * data->blksz;
  1643. data->error = 0;
  1644. }
  1645. return data->error;
  1646. }
  1647. static void dw_mci_set_drto(struct dw_mci *host)
  1648. {
  1649. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1650. unsigned int drto_clks;
  1651. unsigned int drto_div;
  1652. unsigned int drto_ms;
  1653. unsigned long irqflags;
  1654. if (drv_data && drv_data->get_drto_clks)
  1655. drto_clks = drv_data->get_drto_clks(host);
  1656. else
  1657. drto_clks = mci_readl(host, TMOUT) >> 8;
  1658. drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
  1659. if (drto_div == 0)
  1660. drto_div = 1;
  1661. drto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * drto_clks * drto_div,
  1662. host->bus_hz);
  1663. dev_dbg(host->dev, "drto_ms: %u\n", drto_ms);
  1664. /* add a bit spare time */
  1665. drto_ms += 10;
  1666. spin_lock_irqsave(&host->irq_lock, irqflags);
  1667. if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
  1668. mod_timer(&host->dto_timer,
  1669. jiffies + msecs_to_jiffies(drto_ms));
  1670. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1671. }
  1672. static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host)
  1673. {
  1674. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  1675. return false;
  1676. /*
  1677. * Really be certain that the timer has stopped. This is a bit of
  1678. * paranoia and could only really happen if we had really bad
  1679. * interrupt latency and the interrupt routine and timeout were
  1680. * running concurrently so that the del_timer() in the interrupt
  1681. * handler couldn't run.
  1682. */
  1683. WARN_ON(del_timer_sync(&host->cto_timer));
  1684. clear_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1685. return true;
  1686. }
  1687. static bool dw_mci_clear_pending_data_complete(struct dw_mci *host)
  1688. {
  1689. if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
  1690. return false;
  1691. /* Extra paranoia just like dw_mci_clear_pending_cmd_complete() */
  1692. WARN_ON(del_timer_sync(&host->dto_timer));
  1693. clear_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1694. return true;
  1695. }
  1696. static void dw_mci_tasklet_func(struct tasklet_struct *t)
  1697. {
  1698. struct dw_mci *host = from_tasklet(host, t, tasklet);
  1699. struct mmc_data *data;
  1700. struct mmc_command *cmd;
  1701. struct mmc_request *mrq;
  1702. enum dw_mci_state state;
  1703. enum dw_mci_state prev_state;
  1704. unsigned int err;
  1705. spin_lock(&host->lock);
  1706. state = host->state;
  1707. data = host->data;
  1708. mrq = host->mrq;
  1709. do {
  1710. prev_state = state;
  1711. switch (state) {
  1712. case STATE_IDLE:
  1713. case STATE_WAITING_CMD11_DONE:
  1714. break;
  1715. case STATE_SENDING_CMD11:
  1716. case STATE_SENDING_CMD:
  1717. if (!dw_mci_clear_pending_cmd_complete(host))
  1718. break;
  1719. cmd = host->cmd;
  1720. host->cmd = NULL;
  1721. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  1722. err = dw_mci_command_complete(host, cmd);
  1723. if (cmd == mrq->sbc && !err) {
  1724. __dw_mci_start_request(host, host->slot,
  1725. mrq->cmd);
  1726. goto unlock;
  1727. }
  1728. if (cmd->data && err) {
  1729. /*
  1730. * During UHS tuning sequence, sending the stop
  1731. * command after the response CRC error would
  1732. * throw the system into a confused state
  1733. * causing all future tuning phases to report
  1734. * failure.
  1735. *
  1736. * In such case controller will move into a data
  1737. * transfer state after a response error or
  1738. * response CRC error. Let's let that finish
  1739. * before trying to send a stop, so we'll go to
  1740. * STATE_SENDING_DATA.
  1741. *
  1742. * Although letting the data transfer take place
  1743. * will waste a bit of time (we already know
  1744. * the command was bad), it can't cause any
  1745. * errors since it's possible it would have
  1746. * taken place anyway if this tasklet got
  1747. * delayed. Allowing the transfer to take place
  1748. * avoids races and keeps things simple.
  1749. */
  1750. if (err != -ETIMEDOUT &&
  1751. host->dir_status == DW_MCI_RECV_STATUS) {
  1752. state = STATE_SENDING_DATA;
  1753. continue;
  1754. }
  1755. send_stop_abort(host, data);
  1756. dw_mci_stop_dma(host);
  1757. state = STATE_SENDING_STOP;
  1758. break;
  1759. }
  1760. if (!cmd->data || err) {
  1761. dw_mci_request_end(host, mrq);
  1762. goto unlock;
  1763. }
  1764. prev_state = state = STATE_SENDING_DATA;
  1765. fallthrough;
  1766. case STATE_SENDING_DATA:
  1767. /*
  1768. * We could get a data error and never a transfer
  1769. * complete so we'd better check for it here.
  1770. *
  1771. * Note that we don't really care if we also got a
  1772. * transfer complete; stopping the DMA and sending an
  1773. * abort won't hurt.
  1774. */
  1775. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1776. &host->pending_events)) {
  1777. if (!(host->data_status & (SDMMC_INT_DRTO |
  1778. SDMMC_INT_EBE)))
  1779. send_stop_abort(host, data);
  1780. dw_mci_stop_dma(host);
  1781. state = STATE_DATA_ERROR;
  1782. break;
  1783. }
  1784. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1785. &host->pending_events)) {
  1786. /*
  1787. * If all data-related interrupts don't come
  1788. * within the given time in reading data state.
  1789. */
  1790. if (host->dir_status == DW_MCI_RECV_STATUS)
  1791. dw_mci_set_drto(host);
  1792. break;
  1793. }
  1794. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  1795. /*
  1796. * Handle an EVENT_DATA_ERROR that might have shown up
  1797. * before the transfer completed. This might not have
  1798. * been caught by the check above because the interrupt
  1799. * could have gone off between the previous check and
  1800. * the check for transfer complete.
  1801. *
  1802. * Technically this ought not be needed assuming we
  1803. * get a DATA_COMPLETE eventually (we'll notice the
  1804. * error and end the request), but it shouldn't hurt.
  1805. *
  1806. * This has the advantage of sending the stop command.
  1807. */
  1808. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1809. &host->pending_events)) {
  1810. if (!(host->data_status & (SDMMC_INT_DRTO |
  1811. SDMMC_INT_EBE)))
  1812. send_stop_abort(host, data);
  1813. dw_mci_stop_dma(host);
  1814. state = STATE_DATA_ERROR;
  1815. break;
  1816. }
  1817. prev_state = state = STATE_DATA_BUSY;
  1818. fallthrough;
  1819. case STATE_DATA_BUSY:
  1820. if (!dw_mci_clear_pending_data_complete(host)) {
  1821. /*
  1822. * If data error interrupt comes but data over
  1823. * interrupt doesn't come within the given time.
  1824. * in reading data state.
  1825. */
  1826. if (host->dir_status == DW_MCI_RECV_STATUS)
  1827. dw_mci_set_drto(host);
  1828. break;
  1829. }
  1830. dw_mci_stop_fault_timer(host);
  1831. host->data = NULL;
  1832. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  1833. err = dw_mci_data_complete(host, data);
  1834. if (!err) {
  1835. if (!data->stop || mrq->sbc) {
  1836. if (mrq->sbc && data->stop)
  1837. data->stop->error = 0;
  1838. dw_mci_request_end(host, mrq);
  1839. goto unlock;
  1840. }
  1841. /* stop command for open-ended transfer*/
  1842. if (data->stop)
  1843. send_stop_abort(host, data);
  1844. } else {
  1845. /*
  1846. * If we don't have a command complete now we'll
  1847. * never get one since we just reset everything;
  1848. * better end the request.
  1849. *
  1850. * If we do have a command complete we'll fall
  1851. * through to the SENDING_STOP command and
  1852. * everything will be peachy keen.
  1853. */
  1854. if (!test_bit(EVENT_CMD_COMPLETE,
  1855. &host->pending_events)) {
  1856. host->cmd = NULL;
  1857. dw_mci_request_end(host, mrq);
  1858. goto unlock;
  1859. }
  1860. }
  1861. /*
  1862. * If err has non-zero,
  1863. * stop-abort command has been already issued.
  1864. */
  1865. prev_state = state = STATE_SENDING_STOP;
  1866. fallthrough;
  1867. case STATE_SENDING_STOP:
  1868. if (!dw_mci_clear_pending_cmd_complete(host))
  1869. break;
  1870. /* CMD error in data command */
  1871. if (mrq->cmd->error && mrq->data)
  1872. dw_mci_reset(host);
  1873. dw_mci_stop_fault_timer(host);
  1874. host->cmd = NULL;
  1875. host->data = NULL;
  1876. if (!mrq->sbc && mrq->stop)
  1877. dw_mci_command_complete(host, mrq->stop);
  1878. else
  1879. host->cmd_status = 0;
  1880. dw_mci_request_end(host, mrq);
  1881. goto unlock;
  1882. case STATE_DATA_ERROR:
  1883. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1884. &host->pending_events))
  1885. break;
  1886. state = STATE_DATA_BUSY;
  1887. break;
  1888. }
  1889. } while (state != prev_state);
  1890. host->state = state;
  1891. unlock:
  1892. spin_unlock(&host->lock);
  1893. }
  1894. /* push final bytes to part_buf, only use during push */
  1895. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1896. {
  1897. memcpy((void *)&host->part_buf, buf, cnt);
  1898. host->part_buf_count = cnt;
  1899. }
  1900. /* append bytes to part_buf, only use during push */
  1901. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1902. {
  1903. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  1904. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  1905. host->part_buf_count += cnt;
  1906. return cnt;
  1907. }
  1908. /* pull first bytes from part_buf, only use during pull */
  1909. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1910. {
  1911. cnt = min_t(int, cnt, host->part_buf_count);
  1912. if (cnt) {
  1913. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  1914. cnt);
  1915. host->part_buf_count -= cnt;
  1916. host->part_buf_start += cnt;
  1917. }
  1918. return cnt;
  1919. }
  1920. /* pull final bytes from the part_buf, assuming it's just been filled */
  1921. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  1922. {
  1923. memcpy(buf, &host->part_buf, cnt);
  1924. host->part_buf_start = cnt;
  1925. host->part_buf_count = (1 << host->data_shift) - cnt;
  1926. }
  1927. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  1928. {
  1929. struct mmc_data *data = host->data;
  1930. int init_cnt = cnt;
  1931. /* try and push anything in the part_buf */
  1932. if (unlikely(host->part_buf_count)) {
  1933. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1934. buf += len;
  1935. cnt -= len;
  1936. if (host->part_buf_count == 2) {
  1937. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1938. host->part_buf_count = 0;
  1939. }
  1940. }
  1941. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1942. if (unlikely((unsigned long)buf & 0x1)) {
  1943. while (cnt >= 2) {
  1944. u16 aligned_buf[64];
  1945. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1946. int items = len >> 1;
  1947. int i;
  1948. /* memcpy from input buffer into aligned buffer */
  1949. memcpy(aligned_buf, buf, len);
  1950. buf += len;
  1951. cnt -= len;
  1952. /* push data from aligned buffer into fifo */
  1953. for (i = 0; i < items; ++i)
  1954. mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
  1955. }
  1956. } else
  1957. #endif
  1958. {
  1959. u16 *pdata = buf;
  1960. for (; cnt >= 2; cnt -= 2)
  1961. mci_fifo_writew(host->fifo_reg, *pdata++);
  1962. buf = pdata;
  1963. }
  1964. /* put anything remaining in the part_buf */
  1965. if (cnt) {
  1966. dw_mci_set_part_bytes(host, buf, cnt);
  1967. /* Push data if we have reached the expected data length */
  1968. if ((data->bytes_xfered + init_cnt) ==
  1969. (data->blksz * data->blocks))
  1970. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1971. }
  1972. }
  1973. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1974. {
  1975. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1976. if (unlikely((unsigned long)buf & 0x1)) {
  1977. while (cnt >= 2) {
  1978. /* pull data from fifo into aligned buffer */
  1979. u16 aligned_buf[64];
  1980. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1981. int items = len >> 1;
  1982. int i;
  1983. for (i = 0; i < items; ++i)
  1984. aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
  1985. /* memcpy from aligned buffer into output buffer */
  1986. memcpy(buf, aligned_buf, len);
  1987. buf += len;
  1988. cnt -= len;
  1989. }
  1990. } else
  1991. #endif
  1992. {
  1993. u16 *pdata = buf;
  1994. for (; cnt >= 2; cnt -= 2)
  1995. *pdata++ = mci_fifo_readw(host->fifo_reg);
  1996. buf = pdata;
  1997. }
  1998. if (cnt) {
  1999. host->part_buf16 = mci_fifo_readw(host->fifo_reg);
  2000. dw_mci_pull_final_bytes(host, buf, cnt);
  2001. }
  2002. }
  2003. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  2004. {
  2005. struct mmc_data *data = host->data;
  2006. int init_cnt = cnt;
  2007. /* try and push anything in the part_buf */
  2008. if (unlikely(host->part_buf_count)) {
  2009. int len = dw_mci_push_part_bytes(host, buf, cnt);
  2010. buf += len;
  2011. cnt -= len;
  2012. if (host->part_buf_count == 4) {
  2013. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  2014. host->part_buf_count = 0;
  2015. }
  2016. }
  2017. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2018. if (unlikely((unsigned long)buf & 0x3)) {
  2019. while (cnt >= 4) {
  2020. u32 aligned_buf[32];
  2021. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  2022. int items = len >> 2;
  2023. int i;
  2024. /* memcpy from input buffer into aligned buffer */
  2025. memcpy(aligned_buf, buf, len);
  2026. buf += len;
  2027. cnt -= len;
  2028. /* push data from aligned buffer into fifo */
  2029. for (i = 0; i < items; ++i)
  2030. mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
  2031. }
  2032. } else
  2033. #endif
  2034. {
  2035. u32 *pdata = buf;
  2036. for (; cnt >= 4; cnt -= 4)
  2037. mci_fifo_writel(host->fifo_reg, *pdata++);
  2038. buf = pdata;
  2039. }
  2040. /* put anything remaining in the part_buf */
  2041. if (cnt) {
  2042. dw_mci_set_part_bytes(host, buf, cnt);
  2043. /* Push data if we have reached the expected data length */
  2044. if ((data->bytes_xfered + init_cnt) ==
  2045. (data->blksz * data->blocks))
  2046. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  2047. }
  2048. }
  2049. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  2050. {
  2051. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2052. if (unlikely((unsigned long)buf & 0x3)) {
  2053. while (cnt >= 4) {
  2054. /* pull data from fifo into aligned buffer */
  2055. u32 aligned_buf[32];
  2056. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  2057. int items = len >> 2;
  2058. int i;
  2059. for (i = 0; i < items; ++i)
  2060. aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
  2061. /* memcpy from aligned buffer into output buffer */
  2062. memcpy(buf, aligned_buf, len);
  2063. buf += len;
  2064. cnt -= len;
  2065. }
  2066. } else
  2067. #endif
  2068. {
  2069. u32 *pdata = buf;
  2070. for (; cnt >= 4; cnt -= 4)
  2071. *pdata++ = mci_fifo_readl(host->fifo_reg);
  2072. buf = pdata;
  2073. }
  2074. if (cnt) {
  2075. host->part_buf32 = mci_fifo_readl(host->fifo_reg);
  2076. dw_mci_pull_final_bytes(host, buf, cnt);
  2077. }
  2078. }
  2079. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  2080. {
  2081. struct mmc_data *data = host->data;
  2082. int init_cnt = cnt;
  2083. /* try and push anything in the part_buf */
  2084. if (unlikely(host->part_buf_count)) {
  2085. int len = dw_mci_push_part_bytes(host, buf, cnt);
  2086. buf += len;
  2087. cnt -= len;
  2088. if (host->part_buf_count == 8) {
  2089. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  2090. host->part_buf_count = 0;
  2091. }
  2092. }
  2093. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2094. if (unlikely((unsigned long)buf & 0x7)) {
  2095. while (cnt >= 8) {
  2096. u64 aligned_buf[16];
  2097. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  2098. int items = len >> 3;
  2099. int i;
  2100. /* memcpy from input buffer into aligned buffer */
  2101. memcpy(aligned_buf, buf, len);
  2102. buf += len;
  2103. cnt -= len;
  2104. /* push data from aligned buffer into fifo */
  2105. for (i = 0; i < items; ++i)
  2106. mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
  2107. }
  2108. } else
  2109. #endif
  2110. {
  2111. u64 *pdata = buf;
  2112. for (; cnt >= 8; cnt -= 8)
  2113. mci_fifo_writeq(host->fifo_reg, *pdata++);
  2114. buf = pdata;
  2115. }
  2116. /* put anything remaining in the part_buf */
  2117. if (cnt) {
  2118. dw_mci_set_part_bytes(host, buf, cnt);
  2119. /* Push data if we have reached the expected data length */
  2120. if ((data->bytes_xfered + init_cnt) ==
  2121. (data->blksz * data->blocks))
  2122. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  2123. }
  2124. }
  2125. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  2126. {
  2127. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2128. if (unlikely((unsigned long)buf & 0x7)) {
  2129. while (cnt >= 8) {
  2130. /* pull data from fifo into aligned buffer */
  2131. u64 aligned_buf[16];
  2132. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  2133. int items = len >> 3;
  2134. int i;
  2135. for (i = 0; i < items; ++i)
  2136. aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
  2137. /* memcpy from aligned buffer into output buffer */
  2138. memcpy(buf, aligned_buf, len);
  2139. buf += len;
  2140. cnt -= len;
  2141. }
  2142. } else
  2143. #endif
  2144. {
  2145. u64 *pdata = buf;
  2146. for (; cnt >= 8; cnt -= 8)
  2147. *pdata++ = mci_fifo_readq(host->fifo_reg);
  2148. buf = pdata;
  2149. }
  2150. if (cnt) {
  2151. host->part_buf = mci_fifo_readq(host->fifo_reg);
  2152. dw_mci_pull_final_bytes(host, buf, cnt);
  2153. }
  2154. }
  2155. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  2156. {
  2157. int len;
  2158. /* get remaining partial bytes */
  2159. len = dw_mci_pull_part_bytes(host, buf, cnt);
  2160. if (unlikely(len == cnt))
  2161. return;
  2162. buf += len;
  2163. cnt -= len;
  2164. /* get the rest of the data */
  2165. host->pull_data(host, buf, cnt);
  2166. }
  2167. static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
  2168. {
  2169. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  2170. void *buf;
  2171. unsigned int offset;
  2172. struct mmc_data *data = host->data;
  2173. int shift = host->data_shift;
  2174. u32 status;
  2175. unsigned int len;
  2176. unsigned int remain, fcnt;
  2177. do {
  2178. if (!sg_miter_next(sg_miter))
  2179. goto done;
  2180. host->sg = sg_miter->piter.sg;
  2181. buf = sg_miter->addr;
  2182. remain = sg_miter->length;
  2183. offset = 0;
  2184. do {
  2185. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  2186. << shift) + host->part_buf_count;
  2187. len = min(remain, fcnt);
  2188. if (!len)
  2189. break;
  2190. dw_mci_pull_data(host, (void *)(buf + offset), len);
  2191. data->bytes_xfered += len;
  2192. offset += len;
  2193. remain -= len;
  2194. } while (remain);
  2195. sg_miter->consumed = offset;
  2196. status = mci_readl(host, MINTSTS);
  2197. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  2198. /* if the RXDR is ready read again */
  2199. } while ((status & SDMMC_INT_RXDR) ||
  2200. (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
  2201. if (!remain) {
  2202. if (!sg_miter_next(sg_miter))
  2203. goto done;
  2204. sg_miter->consumed = 0;
  2205. }
  2206. sg_miter_stop(sg_miter);
  2207. return;
  2208. done:
  2209. sg_miter_stop(sg_miter);
  2210. host->sg = NULL;
  2211. smp_wmb(); /* drain writebuffer */
  2212. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  2213. }
  2214. static void dw_mci_write_data_pio(struct dw_mci *host)
  2215. {
  2216. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  2217. void *buf;
  2218. unsigned int offset;
  2219. struct mmc_data *data = host->data;
  2220. int shift = host->data_shift;
  2221. u32 status;
  2222. unsigned int len;
  2223. unsigned int fifo_depth = host->fifo_depth;
  2224. unsigned int remain, fcnt;
  2225. do {
  2226. if (!sg_miter_next(sg_miter))
  2227. goto done;
  2228. host->sg = sg_miter->piter.sg;
  2229. buf = sg_miter->addr;
  2230. remain = sg_miter->length;
  2231. offset = 0;
  2232. do {
  2233. fcnt = ((fifo_depth -
  2234. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  2235. << shift) - host->part_buf_count;
  2236. len = min(remain, fcnt);
  2237. if (!len)
  2238. break;
  2239. host->push_data(host, (void *)(buf + offset), len);
  2240. data->bytes_xfered += len;
  2241. offset += len;
  2242. remain -= len;
  2243. } while (remain);
  2244. sg_miter->consumed = offset;
  2245. status = mci_readl(host, MINTSTS);
  2246. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  2247. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  2248. if (!remain) {
  2249. if (!sg_miter_next(sg_miter))
  2250. goto done;
  2251. sg_miter->consumed = 0;
  2252. }
  2253. sg_miter_stop(sg_miter);
  2254. return;
  2255. done:
  2256. sg_miter_stop(sg_miter);
  2257. host->sg = NULL;
  2258. smp_wmb(); /* drain writebuffer */
  2259. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  2260. }
  2261. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  2262. {
  2263. del_timer(&host->cto_timer);
  2264. if (!host->cmd_status)
  2265. host->cmd_status = status;
  2266. smp_wmb(); /* drain writebuffer */
  2267. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2268. tasklet_schedule(&host->tasklet);
  2269. dw_mci_start_fault_timer(host);
  2270. }
  2271. static void dw_mci_handle_cd(struct dw_mci *host)
  2272. {
  2273. struct dw_mci_slot *slot = host->slot;
  2274. mmc_detect_change(slot->mmc,
  2275. msecs_to_jiffies(host->pdata->detect_delay_ms));
  2276. }
  2277. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  2278. {
  2279. struct dw_mci *host = dev_id;
  2280. u32 pending;
  2281. struct dw_mci_slot *slot = host->slot;
  2282. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  2283. if (pending) {
  2284. /* Check volt switch first, since it can look like an error */
  2285. if ((host->state == STATE_SENDING_CMD11) &&
  2286. (pending & SDMMC_INT_VOLT_SWITCH)) {
  2287. mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
  2288. pending &= ~SDMMC_INT_VOLT_SWITCH;
  2289. /*
  2290. * Hold the lock; we know cmd11_timer can't be kicked
  2291. * off after the lock is released, so safe to delete.
  2292. */
  2293. spin_lock(&host->irq_lock);
  2294. dw_mci_cmd_interrupt(host, pending);
  2295. spin_unlock(&host->irq_lock);
  2296. del_timer(&host->cmd11_timer);
  2297. }
  2298. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  2299. spin_lock(&host->irq_lock);
  2300. del_timer(&host->cto_timer);
  2301. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  2302. host->cmd_status = pending;
  2303. smp_wmb(); /* drain writebuffer */
  2304. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2305. spin_unlock(&host->irq_lock);
  2306. }
  2307. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  2308. spin_lock(&host->irq_lock);
  2309. if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
  2310. del_timer(&host->dto_timer);
  2311. /* if there is an error report DATA_ERROR */
  2312. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  2313. host->data_status = pending;
  2314. smp_wmb(); /* drain writebuffer */
  2315. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  2316. if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
  2317. /* In case of error, we cannot expect a DTO */
  2318. set_bit(EVENT_DATA_COMPLETE,
  2319. &host->pending_events);
  2320. tasklet_schedule(&host->tasklet);
  2321. spin_unlock(&host->irq_lock);
  2322. }
  2323. if (pending & SDMMC_INT_DATA_OVER) {
  2324. spin_lock(&host->irq_lock);
  2325. del_timer(&host->dto_timer);
  2326. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  2327. if (!host->data_status)
  2328. host->data_status = pending;
  2329. smp_wmb(); /* drain writebuffer */
  2330. if (host->dir_status == DW_MCI_RECV_STATUS) {
  2331. if (host->sg != NULL)
  2332. dw_mci_read_data_pio(host, true);
  2333. }
  2334. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  2335. tasklet_schedule(&host->tasklet);
  2336. spin_unlock(&host->irq_lock);
  2337. }
  2338. if (pending & SDMMC_INT_RXDR) {
  2339. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  2340. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  2341. dw_mci_read_data_pio(host, false);
  2342. }
  2343. if (pending & SDMMC_INT_TXDR) {
  2344. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  2345. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  2346. dw_mci_write_data_pio(host);
  2347. }
  2348. if (pending & SDMMC_INT_CMD_DONE) {
  2349. spin_lock(&host->irq_lock);
  2350. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  2351. dw_mci_cmd_interrupt(host, pending);
  2352. spin_unlock(&host->irq_lock);
  2353. }
  2354. if (pending & SDMMC_INT_CD) {
  2355. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  2356. dw_mci_handle_cd(host);
  2357. }
  2358. if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
  2359. mci_writel(host, RINTSTS,
  2360. SDMMC_INT_SDIO(slot->sdio_id));
  2361. __dw_mci_enable_sdio_irq(slot, 0);
  2362. sdio_signal_irq(slot->mmc);
  2363. }
  2364. }
  2365. if (host->use_dma != TRANS_MODE_IDMAC)
  2366. return IRQ_HANDLED;
  2367. /* Handle IDMA interrupts */
  2368. if (host->dma_64bit_address == 1) {
  2369. pending = mci_readl(host, IDSTS64);
  2370. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  2371. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
  2372. SDMMC_IDMAC_INT_RI);
  2373. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
  2374. if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
  2375. host->dma_ops->complete((void *)host);
  2376. }
  2377. } else {
  2378. pending = mci_readl(host, IDSTS);
  2379. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  2380. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
  2381. SDMMC_IDMAC_INT_RI);
  2382. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  2383. if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
  2384. host->dma_ops->complete((void *)host);
  2385. }
  2386. }
  2387. return IRQ_HANDLED;
  2388. }
  2389. static int dw_mci_init_slot_caps(struct dw_mci_slot *slot)
  2390. {
  2391. struct dw_mci *host = slot->host;
  2392. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2393. struct mmc_host *mmc = slot->mmc;
  2394. int ctrl_id;
  2395. if (host->pdata->caps)
  2396. mmc->caps = host->pdata->caps;
  2397. if (host->pdata->pm_caps)
  2398. mmc->pm_caps = host->pdata->pm_caps;
  2399. if (drv_data)
  2400. mmc->caps |= drv_data->common_caps;
  2401. if (host->dev->of_node) {
  2402. ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  2403. if (ctrl_id < 0)
  2404. ctrl_id = 0;
  2405. } else {
  2406. ctrl_id = to_platform_device(host->dev)->id;
  2407. }
  2408. if (drv_data && drv_data->caps) {
  2409. if (ctrl_id >= drv_data->num_caps) {
  2410. dev_err(host->dev, "invalid controller id %d\n",
  2411. ctrl_id);
  2412. return -EINVAL;
  2413. }
  2414. mmc->caps |= drv_data->caps[ctrl_id];
  2415. }
  2416. if (host->pdata->caps2)
  2417. mmc->caps2 = host->pdata->caps2;
  2418. /* if host has set a minimum_freq, we should respect it */
  2419. if (host->minimum_speed)
  2420. mmc->f_min = host->minimum_speed;
  2421. else
  2422. mmc->f_min = DW_MCI_FREQ_MIN;
  2423. if (!mmc->f_max)
  2424. mmc->f_max = DW_MCI_FREQ_MAX;
  2425. /* Process SDIO IRQs through the sdio_irq_work. */
  2426. if (mmc->caps & MMC_CAP_SDIO_IRQ)
  2427. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  2428. return 0;
  2429. }
  2430. static int dw_mci_init_slot(struct dw_mci *host)
  2431. {
  2432. struct mmc_host *mmc;
  2433. struct dw_mci_slot *slot;
  2434. int ret;
  2435. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
  2436. if (!mmc)
  2437. return -ENOMEM;
  2438. slot = mmc_priv(mmc);
  2439. slot->id = 0;
  2440. slot->sdio_id = host->sdio_id0 + slot->id;
  2441. slot->mmc = mmc;
  2442. slot->host = host;
  2443. host->slot = slot;
  2444. mmc->ops = &dw_mci_ops;
  2445. /*if there are external regulators, get them*/
  2446. ret = mmc_regulator_get_supply(mmc);
  2447. if (ret)
  2448. goto err_host_allocated;
  2449. if (!mmc->ocr_avail)
  2450. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  2451. ret = mmc_of_parse(mmc);
  2452. if (ret)
  2453. goto err_host_allocated;
  2454. ret = dw_mci_init_slot_caps(slot);
  2455. if (ret)
  2456. goto err_host_allocated;
  2457. /* Useful defaults if platform data is unset. */
  2458. if (host->use_dma == TRANS_MODE_IDMAC) {
  2459. mmc->max_segs = host->ring_size;
  2460. mmc->max_blk_size = 65535;
  2461. mmc->max_seg_size = 0x1000;
  2462. mmc->max_req_size = mmc->max_seg_size * host->ring_size;
  2463. mmc->max_blk_count = mmc->max_req_size / 512;
  2464. } else if (host->use_dma == TRANS_MODE_EDMAC) {
  2465. mmc->max_segs = 64;
  2466. mmc->max_blk_size = 65535;
  2467. mmc->max_blk_count = 65535;
  2468. mmc->max_req_size =
  2469. mmc->max_blk_size * mmc->max_blk_count;
  2470. mmc->max_seg_size = mmc->max_req_size;
  2471. } else {
  2472. /* TRANS_MODE_PIO */
  2473. mmc->max_segs = 64;
  2474. mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
  2475. mmc->max_blk_count = 512;
  2476. mmc->max_req_size = mmc->max_blk_size *
  2477. mmc->max_blk_count;
  2478. mmc->max_seg_size = mmc->max_req_size;
  2479. }
  2480. dw_mci_get_cd(mmc);
  2481. ret = mmc_add_host(mmc);
  2482. if (ret)
  2483. goto err_host_allocated;
  2484. #if defined(CONFIG_DEBUG_FS)
  2485. dw_mci_init_debugfs(slot);
  2486. #endif
  2487. return 0;
  2488. err_host_allocated:
  2489. mmc_free_host(mmc);
  2490. return ret;
  2491. }
  2492. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot)
  2493. {
  2494. /* Debugfs stuff is cleaned up by mmc core */
  2495. mmc_remove_host(slot->mmc);
  2496. slot->host->slot = NULL;
  2497. mmc_free_host(slot->mmc);
  2498. }
  2499. static void dw_mci_init_dma(struct dw_mci *host)
  2500. {
  2501. int addr_config;
  2502. struct device *dev = host->dev;
  2503. /*
  2504. * Check tansfer mode from HCON[17:16]
  2505. * Clear the ambiguous description of dw_mmc databook:
  2506. * 2b'00: No DMA Interface -> Actually means using Internal DMA block
  2507. * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
  2508. * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
  2509. * 2b'11: Non DW DMA Interface -> pio only
  2510. * Compared to DesignWare DMA Interface, Generic DMA Interface has a
  2511. * simpler request/acknowledge handshake mechanism and both of them
  2512. * are regarded as external dma master for dw_mmc.
  2513. */
  2514. host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
  2515. if (host->use_dma == DMA_INTERFACE_IDMA) {
  2516. host->use_dma = TRANS_MODE_IDMAC;
  2517. } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
  2518. host->use_dma == DMA_INTERFACE_GDMA) {
  2519. host->use_dma = TRANS_MODE_EDMAC;
  2520. } else {
  2521. goto no_dma;
  2522. }
  2523. /* Determine which DMA interface to use */
  2524. if (host->use_dma == TRANS_MODE_IDMAC) {
  2525. /*
  2526. * Check ADDR_CONFIG bit in HCON to find
  2527. * IDMAC address bus width
  2528. */
  2529. addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
  2530. if (addr_config == 1) {
  2531. /* host supports IDMAC in 64-bit address mode */
  2532. host->dma_64bit_address = 1;
  2533. dev_info(host->dev,
  2534. "IDMAC supports 64-bit address mode.\n");
  2535. if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
  2536. dma_set_coherent_mask(host->dev,
  2537. DMA_BIT_MASK(64));
  2538. } else {
  2539. /* host supports IDMAC in 32-bit address mode */
  2540. host->dma_64bit_address = 0;
  2541. dev_info(host->dev,
  2542. "IDMAC supports 32-bit address mode.\n");
  2543. }
  2544. /* Alloc memory for sg translation */
  2545. host->sg_cpu = dmam_alloc_coherent(host->dev,
  2546. DESC_RING_BUF_SZ,
  2547. &host->sg_dma, GFP_KERNEL);
  2548. if (!host->sg_cpu) {
  2549. dev_err(host->dev,
  2550. "%s: could not alloc DMA memory\n",
  2551. __func__);
  2552. goto no_dma;
  2553. }
  2554. host->dma_ops = &dw_mci_idmac_ops;
  2555. dev_info(host->dev, "Using internal DMA controller.\n");
  2556. } else {
  2557. /* TRANS_MODE_EDMAC: check dma bindings again */
  2558. if ((device_property_string_array_count(dev, "dma-names") < 0) ||
  2559. !device_property_present(dev, "dmas")) {
  2560. goto no_dma;
  2561. }
  2562. host->dma_ops = &dw_mci_edmac_ops;
  2563. dev_info(host->dev, "Using external DMA controller.\n");
  2564. }
  2565. if (host->dma_ops->init && host->dma_ops->start &&
  2566. host->dma_ops->stop && host->dma_ops->cleanup) {
  2567. if (host->dma_ops->init(host)) {
  2568. dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
  2569. __func__);
  2570. goto no_dma;
  2571. }
  2572. } else {
  2573. dev_err(host->dev, "DMA initialization not found.\n");
  2574. goto no_dma;
  2575. }
  2576. return;
  2577. no_dma:
  2578. dev_info(host->dev, "Using PIO mode.\n");
  2579. host->use_dma = TRANS_MODE_PIO;
  2580. }
  2581. static void dw_mci_cmd11_timer(struct timer_list *t)
  2582. {
  2583. struct dw_mci *host = from_timer(host, t, cmd11_timer);
  2584. if (host->state != STATE_SENDING_CMD11) {
  2585. dev_warn(host->dev, "Unexpected CMD11 timeout\n");
  2586. return;
  2587. }
  2588. host->cmd_status = SDMMC_INT_RTO;
  2589. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2590. tasklet_schedule(&host->tasklet);
  2591. }
  2592. static void dw_mci_cto_timer(struct timer_list *t)
  2593. {
  2594. struct dw_mci *host = from_timer(host, t, cto_timer);
  2595. unsigned long irqflags;
  2596. u32 pending;
  2597. spin_lock_irqsave(&host->irq_lock, irqflags);
  2598. /*
  2599. * If somehow we have very bad interrupt latency it's remotely possible
  2600. * that the timer could fire while the interrupt is still pending or
  2601. * while the interrupt is midway through running. Let's be paranoid
  2602. * and detect those two cases. Note that this is paranoia is somewhat
  2603. * justified because in this function we don't actually cancel the
  2604. * pending command in the controller--we just assume it will never come.
  2605. */
  2606. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  2607. if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) {
  2608. /* The interrupt should fire; no need to act but we can warn */
  2609. dev_warn(host->dev, "Unexpected interrupt latency\n");
  2610. goto exit;
  2611. }
  2612. if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) {
  2613. /* Presumably interrupt handler couldn't delete the timer */
  2614. dev_warn(host->dev, "CTO timeout when already completed\n");
  2615. goto exit;
  2616. }
  2617. /*
  2618. * Continued paranoia to make sure we're in the state we expect.
  2619. * This paranoia isn't really justified but it seems good to be safe.
  2620. */
  2621. switch (host->state) {
  2622. case STATE_SENDING_CMD11:
  2623. case STATE_SENDING_CMD:
  2624. case STATE_SENDING_STOP:
  2625. /*
  2626. * If CMD_DONE interrupt does NOT come in sending command
  2627. * state, we should notify the driver to terminate current
  2628. * transfer and report a command timeout to the core.
  2629. */
  2630. host->cmd_status = SDMMC_INT_RTO;
  2631. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2632. tasklet_schedule(&host->tasklet);
  2633. break;
  2634. default:
  2635. dev_warn(host->dev, "Unexpected command timeout, state %d\n",
  2636. host->state);
  2637. break;
  2638. }
  2639. exit:
  2640. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2641. }
  2642. static void dw_mci_dto_timer(struct timer_list *t)
  2643. {
  2644. struct dw_mci *host = from_timer(host, t, dto_timer);
  2645. unsigned long irqflags;
  2646. u32 pending;
  2647. spin_lock_irqsave(&host->irq_lock, irqflags);
  2648. /*
  2649. * The DTO timer is much longer than the CTO timer, so it's even less
  2650. * likely that we'll these cases, but it pays to be paranoid.
  2651. */
  2652. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  2653. if (pending & SDMMC_INT_DATA_OVER) {
  2654. /* The interrupt should fire; no need to act but we can warn */
  2655. dev_warn(host->dev, "Unexpected data interrupt latency\n");
  2656. goto exit;
  2657. }
  2658. if (test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) {
  2659. /* Presumably interrupt handler couldn't delete the timer */
  2660. dev_warn(host->dev, "DTO timeout when already completed\n");
  2661. goto exit;
  2662. }
  2663. /*
  2664. * Continued paranoia to make sure we're in the state we expect.
  2665. * This paranoia isn't really justified but it seems good to be safe.
  2666. */
  2667. switch (host->state) {
  2668. case STATE_SENDING_DATA:
  2669. case STATE_DATA_BUSY:
  2670. /*
  2671. * If DTO interrupt does NOT come in sending data state,
  2672. * we should notify the driver to terminate current transfer
  2673. * and report a data timeout to the core.
  2674. */
  2675. host->data_status = SDMMC_INT_DRTO;
  2676. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  2677. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  2678. tasklet_schedule(&host->tasklet);
  2679. break;
  2680. default:
  2681. dev_warn(host->dev, "Unexpected data timeout, state %d\n",
  2682. host->state);
  2683. break;
  2684. }
  2685. exit:
  2686. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2687. }
  2688. #ifdef CONFIG_OF
  2689. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2690. {
  2691. struct dw_mci_board *pdata;
  2692. struct device *dev = host->dev;
  2693. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2694. int ret;
  2695. u32 clock_frequency;
  2696. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2697. if (!pdata)
  2698. return ERR_PTR(-ENOMEM);
  2699. /* find reset controller when exist */
  2700. pdata->rstc = devm_reset_control_get_optional_exclusive(dev, "reset");
  2701. if (IS_ERR(pdata->rstc))
  2702. return ERR_CAST(pdata->rstc);
  2703. if (device_property_read_u32(dev, "fifo-depth", &pdata->fifo_depth))
  2704. dev_info(dev,
  2705. "fifo-depth property not found, using value of FIFOTH register as default\n");
  2706. device_property_read_u32(dev, "card-detect-delay",
  2707. &pdata->detect_delay_ms);
  2708. device_property_read_u32(dev, "data-addr", &host->data_addr_override);
  2709. if (device_property_present(dev, "fifo-watermark-aligned"))
  2710. host->wm_aligned = true;
  2711. if (!device_property_read_u32(dev, "clock-frequency", &clock_frequency))
  2712. pdata->bus_hz = clock_frequency;
  2713. if (drv_data && drv_data->parse_dt) {
  2714. ret = drv_data->parse_dt(host);
  2715. if (ret)
  2716. return ERR_PTR(ret);
  2717. }
  2718. return pdata;
  2719. }
  2720. #else /* CONFIG_OF */
  2721. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2722. {
  2723. return ERR_PTR(-EINVAL);
  2724. }
  2725. #endif /* CONFIG_OF */
  2726. static void dw_mci_enable_cd(struct dw_mci *host)
  2727. {
  2728. unsigned long irqflags;
  2729. u32 temp;
  2730. /*
  2731. * No need for CD if all slots have a non-error GPIO
  2732. * as well as broken card detection is found.
  2733. */
  2734. if (host->slot->mmc->caps & MMC_CAP_NEEDS_POLL)
  2735. return;
  2736. if (mmc_gpio_get_cd(host->slot->mmc) < 0) {
  2737. spin_lock_irqsave(&host->irq_lock, irqflags);
  2738. temp = mci_readl(host, INTMASK);
  2739. temp |= SDMMC_INT_CD;
  2740. mci_writel(host, INTMASK, temp);
  2741. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2742. }
  2743. }
  2744. int dw_mci_probe(struct dw_mci *host)
  2745. {
  2746. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2747. int width, i, ret = 0;
  2748. u32 fifo_size;
  2749. if (!host->pdata) {
  2750. host->pdata = dw_mci_parse_dt(host);
  2751. if (IS_ERR(host->pdata))
  2752. return dev_err_probe(host->dev, PTR_ERR(host->pdata),
  2753. "platform data not available\n");
  2754. }
  2755. host->biu_clk = devm_clk_get(host->dev, "biu");
  2756. if (IS_ERR(host->biu_clk)) {
  2757. dev_dbg(host->dev, "biu clock not available\n");
  2758. } else {
  2759. ret = clk_prepare_enable(host->biu_clk);
  2760. if (ret) {
  2761. dev_err(host->dev, "failed to enable biu clock\n");
  2762. return ret;
  2763. }
  2764. }
  2765. host->ciu_clk = devm_clk_get(host->dev, "ciu");
  2766. if (IS_ERR(host->ciu_clk)) {
  2767. dev_dbg(host->dev, "ciu clock not available\n");
  2768. host->bus_hz = host->pdata->bus_hz;
  2769. } else {
  2770. ret = clk_prepare_enable(host->ciu_clk);
  2771. if (ret) {
  2772. dev_err(host->dev, "failed to enable ciu clock\n");
  2773. goto err_clk_biu;
  2774. }
  2775. if (host->pdata->bus_hz) {
  2776. ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
  2777. if (ret)
  2778. dev_warn(host->dev,
  2779. "Unable to set bus rate to %uHz\n",
  2780. host->pdata->bus_hz);
  2781. }
  2782. host->bus_hz = clk_get_rate(host->ciu_clk);
  2783. }
  2784. if (!host->bus_hz) {
  2785. dev_err(host->dev,
  2786. "Platform data must supply bus speed\n");
  2787. ret = -ENODEV;
  2788. goto err_clk_ciu;
  2789. }
  2790. if (host->pdata->rstc) {
  2791. reset_control_assert(host->pdata->rstc);
  2792. usleep_range(10, 50);
  2793. reset_control_deassert(host->pdata->rstc);
  2794. }
  2795. if (drv_data && drv_data->init) {
  2796. ret = drv_data->init(host);
  2797. if (ret) {
  2798. dev_err(host->dev,
  2799. "implementation specific init failed\n");
  2800. goto err_clk_ciu;
  2801. }
  2802. }
  2803. timer_setup(&host->cmd11_timer, dw_mci_cmd11_timer, 0);
  2804. timer_setup(&host->cto_timer, dw_mci_cto_timer, 0);
  2805. timer_setup(&host->dto_timer, dw_mci_dto_timer, 0);
  2806. spin_lock_init(&host->lock);
  2807. spin_lock_init(&host->irq_lock);
  2808. INIT_LIST_HEAD(&host->queue);
  2809. dw_mci_init_fault(host);
  2810. /*
  2811. * Get the host data width - this assumes that HCON has been set with
  2812. * the correct values.
  2813. */
  2814. i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
  2815. if (!i) {
  2816. host->push_data = dw_mci_push_data16;
  2817. host->pull_data = dw_mci_pull_data16;
  2818. width = 16;
  2819. host->data_shift = 1;
  2820. } else if (i == 2) {
  2821. host->push_data = dw_mci_push_data64;
  2822. host->pull_data = dw_mci_pull_data64;
  2823. width = 64;
  2824. host->data_shift = 3;
  2825. } else {
  2826. /* Check for a reserved value, and warn if it is */
  2827. WARN((i != 1),
  2828. "HCON reports a reserved host data width!\n"
  2829. "Defaulting to 32-bit access.\n");
  2830. host->push_data = dw_mci_push_data32;
  2831. host->pull_data = dw_mci_pull_data32;
  2832. width = 32;
  2833. host->data_shift = 2;
  2834. }
  2835. /* Reset all blocks */
  2836. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2837. ret = -ENODEV;
  2838. goto err_clk_ciu;
  2839. }
  2840. host->dma_ops = host->pdata->dma_ops;
  2841. dw_mci_init_dma(host);
  2842. /* Clear the interrupts for the host controller */
  2843. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2844. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2845. /* Put in max timeout */
  2846. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2847. /*
  2848. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  2849. * Tx Mark = fifo_size / 2 DMA Size = 8
  2850. */
  2851. if (!host->pdata->fifo_depth) {
  2852. /*
  2853. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  2854. * have been overwritten by the bootloader, just like we're
  2855. * about to do, so if you know the value for your hardware, you
  2856. * should put it in the platform data.
  2857. */
  2858. fifo_size = mci_readl(host, FIFOTH);
  2859. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  2860. } else {
  2861. fifo_size = host->pdata->fifo_depth;
  2862. }
  2863. host->fifo_depth = fifo_size;
  2864. host->fifoth_val =
  2865. SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
  2866. mci_writel(host, FIFOTH, host->fifoth_val);
  2867. /* disable clock to CIU */
  2868. mci_writel(host, CLKENA, 0);
  2869. mci_writel(host, CLKSRC, 0);
  2870. /*
  2871. * In 2.40a spec, Data offset is changed.
  2872. * Need to check the version-id and set data-offset for DATA register.
  2873. */
  2874. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  2875. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  2876. if (host->data_addr_override)
  2877. host->fifo_reg = host->regs + host->data_addr_override;
  2878. else if (host->verid < DW_MMC_240A)
  2879. host->fifo_reg = host->regs + DATA_OFFSET;
  2880. else
  2881. host->fifo_reg = host->regs + DATA_240A_OFFSET;
  2882. tasklet_setup(&host->tasklet, dw_mci_tasklet_func);
  2883. ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
  2884. host->irq_flags, "dw-mci", host);
  2885. if (ret)
  2886. goto err_dmaunmap;
  2887. /*
  2888. * Enable interrupts for command done, data over, data empty,
  2889. * receive ready and error such as transmit, receive timeout, crc error
  2890. */
  2891. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2892. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2893. DW_MCI_ERROR_FLAGS);
  2894. /* Enable mci interrupt */
  2895. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2896. dev_info(host->dev,
  2897. "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
  2898. host->irq, width, fifo_size);
  2899. /* We need at least one slot to succeed */
  2900. ret = dw_mci_init_slot(host);
  2901. if (ret) {
  2902. dev_dbg(host->dev, "slot %d init failed\n", i);
  2903. goto err_dmaunmap;
  2904. }
  2905. /* Now that slots are all setup, we can enable card detect */
  2906. dw_mci_enable_cd(host);
  2907. return 0;
  2908. err_dmaunmap:
  2909. if (host->use_dma && host->dma_ops->exit)
  2910. host->dma_ops->exit(host);
  2911. reset_control_assert(host->pdata->rstc);
  2912. err_clk_ciu:
  2913. clk_disable_unprepare(host->ciu_clk);
  2914. err_clk_biu:
  2915. clk_disable_unprepare(host->biu_clk);
  2916. return ret;
  2917. }
  2918. EXPORT_SYMBOL(dw_mci_probe);
  2919. void dw_mci_remove(struct dw_mci *host)
  2920. {
  2921. dev_dbg(host->dev, "remove slot\n");
  2922. if (host->slot)
  2923. dw_mci_cleanup_slot(host->slot);
  2924. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2925. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2926. /* disable clock to CIU */
  2927. mci_writel(host, CLKENA, 0);
  2928. mci_writel(host, CLKSRC, 0);
  2929. if (host->use_dma && host->dma_ops->exit)
  2930. host->dma_ops->exit(host);
  2931. reset_control_assert(host->pdata->rstc);
  2932. clk_disable_unprepare(host->ciu_clk);
  2933. clk_disable_unprepare(host->biu_clk);
  2934. }
  2935. EXPORT_SYMBOL(dw_mci_remove);
  2936. #ifdef CONFIG_PM
  2937. int dw_mci_runtime_suspend(struct device *dev)
  2938. {
  2939. struct dw_mci *host = dev_get_drvdata(dev);
  2940. if (host->use_dma && host->dma_ops->exit)
  2941. host->dma_ops->exit(host);
  2942. clk_disable_unprepare(host->ciu_clk);
  2943. if (host->slot &&
  2944. (mmc_can_gpio_cd(host->slot->mmc) ||
  2945. !mmc_card_is_removable(host->slot->mmc)))
  2946. clk_disable_unprepare(host->biu_clk);
  2947. return 0;
  2948. }
  2949. EXPORT_SYMBOL(dw_mci_runtime_suspend);
  2950. int dw_mci_runtime_resume(struct device *dev)
  2951. {
  2952. int ret = 0;
  2953. struct dw_mci *host = dev_get_drvdata(dev);
  2954. if (host->slot &&
  2955. (mmc_can_gpio_cd(host->slot->mmc) ||
  2956. !mmc_card_is_removable(host->slot->mmc))) {
  2957. ret = clk_prepare_enable(host->biu_clk);
  2958. if (ret)
  2959. return ret;
  2960. }
  2961. ret = clk_prepare_enable(host->ciu_clk);
  2962. if (ret)
  2963. goto err;
  2964. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2965. clk_disable_unprepare(host->ciu_clk);
  2966. ret = -ENODEV;
  2967. goto err;
  2968. }
  2969. if (host->use_dma && host->dma_ops->init)
  2970. host->dma_ops->init(host);
  2971. /*
  2972. * Restore the initial value at FIFOTH register
  2973. * And Invalidate the prev_blksz with zero
  2974. */
  2975. mci_writel(host, FIFOTH, host->fifoth_val);
  2976. host->prev_blksz = 0;
  2977. /* Put in max timeout */
  2978. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2979. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2980. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2981. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2982. DW_MCI_ERROR_FLAGS);
  2983. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2984. if (host->slot && host->slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
  2985. dw_mci_set_ios(host->slot->mmc, &host->slot->mmc->ios);
  2986. /* Force setup bus to guarantee available clock output */
  2987. dw_mci_setup_bus(host->slot, true);
  2988. /* Re-enable SDIO interrupts. */
  2989. if (sdio_irq_claimed(host->slot->mmc))
  2990. __dw_mci_enable_sdio_irq(host->slot, 1);
  2991. /* Now that slots are all setup, we can enable card detect */
  2992. dw_mci_enable_cd(host);
  2993. return 0;
  2994. err:
  2995. if (host->slot &&
  2996. (mmc_can_gpio_cd(host->slot->mmc) ||
  2997. !mmc_card_is_removable(host->slot->mmc)))
  2998. clk_disable_unprepare(host->biu_clk);
  2999. return ret;
  3000. }
  3001. EXPORT_SYMBOL(dw_mci_runtime_resume);
  3002. #endif /* CONFIG_PM */
  3003. static int __init dw_mci_init(void)
  3004. {
  3005. pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
  3006. return 0;
  3007. }
  3008. static void __exit dw_mci_exit(void)
  3009. {
  3010. }
  3011. module_init(dw_mci_init);
  3012. module_exit(dw_mci_exit);
  3013. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  3014. MODULE_AUTHOR("NXP Semiconductor VietNam");
  3015. MODULE_AUTHOR("Imagination Technologies Ltd");
  3016. MODULE_LICENSE("GPL v2");