dw_mmc.c 42 KB

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