dw_mmc.c 60 KB

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