dw_mmc.c 84 KB

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