dw_mmc.c 54 KB

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