dw_mmc.c 54 KB

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