dw_mmc.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188
  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. struct mmc_command *cmd)
  483. {
  484. struct mmc_request *mrq;
  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 = cmd->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. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  504. /* this is the first command, send the initialization clock */
  505. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  506. cmdflags |= SDMMC_CMD_INIT;
  507. if (data) {
  508. dw_mci_submit_data(host, data);
  509. wmb();
  510. }
  511. dw_mci_start_command(host, cmd, cmdflags);
  512. if (mrq->stop)
  513. host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
  514. }
  515. static void dw_mci_start_request(struct dw_mci *host,
  516. struct dw_mci_slot *slot)
  517. {
  518. struct mmc_request *mrq = slot->mrq;
  519. struct mmc_command *cmd;
  520. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  521. __dw_mci_start_request(host, slot, cmd);
  522. }
  523. /* must be called with host->lock held */
  524. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  525. struct mmc_request *mrq)
  526. {
  527. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  528. host->state);
  529. slot->mrq = mrq;
  530. if (host->state == STATE_IDLE) {
  531. host->state = STATE_SENDING_CMD;
  532. dw_mci_start_request(host, slot);
  533. } else {
  534. list_add_tail(&slot->queue_node, &host->queue);
  535. }
  536. }
  537. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  538. {
  539. struct dw_mci_slot *slot = mmc_priv(mmc);
  540. struct dw_mci *host = slot->host;
  541. WARN_ON(slot->mrq);
  542. /*
  543. * The check for card presence and queueing of the request must be
  544. * atomic, otherwise the card could be removed in between and the
  545. * request wouldn't fail until another card was inserted.
  546. */
  547. spin_lock_bh(&host->lock);
  548. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  549. spin_unlock_bh(&host->lock);
  550. mrq->cmd->error = -ENOMEDIUM;
  551. mmc_request_done(mmc, mrq);
  552. return;
  553. }
  554. dw_mci_queue_request(host, slot, mrq);
  555. spin_unlock_bh(&host->lock);
  556. }
  557. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  558. {
  559. struct dw_mci_slot *slot = mmc_priv(mmc);
  560. u32 regs;
  561. /* set default 1 bit mode */
  562. slot->ctype = SDMMC_CTYPE_1BIT;
  563. switch (ios->bus_width) {
  564. case MMC_BUS_WIDTH_1:
  565. slot->ctype = SDMMC_CTYPE_1BIT;
  566. break;
  567. case MMC_BUS_WIDTH_4:
  568. slot->ctype = SDMMC_CTYPE_4BIT;
  569. break;
  570. case MMC_BUS_WIDTH_8:
  571. slot->ctype = SDMMC_CTYPE_8BIT;
  572. break;
  573. }
  574. regs = mci_readl(slot->host, UHS_REG);
  575. /* DDR mode set */
  576. if (ios->timing == MMC_TIMING_UHS_DDR50)
  577. regs |= (0x1 << slot->id) << 16;
  578. else
  579. regs &= ~(0x1 << slot->id) << 16;
  580. mci_writel(slot->host, UHS_REG, regs);
  581. if (ios->clock) {
  582. /*
  583. * Use mirror of ios->clock to prevent race with mmc
  584. * core ios update when finding the minimum.
  585. */
  586. slot->clock = ios->clock;
  587. }
  588. switch (ios->power_mode) {
  589. case MMC_POWER_UP:
  590. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  591. break;
  592. default:
  593. break;
  594. }
  595. }
  596. static int dw_mci_get_ro(struct mmc_host *mmc)
  597. {
  598. int read_only;
  599. struct dw_mci_slot *slot = mmc_priv(mmc);
  600. struct dw_mci_board *brd = slot->host->pdata;
  601. /* Use platform get_ro function, else try on board write protect */
  602. if (brd->get_ro)
  603. read_only = brd->get_ro(slot->id);
  604. else
  605. read_only =
  606. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  607. dev_dbg(&mmc->class_dev, "card is %s\n",
  608. read_only ? "read-only" : "read-write");
  609. return read_only;
  610. }
  611. static int dw_mci_get_cd(struct mmc_host *mmc)
  612. {
  613. int present;
  614. struct dw_mci_slot *slot = mmc_priv(mmc);
  615. struct dw_mci_board *brd = slot->host->pdata;
  616. /* Use platform get_cd function, else try onboard card detect */
  617. if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  618. present = 1;
  619. else if (brd->get_cd)
  620. present = !brd->get_cd(slot->id);
  621. else
  622. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  623. == 0 ? 1 : 0;
  624. if (present)
  625. dev_dbg(&mmc->class_dev, "card is present\n");
  626. else
  627. dev_dbg(&mmc->class_dev, "card is not present\n");
  628. return present;
  629. }
  630. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  631. {
  632. struct dw_mci_slot *slot = mmc_priv(mmc);
  633. struct dw_mci *host = slot->host;
  634. u32 int_mask;
  635. /* Enable/disable Slot Specific SDIO interrupt */
  636. int_mask = mci_readl(host, INTMASK);
  637. if (enb) {
  638. mci_writel(host, INTMASK,
  639. (int_mask | (1 << SDMMC_INT_SDIO(slot->id))));
  640. } else {
  641. mci_writel(host, INTMASK,
  642. (int_mask & ~(1 << SDMMC_INT_SDIO(slot->id))));
  643. }
  644. }
  645. static const struct mmc_host_ops dw_mci_ops = {
  646. .request = dw_mci_request,
  647. .set_ios = dw_mci_set_ios,
  648. .get_ro = dw_mci_get_ro,
  649. .get_cd = dw_mci_get_cd,
  650. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  651. };
  652. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  653. __releases(&host->lock)
  654. __acquires(&host->lock)
  655. {
  656. struct dw_mci_slot *slot;
  657. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  658. WARN_ON(host->cmd || host->data);
  659. host->cur_slot->mrq = NULL;
  660. host->mrq = NULL;
  661. if (!list_empty(&host->queue)) {
  662. slot = list_entry(host->queue.next,
  663. struct dw_mci_slot, queue_node);
  664. list_del(&slot->queue_node);
  665. dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
  666. mmc_hostname(slot->mmc));
  667. host->state = STATE_SENDING_CMD;
  668. dw_mci_start_request(host, slot);
  669. } else {
  670. dev_vdbg(&host->pdev->dev, "list empty\n");
  671. host->state = STATE_IDLE;
  672. }
  673. spin_unlock(&host->lock);
  674. mmc_request_done(prev_mmc, mrq);
  675. spin_lock(&host->lock);
  676. }
  677. static void dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  678. {
  679. u32 status = host->cmd_status;
  680. host->cmd_status = 0;
  681. /* Read the response from the card (up to 16 bytes) */
  682. if (cmd->flags & MMC_RSP_PRESENT) {
  683. if (cmd->flags & MMC_RSP_136) {
  684. cmd->resp[3] = mci_readl(host, RESP0);
  685. cmd->resp[2] = mci_readl(host, RESP1);
  686. cmd->resp[1] = mci_readl(host, RESP2);
  687. cmd->resp[0] = mci_readl(host, RESP3);
  688. } else {
  689. cmd->resp[0] = mci_readl(host, RESP0);
  690. cmd->resp[1] = 0;
  691. cmd->resp[2] = 0;
  692. cmd->resp[3] = 0;
  693. }
  694. }
  695. if (status & SDMMC_INT_RTO)
  696. cmd->error = -ETIMEDOUT;
  697. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  698. cmd->error = -EILSEQ;
  699. else if (status & SDMMC_INT_RESP_ERR)
  700. cmd->error = -EIO;
  701. else
  702. cmd->error = 0;
  703. if (cmd->error) {
  704. /* newer ip versions need a delay between retries */
  705. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  706. mdelay(20);
  707. if (cmd->data) {
  708. host->data = NULL;
  709. dw_mci_stop_dma(host);
  710. }
  711. }
  712. }
  713. static void dw_mci_tasklet_func(unsigned long priv)
  714. {
  715. struct dw_mci *host = (struct dw_mci *)priv;
  716. struct mmc_data *data;
  717. struct mmc_command *cmd;
  718. enum dw_mci_state state;
  719. enum dw_mci_state prev_state;
  720. u32 status, ctrl;
  721. spin_lock(&host->lock);
  722. state = host->state;
  723. data = host->data;
  724. do {
  725. prev_state = state;
  726. switch (state) {
  727. case STATE_IDLE:
  728. break;
  729. case STATE_SENDING_CMD:
  730. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  731. &host->pending_events))
  732. break;
  733. cmd = host->cmd;
  734. host->cmd = NULL;
  735. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  736. dw_mci_command_complete(host, cmd);
  737. if (cmd == host->mrq->sbc && !cmd->error) {
  738. prev_state = state = STATE_SENDING_CMD;
  739. __dw_mci_start_request(host, host->cur_slot,
  740. host->mrq->cmd);
  741. goto unlock;
  742. }
  743. if (!host->mrq->data || cmd->error) {
  744. dw_mci_request_end(host, host->mrq);
  745. goto unlock;
  746. }
  747. prev_state = state = STATE_SENDING_DATA;
  748. /* fall through */
  749. case STATE_SENDING_DATA:
  750. if (test_and_clear_bit(EVENT_DATA_ERROR,
  751. &host->pending_events)) {
  752. dw_mci_stop_dma(host);
  753. if (data->stop)
  754. send_stop_cmd(host, data);
  755. state = STATE_DATA_ERROR;
  756. break;
  757. }
  758. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  759. &host->pending_events))
  760. break;
  761. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  762. prev_state = state = STATE_DATA_BUSY;
  763. /* fall through */
  764. case STATE_DATA_BUSY:
  765. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  766. &host->pending_events))
  767. break;
  768. host->data = NULL;
  769. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  770. status = host->data_status;
  771. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  772. if (status & SDMMC_INT_DTO) {
  773. data->error = -ETIMEDOUT;
  774. } else if (status & SDMMC_INT_DCRC) {
  775. data->error = -EILSEQ;
  776. } else if (status & SDMMC_INT_EBE &&
  777. host->dir_status ==
  778. DW_MCI_SEND_STATUS) {
  779. /*
  780. * No data CRC status was returned.
  781. * The number of bytes transferred will
  782. * be exaggerated in PIO mode.
  783. */
  784. data->bytes_xfered = 0;
  785. data->error = -ETIMEDOUT;
  786. } else {
  787. dev_err(&host->pdev->dev,
  788. "data FIFO error "
  789. "(status=%08x)\n",
  790. status);
  791. data->error = -EIO;
  792. }
  793. /*
  794. * After an error, there may be data lingering
  795. * in the FIFO, so reset it - doing so
  796. * generates a block interrupt, hence setting
  797. * the scatter-gather pointer to NULL.
  798. */
  799. host->sg = NULL;
  800. ctrl = mci_readl(host, CTRL);
  801. ctrl |= SDMMC_CTRL_FIFO_RESET;
  802. mci_writel(host, CTRL, ctrl);
  803. } else {
  804. data->bytes_xfered = data->blocks * data->blksz;
  805. data->error = 0;
  806. }
  807. if (!data->stop) {
  808. dw_mci_request_end(host, host->mrq);
  809. goto unlock;
  810. }
  811. if (host->mrq->sbc && !data->error) {
  812. data->stop->error = 0;
  813. dw_mci_request_end(host, host->mrq);
  814. goto unlock;
  815. }
  816. prev_state = state = STATE_SENDING_STOP;
  817. if (!data->error)
  818. send_stop_cmd(host, data);
  819. /* fall through */
  820. case STATE_SENDING_STOP:
  821. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  822. &host->pending_events))
  823. break;
  824. host->cmd = NULL;
  825. dw_mci_command_complete(host, host->mrq->stop);
  826. dw_mci_request_end(host, host->mrq);
  827. goto unlock;
  828. case STATE_DATA_ERROR:
  829. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  830. &host->pending_events))
  831. break;
  832. state = STATE_DATA_BUSY;
  833. break;
  834. }
  835. } while (state != prev_state);
  836. host->state = state;
  837. unlock:
  838. spin_unlock(&host->lock);
  839. }
  840. /* push final bytes to part_buf, only use during push */
  841. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  842. {
  843. memcpy((void *)&host->part_buf, buf, cnt);
  844. host->part_buf_count = cnt;
  845. }
  846. /* append bytes to part_buf, only use during push */
  847. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  848. {
  849. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  850. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  851. host->part_buf_count += cnt;
  852. return cnt;
  853. }
  854. /* pull first bytes from part_buf, only use during pull */
  855. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  856. {
  857. cnt = min(cnt, (int)host->part_buf_count);
  858. if (cnt) {
  859. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  860. cnt);
  861. host->part_buf_count -= cnt;
  862. host->part_buf_start += cnt;
  863. }
  864. return cnt;
  865. }
  866. /* pull final bytes from the part_buf, assuming it's just been filled */
  867. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  868. {
  869. memcpy(buf, &host->part_buf, cnt);
  870. host->part_buf_start = cnt;
  871. host->part_buf_count = (1 << host->data_shift) - cnt;
  872. }
  873. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  874. {
  875. /* try and push anything in the part_buf */
  876. if (unlikely(host->part_buf_count)) {
  877. int len = dw_mci_push_part_bytes(host, buf, cnt);
  878. buf += len;
  879. cnt -= len;
  880. if (!sg_next(host->sg) || host->part_buf_count == 2) {
  881. mci_writew(host, DATA(host->data_offset),
  882. host->part_buf16);
  883. host->part_buf_count = 0;
  884. }
  885. }
  886. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  887. if (unlikely((unsigned long)buf & 0x1)) {
  888. while (cnt >= 2) {
  889. u16 aligned_buf[64];
  890. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  891. int items = len >> 1;
  892. int i;
  893. /* memcpy from input buffer into aligned buffer */
  894. memcpy(aligned_buf, buf, len);
  895. buf += len;
  896. cnt -= len;
  897. /* push data from aligned buffer into fifo */
  898. for (i = 0; i < items; ++i)
  899. mci_writew(host, DATA(host->data_offset),
  900. aligned_buf[i]);
  901. }
  902. } else
  903. #endif
  904. {
  905. u16 *pdata = buf;
  906. for (; cnt >= 2; cnt -= 2)
  907. mci_writew(host, DATA(host->data_offset), *pdata++);
  908. buf = pdata;
  909. }
  910. /* put anything remaining in the part_buf */
  911. if (cnt) {
  912. dw_mci_set_part_bytes(host, buf, cnt);
  913. if (!sg_next(host->sg))
  914. mci_writew(host, DATA(host->data_offset),
  915. host->part_buf16);
  916. }
  917. }
  918. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  919. {
  920. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  921. if (unlikely((unsigned long)buf & 0x1)) {
  922. while (cnt >= 2) {
  923. /* pull data from fifo into aligned buffer */
  924. u16 aligned_buf[64];
  925. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  926. int items = len >> 1;
  927. int i;
  928. for (i = 0; i < items; ++i)
  929. aligned_buf[i] = mci_readw(host,
  930. DATA(host->data_offset));
  931. /* memcpy from aligned buffer into output buffer */
  932. memcpy(buf, aligned_buf, len);
  933. buf += len;
  934. cnt -= len;
  935. }
  936. } else
  937. #endif
  938. {
  939. u16 *pdata = buf;
  940. for (; cnt >= 2; cnt -= 2)
  941. *pdata++ = mci_readw(host, DATA(host->data_offset));
  942. buf = pdata;
  943. }
  944. if (cnt) {
  945. host->part_buf16 = mci_readw(host, DATA(host->data_offset));
  946. dw_mci_pull_final_bytes(host, buf, cnt);
  947. }
  948. }
  949. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  950. {
  951. /* try and push anything in the part_buf */
  952. if (unlikely(host->part_buf_count)) {
  953. int len = dw_mci_push_part_bytes(host, buf, cnt);
  954. buf += len;
  955. cnt -= len;
  956. if (!sg_next(host->sg) || host->part_buf_count == 4) {
  957. mci_writel(host, DATA(host->data_offset),
  958. host->part_buf32);
  959. host->part_buf_count = 0;
  960. }
  961. }
  962. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  963. if (unlikely((unsigned long)buf & 0x3)) {
  964. while (cnt >= 4) {
  965. u32 aligned_buf[32];
  966. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  967. int items = len >> 2;
  968. int i;
  969. /* memcpy from input buffer into aligned buffer */
  970. memcpy(aligned_buf, buf, len);
  971. buf += len;
  972. cnt -= len;
  973. /* push data from aligned buffer into fifo */
  974. for (i = 0; i < items; ++i)
  975. mci_writel(host, DATA(host->data_offset),
  976. aligned_buf[i]);
  977. }
  978. } else
  979. #endif
  980. {
  981. u32 *pdata = buf;
  982. for (; cnt >= 4; cnt -= 4)
  983. mci_writel(host, DATA(host->data_offset), *pdata++);
  984. buf = pdata;
  985. }
  986. /* put anything remaining in the part_buf */
  987. if (cnt) {
  988. dw_mci_set_part_bytes(host, buf, cnt);
  989. if (!sg_next(host->sg))
  990. mci_writel(host, DATA(host->data_offset),
  991. host->part_buf32);
  992. }
  993. }
  994. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  995. {
  996. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  997. if (unlikely((unsigned long)buf & 0x3)) {
  998. while (cnt >= 4) {
  999. /* pull data from fifo into aligned buffer */
  1000. u32 aligned_buf[32];
  1001. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1002. int items = len >> 2;
  1003. int i;
  1004. for (i = 0; i < items; ++i)
  1005. aligned_buf[i] = mci_readl(host,
  1006. DATA(host->data_offset));
  1007. /* memcpy from aligned buffer into output buffer */
  1008. memcpy(buf, aligned_buf, len);
  1009. buf += len;
  1010. cnt -= len;
  1011. }
  1012. } else
  1013. #endif
  1014. {
  1015. u32 *pdata = buf;
  1016. for (; cnt >= 4; cnt -= 4)
  1017. *pdata++ = mci_readl(host, DATA(host->data_offset));
  1018. buf = pdata;
  1019. }
  1020. if (cnt) {
  1021. host->part_buf32 = mci_readl(host, DATA(host->data_offset));
  1022. dw_mci_pull_final_bytes(host, buf, cnt);
  1023. }
  1024. }
  1025. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1026. {
  1027. /* try and push anything in the part_buf */
  1028. if (unlikely(host->part_buf_count)) {
  1029. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1030. buf += len;
  1031. cnt -= len;
  1032. if (!sg_next(host->sg) || host->part_buf_count == 8) {
  1033. mci_writew(host, DATA(host->data_offset),
  1034. host->part_buf);
  1035. host->part_buf_count = 0;
  1036. }
  1037. }
  1038. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1039. if (unlikely((unsigned long)buf & 0x7)) {
  1040. while (cnt >= 8) {
  1041. u64 aligned_buf[16];
  1042. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1043. int items = len >> 3;
  1044. int i;
  1045. /* memcpy from input buffer into aligned buffer */
  1046. memcpy(aligned_buf, buf, len);
  1047. buf += len;
  1048. cnt -= len;
  1049. /* push data from aligned buffer into fifo */
  1050. for (i = 0; i < items; ++i)
  1051. mci_writeq(host, DATA(host->data_offset),
  1052. aligned_buf[i]);
  1053. }
  1054. } else
  1055. #endif
  1056. {
  1057. u64 *pdata = buf;
  1058. for (; cnt >= 8; cnt -= 8)
  1059. mci_writeq(host, DATA(host->data_offset), *pdata++);
  1060. buf = pdata;
  1061. }
  1062. /* put anything remaining in the part_buf */
  1063. if (cnt) {
  1064. dw_mci_set_part_bytes(host, buf, cnt);
  1065. if (!sg_next(host->sg))
  1066. mci_writeq(host, DATA(host->data_offset),
  1067. host->part_buf);
  1068. }
  1069. }
  1070. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1071. {
  1072. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1073. if (unlikely((unsigned long)buf & 0x7)) {
  1074. while (cnt >= 8) {
  1075. /* pull data from fifo into aligned buffer */
  1076. u64 aligned_buf[16];
  1077. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1078. int items = len >> 3;
  1079. int i;
  1080. for (i = 0; i < items; ++i)
  1081. aligned_buf[i] = mci_readq(host,
  1082. DATA(host->data_offset));
  1083. /* memcpy from aligned buffer into output buffer */
  1084. memcpy(buf, aligned_buf, len);
  1085. buf += len;
  1086. cnt -= len;
  1087. }
  1088. } else
  1089. #endif
  1090. {
  1091. u64 *pdata = buf;
  1092. for (; cnt >= 8; cnt -= 8)
  1093. *pdata++ = mci_readq(host, DATA(host->data_offset));
  1094. buf = pdata;
  1095. }
  1096. if (cnt) {
  1097. host->part_buf = mci_readq(host, DATA(host->data_offset));
  1098. dw_mci_pull_final_bytes(host, buf, cnt);
  1099. }
  1100. }
  1101. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  1102. {
  1103. int len;
  1104. /* get remaining partial bytes */
  1105. len = dw_mci_pull_part_bytes(host, buf, cnt);
  1106. if (unlikely(len == cnt))
  1107. return;
  1108. buf += len;
  1109. cnt -= len;
  1110. /* get the rest of the data */
  1111. host->pull_data(host, buf, cnt);
  1112. }
  1113. static void dw_mci_read_data_pio(struct dw_mci *host)
  1114. {
  1115. struct scatterlist *sg = host->sg;
  1116. void *buf = sg_virt(sg);
  1117. unsigned int offset = host->pio_offset;
  1118. struct mmc_data *data = host->data;
  1119. int shift = host->data_shift;
  1120. u32 status;
  1121. unsigned int nbytes = 0, len;
  1122. do {
  1123. len = host->part_buf_count +
  1124. (SDMMC_GET_FCNT(mci_readl(host, STATUS)) << shift);
  1125. if (offset + len <= sg->length) {
  1126. dw_mci_pull_data(host, (void *)(buf + offset), len);
  1127. offset += len;
  1128. nbytes += len;
  1129. if (offset == sg->length) {
  1130. flush_dcache_page(sg_page(sg));
  1131. host->sg = sg = sg_next(sg);
  1132. if (!sg)
  1133. goto done;
  1134. offset = 0;
  1135. buf = sg_virt(sg);
  1136. }
  1137. } else {
  1138. unsigned int remaining = sg->length - offset;
  1139. dw_mci_pull_data(host, (void *)(buf + offset),
  1140. remaining);
  1141. nbytes += remaining;
  1142. flush_dcache_page(sg_page(sg));
  1143. host->sg = sg = sg_next(sg);
  1144. if (!sg)
  1145. goto done;
  1146. offset = len - remaining;
  1147. buf = sg_virt(sg);
  1148. dw_mci_pull_data(host, buf, offset);
  1149. nbytes += offset;
  1150. }
  1151. status = mci_readl(host, MINTSTS);
  1152. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1153. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1154. host->data_status = status;
  1155. data->bytes_xfered += nbytes;
  1156. smp_wmb();
  1157. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1158. tasklet_schedule(&host->tasklet);
  1159. return;
  1160. }
  1161. } while (status & SDMMC_INT_RXDR); /*if the RXDR is ready read again*/
  1162. host->pio_offset = offset;
  1163. data->bytes_xfered += nbytes;
  1164. return;
  1165. done:
  1166. data->bytes_xfered += nbytes;
  1167. smp_wmb();
  1168. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1169. }
  1170. static void dw_mci_write_data_pio(struct dw_mci *host)
  1171. {
  1172. struct scatterlist *sg = host->sg;
  1173. void *buf = sg_virt(sg);
  1174. unsigned int offset = host->pio_offset;
  1175. struct mmc_data *data = host->data;
  1176. int shift = host->data_shift;
  1177. u32 status;
  1178. unsigned int nbytes = 0, len;
  1179. do {
  1180. len = ((host->fifo_depth -
  1181. SDMMC_GET_FCNT(mci_readl(host, STATUS))) << shift)
  1182. - host->part_buf_count;
  1183. if (offset + len <= sg->length) {
  1184. host->push_data(host, (void *)(buf + offset), len);
  1185. offset += len;
  1186. nbytes += len;
  1187. if (offset == sg->length) {
  1188. host->sg = sg = sg_next(sg);
  1189. if (!sg)
  1190. goto done;
  1191. offset = 0;
  1192. buf = sg_virt(sg);
  1193. }
  1194. } else {
  1195. unsigned int remaining = sg->length - offset;
  1196. host->push_data(host, (void *)(buf + offset),
  1197. remaining);
  1198. nbytes += remaining;
  1199. host->sg = sg = sg_next(sg);
  1200. if (!sg)
  1201. goto done;
  1202. offset = len - remaining;
  1203. buf = sg_virt(sg);
  1204. host->push_data(host, (void *)buf, offset);
  1205. nbytes += offset;
  1206. }
  1207. status = mci_readl(host, MINTSTS);
  1208. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1209. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1210. host->data_status = status;
  1211. data->bytes_xfered += nbytes;
  1212. smp_wmb();
  1213. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1214. tasklet_schedule(&host->tasklet);
  1215. return;
  1216. }
  1217. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  1218. host->pio_offset = offset;
  1219. data->bytes_xfered += nbytes;
  1220. return;
  1221. done:
  1222. data->bytes_xfered += nbytes;
  1223. smp_wmb();
  1224. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1225. }
  1226. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  1227. {
  1228. if (!host->cmd_status)
  1229. host->cmd_status = status;
  1230. smp_wmb();
  1231. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1232. tasklet_schedule(&host->tasklet);
  1233. }
  1234. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  1235. {
  1236. struct dw_mci *host = dev_id;
  1237. u32 status, pending;
  1238. unsigned int pass_count = 0;
  1239. int i;
  1240. do {
  1241. status = mci_readl(host, RINTSTS);
  1242. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  1243. /*
  1244. * DTO fix - version 2.10a and below, and only if internal DMA
  1245. * is configured.
  1246. */
  1247. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  1248. if (!pending &&
  1249. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  1250. pending |= SDMMC_INT_DATA_OVER;
  1251. }
  1252. if (!pending)
  1253. break;
  1254. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  1255. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  1256. host->cmd_status = status;
  1257. smp_wmb();
  1258. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1259. }
  1260. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  1261. /* if there is an error report DATA_ERROR */
  1262. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  1263. host->data_status = status;
  1264. smp_wmb();
  1265. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1266. if (!(pending & (SDMMC_INT_DTO | SDMMC_INT_DCRC |
  1267. SDMMC_INT_SBE | SDMMC_INT_EBE)))
  1268. tasklet_schedule(&host->tasklet);
  1269. }
  1270. if (pending & SDMMC_INT_DATA_OVER) {
  1271. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  1272. if (!host->data_status)
  1273. host->data_status = status;
  1274. smp_wmb();
  1275. if (host->dir_status == DW_MCI_RECV_STATUS) {
  1276. if (host->sg != NULL)
  1277. dw_mci_read_data_pio(host);
  1278. }
  1279. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1280. tasklet_schedule(&host->tasklet);
  1281. }
  1282. if (pending & SDMMC_INT_RXDR) {
  1283. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1284. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  1285. dw_mci_read_data_pio(host);
  1286. }
  1287. if (pending & SDMMC_INT_TXDR) {
  1288. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1289. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  1290. dw_mci_write_data_pio(host);
  1291. }
  1292. if (pending & SDMMC_INT_CMD_DONE) {
  1293. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1294. dw_mci_cmd_interrupt(host, status);
  1295. }
  1296. if (pending & SDMMC_INT_CD) {
  1297. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1298. queue_work(dw_mci_card_workqueue, &host->card_work);
  1299. }
  1300. /* Handle SDIO Interrupts */
  1301. for (i = 0; i < host->num_slots; i++) {
  1302. struct dw_mci_slot *slot = host->slot[i];
  1303. if (pending & SDMMC_INT_SDIO(i)) {
  1304. mci_writel(host, RINTSTS, SDMMC_INT_SDIO(i));
  1305. mmc_signal_sdio_irq(slot->mmc);
  1306. }
  1307. }
  1308. } while (pass_count++ < 5);
  1309. #ifdef CONFIG_MMC_DW_IDMAC
  1310. /* Handle DMA interrupts */
  1311. pending = mci_readl(host, IDSTS);
  1312. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1313. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI);
  1314. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1315. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1316. host->dma_ops->complete(host);
  1317. }
  1318. #endif
  1319. return IRQ_HANDLED;
  1320. }
  1321. static void dw_mci_work_routine_card(struct work_struct *work)
  1322. {
  1323. struct dw_mci *host = container_of(work, struct dw_mci, card_work);
  1324. int i;
  1325. for (i = 0; i < host->num_slots; i++) {
  1326. struct dw_mci_slot *slot = host->slot[i];
  1327. struct mmc_host *mmc = slot->mmc;
  1328. struct mmc_request *mrq;
  1329. int present;
  1330. u32 ctrl;
  1331. present = dw_mci_get_cd(mmc);
  1332. while (present != slot->last_detect_state) {
  1333. dev_dbg(&slot->mmc->class_dev, "card %s\n",
  1334. present ? "inserted" : "removed");
  1335. /* Power up slot (before spin_lock, may sleep) */
  1336. if (present != 0 && host->pdata->setpower)
  1337. host->pdata->setpower(slot->id, mmc->ocr_avail);
  1338. spin_lock_bh(&host->lock);
  1339. /* Card change detected */
  1340. slot->last_detect_state = present;
  1341. /* Mark card as present if applicable */
  1342. if (present != 0)
  1343. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1344. /* Clean up queue if present */
  1345. mrq = slot->mrq;
  1346. if (mrq) {
  1347. if (mrq == host->mrq) {
  1348. host->data = NULL;
  1349. host->cmd = NULL;
  1350. switch (host->state) {
  1351. case STATE_IDLE:
  1352. break;
  1353. case STATE_SENDING_CMD:
  1354. mrq->cmd->error = -ENOMEDIUM;
  1355. if (!mrq->data)
  1356. break;
  1357. /* fall through */
  1358. case STATE_SENDING_DATA:
  1359. mrq->data->error = -ENOMEDIUM;
  1360. dw_mci_stop_dma(host);
  1361. break;
  1362. case STATE_DATA_BUSY:
  1363. case STATE_DATA_ERROR:
  1364. if (mrq->data->error == -EINPROGRESS)
  1365. mrq->data->error = -ENOMEDIUM;
  1366. if (!mrq->stop)
  1367. break;
  1368. /* fall through */
  1369. case STATE_SENDING_STOP:
  1370. mrq->stop->error = -ENOMEDIUM;
  1371. break;
  1372. }
  1373. dw_mci_request_end(host, mrq);
  1374. } else {
  1375. list_del(&slot->queue_node);
  1376. mrq->cmd->error = -ENOMEDIUM;
  1377. if (mrq->data)
  1378. mrq->data->error = -ENOMEDIUM;
  1379. if (mrq->stop)
  1380. mrq->stop->error = -ENOMEDIUM;
  1381. spin_unlock(&host->lock);
  1382. mmc_request_done(slot->mmc, mrq);
  1383. spin_lock(&host->lock);
  1384. }
  1385. }
  1386. /* Power down slot */
  1387. if (present == 0) {
  1388. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1389. /*
  1390. * Clear down the FIFO - doing so generates a
  1391. * block interrupt, hence setting the
  1392. * scatter-gather pointer to NULL.
  1393. */
  1394. host->sg = NULL;
  1395. ctrl = mci_readl(host, CTRL);
  1396. ctrl |= SDMMC_CTRL_FIFO_RESET;
  1397. mci_writel(host, CTRL, ctrl);
  1398. #ifdef CONFIG_MMC_DW_IDMAC
  1399. ctrl = mci_readl(host, BMOD);
  1400. ctrl |= 0x01; /* Software reset of DMA */
  1401. mci_writel(host, BMOD, ctrl);
  1402. #endif
  1403. }
  1404. spin_unlock_bh(&host->lock);
  1405. /* Power down slot (after spin_unlock, may sleep) */
  1406. if (present == 0 && host->pdata->setpower)
  1407. host->pdata->setpower(slot->id, 0);
  1408. present = dw_mci_get_cd(mmc);
  1409. }
  1410. mmc_detect_change(slot->mmc,
  1411. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1412. }
  1413. }
  1414. static int __init dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  1415. {
  1416. struct mmc_host *mmc;
  1417. struct dw_mci_slot *slot;
  1418. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), &host->pdev->dev);
  1419. if (!mmc)
  1420. return -ENOMEM;
  1421. slot = mmc_priv(mmc);
  1422. slot->id = id;
  1423. slot->mmc = mmc;
  1424. slot->host = host;
  1425. mmc->ops = &dw_mci_ops;
  1426. mmc->f_min = DIV_ROUND_UP(host->bus_hz, 510);
  1427. mmc->f_max = host->bus_hz;
  1428. if (host->pdata->get_ocr)
  1429. mmc->ocr_avail = host->pdata->get_ocr(id);
  1430. else
  1431. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  1432. /*
  1433. * Start with slot power disabled, it will be enabled when a card
  1434. * is detected.
  1435. */
  1436. if (host->pdata->setpower)
  1437. host->pdata->setpower(id, 0);
  1438. if (host->pdata->caps)
  1439. mmc->caps = host->pdata->caps;
  1440. if (host->pdata->caps2)
  1441. mmc->caps2 = host->pdata->caps2;
  1442. if (host->pdata->get_bus_wd)
  1443. if (host->pdata->get_bus_wd(slot->id) >= 4)
  1444. mmc->caps |= MMC_CAP_4_BIT_DATA;
  1445. if (host->pdata->quirks & DW_MCI_QUIRK_HIGHSPEED)
  1446. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  1447. #ifdef CONFIG_MMC_DW_IDMAC
  1448. mmc->max_segs = host->ring_size;
  1449. mmc->max_blk_size = 65536;
  1450. mmc->max_blk_count = host->ring_size;
  1451. mmc->max_seg_size = 0x1000;
  1452. mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
  1453. #else
  1454. if (host->pdata->blk_settings) {
  1455. mmc->max_segs = host->pdata->blk_settings->max_segs;
  1456. mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
  1457. mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
  1458. mmc->max_req_size = host->pdata->blk_settings->max_req_size;
  1459. mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
  1460. } else {
  1461. /* Useful defaults if platform data is unset. */
  1462. mmc->max_segs = 64;
  1463. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  1464. mmc->max_blk_count = 512;
  1465. mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
  1466. mmc->max_seg_size = mmc->max_req_size;
  1467. }
  1468. #endif /* CONFIG_MMC_DW_IDMAC */
  1469. host->vmmc = regulator_get(mmc_dev(mmc), "vmmc");
  1470. if (IS_ERR(host->vmmc)) {
  1471. pr_info("%s: no vmmc regulator found\n", mmc_hostname(mmc));
  1472. host->vmmc = NULL;
  1473. } else
  1474. regulator_enable(host->vmmc);
  1475. if (dw_mci_get_cd(mmc))
  1476. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1477. else
  1478. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1479. host->slot[id] = slot;
  1480. mmc_add_host(mmc);
  1481. #if defined(CONFIG_DEBUG_FS)
  1482. dw_mci_init_debugfs(slot);
  1483. #endif
  1484. /* Card initially undetected */
  1485. slot->last_detect_state = 0;
  1486. /*
  1487. * Card may have been plugged in prior to boot so we
  1488. * need to run the detect tasklet
  1489. */
  1490. queue_work(dw_mci_card_workqueue, &host->card_work);
  1491. return 0;
  1492. }
  1493. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  1494. {
  1495. /* Shutdown detect IRQ */
  1496. if (slot->host->pdata->exit)
  1497. slot->host->pdata->exit(id);
  1498. /* Debugfs stuff is cleaned up by mmc core */
  1499. mmc_remove_host(slot->mmc);
  1500. slot->host->slot[id] = NULL;
  1501. mmc_free_host(slot->mmc);
  1502. }
  1503. static void dw_mci_init_dma(struct dw_mci *host)
  1504. {
  1505. /* Alloc memory for sg translation */
  1506. host->sg_cpu = dma_alloc_coherent(&host->pdev->dev, PAGE_SIZE,
  1507. &host->sg_dma, GFP_KERNEL);
  1508. if (!host->sg_cpu) {
  1509. dev_err(&host->pdev->dev, "%s: could not alloc DMA memory\n",
  1510. __func__);
  1511. goto no_dma;
  1512. }
  1513. /* Determine which DMA interface to use */
  1514. #ifdef CONFIG_MMC_DW_IDMAC
  1515. host->dma_ops = &dw_mci_idmac_ops;
  1516. dev_info(&host->pdev->dev, "Using internal DMA controller.\n");
  1517. #endif
  1518. if (!host->dma_ops)
  1519. goto no_dma;
  1520. if (host->dma_ops->init) {
  1521. if (host->dma_ops->init(host)) {
  1522. dev_err(&host->pdev->dev, "%s: Unable to initialize "
  1523. "DMA Controller.\n", __func__);
  1524. goto no_dma;
  1525. }
  1526. } else {
  1527. dev_err(&host->pdev->dev, "DMA initialization not found.\n");
  1528. goto no_dma;
  1529. }
  1530. host->use_dma = 1;
  1531. return;
  1532. no_dma:
  1533. dev_info(&host->pdev->dev, "Using PIO mode.\n");
  1534. host->use_dma = 0;
  1535. return;
  1536. }
  1537. static bool mci_wait_reset(struct device *dev, struct dw_mci *host)
  1538. {
  1539. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  1540. unsigned int ctrl;
  1541. mci_writel(host, CTRL, (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1542. SDMMC_CTRL_DMA_RESET));
  1543. /* wait till resets clear */
  1544. do {
  1545. ctrl = mci_readl(host, CTRL);
  1546. if (!(ctrl & (SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET |
  1547. SDMMC_CTRL_DMA_RESET)))
  1548. return true;
  1549. } while (time_before(jiffies, timeout));
  1550. dev_err(dev, "Timeout resetting block (ctrl %#x)\n", ctrl);
  1551. return false;
  1552. }
  1553. static int dw_mci_probe(struct platform_device *pdev)
  1554. {
  1555. struct dw_mci *host;
  1556. struct resource *regs;
  1557. struct dw_mci_board *pdata;
  1558. int irq, ret, i, width;
  1559. u32 fifo_size;
  1560. regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1561. if (!regs)
  1562. return -ENXIO;
  1563. irq = platform_get_irq(pdev, 0);
  1564. if (irq < 0)
  1565. return irq;
  1566. host = kzalloc(sizeof(struct dw_mci), GFP_KERNEL);
  1567. if (!host)
  1568. return -ENOMEM;
  1569. host->pdev = pdev;
  1570. host->pdata = pdata = pdev->dev.platform_data;
  1571. if (!pdata || !pdata->init) {
  1572. dev_err(&pdev->dev,
  1573. "Platform data must supply init function\n");
  1574. ret = -ENODEV;
  1575. goto err_freehost;
  1576. }
  1577. if (!pdata->select_slot && pdata->num_slots > 1) {
  1578. dev_err(&pdev->dev,
  1579. "Platform data must supply select_slot function\n");
  1580. ret = -ENODEV;
  1581. goto err_freehost;
  1582. }
  1583. if (!pdata->bus_hz) {
  1584. dev_err(&pdev->dev,
  1585. "Platform data must supply bus speed\n");
  1586. ret = -ENODEV;
  1587. goto err_freehost;
  1588. }
  1589. host->bus_hz = pdata->bus_hz;
  1590. host->quirks = pdata->quirks;
  1591. spin_lock_init(&host->lock);
  1592. INIT_LIST_HEAD(&host->queue);
  1593. ret = -ENOMEM;
  1594. host->regs = ioremap(regs->start, resource_size(regs));
  1595. if (!host->regs)
  1596. goto err_freehost;
  1597. host->dma_ops = pdata->dma_ops;
  1598. dw_mci_init_dma(host);
  1599. /*
  1600. * Get the host data width - this assumes that HCON has been set with
  1601. * the correct values.
  1602. */
  1603. i = (mci_readl(host, HCON) >> 7) & 0x7;
  1604. if (!i) {
  1605. host->push_data = dw_mci_push_data16;
  1606. host->pull_data = dw_mci_pull_data16;
  1607. width = 16;
  1608. host->data_shift = 1;
  1609. } else if (i == 2) {
  1610. host->push_data = dw_mci_push_data64;
  1611. host->pull_data = dw_mci_pull_data64;
  1612. width = 64;
  1613. host->data_shift = 3;
  1614. } else {
  1615. /* Check for a reserved value, and warn if it is */
  1616. WARN((i != 1),
  1617. "HCON reports a reserved host data width!\n"
  1618. "Defaulting to 32-bit access.\n");
  1619. host->push_data = dw_mci_push_data32;
  1620. host->pull_data = dw_mci_pull_data32;
  1621. width = 32;
  1622. host->data_shift = 2;
  1623. }
  1624. /* Reset all blocks */
  1625. if (!mci_wait_reset(&pdev->dev, host)) {
  1626. ret = -ENODEV;
  1627. goto err_dmaunmap;
  1628. }
  1629. /* Clear the interrupts for the host controller */
  1630. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1631. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1632. /* Put in max timeout */
  1633. mci_writel(host, TMOUT, 0xFFFFFFFF);
  1634. /*
  1635. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  1636. * Tx Mark = fifo_size / 2 DMA Size = 8
  1637. */
  1638. if (!host->pdata->fifo_depth) {
  1639. /*
  1640. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  1641. * have been overwritten by the bootloader, just like we're
  1642. * about to do, so if you know the value for your hardware, you
  1643. * should put it in the platform data.
  1644. */
  1645. fifo_size = mci_readl(host, FIFOTH);
  1646. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  1647. } else {
  1648. fifo_size = host->pdata->fifo_depth;
  1649. }
  1650. host->fifo_depth = fifo_size;
  1651. host->fifoth_val = ((0x2 << 28) | ((fifo_size/2 - 1) << 16) |
  1652. ((fifo_size/2) << 0));
  1653. mci_writel(host, FIFOTH, host->fifoth_val);
  1654. /* disable clock to CIU */
  1655. mci_writel(host, CLKENA, 0);
  1656. mci_writel(host, CLKSRC, 0);
  1657. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  1658. dw_mci_card_workqueue = alloc_workqueue("dw-mci-card",
  1659. WQ_MEM_RECLAIM | WQ_NON_REENTRANT, 1);
  1660. if (!dw_mci_card_workqueue)
  1661. goto err_dmaunmap;
  1662. INIT_WORK(&host->card_work, dw_mci_work_routine_card);
  1663. ret = request_irq(irq, dw_mci_interrupt, 0, "dw-mci", host);
  1664. if (ret)
  1665. goto err_workqueue;
  1666. platform_set_drvdata(pdev, host);
  1667. if (host->pdata->num_slots)
  1668. host->num_slots = host->pdata->num_slots;
  1669. else
  1670. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  1671. /* We need at least one slot to succeed */
  1672. for (i = 0; i < host->num_slots; i++) {
  1673. ret = dw_mci_init_slot(host, i);
  1674. if (ret) {
  1675. ret = -ENODEV;
  1676. goto err_init_slot;
  1677. }
  1678. }
  1679. /*
  1680. * In 2.40a spec, Data offset is changed.
  1681. * Need to check the version-id and set data-offset for DATA register.
  1682. */
  1683. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  1684. dev_info(&pdev->dev, "Version ID is %04x\n", host->verid);
  1685. if (host->verid < DW_MMC_240A)
  1686. host->data_offset = DATA_OFFSET;
  1687. else
  1688. host->data_offset = DATA_240A_OFFSET;
  1689. /*
  1690. * Enable interrupts for command done, data over, data empty, card det,
  1691. * receive ready and error such as transmit, receive timeout, crc error
  1692. */
  1693. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1694. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1695. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1696. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1697. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
  1698. dev_info(&pdev->dev, "DW MMC controller at irq %d, "
  1699. "%d bit host data width, "
  1700. "%u deep fifo\n",
  1701. irq, width, fifo_size);
  1702. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  1703. dev_info(&pdev->dev, "Internal DMAC interrupt fix enabled.\n");
  1704. return 0;
  1705. err_init_slot:
  1706. /* De-init any initialized slots */
  1707. while (i > 0) {
  1708. if (host->slot[i])
  1709. dw_mci_cleanup_slot(host->slot[i], i);
  1710. i--;
  1711. }
  1712. free_irq(irq, host);
  1713. err_workqueue:
  1714. destroy_workqueue(dw_mci_card_workqueue);
  1715. err_dmaunmap:
  1716. if (host->use_dma && host->dma_ops->exit)
  1717. host->dma_ops->exit(host);
  1718. dma_free_coherent(&host->pdev->dev, PAGE_SIZE,
  1719. host->sg_cpu, host->sg_dma);
  1720. iounmap(host->regs);
  1721. if (host->vmmc) {
  1722. regulator_disable(host->vmmc);
  1723. regulator_put(host->vmmc);
  1724. }
  1725. err_freehost:
  1726. kfree(host);
  1727. return ret;
  1728. }
  1729. static int __exit dw_mci_remove(struct platform_device *pdev)
  1730. {
  1731. struct dw_mci *host = platform_get_drvdata(pdev);
  1732. int i;
  1733. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1734. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  1735. platform_set_drvdata(pdev, NULL);
  1736. for (i = 0; i < host->num_slots; i++) {
  1737. dev_dbg(&pdev->dev, "remove slot %d\n", i);
  1738. if (host->slot[i])
  1739. dw_mci_cleanup_slot(host->slot[i], i);
  1740. }
  1741. /* disable clock to CIU */
  1742. mci_writel(host, CLKENA, 0);
  1743. mci_writel(host, CLKSRC, 0);
  1744. free_irq(platform_get_irq(pdev, 0), host);
  1745. destroy_workqueue(dw_mci_card_workqueue);
  1746. dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
  1747. if (host->use_dma && host->dma_ops->exit)
  1748. host->dma_ops->exit(host);
  1749. if (host->vmmc) {
  1750. regulator_disable(host->vmmc);
  1751. regulator_put(host->vmmc);
  1752. }
  1753. iounmap(host->regs);
  1754. kfree(host);
  1755. return 0;
  1756. }
  1757. #ifdef CONFIG_PM_SLEEP
  1758. /*
  1759. * TODO: we should probably disable the clock to the card in the suspend path.
  1760. */
  1761. static int dw_mci_suspend(struct device *dev)
  1762. {
  1763. int i, ret;
  1764. struct dw_mci *host = dev_get_drvdata(dev);
  1765. for (i = 0; i < host->num_slots; i++) {
  1766. struct dw_mci_slot *slot = host->slot[i];
  1767. if (!slot)
  1768. continue;
  1769. ret = mmc_suspend_host(slot->mmc);
  1770. if (ret < 0) {
  1771. while (--i >= 0) {
  1772. slot = host->slot[i];
  1773. if (slot)
  1774. mmc_resume_host(host->slot[i]->mmc);
  1775. }
  1776. return ret;
  1777. }
  1778. }
  1779. if (host->vmmc)
  1780. regulator_disable(host->vmmc);
  1781. return 0;
  1782. }
  1783. static int dw_mci_resume(struct device *dev)
  1784. {
  1785. int i, ret;
  1786. struct dw_mci *host = dev_get_drvdata(dev);
  1787. if (host->vmmc)
  1788. regulator_enable(host->vmmc);
  1789. if (host->dma_ops->init)
  1790. host->dma_ops->init(host);
  1791. if (!mci_wait_reset(dev, host)) {
  1792. ret = -ENODEV;
  1793. return ret;
  1794. }
  1795. /* Restore the old value at FIFOTH register */
  1796. mci_writel(host, FIFOTH, host->fifoth_val);
  1797. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1798. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  1799. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  1800. DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
  1801. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  1802. for (i = 0; i < host->num_slots; i++) {
  1803. struct dw_mci_slot *slot = host->slot[i];
  1804. if (!slot)
  1805. continue;
  1806. ret = mmc_resume_host(host->slot[i]->mmc);
  1807. if (ret < 0)
  1808. return ret;
  1809. }
  1810. return 0;
  1811. }
  1812. #else
  1813. #define dw_mci_suspend NULL
  1814. #define dw_mci_resume NULL
  1815. #endif /* CONFIG_PM_SLEEP */
  1816. static SIMPLE_DEV_PM_OPS(dw_mci_pmops, dw_mci_suspend, dw_mci_resume);
  1817. static struct platform_driver dw_mci_driver = {
  1818. .remove = __exit_p(dw_mci_remove),
  1819. .driver = {
  1820. .name = "dw_mmc",
  1821. .pm = &dw_mci_pmops,
  1822. },
  1823. };
  1824. static int __init dw_mci_init(void)
  1825. {
  1826. return platform_driver_probe(&dw_mci_driver, dw_mci_probe);
  1827. }
  1828. static void __exit dw_mci_exit(void)
  1829. {
  1830. platform_driver_unregister(&dw_mci_driver);
  1831. }
  1832. module_init(dw_mci_init);
  1833. module_exit(dw_mci_exit);
  1834. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  1835. MODULE_AUTHOR("NXP Semiconductor VietNam");
  1836. MODULE_AUTHOR("Imagination Technologies Ltd");
  1837. MODULE_LICENSE("GPL v2");