dw_mmc.c 61 KB

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