dw_mmc.c 82 KB

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