dw_mmc.c 70 KB

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