dw_mmc.c 68 KB

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