dw_mmc.c 53 KB

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