dw_mmc.c 65 KB

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