dw_mmc.c 69 KB

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