dw_mmc.c 66 KB

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