dw_mmc.c 64 KB

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