dw_mmc.c 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137
  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/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/sd.h>
  33. #include <linux/mmc/sdio.h>
  34. #include <linux/mmc/dw_mmc.h>
  35. #include <linux/bitops.h>
  36. #include <linux/regulator/consumer.h>
  37. #include <linux/of.h>
  38. #include <linux/of_gpio.h>
  39. #include <linux/mmc/slot-gpio.h>
  40. #include "dw_mmc.h"
  41. /* Common flag combinations */
  42. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
  43. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  44. SDMMC_INT_EBE)
  45. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  46. SDMMC_INT_RESP_ERR)
  47. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  48. DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
  49. #define DW_MCI_SEND_STATUS 1
  50. #define DW_MCI_RECV_STATUS 2
  51. #define DW_MCI_DMA_THRESHOLD 16
  52. #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
  53. #define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
  54. #ifdef CONFIG_MMC_DW_IDMAC
  55. #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
  56. SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
  57. SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
  58. SDMMC_IDMAC_INT_TI)
  59. struct idmac_desc_64addr {
  60. u32 des0; /* Control Descriptor */
  61. u32 des1; /* Reserved */
  62. u32 des2; /*Buffer sizes */
  63. #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
  64. ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
  65. ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
  66. u32 des3; /* Reserved */
  67. u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
  68. u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
  69. u32 des6; /* Lower 32-bits of Next Descriptor Address */
  70. u32 des7; /* Upper 32-bits of Next Descriptor Address */
  71. };
  72. struct idmac_desc {
  73. __le32 des0; /* Control Descriptor */
  74. #define IDMAC_DES0_DIC BIT(1)
  75. #define IDMAC_DES0_LD BIT(2)
  76. #define IDMAC_DES0_FD BIT(3)
  77. #define IDMAC_DES0_CH BIT(4)
  78. #define IDMAC_DES0_ER BIT(5)
  79. #define IDMAC_DES0_CES BIT(30)
  80. #define IDMAC_DES0_OWN BIT(31)
  81. __le32 des1; /* Buffer sizes */
  82. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  83. ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
  84. __le32 des2; /* buffer 1 physical address */
  85. __le32 des3; /* buffer 2 physical address */
  86. };
  87. /* Each descriptor can transfer up to 4KB of data in chained mode */
  88. #define DW_MCI_DESC_DATA_LENGTH 0x1000
  89. #endif /* CONFIG_MMC_DW_IDMAC */
  90. static bool dw_mci_reset(struct dw_mci *host);
  91. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
  92. static int dw_mci_card_busy(struct mmc_host *mmc);
  93. #if defined(CONFIG_DEBUG_FS)
  94. static int dw_mci_req_show(struct seq_file *s, void *v)
  95. {
  96. struct dw_mci_slot *slot = s->private;
  97. struct mmc_request *mrq;
  98. struct mmc_command *cmd;
  99. struct mmc_command *stop;
  100. struct mmc_data *data;
  101. /* Make sure we get a consistent snapshot */
  102. spin_lock_bh(&slot->host->lock);
  103. mrq = slot->mrq;
  104. if (mrq) {
  105. cmd = mrq->cmd;
  106. data = mrq->data;
  107. stop = mrq->stop;
  108. if (cmd)
  109. seq_printf(s,
  110. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  111. cmd->opcode, cmd->arg, cmd->flags,
  112. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  113. cmd->resp[2], cmd->error);
  114. if (data)
  115. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  116. data->bytes_xfered, data->blocks,
  117. data->blksz, data->flags, data->error);
  118. if (stop)
  119. seq_printf(s,
  120. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  121. stop->opcode, stop->arg, stop->flags,
  122. stop->resp[0], stop->resp[1], stop->resp[2],
  123. stop->resp[2], stop->error);
  124. }
  125. spin_unlock_bh(&slot->host->lock);
  126. return 0;
  127. }
  128. static int dw_mci_req_open(struct inode *inode, struct file *file)
  129. {
  130. return single_open(file, dw_mci_req_show, inode->i_private);
  131. }
  132. static const struct file_operations dw_mci_req_fops = {
  133. .owner = THIS_MODULE,
  134. .open = dw_mci_req_open,
  135. .read = seq_read,
  136. .llseek = seq_lseek,
  137. .release = single_release,
  138. };
  139. static int dw_mci_regs_show(struct seq_file *s, void *v)
  140. {
  141. seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
  142. seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
  143. seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
  144. seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
  145. seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
  146. seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
  147. return 0;
  148. }
  149. static int dw_mci_regs_open(struct inode *inode, struct file *file)
  150. {
  151. return single_open(file, dw_mci_regs_show, inode->i_private);
  152. }
  153. static const struct file_operations dw_mci_regs_fops = {
  154. .owner = THIS_MODULE,
  155. .open = dw_mci_regs_open,
  156. .read = seq_read,
  157. .llseek = seq_lseek,
  158. .release = single_release,
  159. };
  160. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  161. {
  162. struct mmc_host *mmc = slot->mmc;
  163. struct dw_mci *host = slot->host;
  164. struct dentry *root;
  165. struct dentry *node;
  166. root = mmc->debugfs_root;
  167. if (!root)
  168. return;
  169. node = debugfs_create_file("regs", S_IRUSR, root, host,
  170. &dw_mci_regs_fops);
  171. if (!node)
  172. goto err;
  173. node = debugfs_create_file("req", S_IRUSR, root, slot,
  174. &dw_mci_req_fops);
  175. if (!node)
  176. goto err;
  177. node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
  178. if (!node)
  179. goto err;
  180. node = debugfs_create_x32("pending_events", S_IRUSR, root,
  181. (u32 *)&host->pending_events);
  182. if (!node)
  183. goto err;
  184. node = debugfs_create_x32("completed_events", S_IRUSR, root,
  185. (u32 *)&host->completed_events);
  186. if (!node)
  187. goto err;
  188. return;
  189. err:
  190. dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
  191. }
  192. #endif /* defined(CONFIG_DEBUG_FS) */
  193. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
  194. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  195. {
  196. struct mmc_data *data;
  197. struct dw_mci_slot *slot = mmc_priv(mmc);
  198. struct dw_mci *host = slot->host;
  199. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  200. u32 cmdr;
  201. cmd->error = -EINPROGRESS;
  202. cmdr = cmd->opcode;
  203. if (cmd->opcode == MMC_STOP_TRANSMISSION ||
  204. cmd->opcode == MMC_GO_IDLE_STATE ||
  205. cmd->opcode == MMC_GO_INACTIVE_STATE ||
  206. (cmd->opcode == SD_IO_RW_DIRECT &&
  207. ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
  208. cmdr |= SDMMC_CMD_STOP;
  209. else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
  210. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  211. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  212. u32 clk_en_a;
  213. /* Special bit makes CMD11 not die */
  214. cmdr |= SDMMC_CMD_VOLT_SWITCH;
  215. /* Change state to continue to handle CMD11 weirdness */
  216. WARN_ON(slot->host->state != STATE_SENDING_CMD);
  217. slot->host->state = STATE_SENDING_CMD11;
  218. /*
  219. * We need to disable low power mode (automatic clock stop)
  220. * while doing voltage switch so we don't confuse the card,
  221. * since stopping the clock is a specific part of the UHS
  222. * voltage change dance.
  223. *
  224. * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
  225. * unconditionally turned back on in dw_mci_setup_bus() if it's
  226. * ever called with a non-zero clock. That shouldn't happen
  227. * until the voltage change is all done.
  228. */
  229. clk_en_a = mci_readl(host, CLKENA);
  230. clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
  231. mci_writel(host, CLKENA, clk_en_a);
  232. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  233. SDMMC_CMD_PRV_DAT_WAIT, 0);
  234. }
  235. if (cmd->flags & MMC_RSP_PRESENT) {
  236. /* We expect a response, so set this bit */
  237. cmdr |= SDMMC_CMD_RESP_EXP;
  238. if (cmd->flags & MMC_RSP_136)
  239. cmdr |= SDMMC_CMD_RESP_LONG;
  240. }
  241. if (cmd->flags & MMC_RSP_CRC)
  242. cmdr |= SDMMC_CMD_RESP_CRC;
  243. data = cmd->data;
  244. if (data) {
  245. cmdr |= SDMMC_CMD_DAT_EXP;
  246. if (data->flags & MMC_DATA_STREAM)
  247. cmdr |= SDMMC_CMD_STRM_MODE;
  248. if (data->flags & MMC_DATA_WRITE)
  249. cmdr |= SDMMC_CMD_DAT_WR;
  250. }
  251. if (drv_data && drv_data->prepare_command)
  252. drv_data->prepare_command(slot->host, &cmdr);
  253. return cmdr;
  254. }
  255. static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
  256. {
  257. struct mmc_command *stop;
  258. u32 cmdr;
  259. if (!cmd->data)
  260. return 0;
  261. stop = &host->stop_abort;
  262. cmdr = cmd->opcode;
  263. memset(stop, 0, sizeof(struct mmc_command));
  264. if (cmdr == MMC_READ_SINGLE_BLOCK ||
  265. cmdr == MMC_READ_MULTIPLE_BLOCK ||
  266. cmdr == MMC_WRITE_BLOCK ||
  267. cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
  268. cmdr == MMC_SEND_TUNING_BLOCK ||
  269. cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
  270. stop->opcode = MMC_STOP_TRANSMISSION;
  271. stop->arg = 0;
  272. stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  273. } else if (cmdr == SD_IO_RW_EXTENDED) {
  274. stop->opcode = SD_IO_RW_DIRECT;
  275. stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
  276. ((cmd->arg >> 28) & 0x7);
  277. stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  278. } else {
  279. return 0;
  280. }
  281. cmdr = stop->opcode | SDMMC_CMD_STOP |
  282. SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
  283. return cmdr;
  284. }
  285. static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
  286. {
  287. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  288. /*
  289. * Databook says that before issuing a new data transfer command
  290. * we need to check to see if the card is busy. Data transfer commands
  291. * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
  292. *
  293. * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
  294. * expected.
  295. */
  296. if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
  297. !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
  298. while (mci_readl(host, STATUS) & SDMMC_STATUS_BUSY) {
  299. if (time_after(jiffies, timeout)) {
  300. /* Command will fail; we'll pass error then */
  301. dev_err(host->dev, "Busy; trying anyway\n");
  302. break;
  303. }
  304. udelay(10);
  305. }
  306. }
  307. }
  308. static void dw_mci_start_command(struct dw_mci *host,
  309. struct mmc_command *cmd, u32 cmd_flags)
  310. {
  311. host->cmd = cmd;
  312. dev_vdbg(host->dev,
  313. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  314. cmd->arg, cmd_flags);
  315. mci_writel(host, CMDARG, cmd->arg);
  316. wmb(); /* drain writebuffer */
  317. dw_mci_wait_while_busy(host, cmd_flags);
  318. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  319. }
  320. static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
  321. {
  322. struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
  323. dw_mci_start_command(host, stop, host->stop_cmdr);
  324. }
  325. /* DMA interface functions */
  326. static void dw_mci_stop_dma(struct dw_mci *host)
  327. {
  328. if (host->using_dma) {
  329. host->dma_ops->stop(host);
  330. host->dma_ops->cleanup(host);
  331. }
  332. /* Data transfer was stopped by the interrupt handler */
  333. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  334. }
  335. static int dw_mci_get_dma_dir(struct mmc_data *data)
  336. {
  337. if (data->flags & MMC_DATA_WRITE)
  338. return DMA_TO_DEVICE;
  339. else
  340. return DMA_FROM_DEVICE;
  341. }
  342. #ifdef CONFIG_MMC_DW_IDMAC
  343. static void dw_mci_dma_cleanup(struct dw_mci *host)
  344. {
  345. struct mmc_data *data = host->data;
  346. if (data)
  347. if (!data->host_cookie)
  348. dma_unmap_sg(host->dev,
  349. data->sg,
  350. data->sg_len,
  351. dw_mci_get_dma_dir(data));
  352. }
  353. static void dw_mci_idmac_reset(struct dw_mci *host)
  354. {
  355. u32 bmod = mci_readl(host, BMOD);
  356. /* Software reset of DMA */
  357. bmod |= SDMMC_IDMAC_SWRESET;
  358. mci_writel(host, BMOD, bmod);
  359. }
  360. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  361. {
  362. u32 temp;
  363. /* Disable and reset the IDMAC interface */
  364. temp = mci_readl(host, CTRL);
  365. temp &= ~SDMMC_CTRL_USE_IDMAC;
  366. temp |= SDMMC_CTRL_DMA_RESET;
  367. mci_writel(host, CTRL, temp);
  368. /* Stop the IDMAC running */
  369. temp = mci_readl(host, BMOD);
  370. temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
  371. temp |= SDMMC_IDMAC_SWRESET;
  372. mci_writel(host, BMOD, temp);
  373. }
  374. static void dw_mci_idmac_complete_dma(struct dw_mci *host)
  375. {
  376. struct mmc_data *data = host->data;
  377. dev_vdbg(host->dev, "DMA complete\n");
  378. host->dma_ops->cleanup(host);
  379. /*
  380. * If the card was removed, data will be NULL. No point in trying to
  381. * send the stop command or waiting for NBUSY in this case.
  382. */
  383. if (data) {
  384. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  385. tasklet_schedule(&host->tasklet);
  386. }
  387. }
  388. static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
  389. unsigned int sg_len)
  390. {
  391. unsigned int desc_len;
  392. int i;
  393. if (host->dma_64bit_address == 1) {
  394. struct idmac_desc_64addr *desc_first, *desc_last, *desc;
  395. desc_first = desc_last = desc = host->sg_cpu;
  396. for (i = 0; i < sg_len; i++) {
  397. unsigned int length = sg_dma_len(&data->sg[i]);
  398. u64 mem_addr = sg_dma_address(&data->sg[i]);
  399. for ( ; length ; desc++) {
  400. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  401. length : DW_MCI_DESC_DATA_LENGTH;
  402. length -= desc_len;
  403. /*
  404. * Set the OWN bit and disable interrupts
  405. * for this descriptor
  406. */
  407. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
  408. IDMAC_DES0_CH;
  409. /* Buffer length */
  410. IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
  411. /* Physical address to DMA to/from */
  412. desc->des4 = mem_addr & 0xffffffff;
  413. desc->des5 = mem_addr >> 32;
  414. /* Update physical address for the next desc */
  415. mem_addr += desc_len;
  416. /* Save pointer to the last descriptor */
  417. desc_last = desc;
  418. }
  419. }
  420. /* Set first descriptor */
  421. desc_first->des0 |= IDMAC_DES0_FD;
  422. /* Set last descriptor */
  423. desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  424. desc_last->des0 |= IDMAC_DES0_LD;
  425. } else {
  426. struct idmac_desc *desc_first, *desc_last, *desc;
  427. desc_first = desc_last = desc = host->sg_cpu;
  428. for (i = 0; i < sg_len; i++) {
  429. unsigned int length = sg_dma_len(&data->sg[i]);
  430. u32 mem_addr = sg_dma_address(&data->sg[i]);
  431. for ( ; length ; desc++) {
  432. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  433. length : DW_MCI_DESC_DATA_LENGTH;
  434. length -= desc_len;
  435. /*
  436. * Set the OWN bit and disable interrupts
  437. * for this descriptor
  438. */
  439. desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
  440. IDMAC_DES0_DIC |
  441. IDMAC_DES0_CH);
  442. /* Buffer length */
  443. IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
  444. /* Physical address to DMA to/from */
  445. desc->des2 = cpu_to_le32(mem_addr);
  446. /* Update physical address for the next desc */
  447. mem_addr += desc_len;
  448. /* Save pointer to the last descriptor */
  449. desc_last = desc;
  450. }
  451. }
  452. /* Set first descriptor */
  453. desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
  454. /* Set last descriptor */
  455. desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
  456. IDMAC_DES0_DIC));
  457. desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
  458. }
  459. wmb(); /* drain writebuffer */
  460. }
  461. static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  462. {
  463. u32 temp;
  464. dw_mci_translate_sglist(host, host->data, sg_len);
  465. /* Make sure to reset DMA in case we did PIO before this */
  466. dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
  467. dw_mci_idmac_reset(host);
  468. /* Select IDMAC interface */
  469. temp = mci_readl(host, CTRL);
  470. temp |= SDMMC_CTRL_USE_IDMAC;
  471. mci_writel(host, CTRL, temp);
  472. /* drain writebuffer */
  473. wmb();
  474. /* Enable the IDMAC */
  475. temp = mci_readl(host, BMOD);
  476. temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
  477. mci_writel(host, BMOD, temp);
  478. /* Start it running */
  479. mci_writel(host, PLDMND, 1);
  480. }
  481. static int dw_mci_idmac_init(struct dw_mci *host)
  482. {
  483. int i;
  484. if (host->dma_64bit_address == 1) {
  485. struct idmac_desc_64addr *p;
  486. /* Number of descriptors in the ring buffer */
  487. host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr);
  488. /* Forward link the descriptor list */
  489. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
  490. i++, p++) {
  491. p->des6 = (host->sg_dma +
  492. (sizeof(struct idmac_desc_64addr) *
  493. (i + 1))) & 0xffffffff;
  494. p->des7 = (u64)(host->sg_dma +
  495. (sizeof(struct idmac_desc_64addr) *
  496. (i + 1))) >> 32;
  497. /* Initialize reserved and buffer size fields to "0" */
  498. p->des1 = 0;
  499. p->des2 = 0;
  500. p->des3 = 0;
  501. }
  502. /* Set the last descriptor as the end-of-ring descriptor */
  503. p->des6 = host->sg_dma & 0xffffffff;
  504. p->des7 = (u64)host->sg_dma >> 32;
  505. p->des0 = IDMAC_DES0_ER;
  506. } else {
  507. struct idmac_desc *p;
  508. /* Number of descriptors in the ring buffer */
  509. host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
  510. /* Forward link the descriptor list */
  511. for (i = 0, p = host->sg_cpu;
  512. i < host->ring_size - 1;
  513. i++, p++) {
  514. p->des3 = cpu_to_le32(host->sg_dma +
  515. (sizeof(struct idmac_desc) * (i + 1)));
  516. p->des1 = 0;
  517. }
  518. /* Set the last descriptor as the end-of-ring descriptor */
  519. p->des3 = cpu_to_le32(host->sg_dma);
  520. p->des0 = cpu_to_le32(IDMAC_DES0_ER);
  521. }
  522. dw_mci_idmac_reset(host);
  523. if (host->dma_64bit_address == 1) {
  524. /* Mask out interrupts - get Tx & Rx complete only */
  525. mci_writel(host, IDSTS64, IDMAC_INT_CLR);
  526. mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
  527. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  528. /* Set the descriptor base address */
  529. mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
  530. mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
  531. } else {
  532. /* Mask out interrupts - get Tx & Rx complete only */
  533. mci_writel(host, IDSTS, IDMAC_INT_CLR);
  534. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
  535. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  536. /* Set the descriptor base address */
  537. mci_writel(host, DBADDR, host->sg_dma);
  538. }
  539. return 0;
  540. }
  541. static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
  542. .init = dw_mci_idmac_init,
  543. .start = dw_mci_idmac_start_dma,
  544. .stop = dw_mci_idmac_stop_dma,
  545. .complete = dw_mci_idmac_complete_dma,
  546. .cleanup = dw_mci_dma_cleanup,
  547. };
  548. #endif /* CONFIG_MMC_DW_IDMAC */
  549. static int dw_mci_pre_dma_transfer(struct dw_mci *host,
  550. struct mmc_data *data,
  551. bool next)
  552. {
  553. struct scatterlist *sg;
  554. unsigned int i, sg_len;
  555. if (!next && data->host_cookie)
  556. return data->host_cookie;
  557. /*
  558. * We don't do DMA on "complex" transfers, i.e. with
  559. * non-word-aligned buffers or lengths. Also, we don't bother
  560. * with all the DMA setup overhead for short transfers.
  561. */
  562. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  563. return -EINVAL;
  564. if (data->blksz & 3)
  565. return -EINVAL;
  566. for_each_sg(data->sg, sg, data->sg_len, i) {
  567. if (sg->offset & 3 || sg->length & 3)
  568. return -EINVAL;
  569. }
  570. sg_len = dma_map_sg(host->dev,
  571. data->sg,
  572. data->sg_len,
  573. dw_mci_get_dma_dir(data));
  574. if (sg_len == 0)
  575. return -EINVAL;
  576. if (next)
  577. data->host_cookie = sg_len;
  578. return sg_len;
  579. }
  580. static void dw_mci_pre_req(struct mmc_host *mmc,
  581. struct mmc_request *mrq,
  582. bool is_first_req)
  583. {
  584. struct dw_mci_slot *slot = mmc_priv(mmc);
  585. struct mmc_data *data = mrq->data;
  586. if (!slot->host->use_dma || !data)
  587. return;
  588. if (data->host_cookie) {
  589. data->host_cookie = 0;
  590. return;
  591. }
  592. if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
  593. data->host_cookie = 0;
  594. }
  595. static void dw_mci_post_req(struct mmc_host *mmc,
  596. struct mmc_request *mrq,
  597. int err)
  598. {
  599. struct dw_mci_slot *slot = mmc_priv(mmc);
  600. struct mmc_data *data = mrq->data;
  601. if (!slot->host->use_dma || !data)
  602. return;
  603. if (data->host_cookie)
  604. dma_unmap_sg(slot->host->dev,
  605. data->sg,
  606. data->sg_len,
  607. dw_mci_get_dma_dir(data));
  608. data->host_cookie = 0;
  609. }
  610. static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
  611. {
  612. #ifdef CONFIG_MMC_DW_IDMAC
  613. unsigned int blksz = data->blksz;
  614. const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  615. u32 fifo_width = 1 << host->data_shift;
  616. u32 blksz_depth = blksz / fifo_width, fifoth_val;
  617. u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
  618. int idx = ARRAY_SIZE(mszs) - 1;
  619. tx_wmark = (host->fifo_depth) / 2;
  620. tx_wmark_invers = host->fifo_depth - tx_wmark;
  621. /*
  622. * MSIZE is '1',
  623. * if blksz is not a multiple of the FIFO width
  624. */
  625. if (blksz % fifo_width) {
  626. msize = 0;
  627. rx_wmark = 1;
  628. goto done;
  629. }
  630. do {
  631. if (!((blksz_depth % mszs[idx]) ||
  632. (tx_wmark_invers % mszs[idx]))) {
  633. msize = idx;
  634. rx_wmark = mszs[idx] - 1;
  635. break;
  636. }
  637. } while (--idx > 0);
  638. /*
  639. * If idx is '0', it won't be tried
  640. * Thus, initial values are uesed
  641. */
  642. done:
  643. fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
  644. mci_writel(host, FIFOTH, fifoth_val);
  645. #endif
  646. }
  647. static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
  648. {
  649. unsigned int blksz = data->blksz;
  650. u32 blksz_depth, fifo_depth;
  651. u16 thld_size;
  652. WARN_ON(!(data->flags & MMC_DATA_READ));
  653. /*
  654. * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
  655. * in the FIFO region, so we really shouldn't access it).
  656. */
  657. if (host->verid < DW_MMC_240A)
  658. return;
  659. if (host->timing != MMC_TIMING_MMC_HS200 &&
  660. host->timing != MMC_TIMING_MMC_HS400 &&
  661. host->timing != MMC_TIMING_UHS_SDR104)
  662. goto disable;
  663. blksz_depth = blksz / (1 << host->data_shift);
  664. fifo_depth = host->fifo_depth;
  665. if (blksz_depth > fifo_depth)
  666. goto disable;
  667. /*
  668. * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
  669. * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
  670. * Currently just choose blksz.
  671. */
  672. thld_size = blksz;
  673. mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
  674. return;
  675. disable:
  676. mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
  677. }
  678. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  679. {
  680. unsigned long irqflags;
  681. int sg_len;
  682. u32 temp;
  683. host->using_dma = 0;
  684. /* If we don't have a channel, we can't do DMA */
  685. if (!host->use_dma)
  686. return -ENODEV;
  687. sg_len = dw_mci_pre_dma_transfer(host, data, 0);
  688. if (sg_len < 0) {
  689. host->dma_ops->stop(host);
  690. return sg_len;
  691. }
  692. host->using_dma = 1;
  693. dev_vdbg(host->dev,
  694. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  695. (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
  696. sg_len);
  697. /*
  698. * Decide the MSIZE and RX/TX Watermark.
  699. * If current block size is same with previous size,
  700. * no need to update fifoth.
  701. */
  702. if (host->prev_blksz != data->blksz)
  703. dw_mci_adjust_fifoth(host, data);
  704. /* Enable the DMA interface */
  705. temp = mci_readl(host, CTRL);
  706. temp |= SDMMC_CTRL_DMA_ENABLE;
  707. mci_writel(host, CTRL, temp);
  708. /* Disable RX/TX IRQs, let DMA handle it */
  709. spin_lock_irqsave(&host->irq_lock, irqflags);
  710. temp = mci_readl(host, INTMASK);
  711. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  712. mci_writel(host, INTMASK, temp);
  713. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  714. host->dma_ops->start(host, sg_len);
  715. return 0;
  716. }
  717. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  718. {
  719. unsigned long irqflags;
  720. int flags = SG_MITER_ATOMIC;
  721. u32 temp;
  722. data->error = -EINPROGRESS;
  723. WARN_ON(host->data);
  724. host->sg = NULL;
  725. host->data = data;
  726. if (data->flags & MMC_DATA_READ) {
  727. host->dir_status = DW_MCI_RECV_STATUS;
  728. dw_mci_ctrl_rd_thld(host, data);
  729. } else {
  730. host->dir_status = DW_MCI_SEND_STATUS;
  731. }
  732. if (dw_mci_submit_data_dma(host, data)) {
  733. if (host->data->flags & MMC_DATA_READ)
  734. flags |= SG_MITER_TO_SG;
  735. else
  736. flags |= SG_MITER_FROM_SG;
  737. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  738. host->sg = data->sg;
  739. host->part_buf_start = 0;
  740. host->part_buf_count = 0;
  741. mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
  742. spin_lock_irqsave(&host->irq_lock, irqflags);
  743. temp = mci_readl(host, INTMASK);
  744. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  745. mci_writel(host, INTMASK, temp);
  746. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  747. temp = mci_readl(host, CTRL);
  748. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  749. mci_writel(host, CTRL, temp);
  750. /*
  751. * Use the initial fifoth_val for PIO mode.
  752. * If next issued data may be transfered by DMA mode,
  753. * prev_blksz should be invalidated.
  754. */
  755. mci_writel(host, FIFOTH, host->fifoth_val);
  756. host->prev_blksz = 0;
  757. } else {
  758. /*
  759. * Keep the current block size.
  760. * It will be used to decide whether to update
  761. * fifoth register next time.
  762. */
  763. host->prev_blksz = data->blksz;
  764. }
  765. }
  766. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  767. {
  768. struct dw_mci *host = slot->host;
  769. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  770. unsigned int cmd_status = 0;
  771. mci_writel(host, CMDARG, arg);
  772. wmb(); /* drain writebuffer */
  773. dw_mci_wait_while_busy(host, cmd);
  774. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  775. while (time_before(jiffies, timeout)) {
  776. cmd_status = mci_readl(host, CMD);
  777. if (!(cmd_status & SDMMC_CMD_START))
  778. return;
  779. }
  780. dev_err(&slot->mmc->class_dev,
  781. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  782. cmd, arg, cmd_status);
  783. }
  784. static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
  785. {
  786. struct dw_mci *host = slot->host;
  787. unsigned int clock = slot->clock;
  788. u32 div;
  789. u32 clk_en_a;
  790. u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
  791. /* We must continue to set bit 28 in CMD until the change is complete */
  792. if (host->state == STATE_WAITING_CMD11_DONE)
  793. sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
  794. if (!clock) {
  795. mci_writel(host, CLKENA, 0);
  796. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  797. } else if (clock != host->current_speed || force_clkinit) {
  798. div = host->bus_hz / clock;
  799. if (host->bus_hz % clock && host->bus_hz > clock)
  800. /*
  801. * move the + 1 after the divide to prevent
  802. * over-clocking the card.
  803. */
  804. div += 1;
  805. div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
  806. if ((clock << div) != slot->__clk_old || force_clkinit)
  807. dev_info(&slot->mmc->class_dev,
  808. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
  809. slot->id, host->bus_hz, clock,
  810. div ? ((host->bus_hz / div) >> 1) :
  811. host->bus_hz, div);
  812. /* disable clock */
  813. mci_writel(host, CLKENA, 0);
  814. mci_writel(host, CLKSRC, 0);
  815. /* inform CIU */
  816. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  817. /* set clock to desired speed */
  818. mci_writel(host, CLKDIV, div);
  819. /* inform CIU */
  820. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  821. /* enable clock; only low power if no SDIO */
  822. clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
  823. if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
  824. clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
  825. mci_writel(host, CLKENA, clk_en_a);
  826. /* inform CIU */
  827. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  828. /* keep the clock with reflecting clock dividor */
  829. slot->__clk_old = clock << div;
  830. }
  831. host->current_speed = clock;
  832. /* Set the current slot bus width */
  833. mci_writel(host, CTYPE, (slot->ctype << slot->id));
  834. }
  835. static void __dw_mci_start_request(struct dw_mci *host,
  836. struct dw_mci_slot *slot,
  837. struct mmc_command *cmd)
  838. {
  839. struct mmc_request *mrq;
  840. struct mmc_data *data;
  841. u32 cmdflags;
  842. mrq = slot->mrq;
  843. host->cur_slot = slot;
  844. host->mrq = mrq;
  845. host->pending_events = 0;
  846. host->completed_events = 0;
  847. host->cmd_status = 0;
  848. host->data_status = 0;
  849. host->dir_status = 0;
  850. data = cmd->data;
  851. if (data) {
  852. mci_writel(host, TMOUT, 0xFFFFFFFF);
  853. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  854. mci_writel(host, BLKSIZ, data->blksz);
  855. }
  856. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  857. /* this is the first command, send the initialization clock */
  858. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  859. cmdflags |= SDMMC_CMD_INIT;
  860. if (data) {
  861. dw_mci_submit_data(host, data);
  862. wmb(); /* drain writebuffer */
  863. }
  864. dw_mci_start_command(host, cmd, cmdflags);
  865. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  866. unsigned long irqflags;
  867. /*
  868. * Databook says to fail after 2ms w/ no response, but evidence
  869. * shows that sometimes the cmd11 interrupt takes over 130ms.
  870. * We'll set to 500ms, plus an extra jiffy just in case jiffies
  871. * is just about to roll over.
  872. *
  873. * We do this whole thing under spinlock and only if the
  874. * command hasn't already completed (indicating the the irq
  875. * already ran so we don't want the timeout).
  876. */
  877. spin_lock_irqsave(&host->irq_lock, irqflags);
  878. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  879. mod_timer(&host->cmd11_timer,
  880. jiffies + msecs_to_jiffies(500) + 1);
  881. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  882. }
  883. if (mrq->stop)
  884. host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
  885. else
  886. host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
  887. }
  888. static void dw_mci_start_request(struct dw_mci *host,
  889. struct dw_mci_slot *slot)
  890. {
  891. struct mmc_request *mrq = slot->mrq;
  892. struct mmc_command *cmd;
  893. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  894. __dw_mci_start_request(host, slot, cmd);
  895. }
  896. /* must be called with host->lock held */
  897. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  898. struct mmc_request *mrq)
  899. {
  900. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  901. host->state);
  902. slot->mrq = mrq;
  903. if (host->state == STATE_WAITING_CMD11_DONE) {
  904. dev_warn(&slot->mmc->class_dev,
  905. "Voltage change didn't complete\n");
  906. /*
  907. * this case isn't expected to happen, so we can
  908. * either crash here or just try to continue on
  909. * in the closest possible state
  910. */
  911. host->state = STATE_IDLE;
  912. }
  913. if (host->state == STATE_IDLE) {
  914. host->state = STATE_SENDING_CMD;
  915. dw_mci_start_request(host, slot);
  916. } else {
  917. list_add_tail(&slot->queue_node, &host->queue);
  918. }
  919. }
  920. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  921. {
  922. struct dw_mci_slot *slot = mmc_priv(mmc);
  923. struct dw_mci *host = slot->host;
  924. WARN_ON(slot->mrq);
  925. /*
  926. * The check for card presence and queueing of the request must be
  927. * atomic, otherwise the card could be removed in between and the
  928. * request wouldn't fail until another card was inserted.
  929. */
  930. spin_lock_bh(&host->lock);
  931. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  932. spin_unlock_bh(&host->lock);
  933. mrq->cmd->error = -ENOMEDIUM;
  934. mmc_request_done(mmc, mrq);
  935. return;
  936. }
  937. dw_mci_queue_request(host, slot, mrq);
  938. spin_unlock_bh(&host->lock);
  939. }
  940. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  941. {
  942. struct dw_mci_slot *slot = mmc_priv(mmc);
  943. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  944. u32 regs;
  945. int ret;
  946. switch (ios->bus_width) {
  947. case MMC_BUS_WIDTH_4:
  948. slot->ctype = SDMMC_CTYPE_4BIT;
  949. break;
  950. case MMC_BUS_WIDTH_8:
  951. slot->ctype = SDMMC_CTYPE_8BIT;
  952. break;
  953. default:
  954. /* set default 1 bit mode */
  955. slot->ctype = SDMMC_CTYPE_1BIT;
  956. }
  957. regs = mci_readl(slot->host, UHS_REG);
  958. /* DDR mode set */
  959. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  960. ios->timing == MMC_TIMING_MMC_HS400)
  961. regs |= ((0x1 << slot->id) << 16);
  962. else
  963. regs &= ~((0x1 << slot->id) << 16);
  964. mci_writel(slot->host, UHS_REG, regs);
  965. slot->host->timing = ios->timing;
  966. /*
  967. * Use mirror of ios->clock to prevent race with mmc
  968. * core ios update when finding the minimum.
  969. */
  970. slot->clock = ios->clock;
  971. if (drv_data && drv_data->set_ios)
  972. drv_data->set_ios(slot->host, ios);
  973. switch (ios->power_mode) {
  974. case MMC_POWER_UP:
  975. if (!IS_ERR(mmc->supply.vmmc)) {
  976. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  977. ios->vdd);
  978. if (ret) {
  979. dev_err(slot->host->dev,
  980. "failed to enable vmmc regulator\n");
  981. /*return, if failed turn on vmmc*/
  982. return;
  983. }
  984. }
  985. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  986. regs = mci_readl(slot->host, PWREN);
  987. regs |= (1 << slot->id);
  988. mci_writel(slot->host, PWREN, regs);
  989. break;
  990. case MMC_POWER_ON:
  991. if (!slot->host->vqmmc_enabled) {
  992. if (!IS_ERR(mmc->supply.vqmmc)) {
  993. ret = regulator_enable(mmc->supply.vqmmc);
  994. if (ret < 0)
  995. dev_err(slot->host->dev,
  996. "failed to enable vqmmc\n");
  997. else
  998. slot->host->vqmmc_enabled = true;
  999. } else {
  1000. /* Keep track so we don't reset again */
  1001. slot->host->vqmmc_enabled = true;
  1002. }
  1003. /* Reset our state machine after powering on */
  1004. dw_mci_ctrl_reset(slot->host,
  1005. SDMMC_CTRL_ALL_RESET_FLAGS);
  1006. }
  1007. /* Adjust clock / bus width after power is up */
  1008. dw_mci_setup_bus(slot, false);
  1009. break;
  1010. case MMC_POWER_OFF:
  1011. /* Turn clock off before power goes down */
  1012. dw_mci_setup_bus(slot, false);
  1013. if (!IS_ERR(mmc->supply.vmmc))
  1014. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1015. if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
  1016. regulator_disable(mmc->supply.vqmmc);
  1017. slot->host->vqmmc_enabled = false;
  1018. regs = mci_readl(slot->host, PWREN);
  1019. regs &= ~(1 << slot->id);
  1020. mci_writel(slot->host, PWREN, regs);
  1021. break;
  1022. default:
  1023. break;
  1024. }
  1025. if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
  1026. slot->host->state = STATE_IDLE;
  1027. }
  1028. static int dw_mci_card_busy(struct mmc_host *mmc)
  1029. {
  1030. struct dw_mci_slot *slot = mmc_priv(mmc);
  1031. u32 status;
  1032. /*
  1033. * Check the busy bit which is low when DAT[3:0]
  1034. * (the data lines) are 0000
  1035. */
  1036. status = mci_readl(slot->host, STATUS);
  1037. return !!(status & SDMMC_STATUS_BUSY);
  1038. }
  1039. static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  1040. {
  1041. struct dw_mci_slot *slot = mmc_priv(mmc);
  1042. struct dw_mci *host = slot->host;
  1043. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1044. u32 uhs;
  1045. u32 v18 = SDMMC_UHS_18V << slot->id;
  1046. int min_uv, max_uv;
  1047. int ret;
  1048. if (drv_data && drv_data->switch_voltage)
  1049. return drv_data->switch_voltage(mmc, ios);
  1050. /*
  1051. * Program the voltage. Note that some instances of dw_mmc may use
  1052. * the UHS_REG for this. For other instances (like exynos) the UHS_REG
  1053. * does no harm but you need to set the regulator directly. Try both.
  1054. */
  1055. uhs = mci_readl(host, UHS_REG);
  1056. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
  1057. min_uv = 2700000;
  1058. max_uv = 3600000;
  1059. uhs &= ~v18;
  1060. } else {
  1061. min_uv = 1700000;
  1062. max_uv = 1950000;
  1063. uhs |= v18;
  1064. }
  1065. if (!IS_ERR(mmc->supply.vqmmc)) {
  1066. ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
  1067. if (ret) {
  1068. dev_dbg(&mmc->class_dev,
  1069. "Regulator set error %d: %d - %d\n",
  1070. ret, min_uv, max_uv);
  1071. return ret;
  1072. }
  1073. }
  1074. mci_writel(host, UHS_REG, uhs);
  1075. return 0;
  1076. }
  1077. static int dw_mci_get_ro(struct mmc_host *mmc)
  1078. {
  1079. int read_only;
  1080. struct dw_mci_slot *slot = mmc_priv(mmc);
  1081. int gpio_ro = mmc_gpio_get_ro(mmc);
  1082. /* Use platform get_ro function, else try on board write protect */
  1083. if (!IS_ERR_VALUE(gpio_ro))
  1084. read_only = gpio_ro;
  1085. else
  1086. read_only =
  1087. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  1088. dev_dbg(&mmc->class_dev, "card is %s\n",
  1089. read_only ? "read-only" : "read-write");
  1090. return read_only;
  1091. }
  1092. static int dw_mci_get_cd(struct mmc_host *mmc)
  1093. {
  1094. int present;
  1095. struct dw_mci_slot *slot = mmc_priv(mmc);
  1096. struct dw_mci_board *brd = slot->host->pdata;
  1097. struct dw_mci *host = slot->host;
  1098. int gpio_cd = mmc_gpio_get_cd(mmc);
  1099. /* Use platform get_cd function, else try onboard card detect */
  1100. if ((brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION) ||
  1101. (mmc->caps & MMC_CAP_NONREMOVABLE))
  1102. present = 1;
  1103. else if (!IS_ERR_VALUE(gpio_cd))
  1104. present = gpio_cd;
  1105. else
  1106. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  1107. == 0 ? 1 : 0;
  1108. spin_lock_bh(&host->lock);
  1109. if (present) {
  1110. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1111. dev_dbg(&mmc->class_dev, "card is present\n");
  1112. } else {
  1113. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  1114. dev_dbg(&mmc->class_dev, "card is not present\n");
  1115. }
  1116. spin_unlock_bh(&host->lock);
  1117. return present;
  1118. }
  1119. static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
  1120. {
  1121. struct dw_mci_slot *slot = mmc_priv(mmc);
  1122. struct dw_mci *host = slot->host;
  1123. /*
  1124. * Low power mode will stop the card clock when idle. According to the
  1125. * description of the CLKENA register we should disable low power mode
  1126. * for SDIO cards if we need SDIO interrupts to work.
  1127. */
  1128. if (mmc->caps & MMC_CAP_SDIO_IRQ) {
  1129. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  1130. u32 clk_en_a_old;
  1131. u32 clk_en_a;
  1132. clk_en_a_old = mci_readl(host, CLKENA);
  1133. if (card->type == MMC_TYPE_SDIO ||
  1134. card->type == MMC_TYPE_SD_COMBO) {
  1135. set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1136. clk_en_a = clk_en_a_old & ~clken_low_pwr;
  1137. } else {
  1138. clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1139. clk_en_a = clk_en_a_old | clken_low_pwr;
  1140. }
  1141. if (clk_en_a != clk_en_a_old) {
  1142. mci_writel(host, CLKENA, clk_en_a);
  1143. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  1144. SDMMC_CMD_PRV_DAT_WAIT, 0);
  1145. }
  1146. }
  1147. }
  1148. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  1149. {
  1150. struct dw_mci_slot *slot = mmc_priv(mmc);
  1151. struct dw_mci *host = slot->host;
  1152. unsigned long irqflags;
  1153. u32 int_mask;
  1154. spin_lock_irqsave(&host->irq_lock, irqflags);
  1155. /* Enable/disable Slot Specific SDIO interrupt */
  1156. int_mask = mci_readl(host, INTMASK);
  1157. if (enb)
  1158. int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
  1159. else
  1160. int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
  1161. mci_writel(host, INTMASK, int_mask);
  1162. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1163. }
  1164. static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1165. {
  1166. struct dw_mci_slot *slot = mmc_priv(mmc);
  1167. struct dw_mci *host = slot->host;
  1168. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1169. int err = -EINVAL;
  1170. if (drv_data && drv_data->execute_tuning)
  1171. err = drv_data->execute_tuning(slot);
  1172. return err;
  1173. }
  1174. static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
  1175. struct mmc_ios *ios)
  1176. {
  1177. struct dw_mci_slot *slot = mmc_priv(mmc);
  1178. struct dw_mci *host = slot->host;
  1179. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1180. if (drv_data && drv_data->prepare_hs400_tuning)
  1181. return drv_data->prepare_hs400_tuning(host, ios);
  1182. return 0;
  1183. }
  1184. static const struct mmc_host_ops dw_mci_ops = {
  1185. .request = dw_mci_request,
  1186. .pre_req = dw_mci_pre_req,
  1187. .post_req = dw_mci_post_req,
  1188. .set_ios = dw_mci_set_ios,
  1189. .get_ro = dw_mci_get_ro,
  1190. .get_cd = dw_mci_get_cd,
  1191. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  1192. .execute_tuning = dw_mci_execute_tuning,
  1193. .card_busy = dw_mci_card_busy,
  1194. .start_signal_voltage_switch = dw_mci_switch_voltage,
  1195. .init_card = dw_mci_init_card,
  1196. .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
  1197. };
  1198. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  1199. __releases(&host->lock)
  1200. __acquires(&host->lock)
  1201. {
  1202. struct dw_mci_slot *slot;
  1203. struct mmc_host *prev_mmc = host->cur_slot->mmc;
  1204. WARN_ON(host->cmd || host->data);
  1205. host->cur_slot->mrq = NULL;
  1206. host->mrq = NULL;
  1207. if (!list_empty(&host->queue)) {
  1208. slot = list_entry(host->queue.next,
  1209. struct dw_mci_slot, queue_node);
  1210. list_del(&slot->queue_node);
  1211. dev_vdbg(host->dev, "list not empty: %s is next\n",
  1212. mmc_hostname(slot->mmc));
  1213. host->state = STATE_SENDING_CMD;
  1214. dw_mci_start_request(host, slot);
  1215. } else {
  1216. dev_vdbg(host->dev, "list empty\n");
  1217. if (host->state == STATE_SENDING_CMD11)
  1218. host->state = STATE_WAITING_CMD11_DONE;
  1219. else
  1220. host->state = STATE_IDLE;
  1221. }
  1222. spin_unlock(&host->lock);
  1223. mmc_request_done(prev_mmc, mrq);
  1224. spin_lock(&host->lock);
  1225. }
  1226. static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  1227. {
  1228. u32 status = host->cmd_status;
  1229. host->cmd_status = 0;
  1230. /* Read the response from the card (up to 16 bytes) */
  1231. if (cmd->flags & MMC_RSP_PRESENT) {
  1232. if (cmd->flags & MMC_RSP_136) {
  1233. cmd->resp[3] = mci_readl(host, RESP0);
  1234. cmd->resp[2] = mci_readl(host, RESP1);
  1235. cmd->resp[1] = mci_readl(host, RESP2);
  1236. cmd->resp[0] = mci_readl(host, RESP3);
  1237. } else {
  1238. cmd->resp[0] = mci_readl(host, RESP0);
  1239. cmd->resp[1] = 0;
  1240. cmd->resp[2] = 0;
  1241. cmd->resp[3] = 0;
  1242. }
  1243. }
  1244. if (status & SDMMC_INT_RTO)
  1245. cmd->error = -ETIMEDOUT;
  1246. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  1247. cmd->error = -EILSEQ;
  1248. else if (status & SDMMC_INT_RESP_ERR)
  1249. cmd->error = -EIO;
  1250. else
  1251. cmd->error = 0;
  1252. if (cmd->error) {
  1253. /* newer ip versions need a delay between retries */
  1254. if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
  1255. mdelay(20);
  1256. }
  1257. return cmd->error;
  1258. }
  1259. static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
  1260. {
  1261. u32 status = host->data_status;
  1262. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1263. if (status & SDMMC_INT_DRTO) {
  1264. data->error = -ETIMEDOUT;
  1265. } else if (status & SDMMC_INT_DCRC) {
  1266. data->error = -EILSEQ;
  1267. } else if (status & SDMMC_INT_EBE) {
  1268. if (host->dir_status ==
  1269. DW_MCI_SEND_STATUS) {
  1270. /*
  1271. * No data CRC status was returned.
  1272. * The number of bytes transferred
  1273. * will be exaggerated in PIO mode.
  1274. */
  1275. data->bytes_xfered = 0;
  1276. data->error = -ETIMEDOUT;
  1277. } else if (host->dir_status ==
  1278. DW_MCI_RECV_STATUS) {
  1279. data->error = -EIO;
  1280. }
  1281. } else {
  1282. /* SDMMC_INT_SBE is included */
  1283. data->error = -EIO;
  1284. }
  1285. dev_dbg(host->dev, "data error, status 0x%08x\n", status);
  1286. /*
  1287. * After an error, there may be data lingering
  1288. * in the FIFO
  1289. */
  1290. dw_mci_reset(host);
  1291. } else {
  1292. data->bytes_xfered = data->blocks * data->blksz;
  1293. data->error = 0;
  1294. }
  1295. return data->error;
  1296. }
  1297. static void dw_mci_set_drto(struct dw_mci *host)
  1298. {
  1299. unsigned int drto_clks;
  1300. unsigned int drto_ms;
  1301. drto_clks = mci_readl(host, TMOUT) >> 8;
  1302. drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
  1303. /* add a bit spare time */
  1304. drto_ms += 10;
  1305. mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
  1306. }
  1307. static void dw_mci_tasklet_func(unsigned long priv)
  1308. {
  1309. struct dw_mci *host = (struct dw_mci *)priv;
  1310. struct mmc_data *data;
  1311. struct mmc_command *cmd;
  1312. struct mmc_request *mrq;
  1313. enum dw_mci_state state;
  1314. enum dw_mci_state prev_state;
  1315. unsigned int err;
  1316. spin_lock(&host->lock);
  1317. state = host->state;
  1318. data = host->data;
  1319. mrq = host->mrq;
  1320. do {
  1321. prev_state = state;
  1322. switch (state) {
  1323. case STATE_IDLE:
  1324. case STATE_WAITING_CMD11_DONE:
  1325. break;
  1326. case STATE_SENDING_CMD11:
  1327. case STATE_SENDING_CMD:
  1328. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1329. &host->pending_events))
  1330. break;
  1331. cmd = host->cmd;
  1332. host->cmd = NULL;
  1333. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  1334. err = dw_mci_command_complete(host, cmd);
  1335. if (cmd == mrq->sbc && !err) {
  1336. prev_state = state = STATE_SENDING_CMD;
  1337. __dw_mci_start_request(host, host->cur_slot,
  1338. mrq->cmd);
  1339. goto unlock;
  1340. }
  1341. if (cmd->data && err) {
  1342. dw_mci_stop_dma(host);
  1343. send_stop_abort(host, data);
  1344. state = STATE_SENDING_STOP;
  1345. break;
  1346. }
  1347. if (!cmd->data || err) {
  1348. dw_mci_request_end(host, mrq);
  1349. goto unlock;
  1350. }
  1351. prev_state = state = STATE_SENDING_DATA;
  1352. /* fall through */
  1353. case STATE_SENDING_DATA:
  1354. /*
  1355. * We could get a data error and never a transfer
  1356. * complete so we'd better check for it here.
  1357. *
  1358. * Note that we don't really care if we also got a
  1359. * transfer complete; stopping the DMA and sending an
  1360. * abort won't hurt.
  1361. */
  1362. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1363. &host->pending_events)) {
  1364. dw_mci_stop_dma(host);
  1365. if (data->stop ||
  1366. !(host->data_status & (SDMMC_INT_DRTO |
  1367. SDMMC_INT_EBE)))
  1368. send_stop_abort(host, data);
  1369. state = STATE_DATA_ERROR;
  1370. break;
  1371. }
  1372. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1373. &host->pending_events)) {
  1374. /*
  1375. * If all data-related interrupts don't come
  1376. * within the given time in reading data state.
  1377. */
  1378. if ((host->quirks & DW_MCI_QUIRK_BROKEN_DTO) &&
  1379. (host->dir_status == DW_MCI_RECV_STATUS))
  1380. dw_mci_set_drto(host);
  1381. break;
  1382. }
  1383. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  1384. /*
  1385. * Handle an EVENT_DATA_ERROR that might have shown up
  1386. * before the transfer completed. This might not have
  1387. * been caught by the check above because the interrupt
  1388. * could have gone off between the previous check and
  1389. * the check for transfer complete.
  1390. *
  1391. * Technically this ought not be needed assuming we
  1392. * get a DATA_COMPLETE eventually (we'll notice the
  1393. * error and end the request), but it shouldn't hurt.
  1394. *
  1395. * This has the advantage of sending the stop command.
  1396. */
  1397. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1398. &host->pending_events)) {
  1399. dw_mci_stop_dma(host);
  1400. if (data->stop ||
  1401. !(host->data_status & (SDMMC_INT_DRTO |
  1402. SDMMC_INT_EBE)))
  1403. send_stop_abort(host, data);
  1404. state = STATE_DATA_ERROR;
  1405. break;
  1406. }
  1407. prev_state = state = STATE_DATA_BUSY;
  1408. /* fall through */
  1409. case STATE_DATA_BUSY:
  1410. if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
  1411. &host->pending_events)) {
  1412. /*
  1413. * If data error interrupt comes but data over
  1414. * interrupt doesn't come within the given time.
  1415. * in reading data state.
  1416. */
  1417. if ((host->quirks & DW_MCI_QUIRK_BROKEN_DTO) &&
  1418. (host->dir_status == DW_MCI_RECV_STATUS))
  1419. dw_mci_set_drto(host);
  1420. break;
  1421. }
  1422. host->data = NULL;
  1423. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  1424. err = dw_mci_data_complete(host, data);
  1425. if (!err) {
  1426. if (!data->stop || mrq->sbc) {
  1427. if (mrq->sbc && data->stop)
  1428. data->stop->error = 0;
  1429. dw_mci_request_end(host, mrq);
  1430. goto unlock;
  1431. }
  1432. /* stop command for open-ended transfer*/
  1433. if (data->stop)
  1434. send_stop_abort(host, data);
  1435. } else {
  1436. /*
  1437. * If we don't have a command complete now we'll
  1438. * never get one since we just reset everything;
  1439. * better end the request.
  1440. *
  1441. * If we do have a command complete we'll fall
  1442. * through to the SENDING_STOP command and
  1443. * everything will be peachy keen.
  1444. */
  1445. if (!test_bit(EVENT_CMD_COMPLETE,
  1446. &host->pending_events)) {
  1447. host->cmd = NULL;
  1448. dw_mci_request_end(host, mrq);
  1449. goto unlock;
  1450. }
  1451. }
  1452. /*
  1453. * If err has non-zero,
  1454. * stop-abort command has been already issued.
  1455. */
  1456. prev_state = state = STATE_SENDING_STOP;
  1457. /* fall through */
  1458. case STATE_SENDING_STOP:
  1459. if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
  1460. &host->pending_events))
  1461. break;
  1462. /* CMD error in data command */
  1463. if (mrq->cmd->error && mrq->data)
  1464. dw_mci_reset(host);
  1465. host->cmd = NULL;
  1466. host->data = NULL;
  1467. if (mrq->stop)
  1468. dw_mci_command_complete(host, mrq->stop);
  1469. else
  1470. host->cmd_status = 0;
  1471. dw_mci_request_end(host, mrq);
  1472. goto unlock;
  1473. case STATE_DATA_ERROR:
  1474. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1475. &host->pending_events))
  1476. break;
  1477. state = STATE_DATA_BUSY;
  1478. break;
  1479. }
  1480. } while (state != prev_state);
  1481. host->state = state;
  1482. unlock:
  1483. spin_unlock(&host->lock);
  1484. }
  1485. /* push final bytes to part_buf, only use during push */
  1486. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1487. {
  1488. memcpy((void *)&host->part_buf, buf, cnt);
  1489. host->part_buf_count = cnt;
  1490. }
  1491. /* append bytes to part_buf, only use during push */
  1492. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1493. {
  1494. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  1495. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  1496. host->part_buf_count += cnt;
  1497. return cnt;
  1498. }
  1499. /* pull first bytes from part_buf, only use during pull */
  1500. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1501. {
  1502. cnt = min_t(int, cnt, host->part_buf_count);
  1503. if (cnt) {
  1504. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  1505. cnt);
  1506. host->part_buf_count -= cnt;
  1507. host->part_buf_start += cnt;
  1508. }
  1509. return cnt;
  1510. }
  1511. /* pull final bytes from the part_buf, assuming it's just been filled */
  1512. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  1513. {
  1514. memcpy(buf, &host->part_buf, cnt);
  1515. host->part_buf_start = cnt;
  1516. host->part_buf_count = (1 << host->data_shift) - cnt;
  1517. }
  1518. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  1519. {
  1520. struct mmc_data *data = host->data;
  1521. int init_cnt = cnt;
  1522. /* try and push anything in the part_buf */
  1523. if (unlikely(host->part_buf_count)) {
  1524. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1525. buf += len;
  1526. cnt -= len;
  1527. if (host->part_buf_count == 2) {
  1528. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1529. host->part_buf_count = 0;
  1530. }
  1531. }
  1532. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1533. if (unlikely((unsigned long)buf & 0x1)) {
  1534. while (cnt >= 2) {
  1535. u16 aligned_buf[64];
  1536. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1537. int items = len >> 1;
  1538. int i;
  1539. /* memcpy from input buffer into aligned buffer */
  1540. memcpy(aligned_buf, buf, len);
  1541. buf += len;
  1542. cnt -= len;
  1543. /* push data from aligned buffer into fifo */
  1544. for (i = 0; i < items; ++i)
  1545. mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
  1546. }
  1547. } else
  1548. #endif
  1549. {
  1550. u16 *pdata = buf;
  1551. for (; cnt >= 2; cnt -= 2)
  1552. mci_fifo_writew(host->fifo_reg, *pdata++);
  1553. buf = pdata;
  1554. }
  1555. /* put anything remaining in the part_buf */
  1556. if (cnt) {
  1557. dw_mci_set_part_bytes(host, buf, cnt);
  1558. /* Push data if we have reached the expected data length */
  1559. if ((data->bytes_xfered + init_cnt) ==
  1560. (data->blksz * data->blocks))
  1561. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1562. }
  1563. }
  1564. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1565. {
  1566. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1567. if (unlikely((unsigned long)buf & 0x1)) {
  1568. while (cnt >= 2) {
  1569. /* pull data from fifo into aligned buffer */
  1570. u16 aligned_buf[64];
  1571. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1572. int items = len >> 1;
  1573. int i;
  1574. for (i = 0; i < items; ++i)
  1575. aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
  1576. /* memcpy from aligned buffer into output buffer */
  1577. memcpy(buf, aligned_buf, len);
  1578. buf += len;
  1579. cnt -= len;
  1580. }
  1581. } else
  1582. #endif
  1583. {
  1584. u16 *pdata = buf;
  1585. for (; cnt >= 2; cnt -= 2)
  1586. *pdata++ = mci_fifo_readw(host->fifo_reg);
  1587. buf = pdata;
  1588. }
  1589. if (cnt) {
  1590. host->part_buf16 = mci_fifo_readw(host->fifo_reg);
  1591. dw_mci_pull_final_bytes(host, buf, cnt);
  1592. }
  1593. }
  1594. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  1595. {
  1596. struct mmc_data *data = host->data;
  1597. int init_cnt = cnt;
  1598. /* try and push anything in the part_buf */
  1599. if (unlikely(host->part_buf_count)) {
  1600. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1601. buf += len;
  1602. cnt -= len;
  1603. if (host->part_buf_count == 4) {
  1604. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  1605. host->part_buf_count = 0;
  1606. }
  1607. }
  1608. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1609. if (unlikely((unsigned long)buf & 0x3)) {
  1610. while (cnt >= 4) {
  1611. u32 aligned_buf[32];
  1612. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1613. int items = len >> 2;
  1614. int i;
  1615. /* memcpy from input buffer into aligned buffer */
  1616. memcpy(aligned_buf, buf, len);
  1617. buf += len;
  1618. cnt -= len;
  1619. /* push data from aligned buffer into fifo */
  1620. for (i = 0; i < items; ++i)
  1621. mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
  1622. }
  1623. } else
  1624. #endif
  1625. {
  1626. u32 *pdata = buf;
  1627. for (; cnt >= 4; cnt -= 4)
  1628. mci_fifo_writel(host->fifo_reg, *pdata++);
  1629. buf = pdata;
  1630. }
  1631. /* put anything remaining in the part_buf */
  1632. if (cnt) {
  1633. dw_mci_set_part_bytes(host, buf, cnt);
  1634. /* Push data if we have reached the expected data length */
  1635. if ((data->bytes_xfered + init_cnt) ==
  1636. (data->blksz * data->blocks))
  1637. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  1638. }
  1639. }
  1640. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  1641. {
  1642. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1643. if (unlikely((unsigned long)buf & 0x3)) {
  1644. while (cnt >= 4) {
  1645. /* pull data from fifo into aligned buffer */
  1646. u32 aligned_buf[32];
  1647. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  1648. int items = len >> 2;
  1649. int i;
  1650. for (i = 0; i < items; ++i)
  1651. aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
  1652. /* memcpy from aligned buffer into output buffer */
  1653. memcpy(buf, aligned_buf, len);
  1654. buf += len;
  1655. cnt -= len;
  1656. }
  1657. } else
  1658. #endif
  1659. {
  1660. u32 *pdata = buf;
  1661. for (; cnt >= 4; cnt -= 4)
  1662. *pdata++ = mci_fifo_readl(host->fifo_reg);
  1663. buf = pdata;
  1664. }
  1665. if (cnt) {
  1666. host->part_buf32 = mci_fifo_readl(host->fifo_reg);
  1667. dw_mci_pull_final_bytes(host, buf, cnt);
  1668. }
  1669. }
  1670. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  1671. {
  1672. struct mmc_data *data = host->data;
  1673. int init_cnt = cnt;
  1674. /* try and push anything in the part_buf */
  1675. if (unlikely(host->part_buf_count)) {
  1676. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1677. buf += len;
  1678. cnt -= len;
  1679. if (host->part_buf_count == 8) {
  1680. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  1681. host->part_buf_count = 0;
  1682. }
  1683. }
  1684. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1685. if (unlikely((unsigned long)buf & 0x7)) {
  1686. while (cnt >= 8) {
  1687. u64 aligned_buf[16];
  1688. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1689. int items = len >> 3;
  1690. int i;
  1691. /* memcpy from input buffer into aligned buffer */
  1692. memcpy(aligned_buf, buf, len);
  1693. buf += len;
  1694. cnt -= len;
  1695. /* push data from aligned buffer into fifo */
  1696. for (i = 0; i < items; ++i)
  1697. mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
  1698. }
  1699. } else
  1700. #endif
  1701. {
  1702. u64 *pdata = buf;
  1703. for (; cnt >= 8; cnt -= 8)
  1704. mci_fifo_writeq(host->fifo_reg, *pdata++);
  1705. buf = pdata;
  1706. }
  1707. /* put anything remaining in the part_buf */
  1708. if (cnt) {
  1709. dw_mci_set_part_bytes(host, buf, cnt);
  1710. /* Push data if we have reached the expected data length */
  1711. if ((data->bytes_xfered + init_cnt) ==
  1712. (data->blksz * data->blocks))
  1713. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  1714. }
  1715. }
  1716. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  1717. {
  1718. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1719. if (unlikely((unsigned long)buf & 0x7)) {
  1720. while (cnt >= 8) {
  1721. /* pull data from fifo into aligned buffer */
  1722. u64 aligned_buf[16];
  1723. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  1724. int items = len >> 3;
  1725. int i;
  1726. for (i = 0; i < items; ++i)
  1727. aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
  1728. /* memcpy from aligned buffer into output buffer */
  1729. memcpy(buf, aligned_buf, len);
  1730. buf += len;
  1731. cnt -= len;
  1732. }
  1733. } else
  1734. #endif
  1735. {
  1736. u64 *pdata = buf;
  1737. for (; cnt >= 8; cnt -= 8)
  1738. *pdata++ = mci_fifo_readq(host->fifo_reg);
  1739. buf = pdata;
  1740. }
  1741. if (cnt) {
  1742. host->part_buf = mci_fifo_readq(host->fifo_reg);
  1743. dw_mci_pull_final_bytes(host, buf, cnt);
  1744. }
  1745. }
  1746. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  1747. {
  1748. int len;
  1749. /* get remaining partial bytes */
  1750. len = dw_mci_pull_part_bytes(host, buf, cnt);
  1751. if (unlikely(len == cnt))
  1752. return;
  1753. buf += len;
  1754. cnt -= len;
  1755. /* get the rest of the data */
  1756. host->pull_data(host, buf, cnt);
  1757. }
  1758. static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
  1759. {
  1760. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1761. void *buf;
  1762. unsigned int offset;
  1763. struct mmc_data *data = host->data;
  1764. int shift = host->data_shift;
  1765. u32 status;
  1766. unsigned int len;
  1767. unsigned int remain, fcnt;
  1768. do {
  1769. if (!sg_miter_next(sg_miter))
  1770. goto done;
  1771. host->sg = sg_miter->piter.sg;
  1772. buf = sg_miter->addr;
  1773. remain = sg_miter->length;
  1774. offset = 0;
  1775. do {
  1776. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  1777. << shift) + host->part_buf_count;
  1778. len = min(remain, fcnt);
  1779. if (!len)
  1780. break;
  1781. dw_mci_pull_data(host, (void *)(buf + offset), len);
  1782. data->bytes_xfered += len;
  1783. offset += len;
  1784. remain -= len;
  1785. } while (remain);
  1786. sg_miter->consumed = offset;
  1787. status = mci_readl(host, MINTSTS);
  1788. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1789. /* if the RXDR is ready read again */
  1790. } while ((status & SDMMC_INT_RXDR) ||
  1791. (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
  1792. if (!remain) {
  1793. if (!sg_miter_next(sg_miter))
  1794. goto done;
  1795. sg_miter->consumed = 0;
  1796. }
  1797. sg_miter_stop(sg_miter);
  1798. return;
  1799. done:
  1800. sg_miter_stop(sg_miter);
  1801. host->sg = NULL;
  1802. smp_wmb(); /* drain writebuffer */
  1803. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1804. }
  1805. static void dw_mci_write_data_pio(struct dw_mci *host)
  1806. {
  1807. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1808. void *buf;
  1809. unsigned int offset;
  1810. struct mmc_data *data = host->data;
  1811. int shift = host->data_shift;
  1812. u32 status;
  1813. unsigned int len;
  1814. unsigned int fifo_depth = host->fifo_depth;
  1815. unsigned int remain, fcnt;
  1816. do {
  1817. if (!sg_miter_next(sg_miter))
  1818. goto done;
  1819. host->sg = sg_miter->piter.sg;
  1820. buf = sg_miter->addr;
  1821. remain = sg_miter->length;
  1822. offset = 0;
  1823. do {
  1824. fcnt = ((fifo_depth -
  1825. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  1826. << shift) - host->part_buf_count;
  1827. len = min(remain, fcnt);
  1828. if (!len)
  1829. break;
  1830. host->push_data(host, (void *)(buf + offset), len);
  1831. data->bytes_xfered += len;
  1832. offset += len;
  1833. remain -= len;
  1834. } while (remain);
  1835. sg_miter->consumed = offset;
  1836. status = mci_readl(host, MINTSTS);
  1837. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1838. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  1839. if (!remain) {
  1840. if (!sg_miter_next(sg_miter))
  1841. goto done;
  1842. sg_miter->consumed = 0;
  1843. }
  1844. sg_miter_stop(sg_miter);
  1845. return;
  1846. done:
  1847. sg_miter_stop(sg_miter);
  1848. host->sg = NULL;
  1849. smp_wmb(); /* drain writebuffer */
  1850. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  1851. }
  1852. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  1853. {
  1854. if (!host->cmd_status)
  1855. host->cmd_status = status;
  1856. smp_wmb(); /* drain writebuffer */
  1857. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1858. tasklet_schedule(&host->tasklet);
  1859. }
  1860. static void dw_mci_handle_cd(struct dw_mci *host)
  1861. {
  1862. int i;
  1863. for (i = 0; i < host->num_slots; i++) {
  1864. struct dw_mci_slot *slot = host->slot[i];
  1865. if (!slot)
  1866. continue;
  1867. if (slot->mmc->ops->card_event)
  1868. slot->mmc->ops->card_event(slot->mmc);
  1869. mmc_detect_change(slot->mmc,
  1870. msecs_to_jiffies(host->pdata->detect_delay_ms));
  1871. }
  1872. }
  1873. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  1874. {
  1875. struct dw_mci *host = dev_id;
  1876. u32 pending;
  1877. int i;
  1878. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  1879. /*
  1880. * DTO fix - version 2.10a and below, and only if internal DMA
  1881. * is configured.
  1882. */
  1883. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
  1884. if (!pending &&
  1885. ((mci_readl(host, STATUS) >> 17) & 0x1fff))
  1886. pending |= SDMMC_INT_DATA_OVER;
  1887. }
  1888. if (pending) {
  1889. /* Check volt switch first, since it can look like an error */
  1890. if ((host->state == STATE_SENDING_CMD11) &&
  1891. (pending & SDMMC_INT_VOLT_SWITCH)) {
  1892. unsigned long irqflags;
  1893. mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
  1894. pending &= ~SDMMC_INT_VOLT_SWITCH;
  1895. /*
  1896. * Hold the lock; we know cmd11_timer can't be kicked
  1897. * off after the lock is released, so safe to delete.
  1898. */
  1899. spin_lock_irqsave(&host->irq_lock, irqflags);
  1900. dw_mci_cmd_interrupt(host, pending);
  1901. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1902. del_timer(&host->cmd11_timer);
  1903. }
  1904. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  1905. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  1906. host->cmd_status = pending;
  1907. smp_wmb(); /* drain writebuffer */
  1908. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1909. }
  1910. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  1911. /* if there is an error report DATA_ERROR */
  1912. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  1913. host->data_status = pending;
  1914. smp_wmb(); /* drain writebuffer */
  1915. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1916. tasklet_schedule(&host->tasklet);
  1917. }
  1918. if (pending & SDMMC_INT_DATA_OVER) {
  1919. if (host->quirks & DW_MCI_QUIRK_BROKEN_DTO)
  1920. del_timer(&host->dto_timer);
  1921. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  1922. if (!host->data_status)
  1923. host->data_status = pending;
  1924. smp_wmb(); /* drain writebuffer */
  1925. if (host->dir_status == DW_MCI_RECV_STATUS) {
  1926. if (host->sg != NULL)
  1927. dw_mci_read_data_pio(host, true);
  1928. }
  1929. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1930. tasklet_schedule(&host->tasklet);
  1931. }
  1932. if (pending & SDMMC_INT_RXDR) {
  1933. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  1934. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  1935. dw_mci_read_data_pio(host, false);
  1936. }
  1937. if (pending & SDMMC_INT_TXDR) {
  1938. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  1939. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  1940. dw_mci_write_data_pio(host);
  1941. }
  1942. if (pending & SDMMC_INT_CMD_DONE) {
  1943. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  1944. dw_mci_cmd_interrupt(host, pending);
  1945. }
  1946. if (pending & SDMMC_INT_CD) {
  1947. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  1948. dw_mci_handle_cd(host);
  1949. }
  1950. /* Handle SDIO Interrupts */
  1951. for (i = 0; i < host->num_slots; i++) {
  1952. struct dw_mci_slot *slot = host->slot[i];
  1953. if (!slot)
  1954. continue;
  1955. if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
  1956. mci_writel(host, RINTSTS,
  1957. SDMMC_INT_SDIO(slot->sdio_id));
  1958. mmc_signal_sdio_irq(slot->mmc);
  1959. }
  1960. }
  1961. }
  1962. #ifdef CONFIG_MMC_DW_IDMAC
  1963. /* Handle DMA interrupts */
  1964. if (host->dma_64bit_address == 1) {
  1965. pending = mci_readl(host, IDSTS64);
  1966. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1967. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
  1968. SDMMC_IDMAC_INT_RI);
  1969. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
  1970. host->dma_ops->complete(host);
  1971. }
  1972. } else {
  1973. pending = mci_readl(host, IDSTS);
  1974. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  1975. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
  1976. SDMMC_IDMAC_INT_RI);
  1977. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  1978. host->dma_ops->complete(host);
  1979. }
  1980. }
  1981. #endif
  1982. return IRQ_HANDLED;
  1983. }
  1984. #ifdef CONFIG_OF
  1985. /* given a slot, find out the device node representing that slot */
  1986. static struct device_node *dw_mci_of_find_slot_node(struct dw_mci_slot *slot)
  1987. {
  1988. struct device *dev = slot->mmc->parent;
  1989. struct device_node *np;
  1990. const __be32 *addr;
  1991. int len;
  1992. if (!dev || !dev->of_node)
  1993. return NULL;
  1994. for_each_child_of_node(dev->of_node, np) {
  1995. addr = of_get_property(np, "reg", &len);
  1996. if (!addr || (len < sizeof(int)))
  1997. continue;
  1998. if (be32_to_cpup(addr) == slot->id)
  1999. return np;
  2000. }
  2001. return NULL;
  2002. }
  2003. static void dw_mci_slot_of_parse(struct dw_mci_slot *slot)
  2004. {
  2005. struct device_node *np = dw_mci_of_find_slot_node(slot);
  2006. if (!np)
  2007. return;
  2008. if (of_property_read_bool(np, "disable-wp")) {
  2009. slot->mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
  2010. dev_warn(slot->mmc->parent,
  2011. "Slot quirk 'disable-wp' is deprecated\n");
  2012. }
  2013. }
  2014. #else /* CONFIG_OF */
  2015. static void dw_mci_slot_of_parse(struct dw_mci_slot *slot)
  2016. {
  2017. }
  2018. #endif /* CONFIG_OF */
  2019. static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
  2020. {
  2021. struct mmc_host *mmc;
  2022. struct dw_mci_slot *slot;
  2023. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2024. int ctrl_id, ret;
  2025. u32 freq[2];
  2026. mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
  2027. if (!mmc)
  2028. return -ENOMEM;
  2029. slot = mmc_priv(mmc);
  2030. slot->id = id;
  2031. slot->sdio_id = host->sdio_id0 + id;
  2032. slot->mmc = mmc;
  2033. slot->host = host;
  2034. host->slot[id] = slot;
  2035. mmc->ops = &dw_mci_ops;
  2036. if (of_property_read_u32_array(host->dev->of_node,
  2037. "clock-freq-min-max", freq, 2)) {
  2038. mmc->f_min = DW_MCI_FREQ_MIN;
  2039. mmc->f_max = DW_MCI_FREQ_MAX;
  2040. } else {
  2041. mmc->f_min = freq[0];
  2042. mmc->f_max = freq[1];
  2043. }
  2044. /*if there are external regulators, get them*/
  2045. ret = mmc_regulator_get_supply(mmc);
  2046. if (ret == -EPROBE_DEFER)
  2047. goto err_host_allocated;
  2048. if (!mmc->ocr_avail)
  2049. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  2050. if (host->pdata->caps)
  2051. mmc->caps = host->pdata->caps;
  2052. if (host->pdata->pm_caps)
  2053. mmc->pm_caps = host->pdata->pm_caps;
  2054. if (host->dev->of_node) {
  2055. ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  2056. if (ctrl_id < 0)
  2057. ctrl_id = 0;
  2058. } else {
  2059. ctrl_id = to_platform_device(host->dev)->id;
  2060. }
  2061. if (drv_data && drv_data->caps)
  2062. mmc->caps |= drv_data->caps[ctrl_id];
  2063. if (host->pdata->caps2)
  2064. mmc->caps2 = host->pdata->caps2;
  2065. dw_mci_slot_of_parse(slot);
  2066. ret = mmc_of_parse(mmc);
  2067. if (ret)
  2068. goto err_host_allocated;
  2069. /* Useful defaults if platform data is unset. */
  2070. if (host->use_dma) {
  2071. mmc->max_segs = host->ring_size;
  2072. mmc->max_blk_size = 65536;
  2073. mmc->max_seg_size = 0x1000;
  2074. mmc->max_req_size = mmc->max_seg_size * host->ring_size;
  2075. mmc->max_blk_count = mmc->max_req_size / 512;
  2076. } else {
  2077. mmc->max_segs = 64;
  2078. mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
  2079. mmc->max_blk_count = 512;
  2080. mmc->max_req_size = mmc->max_blk_size *
  2081. mmc->max_blk_count;
  2082. mmc->max_seg_size = mmc->max_req_size;
  2083. }
  2084. if (dw_mci_get_cd(mmc))
  2085. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  2086. else
  2087. clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  2088. ret = mmc_add_host(mmc);
  2089. if (ret)
  2090. goto err_host_allocated;
  2091. #if defined(CONFIG_DEBUG_FS)
  2092. dw_mci_init_debugfs(slot);
  2093. #endif
  2094. return 0;
  2095. err_host_allocated:
  2096. mmc_free_host(mmc);
  2097. return ret;
  2098. }
  2099. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
  2100. {
  2101. /* Debugfs stuff is cleaned up by mmc core */
  2102. mmc_remove_host(slot->mmc);
  2103. slot->host->slot[id] = NULL;
  2104. mmc_free_host(slot->mmc);
  2105. }
  2106. static void dw_mci_init_dma(struct dw_mci *host)
  2107. {
  2108. int addr_config;
  2109. /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */
  2110. addr_config = (mci_readl(host, HCON) >> 27) & 0x01;
  2111. if (addr_config == 1) {
  2112. /* host supports IDMAC in 64-bit address mode */
  2113. host->dma_64bit_address = 1;
  2114. dev_info(host->dev, "IDMAC supports 64-bit address mode.\n");
  2115. if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
  2116. dma_set_coherent_mask(host->dev, DMA_BIT_MASK(64));
  2117. } else {
  2118. /* host supports IDMAC in 32-bit address mode */
  2119. host->dma_64bit_address = 0;
  2120. dev_info(host->dev, "IDMAC supports 32-bit address mode.\n");
  2121. }
  2122. /* Alloc memory for sg translation */
  2123. host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
  2124. &host->sg_dma, GFP_KERNEL);
  2125. if (!host->sg_cpu) {
  2126. dev_err(host->dev, "%s: could not alloc DMA memory\n",
  2127. __func__);
  2128. goto no_dma;
  2129. }
  2130. /* Determine which DMA interface to use */
  2131. #ifdef CONFIG_MMC_DW_IDMAC
  2132. host->dma_ops = &dw_mci_idmac_ops;
  2133. dev_info(host->dev, "Using internal DMA controller.\n");
  2134. #endif
  2135. if (!host->dma_ops)
  2136. goto no_dma;
  2137. if (host->dma_ops->init && host->dma_ops->start &&
  2138. host->dma_ops->stop && host->dma_ops->cleanup) {
  2139. if (host->dma_ops->init(host)) {
  2140. dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
  2141. __func__);
  2142. goto no_dma;
  2143. }
  2144. } else {
  2145. dev_err(host->dev, "DMA initialization not found.\n");
  2146. goto no_dma;
  2147. }
  2148. host->use_dma = 1;
  2149. return;
  2150. no_dma:
  2151. dev_info(host->dev, "Using PIO mode.\n");
  2152. host->use_dma = 0;
  2153. }
  2154. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
  2155. {
  2156. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  2157. u32 ctrl;
  2158. ctrl = mci_readl(host, CTRL);
  2159. ctrl |= reset;
  2160. mci_writel(host, CTRL, ctrl);
  2161. /* wait till resets clear */
  2162. do {
  2163. ctrl = mci_readl(host, CTRL);
  2164. if (!(ctrl & reset))
  2165. return true;
  2166. } while (time_before(jiffies, timeout));
  2167. dev_err(host->dev,
  2168. "Timeout resetting block (ctrl reset %#x)\n",
  2169. ctrl & reset);
  2170. return false;
  2171. }
  2172. static bool dw_mci_reset(struct dw_mci *host)
  2173. {
  2174. u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
  2175. bool ret = false;
  2176. /*
  2177. * Reseting generates a block interrupt, hence setting
  2178. * the scatter-gather pointer to NULL.
  2179. */
  2180. if (host->sg) {
  2181. sg_miter_stop(&host->sg_miter);
  2182. host->sg = NULL;
  2183. }
  2184. if (host->use_dma)
  2185. flags |= SDMMC_CTRL_DMA_RESET;
  2186. if (dw_mci_ctrl_reset(host, flags)) {
  2187. /*
  2188. * In all cases we clear the RAWINTS register to clear any
  2189. * interrupts.
  2190. */
  2191. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2192. /* if using dma we wait for dma_req to clear */
  2193. if (host->use_dma) {
  2194. unsigned long timeout = jiffies + msecs_to_jiffies(500);
  2195. u32 status;
  2196. do {
  2197. status = mci_readl(host, STATUS);
  2198. if (!(status & SDMMC_STATUS_DMA_REQ))
  2199. break;
  2200. cpu_relax();
  2201. } while (time_before(jiffies, timeout));
  2202. if (status & SDMMC_STATUS_DMA_REQ) {
  2203. dev_err(host->dev,
  2204. "%s: Timeout waiting for dma_req to clear during reset\n",
  2205. __func__);
  2206. goto ciu_out;
  2207. }
  2208. /* when using DMA next we reset the fifo again */
  2209. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
  2210. goto ciu_out;
  2211. }
  2212. } else {
  2213. /* if the controller reset bit did clear, then set clock regs */
  2214. if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
  2215. dev_err(host->dev,
  2216. "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
  2217. __func__);
  2218. goto ciu_out;
  2219. }
  2220. }
  2221. #if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
  2222. /* It is also recommended that we reset and reprogram idmac */
  2223. dw_mci_idmac_reset(host);
  2224. #endif
  2225. ret = true;
  2226. ciu_out:
  2227. /* After a CTRL reset we need to have CIU set clock registers */
  2228. mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
  2229. return ret;
  2230. }
  2231. static void dw_mci_cmd11_timer(unsigned long arg)
  2232. {
  2233. struct dw_mci *host = (struct dw_mci *)arg;
  2234. if (host->state != STATE_SENDING_CMD11) {
  2235. dev_warn(host->dev, "Unexpected CMD11 timeout\n");
  2236. return;
  2237. }
  2238. host->cmd_status = SDMMC_INT_RTO;
  2239. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2240. tasklet_schedule(&host->tasklet);
  2241. }
  2242. static void dw_mci_dto_timer(unsigned long arg)
  2243. {
  2244. struct dw_mci *host = (struct dw_mci *)arg;
  2245. switch (host->state) {
  2246. case STATE_SENDING_DATA:
  2247. case STATE_DATA_BUSY:
  2248. /*
  2249. * If DTO interrupt does NOT come in sending data state,
  2250. * we should notify the driver to terminate current transfer
  2251. * and report a data timeout to the core.
  2252. */
  2253. host->data_status = SDMMC_INT_DRTO;
  2254. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  2255. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  2256. tasklet_schedule(&host->tasklet);
  2257. break;
  2258. default:
  2259. break;
  2260. }
  2261. }
  2262. #ifdef CONFIG_OF
  2263. static struct dw_mci_of_quirks {
  2264. char *quirk;
  2265. int id;
  2266. } of_quirks[] = {
  2267. {
  2268. .quirk = "broken-cd",
  2269. .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
  2270. },
  2271. };
  2272. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2273. {
  2274. struct dw_mci_board *pdata;
  2275. struct device *dev = host->dev;
  2276. struct device_node *np = dev->of_node;
  2277. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2278. int idx, ret;
  2279. u32 clock_frequency;
  2280. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2281. if (!pdata)
  2282. return ERR_PTR(-ENOMEM);
  2283. /* find out number of slots supported */
  2284. if (of_property_read_u32(dev->of_node, "num-slots",
  2285. &pdata->num_slots)) {
  2286. dev_info(dev,
  2287. "num-slots property not found, assuming 1 slot is available\n");
  2288. pdata->num_slots = 1;
  2289. }
  2290. /* get quirks */
  2291. for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
  2292. if (of_get_property(np, of_quirks[idx].quirk, NULL))
  2293. pdata->quirks |= of_quirks[idx].id;
  2294. if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
  2295. dev_info(dev,
  2296. "fifo-depth property not found, using value of FIFOTH register as default\n");
  2297. of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
  2298. if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
  2299. pdata->bus_hz = clock_frequency;
  2300. if (drv_data && drv_data->parse_dt) {
  2301. ret = drv_data->parse_dt(host);
  2302. if (ret)
  2303. return ERR_PTR(ret);
  2304. }
  2305. if (of_find_property(np, "supports-highspeed", NULL)) {
  2306. dev_info(dev, "supports-highspeed property is deprecated.\n");
  2307. pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  2308. }
  2309. return pdata;
  2310. }
  2311. #else /* CONFIG_OF */
  2312. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2313. {
  2314. return ERR_PTR(-EINVAL);
  2315. }
  2316. #endif /* CONFIG_OF */
  2317. static void dw_mci_enable_cd(struct dw_mci *host)
  2318. {
  2319. struct dw_mci_board *brd = host->pdata;
  2320. unsigned long irqflags;
  2321. u32 temp;
  2322. int i;
  2323. /* No need for CD if broken card detection */
  2324. if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
  2325. return;
  2326. /* No need for CD if all slots have a non-error GPIO */
  2327. for (i = 0; i < host->num_slots; i++) {
  2328. struct dw_mci_slot *slot = host->slot[i];
  2329. if (IS_ERR_VALUE(mmc_gpio_get_cd(slot->mmc)))
  2330. break;
  2331. }
  2332. if (i == host->num_slots)
  2333. return;
  2334. spin_lock_irqsave(&host->irq_lock, irqflags);
  2335. temp = mci_readl(host, INTMASK);
  2336. temp |= SDMMC_INT_CD;
  2337. mci_writel(host, INTMASK, temp);
  2338. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2339. }
  2340. int dw_mci_probe(struct dw_mci *host)
  2341. {
  2342. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2343. int width, i, ret = 0;
  2344. u32 fifo_size;
  2345. int init_slots = 0;
  2346. if (!host->pdata) {
  2347. host->pdata = dw_mci_parse_dt(host);
  2348. if (IS_ERR(host->pdata)) {
  2349. dev_err(host->dev, "platform data not available\n");
  2350. return -EINVAL;
  2351. }
  2352. }
  2353. if (host->pdata->num_slots < 1) {
  2354. dev_err(host->dev,
  2355. "Platform data must supply num_slots.\n");
  2356. return -ENODEV;
  2357. }
  2358. host->biu_clk = devm_clk_get(host->dev, "biu");
  2359. if (IS_ERR(host->biu_clk)) {
  2360. dev_dbg(host->dev, "biu clock not available\n");
  2361. } else {
  2362. ret = clk_prepare_enable(host->biu_clk);
  2363. if (ret) {
  2364. dev_err(host->dev, "failed to enable biu clock\n");
  2365. return ret;
  2366. }
  2367. }
  2368. host->ciu_clk = devm_clk_get(host->dev, "ciu");
  2369. if (IS_ERR(host->ciu_clk)) {
  2370. dev_dbg(host->dev, "ciu clock not available\n");
  2371. host->bus_hz = host->pdata->bus_hz;
  2372. } else {
  2373. ret = clk_prepare_enable(host->ciu_clk);
  2374. if (ret) {
  2375. dev_err(host->dev, "failed to enable ciu clock\n");
  2376. goto err_clk_biu;
  2377. }
  2378. if (host->pdata->bus_hz) {
  2379. ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
  2380. if (ret)
  2381. dev_warn(host->dev,
  2382. "Unable to set bus rate to %uHz\n",
  2383. host->pdata->bus_hz);
  2384. }
  2385. host->bus_hz = clk_get_rate(host->ciu_clk);
  2386. }
  2387. if (!host->bus_hz) {
  2388. dev_err(host->dev,
  2389. "Platform data must supply bus speed\n");
  2390. ret = -ENODEV;
  2391. goto err_clk_ciu;
  2392. }
  2393. if (drv_data && drv_data->init) {
  2394. ret = drv_data->init(host);
  2395. if (ret) {
  2396. dev_err(host->dev,
  2397. "implementation specific init failed\n");
  2398. goto err_clk_ciu;
  2399. }
  2400. }
  2401. if (drv_data && drv_data->setup_clock) {
  2402. ret = drv_data->setup_clock(host);
  2403. if (ret) {
  2404. dev_err(host->dev,
  2405. "implementation specific clock setup failed\n");
  2406. goto err_clk_ciu;
  2407. }
  2408. }
  2409. setup_timer(&host->cmd11_timer,
  2410. dw_mci_cmd11_timer, (unsigned long)host);
  2411. host->quirks = host->pdata->quirks;
  2412. if (host->quirks & DW_MCI_QUIRK_BROKEN_DTO)
  2413. setup_timer(&host->dto_timer,
  2414. dw_mci_dto_timer, (unsigned long)host);
  2415. spin_lock_init(&host->lock);
  2416. spin_lock_init(&host->irq_lock);
  2417. INIT_LIST_HEAD(&host->queue);
  2418. /*
  2419. * Get the host data width - this assumes that HCON has been set with
  2420. * the correct values.
  2421. */
  2422. i = (mci_readl(host, HCON) >> 7) & 0x7;
  2423. if (!i) {
  2424. host->push_data = dw_mci_push_data16;
  2425. host->pull_data = dw_mci_pull_data16;
  2426. width = 16;
  2427. host->data_shift = 1;
  2428. } else if (i == 2) {
  2429. host->push_data = dw_mci_push_data64;
  2430. host->pull_data = dw_mci_pull_data64;
  2431. width = 64;
  2432. host->data_shift = 3;
  2433. } else {
  2434. /* Check for a reserved value, and warn if it is */
  2435. WARN((i != 1),
  2436. "HCON reports a reserved host data width!\n"
  2437. "Defaulting to 32-bit access.\n");
  2438. host->push_data = dw_mci_push_data32;
  2439. host->pull_data = dw_mci_pull_data32;
  2440. width = 32;
  2441. host->data_shift = 2;
  2442. }
  2443. /* Reset all blocks */
  2444. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
  2445. return -ENODEV;
  2446. host->dma_ops = host->pdata->dma_ops;
  2447. dw_mci_init_dma(host);
  2448. /* Clear the interrupts for the host controller */
  2449. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2450. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2451. /* Put in max timeout */
  2452. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2453. /*
  2454. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  2455. * Tx Mark = fifo_size / 2 DMA Size = 8
  2456. */
  2457. if (!host->pdata->fifo_depth) {
  2458. /*
  2459. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  2460. * have been overwritten by the bootloader, just like we're
  2461. * about to do, so if you know the value for your hardware, you
  2462. * should put it in the platform data.
  2463. */
  2464. fifo_size = mci_readl(host, FIFOTH);
  2465. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  2466. } else {
  2467. fifo_size = host->pdata->fifo_depth;
  2468. }
  2469. host->fifo_depth = fifo_size;
  2470. host->fifoth_val =
  2471. SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
  2472. mci_writel(host, FIFOTH, host->fifoth_val);
  2473. /* disable clock to CIU */
  2474. mci_writel(host, CLKENA, 0);
  2475. mci_writel(host, CLKSRC, 0);
  2476. /*
  2477. * In 2.40a spec, Data offset is changed.
  2478. * Need to check the version-id and set data-offset for DATA register.
  2479. */
  2480. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  2481. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  2482. if (host->verid < DW_MMC_240A)
  2483. host->fifo_reg = host->regs + DATA_OFFSET;
  2484. else
  2485. host->fifo_reg = host->regs + DATA_240A_OFFSET;
  2486. tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
  2487. ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
  2488. host->irq_flags, "dw-mci", host);
  2489. if (ret)
  2490. goto err_dmaunmap;
  2491. if (host->pdata->num_slots)
  2492. host->num_slots = host->pdata->num_slots;
  2493. else
  2494. host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
  2495. /*
  2496. * Enable interrupts for command done, data over, data empty,
  2497. * receive ready and error such as transmit, receive timeout, crc error
  2498. */
  2499. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2500. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2501. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2502. DW_MCI_ERROR_FLAGS);
  2503. /* Enable mci interrupt */
  2504. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2505. dev_info(host->dev,
  2506. "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
  2507. host->irq, width, fifo_size);
  2508. /* We need at least one slot to succeed */
  2509. for (i = 0; i < host->num_slots; i++) {
  2510. ret = dw_mci_init_slot(host, i);
  2511. if (ret)
  2512. dev_dbg(host->dev, "slot %d init failed\n", i);
  2513. else
  2514. init_slots++;
  2515. }
  2516. if (init_slots) {
  2517. dev_info(host->dev, "%d slots initialized\n", init_slots);
  2518. } else {
  2519. dev_dbg(host->dev,
  2520. "attempted to initialize %d slots, but failed on all\n",
  2521. host->num_slots);
  2522. goto err_dmaunmap;
  2523. }
  2524. /* Now that slots are all setup, we can enable card detect */
  2525. dw_mci_enable_cd(host);
  2526. if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
  2527. dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
  2528. return 0;
  2529. err_dmaunmap:
  2530. if (host->use_dma && host->dma_ops->exit)
  2531. host->dma_ops->exit(host);
  2532. err_clk_ciu:
  2533. if (!IS_ERR(host->ciu_clk))
  2534. clk_disable_unprepare(host->ciu_clk);
  2535. err_clk_biu:
  2536. if (!IS_ERR(host->biu_clk))
  2537. clk_disable_unprepare(host->biu_clk);
  2538. return ret;
  2539. }
  2540. EXPORT_SYMBOL(dw_mci_probe);
  2541. void dw_mci_remove(struct dw_mci *host)
  2542. {
  2543. int i;
  2544. for (i = 0; i < host->num_slots; i++) {
  2545. dev_dbg(host->dev, "remove slot %d\n", i);
  2546. if (host->slot[i])
  2547. dw_mci_cleanup_slot(host->slot[i], i);
  2548. }
  2549. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2550. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2551. /* disable clock to CIU */
  2552. mci_writel(host, CLKENA, 0);
  2553. mci_writel(host, CLKSRC, 0);
  2554. if (host->use_dma && host->dma_ops->exit)
  2555. host->dma_ops->exit(host);
  2556. if (!IS_ERR(host->ciu_clk))
  2557. clk_disable_unprepare(host->ciu_clk);
  2558. if (!IS_ERR(host->biu_clk))
  2559. clk_disable_unprepare(host->biu_clk);
  2560. }
  2561. EXPORT_SYMBOL(dw_mci_remove);
  2562. #ifdef CONFIG_PM_SLEEP
  2563. /*
  2564. * TODO: we should probably disable the clock to the card in the suspend path.
  2565. */
  2566. int dw_mci_suspend(struct dw_mci *host)
  2567. {
  2568. return 0;
  2569. }
  2570. EXPORT_SYMBOL(dw_mci_suspend);
  2571. int dw_mci_resume(struct dw_mci *host)
  2572. {
  2573. int i, ret;
  2574. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2575. ret = -ENODEV;
  2576. return ret;
  2577. }
  2578. if (host->use_dma && host->dma_ops->init)
  2579. host->dma_ops->init(host);
  2580. /*
  2581. * Restore the initial value at FIFOTH register
  2582. * And Invalidate the prev_blksz with zero
  2583. */
  2584. mci_writel(host, FIFOTH, host->fifoth_val);
  2585. host->prev_blksz = 0;
  2586. /* Put in max timeout */
  2587. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2588. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2589. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2590. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2591. DW_MCI_ERROR_FLAGS);
  2592. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2593. for (i = 0; i < host->num_slots; i++) {
  2594. struct dw_mci_slot *slot = host->slot[i];
  2595. if (!slot)
  2596. continue;
  2597. if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
  2598. dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
  2599. dw_mci_setup_bus(slot, true);
  2600. }
  2601. }
  2602. /* Now that slots are all setup, we can enable card detect */
  2603. dw_mci_enable_cd(host);
  2604. return 0;
  2605. }
  2606. EXPORT_SYMBOL(dw_mci_resume);
  2607. #endif /* CONFIG_PM_SLEEP */
  2608. static int __init dw_mci_init(void)
  2609. {
  2610. pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
  2611. return 0;
  2612. }
  2613. static void __exit dw_mci_exit(void)
  2614. {
  2615. }
  2616. module_init(dw_mci_init);
  2617. module_exit(dw_mci_exit);
  2618. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  2619. MODULE_AUTHOR("NXP Semiconductor VietNam");
  2620. MODULE_AUTHOR("Imagination Technologies Ltd");
  2621. MODULE_LICENSE("GPL v2");