dw_mmc.c 53 KB

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