dw_mmc.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806
  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. case MMC_BUS_WIDTH_8:
  549. slot->ctype = SDMMC_CTYPE_8BIT;
  550. break;
  551. }
  552. if (ios->clock) {
  553. /*
  554. * Use mirror of ios->clock to prevent race with mmc
  555. * core ios update when finding the minimum.
  556. */
  557. slot->clock = ios->clock;
  558. }
  559. switch (ios->power_mode) {
  560. case MMC_POWER_UP:
  561. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  562. break;
  563. default:
  564. break;
  565. }
  566. }
  567. static int dw_mci_get_ro(struct mmc_host *mmc)
  568. {
  569. int read_only;
  570. struct dw_mci_slot *slot = mmc_priv(mmc);
  571. struct dw_mci_board *brd = slot->host->pdata;
  572. /* Use platform get_ro function, else try on board write protect */
  573. if (brd->get_ro)
  574. read_only = brd->get_ro(slot->id);
  575. else
  576. read_only =
  577. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  578. dev_dbg(&mmc->class_dev, "card is %s\n",
  579. read_only ? "read-only" : "read-write");
  580. return read_only;
  581. }
  582. static int dw_mci_get_cd(struct mmc_host *mmc)
  583. {
  584. int present;
  585. struct dw_mci_slot *slot = mmc_priv(mmc);
  586. struct dw_mci_board *brd = slot->host->pdata;
  587. /* Use platform get_cd function, else try onboard card detect */
  588. if (brd->get_cd)
  589. present = !brd->get_cd(slot->id);
  590. else
  591. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  592. == 0 ? 1 : 0;
  593. if (present)
  594. dev_dbg(&mmc->class_dev, "card is present\n");
  595. else
  596. dev_dbg(&mmc->class_dev, "card is not present\n");
  597. return present;
  598. }
  599. static const struct mmc_host_ops dw_mci_ops = {
  600. .request = dw_mci_request,
  601. .set_ios = dw_mci_set_ios,
  602. .get_ro = dw_mci_get_ro,
  603. .get_cd = dw_mci_get_cd,
  604. };
  605. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  606. __releases(&host->lock)
  607. __acquires(&host->lock)
  608. {
  609. struct dw_mci_slot *slot;
  610. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  611. WARN_ON(host->cmd || host->data);
  612. host->cur_slot->mrq = NULL;
  613. host->mrq = NULL;
  614. if (!list_empty(&host->queue)) {
  615. slot = list_entry(host->queue.next,
  616. struct dw_mci_slot, queue_node);
  617. list_del(&slot->queue_node);
  618. dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
  619. mmc_hostname(slot->mmc));
  620. host->state = STATE_SENDING_CMD;
  621. dw_mci_start_request(host, slot);
  622. } else {
  623. dev_vdbg(&host->pdev->dev, "list empty\n");
  624. host->state = STATE_IDLE;
  625. }
  626. spin_unlock(&host->lock);
  627. mmc_request_done(prev_mmc, mrq);
  628. spin_lock(&host->lock);
  629. }
  630. static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  631. {
  632. u32 status = host->cmd_status;
  633. host->cmd_status = 0;
  634. /* Read the response from the card (up to 16 bytes) */
  635. if (cmd->flags & MMC_RSP_PRESENT) {
  636. if (cmd->flags & MMC_RSP_136) {
  637. cmd->resp[3] = mci_readl(host, RESP0);
  638. cmd->resp[2] = mci_readl(host, RESP1);
  639. cmd->resp[1] = mci_readl(host, RESP2);
  640. cmd->resp[0] = mci_readl(host, RESP3);
  641. } else {
  642. cmd->resp[0] = mci_readl(host, RESP0);
  643. cmd->resp[1] = 0;
  644. cmd->resp[2] = 0;
  645. cmd->resp[3] = 0;
  646. }
  647. }
  648. if (status & SDMMC_INT_RTO)
  649. cmd->error = -ETIMEDOUT;
  650. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  651. cmd->error = -EILSEQ;
  652. else if (status & SDMMC_INT_RESP_ERR)
  653. cmd->error = -EIO;
  654. else
  655. cmd->error = 0;
  656. if (cmd->error) {
  657. /* newer ip versions need a delay between retries */
  658. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  659. mdelay(20);
  660. if (cmd->data) {
  661. host->data = NULL;
  662. dw_mci_stop_dma(host);
  663. }
  664. }
  665. }
  666. static void dw_mci_tasklet_func(unsigned long priv)
  667. {
  668. struct dw_mci *host = (struct dw_mci *)priv;
  669. struct mmc_data *data;
  670. struct mmc_command *cmd;
  671. enum dw_mci_state state;
  672. enum dw_mci_state prev_state;
  673. u32 status;
  674. spin_lock(&host->lock);
  675. state = host->state;
  676. data = host->data;
  677. do {
  678. prev_state = state;
  679. switch (state) {
  680. case STATE_IDLE:
  681. break;
  682. case STATE_SENDING_CMD:
  683. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  684. &host->pending_events))
  685. break;
  686. cmd = host->cmd;
  687. host->cmd = NULL;
  688. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  689. dw_mci_command_complete(host, host->mrq->cmd);
  690. if (!host->mrq->data || cmd->error) {
  691. dw_mci_request_end(host, host->mrq);
  692. goto unlock;
  693. }
  694. prev_state = state = STATE_SENDING_DATA;
  695. /* fall through */
  696. case STATE_SENDING_DATA:
  697. if (test_and_clear_bit(EVENT_DATA_ERROR,
  698. &host->pending_events)) {
  699. dw_mci_stop_dma(host);
  700. if (data->stop)
  701. send_stop_cmd(host, data);
  702. state = STATE_DATA_ERROR;
  703. break;
  704. }
  705. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  706. &host->pending_events))
  707. break;
  708. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  709. prev_state = state = STATE_DATA_BUSY;
  710. /* fall through */
  711. case STATE_DATA_BUSY:
  712. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  713. &host->pending_events))
  714. break;
  715. host->data = NULL;
  716. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  717. status = host->data_status;
  718. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  719. if (status & SDMMC_INT_DTO) {
  720. dev_err(&host->pdev->dev,
  721. "data timeout error\n");
  722. data->error = -ETIMEDOUT;
  723. } else if (status & SDMMC_INT_DCRC) {
  724. dev_err(&host->pdev->dev,
  725. "data CRC error\n");
  726. data->error = -EILSEQ;
  727. } else {
  728. dev_err(&host->pdev->dev,
  729. "data FIFO error "
  730. "(status=%08x)\n",
  731. status);
  732. data->error = -EIO;
  733. }
  734. } else {
  735. data->bytes_xfered = data->blocks * data->blksz;
  736. data->error = 0;
  737. }
  738. if (!data->stop) {
  739. dw_mci_request_end(host, host->mrq);
  740. goto unlock;
  741. }
  742. prev_state = state = STATE_SENDING_STOP;
  743. if (!data->error)
  744. send_stop_cmd(host, data);
  745. /* fall through */
  746. case STATE_SENDING_STOP:
  747. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  748. &host->pending_events))
  749. break;
  750. host->cmd = NULL;
  751. dw_mci_command_complete(host, host->mrq->stop);
  752. dw_mci_request_end(host, host->mrq);
  753. goto unlock;
  754. case STATE_DATA_ERROR:
  755. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  756. &host->pending_events))
  757. break;
  758. state = STATE_DATA_BUSY;
  759. break;
  760. }
  761. } while (state != prev_state);
  762. host->state = state;
  763. unlock:
  764. spin_unlock(&host->lock);
  765. }
  766. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  767. {
  768. u16 *pdata = (u16 *)buf;
  769. WARN_ON(cnt % 2 != 0);
  770. cnt = cnt >> 1;
  771. while (cnt > 0) {
  772. mci_writew(host, DATA, *pdata++);
  773. cnt--;
  774. }
  775. }
  776. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  777. {
  778. u16 *pdata = (u16 *)buf;
  779. WARN_ON(cnt % 2 != 0);
  780. cnt = cnt >> 1;
  781. while (cnt > 0) {
  782. *pdata++ = mci_readw(host, DATA);
  783. cnt--;
  784. }
  785. }
  786. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  787. {
  788. u32 *pdata = (u32 *)buf;
  789. WARN_ON(cnt % 4 != 0);
  790. WARN_ON((unsigned long)pdata & 0x3);
  791. cnt = cnt >> 2;
  792. while (cnt > 0) {
  793. mci_writel(host, DATA, *pdata++);
  794. cnt--;
  795. }
  796. }
  797. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  798. {
  799. u32 *pdata = (u32 *)buf;
  800. WARN_ON(cnt % 4 != 0);
  801. WARN_ON((unsigned long)pdata & 0x3);
  802. cnt = cnt >> 2;
  803. while (cnt > 0) {
  804. *pdata++ = mci_readl(host, DATA);
  805. cnt--;
  806. }
  807. }
  808. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  809. {
  810. u64 *pdata = (u64 *)buf;
  811. WARN_ON(cnt % 8 != 0);
  812. cnt = cnt >> 3;
  813. while (cnt > 0) {
  814. mci_writeq(host, DATA, *pdata++);
  815. cnt--;
  816. }
  817. }
  818. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  819. {
  820. u64 *pdata = (u64 *)buf;
  821. WARN_ON(cnt % 8 != 0);
  822. cnt = cnt >> 3;
  823. while (cnt > 0) {
  824. *pdata++ = mci_readq(host, DATA);
  825. cnt--;
  826. }
  827. }
  828. static void dw_mci_read_data_pio(struct dw_mci *host)
  829. {
  830. struct scatterlist *sg = host->sg;
  831. void *buf = sg_virt(sg);
  832. unsigned int offset = host->pio_offset;
  833. struct mmc_data *data = host->data;
  834. int shift = host->data_shift;
  835. u32 status;
  836. unsigned int nbytes = 0, len, old_len, count = 0;
  837. do {
  838. len = SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift;
  839. if (count == 0)
  840. old_len = len;
  841. if (offset + len <= sg->length) {
  842. host->pull_data(host, (void *)(buf + offset), len);
  843. offset += len;
  844. nbytes += len;
  845. if (offset == sg->length) {
  846. flush_dcache_page(sg_page(sg));
  847. host->sg = sg = sg_next(sg);
  848. if (!sg)
  849. goto done;
  850. offset = 0;
  851. buf = sg_virt(sg);
  852. }
  853. } else {
  854. unsigned int remaining = sg->length - offset;
  855. host->pull_data(host, (void *)(buf + offset),
  856. remaining);
  857. nbytes += remaining;
  858. flush_dcache_page(sg_page(sg));
  859. host->sg = sg = sg_next(sg);
  860. if (!sg)
  861. goto done;
  862. offset = len - remaining;
  863. buf = sg_virt(sg);
  864. host->pull_data(host, buf, offset);
  865. nbytes += offset;
  866. }
  867. status = mci_readl(host, MINTSTS);
  868. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  869. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  870. host->data_status = status;
  871. data->bytes_xfered += nbytes;
  872. smp_wmb();
  873. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  874. tasklet_schedule(&host->tasklet);
  875. return;
  876. }
  877. count++;
  878. } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/
  879. len = SDMMC_GET_FCNT(mci_readl(host, STATUS));
  880. host->pio_offset = offset;
  881. data->bytes_xfered += nbytes;
  882. return;
  883. done:
  884. data->bytes_xfered += nbytes;
  885. smp_wmb();
  886. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  887. }
  888. static void dw_mci_write_data_pio(struct dw_mci *host)
  889. {
  890. struct scatterlist *sg = host->sg;
  891. void *buf = sg_virt(sg);
  892. unsigned int offset = host->pio_offset;
  893. struct mmc_data *data = host->data;
  894. int shift = host->data_shift;
  895. u32 status;
  896. unsigned int nbytes = 0, len;
  897. do {
  898. len = SDMMC_FIFO_SZ -
  899. (SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift);
  900. if (offset + len <= sg->length) {
  901. host->push_data(host, (void *)(buf + offset), len);
  902. offset += len;
  903. nbytes += len;
  904. if (offset == sg->length) {
  905. host->sg = sg = sg_next(sg);
  906. if (!sg)
  907. goto done;
  908. offset = 0;
  909. buf = sg_virt(sg);
  910. }
  911. } else {
  912. unsigned int remaining = sg->length - offset;
  913. host->push_data(host, (void *)(buf + offset),
  914. remaining);
  915. nbytes += remaining;
  916. host->sg = sg = sg_next(sg);
  917. if (!sg)
  918. goto done;
  919. offset = len - remaining;
  920. buf = sg_virt(sg);
  921. host->push_data(host, (void *)buf, offset);
  922. nbytes += offset;
  923. }
  924. status = mci_readl(host, MINTSTS);
  925. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  926. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  927. host->data_status = status;
  928. data->bytes_xfered += nbytes;
  929. smp_wmb();
  930. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  931. tasklet_schedule(&host->tasklet);
  932. return;
  933. }
  934. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  935. host->pio_offset = offset;
  936. data->bytes_xfered += nbytes;
  937. return;
  938. done:
  939. data->bytes_xfered += nbytes;
  940. smp_wmb();
  941. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  942. }
  943. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  944. {
  945. if (!host->cmd_status)
  946. host->cmd_status = status;
  947. smp_wmb();
  948. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  949. tasklet_schedule(&host->tasklet);
  950. }
  951. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  952. {
  953. struct dw_mci *host = dev_id;
  954. u32 status, pending;
  955. unsigned int pass_count = 0;
  956. do {
  957. status = mci_readl(host, RINTSTS);
  958. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  959. /*
  960. * DTO fix - version 2.10a and below, and only if internal DMA
  961. * is configured.
  962. */
  963. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  964. if (!pending &&
  965. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  966. pending |= SDMMC_INT_DATA_OVER;
  967. }
  968. if (!pending)
  969. break;
  970. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  971. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  972. host->cmd_status = status;
  973. smp_wmb();
  974. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  975. tasklet_schedule(&host->tasklet);
  976. }
  977. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  978. /* if there is an error report DATA_ERROR */
  979. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  980. host->data_status = status;
  981. smp_wmb();
  982. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  983. tasklet_schedule(&host->tasklet);
  984. }
  985. if (pending & SDMMC_INT_DATA_OVER) {
  986. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  987. if (!host->data_status)
  988. host->data_status = status;
  989. smp_wmb();
  990. if (host->dir_status == DW_MCI_RECV_STATUS) {
  991. if (host->sg != NULL)
  992. dw_mci_read_data_pio(host);
  993. }
  994. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  995. tasklet_schedule(&host->tasklet);
  996. }
  997. if (pending & SDMMC_INT_RXDR) {
  998. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  999. if (host->sg)
  1000. dw_mci_read_data_pio(host);
  1001. }
  1002. if (pending & SDMMC_INT_TXDR) {
  1003. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1004. if (host->sg)
  1005. dw_mci_write_data_pio(host);
  1006. }
  1007. if (pending & SDMMC_INT_CMD_DONE) {
  1008. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1009. dw_mci_cmd_interrupt(host, status);
  1010. }
  1011. if (pending & SDMMC_INT_CD) {
  1012. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1013. tasklet_schedule(&host->card_tasklet);
  1014. }
  1015. } while (pass_count++ < 5);
  1016. #ifdef CONFIG_MMC_DW_IDMAC
  1017. /* Handle DMA interrupts */
  1018. pending = mci_readl(host, IDSTS);
  1019. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1020. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
  1021. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1022. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1023. host->dma_ops->complete(host);
  1024. }
  1025. #endif
  1026. return IRQ_HANDLED;
  1027. }
  1028. static void dw_mci_tasklet_card(unsigned long data)
  1029. {
  1030. struct dw_mci *host = (struct dw_mci *)data;
  1031. int i;
  1032. for (i = 0; i < host->num_slots; i++) {
  1033. struct dw_mci_slot *slot = host->slot[i];
  1034. struct mmc_host *mmc = slot->mmc;
  1035. struct mmc_request *mrq;
  1036. int present;
  1037. u32 ctrl;
  1038. present = dw_mci_get_cd(mmc);
  1039. while (present != slot->last_detect_state) {
  1040. spin_lock(&host->lock);
  1041. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1042. present ? "inserted" : "removed");
  1043. /* Card change detected */
  1044. slot->last_detect_state = present;
  1045. /* Power up slot */
  1046. if (present != 0) {
  1047. if (host->pdata->setpower)
  1048. host->pdata->setpower(slot->id,
  1049. mmc->ocr_avail);
  1050. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1051. }
  1052. /* Clean up queue if present */
  1053. mrq = slot->mrq;
  1054. if (mrq) {
  1055. if (mrq == host->mrq) {
  1056. host->data = NULL;
  1057. host->cmd = NULL;
  1058. switch (host->state) {
  1059. case STATE_IDLE:
  1060. break;
  1061. case STATE_SENDING_CMD:
  1062. mrq->cmd->error = -ENOMEDIUM;
  1063. if (!mrq->data)
  1064. break;
  1065. /* fall through */
  1066. case STATE_SENDING_DATA:
  1067. mrq->data->error = -ENOMEDIUM;
  1068. dw_mci_stop_dma(host);
  1069. break;
  1070. case STATE_DATA_BUSY:
  1071. case STATE_DATA_ERROR:
  1072. if (mrq->data->error == -EINPROGRESS)
  1073. mrq->data->error = -ENOMEDIUM;
  1074. if (!mrq->stop)
  1075. break;
  1076. /* fall through */
  1077. case STATE_SENDING_STOP:
  1078. mrq->stop->error = -ENOMEDIUM;
  1079. break;
  1080. }
  1081. dw_mci_request_end(host, mrq);
  1082. } else {
  1083. list_del(&slot->queue_node);
  1084. mrq->cmd->error = -ENOMEDIUM;
  1085. if (mrq->data)
  1086. mrq->data->error = -ENOMEDIUM;
  1087. if (mrq->stop)
  1088. mrq->stop->error = -ENOMEDIUM;
  1089. spin_unlock(&host->lock);
  1090. mmc_request_done(slot->mmc, mrq);
  1091. spin_lock(&host->lock);
  1092. }
  1093. }
  1094. /* Power down slot */
  1095. if (present == 0) {
  1096. if (host->pdata->setpower)
  1097. host->pdata->setpower(slot->id, 0);
  1098. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1099. /*
  1100. * Clear down the FIFO - doing so generates a
  1101. * block interrupt, hence setting the
  1102. * scatter-gather pointer to NULL.
  1103. */
  1104. host->sg = NULL;
  1105. ctrl = mci_readl(host, CTRL);
  1106. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1107. mci_writel(host, CTRL, ctrl);
  1108. #ifdef CONFIG_MMC_DW_IDMAC
  1109. ctrl = mci_readl(host, BMOD);
  1110. ctrl |= 0x01; /* Software reset of DMA */
  1111. mci_writel(host, BMOD, ctrl);
  1112. #endif
  1113. }
  1114. spin_unlock(&host->lock);
  1115. present = dw_mci_get_cd(mmc);
  1116. }
  1117. mmc_detect_change(slot->mmc,
  1118. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1119. }
  1120. }
  1121. static int __init dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1122. {
  1123. struct mmc_host *mmc;
  1124. struct dw_mci_slot *slot;
  1125. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->pdev->dev);
  1126. if (!mmc)
  1127. return -ENOMEM;
  1128. slot = mmc_priv(mmc);
  1129. slot->id = id;
  1130. slot->mmc = mmc;
  1131. slot->host = host;
  1132. mmc->ops = &dw_mci_ops;
  1133. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
  1134. mmc->f_max = host->bus_hz;
  1135. if (host->pdata->get_ocr)
  1136. mmc->ocr_avail = host->pdata->get_ocr(id);
  1137. else
  1138. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1139. /*
  1140. * Start with slot power disabled, it will be enabled when a card
  1141. * is detected.
  1142. */
  1143. if (host->pdata->setpower)
  1144. host->pdata->setpower(id, 0);
  1145. mmc->caps = 0;
  1146. if (host->pdata->get_bus_wd)
  1147. if (host->pdata->get_bus_wd(slot->id) >= 4)
  1148. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1149. if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
  1150. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1151. #ifdef CONFIG_MMC_DW_IDMAC
  1152. mmc->max_segs = host->ring_size;
  1153. mmc->max_blk_size = 65536;
  1154. mmc->max_blk_count = host->ring_size;
  1155. mmc->max_seg_size = 0x1000;
  1156. mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
  1157. #else
  1158. if (host->pdata->blk_settings) {
  1159. mmc->max_segs = host->pdata->blk_settings->max_segs;
  1160. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  1161. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  1162. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  1163. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  1164. } else {
  1165. /* Useful defaults if platform data is unset. */
  1166. mmc->max_segs = 64;
  1167. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  1168. mmc->max_blk_count = 512;
  1169. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1170. mmc->max_seg_size = mmc->max_req_size;
  1171. }
  1172. #endif /* CONFIG_MMC_DW_IDMAC */
  1173. if (dw_mci_get_cd(mmc))
  1174. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1175. else
  1176. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1177. host->slot[id] = slot;
  1178. mmc_add_host(mmc);
  1179. #if defined(CONFIG_DEBUG_FS)
  1180. dw_mci_init_debugfs(slot);
  1181. #endif
  1182. /* Card initially undetected */
  1183. slot->last_detect_state = 0;
  1184. /*
  1185. * Card may have been plugged in prior to boot so we
  1186. * need to run the detect tasklet
  1187. */
  1188. tasklet_schedule(&host->card_tasklet);
  1189. return 0;
  1190. }
  1191. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  1192. {
  1193. /* Shutdown detect IRQ */
  1194. if (slot->host->pdata->exit)
  1195. slot->host->pdata->exit(id);
  1196. /* Debugfs stuff is cleaned up by mmc core */
  1197. mmc_remove_host(slot->mmc);
  1198. slot->host->slot[id] = NULL;
  1199. mmc_free_host(slot->mmc);
  1200. }
  1201. static void dw_mci_init_dma(struct dw_mci *host)
  1202. {
  1203. /* Alloc memory for sg translation */
  1204. host->sg_cpu = dma_alloc_coherent(&host->pdev->dev, PAGE_SIZE,
  1205. &host->sg_dma, GFP_KERNEL);
  1206. if (!host->sg_cpu) {
  1207. dev_err(&host->pdev->dev, "%s: could not alloc DMA memory\n",
  1208. __func__);
  1209. goto no_dma;
  1210. }
  1211. /* Determine which DMA interface to use */
  1212. #ifdef CONFIG_MMC_DW_IDMAC
  1213. host->dma_ops = &dw_mci_idmac_ops;
  1214. dev_info(&host->pdev->dev, "Using internal DMA controller.\n");
  1215. #endif
  1216. if (!host->dma_ops)
  1217. goto no_dma;
  1218. if (host->dma_ops->init) {
  1219. if (host->dma_ops->init(host)) {
  1220. dev_err(&host->pdev->dev, "%s: Unable to initialize "
  1221. "DMA Controller.\n", __func__);
  1222. goto no_dma;
  1223. }
  1224. } else {
  1225. dev_err(&host->pdev->dev, "DMA initialization not found.\n");
  1226. goto no_dma;
  1227. }
  1228. host->use_dma = 1;
  1229. return;
  1230. no_dma:
  1231. dev_info(&host->pdev->dev, "Using PIO mode.\n");
  1232. host->use_dma = 0;
  1233. return;
  1234. }
  1235. static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
  1236. {
  1237. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  1238. unsigned int ctrl;
  1239. mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1240. SDMMC_CTRL_DMA_RESET));
  1241. /* wait till resets clear */
  1242. do {
  1243. ctrl = mci_readl(host, CTRL);
  1244. if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1245. SDMMC_CTRL_DMA_RESET)))
  1246. return true;
  1247. } while (time_before(jiffies, timeout));
  1248. dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
  1249. return false;
  1250. }
  1251. static int dw_mci_probe(struct platform_device *pdev)
  1252. {
  1253. struct dw_mci *host;
  1254. struct resource *regs;
  1255. struct dw_mci_board *pdata;
  1256. int irq, ret, i, width;
  1257. u32 fifo_size;
  1258. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1259. if (!regs)
  1260. return -ENXIO;
  1261. irq = platform_get_irq(pdev, 0);
  1262. if (irq < 0)
  1263. return irq;
  1264. host = kzalloc(sizeof(struct dw_mci), GFP_KERNEL);
  1265. if (!host)
  1266. return -ENOMEM;
  1267. host->pdev = pdev;
  1268. host->pdata = pdata = pdev->dev.platform_data;
  1269. if (!pdata || !pdata->init) {
  1270. dev_err(&pdev->dev,
  1271. "Platform data must supply init function\n");
  1272. ret = -ENODEV;
  1273. goto err_freehost;
  1274. }
  1275. if (!pdata->select_slot && pdata->num_slots > 1) {
  1276. dev_err(&pdev->dev,
  1277. "Platform data must supply select_slot function\n");
  1278. ret = -ENODEV;
  1279. goto err_freehost;
  1280. }
  1281. if (!pdata->bus_hz) {
  1282. dev_err(&pdev->dev,
  1283. "Platform data must supply bus speed\n");
  1284. ret = -ENODEV;
  1285. goto err_freehost;
  1286. }
  1287. host->bus_hz = pdata->bus_hz;
  1288. host->quirks = pdata->quirks;
  1289. spin_lock_init(&host->lock);
  1290. INIT_LIST_HEAD(&host->queue);
  1291. ret = -ENOMEM;
  1292. host->regs = ioremap(regs->start, regs->end - regs->start + 1);
  1293. if (!host->regs)
  1294. goto err_freehost;
  1295. host->dma_ops = pdata->dma_ops;
  1296. dw_mci_init_dma(host);
  1297. /*
  1298. * Get the host data width - this assumes that HCON has been set with
  1299. * the correct values.
  1300. */
  1301. i = (mci_readl(host, HCON) >> 7) & 0x7;
  1302. if (!i) {
  1303. host->push_data = dw_mci_push_data16;
  1304. host->pull_data = dw_mci_pull_data16;
  1305. width = 16;
  1306. host->data_shift = 1;
  1307. } else if (i == 2) {
  1308. host->push_data = dw_mci_push_data64;
  1309. host->pull_data = dw_mci_pull_data64;
  1310. width = 64;
  1311. host->data_shift = 3;
  1312. } else {
  1313. /* Check for a reserved value, and warn if it is */
  1314. WARN((i != 1),
  1315. "HCON reports a reserved host data width!\n"
  1316. "Defaulting to 32-bit access.\n");
  1317. host->push_data = dw_mci_push_data32;
  1318. host->pull_data = dw_mci_pull_data32;
  1319. width = 32;
  1320. host->data_shift = 2;
  1321. }
  1322. /* Reset all blocks */
  1323. if (!mci_wait_reset(&pdev->dev, host)) {
  1324. ret = -ENODEV;
  1325. goto err_dmaunmap;
  1326. }
  1327. /* Clear the interrupts for the host controller */
  1328. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1329. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1330. /* Put in max timeout */
  1331. mci_writel(host, TMOUT, 0xFFFFFFFF);
  1332. /*
  1333. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  1334. * Tx Mark = fifo_size / 2 DMA Size = 8
  1335. */
  1336. fifo_size = mci_readl(host, FIFOTH);
  1337. fifo_size = (fifo_size >> 16) & 0x7ff;
  1338. mci_writel(host, FIFOTH, ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
  1339. ((fifo_size/2) << 0)));
  1340. /* disable clock to CIU */
  1341. mci_writel(host, CLKENA, 0);
  1342. mci_writel(host, CLKSRC, 0);
  1343. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  1344. tasklet_init(&host->card_tasklet,
  1345. dw_mci_tasklet_card, (unsigned long)host);
  1346. ret = request_irq(irq, dw_mci_interrupt, 0, "dw-mci", host);
  1347. if (ret)
  1348. goto err_dmaunmap;
  1349. platform_set_drvdata(pdev, host);
  1350. if (host->pdata->num_slots)
  1351. host->num_slots = host->pdata->num_slots;
  1352. else
  1353. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  1354. /* We need at least one slot to succeed */
  1355. for (i = 0; i < host->num_slots; i++) {
  1356. ret = dw_mci_init_slot(host, i);
  1357. if (ret) {
  1358. ret = -ENODEV;
  1359. goto err_init_slot;
  1360. }
  1361. }
  1362. /*
  1363. * Enable interrupts for command done, data over, data empty, card det,
  1364. * receive ready and error such as transmit, receive timeout, crc error
  1365. */
  1366. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1367. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1368. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1369. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1370. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  1371. dev_info(&pdev->dev, "DW MMC controller at irq %d, "
  1372. "%d bit host data width\n", irq, width);
  1373. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  1374. dev_info(&pdev->dev, "Internal DMAC interrupt fix enabled.\n");
  1375. return 0;
  1376. err_init_slot:
  1377. /* De-init any initialized slots */
  1378. while (i > 0) {
  1379. if (host->slot[i])
  1380. dw_mci_cleanup_slot(host->slot[i], i);
  1381. i--;
  1382. }
  1383. free_irq(irq, host);
  1384. err_dmaunmap:
  1385. if (host->use_dma && host->dma_ops->exit)
  1386. host->dma_ops->exit(host);
  1387. dma_free_coherent(&host->pdev->dev, PAGE_SIZE,
  1388. host->sg_cpu, host->sg_dma);
  1389. iounmap(host->regs);
  1390. err_freehost:
  1391. kfree(host);
  1392. return ret;
  1393. }
  1394. static int __exit dw_mci_remove(struct platform_device *pdev)
  1395. {
  1396. struct dw_mci *host = platform_get_drvdata(pdev);
  1397. int i;
  1398. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1399. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1400. platform_set_drvdata(pdev, NULL);
  1401. for (i = 0; i < host->num_slots; i++) {
  1402. dev_dbg(&pdev->dev, "remove slot %d\n", i);
  1403. if (host->slot[i])
  1404. dw_mci_cleanup_slot(host->slot[i], i);
  1405. }
  1406. /* disable clock to CIU */
  1407. mci_writel(host, CLKENA, 0);
  1408. mci_writel(host, CLKSRC, 0);
  1409. free_irq(platform_get_irq(pdev, 0), host);
  1410. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  1411. if (host->use_dma && host->dma_ops->exit)
  1412. host->dma_ops->exit(host);
  1413. iounmap(host->regs);
  1414. kfree(host);
  1415. return 0;
  1416. }
  1417. #ifdef CONFIG_PM
  1418. /*
  1419. * TODO: we should probably disable the clock to the card in the suspend path.
  1420. */
  1421. static int dw_mci_suspend(struct platform_device *pdev, pm_message_t mesg)
  1422. {
  1423. int i, ret;
  1424. struct dw_mci *host = platform_get_drvdata(pdev);
  1425. for (i = 0; i < host->num_slots; i++) {
  1426. struct dw_mci_slot *slot = host->slot[i];
  1427. if (!slot)
  1428. continue;
  1429. ret = mmc_suspend_host(slot->mmc);
  1430. if (ret < 0) {
  1431. while (--i >= 0) {
  1432. slot = host->slot[i];
  1433. if (slot)
  1434. mmc_resume_host(host->slot[i]->mmc);
  1435. }
  1436. return ret;
  1437. }
  1438. }
  1439. return 0;
  1440. }
  1441. static int dw_mci_resume(struct platform_device *pdev)
  1442. {
  1443. int i, ret;
  1444. struct dw_mci *host = platform_get_drvdata(pdev);
  1445. for (i = 0; i < host->num_slots; i++) {
  1446. struct dw_mci_slot *slot = host->slot[i];
  1447. if (!slot)
  1448. continue;
  1449. ret = mmc_resume_host(host->slot[i]->mmc);
  1450. if (ret < 0)
  1451. return ret;
  1452. }
  1453. return 0;
  1454. }
  1455. #else
  1456. #define dw_mci_suspend NULL
  1457. #define dw_mci_resume NULL
  1458. #endif /* CONFIG_PM */
  1459. static struct platform_driver dw_mci_driver = {
  1460. .remove = __exit_p(dw_mci_remove),
  1461. .suspend = dw_mci_suspend,
  1462. .resume = dw_mci_resume,
  1463. .driver = {
  1464. .name = "dw_mmc",
  1465. },
  1466. };
  1467. static int __init dw_mci_init(void)
  1468. {
  1469. return platform_driver_probe(&dw_mci_driver, dw_mci_probe);
  1470. }
  1471. static void __exit dw_mci_exit(void)
  1472. {
  1473. platform_driver_unregister(&dw_mci_driver);
  1474. }
  1475. module_init(dw_mci_init);
  1476. module_exit(dw_mci_exit);
  1477. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  1478. MODULE_AUTHOR("NXP Semiconductor VietNam");
  1479. MODULE_AUTHOR("Imagination Technologies Ltd");
  1480. MODULE_LICENSE("GPL v2");