dw_mmc.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763
  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->data_status = 0;
  699. data = cmd->data;
  700. if (data) {
  701. dw_mci_set_timeout(host);
  702. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  703. mci_writel(host, BLKSIZ, data->blksz);
  704. }
  705. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  706. /* this is the first command, send the initialization clock */
  707. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  708. cmdflags |= SDMMC_CMD_INIT;
  709. if (data) {
  710. dw_mci_submit_data(host, data);
  711. wmb();
  712. }
  713. dw_mci_start_command(host, cmd, cmdflags);
  714. if (mrq->stop)
  715. host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
  716. else
  717. host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
  718. }
  719. static void dw_mci_start_request(struct dw_mci *host,
  720. struct dw_mci_slot *slot)
  721. {
  722. struct mmc_request *mrq = slot->mrq;
  723. struct mmc_command *cmd;
  724. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  725. __dw_mci_start_request(host, slot, cmd);
  726. }
  727. /* must be called with host->lock held */
  728. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  729. struct mmc_request *mrq)
  730. {
  731. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  732. host->state);
  733. slot->mrq = mrq;
  734. if (host->state == STATE_IDLE) {
  735. host->state = STATE_SENDING_CMD;
  736. dw_mci_start_request(host, slot);
  737. } else {
  738. list_add_tail(&slot->queue_node, &host->queue);
  739. }
  740. }
  741. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  742. {
  743. struct dw_mci_slot *slot = mmc_priv(mmc);
  744. struct dw_mci *host = slot->host;
  745. WARN_ON(slot->mrq);
  746. /*
  747. * The check for card presence and queueing of the request must be
  748. * atomic, otherwise the card could be removed in between and the
  749. * request wouldn't fail until another card was inserted.
  750. */
  751. spin_lock_bh(&host->lock);
  752. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  753. spin_unlock_bh(&host->lock);
  754. mrq->cmd->error = -ENOMEDIUM;
  755. mmc_request_done(mmc, mrq);
  756. return;
  757. }
  758. dw_mci_queue_request(host, slot, mrq);
  759. spin_unlock_bh(&host->lock);
  760. }
  761. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  762. {
  763. struct dw_mci_slot *slot = mmc_priv(mmc);
  764. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  765. u32 regs;
  766. switch (ios->bus_width) {
  767. case MMC_BUS_WIDTH_4:
  768. slot->ctype = SDMMC_CTYPE_4BIT;
  769. break;
  770. case MMC_BUS_WIDTH_8:
  771. slot->ctype = SDMMC_CTYPE_8BIT;
  772. break;
  773. default:
  774. /* set default 1 bit mode */
  775. slot->ctype = SDMMC_CTYPE_1BIT;
  776. }
  777. regs = mci_readl(slot->host, UHS_REG);
  778. /* DDR mode set */
  779. if (ios->timing == MMC_TIMING_UHS_DDR50)
  780. regs |= ((0x1 << slot->id) << 16);
  781. else
  782. regs &= ~((0x1 << slot->id) << 16);
  783. mci_writel(slot->host, UHS_REG, regs);
  784. slot->host->timing = ios->timing;
  785. /*
  786. * Use mirror of ios->clock to prevent race with mmc
  787. * core ios update when finding the minimum.
  788. */
  789. slot->clock = ios->clock;
  790. if (drv_data && drv_data->set_ios)
  791. drv_data->set_ios(slot->host, ios);
  792. /* Slot specific timing and width adjustment */
  793. dw_mci_setup_bus(slot, false);
  794. switch (ios->power_mode) {
  795. case MMC_POWER_UP:
  796. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  797. /* Power up slot */
  798. if (slot->host->pdata->setpower)
  799. slot->host->pdata->setpower(slot->id, mmc->ocr_avail);
  800. regs = mci_readl(slot->host, PWREN);
  801. regs |= (1 << slot->id);
  802. mci_writel(slot->host, PWREN, regs);
  803. break;
  804. case MMC_POWER_OFF:
  805. /* Power down slot */
  806. if (slot->host->pdata->setpower)
  807. slot->host->pdata->setpower(slot->id, 0);
  808. regs = mci_readl(slot->host, PWREN);
  809. regs &= ~(1 << slot->id);
  810. mci_writel(slot->host, PWREN, regs);
  811. break;
  812. default:
  813. break;
  814. }
  815. }
  816. static int dw_mci_get_ro(struct mmc_host *mmc)
  817. {
  818. int read_only;
  819. struct dw_mci_slot *slot = mmc_priv(mmc);
  820. struct dw_mci_board *brd = slot->host->pdata;
  821. /* Use platform get_ro function, else try on board write protect */
  822. if (slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT)
  823. read_only = 0;
  824. else if (brd->get_ro)
  825. read_only = brd->get_ro(slot->id);
  826. else if (gpio_is_valid(slot->wp_gpio))
  827. read_only = gpio_get_value(slot->wp_gpio);
  828. else
  829. read_only =
  830. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  831. dev_dbg(&mmc->class_dev, "card is %s\n",
  832. read_only ? "read-only" : "read-write");
  833. return read_only;
  834. }
  835. static int dw_mci_get_cd(struct mmc_host *mmc)
  836. {
  837. int present;
  838. struct dw_mci_slot *slot = mmc_priv(mmc);
  839. struct dw_mci_board *brd = slot->host->pdata;
  840. /* Use platform get_cd function, else try onboard card detect */
  841. if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  842. present = 1;
  843. else if (brd->get_cd)
  844. present = !brd->get_cd(slot->id);
  845. else
  846. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  847. == 0 ? 1 : 0;
  848. if (present)
  849. dev_dbg(&mmc->class_dev, "card is present\n");
  850. else
  851. dev_dbg(&mmc->class_dev, "card is not present\n");
  852. return present;
  853. }
  854. /*
  855. * Disable lower power mode.
  856. *
  857. * Low power mode will stop the card clock when idle. According to the
  858. * description of the CLKENA register we should disable low power mode
  859. * for SDIO cards if we need SDIO interrupts to work.
  860. *
  861. * This function is fast if low power mode is already disabled.
  862. */
  863. static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
  864. {
  865. struct dw_mci *host = slot->host;
  866. u32 clk_en_a;
  867. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  868. clk_en_a = mci_readl(host, CLKENA);
  869. if (clk_en_a & clken_low_pwr) {
  870. mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
  871. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  872. SDMMC_CMD_PRV_DAT_WAIT, 0);
  873. }
  874. }
  875. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  876. {
  877. struct dw_mci_slot *slot = mmc_priv(mmc);
  878. struct dw_mci *host = slot->host;
  879. u32 int_mask;
  880. /* Enable/disable Slot Specific SDIO interrupt */
  881. int_mask = mci_readl(host, INTMASK);
  882. if (enb) {
  883. /*
  884. * Turn off low power mode if it was enabled. This is a bit of
  885. * a heavy operation and we disable / enable IRQs a lot, so
  886. * we'll leave low power mode disabled and it will get
  887. * re-enabled again in dw_mci_setup_bus().
  888. */
  889. dw_mci_disable_low_power(slot);
  890. mci_writel(host, INTMASK,
  891. (int_mask | SDMMC_INT_SDIO(slot->id)));
  892. } else {
  893. mci_writel(host, INTMASK,
  894. (int_mask & ~SDMMC_INT_SDIO(slot->id)));
  895. }
  896. }
  897. static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  898. {
  899. struct dw_mci_slot *slot = mmc_priv(mmc);
  900. struct dw_mci *host = slot->host;
  901. const struct dw_mci_drv_data *drv_data = host->drv_data;
  902. struct dw_mci_tuning_data tuning_data;
  903. int err = -ENOSYS;
  904. if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
  905. if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
  906. tuning_data.blk_pattern = tuning_blk_pattern_8bit;
  907. tuning_data.blksz = sizeof(tuning_blk_pattern_8bit);
  908. } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
  909. tuning_data.blk_pattern = tuning_blk_pattern_4bit;
  910. tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
  911. } else {
  912. return -EINVAL;
  913. }
  914. } else if (opcode == MMC_SEND_TUNING_BLOCK) {
  915. tuning_data.blk_pattern = tuning_blk_pattern_4bit;
  916. tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
  917. } else {
  918. dev_err(host->dev,
  919. "Undefined command(%d) for tuning\n", opcode);
  920. return -EINVAL;
  921. }
  922. if (drv_data && drv_data->execute_tuning)
  923. err = drv_data->execute_tuning(slot, opcode, &tuning_data);
  924. return err;
  925. }
  926. static const struct mmc_host_ops dw_mci_ops = {
  927. .request = dw_mci_request,
  928. .pre_req = dw_mci_pre_req,
  929. .post_req = dw_mci_post_req,
  930. .set_ios = dw_mci_set_ios,
  931. .get_ro = dw_mci_get_ro,
  932. .get_cd = dw_mci_get_cd,
  933. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  934. .execute_tuning = dw_mci_execute_tuning,
  935. };
  936. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  937. __releases(&host->lock)
  938. __acquires(&host->lock)
  939. {
  940. struct dw_mci_slot *slot;
  941. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  942. WARN_ON(host->cmd || host->data);
  943. host->cur_slot->mrq = NULL;
  944. host->mrq = NULL;
  945. if (!list_empty(&host->queue)) {
  946. slot = list_entry(host->queue.next,
  947. struct dw_mci_slot, queue_node);
  948. list_del(&slot->queue_node);
  949. dev_vdbg(host->dev, "list not empty: %s is next\n",
  950. mmc_hostname(slot->mmc));
  951. host->state = STATE_SENDING_CMD;
  952. dw_mci_start_request(host, slot);
  953. } else {
  954. dev_vdbg(host->dev, "list empty\n");
  955. host->state = STATE_IDLE;
  956. }
  957. spin_unlock(&host->lock);
  958. mmc_request_done(prev_mmc, mrq);
  959. spin_lock(&host->lock);
  960. }
  961. static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  962. {
  963. u32 status = host->cmd_status;
  964. host->cmd_status = 0;
  965. /* Read the response from the card (up to 16 bytes) */
  966. if (cmd->flags & MMC_RSP_PRESENT) {
  967. if (cmd->flags & MMC_RSP_136) {
  968. cmd->resp[3] = mci_readl(host, RESP0);
  969. cmd->resp[2] = mci_readl(host, RESP1);
  970. cmd->resp[1] = mci_readl(host, RESP2);
  971. cmd->resp[0] = mci_readl(host, RESP3);
  972. } else {
  973. cmd->resp[0] = mci_readl(host, RESP0);
  974. cmd->resp[1] = 0;
  975. cmd->resp[2] = 0;
  976. cmd->resp[3] = 0;
  977. }
  978. }
  979. if (status & SDMMC_INT_RTO)
  980. cmd->error = -ETIMEDOUT;
  981. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  982. cmd->error = -EILSEQ;
  983. else if (status & SDMMC_INT_RESP_ERR)
  984. cmd->error = -EIO;
  985. else
  986. cmd->error = 0;
  987. if (cmd->error) {
  988. /* newer ip versions need a delay between retries */
  989. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  990. mdelay(20);
  991. }
  992. }
  993. static void dw_mci_tasklet_func(unsigned long priv)
  994. {
  995. struct dw_mci *host = (struct dw_mci *)priv;
  996. struct mmc_data *data;
  997. struct mmc_command *cmd;
  998. enum dw_mci_state state;
  999. enum dw_mci_state prev_state;
  1000. u32 status, ctrl;
  1001. spin_lock(&host->lock);
  1002. state = host->state;
  1003. data = host->data;
  1004. do {
  1005. prev_state = state;
  1006. switch (state) {
  1007. case STATE_IDLE:
  1008. break;
  1009. case STATE_SENDING_CMD:
  1010. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1011. &host->pending_events))
  1012. break;
  1013. cmd = host->cmd;
  1014. host->cmd = NULL;
  1015. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  1016. dw_mci_command_complete(host, cmd);
  1017. if (cmd == host->mrq->sbc && !cmd->error) {
  1018. prev_state = state = STATE_SENDING_CMD;
  1019. __dw_mci_start_request(host, host->cur_slot,
  1020. host->mrq->cmd);
  1021. goto unlock;
  1022. }
  1023. if (cmd->data && cmd->error) {
  1024. dw_mci_stop_dma(host);
  1025. send_stop_abort(host, data);
  1026. state = STATE_SENDING_STOP;
  1027. break;
  1028. }
  1029. if (!host->mrq->data || cmd->error) {
  1030. dw_mci_request_end(host, host->mrq);
  1031. goto unlock;
  1032. }
  1033. prev_state = state = STATE_SENDING_DATA;
  1034. /* fall through */
  1035. case STATE_SENDING_DATA:
  1036. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1037. &host->pending_events)) {
  1038. dw_mci_stop_dma(host);
  1039. send_stop_abort(host, data);
  1040. state = STATE_DATA_ERROR;
  1041. break;
  1042. }
  1043. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1044. &host->pending_events))
  1045. break;
  1046. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  1047. prev_state = state = STATE_DATA_BUSY;
  1048. /* fall through */
  1049. case STATE_DATA_BUSY:
  1050. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  1051. &host->pending_events))
  1052. break;
  1053. host->data = NULL;
  1054. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  1055. status = host->data_status;
  1056. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1057. if (status & SDMMC_INT_DRTO) {
  1058. data->error = -ETIMEDOUT;
  1059. } else if (status & SDMMC_INT_DCRC) {
  1060. data->error = -EILSEQ;
  1061. } else if (status & SDMMC_INT_EBE &&
  1062. host->dir_status ==
  1063. DW_MCI_SEND_STATUS) {
  1064. /*
  1065. * No data CRC status was returned.
  1066. * The number of bytes transferred will
  1067. * be exaggerated in PIO mode.
  1068. */
  1069. data->bytes_xfered = 0;
  1070. data->error = -ETIMEDOUT;
  1071. } else {
  1072. dev_err(host->dev,
  1073. "data FIFO error "
  1074. "(status=%08x)\n",
  1075. status);
  1076. data->error = -EIO;
  1077. }
  1078. /*
  1079. * After an error, there may be data lingering
  1080. * in the FIFO, so reset it - doing so
  1081. * generates a block interrupt, hence setting
  1082. * the scatter-gather pointer to NULL.
  1083. */
  1084. sg_miter_stop(&host->sg_miter);
  1085. host->sg = NULL;
  1086. ctrl = mci_readl(host, CTRL);
  1087. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1088. mci_writel(host, CTRL, ctrl);
  1089. } else {
  1090. data->bytes_xfered = data->blocks * data->blksz;
  1091. data->error = 0;
  1092. }
  1093. if (!data->stop && !data->error) {
  1094. dw_mci_request_end(host, host->mrq);
  1095. goto unlock;
  1096. }
  1097. if (host->mrq->sbc && !data->error) {
  1098. data->stop->error = 0;
  1099. dw_mci_request_end(host, host->mrq);
  1100. goto unlock;
  1101. }
  1102. prev_state = state = STATE_SENDING_STOP;
  1103. if (data->stop && !data->error) {
  1104. /* stop command for open-ended transfer*/
  1105. send_stop_abort(host, data);
  1106. }
  1107. /* fall through */
  1108. case STATE_SENDING_STOP:
  1109. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1110. &host->pending_events))
  1111. break;
  1112. /* CMD error in data command */
  1113. if (host->mrq->cmd->error && host->mrq->data) {
  1114. sg_miter_stop(&host->sg_miter);
  1115. host->sg = NULL;
  1116. ctrl = mci_readl(host, CTRL);
  1117. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1118. mci_writel(host, CTRL, ctrl);
  1119. }
  1120. host->cmd = NULL;
  1121. host->data = NULL;
  1122. if (host->mrq->stop)
  1123. dw_mci_command_complete(host, host->mrq->stop);
  1124. else
  1125. host->cmd_status = 0;
  1126. dw_mci_request_end(host, host->mrq);
  1127. goto unlock;
  1128. case STATE_DATA_ERROR:
  1129. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1130. &host->pending_events))
  1131. break;
  1132. state = STATE_DATA_BUSY;
  1133. break;
  1134. }
  1135. } while (state != prev_state);
  1136. host->state = state;
  1137. unlock:
  1138. spin_unlock(&host->lock);
  1139. }
  1140. /* push final bytes to part_buf, only use during push */
  1141. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1142. {
  1143. memcpy((void *)&host->part_buf, buf, cnt);
  1144. host->part_buf_count = cnt;
  1145. }
  1146. /* append bytes to part_buf, only use during push */
  1147. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1148. {
  1149. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  1150. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  1151. host->part_buf_count += cnt;
  1152. return cnt;
  1153. }
  1154. /* pull first bytes from part_buf, only use during pull */
  1155. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1156. {
  1157. cnt = min(cnt, (int)host->part_buf_count);
  1158. if (cnt) {
  1159. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  1160. cnt);
  1161. host->part_buf_count -= cnt;
  1162. host->part_buf_start += cnt;
  1163. }
  1164. return cnt;
  1165. }
  1166. /* pull final bytes from the part_buf, assuming it's just been filled */
  1167. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  1168. {
  1169. memcpy(buf, &host->part_buf, cnt);
  1170. host->part_buf_start = cnt;
  1171. host->part_buf_count = (1 << host->data_shift) - cnt;
  1172. }
  1173. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  1174. {
  1175. struct mmc_data *data = host->data;
  1176. int init_cnt = cnt;
  1177. /* try and push anything in the part_buf */
  1178. if (unlikely(host->part_buf_count)) {
  1179. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1180. buf += len;
  1181. cnt -= len;
  1182. if (host->part_buf_count == 2) {
  1183. mci_writew(host, DATA(host->data_offset),
  1184. host->part_buf16);
  1185. host->part_buf_count = 0;
  1186. }
  1187. }
  1188. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1189. if (unlikely((unsigned long)buf & 0x1)) {
  1190. while (cnt >= 2) {
  1191. u16 aligned_buf[64];
  1192. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1193. int items = len >> 1;
  1194. int i;
  1195. /* memcpy from input buffer into aligned buffer */
  1196. memcpy(aligned_buf, buf, len);
  1197. buf += len;
  1198. cnt -= len;
  1199. /* push data from aligned buffer into fifo */
  1200. for (i = 0; i < items; ++i)
  1201. mci_writew(host, DATA(host->data_offset),
  1202. aligned_buf[i]);
  1203. }
  1204. } else
  1205. #endif
  1206. {
  1207. u16 *pdata = buf;
  1208. for (; cnt >= 2; cnt -= 2)
  1209. mci_writew(host, DATA(host->data_offset), *pdata++);
  1210. buf = pdata;
  1211. }
  1212. /* put anything remaining in the part_buf */
  1213. if (cnt) {
  1214. dw_mci_set_part_bytes(host, buf, cnt);
  1215. /* Push data if we have reached the expected data length */
  1216. if ((data->bytes_xfered + init_cnt) ==
  1217. (data->blksz * data->blocks))
  1218. mci_writew(host, DATA(host->data_offset),
  1219. host->part_buf16);
  1220. }
  1221. }
  1222. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1223. {
  1224. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1225. if (unlikely((unsigned long)buf & 0x1)) {
  1226. while (cnt >= 2) {
  1227. /* pull data from fifo into aligned buffer */
  1228. u16 aligned_buf[64];
  1229. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1230. int items = len >> 1;
  1231. int i;
  1232. for (i = 0; i < items; ++i)
  1233. aligned_buf[i] = mci_readw(host,
  1234. DATA(host->data_offset));
  1235. /* memcpy from aligned buffer into output buffer */
  1236. memcpy(buf, aligned_buf, len);
  1237. buf += len;
  1238. cnt -= len;
  1239. }
  1240. } else
  1241. #endif
  1242. {
  1243. u16 *pdata = buf;
  1244. for (; cnt >= 2; cnt -= 2)
  1245. *pdata++ = mci_readw(host, DATA(host->data_offset));
  1246. buf = pdata;
  1247. }
  1248. if (cnt) {
  1249. host->part_buf16 = mci_readw(host, DATA(host->data_offset));
  1250. dw_mci_pull_final_bytes(host, buf, cnt);
  1251. }
  1252. }
  1253. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  1254. {
  1255. struct mmc_data *data = host->data;
  1256. int init_cnt = cnt;
  1257. /* try and push anything in the part_buf */
  1258. if (unlikely(host->part_buf_count)) {
  1259. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1260. buf += len;
  1261. cnt -= len;
  1262. if (host->part_buf_count == 4) {
  1263. mci_writel(host, DATA(host->data_offset),
  1264. host->part_buf32);
  1265. host->part_buf_count = 0;
  1266. }
  1267. }
  1268. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1269. if (unlikely((unsigned long)buf & 0x3)) {
  1270. while (cnt >= 4) {
  1271. u32 aligned_buf[32];
  1272. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1273. int items = len >> 2;
  1274. int i;
  1275. /* memcpy from input buffer into aligned buffer */
  1276. memcpy(aligned_buf, buf, len);
  1277. buf += len;
  1278. cnt -= len;
  1279. /* push data from aligned buffer into fifo */
  1280. for (i = 0; i < items; ++i)
  1281. mci_writel(host, DATA(host->data_offset),
  1282. aligned_buf[i]);
  1283. }
  1284. } else
  1285. #endif
  1286. {
  1287. u32 *pdata = buf;
  1288. for (; cnt >= 4; cnt -= 4)
  1289. mci_writel(host, DATA(host->data_offset), *pdata++);
  1290. buf = pdata;
  1291. }
  1292. /* put anything remaining in the part_buf */
  1293. if (cnt) {
  1294. dw_mci_set_part_bytes(host, buf, cnt);
  1295. /* Push data if we have reached the expected data length */
  1296. if ((data->bytes_xfered + init_cnt) ==
  1297. (data->blksz * data->blocks))
  1298. mci_writel(host, DATA(host->data_offset),
  1299. host->part_buf32);
  1300. }
  1301. }
  1302. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  1303. {
  1304. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1305. if (unlikely((unsigned long)buf & 0x3)) {
  1306. while (cnt >= 4) {
  1307. /* pull data from fifo into aligned buffer */
  1308. u32 aligned_buf[32];
  1309. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1310. int items = len >> 2;
  1311. int i;
  1312. for (i = 0; i < items; ++i)
  1313. aligned_buf[i] = mci_readl(host,
  1314. DATA(host->data_offset));
  1315. /* memcpy from aligned buffer into output buffer */
  1316. memcpy(buf, aligned_buf, len);
  1317. buf += len;
  1318. cnt -= len;
  1319. }
  1320. } else
  1321. #endif
  1322. {
  1323. u32 *pdata = buf;
  1324. for (; cnt >= 4; cnt -= 4)
  1325. *pdata++ = mci_readl(host, DATA(host->data_offset));
  1326. buf = pdata;
  1327. }
  1328. if (cnt) {
  1329. host->part_buf32 = mci_readl(host, DATA(host->data_offset));
  1330. dw_mci_pull_final_bytes(host, buf, cnt);
  1331. }
  1332. }
  1333. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1334. {
  1335. struct mmc_data *data = host->data;
  1336. int init_cnt = cnt;
  1337. /* try and push anything in the part_buf */
  1338. if (unlikely(host->part_buf_count)) {
  1339. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1340. buf += len;
  1341. cnt -= len;
  1342. if (host->part_buf_count == 8) {
  1343. mci_writeq(host, DATA(host->data_offset),
  1344. host->part_buf);
  1345. host->part_buf_count = 0;
  1346. }
  1347. }
  1348. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1349. if (unlikely((unsigned long)buf & 0x7)) {
  1350. while (cnt >= 8) {
  1351. u64 aligned_buf[16];
  1352. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1353. int items = len >> 3;
  1354. int i;
  1355. /* memcpy from input buffer into aligned buffer */
  1356. memcpy(aligned_buf, buf, len);
  1357. buf += len;
  1358. cnt -= len;
  1359. /* push data from aligned buffer into fifo */
  1360. for (i = 0; i < items; ++i)
  1361. mci_writeq(host, DATA(host->data_offset),
  1362. aligned_buf[i]);
  1363. }
  1364. } else
  1365. #endif
  1366. {
  1367. u64 *pdata = buf;
  1368. for (; cnt >= 8; cnt -= 8)
  1369. mci_writeq(host, DATA(host->data_offset), *pdata++);
  1370. buf = pdata;
  1371. }
  1372. /* put anything remaining in the part_buf */
  1373. if (cnt) {
  1374. dw_mci_set_part_bytes(host, buf, cnt);
  1375. /* Push data if we have reached the expected data length */
  1376. if ((data->bytes_xfered + init_cnt) ==
  1377. (data->blksz * data->blocks))
  1378. mci_writeq(host, DATA(host->data_offset),
  1379. host->part_buf);
  1380. }
  1381. }
  1382. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1383. {
  1384. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1385. if (unlikely((unsigned long)buf & 0x7)) {
  1386. while (cnt >= 8) {
  1387. /* pull data from fifo into aligned buffer */
  1388. u64 aligned_buf[16];
  1389. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1390. int items = len >> 3;
  1391. int i;
  1392. for (i = 0; i < items; ++i)
  1393. aligned_buf[i] = mci_readq(host,
  1394. DATA(host->data_offset));
  1395. /* memcpy from aligned buffer into output buffer */
  1396. memcpy(buf, aligned_buf, len);
  1397. buf += len;
  1398. cnt -= len;
  1399. }
  1400. } else
  1401. #endif
  1402. {
  1403. u64 *pdata = buf;
  1404. for (; cnt >= 8; cnt -= 8)
  1405. *pdata++ = mci_readq(host, DATA(host->data_offset));
  1406. buf = pdata;
  1407. }
  1408. if (cnt) {
  1409. host->part_buf = mci_readq(host, DATA(host->data_offset));
  1410. dw_mci_pull_final_bytes(host, buf, cnt);
  1411. }
  1412. }
  1413. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  1414. {
  1415. int len;
  1416. /* get remaining partial bytes */
  1417. len = dw_mci_pull_part_bytes(host, buf, cnt);
  1418. if (unlikely(len == cnt))
  1419. return;
  1420. buf += len;
  1421. cnt -= len;
  1422. /* get the rest of the data */
  1423. host->pull_data(host, buf, cnt);
  1424. }
  1425. static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
  1426. {
  1427. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1428. void *buf;
  1429. unsigned int offset;
  1430. struct mmc_data *data = host->data;
  1431. int shift = host->data_shift;
  1432. u32 status;
  1433. unsigned int len;
  1434. unsigned int remain, fcnt;
  1435. do {
  1436. if (!sg_miter_next(sg_miter))
  1437. goto done;
  1438. host->sg = sg_miter->piter.sg;
  1439. buf = sg_miter->addr;
  1440. remain = sg_miter->length;
  1441. offset = 0;
  1442. do {
  1443. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  1444. << shift) + host->part_buf_count;
  1445. len = min(remain, fcnt);
  1446. if (!len)
  1447. break;
  1448. dw_mci_pull_data(host, (void *)(buf + offset), len);
  1449. data->bytes_xfered += len;
  1450. offset += len;
  1451. remain -= len;
  1452. } while (remain);
  1453. sg_miter->consumed = offset;
  1454. status = mci_readl(host, MINTSTS);
  1455. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1456. /* if the RXDR is ready read again */
  1457. } while ((status & SDMMC_INT_RXDR) ||
  1458. (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
  1459. if (!remain) {
  1460. if (!sg_miter_next(sg_miter))
  1461. goto done;
  1462. sg_miter->consumed = 0;
  1463. }
  1464. sg_miter_stop(sg_miter);
  1465. return;
  1466. done:
  1467. sg_miter_stop(sg_miter);
  1468. host->sg = NULL;
  1469. smp_wmb();
  1470. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1471. }
  1472. static void dw_mci_write_data_pio(struct dw_mci *host)
  1473. {
  1474. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1475. void *buf;
  1476. unsigned int offset;
  1477. struct mmc_data *data = host->data;
  1478. int shift = host->data_shift;
  1479. u32 status;
  1480. unsigned int len;
  1481. unsigned int fifo_depth = host->fifo_depth;
  1482. unsigned int remain, fcnt;
  1483. do {
  1484. if (!sg_miter_next(sg_miter))
  1485. goto done;
  1486. host->sg = sg_miter->piter.sg;
  1487. buf = sg_miter->addr;
  1488. remain = sg_miter->length;
  1489. offset = 0;
  1490. do {
  1491. fcnt = ((fifo_depth -
  1492. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  1493. << shift) - host->part_buf_count;
  1494. len = min(remain, fcnt);
  1495. if (!len)
  1496. break;
  1497. host->push_data(host, (void *)(buf + offset), len);
  1498. data->bytes_xfered += len;
  1499. offset += len;
  1500. remain -= len;
  1501. } while (remain);
  1502. sg_miter->consumed = offset;
  1503. status = mci_readl(host, MINTSTS);
  1504. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1505. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  1506. if (!remain) {
  1507. if (!sg_miter_next(sg_miter))
  1508. goto done;
  1509. sg_miter->consumed = 0;
  1510. }
  1511. sg_miter_stop(sg_miter);
  1512. return;
  1513. done:
  1514. sg_miter_stop(sg_miter);
  1515. host->sg = NULL;
  1516. smp_wmb();
  1517. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1518. }
  1519. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  1520. {
  1521. if (!host->cmd_status)
  1522. host->cmd_status = status;
  1523. smp_wmb();
  1524. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1525. tasklet_schedule(&host->tasklet);
  1526. }
  1527. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  1528. {
  1529. struct dw_mci *host = dev_id;
  1530. u32 pending;
  1531. int i;
  1532. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  1533. /*
  1534. * DTO fix - version 2.10a and below, and only if internal DMA
  1535. * is configured.
  1536. */
  1537. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  1538. if (!pending &&
  1539. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  1540. pending |= SDMMC_INT_DATA_OVER;
  1541. }
  1542. if (pending) {
  1543. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  1544. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  1545. host->cmd_status = pending;
  1546. smp_wmb();
  1547. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1548. }
  1549. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  1550. /* if there is an error report DATA_ERROR */
  1551. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  1552. host->data_status = pending;
  1553. smp_wmb();
  1554. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1555. tasklet_schedule(&host->tasklet);
  1556. }
  1557. if (pending & SDMMC_INT_DATA_OVER) {
  1558. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  1559. if (!host->data_status)
  1560. host->data_status = pending;
  1561. smp_wmb();
  1562. if (host->dir_status == DW_MCI_RECV_STATUS) {
  1563. if (host->sg != NULL)
  1564. dw_mci_read_data_pio(host, true);
  1565. }
  1566. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1567. tasklet_schedule(&host->tasklet);
  1568. }
  1569. if (pending & SDMMC_INT_RXDR) {
  1570. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1571. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  1572. dw_mci_read_data_pio(host, false);
  1573. }
  1574. if (pending & SDMMC_INT_TXDR) {
  1575. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1576. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  1577. dw_mci_write_data_pio(host);
  1578. }
  1579. if (pending & SDMMC_INT_CMD_DONE) {
  1580. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1581. dw_mci_cmd_interrupt(host, pending);
  1582. }
  1583. if (pending & SDMMC_INT_CD) {
  1584. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1585. queue_work(host->card_workqueue, &host->card_work);
  1586. }
  1587. /* Handle SDIO Interrupts */
  1588. for (i = 0; i < host->num_slots; i++) {
  1589. struct dw_mci_slot *slot = host->slot[i];
  1590. if (pending & SDMMC_INT_SDIO(i)) {
  1591. mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i));
  1592. mmc_signal_sdio_irq(slot->mmc);
  1593. }
  1594. }
  1595. }
  1596. #ifdef CONFIG_MMC_DW_IDMAC
  1597. /* Handle DMA interrupts */
  1598. pending = mci_readl(host, IDSTS);
  1599. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1600. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
  1601. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1602. host->dma_ops->complete(host);
  1603. }
  1604. #endif
  1605. return IRQ_HANDLED;
  1606. }
  1607. static void dw_mci_work_routine_card(struct work_struct *work)
  1608. {
  1609. struct dw_mci *host = container_of(work, struct dw_mci, card_work);
  1610. int i;
  1611. for (i = 0; i < host->num_slots; i++) {
  1612. struct dw_mci_slot *slot = host->slot[i];
  1613. struct mmc_host *mmc = slot->mmc;
  1614. struct mmc_request *mrq;
  1615. int present;
  1616. u32 ctrl;
  1617. present = dw_mci_get_cd(mmc);
  1618. while (present != slot->last_detect_state) {
  1619. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1620. present ? "inserted" : "removed");
  1621. spin_lock_bh(&host->lock);
  1622. /* Card change detected */
  1623. slot->last_detect_state = present;
  1624. /* Mark card as present if applicable */
  1625. if (present != 0)
  1626. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1627. /* Clean up queue if present */
  1628. mrq = slot->mrq;
  1629. if (mrq) {
  1630. if (mrq == host->mrq) {
  1631. host->data = NULL;
  1632. host->cmd = NULL;
  1633. switch (host->state) {
  1634. case STATE_IDLE:
  1635. break;
  1636. case STATE_SENDING_CMD:
  1637. mrq->cmd->error = -ENOMEDIUM;
  1638. if (!mrq->data)
  1639. break;
  1640. /* fall through */
  1641. case STATE_SENDING_DATA:
  1642. mrq->data->error = -ENOMEDIUM;
  1643. dw_mci_stop_dma(host);
  1644. break;
  1645. case STATE_DATA_BUSY:
  1646. case STATE_DATA_ERROR:
  1647. if (mrq->data->error == -EINPROGRESS)
  1648. mrq->data->error = -ENOMEDIUM;
  1649. /* fall through */
  1650. case STATE_SENDING_STOP:
  1651. if (mrq->stop)
  1652. mrq->stop->error = -ENOMEDIUM;
  1653. break;
  1654. }
  1655. dw_mci_request_end(host, mrq);
  1656. } else {
  1657. list_del(&slot->queue_node);
  1658. mrq->cmd->error = -ENOMEDIUM;
  1659. if (mrq->data)
  1660. mrq->data->error = -ENOMEDIUM;
  1661. if (mrq->stop)
  1662. mrq->stop->error = -ENOMEDIUM;
  1663. spin_unlock(&host->lock);
  1664. mmc_request_done(slot->mmc, mrq);
  1665. spin_lock(&host->lock);
  1666. }
  1667. }
  1668. /* Power down slot */
  1669. if (present == 0) {
  1670. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1671. /*
  1672. * Clear down the FIFO - doing so generates a
  1673. * block interrupt, hence setting the
  1674. * scatter-gather pointer to NULL.
  1675. */
  1676. sg_miter_stop(&host->sg_miter);
  1677. host->sg = NULL;
  1678. ctrl = mci_readl(host, CTRL);
  1679. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1680. mci_writel(host, CTRL, ctrl);
  1681. #ifdef CONFIG_MMC_DW_IDMAC
  1682. ctrl = mci_readl(host, BMOD);
  1683. /* Software reset of DMA */
  1684. ctrl |= SDMMC_IDMAC_SWRESET;
  1685. mci_writel(host, BMOD, ctrl);
  1686. #endif
  1687. }
  1688. spin_unlock_bh(&host->lock);
  1689. present = dw_mci_get_cd(mmc);
  1690. }
  1691. mmc_detect_change(slot->mmc,
  1692. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1693. }
  1694. }
  1695. #ifdef CONFIG_OF
  1696. /* given a slot id, find out the device node representing that slot */
  1697. static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
  1698. {
  1699. struct device_node *np;
  1700. const __be32 *addr;
  1701. int len;
  1702. if (!dev || !dev->of_node)
  1703. return NULL;
  1704. for_each_child_of_node(dev->of_node, np) {
  1705. addr = of_get_property(np, "reg", &len);
  1706. if (!addr || (len < sizeof(int)))
  1707. continue;
  1708. if (be32_to_cpup(addr) == slot)
  1709. return np;
  1710. }
  1711. return NULL;
  1712. }
  1713. static struct dw_mci_of_slot_quirks {
  1714. char *quirk;
  1715. int id;
  1716. } of_slot_quirks[] = {
  1717. {
  1718. .quirk = "disable-wp",
  1719. .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
  1720. },
  1721. };
  1722. static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
  1723. {
  1724. struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
  1725. int quirks = 0;
  1726. int idx;
  1727. /* get quirks */
  1728. for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
  1729. if (of_get_property(np, of_slot_quirks[idx].quirk, NULL))
  1730. quirks |= of_slot_quirks[idx].id;
  1731. return quirks;
  1732. }
  1733. /* find out bus-width for a given slot */
  1734. static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
  1735. {
  1736. struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
  1737. u32 bus_wd = 1;
  1738. if (!np)
  1739. return 1;
  1740. if (of_property_read_u32(np, "bus-width", &bus_wd))
  1741. dev_err(dev, "bus-width property not found, assuming width"
  1742. " as 1\n");
  1743. return bus_wd;
  1744. }
  1745. /* find the write protect gpio for a given slot; or -1 if none specified */
  1746. static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
  1747. {
  1748. struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
  1749. int gpio;
  1750. if (!np)
  1751. return -EINVAL;
  1752. gpio = of_get_named_gpio(np, "wp-gpios", 0);
  1753. /* Having a missing entry is valid; return silently */
  1754. if (!gpio_is_valid(gpio))
  1755. return -EINVAL;
  1756. if (devm_gpio_request(dev, gpio, "dw-mci-wp")) {
  1757. dev_warn(dev, "gpio [%d] request failed\n", gpio);
  1758. return -EINVAL;
  1759. }
  1760. return gpio;
  1761. }
  1762. #else /* CONFIG_OF */
  1763. static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
  1764. {
  1765. return 0;
  1766. }
  1767. static u32 dw_mci_of_get_bus_wd(struct device *dev, u8 slot)
  1768. {
  1769. return 1;
  1770. }
  1771. static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
  1772. {
  1773. return NULL;
  1774. }
  1775. static int dw_mci_of_get_wp_gpio(struct device *dev, u8 slot)
  1776. {
  1777. return -EINVAL;
  1778. }
  1779. #endif /* CONFIG_OF */
  1780. static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1781. {
  1782. struct mmc_host *mmc;
  1783. struct dw_mci_slot *slot;
  1784. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1785. int ctrl_id, ret;
  1786. u32 freq[2];
  1787. u8 bus_width;
  1788. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
  1789. if (!mmc)
  1790. return -ENOMEM;
  1791. slot = mmc_priv(mmc);
  1792. slot->id = id;
  1793. slot->mmc = mmc;
  1794. slot->host = host;
  1795. host->slot[id] = slot;
  1796. slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
  1797. mmc->ops = &dw_mci_ops;
  1798. if (of_property_read_u32_array(host->dev->of_node,
  1799. "clock-freq-min-max", freq, 2)) {
  1800. mmc->f_min = DW_MCI_FREQ_MIN;
  1801. mmc->f_max = DW_MCI_FREQ_MAX;
  1802. } else {
  1803. mmc->f_min = freq[0];
  1804. mmc->f_max = freq[1];
  1805. }
  1806. if (host->pdata->get_ocr)
  1807. mmc->ocr_avail = host->pdata->get_ocr(id);
  1808. else
  1809. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1810. /*
  1811. * Start with slot power disabled, it will be enabled when a card
  1812. * is detected.
  1813. */
  1814. if (host->pdata->setpower)
  1815. host->pdata->setpower(id, 0);
  1816. if (host->pdata->caps)
  1817. mmc->caps = host->pdata->caps;
  1818. if (host->pdata->pm_caps)
  1819. mmc->pm_caps = host->pdata->pm_caps;
  1820. if (host->dev->of_node) {
  1821. ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  1822. if (ctrl_id < 0)
  1823. ctrl_id = 0;
  1824. } else {
  1825. ctrl_id = to_platform_device(host->dev)->id;
  1826. }
  1827. if (drv_data && drv_data->caps)
  1828. mmc->caps |= drv_data->caps[ctrl_id];
  1829. if (host->pdata->caps2)
  1830. mmc->caps2 = host->pdata->caps2;
  1831. if (host->pdata->get_bus_wd)
  1832. bus_width = host->pdata->get_bus_wd(slot->id);
  1833. else if (host->dev->of_node)
  1834. bus_width = dw_mci_of_get_bus_wd(host->dev, slot->id);
  1835. else
  1836. bus_width = 1;
  1837. switch (bus_width) {
  1838. case 8:
  1839. mmc->caps |= MMC_CAP_8_BIT_DATA;
  1840. case 4:
  1841. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1842. }
  1843. if (host->pdata->blk_settings) {
  1844. mmc->max_segs = host->pdata->blk_settings->max_segs;
  1845. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  1846. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  1847. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  1848. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  1849. } else {
  1850. /* Useful defaults if platform data is unset. */
  1851. #ifdef CONFIG_MMC_DW_IDMAC
  1852. mmc->max_segs = host->ring_size;
  1853. mmc->max_blk_size = 65536;
  1854. mmc->max_blk_count = host->ring_size;
  1855. mmc->max_seg_size = 0x1000;
  1856. mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
  1857. #else
  1858. mmc->max_segs = 64;
  1859. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  1860. mmc->max_blk_count = 512;
  1861. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1862. mmc->max_seg_size = mmc->max_req_size;
  1863. #endif /* CONFIG_MMC_DW_IDMAC */
  1864. }
  1865. if (dw_mci_get_cd(mmc))
  1866. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1867. else
  1868. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1869. slot->wp_gpio = dw_mci_of_get_wp_gpio(host->dev, slot->id);
  1870. ret = mmc_add_host(mmc);
  1871. if (ret)
  1872. goto err_setup_bus;
  1873. #if defined(CONFIG_DEBUG_FS)
  1874. dw_mci_init_debugfs(slot);
  1875. #endif
  1876. /* Card initially undetected */
  1877. slot->last_detect_state = 0;
  1878. return 0;
  1879. err_setup_bus:
  1880. mmc_free_host(mmc);
  1881. return -EINVAL;
  1882. }
  1883. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  1884. {
  1885. /* Shutdown detect IRQ */
  1886. if (slot->host->pdata->exit)
  1887. slot->host->pdata->exit(id);
  1888. /* Debugfs stuff is cleaned up by mmc core */
  1889. mmc_remove_host(slot->mmc);
  1890. slot->host->slot[id] = NULL;
  1891. mmc_free_host(slot->mmc);
  1892. }
  1893. static void dw_mci_init_dma(struct dw_mci *host)
  1894. {
  1895. /* Alloc memory for sg translation */
  1896. host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
  1897. &host->sg_dma, GFP_KERNEL);
  1898. if (!host->sg_cpu) {
  1899. dev_err(host->dev, "%s: could not alloc DMA memory\n",
  1900. __func__);
  1901. goto no_dma;
  1902. }
  1903. /* Determine which DMA interface to use */
  1904. #ifdef CONFIG_MMC_DW_IDMAC
  1905. host->dma_ops = &dw_mci_idmac_ops;
  1906. dev_info(host->dev, "Using internal DMA controller.\n");
  1907. #endif
  1908. if (!host->dma_ops)
  1909. goto no_dma;
  1910. if (host->dma_ops->init && host->dma_ops->start &&
  1911. host->dma_ops->stop && host->dma_ops->cleanup) {
  1912. if (host->dma_ops->init(host)) {
  1913. dev_err(host->dev, "%s: Unable to initialize "
  1914. "DMA Controller.\n", __func__);
  1915. goto no_dma;
  1916. }
  1917. } else {
  1918. dev_err(host->dev, "DMA initialization not found.\n");
  1919. goto no_dma;
  1920. }
  1921. host->use_dma = 1;
  1922. return;
  1923. no_dma:
  1924. dev_info(host->dev, "Using PIO mode.\n");
  1925. host->use_dma = 0;
  1926. return;
  1927. }
  1928. static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
  1929. {
  1930. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  1931. unsigned int ctrl;
  1932. mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1933. SDMMC_CTRL_DMA_RESET));
  1934. /* wait till resets clear */
  1935. do {
  1936. ctrl = mci_readl(host, CTRL);
  1937. if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1938. SDMMC_CTRL_DMA_RESET)))
  1939. return true;
  1940. } while (time_before(jiffies, timeout));
  1941. dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
  1942. return false;
  1943. }
  1944. #ifdef CONFIG_OF
  1945. static struct dw_mci_of_quirks {
  1946. char *quirk;
  1947. int id;
  1948. } of_quirks[] = {
  1949. {
  1950. .quirk = "broken-cd",
  1951. .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
  1952. },
  1953. };
  1954. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  1955. {
  1956. struct dw_mci_board *pdata;
  1957. struct device *dev = host->dev;
  1958. struct device_node *np = dev->of_node;
  1959. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1960. int idx, ret;
  1961. u32 clock_frequency;
  1962. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1963. if (!pdata) {
  1964. dev_err(dev, "could not allocate memory for pdata\n");
  1965. return ERR_PTR(-ENOMEM);
  1966. }
  1967. /* find out number of slots supported */
  1968. if (of_property_read_u32(dev->of_node, "num-slots",
  1969. &pdata->num_slots)) {
  1970. dev_info(dev, "num-slots property not found, "
  1971. "assuming 1 slot is available\n");
  1972. pdata->num_slots = 1;
  1973. }
  1974. /* get quirks */
  1975. for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
  1976. if (of_get_property(np, of_quirks[idx].quirk, NULL))
  1977. pdata->quirks |= of_quirks[idx].id;
  1978. if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
  1979. dev_info(dev, "fifo-depth property not found, using "
  1980. "value of FIFOTH register as default\n");
  1981. of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
  1982. if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
  1983. pdata->bus_hz = clock_frequency;
  1984. if (drv_data && drv_data->parse_dt) {
  1985. ret = drv_data->parse_dt(host);
  1986. if (ret)
  1987. return ERR_PTR(ret);
  1988. }
  1989. if (of_find_property(np, "keep-power-in-suspend", NULL))
  1990. pdata->pm_caps |= MMC_PM_KEEP_POWER;
  1991. if (of_find_property(np, "enable-sdio-wakeup", NULL))
  1992. pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  1993. if (of_find_property(np, "supports-highspeed", NULL))
  1994. pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  1995. if (of_find_property(np, "caps2-mmc-hs200-1_8v", NULL))
  1996. pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
  1997. if (of_find_property(np, "caps2-mmc-hs200-1_2v", NULL))
  1998. pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
  1999. return pdata;
  2000. }
  2001. #else /* CONFIG_OF */
  2002. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2003. {
  2004. return ERR_PTR(-EINVAL);
  2005. }
  2006. #endif /* CONFIG_OF */
  2007. int dw_mci_probe(struct dw_mci *host)
  2008. {
  2009. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2010. int width, i, ret = 0;
  2011. u32 fifo_size;
  2012. int init_slots = 0;
  2013. if (!host->pdata) {
  2014. host->pdata = dw_mci_parse_dt(host);
  2015. if (IS_ERR(host->pdata)) {
  2016. dev_err(host->dev, "platform data not available\n");
  2017. return -EINVAL;
  2018. }
  2019. }
  2020. if (!host->pdata->select_slot && host->pdata->num_slots > 1) {
  2021. dev_err(host->dev,
  2022. "Platform data must supply select_slot function\n");
  2023. return -ENODEV;
  2024. }
  2025. host->biu_clk = devm_clk_get(host->dev, "biu");
  2026. if (IS_ERR(host->biu_clk)) {
  2027. dev_dbg(host->dev, "biu clock not available\n");
  2028. } else {
  2029. ret = clk_prepare_enable(host->biu_clk);
  2030. if (ret) {
  2031. dev_err(host->dev, "failed to enable biu clock\n");
  2032. return ret;
  2033. }
  2034. }
  2035. host->ciu_clk = devm_clk_get(host->dev, "ciu");
  2036. if (IS_ERR(host->ciu_clk)) {
  2037. dev_dbg(host->dev, "ciu clock not available\n");
  2038. host->bus_hz = host->pdata->bus_hz;
  2039. } else {
  2040. ret = clk_prepare_enable(host->ciu_clk);
  2041. if (ret) {
  2042. dev_err(host->dev, "failed to enable ciu clock\n");
  2043. goto err_clk_biu;
  2044. }
  2045. if (host->pdata->bus_hz) {
  2046. ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
  2047. if (ret)
  2048. dev_warn(host->dev,
  2049. "Unable to set bus rate to %ul\n",
  2050. host->pdata->bus_hz);
  2051. }
  2052. host->bus_hz = clk_get_rate(host->ciu_clk);
  2053. }
  2054. if (drv_data && drv_data->init) {
  2055. ret = drv_data->init(host);
  2056. if (ret) {
  2057. dev_err(host->dev,
  2058. "implementation specific init failed\n");
  2059. goto err_clk_ciu;
  2060. }
  2061. }
  2062. if (drv_data && drv_data->setup_clock) {
  2063. ret = drv_data->setup_clock(host);
  2064. if (ret) {
  2065. dev_err(host->dev,
  2066. "implementation specific clock setup failed\n");
  2067. goto err_clk_ciu;
  2068. }
  2069. }
  2070. host->vmmc = devm_regulator_get_optional(host->dev, "vmmc");
  2071. if (IS_ERR(host->vmmc)) {
  2072. ret = PTR_ERR(host->vmmc);
  2073. if (ret == -EPROBE_DEFER)
  2074. goto err_clk_ciu;
  2075. dev_info(host->dev, "no vmmc regulator found: %d\n", ret);
  2076. host->vmmc = NULL;
  2077. } else {
  2078. ret = regulator_enable(host->vmmc);
  2079. if (ret) {
  2080. if (ret != -EPROBE_DEFER)
  2081. dev_err(host->dev,
  2082. "regulator_enable fail: %d\n", ret);
  2083. goto err_clk_ciu;
  2084. }
  2085. }
  2086. if (!host->bus_hz) {
  2087. dev_err(host->dev,
  2088. "Platform data must supply bus speed\n");
  2089. ret = -ENODEV;
  2090. goto err_regulator;
  2091. }
  2092. host->quirks = host->pdata->quirks;
  2093. spin_lock_init(&host->lock);
  2094. INIT_LIST_HEAD(&host->queue);
  2095. /*
  2096. * Get the host data width - this assumes that HCON has been set with
  2097. * the correct values.
  2098. */
  2099. i = (mci_readl(host, HCON) >> 7) & 0x7;
  2100. if (!i) {
  2101. host->push_data = dw_mci_push_data16;
  2102. host->pull_data = dw_mci_pull_data16;
  2103. width = 16;
  2104. host->data_shift = 1;
  2105. } else if (i == 2) {
  2106. host->push_data = dw_mci_push_data64;
  2107. host->pull_data = dw_mci_pull_data64;
  2108. width = 64;
  2109. host->data_shift = 3;
  2110. } else {
  2111. /* Check for a reserved value, and warn if it is */
  2112. WARN((i != 1),
  2113. "HCON reports a reserved host data width!\n"
  2114. "Defaulting to 32-bit access.\n");
  2115. host->push_data = dw_mci_push_data32;
  2116. host->pull_data = dw_mci_pull_data32;
  2117. width = 32;
  2118. host->data_shift = 2;
  2119. }
  2120. /* Reset all blocks */
  2121. if (!mci_wait_reset(host->dev, host))
  2122. return -ENODEV;
  2123. host->dma_ops = host->pdata->dma_ops;
  2124. dw_mci_init_dma(host);
  2125. /* Clear the interrupts for the host controller */
  2126. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2127. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2128. /* Put in max timeout */
  2129. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2130. /*
  2131. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  2132. * Tx Mark = fifo_size / 2 DMA Size = 8
  2133. */
  2134. if (!host->pdata->fifo_depth) {
  2135. /*
  2136. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  2137. * have been overwritten by the bootloader, just like we're
  2138. * about to do, so if you know the value for your hardware, you
  2139. * should put it in the platform data.
  2140. */
  2141. fifo_size = mci_readl(host, FIFOTH);
  2142. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  2143. } else {
  2144. fifo_size = host->pdata->fifo_depth;
  2145. }
  2146. host->fifo_depth = fifo_size;
  2147. host->fifoth_val =
  2148. SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
  2149. mci_writel(host, FIFOTH, host->fifoth_val);
  2150. /* disable clock to CIU */
  2151. mci_writel(host, CLKENA, 0);
  2152. mci_writel(host, CLKSRC, 0);
  2153. /*
  2154. * In 2.40a spec, Data offset is changed.
  2155. * Need to check the version-id and set data-offset for DATA register.
  2156. */
  2157. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  2158. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  2159. if (host->verid < DW_MMC_240A)
  2160. host->data_offset = DATA_OFFSET;
  2161. else
  2162. host->data_offset = DATA_240A_OFFSET;
  2163. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  2164. host->card_workqueue = alloc_workqueue("dw-mci-card",
  2165. WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
  2166. if (!host->card_workqueue) {
  2167. ret = -ENOMEM;
  2168. goto err_dmaunmap;
  2169. }
  2170. INIT_WORK(&host->card_work, dw_mci_work_routine_card);
  2171. ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
  2172. host->irq_flags, "dw-mci", host);
  2173. if (ret)
  2174. goto err_workqueue;
  2175. if (host->pdata->num_slots)
  2176. host->num_slots = host->pdata->num_slots;
  2177. else
  2178. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  2179. /*
  2180. * Enable interrupts for command done, data over, data empty, card det,
  2181. * receive ready and error such as transmit, receive timeout, crc error
  2182. */
  2183. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2184. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2185. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2186. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  2187. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  2188. dev_info(host->dev, "DW MMC controller at irq %d, "
  2189. "%d bit host data width, "
  2190. "%u deep fifo\n",
  2191. host->irq, width, fifo_size);
  2192. /* We need at least one slot to succeed */
  2193. for (i = 0; i < host->num_slots; i++) {
  2194. ret = dw_mci_init_slot(host, i);
  2195. if (ret)
  2196. dev_dbg(host->dev, "slot %d init failed\n", i);
  2197. else
  2198. init_slots++;
  2199. }
  2200. if (init_slots) {
  2201. dev_info(host->dev, "%d slots initialized\n", init_slots);
  2202. } else {
  2203. dev_dbg(host->dev, "attempted to initialize %d slots, "
  2204. "but failed on all\n", host->num_slots);
  2205. goto err_workqueue;
  2206. }
  2207. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  2208. dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
  2209. return 0;
  2210. err_workqueue:
  2211. destroy_workqueue(host->card_workqueue);
  2212. err_dmaunmap:
  2213. if (host->use_dma && host->dma_ops->exit)
  2214. host->dma_ops->exit(host);
  2215. err_regulator:
  2216. if (host->vmmc)
  2217. regulator_disable(host->vmmc);
  2218. err_clk_ciu:
  2219. if (!IS_ERR(host->ciu_clk))
  2220. clk_disable_unprepare(host->ciu_clk);
  2221. err_clk_biu:
  2222. if (!IS_ERR(host->biu_clk))
  2223. clk_disable_unprepare(host->biu_clk);
  2224. return ret;
  2225. }
  2226. EXPORT_SYMBOL(dw_mci_probe);
  2227. void dw_mci_remove(struct dw_mci *host)
  2228. {
  2229. int i;
  2230. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2231. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2232. for (i = 0; i < host->num_slots; i++) {
  2233. dev_dbg(host->dev, "remove slot %d\n", i);
  2234. if (host->slot[i])
  2235. dw_mci_cleanup_slot(host->slot[i], i);
  2236. }
  2237. /* disable clock to CIU */
  2238. mci_writel(host, CLKENA, 0);
  2239. mci_writel(host, CLKSRC, 0);
  2240. destroy_workqueue(host->card_workqueue);
  2241. if (host->use_dma && host->dma_ops->exit)
  2242. host->dma_ops->exit(host);
  2243. if (host->vmmc)
  2244. regulator_disable(host->vmmc);
  2245. if (!IS_ERR(host->ciu_clk))
  2246. clk_disable_unprepare(host->ciu_clk);
  2247. if (!IS_ERR(host->biu_clk))
  2248. clk_disable_unprepare(host->biu_clk);
  2249. }
  2250. EXPORT_SYMBOL(dw_mci_remove);
  2251. #ifdef CONFIG_PM_SLEEP
  2252. /*
  2253. * TODO: we should probably disable the clock to the card in the suspend path.
  2254. */
  2255. int dw_mci_suspend(struct dw_mci *host)
  2256. {
  2257. int i, ret = 0;
  2258. for (i = 0; i < host->num_slots; i++) {
  2259. struct dw_mci_slot *slot = host->slot[i];
  2260. if (!slot)
  2261. continue;
  2262. ret = mmc_suspend_host(slot->mmc);
  2263. if (ret < 0) {
  2264. while (--i >= 0) {
  2265. slot = host->slot[i];
  2266. if (slot)
  2267. mmc_resume_host(host->slot[i]->mmc);
  2268. }
  2269. return ret;
  2270. }
  2271. }
  2272. if (host->vmmc)
  2273. regulator_disable(host->vmmc);
  2274. return 0;
  2275. }
  2276. EXPORT_SYMBOL(dw_mci_suspend);
  2277. int dw_mci_resume(struct dw_mci *host)
  2278. {
  2279. int i, ret;
  2280. if (host->vmmc) {
  2281. ret = regulator_enable(host->vmmc);
  2282. if (ret) {
  2283. dev_err(host->dev,
  2284. "failed to enable regulator: %d\n", ret);
  2285. return ret;
  2286. }
  2287. }
  2288. if (!mci_wait_reset(host->dev, host)) {
  2289. ret = -ENODEV;
  2290. return ret;
  2291. }
  2292. if (host->use_dma && host->dma_ops->init)
  2293. host->dma_ops->init(host);
  2294. /*
  2295. * Restore the initial value at FIFOTH register
  2296. * And Invalidate the prev_blksz with zero
  2297. */
  2298. mci_writel(host, FIFOTH, host->fifoth_val);
  2299. host->prev_blksz = 0;
  2300. /* Put in max timeout */
  2301. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2302. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2303. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2304. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2305. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  2306. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2307. for (i = 0; i < host->num_slots; i++) {
  2308. struct dw_mci_slot *slot = host->slot[i];
  2309. if (!slot)
  2310. continue;
  2311. if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
  2312. dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
  2313. dw_mci_setup_bus(slot, true);
  2314. }
  2315. ret = mmc_resume_host(host->slot[i]->mmc);
  2316. if (ret < 0)
  2317. return ret;
  2318. }
  2319. return 0;
  2320. }
  2321. EXPORT_SYMBOL(dw_mci_resume);
  2322. #endif /* CONFIG_PM_SLEEP */
  2323. static int __init dw_mci_init(void)
  2324. {
  2325. pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
  2326. return 0;
  2327. }
  2328. static void __exit dw_mci_exit(void)
  2329. {
  2330. }
  2331. module_init(dw_mci_init);
  2332. module_exit(dw_mci_exit);
  2333. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  2334. MODULE_AUTHOR("NXP Semiconductor VietNam");
  2335. MODULE_AUTHOR("Imagination Technologies Ltd");
  2336. MODULE_LICENSE("GPL v2");