dw_mmc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803
  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/scatterlist.h>
  25. #include <linux/seq_file.h>
  26. #include <linux/slab.h>
  27. #include <linux/stat.h>
  28. #include <linux/delay.h>
  29. #include <linux/irq.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/dw_mmc.h>
  33. #include <linux/bitops.h>
  34. #include "dw_mmc.h"
  35. /* Common flag combinations */
  36. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DTO | SDMMC_INT_DCRC | \
  37. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  38. SDMMC_INT_EBE)
  39. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  40. SDMMC_INT_RESP_ERR)
  41. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  42. DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
  43. #define DW_MCI_SEND_STATUS 1
  44. #define DW_MCI_RECV_STATUS 2
  45. #define DW_MCI_DMA_THRESHOLD 16
  46. #ifdef CONFIG_MMC_DW_IDMAC
  47. struct idmac_desc {
  48. u32 des0; /* Control Descriptor */
  49. #define IDMAC_DES0_DIC BIT(1)
  50. #define IDMAC_DES0_LD BIT(2)
  51. #define IDMAC_DES0_FD BIT(3)
  52. #define IDMAC_DES0_CH BIT(4)
  53. #define IDMAC_DES0_ER BIT(5)
  54. #define IDMAC_DES0_CES BIT(30)
  55. #define IDMAC_DES0_OWN BIT(31)
  56. u32 des1; /* Buffer sizes */
  57. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  58. ((d)->des1 = ((d)->des1 & 0x03ffc000) | ((s) & 0x3fff))
  59. u32 des2; /* buffer 1 physical address */
  60. u32 des3; /* buffer 2 physical address */
  61. };
  62. #endif /* CONFIG_MMC_DW_IDMAC */
  63. /**
  64. * struct dw_mci_slot - MMC slot state
  65. * @mmc: The mmc_host representing this slot.
  66. * @host: The MMC controller this slot is using.
  67. * @ctype: Card type for this slot.
  68. * @mrq: mmc_request currently being processed or waiting to be
  69. * processed, or NULL when the slot is idle.
  70. * @queue_node: List node for placing this node in the @queue list of
  71. * &struct dw_mci.
  72. * @clock: Clock rate configured by set_ios(). Protected by host->lock.
  73. * @flags: Random state bits associated with the slot.
  74. * @id: Number of this slot.
  75. * @last_detect_state: Most recently observed card detect state.
  76. */
  77. struct dw_mci_slot {
  78. struct mmc_host *mmc;
  79. struct dw_mci *host;
  80. u32 ctype;
  81. struct mmc_request *mrq;
  82. struct list_head queue_node;
  83. unsigned int clock;
  84. unsigned long flags;
  85. #define DW_MMC_CARD_PRESENT 0
  86. #define DW_MMC_CARD_NEED_INIT 1
  87. int id;
  88. int last_detect_state;
  89. };
  90. #if defined(CONFIG_DEBUG_FS)
  91. static int dw_mci_req_show(struct seq_file *s, void *v)
  92. {
  93. struct dw_mci_slot *slot = s->private;
  94. struct mmc_request *mrq;
  95. struct mmc_command *cmd;
  96. struct mmc_command *stop;
  97. struct mmc_data *data;
  98. /* Make sure we get a consistent snapshot */
  99. spin_lock_bh(&slot->host->lock);
  100. mrq = slot->mrq;
  101. if (mrq) {
  102. cmd = mrq->cmd;
  103. data = mrq->data;
  104. stop = mrq->stop;
  105. if (cmd)
  106. seq_printf(s,
  107. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  108. cmd->opcode, cmd->arg, cmd->flags,
  109. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  110. cmd->resp[2], cmd->error);
  111. if (data)
  112. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  113. data->bytes_xfered, data->blocks,
  114. data->blksz, data->flags, data->error);
  115. if (stop)
  116. seq_printf(s,
  117. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  118. stop->opcode, stop->arg, stop->flags,
  119. stop->resp[0], stop->resp[1], stop->resp[2],
  120. stop->resp[2], stop->error);
  121. }
  122. spin_unlock_bh(&slot->host->lock);
  123. return 0;
  124. }
  125. static int dw_mci_req_open(struct inode *inode, struct file *file)
  126. {
  127. return single_open(file, dw_mci_req_show, inode->i_private);
  128. }
  129. static const struct file_operations dw_mci_req_fops = {
  130. .owner = THIS_MODULE,
  131. .open = dw_mci_req_open,
  132. .read = seq_read,
  133. .llseek = seq_lseek,
  134. .release = single_release,
  135. };
  136. static int dw_mci_regs_show(struct seq_file *s, void *v)
  137. {
  138. seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
  139. seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
  140. seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
  141. seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
  142. seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
  143. seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
  144. return 0;
  145. }
  146. static int dw_mci_regs_open(struct inode *inode, struct file *file)
  147. {
  148. return single_open(file, dw_mci_regs_show, inode->i_private);
  149. }
  150. static const struct file_operations dw_mci_regs_fops = {
  151. .owner = THIS_MODULE,
  152. .open = dw_mci_regs_open,
  153. .read = seq_read,
  154. .llseek = seq_lseek,
  155. .release = single_release,
  156. };
  157. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  158. {
  159. struct mmc_host *mmc = slot->mmc;
  160. struct dw_mci *host = slot->host;
  161. struct dentry *root;
  162. struct dentry *node;
  163. root = mmc->debugfs_root;
  164. if (!root)
  165. return;
  166. node = debugfs_create_file("regs", S_IRUSR, root, host,
  167. &dw_mci_regs_fops);
  168. if (!node)
  169. goto err;
  170. node = debugfs_create_file("req", S_IRUSR, root, slot,
  171. &dw_mci_req_fops);
  172. if (!node)
  173. goto err;
  174. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  175. if (!node)
  176. goto err;
  177. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  178. (u32 *)&host->pending_events);
  179. if (!node)
  180. goto err;
  181. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  182. (u32 *)&host->completed_events);
  183. if (!node)
  184. goto err;
  185. return;
  186. err:
  187. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  188. }
  189. #endif /* defined(CONFIG_DEBUG_FS) */
  190. static void dw_mci_set_timeout(struct dw_mci *host)
  191. {
  192. /* timeout (maximum) */
  193. mci_writel(host, TMOUT, 0xffffffff);
  194. }
  195. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  196. {
  197. struct mmc_data *data;
  198. u32 cmdr;
  199. cmd->error = -EINPROGRESS;
  200. cmdr = cmd->opcode;
  201. if (cmdr == MMC_STOP_TRANSMISSION)
  202. cmdr |= SDMMC_CMD_STOP;
  203. else
  204. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  205. if (cmd->flags & MMC_RSP_PRESENT) {
  206. /* We expect a response, so set this bit */
  207. cmdr |= SDMMC_CMD_RESP_EXP;
  208. if (cmd->flags & MMC_RSP_136)
  209. cmdr |= SDMMC_CMD_RESP_LONG;
  210. }
  211. if (cmd->flags & MMC_RSP_CRC)
  212. cmdr |= SDMMC_CMD_RESP_CRC;
  213. data = cmd->data;
  214. if (data) {
  215. cmdr |= SDMMC_CMD_DAT_EXP;
  216. if (data->flags & MMC_DATA_STREAM)
  217. cmdr |= SDMMC_CMD_STRM_MODE;
  218. if (data->flags & MMC_DATA_WRITE)
  219. cmdr |= SDMMC_CMD_DAT_WR;
  220. }
  221. return cmdr;
  222. }
  223. static void dw_mci_start_command(struct dw_mci *host,
  224. struct mmc_command *cmd, u32 cmd_flags)
  225. {
  226. host->cmd = cmd;
  227. dev_vdbg(&host->pdev->dev,
  228. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  229. cmd->arg, cmd_flags);
  230. mci_writel(host, CMDARG, cmd->arg);
  231. wmb();
  232. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  233. }
  234. static void send_stop_cmd(struct dw_mci *host, struct mmc_data *data)
  235. {
  236. dw_mci_start_command(host, data->stop, host->stop_cmdr);
  237. }
  238. /* DMA interface functions */
  239. static void dw_mci_stop_dma(struct dw_mci *host)
  240. {
  241. if (host->use_dma) {
  242. host->dma_ops->stop(host);
  243. host->dma_ops->cleanup(host);
  244. } else {
  245. /* Data transfer was stopped by the interrupt handler */
  246. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  247. }
  248. }
  249. #ifdef CONFIG_MMC_DW_IDMAC
  250. static void dw_mci_dma_cleanup(struct dw_mci *host)
  251. {
  252. struct mmc_data *data = host->data;
  253. if (data)
  254. dma_unmap_sg(&host->pdev->dev, data->sg, data->sg_len,
  255. ((data->flags & MMC_DATA_WRITE)
  256. ? DMA_TO_DEVICE : DMA_FROM_DEVICE));
  257. }
  258. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  259. {
  260. u32 temp;
  261. /* Disable and reset the IDMAC interface */
  262. temp = mci_readl(host, CTRL);
  263. temp &= ~SDMMC_CTRL_USE_IDMAC;
  264. temp |= SDMMC_CTRL_DMA_RESET;
  265. mci_writel(host, CTRL, temp);
  266. /* Stop the IDMAC running */
  267. temp = mci_readl(host, BMOD);
  268. temp &= ~SDMMC_IDMAC_ENABLE;
  269. mci_writel(host, BMOD, temp);
  270. }
  271. static void dw_mci_idmac_complete_dma(struct dw_mci *host)
  272. {
  273. struct mmc_data *data = host->data;
  274. dev_vdbg(&host->pdev->dev, "DMA complete\n");
  275. host->dma_ops->cleanup(host);
  276. /*
  277. * If the card was removed, data will be NULL. No point in trying to
  278. * send the stop command or waiting for NBUSY in this case.
  279. */
  280. if (data) {
  281. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  282. tasklet_schedule(&host->tasklet);
  283. }
  284. }
  285. static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
  286. unsigned int sg_len)
  287. {
  288. int i;
  289. struct idmac_desc *desc = host->sg_cpu;
  290. for (i = 0; i < sg_len; i++, desc++) {
  291. unsigned int length = sg_dma_len(&data->sg[i]);
  292. u32 mem_addr = sg_dma_address(&data->sg[i]);
  293. /* Set the OWN bit and disable interrupts for this descriptor */
  294. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | IDMAC_DES0_CH;
  295. /* Buffer length */
  296. IDMAC_SET_BUFFER1_SIZE(desc, length);
  297. /* Physical address to DMA to/from */
  298. desc->des2 = mem_addr;
  299. }
  300. /* Set first descriptor */
  301. desc = host->sg_cpu;
  302. desc->des0 |= IDMAC_DES0_FD;
  303. /* Set last descriptor */
  304. desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
  305. desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  306. desc->des0 |= IDMAC_DES0_LD;
  307. wmb();
  308. }
  309. static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  310. {
  311. u32 temp;
  312. dw_mci_translate_sglist(host, host->data, sg_len);
  313. /* Select IDMAC interface */
  314. temp = mci_readl(host, CTRL);
  315. temp |= SDMMC_CTRL_USE_IDMAC;
  316. mci_writel(host, CTRL, temp);
  317. wmb();
  318. /* Enable the IDMAC */
  319. temp = mci_readl(host, BMOD);
  320. temp |= SDMMC_IDMAC_ENABLE;
  321. mci_writel(host, BMOD, temp);
  322. /* Start it running */
  323. mci_writel(host, PLDMND, 1);
  324. }
  325. static int dw_mci_idmac_init(struct dw_mci *host)
  326. {
  327. struct idmac_desc *p;
  328. int i;
  329. /* Number of descriptors in the ring buffer */
  330. host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
  331. /* Forward link the descriptor list */
  332. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
  333. p->des3 = host->sg_dma + (sizeof(struct idmac_desc) * (i + 1));
  334. /* Set the last descriptor as the end-of-ring descriptor */
  335. p->des3 = host->sg_dma;
  336. p->des0 = IDMAC_DES0_ER;
  337. /* Mask out interrupts - get Tx & Rx complete only */
  338. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | SDMMC_IDMAC_INT_RI |
  339. SDMMC_IDMAC_INT_TI);
  340. /* Set the descriptor base address */
  341. mci_writel(host, DBADDR, host->sg_dma);
  342. return 0;
  343. }
  344. static struct dw_mci_dma_ops dw_mci_idmac_ops = {
  345. .init = dw_mci_idmac_init,
  346. .start = dw_mci_idmac_start_dma,
  347. .stop = dw_mci_idmac_stop_dma,
  348. .complete = dw_mci_idmac_complete_dma,
  349. .cleanup = dw_mci_dma_cleanup,
  350. };
  351. #endif /* CONFIG_MMC_DW_IDMAC */
  352. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  353. {
  354. struct scatterlist *sg;
  355. unsigned int i, direction, sg_len;
  356. u32 temp;
  357. /* If we don't have a channel, we can't do DMA */
  358. if (!host->use_dma)
  359. return -ENODEV;
  360. /*
  361. * We don't do DMA on "complex" transfers, i.e. with
  362. * non-word-aligned buffers or lengths. Also, we don't bother
  363. * with all the DMA setup overhead for short transfers.
  364. */
  365. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  366. return -EINVAL;
  367. if (data->blksz & 3)
  368. return -EINVAL;
  369. for_each_sg(data->sg, sg, data->sg_len, i) {
  370. if (sg->offset & 3 || sg->length & 3)
  371. return -EINVAL;
  372. }
  373. if (data->flags & MMC_DATA_READ)
  374. direction = DMA_FROM_DEVICE;
  375. else
  376. direction = DMA_TO_DEVICE;
  377. sg_len = dma_map_sg(&host->pdev->dev, data->sg, data->sg_len,
  378. direction);
  379. dev_vdbg(&host->pdev->dev,
  380. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  381. (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
  382. sg_len);
  383. /* Enable the DMA interface */
  384. temp = mci_readl(host, CTRL);
  385. temp |= SDMMC_CTRL_DMA_ENABLE;
  386. mci_writel(host, CTRL, temp);
  387. /* Disable RX/TX IRQs, let DMA handle it */
  388. temp = mci_readl(host, INTMASK);
  389. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  390. mci_writel(host, INTMASK, temp);
  391. host->dma_ops->start(host, sg_len);
  392. return 0;
  393. }
  394. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  395. {
  396. u32 temp;
  397. data->error = -EINPROGRESS;
  398. WARN_ON(host->data);
  399. host->sg = NULL;
  400. host->data = data;
  401. if (dw_mci_submit_data_dma(host, data)) {
  402. host->sg = data->sg;
  403. host->pio_offset = 0;
  404. if (data->flags & MMC_DATA_READ)
  405. host->dir_status = DW_MCI_RECV_STATUS;
  406. else
  407. host->dir_status = DW_MCI_SEND_STATUS;
  408. temp = mci_readl(host, INTMASK);
  409. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  410. mci_writel(host, INTMASK, temp);
  411. temp = mci_readl(host, CTRL);
  412. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  413. mci_writel(host, CTRL, temp);
  414. }
  415. }
  416. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  417. {
  418. struct dw_mci *host = slot->host;
  419. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  420. unsigned int cmd_status = 0;
  421. mci_writel(host, CMDARG, arg);
  422. wmb();
  423. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  424. while (time_before(jiffies, timeout)) {
  425. cmd_status = mci_readl(host, CMD);
  426. if (!(cmd_status & SDMMC_CMD_START))
  427. return;
  428. }
  429. dev_err(&slot->mmc->class_dev,
  430. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  431. cmd, arg, cmd_status);
  432. }
  433. static void dw_mci_setup_bus(struct dw_mci_slot *slot)
  434. {
  435. struct dw_mci *host = slot->host;
  436. u32 div;
  437. if (slot->clock != host->current_speed) {
  438. if (host->bus_hz % slot->clock)
  439. /*
  440. * move the + 1 after the divide to prevent
  441. * over-clocking the card.
  442. */
  443. div = ((host->bus_hz / slot->clock) >> 1) + 1;
  444. else
  445. div = (host->bus_hz / slot->clock) >> 1;
  446. dev_info(&slot->mmc->class_dev,
  447. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ"
  448. " div = %d)\n", slot->id, host->bus_hz, slot->clock,
  449. div ? ((host->bus_hz / div) >> 1) : host->bus_hz, div);
  450. /* disable clock */
  451. mci_writel(host, CLKENA, 0);
  452. mci_writel(host, CLKSRC, 0);
  453. /* inform CIU */
  454. mci_send_cmd(slot,
  455. SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
  456. /* set clock to desired speed */
  457. mci_writel(host, CLKDIV, div);
  458. /* inform CIU */
  459. mci_send_cmd(slot,
  460. SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
  461. /* enable clock */
  462. mci_writel(host, CLKENA, SDMMC_CLKEN_ENABLE |
  463. SDMMC_CLKEN_LOW_PWR);
  464. /* inform CIU */
  465. mci_send_cmd(slot,
  466. SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, 0);
  467. host->current_speed = slot->clock;
  468. }
  469. /* Set the current slot bus width */
  470. mci_writel(host, CTYPE, slot->ctype);
  471. }
  472. static void dw_mci_start_request(struct dw_mci *host,
  473. struct dw_mci_slot *slot)
  474. {
  475. struct mmc_request *mrq;
  476. struct mmc_command *cmd;
  477. struct mmc_data *data;
  478. u32 cmdflags;
  479. mrq = slot->mrq;
  480. if (host->pdata->select_slot)
  481. host->pdata->select_slot(slot->id);
  482. /* Slot specific timing and width adjustment */
  483. dw_mci_setup_bus(slot);
  484. host->cur_slot = slot;
  485. host->mrq = mrq;
  486. host->pending_events = 0;
  487. host->completed_events = 0;
  488. host->data_status = 0;
  489. data = mrq->data;
  490. if (data) {
  491. dw_mci_set_timeout(host);
  492. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  493. mci_writel(host, BLKSIZ, data->blksz);
  494. }
  495. cmd = mrq->cmd;
  496. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  497. /* this is the first command, send the initialization clock */
  498. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  499. cmdflags |= SDMMC_CMD_INIT;
  500. if (data) {
  501. dw_mci_submit_data(host, data);
  502. wmb();
  503. }
  504. dw_mci_start_command(host, cmd, cmdflags);
  505. if (mrq->stop)
  506. host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
  507. }
  508. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  509. struct mmc_request *mrq)
  510. {
  511. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  512. host->state);
  513. spin_lock_bh(&host->lock);
  514. slot->mrq = mrq;
  515. if (host->state == STATE_IDLE) {
  516. host->state = STATE_SENDING_CMD;
  517. dw_mci_start_request(host, slot);
  518. } else {
  519. list_add_tail(&slot->queue_node, &host->queue);
  520. }
  521. spin_unlock_bh(&host->lock);
  522. }
  523. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  524. {
  525. struct dw_mci_slot *slot = mmc_priv(mmc);
  526. struct dw_mci *host = slot->host;
  527. WARN_ON(slot->mrq);
  528. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  529. mrq->cmd->error = -ENOMEDIUM;
  530. mmc_request_done(mmc, mrq);
  531. return;
  532. }
  533. /* We don't support multiple blocks of weird lengths. */
  534. dw_mci_queue_request(host, slot, mrq);
  535. }
  536. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  537. {
  538. struct dw_mci_slot *slot = mmc_priv(mmc);
  539. /* set default 1 bit mode */
  540. slot->ctype = SDMMC_CTYPE_1BIT;
  541. switch (ios->bus_width) {
  542. case MMC_BUS_WIDTH_1:
  543. slot->ctype = SDMMC_CTYPE_1BIT;
  544. break;
  545. case MMC_BUS_WIDTH_4:
  546. slot->ctype = SDMMC_CTYPE_4BIT;
  547. break;
  548. }
  549. if (ios->clock) {
  550. /*
  551. * Use mirror of ios->clock to prevent race with mmc
  552. * core ios update when finding the minimum.
  553. */
  554. slot->clock = ios->clock;
  555. }
  556. switch (ios->power_mode) {
  557. case MMC_POWER_UP:
  558. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  559. break;
  560. default:
  561. break;
  562. }
  563. }
  564. static int dw_mci_get_ro(struct mmc_host *mmc)
  565. {
  566. int read_only;
  567. struct dw_mci_slot *slot = mmc_priv(mmc);
  568. struct dw_mci_board *brd = slot->host->pdata;
  569. /* Use platform get_ro function, else try on board write protect */
  570. if (brd->get_ro)
  571. read_only = brd->get_ro(slot->id);
  572. else
  573. read_only =
  574. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  575. dev_dbg(&mmc->class_dev, "card is %s\n",
  576. read_only ? "read-only" : "read-write");
  577. return read_only;
  578. }
  579. static int dw_mci_get_cd(struct mmc_host *mmc)
  580. {
  581. int present;
  582. struct dw_mci_slot *slot = mmc_priv(mmc);
  583. struct dw_mci_board *brd = slot->host->pdata;
  584. /* Use platform get_cd function, else try onboard card detect */
  585. if (brd->get_cd)
  586. present = !brd->get_cd(slot->id);
  587. else
  588. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  589. == 0 ? 1 : 0;
  590. if (present)
  591. dev_dbg(&mmc->class_dev, "card is present\n");
  592. else
  593. dev_dbg(&mmc->class_dev, "card is not present\n");
  594. return present;
  595. }
  596. static const struct mmc_host_ops dw_mci_ops = {
  597. .request = dw_mci_request,
  598. .set_ios = dw_mci_set_ios,
  599. .get_ro = dw_mci_get_ro,
  600. .get_cd = dw_mci_get_cd,
  601. };
  602. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  603. __releases(&host->lock)
  604. __acquires(&host->lock)
  605. {
  606. struct dw_mci_slot *slot;
  607. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  608. WARN_ON(host->cmd || host->data);
  609. host->cur_slot->mrq = NULL;
  610. host->mrq = NULL;
  611. if (!list_empty(&host->queue)) {
  612. slot = list_entry(host->queue.next,
  613. struct dw_mci_slot, queue_node);
  614. list_del(&slot->queue_node);
  615. dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
  616. mmc_hostname(slot->mmc));
  617. host->state = STATE_SENDING_CMD;
  618. dw_mci_start_request(host, slot);
  619. } else {
  620. dev_vdbg(&host->pdev->dev, "list empty\n");
  621. host->state = STATE_IDLE;
  622. }
  623. spin_unlock(&host->lock);
  624. mmc_request_done(prev_mmc, mrq);
  625. spin_lock(&host->lock);
  626. }
  627. static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  628. {
  629. u32 status = host->cmd_status;
  630. host->cmd_status = 0;
  631. /* Read the response from the card (up to 16 bytes) */
  632. if (cmd->flags & MMC_RSP_PRESENT) {
  633. if (cmd->flags & MMC_RSP_136) {
  634. cmd->resp[3] = mci_readl(host, RESP0);
  635. cmd->resp[2] = mci_readl(host, RESP1);
  636. cmd->resp[1] = mci_readl(host, RESP2);
  637. cmd->resp[0] = mci_readl(host, RESP3);
  638. } else {
  639. cmd->resp[0] = mci_readl(host, RESP0);
  640. cmd->resp[1] = 0;
  641. cmd->resp[2] = 0;
  642. cmd->resp[3] = 0;
  643. }
  644. }
  645. if (status & SDMMC_INT_RTO)
  646. cmd->error = -ETIMEDOUT;
  647. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  648. cmd->error = -EILSEQ;
  649. else if (status & SDMMC_INT_RESP_ERR)
  650. cmd->error = -EIO;
  651. else
  652. cmd->error = 0;
  653. if (cmd->error) {
  654. /* newer ip versions need a delay between retries */
  655. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  656. mdelay(20);
  657. if (cmd->data) {
  658. host->data = NULL;
  659. dw_mci_stop_dma(host);
  660. }
  661. }
  662. }
  663. static void dw_mci_tasklet_func(unsigned long priv)
  664. {
  665. struct dw_mci *host = (struct dw_mci *)priv;
  666. struct mmc_data *data;
  667. struct mmc_command *cmd;
  668. enum dw_mci_state state;
  669. enum dw_mci_state prev_state;
  670. u32 status;
  671. spin_lock(&host->lock);
  672. state = host->state;
  673. data = host->data;
  674. do {
  675. prev_state = state;
  676. switch (state) {
  677. case STATE_IDLE:
  678. break;
  679. case STATE_SENDING_CMD:
  680. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  681. &host->pending_events))
  682. break;
  683. cmd = host->cmd;
  684. host->cmd = NULL;
  685. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  686. dw_mci_command_complete(host, host->mrq->cmd);
  687. if (!host->mrq->data || cmd->error) {
  688. dw_mci_request_end(host, host->mrq);
  689. goto unlock;
  690. }
  691. prev_state = state = STATE_SENDING_DATA;
  692. /* fall through */
  693. case STATE_SENDING_DATA:
  694. if (test_and_clear_bit(EVENT_DATA_ERROR,
  695. &host->pending_events)) {
  696. dw_mci_stop_dma(host);
  697. if (data->stop)
  698. send_stop_cmd(host, data);
  699. state = STATE_DATA_ERROR;
  700. break;
  701. }
  702. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  703. &host->pending_events))
  704. break;
  705. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  706. prev_state = state = STATE_DATA_BUSY;
  707. /* fall through */
  708. case STATE_DATA_BUSY:
  709. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  710. &host->pending_events))
  711. break;
  712. host->data = NULL;
  713. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  714. status = host->data_status;
  715. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  716. if (status & SDMMC_INT_DTO) {
  717. dev_err(&host->pdev->dev,
  718. "data timeout error\n");
  719. data->error = -ETIMEDOUT;
  720. } else if (status & SDMMC_INT_DCRC) {
  721. dev_err(&host->pdev->dev,
  722. "data CRC error\n");
  723. data->error = -EILSEQ;
  724. } else {
  725. dev_err(&host->pdev->dev,
  726. "data FIFO error "
  727. "(status=%08x)\n",
  728. status);
  729. data->error = -EIO;
  730. }
  731. } else {
  732. data->bytes_xfered = data->blocks * data->blksz;
  733. data->error = 0;
  734. }
  735. if (!data->stop) {
  736. dw_mci_request_end(host, host->mrq);
  737. goto unlock;
  738. }
  739. prev_state = state = STATE_SENDING_STOP;
  740. if (!data->error)
  741. send_stop_cmd(host, data);
  742. /* fall through */
  743. case STATE_SENDING_STOP:
  744. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  745. &host->pending_events))
  746. break;
  747. host->cmd = NULL;
  748. dw_mci_command_complete(host, host->mrq->stop);
  749. dw_mci_request_end(host, host->mrq);
  750. goto unlock;
  751. case STATE_DATA_ERROR:
  752. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  753. &host->pending_events))
  754. break;
  755. state = STATE_DATA_BUSY;
  756. break;
  757. }
  758. } while (state != prev_state);
  759. host->state = state;
  760. unlock:
  761. spin_unlock(&host->lock);
  762. }
  763. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  764. {
  765. u16 *pdata = (u16 *)buf;
  766. WARN_ON(cnt % 2 != 0);
  767. cnt = cnt >> 1;
  768. while (cnt > 0) {
  769. mci_writew(host, DATA, *pdata++);
  770. cnt--;
  771. }
  772. }
  773. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  774. {
  775. u16 *pdata = (u16 *)buf;
  776. WARN_ON(cnt % 2 != 0);
  777. cnt = cnt >> 1;
  778. while (cnt > 0) {
  779. *pdata++ = mci_readw(host, DATA);
  780. cnt--;
  781. }
  782. }
  783. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  784. {
  785. u32 *pdata = (u32 *)buf;
  786. WARN_ON(cnt % 4 != 0);
  787. WARN_ON((unsigned long)pdata & 0x3);
  788. cnt = cnt >> 2;
  789. while (cnt > 0) {
  790. mci_writel(host, DATA, *pdata++);
  791. cnt--;
  792. }
  793. }
  794. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  795. {
  796. u32 *pdata = (u32 *)buf;
  797. WARN_ON(cnt % 4 != 0);
  798. WARN_ON((unsigned long)pdata & 0x3);
  799. cnt = cnt >> 2;
  800. while (cnt > 0) {
  801. *pdata++ = mci_readl(host, DATA);
  802. cnt--;
  803. }
  804. }
  805. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  806. {
  807. u64 *pdata = (u64 *)buf;
  808. WARN_ON(cnt % 8 != 0);
  809. cnt = cnt >> 3;
  810. while (cnt > 0) {
  811. mci_writeq(host, DATA, *pdata++);
  812. cnt--;
  813. }
  814. }
  815. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  816. {
  817. u64 *pdata = (u64 *)buf;
  818. WARN_ON(cnt % 8 != 0);
  819. cnt = cnt >> 3;
  820. while (cnt > 0) {
  821. *pdata++ = mci_readq(host, DATA);
  822. cnt--;
  823. }
  824. }
  825. static void dw_mci_read_data_pio(struct dw_mci *host)
  826. {
  827. struct scatterlist *sg = host->sg;
  828. void *buf = sg_virt(sg);
  829. unsigned int offset = host->pio_offset;
  830. struct mmc_data *data = host->data;
  831. int shift = host->data_shift;
  832. u32 status;
  833. unsigned int nbytes = 0, len, old_len, count = 0;
  834. do {
  835. len = SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift;
  836. if (count == 0)
  837. old_len = len;
  838. if (offset + len <= sg->length) {
  839. host->pull_data(host, (void *)(buf + offset), len);
  840. offset += len;
  841. nbytes += len;
  842. if (offset == sg->length) {
  843. flush_dcache_page(sg_page(sg));
  844. host->sg = sg = sg_next(sg);
  845. if (!sg)
  846. goto done;
  847. offset = 0;
  848. buf = sg_virt(sg);
  849. }
  850. } else {
  851. unsigned int remaining = sg->length - offset;
  852. host->pull_data(host, (void *)(buf + offset),
  853. remaining);
  854. nbytes += remaining;
  855. flush_dcache_page(sg_page(sg));
  856. host->sg = sg = sg_next(sg);
  857. if (!sg)
  858. goto done;
  859. offset = len - remaining;
  860. buf = sg_virt(sg);
  861. host->pull_data(host, buf, offset);
  862. nbytes += offset;
  863. }
  864. status = mci_readl(host, MINTSTS);
  865. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  866. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  867. host->data_status = status;
  868. data->bytes_xfered += nbytes;
  869. smp_wmb();
  870. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  871. tasklet_schedule(&host->tasklet);
  872. return;
  873. }
  874. count++;
  875. } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/
  876. len = SDMMC_GET_FCNT(mci_readl(host, STATUS));
  877. host->pio_offset = offset;
  878. data->bytes_xfered += nbytes;
  879. return;
  880. done:
  881. data->bytes_xfered += nbytes;
  882. smp_wmb();
  883. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  884. }
  885. static void dw_mci_write_data_pio(struct dw_mci *host)
  886. {
  887. struct scatterlist *sg = host->sg;
  888. void *buf = sg_virt(sg);
  889. unsigned int offset = host->pio_offset;
  890. struct mmc_data *data = host->data;
  891. int shift = host->data_shift;
  892. u32 status;
  893. unsigned int nbytes = 0, len;
  894. do {
  895. len = SDMMC_FIFO_SZ -
  896. (SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift);
  897. if (offset + len <= sg->length) {
  898. host->push_data(host, (void *)(buf + offset), len);
  899. offset += len;
  900. nbytes += len;
  901. if (offset == sg->length) {
  902. host->sg = sg = sg_next(sg);
  903. if (!sg)
  904. goto done;
  905. offset = 0;
  906. buf = sg_virt(sg);
  907. }
  908. } else {
  909. unsigned int remaining = sg->length - offset;
  910. host->push_data(host, (void *)(buf + offset),
  911. remaining);
  912. nbytes += remaining;
  913. host->sg = sg = sg_next(sg);
  914. if (!sg)
  915. goto done;
  916. offset = len - remaining;
  917. buf = sg_virt(sg);
  918. host->push_data(host, (void *)buf, offset);
  919. nbytes += offset;
  920. }
  921. status = mci_readl(host, MINTSTS);
  922. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  923. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  924. host->data_status = status;
  925. data->bytes_xfered += nbytes;
  926. smp_wmb();
  927. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  928. tasklet_schedule(&host->tasklet);
  929. return;
  930. }
  931. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  932. host->pio_offset = offset;
  933. data->bytes_xfered += nbytes;
  934. return;
  935. done:
  936. data->bytes_xfered += nbytes;
  937. smp_wmb();
  938. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  939. }
  940. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  941. {
  942. if (!host->cmd_status)
  943. host->cmd_status = status;
  944. smp_wmb();
  945. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  946. tasklet_schedule(&host->tasklet);
  947. }
  948. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  949. {
  950. struct dw_mci *host = dev_id;
  951. u32 status, pending;
  952. unsigned int pass_count = 0;
  953. do {
  954. status = mci_readl(host, RINTSTS);
  955. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  956. /*
  957. * DTO fix - version 2.10a and below, and only if internal DMA
  958. * is configured.
  959. */
  960. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  961. if (!pending &&
  962. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  963. pending |= SDMMC_INT_DATA_OVER;
  964. }
  965. if (!pending)
  966. break;
  967. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  968. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  969. host->cmd_status = status;
  970. smp_wmb();
  971. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  972. tasklet_schedule(&host->tasklet);
  973. }
  974. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  975. /* if there is an error report DATA_ERROR */
  976. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  977. host->data_status = status;
  978. smp_wmb();
  979. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  980. tasklet_schedule(&host->tasklet);
  981. }
  982. if (pending & SDMMC_INT_DATA_OVER) {
  983. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  984. if (!host->data_status)
  985. host->data_status = status;
  986. smp_wmb();
  987. if (host->dir_status == DW_MCI_RECV_STATUS) {
  988. if (host->sg != NULL)
  989. dw_mci_read_data_pio(host);
  990. }
  991. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  992. tasklet_schedule(&host->tasklet);
  993. }
  994. if (pending & SDMMC_INT_RXDR) {
  995. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  996. if (host->sg)
  997. dw_mci_read_data_pio(host);
  998. }
  999. if (pending & SDMMC_INT_TXDR) {
  1000. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1001. if (host->sg)
  1002. dw_mci_write_data_pio(host);
  1003. }
  1004. if (pending & SDMMC_INT_CMD_DONE) {
  1005. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1006. dw_mci_cmd_interrupt(host, status);
  1007. }
  1008. if (pending & SDMMC_INT_CD) {
  1009. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1010. tasklet_schedule(&host->card_tasklet);
  1011. }
  1012. } while (pass_count++ < 5);
  1013. #ifdef CONFIG_MMC_DW_IDMAC
  1014. /* Handle DMA interrupts */
  1015. pending = mci_readl(host, IDSTS);
  1016. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1017. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
  1018. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1019. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1020. host->dma_ops->complete(host);
  1021. }
  1022. #endif
  1023. return IRQ_HANDLED;
  1024. }
  1025. static void dw_mci_tasklet_card(unsigned long data)
  1026. {
  1027. struct dw_mci *host = (struct dw_mci *)data;
  1028. int i;
  1029. for (i = 0; i < host->num_slots; i++) {
  1030. struct dw_mci_slot *slot = host->slot[i];
  1031. struct mmc_host *mmc = slot->mmc;
  1032. struct mmc_request *mrq;
  1033. int present;
  1034. u32 ctrl;
  1035. present = dw_mci_get_cd(mmc);
  1036. while (present != slot->last_detect_state) {
  1037. spin_lock(&host->lock);
  1038. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1039. present ? "inserted" : "removed");
  1040. /* Card change detected */
  1041. slot->last_detect_state = present;
  1042. /* Power up slot */
  1043. if (present != 0) {
  1044. if (host->pdata->setpower)
  1045. host->pdata->setpower(slot->id,
  1046. mmc->ocr_avail);
  1047. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1048. }
  1049. /* Clean up queue if present */
  1050. mrq = slot->mrq;
  1051. if (mrq) {
  1052. if (mrq == host->mrq) {
  1053. host->data = NULL;
  1054. host->cmd = NULL;
  1055. switch (host->state) {
  1056. case STATE_IDLE:
  1057. break;
  1058. case STATE_SENDING_CMD:
  1059. mrq->cmd->error = -ENOMEDIUM;
  1060. if (!mrq->data)
  1061. break;
  1062. /* fall through */
  1063. case STATE_SENDING_DATA:
  1064. mrq->data->error = -ENOMEDIUM;
  1065. dw_mci_stop_dma(host);
  1066. break;
  1067. case STATE_DATA_BUSY:
  1068. case STATE_DATA_ERROR:
  1069. if (mrq->data->error == -EINPROGRESS)
  1070. mrq->data->error = -ENOMEDIUM;
  1071. if (!mrq->stop)
  1072. break;
  1073. /* fall through */
  1074. case STATE_SENDING_STOP:
  1075. mrq->stop->error = -ENOMEDIUM;
  1076. break;
  1077. }
  1078. dw_mci_request_end(host, mrq);
  1079. } else {
  1080. list_del(&slot->queue_node);
  1081. mrq->cmd->error = -ENOMEDIUM;
  1082. if (mrq->data)
  1083. mrq->data->error = -ENOMEDIUM;
  1084. if (mrq->stop)
  1085. mrq->stop->error = -ENOMEDIUM;
  1086. spin_unlock(&host->lock);
  1087. mmc_request_done(slot->mmc, mrq);
  1088. spin_lock(&host->lock);
  1089. }
  1090. }
  1091. /* Power down slot */
  1092. if (present == 0) {
  1093. if (host->pdata->setpower)
  1094. host->pdata->setpower(slot->id, 0);
  1095. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1096. /*
  1097. * Clear down the FIFO - doing so generates a
  1098. * block interrupt, hence setting the
  1099. * scatter-gather pointer to NULL.
  1100. */
  1101. host->sg = NULL;
  1102. ctrl = mci_readl(host, CTRL);
  1103. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1104. mci_writel(host, CTRL, ctrl);
  1105. #ifdef CONFIG_MMC_DW_IDMAC
  1106. ctrl = mci_readl(host, BMOD);
  1107. ctrl |= 0x01; /* Software reset of DMA */
  1108. mci_writel(host, BMOD, ctrl);
  1109. #endif
  1110. }
  1111. spin_unlock(&host->lock);
  1112. present = dw_mci_get_cd(mmc);
  1113. }
  1114. mmc_detect_change(slot->mmc,
  1115. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1116. }
  1117. }
  1118. static int __init dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1119. {
  1120. struct mmc_host *mmc;
  1121. struct dw_mci_slot *slot;
  1122. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->pdev->dev);
  1123. if (!mmc)
  1124. return -ENOMEM;
  1125. slot = mmc_priv(mmc);
  1126. slot->id = id;
  1127. slot->mmc = mmc;
  1128. slot->host = host;
  1129. mmc->ops = &dw_mci_ops;
  1130. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
  1131. mmc->f_max = host->bus_hz;
  1132. if (host->pdata->get_ocr)
  1133. mmc->ocr_avail = host->pdata->get_ocr(id);
  1134. else
  1135. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1136. /*
  1137. * Start with slot power disabled, it will be enabled when a card
  1138. * is detected.
  1139. */
  1140. if (host->pdata->setpower)
  1141. host->pdata->setpower(id, 0);
  1142. mmc->caps = 0;
  1143. if (host->pdata->get_bus_wd)
  1144. if (host->pdata->get_bus_wd(slot->id) >= 4)
  1145. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1146. if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
  1147. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1148. #ifdef CONFIG_MMC_DW_IDMAC
  1149. mmc->max_segs = host->ring_size;
  1150. mmc->max_blk_size = 65536;
  1151. mmc->max_blk_count = host->ring_size;
  1152. mmc->max_seg_size = 0x1000;
  1153. mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
  1154. #else
  1155. if (host->pdata->blk_settings) {
  1156. mmc->max_segs = host->pdata->blk_settings->max_segs;
  1157. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  1158. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  1159. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  1160. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  1161. } else {
  1162. /* Useful defaults if platform data is unset. */
  1163. mmc->max_segs = 64;
  1164. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  1165. mmc->max_blk_count = 512;
  1166. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1167. mmc->max_seg_size = mmc->max_req_size;
  1168. }
  1169. #endif /* CONFIG_MMC_DW_IDMAC */
  1170. if (dw_mci_get_cd(mmc))
  1171. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1172. else
  1173. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1174. host->slot[id] = slot;
  1175. mmc_add_host(mmc);
  1176. #if defined(CONFIG_DEBUG_FS)
  1177. dw_mci_init_debugfs(slot);
  1178. #endif
  1179. /* Card initially undetected */
  1180. slot->last_detect_state = 0;
  1181. /*
  1182. * Card may have been plugged in prior to boot so we
  1183. * need to run the detect tasklet
  1184. */
  1185. tasklet_schedule(&host->card_tasklet);
  1186. return 0;
  1187. }
  1188. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  1189. {
  1190. /* Shutdown detect IRQ */
  1191. if (slot->host->pdata->exit)
  1192. slot->host->pdata->exit(id);
  1193. /* Debugfs stuff is cleaned up by mmc core */
  1194. mmc_remove_host(slot->mmc);
  1195. slot->host->slot[id] = NULL;
  1196. mmc_free_host(slot->mmc);
  1197. }
  1198. static void dw_mci_init_dma(struct dw_mci *host)
  1199. {
  1200. /* Alloc memory for sg translation */
  1201. host->sg_cpu = dma_alloc_coherent(&host->pdev->dev, PAGE_SIZE,
  1202. &host->sg_dma, GFP_KERNEL);
  1203. if (!host->sg_cpu) {
  1204. dev_err(&host->pdev->dev, "%s: could not alloc DMA memory\n",
  1205. __func__);
  1206. goto no_dma;
  1207. }
  1208. /* Determine which DMA interface to use */
  1209. #ifdef CONFIG_MMC_DW_IDMAC
  1210. host->dma_ops = &dw_mci_idmac_ops;
  1211. dev_info(&host->pdev->dev, "Using internal DMA controller.\n");
  1212. #endif
  1213. if (!host->dma_ops)
  1214. goto no_dma;
  1215. if (host->dma_ops->init) {
  1216. if (host->dma_ops->init(host)) {
  1217. dev_err(&host->pdev->dev, "%s: Unable to initialize "
  1218. "DMA Controller.\n", __func__);
  1219. goto no_dma;
  1220. }
  1221. } else {
  1222. dev_err(&host->pdev->dev, "DMA initialization not found.\n");
  1223. goto no_dma;
  1224. }
  1225. host->use_dma = 1;
  1226. return;
  1227. no_dma:
  1228. dev_info(&host->pdev->dev, "Using PIO mode.\n");
  1229. host->use_dma = 0;
  1230. return;
  1231. }
  1232. static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
  1233. {
  1234. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  1235. unsigned int ctrl;
  1236. mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1237. SDMMC_CTRL_DMA_RESET));
  1238. /* wait till resets clear */
  1239. do {
  1240. ctrl = mci_readl(host, CTRL);
  1241. if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1242. SDMMC_CTRL_DMA_RESET)))
  1243. return true;
  1244. } while (time_before(jiffies, timeout));
  1245. dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
  1246. return false;
  1247. }
  1248. static int dw_mci_probe(struct platform_device *pdev)
  1249. {
  1250. struct dw_mci *host;
  1251. struct resource *regs;
  1252. struct dw_mci_board *pdata;
  1253. int irq, ret, i, width;
  1254. u32 fifo_size;
  1255. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1256. if (!regs)
  1257. return -ENXIO;
  1258. irq = platform_get_irq(pdev, 0);
  1259. if (irq < 0)
  1260. return irq;
  1261. host = kzalloc(sizeof(struct dw_mci), GFP_KERNEL);
  1262. if (!host)
  1263. return -ENOMEM;
  1264. host->pdev = pdev;
  1265. host->pdata = pdata = pdev->dev.platform_data;
  1266. if (!pdata || !pdata->init) {
  1267. dev_err(&pdev->dev,
  1268. "Platform data must supply init function\n");
  1269. ret = -ENODEV;
  1270. goto err_freehost;
  1271. }
  1272. if (!pdata->select_slot && pdata->num_slots > 1) {
  1273. dev_err(&pdev->dev,
  1274. "Platform data must supply select_slot function\n");
  1275. ret = -ENODEV;
  1276. goto err_freehost;
  1277. }
  1278. if (!pdata->bus_hz) {
  1279. dev_err(&pdev->dev,
  1280. "Platform data must supply bus speed\n");
  1281. ret = -ENODEV;
  1282. goto err_freehost;
  1283. }
  1284. host->bus_hz = pdata->bus_hz;
  1285. host->quirks = pdata->quirks;
  1286. spin_lock_init(&host->lock);
  1287. INIT_LIST_HEAD(&host->queue);
  1288. ret = -ENOMEM;
  1289. host->regs = ioremap(regs->start, regs->end - regs->start + 1);
  1290. if (!host->regs)
  1291. goto err_freehost;
  1292. host->dma_ops = pdata->dma_ops;
  1293. dw_mci_init_dma(host);
  1294. /*
  1295. * Get the host data width - this assumes that HCON has been set with
  1296. * the correct values.
  1297. */
  1298. i = (mci_readl(host, HCON) >> 7) & 0x7;
  1299. if (!i) {
  1300. host->push_data = dw_mci_push_data16;
  1301. host->pull_data = dw_mci_pull_data16;
  1302. width = 16;
  1303. host->data_shift = 1;
  1304. } else if (i == 2) {
  1305. host->push_data = dw_mci_push_data64;
  1306. host->pull_data = dw_mci_pull_data64;
  1307. width = 64;
  1308. host->data_shift = 3;
  1309. } else {
  1310. /* Check for a reserved value, and warn if it is */
  1311. WARN((i != 1),
  1312. "HCON reports a reserved host data width!\n"
  1313. "Defaulting to 32-bit access.\n");
  1314. host->push_data = dw_mci_push_data32;
  1315. host->pull_data = dw_mci_pull_data32;
  1316. width = 32;
  1317. host->data_shift = 2;
  1318. }
  1319. /* Reset all blocks */
  1320. if (!mci_wait_reset(&pdev->dev, host)) {
  1321. ret = -ENODEV;
  1322. goto err_dmaunmap;
  1323. }
  1324. /* Clear the interrupts for the host controller */
  1325. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1326. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1327. /* Put in max timeout */
  1328. mci_writel(host, TMOUT, 0xFFFFFFFF);
  1329. /*
  1330. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  1331. * Tx Mark = fifo_size / 2 DMA Size = 8
  1332. */
  1333. fifo_size = mci_readl(host, FIFOTH);
  1334. fifo_size = (fifo_size >> 16) & 0x7ff;
  1335. mci_writel(host, FIFOTH, ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
  1336. ((fifo_size/2) << 0)));
  1337. /* disable clock to CIU */
  1338. mci_writel(host, CLKENA, 0);
  1339. mci_writel(host, CLKSRC, 0);
  1340. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  1341. tasklet_init(&host->card_tasklet,
  1342. dw_mci_tasklet_card, (unsigned long)host);
  1343. ret = request_irq(irq, dw_mci_interrupt, 0, "dw-mci", host);
  1344. if (ret)
  1345. goto err_dmaunmap;
  1346. platform_set_drvdata(pdev, host);
  1347. if (host->pdata->num_slots)
  1348. host->num_slots = host->pdata->num_slots;
  1349. else
  1350. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  1351. /* We need at least one slot to succeed */
  1352. for (i = 0; i < host->num_slots; i++) {
  1353. ret = dw_mci_init_slot(host, i);
  1354. if (ret) {
  1355. ret = -ENODEV;
  1356. goto err_init_slot;
  1357. }
  1358. }
  1359. /*
  1360. * Enable interrupts for command done, data over, data empty, card det,
  1361. * receive ready and error such as transmit, receive timeout, crc error
  1362. */
  1363. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1364. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1365. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1366. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1367. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  1368. dev_info(&pdev->dev, "DW MMC controller at irq %d, "
  1369. "%d bit host data width\n", irq, width);
  1370. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  1371. dev_info(&pdev->dev, "Internal DMAC interrupt fix enabled.\n");
  1372. return 0;
  1373. err_init_slot:
  1374. /* De-init any initialized slots */
  1375. while (i > 0) {
  1376. if (host->slot[i])
  1377. dw_mci_cleanup_slot(host->slot[i], i);
  1378. i--;
  1379. }
  1380. free_irq(irq, host);
  1381. err_dmaunmap:
  1382. if (host->use_dma && host->dma_ops->exit)
  1383. host->dma_ops->exit(host);
  1384. dma_free_coherent(&host->pdev->dev, PAGE_SIZE,
  1385. host->sg_cpu, host->sg_dma);
  1386. iounmap(host->regs);
  1387. err_freehost:
  1388. kfree(host);
  1389. return ret;
  1390. }
  1391. static int __exit dw_mci_remove(struct platform_device *pdev)
  1392. {
  1393. struct dw_mci *host = platform_get_drvdata(pdev);
  1394. int i;
  1395. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1396. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1397. platform_set_drvdata(pdev, NULL);
  1398. for (i = 0; i < host->num_slots; i++) {
  1399. dev_dbg(&pdev->dev, "remove slot %d\n", i);
  1400. if (host->slot[i])
  1401. dw_mci_cleanup_slot(host->slot[i], i);
  1402. }
  1403. /* disable clock to CIU */
  1404. mci_writel(host, CLKENA, 0);
  1405. mci_writel(host, CLKSRC, 0);
  1406. free_irq(platform_get_irq(pdev, 0), host);
  1407. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  1408. if (host->use_dma && host->dma_ops->exit)
  1409. host->dma_ops->exit(host);
  1410. iounmap(host->regs);
  1411. kfree(host);
  1412. return 0;
  1413. }
  1414. #ifdef CONFIG_PM
  1415. /*
  1416. * TODO: we should probably disable the clock to the card in the suspend path.
  1417. */
  1418. static int dw_mci_suspend(struct platform_device *pdev, pm_message_t mesg)
  1419. {
  1420. int i, ret;
  1421. struct dw_mci *host = platform_get_drvdata(pdev);
  1422. for (i = 0; i < host->num_slots; i++) {
  1423. struct dw_mci_slot *slot = host->slot[i];
  1424. if (!slot)
  1425. continue;
  1426. ret = mmc_suspend_host(slot->mmc);
  1427. if (ret < 0) {
  1428. while (--i >= 0) {
  1429. slot = host->slot[i];
  1430. if (slot)
  1431. mmc_resume_host(host->slot[i]->mmc);
  1432. }
  1433. return ret;
  1434. }
  1435. }
  1436. return 0;
  1437. }
  1438. static int dw_mci_resume(struct platform_device *pdev)
  1439. {
  1440. int i, ret;
  1441. struct dw_mci *host = platform_get_drvdata(pdev);
  1442. for (i = 0; i < host->num_slots; i++) {
  1443. struct dw_mci_slot *slot = host->slot[i];
  1444. if (!slot)
  1445. continue;
  1446. ret = mmc_resume_host(host->slot[i]->mmc);
  1447. if (ret < 0)
  1448. return ret;
  1449. }
  1450. return 0;
  1451. }
  1452. #else
  1453. #define dw_mci_suspend NULL
  1454. #define dw_mci_resume NULL
  1455. #endif /* CONFIG_PM */
  1456. static struct platform_driver dw_mci_driver = {
  1457. .remove = __exit_p(dw_mci_remove),
  1458. .suspend = dw_mci_suspend,
  1459. .resume = dw_mci_resume,
  1460. .driver = {
  1461. .name = "dw_mmc",
  1462. },
  1463. };
  1464. static int __init dw_mci_init(void)
  1465. {
  1466. return platform_driver_probe(&dw_mci_driver, dw_mci_probe);
  1467. }
  1468. static void __exit dw_mci_exit(void)
  1469. {
  1470. platform_driver_unregister(&dw_mci_driver);
  1471. }
  1472. module_init(dw_mci_init);
  1473. module_exit(dw_mci_exit);
  1474. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  1475. MODULE_AUTHOR("NXP Semiconductor VietNam");
  1476. MODULE_AUTHOR("Imagination Technologies Ltd");
  1477. MODULE_LICENSE("GPL v2");