dw_mmc.c 54 KB

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