dw_mmc.c 61 KB

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