dw_mmc.c 72 KB

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