dw_mmc.c 52 KB

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