dw_mmc.c 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Synopsys DesignWare Multimedia Card Interface driver
  4. * (Based on NXP driver for lpc 31xx)
  5. *
  6. * Copyright (C) 2009 NXP Semiconductors
  7. * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
  8. */
  9. #include <linux/blkdev.h>
  10. #include <linux/clk.h>
  11. #include <linux/debugfs.h>
  12. #include <linux/device.h>
  13. #include <linux/dma-mapping.h>
  14. #include <linux/err.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/ioport.h>
  19. #include <linux/ktime.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/prandom.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/slab.h>
  26. #include <linux/stat.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/mmc/card.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/sd.h>
  33. #include <linux/mmc/sdio.h>
  34. #include <linux/bitops.h>
  35. #include <linux/regulator/consumer.h>
  36. #include <linux/of.h>
  37. #include <linux/mmc/slot-gpio.h>
  38. #include "dw_mmc.h"
  39. /* Common flag combinations */
  40. #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
  41. SDMMC_INT_HTO | SDMMC_INT_SBE | \
  42. SDMMC_INT_EBE | SDMMC_INT_HLE)
  43. #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
  44. SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
  45. #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
  46. DW_MCI_CMD_ERROR_FLAGS)
  47. #define DW_MCI_SEND_STATUS 1
  48. #define DW_MCI_RECV_STATUS 2
  49. #define DW_MCI_DMA_THRESHOLD 16
  50. #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
  51. #define DW_MCI_FREQ_MIN 100000 /* unit: HZ */
  52. #define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
  53. SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
  54. SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
  55. SDMMC_IDMAC_INT_TI)
  56. #define DESC_RING_BUF_SZ PAGE_SIZE
  57. struct idmac_desc_64addr {
  58. u32 des0; /* Control Descriptor */
  59. #define IDMAC_OWN_CLR64(x) \
  60. !((x) & cpu_to_le32(IDMAC_DES0_OWN))
  61. u32 des1; /* Reserved */
  62. u32 des2; /*Buffer sizes */
  63. #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
  64. ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
  65. ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
  66. u32 des3; /* Reserved */
  67. u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
  68. u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
  69. u32 des6; /* Lower 32-bits of Next Descriptor Address */
  70. u32 des7; /* Upper 32-bits of Next Descriptor Address */
  71. };
  72. struct idmac_desc {
  73. __le32 des0; /* Control Descriptor */
  74. #define IDMAC_DES0_DIC BIT(1)
  75. #define IDMAC_DES0_LD BIT(2)
  76. #define IDMAC_DES0_FD BIT(3)
  77. #define IDMAC_DES0_CH BIT(4)
  78. #define IDMAC_DES0_ER BIT(5)
  79. #define IDMAC_DES0_CES BIT(30)
  80. #define IDMAC_DES0_OWN BIT(31)
  81. __le32 des1; /* Buffer sizes */
  82. #define IDMAC_SET_BUFFER1_SIZE(d, s) \
  83. ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
  84. __le32 des2; /* buffer 1 physical address */
  85. __le32 des3; /* buffer 2 physical address */
  86. };
  87. /* Each descriptor can transfer up to 4KB of data in chained mode */
  88. #define DW_MCI_DESC_DATA_LENGTH 0x1000
  89. #if defined(CONFIG_DEBUG_FS)
  90. static int dw_mci_req_show(struct seq_file *s, void *v)
  91. {
  92. struct dw_mci_slot *slot = s->private;
  93. struct mmc_request *mrq;
  94. struct mmc_command *cmd;
  95. struct mmc_command *stop;
  96. struct mmc_data *data;
  97. /* Make sure we get a consistent snapshot */
  98. spin_lock_bh(&slot->host->lock);
  99. mrq = slot->mrq;
  100. if (mrq) {
  101. cmd = mrq->cmd;
  102. data = mrq->data;
  103. stop = mrq->stop;
  104. if (cmd)
  105. seq_printf(s,
  106. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  107. cmd->opcode, cmd->arg, cmd->flags,
  108. cmd->resp[0], cmd->resp[1], cmd->resp[2],
  109. cmd->resp[2], cmd->error);
  110. if (data)
  111. seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
  112. data->bytes_xfered, data->blocks,
  113. data->blksz, data->flags, data->error);
  114. if (stop)
  115. seq_printf(s,
  116. "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
  117. stop->opcode, stop->arg, stop->flags,
  118. stop->resp[0], stop->resp[1], stop->resp[2],
  119. stop->resp[2], stop->error);
  120. }
  121. spin_unlock_bh(&slot->host->lock);
  122. return 0;
  123. }
  124. DEFINE_SHOW_ATTRIBUTE(dw_mci_req);
  125. static int dw_mci_regs_show(struct seq_file *s, void *v)
  126. {
  127. struct dw_mci *host = s->private;
  128. pm_runtime_get_sync(host->dev);
  129. seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
  130. seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
  131. seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
  132. seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
  133. seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
  134. seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
  135. pm_runtime_put_autosuspend(host->dev);
  136. return 0;
  137. }
  138. DEFINE_SHOW_ATTRIBUTE(dw_mci_regs);
  139. static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
  140. {
  141. struct mmc_host *mmc = slot->mmc;
  142. struct dw_mci *host = slot->host;
  143. struct dentry *root;
  144. root = mmc->debugfs_root;
  145. if (!root)
  146. return;
  147. debugfs_create_file("regs", 0400, root, host, &dw_mci_regs_fops);
  148. debugfs_create_file("req", 0400, root, slot, &dw_mci_req_fops);
  149. debugfs_create_u32("state", 0400, root, &host->state);
  150. debugfs_create_xul("pending_events", 0400, root,
  151. &host->pending_events);
  152. debugfs_create_xul("completed_events", 0400, root,
  153. &host->completed_events);
  154. #ifdef CONFIG_FAULT_INJECTION
  155. fault_create_debugfs_attr("fail_data_crc", root, &host->fail_data_crc);
  156. #endif
  157. }
  158. #endif /* defined(CONFIG_DEBUG_FS) */
  159. static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
  160. {
  161. u32 ctrl;
  162. ctrl = mci_readl(host, CTRL);
  163. ctrl |= reset;
  164. mci_writel(host, CTRL, ctrl);
  165. /* wait till resets clear */
  166. if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl,
  167. !(ctrl & reset),
  168. 1, 500 * USEC_PER_MSEC)) {
  169. dev_err(host->dev,
  170. "Timeout resetting block (ctrl reset %#x)\n",
  171. ctrl & reset);
  172. return false;
  173. }
  174. return true;
  175. }
  176. static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
  177. {
  178. u32 status;
  179. /*
  180. * Databook says that before issuing a new data transfer command
  181. * we need to check to see if the card is busy. Data transfer commands
  182. * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
  183. *
  184. * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
  185. * expected.
  186. */
  187. if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
  188. !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
  189. if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
  190. status,
  191. !(status & SDMMC_STATUS_BUSY),
  192. 10, 500 * USEC_PER_MSEC))
  193. dev_err(host->dev, "Busy; trying anyway\n");
  194. }
  195. }
  196. static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
  197. {
  198. struct dw_mci *host = slot->host;
  199. unsigned int cmd_status = 0;
  200. mci_writel(host, CMDARG, arg);
  201. wmb(); /* drain writebuffer */
  202. dw_mci_wait_while_busy(host, cmd);
  203. mci_writel(host, CMD, SDMMC_CMD_START | cmd);
  204. if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status,
  205. !(cmd_status & SDMMC_CMD_START),
  206. 1, 500 * USEC_PER_MSEC))
  207. dev_err(&slot->mmc->class_dev,
  208. "Timeout sending command (cmd %#x arg %#x status %#x)\n",
  209. cmd, arg, cmd_status);
  210. }
  211. static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
  212. {
  213. struct dw_mci_slot *slot = mmc_priv(mmc);
  214. struct dw_mci *host = slot->host;
  215. u32 cmdr;
  216. cmd->error = -EINPROGRESS;
  217. cmdr = cmd->opcode;
  218. if (cmd->opcode == MMC_STOP_TRANSMISSION ||
  219. cmd->opcode == MMC_GO_IDLE_STATE ||
  220. cmd->opcode == MMC_GO_INACTIVE_STATE ||
  221. (cmd->opcode == SD_IO_RW_DIRECT &&
  222. ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
  223. cmdr |= SDMMC_CMD_STOP;
  224. else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
  225. cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
  226. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  227. u32 clk_en_a;
  228. /* Special bit makes CMD11 not die */
  229. cmdr |= SDMMC_CMD_VOLT_SWITCH;
  230. /* Change state to continue to handle CMD11 weirdness */
  231. WARN_ON(slot->host->state != STATE_SENDING_CMD);
  232. slot->host->state = STATE_SENDING_CMD11;
  233. /*
  234. * We need to disable low power mode (automatic clock stop)
  235. * while doing voltage switch so we don't confuse the card,
  236. * since stopping the clock is a specific part of the UHS
  237. * voltage change dance.
  238. *
  239. * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
  240. * unconditionally turned back on in dw_mci_setup_bus() if it's
  241. * ever called with a non-zero clock. That shouldn't happen
  242. * until the voltage change is all done.
  243. */
  244. clk_en_a = mci_readl(host, CLKENA);
  245. clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
  246. mci_writel(host, CLKENA, clk_en_a);
  247. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
  248. SDMMC_CMD_PRV_DAT_WAIT, 0);
  249. }
  250. if (cmd->flags & MMC_RSP_PRESENT) {
  251. /* We expect a response, so set this bit */
  252. cmdr |= SDMMC_CMD_RESP_EXP;
  253. if (cmd->flags & MMC_RSP_136)
  254. cmdr |= SDMMC_CMD_RESP_LONG;
  255. }
  256. if (cmd->flags & MMC_RSP_CRC)
  257. cmdr |= SDMMC_CMD_RESP_CRC;
  258. if (cmd->data) {
  259. cmdr |= SDMMC_CMD_DAT_EXP;
  260. if (cmd->data->flags & MMC_DATA_WRITE)
  261. cmdr |= SDMMC_CMD_DAT_WR;
  262. }
  263. if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
  264. cmdr |= SDMMC_CMD_USE_HOLD_REG;
  265. return cmdr;
  266. }
  267. static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
  268. {
  269. struct mmc_command *stop;
  270. u32 cmdr;
  271. if (!cmd->data)
  272. return 0;
  273. stop = &host->stop_abort;
  274. cmdr = cmd->opcode;
  275. memset(stop, 0, sizeof(struct mmc_command));
  276. if (cmdr == MMC_READ_SINGLE_BLOCK ||
  277. cmdr == MMC_READ_MULTIPLE_BLOCK ||
  278. cmdr == MMC_WRITE_BLOCK ||
  279. cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
  280. mmc_op_tuning(cmdr) ||
  281. cmdr == MMC_GEN_CMD) {
  282. stop->opcode = MMC_STOP_TRANSMISSION;
  283. stop->arg = 0;
  284. stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
  285. } else if (cmdr == SD_IO_RW_EXTENDED) {
  286. stop->opcode = SD_IO_RW_DIRECT;
  287. stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
  288. ((cmd->arg >> 28) & 0x7);
  289. stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
  290. } else {
  291. return 0;
  292. }
  293. cmdr = stop->opcode | SDMMC_CMD_STOP |
  294. SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
  295. if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags))
  296. cmdr |= SDMMC_CMD_USE_HOLD_REG;
  297. return cmdr;
  298. }
  299. static inline void dw_mci_set_cto(struct dw_mci *host)
  300. {
  301. unsigned int cto_clks;
  302. unsigned int cto_div;
  303. unsigned int cto_ms;
  304. unsigned long irqflags;
  305. cto_clks = mci_readl(host, TMOUT) & 0xff;
  306. cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
  307. if (cto_div == 0)
  308. cto_div = 1;
  309. cto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * cto_clks * cto_div,
  310. host->bus_hz);
  311. /* add a bit spare time */
  312. cto_ms += 10;
  313. /*
  314. * The durations we're working with are fairly short so we have to be
  315. * extra careful about synchronization here. Specifically in hardware a
  316. * command timeout is _at most_ 5.1 ms, so that means we expect an
  317. * interrupt (either command done or timeout) to come rather quickly
  318. * after the mci_writel. ...but just in case we have a long interrupt
  319. * latency let's add a bit of paranoia.
  320. *
  321. * In general we'll assume that at least an interrupt will be asserted
  322. * in hardware by the time the cto_timer runs. ...and if it hasn't
  323. * been asserted in hardware by that time then we'll assume it'll never
  324. * come.
  325. */
  326. spin_lock_irqsave(&host->irq_lock, irqflags);
  327. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  328. mod_timer(&host->cto_timer,
  329. jiffies + msecs_to_jiffies(cto_ms) + 1);
  330. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  331. }
  332. static void dw_mci_start_command(struct dw_mci *host,
  333. struct mmc_command *cmd, u32 cmd_flags)
  334. {
  335. host->cmd = cmd;
  336. dev_vdbg(host->dev,
  337. "start command: ARGR=0x%08x CMDR=0x%08x\n",
  338. cmd->arg, cmd_flags);
  339. mci_writel(host, CMDARG, cmd->arg);
  340. wmb(); /* drain writebuffer */
  341. dw_mci_wait_while_busy(host, cmd_flags);
  342. mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
  343. /* response expected command only */
  344. if (cmd_flags & SDMMC_CMD_RESP_EXP)
  345. dw_mci_set_cto(host);
  346. }
  347. static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
  348. {
  349. struct mmc_command *stop = &host->stop_abort;
  350. dw_mci_start_command(host, stop, host->stop_cmdr);
  351. }
  352. /* DMA interface functions */
  353. static void dw_mci_stop_dma(struct dw_mci *host)
  354. {
  355. if (host->using_dma) {
  356. host->dma_ops->stop(host);
  357. host->dma_ops->cleanup(host);
  358. }
  359. /* Data transfer was stopped by the interrupt handler */
  360. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  361. }
  362. static void dw_mci_dma_cleanup(struct dw_mci *host)
  363. {
  364. struct mmc_data *data = host->data;
  365. if (data && data->host_cookie == COOKIE_MAPPED) {
  366. dma_unmap_sg(host->dev,
  367. data->sg,
  368. data->sg_len,
  369. mmc_get_dma_dir(data));
  370. data->host_cookie = COOKIE_UNMAPPED;
  371. }
  372. }
  373. static void dw_mci_idmac_reset(struct dw_mci *host)
  374. {
  375. u32 bmod = mci_readl(host, BMOD);
  376. /* Software reset of DMA */
  377. bmod |= SDMMC_IDMAC_SWRESET;
  378. mci_writel(host, BMOD, bmod);
  379. }
  380. static void dw_mci_idmac_stop_dma(struct dw_mci *host)
  381. {
  382. u32 temp;
  383. /* Disable and reset the IDMAC interface */
  384. temp = mci_readl(host, CTRL);
  385. temp &= ~SDMMC_CTRL_USE_IDMAC;
  386. temp |= SDMMC_CTRL_DMA_RESET;
  387. mci_writel(host, CTRL, temp);
  388. /* Stop the IDMAC running */
  389. temp = mci_readl(host, BMOD);
  390. temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
  391. temp |= SDMMC_IDMAC_SWRESET;
  392. mci_writel(host, BMOD, temp);
  393. }
  394. static void dw_mci_dmac_complete_dma(void *arg)
  395. {
  396. struct dw_mci *host = arg;
  397. struct mmc_data *data = host->data;
  398. dev_vdbg(host->dev, "DMA complete\n");
  399. if ((host->use_dma == TRANS_MODE_EDMAC) &&
  400. data && (data->flags & MMC_DATA_READ))
  401. /* Invalidate cache after read */
  402. dma_sync_sg_for_cpu(mmc_dev(host->slot->mmc),
  403. data->sg,
  404. data->sg_len,
  405. DMA_FROM_DEVICE);
  406. host->dma_ops->cleanup(host);
  407. /*
  408. * If the card was removed, data will be NULL. No point in trying to
  409. * send the stop command or waiting for NBUSY in this case.
  410. */
  411. if (data) {
  412. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  413. queue_work(system_bh_wq, &host->bh_work);
  414. }
  415. }
  416. static int dw_mci_idmac_init(struct dw_mci *host)
  417. {
  418. int i;
  419. if (host->dma_64bit_address == 1) {
  420. struct idmac_desc_64addr *p;
  421. /* Number of descriptors in the ring buffer */
  422. host->ring_size =
  423. DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
  424. /* Forward link the descriptor list */
  425. for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
  426. i++, p++) {
  427. p->des6 = (host->sg_dma +
  428. (sizeof(struct idmac_desc_64addr) *
  429. (i + 1))) & 0xffffffff;
  430. p->des7 = (u64)(host->sg_dma +
  431. (sizeof(struct idmac_desc_64addr) *
  432. (i + 1))) >> 32;
  433. /* Initialize reserved and buffer size fields to "0" */
  434. p->des0 = 0;
  435. p->des1 = 0;
  436. p->des2 = 0;
  437. p->des3 = 0;
  438. }
  439. /* Set the last descriptor as the end-of-ring descriptor */
  440. p->des6 = host->sg_dma & 0xffffffff;
  441. p->des7 = (u64)host->sg_dma >> 32;
  442. p->des0 = IDMAC_DES0_ER;
  443. } else {
  444. struct idmac_desc *p;
  445. /* Number of descriptors in the ring buffer */
  446. host->ring_size =
  447. DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
  448. /* Forward link the descriptor list */
  449. for (i = 0, p = host->sg_cpu;
  450. i < host->ring_size - 1;
  451. i++, p++) {
  452. p->des3 = cpu_to_le32(host->sg_dma +
  453. (sizeof(struct idmac_desc) * (i + 1)));
  454. p->des0 = 0;
  455. p->des1 = 0;
  456. }
  457. /* Set the last descriptor as the end-of-ring descriptor */
  458. p->des3 = cpu_to_le32(host->sg_dma);
  459. p->des0 = cpu_to_le32(IDMAC_DES0_ER);
  460. }
  461. dw_mci_idmac_reset(host);
  462. if (host->dma_64bit_address == 1) {
  463. /* Mask out interrupts - get Tx & Rx complete only */
  464. mci_writel(host, IDSTS64, IDMAC_INT_CLR);
  465. mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
  466. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  467. /* Set the descriptor base address */
  468. mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
  469. mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
  470. } else {
  471. /* Mask out interrupts - get Tx & Rx complete only */
  472. mci_writel(host, IDSTS, IDMAC_INT_CLR);
  473. mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
  474. SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
  475. /* Set the descriptor base address */
  476. mci_writel(host, DBADDR, host->sg_dma);
  477. }
  478. return 0;
  479. }
  480. static inline int dw_mci_prepare_desc64(struct dw_mci *host,
  481. struct mmc_data *data,
  482. unsigned int sg_len)
  483. {
  484. unsigned int desc_len;
  485. struct idmac_desc_64addr *desc_first, *desc_last, *desc;
  486. u32 val;
  487. int i;
  488. desc_first = desc_last = desc = host->sg_cpu;
  489. for (i = 0; i < sg_len; i++) {
  490. unsigned int length = sg_dma_len(&data->sg[i]);
  491. u64 mem_addr = sg_dma_address(&data->sg[i]);
  492. for ( ; length ; desc++) {
  493. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  494. length : DW_MCI_DESC_DATA_LENGTH;
  495. length -= desc_len;
  496. /*
  497. * Wait for the former clear OWN bit operation
  498. * of IDMAC to make sure that this descriptor
  499. * isn't still owned by IDMAC as IDMAC's write
  500. * ops and CPU's read ops are asynchronous.
  501. */
  502. if (readl_poll_timeout_atomic(&desc->des0, val,
  503. !(val & IDMAC_DES0_OWN),
  504. 10, 100 * USEC_PER_MSEC))
  505. goto err_own_bit;
  506. /*
  507. * Set the OWN bit and disable interrupts
  508. * for this descriptor
  509. */
  510. desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
  511. IDMAC_DES0_CH;
  512. /* Buffer length */
  513. IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
  514. /* Physical address to DMA to/from */
  515. desc->des4 = mem_addr & 0xffffffff;
  516. desc->des5 = mem_addr >> 32;
  517. /* Update physical address for the next desc */
  518. mem_addr += desc_len;
  519. /* Save pointer to the last descriptor */
  520. desc_last = desc;
  521. }
  522. }
  523. /* Set first descriptor */
  524. desc_first->des0 |= IDMAC_DES0_FD;
  525. /* Set last descriptor */
  526. desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
  527. desc_last->des0 |= IDMAC_DES0_LD;
  528. return 0;
  529. err_own_bit:
  530. /* restore the descriptor chain as it's polluted */
  531. dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
  532. memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
  533. dw_mci_idmac_init(host);
  534. return -EINVAL;
  535. }
  536. static inline int dw_mci_prepare_desc32(struct dw_mci *host,
  537. struct mmc_data *data,
  538. unsigned int sg_len)
  539. {
  540. unsigned int desc_len;
  541. struct idmac_desc *desc_first, *desc_last, *desc;
  542. u32 val;
  543. int i;
  544. desc_first = desc_last = desc = host->sg_cpu;
  545. for (i = 0; i < sg_len; i++) {
  546. unsigned int length = sg_dma_len(&data->sg[i]);
  547. u32 mem_addr = sg_dma_address(&data->sg[i]);
  548. for ( ; length ; desc++) {
  549. desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
  550. length : DW_MCI_DESC_DATA_LENGTH;
  551. length -= desc_len;
  552. /*
  553. * Wait for the former clear OWN bit operation
  554. * of IDMAC to make sure that this descriptor
  555. * isn't still owned by IDMAC as IDMAC's write
  556. * ops and CPU's read ops are asynchronous.
  557. */
  558. if (readl_poll_timeout_atomic(&desc->des0, val,
  559. IDMAC_OWN_CLR64(val),
  560. 10,
  561. 100 * USEC_PER_MSEC))
  562. goto err_own_bit;
  563. /*
  564. * Set the OWN bit and disable interrupts
  565. * for this descriptor
  566. */
  567. desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
  568. IDMAC_DES0_DIC |
  569. IDMAC_DES0_CH);
  570. /* Buffer length */
  571. IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
  572. /* Physical address to DMA to/from */
  573. desc->des2 = cpu_to_le32(mem_addr);
  574. /* Update physical address for the next desc */
  575. mem_addr += desc_len;
  576. /* Save pointer to the last descriptor */
  577. desc_last = desc;
  578. }
  579. }
  580. /* Set first descriptor */
  581. desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
  582. /* Set last descriptor */
  583. desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
  584. IDMAC_DES0_DIC));
  585. desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
  586. return 0;
  587. err_own_bit:
  588. /* restore the descriptor chain as it's polluted */
  589. dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
  590. memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
  591. dw_mci_idmac_init(host);
  592. return -EINVAL;
  593. }
  594. static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
  595. {
  596. u32 temp;
  597. int ret;
  598. if (host->dma_64bit_address == 1)
  599. ret = dw_mci_prepare_desc64(host, host->data, sg_len);
  600. else
  601. ret = dw_mci_prepare_desc32(host, host->data, sg_len);
  602. if (ret)
  603. goto out;
  604. /* drain writebuffer */
  605. wmb();
  606. /* Make sure to reset DMA in case we did PIO before this */
  607. dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
  608. dw_mci_idmac_reset(host);
  609. /* Select IDMAC interface */
  610. temp = mci_readl(host, CTRL);
  611. temp |= SDMMC_CTRL_USE_IDMAC;
  612. mci_writel(host, CTRL, temp);
  613. /* drain writebuffer */
  614. wmb();
  615. /* Enable the IDMAC */
  616. temp = mci_readl(host, BMOD);
  617. temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
  618. mci_writel(host, BMOD, temp);
  619. /* Start it running */
  620. mci_writel(host, PLDMND, 1);
  621. out:
  622. return ret;
  623. }
  624. static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
  625. .init = dw_mci_idmac_init,
  626. .start = dw_mci_idmac_start_dma,
  627. .stop = dw_mci_idmac_stop_dma,
  628. .complete = dw_mci_dmac_complete_dma,
  629. .cleanup = dw_mci_dma_cleanup,
  630. };
  631. static void dw_mci_edmac_stop_dma(struct dw_mci *host)
  632. {
  633. dmaengine_terminate_async(host->dms->ch);
  634. }
  635. static int dw_mci_edmac_start_dma(struct dw_mci *host,
  636. unsigned int sg_len)
  637. {
  638. struct dma_slave_config cfg;
  639. struct dma_async_tx_descriptor *desc = NULL;
  640. struct scatterlist *sgl = host->data->sg;
  641. static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  642. u32 sg_elems = host->data->sg_len;
  643. u32 fifoth_val;
  644. u32 fifo_offset = host->fifo_reg - host->regs;
  645. int ret = 0;
  646. /* Set external dma config: burst size, burst width */
  647. memset(&cfg, 0, sizeof(cfg));
  648. cfg.dst_addr = host->phy_regs + fifo_offset;
  649. cfg.src_addr = cfg.dst_addr;
  650. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  651. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  652. /* Match burst msize with external dma config */
  653. fifoth_val = mci_readl(host, FIFOTH);
  654. cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
  655. cfg.src_maxburst = cfg.dst_maxburst;
  656. if (host->data->flags & MMC_DATA_WRITE)
  657. cfg.direction = DMA_MEM_TO_DEV;
  658. else
  659. cfg.direction = DMA_DEV_TO_MEM;
  660. ret = dmaengine_slave_config(host->dms->ch, &cfg);
  661. if (ret) {
  662. dev_err(host->dev, "Failed to config edmac.\n");
  663. return -EBUSY;
  664. }
  665. desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
  666. sg_len, cfg.direction,
  667. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  668. if (!desc) {
  669. dev_err(host->dev, "Can't prepare slave sg.\n");
  670. return -EBUSY;
  671. }
  672. /* Set dw_mci_dmac_complete_dma as callback */
  673. desc->callback = dw_mci_dmac_complete_dma;
  674. desc->callback_param = (void *)host;
  675. dmaengine_submit(desc);
  676. /* Flush cache before write */
  677. if (host->data->flags & MMC_DATA_WRITE)
  678. dma_sync_sg_for_device(mmc_dev(host->slot->mmc), sgl,
  679. sg_elems, DMA_TO_DEVICE);
  680. dma_async_issue_pending(host->dms->ch);
  681. return 0;
  682. }
  683. static int dw_mci_edmac_init(struct dw_mci *host)
  684. {
  685. /* Request external dma channel */
  686. host->dms = kzalloc_obj(struct dw_mci_dma_slave);
  687. if (!host->dms)
  688. return -ENOMEM;
  689. host->dms->ch = dma_request_chan(host->dev, "rx-tx");
  690. if (IS_ERR(host->dms->ch)) {
  691. int ret = PTR_ERR(host->dms->ch);
  692. dev_err(host->dev, "Failed to get external DMA channel.\n");
  693. kfree(host->dms);
  694. host->dms = NULL;
  695. return ret;
  696. }
  697. return 0;
  698. }
  699. static void dw_mci_edmac_exit(struct dw_mci *host)
  700. {
  701. if (host->dms) {
  702. if (host->dms->ch) {
  703. dma_release_channel(host->dms->ch);
  704. host->dms->ch = NULL;
  705. }
  706. kfree(host->dms);
  707. host->dms = NULL;
  708. }
  709. }
  710. static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
  711. .init = dw_mci_edmac_init,
  712. .exit = dw_mci_edmac_exit,
  713. .start = dw_mci_edmac_start_dma,
  714. .stop = dw_mci_edmac_stop_dma,
  715. .complete = dw_mci_dmac_complete_dma,
  716. .cleanup = dw_mci_dma_cleanup,
  717. };
  718. static int dw_mci_pre_dma_transfer(struct dw_mci *host,
  719. struct mmc_data *data,
  720. int cookie)
  721. {
  722. struct scatterlist *sg;
  723. unsigned int i, sg_len;
  724. if (data->host_cookie == COOKIE_PRE_MAPPED)
  725. return data->sg_len;
  726. /*
  727. * We don't do DMA on "complex" transfers, i.e. with
  728. * non-word-aligned buffers or lengths. Also, we don't bother
  729. * with all the DMA setup overhead for short transfers.
  730. */
  731. if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
  732. return -EINVAL;
  733. if (data->blksz & 3)
  734. return -EINVAL;
  735. for_each_sg(data->sg, sg, data->sg_len, i) {
  736. if (sg->offset & 3 || sg->length & 3)
  737. return -EINVAL;
  738. }
  739. sg_len = dma_map_sg(host->dev,
  740. data->sg,
  741. data->sg_len,
  742. mmc_get_dma_dir(data));
  743. if (sg_len == 0)
  744. return -EINVAL;
  745. data->host_cookie = cookie;
  746. return sg_len;
  747. }
  748. static void dw_mci_pre_req(struct mmc_host *mmc,
  749. struct mmc_request *mrq)
  750. {
  751. struct dw_mci_slot *slot = mmc_priv(mmc);
  752. struct mmc_data *data = mrq->data;
  753. if (!slot->host->use_dma || !data)
  754. return;
  755. /* This data might be unmapped at this time */
  756. data->host_cookie = COOKIE_UNMAPPED;
  757. if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
  758. COOKIE_PRE_MAPPED) < 0)
  759. data->host_cookie = COOKIE_UNMAPPED;
  760. }
  761. static void dw_mci_post_req(struct mmc_host *mmc,
  762. struct mmc_request *mrq,
  763. int err)
  764. {
  765. struct dw_mci_slot *slot = mmc_priv(mmc);
  766. struct mmc_data *data = mrq->data;
  767. if (!slot->host->use_dma || !data)
  768. return;
  769. if (data->host_cookie != COOKIE_UNMAPPED)
  770. dma_unmap_sg(slot->host->dev,
  771. data->sg,
  772. data->sg_len,
  773. mmc_get_dma_dir(data));
  774. data->host_cookie = COOKIE_UNMAPPED;
  775. }
  776. static int dw_mci_get_cd(struct mmc_host *mmc)
  777. {
  778. int present;
  779. struct dw_mci_slot *slot = mmc_priv(mmc);
  780. struct dw_mci *host = slot->host;
  781. int gpio_cd = mmc_gpio_get_cd(mmc);
  782. /* Use platform get_cd function, else try onboard card detect */
  783. if (((mmc->caps & MMC_CAP_NEEDS_POLL)
  784. || !mmc_card_is_removable(mmc))) {
  785. present = 1;
  786. if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
  787. if (mmc->caps & MMC_CAP_NEEDS_POLL) {
  788. dev_info(&mmc->class_dev,
  789. "card is polling.\n");
  790. } else {
  791. dev_info(&mmc->class_dev,
  792. "card is non-removable.\n");
  793. }
  794. set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
  795. }
  796. return present;
  797. } else if (gpio_cd >= 0)
  798. present = gpio_cd;
  799. else
  800. present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
  801. == 0 ? 1 : 0;
  802. spin_lock_bh(&host->lock);
  803. if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
  804. dev_dbg(&mmc->class_dev, "card is present\n");
  805. else if (!present &&
  806. !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
  807. dev_dbg(&mmc->class_dev, "card is not present\n");
  808. spin_unlock_bh(&host->lock);
  809. return present;
  810. }
  811. static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
  812. {
  813. unsigned int blksz = data->blksz;
  814. static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
  815. u32 fifo_width = 1 << host->data_shift;
  816. u32 blksz_depth = blksz / fifo_width, fifoth_val;
  817. u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
  818. int idx = ARRAY_SIZE(mszs) - 1;
  819. /* pio should ship this scenario */
  820. if (!host->use_dma)
  821. return;
  822. tx_wmark = (host->fifo_depth) / 2;
  823. tx_wmark_invers = host->fifo_depth - tx_wmark;
  824. /*
  825. * MSIZE is '1',
  826. * if blksz is not a multiple of the FIFO width
  827. */
  828. if (blksz % fifo_width)
  829. goto done;
  830. do {
  831. if (!((blksz_depth % mszs[idx]) ||
  832. (tx_wmark_invers % mszs[idx]))) {
  833. msize = idx;
  834. rx_wmark = mszs[idx] - 1;
  835. break;
  836. }
  837. } while (--idx > 0);
  838. /*
  839. * If idx is '0', it won't be tried
  840. * Thus, initial values are uesed
  841. */
  842. done:
  843. fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
  844. mci_writel(host, FIFOTH, fifoth_val);
  845. }
  846. static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
  847. {
  848. unsigned int blksz = data->blksz;
  849. u32 blksz_depth, fifo_depth;
  850. u16 thld_size;
  851. u8 enable;
  852. /*
  853. * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
  854. * in the FIFO region, so we really shouldn't access it).
  855. */
  856. if (host->verid < DW_MMC_240A ||
  857. (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
  858. return;
  859. /*
  860. * Card write Threshold is introduced since 2.80a
  861. * It's used when HS400 mode is enabled.
  862. */
  863. if (data->flags & MMC_DATA_WRITE &&
  864. host->timing != MMC_TIMING_MMC_HS400)
  865. goto disable;
  866. if (data->flags & MMC_DATA_WRITE)
  867. enable = SDMMC_CARD_WR_THR_EN;
  868. else
  869. enable = SDMMC_CARD_RD_THR_EN;
  870. if (host->timing != MMC_TIMING_MMC_HS200 &&
  871. host->timing != MMC_TIMING_UHS_SDR104 &&
  872. host->timing != MMC_TIMING_MMC_HS400)
  873. goto disable;
  874. blksz_depth = blksz / (1 << host->data_shift);
  875. fifo_depth = host->fifo_depth;
  876. if (blksz_depth > fifo_depth)
  877. goto disable;
  878. /*
  879. * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
  880. * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
  881. * Currently just choose blksz.
  882. */
  883. thld_size = blksz;
  884. mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
  885. return;
  886. disable:
  887. mci_writel(host, CDTHRCTL, 0);
  888. }
  889. static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
  890. {
  891. unsigned long irqflags;
  892. int sg_len;
  893. u32 temp;
  894. host->using_dma = 0;
  895. /* If we don't have a channel, we can't do DMA */
  896. if (!host->use_dma)
  897. return -ENODEV;
  898. sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
  899. if (sg_len < 0) {
  900. host->dma_ops->stop(host);
  901. return sg_len;
  902. }
  903. host->using_dma = 1;
  904. if (host->use_dma == TRANS_MODE_IDMAC)
  905. dev_vdbg(host->dev,
  906. "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
  907. (unsigned long)host->sg_cpu,
  908. (unsigned long)host->sg_dma,
  909. sg_len);
  910. /*
  911. * Decide the MSIZE and RX/TX Watermark.
  912. * If current block size is same with previous size,
  913. * no need to update fifoth.
  914. */
  915. if (host->prev_blksz != data->blksz)
  916. dw_mci_adjust_fifoth(host, data);
  917. /* Enable the DMA interface */
  918. temp = mci_readl(host, CTRL);
  919. temp |= SDMMC_CTRL_DMA_ENABLE;
  920. mci_writel(host, CTRL, temp);
  921. /* Disable RX/TX IRQs, let DMA handle it */
  922. spin_lock_irqsave(&host->irq_lock, irqflags);
  923. temp = mci_readl(host, INTMASK);
  924. temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
  925. mci_writel(host, INTMASK, temp);
  926. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  927. if (host->dma_ops->start(host, sg_len)) {
  928. host->dma_ops->stop(host);
  929. /* We can't do DMA, try PIO for this one */
  930. dev_dbg(host->dev,
  931. "%s: fall back to PIO mode for current transfer\n",
  932. __func__);
  933. return -ENODEV;
  934. }
  935. return 0;
  936. }
  937. static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
  938. {
  939. unsigned long irqflags;
  940. int flags = SG_MITER_ATOMIC;
  941. u32 temp;
  942. data->error = -EINPROGRESS;
  943. WARN_ON(host->data);
  944. host->sg = NULL;
  945. host->data = data;
  946. if (data->flags & MMC_DATA_READ)
  947. host->dir_status = DW_MCI_RECV_STATUS;
  948. else
  949. host->dir_status = DW_MCI_SEND_STATUS;
  950. dw_mci_ctrl_thld(host, data);
  951. if (dw_mci_submit_data_dma(host, data)) {
  952. if (host->data->flags & MMC_DATA_READ)
  953. flags |= SG_MITER_TO_SG;
  954. else
  955. flags |= SG_MITER_FROM_SG;
  956. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  957. host->sg = data->sg;
  958. host->part_buf_start = 0;
  959. host->part_buf_count = 0;
  960. mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
  961. spin_lock_irqsave(&host->irq_lock, irqflags);
  962. temp = mci_readl(host, INTMASK);
  963. temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
  964. mci_writel(host, INTMASK, temp);
  965. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  966. temp = mci_readl(host, CTRL);
  967. temp &= ~SDMMC_CTRL_DMA_ENABLE;
  968. mci_writel(host, CTRL, temp);
  969. /*
  970. * Use the initial fifoth_val for PIO mode. If wm_algined
  971. * is set, we set watermark same as data size.
  972. * If next issued data may be transferred by DMA mode,
  973. * prev_blksz should be invalidated.
  974. */
  975. if (host->wm_aligned)
  976. dw_mci_adjust_fifoth(host, data);
  977. else
  978. mci_writel(host, FIFOTH, host->fifoth_val);
  979. host->prev_blksz = 0;
  980. } else {
  981. /*
  982. * Keep the current block size.
  983. * It will be used to decide whether to update
  984. * fifoth register next time.
  985. */
  986. host->prev_blksz = data->blksz;
  987. }
  988. }
  989. static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
  990. {
  991. struct dw_mci *host = slot->host;
  992. unsigned int clock = slot->clock;
  993. u32 div;
  994. u32 clk_en_a;
  995. u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
  996. /* We must continue to set bit 28 in CMD until the change is complete */
  997. if (host->state == STATE_WAITING_CMD11_DONE)
  998. sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
  999. slot->mmc->actual_clock = 0;
  1000. if (!clock) {
  1001. mci_writel(host, CLKENA, 0);
  1002. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1003. } else if (clock != host->current_speed || force_clkinit) {
  1004. div = host->bus_hz / clock;
  1005. if (host->bus_hz % clock && host->bus_hz > clock)
  1006. /*
  1007. * move the + 1 after the divide to prevent
  1008. * over-clocking the card.
  1009. */
  1010. div += 1;
  1011. div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
  1012. if ((clock != slot->__clk_old &&
  1013. !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
  1014. force_clkinit) {
  1015. /* Silent the verbose log if calling from PM context */
  1016. if (!force_clkinit)
  1017. dev_info(&slot->mmc->class_dev,
  1018. "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
  1019. slot->id, host->bus_hz, clock,
  1020. div ? ((host->bus_hz / div) >> 1) :
  1021. host->bus_hz, div);
  1022. /*
  1023. * If card is polling, display the message only
  1024. * one time at boot time.
  1025. */
  1026. if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
  1027. slot->mmc->f_min == clock)
  1028. set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
  1029. }
  1030. /* disable clock */
  1031. mci_writel(host, CLKENA, 0);
  1032. mci_writel(host, CLKSRC, 0);
  1033. /* inform CIU */
  1034. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1035. /* set clock to desired speed */
  1036. mci_writel(host, CLKDIV, div);
  1037. /* inform CIU */
  1038. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1039. /* enable clock; only low power if no SDIO */
  1040. clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
  1041. if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
  1042. clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
  1043. mci_writel(host, CLKENA, clk_en_a);
  1044. /* inform CIU */
  1045. mci_send_cmd(slot, sdmmc_cmd_bits, 0);
  1046. /* keep the last clock value that was requested from core */
  1047. slot->__clk_old = clock;
  1048. slot->mmc->actual_clock = div ? ((host->bus_hz / div) >> 1) :
  1049. host->bus_hz;
  1050. }
  1051. host->current_speed = clock;
  1052. /* Set the current slot bus width */
  1053. mci_writel(host, CTYPE, (slot->ctype << slot->id));
  1054. }
  1055. static void dw_mci_set_data_timeout(struct dw_mci *host,
  1056. unsigned int timeout_ns)
  1057. {
  1058. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1059. u32 clk_div, tmout;
  1060. u64 tmp;
  1061. if (drv_data && drv_data->set_data_timeout)
  1062. return drv_data->set_data_timeout(host, timeout_ns);
  1063. clk_div = (mci_readl(host, CLKDIV) & 0xFF) * 2;
  1064. if (clk_div == 0)
  1065. clk_div = 1;
  1066. tmp = DIV_ROUND_UP_ULL((u64)timeout_ns * host->bus_hz, NSEC_PER_SEC);
  1067. tmp = DIV_ROUND_UP_ULL(tmp, clk_div);
  1068. /* TMOUT[7:0] (RESPONSE_TIMEOUT) */
  1069. tmout = 0xFF; /* Set maximum */
  1070. /* TMOUT[31:8] (DATA_TIMEOUT) */
  1071. if (!tmp || tmp > 0xFFFFFF)
  1072. tmout |= (0xFFFFFF << 8);
  1073. else
  1074. tmout |= (tmp & 0xFFFFFF) << 8;
  1075. mci_writel(host, TMOUT, tmout);
  1076. dev_dbg(host->dev, "timeout_ns: %u => TMOUT[31:8]: %#08x",
  1077. timeout_ns, tmout >> 8);
  1078. }
  1079. static void __dw_mci_start_request(struct dw_mci *host,
  1080. struct dw_mci_slot *slot,
  1081. struct mmc_command *cmd)
  1082. {
  1083. struct mmc_request *mrq;
  1084. struct mmc_data *data;
  1085. u32 cmdflags;
  1086. mrq = slot->mrq;
  1087. host->mrq = mrq;
  1088. host->pending_events = 0;
  1089. host->completed_events = 0;
  1090. host->cmd_status = 0;
  1091. host->data_status = 0;
  1092. host->dir_status = 0;
  1093. data = cmd->data;
  1094. if (data) {
  1095. dw_mci_set_data_timeout(host, data->timeout_ns);
  1096. mci_writel(host, BYTCNT, data->blksz*data->blocks);
  1097. mci_writel(host, BLKSIZ, data->blksz);
  1098. }
  1099. cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
  1100. /* this is the first command, send the initialization clock */
  1101. if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
  1102. cmdflags |= SDMMC_CMD_INIT;
  1103. if (data) {
  1104. dw_mci_submit_data(host, data);
  1105. wmb(); /* drain writebuffer */
  1106. }
  1107. dw_mci_start_command(host, cmd, cmdflags);
  1108. if (cmd->opcode == SD_SWITCH_VOLTAGE) {
  1109. unsigned long irqflags;
  1110. /*
  1111. * Databook says to fail after 2ms w/ no response, but evidence
  1112. * shows that sometimes the cmd11 interrupt takes over 130ms.
  1113. * We'll set to 500ms, plus an extra jiffy just in case jiffies
  1114. * is just about to roll over.
  1115. *
  1116. * We do this whole thing under spinlock and only if the
  1117. * command hasn't already completed (indicating the irq
  1118. * already ran so we don't want the timeout).
  1119. */
  1120. spin_lock_irqsave(&host->irq_lock, irqflags);
  1121. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  1122. mod_timer(&host->cmd11_timer,
  1123. jiffies + msecs_to_jiffies(500) + 1);
  1124. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1125. }
  1126. host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
  1127. }
  1128. static void dw_mci_start_request(struct dw_mci *host,
  1129. struct dw_mci_slot *slot)
  1130. {
  1131. struct mmc_request *mrq = slot->mrq;
  1132. struct mmc_command *cmd;
  1133. cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
  1134. __dw_mci_start_request(host, slot, cmd);
  1135. }
  1136. /* must be called with host->lock held */
  1137. static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
  1138. struct mmc_request *mrq)
  1139. {
  1140. dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
  1141. host->state);
  1142. slot->mrq = mrq;
  1143. if (host->state == STATE_WAITING_CMD11_DONE) {
  1144. dev_warn(&slot->mmc->class_dev,
  1145. "Voltage change didn't complete\n");
  1146. /*
  1147. * this case isn't expected to happen, so we can
  1148. * either crash here or just try to continue on
  1149. * in the closest possible state
  1150. */
  1151. host->state = STATE_IDLE;
  1152. }
  1153. if (host->state == STATE_IDLE) {
  1154. host->state = STATE_SENDING_CMD;
  1155. dw_mci_start_request(host, slot);
  1156. } else {
  1157. list_add_tail(&slot->queue_node, &host->queue);
  1158. }
  1159. }
  1160. static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1161. {
  1162. struct dw_mci_slot *slot = mmc_priv(mmc);
  1163. struct dw_mci *host = slot->host;
  1164. WARN_ON(slot->mrq);
  1165. /*
  1166. * The check for card presence and queueing of the request must be
  1167. * atomic, otherwise the card could be removed in between and the
  1168. * request wouldn't fail until another card was inserted.
  1169. */
  1170. if (!dw_mci_get_cd(mmc)) {
  1171. mrq->cmd->error = -ENOMEDIUM;
  1172. mmc_request_done(mmc, mrq);
  1173. return;
  1174. }
  1175. spin_lock_bh(&host->lock);
  1176. dw_mci_queue_request(host, slot, mrq);
  1177. spin_unlock_bh(&host->lock);
  1178. }
  1179. static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1180. {
  1181. struct dw_mci_slot *slot = mmc_priv(mmc);
  1182. const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
  1183. u32 regs;
  1184. int ret;
  1185. switch (ios->bus_width) {
  1186. case MMC_BUS_WIDTH_4:
  1187. slot->ctype = SDMMC_CTYPE_4BIT;
  1188. break;
  1189. case MMC_BUS_WIDTH_8:
  1190. slot->ctype = SDMMC_CTYPE_8BIT;
  1191. break;
  1192. default:
  1193. /* set default 1 bit mode */
  1194. slot->ctype = SDMMC_CTYPE_1BIT;
  1195. }
  1196. regs = mci_readl(slot->host, UHS_REG);
  1197. /* DDR mode set */
  1198. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  1199. ios->timing == MMC_TIMING_UHS_DDR50 ||
  1200. ios->timing == MMC_TIMING_MMC_HS400)
  1201. regs |= ((0x1 << slot->id) << 16);
  1202. else
  1203. regs &= ~((0x1 << slot->id) << 16);
  1204. mci_writel(slot->host, UHS_REG, regs);
  1205. slot->host->timing = ios->timing;
  1206. /*
  1207. * Use mirror of ios->clock to prevent race with mmc
  1208. * core ios update when finding the minimum.
  1209. */
  1210. slot->clock = ios->clock;
  1211. if (drv_data && drv_data->set_ios)
  1212. drv_data->set_ios(slot->host, ios);
  1213. switch (ios->power_mode) {
  1214. case MMC_POWER_UP:
  1215. if (!IS_ERR(mmc->supply.vmmc)) {
  1216. ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
  1217. ios->vdd);
  1218. if (ret) {
  1219. dev_err(slot->host->dev,
  1220. "failed to enable vmmc regulator\n");
  1221. /*return, if failed turn on vmmc*/
  1222. return;
  1223. }
  1224. }
  1225. set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
  1226. regs = mci_readl(slot->host, PWREN);
  1227. regs |= (1 << slot->id);
  1228. mci_writel(slot->host, PWREN, regs);
  1229. break;
  1230. case MMC_POWER_ON:
  1231. if (!slot->host->vqmmc_enabled) {
  1232. if (!IS_ERR(mmc->supply.vqmmc)) {
  1233. ret = regulator_enable(mmc->supply.vqmmc);
  1234. if (ret < 0)
  1235. dev_err(slot->host->dev,
  1236. "failed to enable vqmmc\n");
  1237. else
  1238. slot->host->vqmmc_enabled = true;
  1239. } else {
  1240. /* Keep track so we don't reset again */
  1241. slot->host->vqmmc_enabled = true;
  1242. }
  1243. /* Reset our state machine after powering on */
  1244. dw_mci_ctrl_reset(slot->host,
  1245. SDMMC_CTRL_ALL_RESET_FLAGS);
  1246. }
  1247. /* Adjust clock / bus width after power is up */
  1248. dw_mci_setup_bus(slot, false);
  1249. break;
  1250. case MMC_POWER_OFF:
  1251. /* Turn clock off before power goes down */
  1252. dw_mci_setup_bus(slot, false);
  1253. if (!IS_ERR(mmc->supply.vmmc))
  1254. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1255. if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
  1256. regulator_disable(mmc->supply.vqmmc);
  1257. slot->host->vqmmc_enabled = false;
  1258. regs = mci_readl(slot->host, PWREN);
  1259. regs &= ~(1 << slot->id);
  1260. mci_writel(slot->host, PWREN, regs);
  1261. break;
  1262. default:
  1263. break;
  1264. }
  1265. if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
  1266. slot->host->state = STATE_IDLE;
  1267. }
  1268. static int dw_mci_card_busy(struct mmc_host *mmc)
  1269. {
  1270. struct dw_mci_slot *slot = mmc_priv(mmc);
  1271. u32 status;
  1272. /*
  1273. * Check the busy bit which is low when DAT[3:0]
  1274. * (the data lines) are 0000
  1275. */
  1276. status = mci_readl(slot->host, STATUS);
  1277. return !!(status & SDMMC_STATUS_BUSY);
  1278. }
  1279. static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
  1280. {
  1281. struct dw_mci_slot *slot = mmc_priv(mmc);
  1282. struct dw_mci *host = slot->host;
  1283. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1284. u32 uhs;
  1285. u32 v18 = SDMMC_UHS_18V << slot->id;
  1286. int ret;
  1287. if (drv_data && drv_data->switch_voltage)
  1288. return drv_data->switch_voltage(mmc, ios);
  1289. /*
  1290. * Program the voltage. Note that some instances of dw_mmc may use
  1291. * the UHS_REG for this. For other instances (like exynos) the UHS_REG
  1292. * does no harm but you need to set the regulator directly. Try both.
  1293. */
  1294. uhs = mci_readl(host, UHS_REG);
  1295. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  1296. uhs &= ~v18;
  1297. else
  1298. uhs |= v18;
  1299. if (!IS_ERR(mmc->supply.vqmmc)) {
  1300. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1301. if (ret < 0) {
  1302. dev_dbg(&mmc->class_dev,
  1303. "Regulator set error %d - %s V\n",
  1304. ret, uhs & v18 ? "1.8" : "3.3");
  1305. return ret;
  1306. }
  1307. }
  1308. mci_writel(host, UHS_REG, uhs);
  1309. return 0;
  1310. }
  1311. static int dw_mci_get_ro(struct mmc_host *mmc)
  1312. {
  1313. int read_only;
  1314. struct dw_mci_slot *slot = mmc_priv(mmc);
  1315. int gpio_ro = mmc_gpio_get_ro(mmc);
  1316. /* Use platform get_ro function, else try on board write protect */
  1317. if (gpio_ro >= 0)
  1318. read_only = gpio_ro;
  1319. else
  1320. read_only =
  1321. mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
  1322. dev_dbg(&mmc->class_dev, "card is %s\n",
  1323. read_only ? "read-only" : "read-write");
  1324. return read_only;
  1325. }
  1326. static void dw_mci_hw_reset(struct mmc_host *mmc)
  1327. {
  1328. struct dw_mci_slot *slot = mmc_priv(mmc);
  1329. struct dw_mci *host = slot->host;
  1330. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1331. int reset;
  1332. if (host->use_dma == TRANS_MODE_IDMAC)
  1333. dw_mci_idmac_reset(host);
  1334. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
  1335. SDMMC_CTRL_FIFO_RESET))
  1336. return;
  1337. if (drv_data && drv_data->hw_reset) {
  1338. drv_data->hw_reset(host);
  1339. return;
  1340. }
  1341. /*
  1342. * According to eMMC spec, card reset procedure:
  1343. * tRstW >= 1us: RST_n pulse width
  1344. * tRSCA >= 200us: RST_n to Command time
  1345. * tRSTH >= 1us: RST_n high period
  1346. */
  1347. reset = mci_readl(host, RST_N);
  1348. reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
  1349. mci_writel(host, RST_N, reset);
  1350. usleep_range(1, 2);
  1351. reset |= SDMMC_RST_HWACTIVE << slot->id;
  1352. mci_writel(host, RST_N, reset);
  1353. usleep_range(200, 300);
  1354. }
  1355. static void dw_mci_prepare_sdio_irq(struct dw_mci_slot *slot, bool prepare)
  1356. {
  1357. struct dw_mci *host = slot->host;
  1358. const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
  1359. u32 clk_en_a_old;
  1360. u32 clk_en_a;
  1361. /*
  1362. * Low power mode will stop the card clock when idle. According to the
  1363. * description of the CLKENA register we should disable low power mode
  1364. * for SDIO cards if we need SDIO interrupts to work.
  1365. */
  1366. clk_en_a_old = mci_readl(host, CLKENA);
  1367. if (prepare) {
  1368. set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1369. clk_en_a = clk_en_a_old & ~clken_low_pwr;
  1370. } else {
  1371. clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
  1372. clk_en_a = clk_en_a_old | clken_low_pwr;
  1373. }
  1374. if (clk_en_a != clk_en_a_old) {
  1375. mci_writel(host, CLKENA, clk_en_a);
  1376. mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT,
  1377. 0);
  1378. }
  1379. }
  1380. static void __dw_mci_enable_sdio_irq(struct dw_mci_slot *slot, int enb)
  1381. {
  1382. struct dw_mci *host = slot->host;
  1383. unsigned long irqflags;
  1384. u32 int_mask;
  1385. spin_lock_irqsave(&host->irq_lock, irqflags);
  1386. /* Enable/disable Slot Specific SDIO interrupt */
  1387. int_mask = mci_readl(host, INTMASK);
  1388. if (enb)
  1389. int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
  1390. else
  1391. int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
  1392. mci_writel(host, INTMASK, int_mask);
  1393. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1394. }
  1395. static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
  1396. {
  1397. struct dw_mci_slot *slot = mmc_priv(mmc);
  1398. struct dw_mci *host = slot->host;
  1399. dw_mci_prepare_sdio_irq(slot, enb);
  1400. __dw_mci_enable_sdio_irq(slot, enb);
  1401. /* Avoid runtime suspending the device when SDIO IRQ is enabled */
  1402. if (enb)
  1403. pm_runtime_get_noresume(host->dev);
  1404. else
  1405. pm_runtime_put_noidle(host->dev);
  1406. }
  1407. static void dw_mci_ack_sdio_irq(struct mmc_host *mmc)
  1408. {
  1409. struct dw_mci_slot *slot = mmc_priv(mmc);
  1410. __dw_mci_enable_sdio_irq(slot, 1);
  1411. }
  1412. static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1413. {
  1414. struct dw_mci_slot *slot = mmc_priv(mmc);
  1415. struct dw_mci *host = slot->host;
  1416. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1417. int err = -EINVAL;
  1418. if (drv_data && drv_data->execute_tuning)
  1419. err = drv_data->execute_tuning(slot, opcode);
  1420. return err;
  1421. }
  1422. static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
  1423. struct mmc_ios *ios)
  1424. {
  1425. struct dw_mci_slot *slot = mmc_priv(mmc);
  1426. struct dw_mci *host = slot->host;
  1427. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1428. if (drv_data && drv_data->prepare_hs400_tuning)
  1429. return drv_data->prepare_hs400_tuning(host, ios);
  1430. return 0;
  1431. }
  1432. static bool dw_mci_reset(struct dw_mci *host)
  1433. {
  1434. u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
  1435. bool ret = false;
  1436. u32 status = 0;
  1437. /*
  1438. * Resetting generates a block interrupt, hence setting
  1439. * the scatter-gather pointer to NULL.
  1440. */
  1441. if (host->sg) {
  1442. sg_miter_stop(&host->sg_miter);
  1443. host->sg = NULL;
  1444. }
  1445. if (host->use_dma)
  1446. flags |= SDMMC_CTRL_DMA_RESET;
  1447. if (dw_mci_ctrl_reset(host, flags)) {
  1448. /*
  1449. * In all cases we clear the RAWINTS
  1450. * register to clear any interrupts.
  1451. */
  1452. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  1453. if (!host->use_dma) {
  1454. ret = true;
  1455. goto ciu_out;
  1456. }
  1457. /* Wait for dma_req to be cleared */
  1458. if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS,
  1459. status,
  1460. !(status & SDMMC_STATUS_DMA_REQ),
  1461. 1, 500 * USEC_PER_MSEC)) {
  1462. dev_err(host->dev,
  1463. "%s: Timeout waiting for dma_req to be cleared\n",
  1464. __func__);
  1465. goto ciu_out;
  1466. }
  1467. /* when using DMA next we reset the fifo again */
  1468. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
  1469. goto ciu_out;
  1470. } else {
  1471. /* if the controller reset bit did clear, then set clock regs */
  1472. if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
  1473. dev_err(host->dev,
  1474. "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
  1475. __func__);
  1476. goto ciu_out;
  1477. }
  1478. }
  1479. if (host->use_dma == TRANS_MODE_IDMAC)
  1480. /* It is also required that we reinit idmac */
  1481. dw_mci_idmac_init(host);
  1482. ret = true;
  1483. ciu_out:
  1484. /* After a CTRL reset we need to have CIU set clock registers */
  1485. mci_send_cmd(host->slot, SDMMC_CMD_UPD_CLK, 0);
  1486. return ret;
  1487. }
  1488. static const struct mmc_host_ops dw_mci_ops = {
  1489. .request = dw_mci_request,
  1490. .pre_req = dw_mci_pre_req,
  1491. .post_req = dw_mci_post_req,
  1492. .set_ios = dw_mci_set_ios,
  1493. .get_ro = dw_mci_get_ro,
  1494. .get_cd = dw_mci_get_cd,
  1495. .card_hw_reset = dw_mci_hw_reset,
  1496. .enable_sdio_irq = dw_mci_enable_sdio_irq,
  1497. .ack_sdio_irq = dw_mci_ack_sdio_irq,
  1498. .execute_tuning = dw_mci_execute_tuning,
  1499. .card_busy = dw_mci_card_busy,
  1500. .start_signal_voltage_switch = dw_mci_switch_voltage,
  1501. .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
  1502. };
  1503. #ifdef CONFIG_FAULT_INJECTION
  1504. static enum hrtimer_restart dw_mci_fault_timer(struct hrtimer *t)
  1505. {
  1506. struct dw_mci *host = container_of(t, struct dw_mci, fault_timer);
  1507. unsigned long flags;
  1508. spin_lock_irqsave(&host->irq_lock, flags);
  1509. /*
  1510. * Only inject an error if we haven't already got an error or data over
  1511. * interrupt.
  1512. */
  1513. if (!host->data_status) {
  1514. host->data_status = SDMMC_INT_DCRC;
  1515. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  1516. queue_work(system_bh_wq, &host->bh_work);
  1517. }
  1518. spin_unlock_irqrestore(&host->irq_lock, flags);
  1519. return HRTIMER_NORESTART;
  1520. }
  1521. static void dw_mci_start_fault_timer(struct dw_mci *host)
  1522. {
  1523. struct mmc_data *data = host->data;
  1524. if (!data || data->blocks <= 1)
  1525. return;
  1526. if (!should_fail(&host->fail_data_crc, 1))
  1527. return;
  1528. /*
  1529. * Try to inject the error at random points during the data transfer.
  1530. */
  1531. hrtimer_start(&host->fault_timer,
  1532. ms_to_ktime(get_random_u32_below(25)),
  1533. HRTIMER_MODE_REL);
  1534. }
  1535. static void dw_mci_stop_fault_timer(struct dw_mci *host)
  1536. {
  1537. hrtimer_cancel(&host->fault_timer);
  1538. }
  1539. static void dw_mci_init_fault(struct dw_mci *host)
  1540. {
  1541. host->fail_data_crc = (struct fault_attr) FAULT_ATTR_INITIALIZER;
  1542. hrtimer_setup(&host->fault_timer, dw_mci_fault_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1543. }
  1544. #else
  1545. static void dw_mci_init_fault(struct dw_mci *host)
  1546. {
  1547. }
  1548. static void dw_mci_start_fault_timer(struct dw_mci *host)
  1549. {
  1550. }
  1551. static void dw_mci_stop_fault_timer(struct dw_mci *host)
  1552. {
  1553. }
  1554. #endif
  1555. static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
  1556. __releases(&host->lock)
  1557. __acquires(&host->lock)
  1558. {
  1559. struct dw_mci_slot *slot;
  1560. struct mmc_host *prev_mmc = host->slot->mmc;
  1561. WARN_ON(host->cmd || host->data);
  1562. host->slot->mrq = NULL;
  1563. host->mrq = NULL;
  1564. if (!list_empty(&host->queue)) {
  1565. slot = list_entry(host->queue.next,
  1566. struct dw_mci_slot, queue_node);
  1567. list_del(&slot->queue_node);
  1568. dev_vdbg(host->dev, "list not empty: %s is next\n",
  1569. mmc_hostname(slot->mmc));
  1570. host->state = STATE_SENDING_CMD;
  1571. dw_mci_start_request(host, slot);
  1572. } else {
  1573. dev_vdbg(host->dev, "list empty\n");
  1574. if (host->state == STATE_SENDING_CMD11)
  1575. host->state = STATE_WAITING_CMD11_DONE;
  1576. else
  1577. host->state = STATE_IDLE;
  1578. }
  1579. spin_unlock(&host->lock);
  1580. mmc_request_done(prev_mmc, mrq);
  1581. spin_lock(&host->lock);
  1582. }
  1583. static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
  1584. {
  1585. u32 status = host->cmd_status;
  1586. host->cmd_status = 0;
  1587. /* Read the response from the card (up to 16 bytes) */
  1588. if (cmd->flags & MMC_RSP_PRESENT) {
  1589. if (cmd->flags & MMC_RSP_136) {
  1590. cmd->resp[3] = mci_readl(host, RESP0);
  1591. cmd->resp[2] = mci_readl(host, RESP1);
  1592. cmd->resp[1] = mci_readl(host, RESP2);
  1593. cmd->resp[0] = mci_readl(host, RESP3);
  1594. } else {
  1595. cmd->resp[0] = mci_readl(host, RESP0);
  1596. cmd->resp[1] = 0;
  1597. cmd->resp[2] = 0;
  1598. cmd->resp[3] = 0;
  1599. }
  1600. }
  1601. if (status & SDMMC_INT_RTO)
  1602. cmd->error = -ETIMEDOUT;
  1603. else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
  1604. cmd->error = -EILSEQ;
  1605. else if (status & SDMMC_INT_RESP_ERR)
  1606. cmd->error = -EIO;
  1607. else
  1608. cmd->error = 0;
  1609. return cmd->error;
  1610. }
  1611. static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
  1612. {
  1613. u32 status = host->data_status;
  1614. if (status & DW_MCI_DATA_ERROR_FLAGS) {
  1615. if (status & SDMMC_INT_DRTO) {
  1616. data->error = -ETIMEDOUT;
  1617. } else if (status & SDMMC_INT_DCRC) {
  1618. data->error = -EILSEQ;
  1619. } else if (status & SDMMC_INT_EBE) {
  1620. if (host->dir_status ==
  1621. DW_MCI_SEND_STATUS) {
  1622. /*
  1623. * No data CRC status was returned.
  1624. * The number of bytes transferred
  1625. * will be exaggerated in PIO mode.
  1626. */
  1627. data->bytes_xfered = 0;
  1628. data->error = -ETIMEDOUT;
  1629. } else if (host->dir_status ==
  1630. DW_MCI_RECV_STATUS) {
  1631. data->error = -EILSEQ;
  1632. }
  1633. } else {
  1634. /* SDMMC_INT_SBE is included */
  1635. data->error = -EILSEQ;
  1636. }
  1637. dev_dbg(host->dev, "data error, status 0x%08x\n", status);
  1638. /*
  1639. * After an error, there may be data lingering
  1640. * in the FIFO
  1641. */
  1642. dw_mci_reset(host);
  1643. } else {
  1644. data->bytes_xfered = data->blocks * data->blksz;
  1645. data->error = 0;
  1646. }
  1647. return data->error;
  1648. }
  1649. static void dw_mci_set_drto(struct dw_mci *host)
  1650. {
  1651. const struct dw_mci_drv_data *drv_data = host->drv_data;
  1652. unsigned int drto_clks;
  1653. unsigned int drto_div;
  1654. unsigned int drto_ms;
  1655. unsigned long irqflags;
  1656. if (drv_data && drv_data->get_drto_clks)
  1657. drto_clks = drv_data->get_drto_clks(host);
  1658. else
  1659. drto_clks = mci_readl(host, TMOUT) >> 8;
  1660. drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2;
  1661. if (drto_div == 0)
  1662. drto_div = 1;
  1663. drto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * drto_clks * drto_div,
  1664. host->bus_hz);
  1665. dev_dbg(host->dev, "drto_ms: %u\n", drto_ms);
  1666. /* add a bit spare time */
  1667. drto_ms += 10;
  1668. spin_lock_irqsave(&host->irq_lock, irqflags);
  1669. if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
  1670. mod_timer(&host->dto_timer,
  1671. jiffies + msecs_to_jiffies(drto_ms));
  1672. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  1673. }
  1674. static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host)
  1675. {
  1676. if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
  1677. return false;
  1678. /*
  1679. * Really be certain that the timer has stopped. This is a bit of
  1680. * paranoia and could only really happen if we had really bad
  1681. * interrupt latency and the interrupt routine and timeout were
  1682. * running concurrently so that the timer_delete() in the interrupt
  1683. * handler couldn't run.
  1684. */
  1685. WARN_ON(timer_delete_sync(&host->cto_timer));
  1686. clear_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  1687. return true;
  1688. }
  1689. static bool dw_mci_clear_pending_data_complete(struct dw_mci *host)
  1690. {
  1691. if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events))
  1692. return false;
  1693. /* Extra paranoia just like dw_mci_clear_pending_cmd_complete() */
  1694. WARN_ON(timer_delete_sync(&host->dto_timer));
  1695. clear_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  1696. return true;
  1697. }
  1698. static void dw_mci_work_func(struct work_struct *t)
  1699. {
  1700. struct dw_mci *host = from_work(host, t, bh_work);
  1701. struct mmc_data *data;
  1702. struct mmc_command *cmd;
  1703. struct mmc_request *mrq;
  1704. enum dw_mci_state state;
  1705. enum dw_mci_state prev_state;
  1706. unsigned int err;
  1707. spin_lock(&host->lock);
  1708. state = host->state;
  1709. data = host->data;
  1710. mrq = host->mrq;
  1711. do {
  1712. prev_state = state;
  1713. switch (state) {
  1714. case STATE_IDLE:
  1715. case STATE_WAITING_CMD11_DONE:
  1716. break;
  1717. case STATE_SENDING_CMD11:
  1718. case STATE_SENDING_CMD:
  1719. if (!dw_mci_clear_pending_cmd_complete(host))
  1720. break;
  1721. cmd = host->cmd;
  1722. host->cmd = NULL;
  1723. set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
  1724. err = dw_mci_command_complete(host, cmd);
  1725. if (cmd == mrq->sbc && !err) {
  1726. __dw_mci_start_request(host, host->slot,
  1727. mrq->cmd);
  1728. goto unlock;
  1729. }
  1730. if (cmd->data && err) {
  1731. /*
  1732. * During UHS tuning sequence, sending the stop
  1733. * command after the response CRC error would
  1734. * throw the system into a confused state
  1735. * causing all future tuning phases to report
  1736. * failure.
  1737. *
  1738. * In such case controller will move into a data
  1739. * transfer state after a response error or
  1740. * response CRC error. Let's let that finish
  1741. * before trying to send a stop, so we'll go to
  1742. * STATE_SENDING_DATA.
  1743. *
  1744. * Although letting the data transfer take place
  1745. * will waste a bit of time (we already know
  1746. * the command was bad), it can't cause any
  1747. * errors since it's possible it would have
  1748. * taken place anyway if this bh work got
  1749. * delayed. Allowing the transfer to take place
  1750. * avoids races and keeps things simple.
  1751. */
  1752. if (err != -ETIMEDOUT &&
  1753. host->dir_status == DW_MCI_RECV_STATUS) {
  1754. state = STATE_SENDING_DATA;
  1755. continue;
  1756. }
  1757. send_stop_abort(host, data);
  1758. dw_mci_stop_dma(host);
  1759. state = STATE_SENDING_STOP;
  1760. break;
  1761. }
  1762. if (!cmd->data || err) {
  1763. dw_mci_request_end(host, mrq);
  1764. goto unlock;
  1765. }
  1766. prev_state = state = STATE_SENDING_DATA;
  1767. fallthrough;
  1768. case STATE_SENDING_DATA:
  1769. /*
  1770. * We could get a data error and never a transfer
  1771. * complete so we'd better check for it here.
  1772. *
  1773. * Note that we don't really care if we also got a
  1774. * transfer complete; stopping the DMA and sending an
  1775. * abort won't hurt.
  1776. */
  1777. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1778. &host->pending_events)) {
  1779. if (!(host->data_status & (SDMMC_INT_DRTO |
  1780. SDMMC_INT_EBE)))
  1781. send_stop_abort(host, data);
  1782. dw_mci_stop_dma(host);
  1783. state = STATE_DATA_ERROR;
  1784. break;
  1785. }
  1786. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1787. &host->pending_events)) {
  1788. /*
  1789. * If all data-related interrupts don't come
  1790. * within the given time in reading data state.
  1791. */
  1792. if (host->dir_status == DW_MCI_RECV_STATUS)
  1793. dw_mci_set_drto(host);
  1794. break;
  1795. }
  1796. set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
  1797. /*
  1798. * Handle an EVENT_DATA_ERROR that might have shown up
  1799. * before the transfer completed. This might not have
  1800. * been caught by the check above because the interrupt
  1801. * could have gone off between the previous check and
  1802. * the check for transfer complete.
  1803. *
  1804. * Technically this ought not be needed assuming we
  1805. * get a DATA_COMPLETE eventually (we'll notice the
  1806. * error and end the request), but it shouldn't hurt.
  1807. *
  1808. * This has the advantage of sending the stop command.
  1809. */
  1810. if (test_and_clear_bit(EVENT_DATA_ERROR,
  1811. &host->pending_events)) {
  1812. if (!(host->data_status & (SDMMC_INT_DRTO |
  1813. SDMMC_INT_EBE)))
  1814. send_stop_abort(host, data);
  1815. dw_mci_stop_dma(host);
  1816. state = STATE_DATA_ERROR;
  1817. break;
  1818. }
  1819. prev_state = state = STATE_DATA_BUSY;
  1820. fallthrough;
  1821. case STATE_DATA_BUSY:
  1822. if (!dw_mci_clear_pending_data_complete(host)) {
  1823. /*
  1824. * If data error interrupt comes but data over
  1825. * interrupt doesn't come within the given time.
  1826. * in reading data state.
  1827. */
  1828. if (host->dir_status == DW_MCI_RECV_STATUS)
  1829. dw_mci_set_drto(host);
  1830. break;
  1831. }
  1832. dw_mci_stop_fault_timer(host);
  1833. host->data = NULL;
  1834. set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
  1835. err = dw_mci_data_complete(host, data);
  1836. if (!err) {
  1837. if (!data->stop || mrq->sbc) {
  1838. if (mrq->sbc && data->stop)
  1839. data->stop->error = 0;
  1840. dw_mci_request_end(host, mrq);
  1841. goto unlock;
  1842. }
  1843. /* stop command for open-ended transfer*/
  1844. if (data->stop)
  1845. send_stop_abort(host, data);
  1846. } else {
  1847. /*
  1848. * If we don't have a command complete now we'll
  1849. * never get one since we just reset everything;
  1850. * better end the request.
  1851. *
  1852. * If we do have a command complete we'll fall
  1853. * through to the SENDING_STOP command and
  1854. * everything will be peachy keen.
  1855. */
  1856. if (!test_bit(EVENT_CMD_COMPLETE,
  1857. &host->pending_events)) {
  1858. host->cmd = NULL;
  1859. dw_mci_request_end(host, mrq);
  1860. goto unlock;
  1861. }
  1862. }
  1863. /*
  1864. * If err has non-zero,
  1865. * stop-abort command has been already issued.
  1866. */
  1867. prev_state = state = STATE_SENDING_STOP;
  1868. fallthrough;
  1869. case STATE_SENDING_STOP:
  1870. if (!dw_mci_clear_pending_cmd_complete(host))
  1871. break;
  1872. /* CMD error in data command */
  1873. if (mrq->cmd->error && mrq->data)
  1874. dw_mci_reset(host);
  1875. dw_mci_stop_fault_timer(host);
  1876. host->cmd = NULL;
  1877. host->data = NULL;
  1878. if (!mrq->sbc && mrq->stop)
  1879. dw_mci_command_complete(host, mrq->stop);
  1880. else
  1881. host->cmd_status = 0;
  1882. dw_mci_request_end(host, mrq);
  1883. goto unlock;
  1884. case STATE_DATA_ERROR:
  1885. if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
  1886. &host->pending_events))
  1887. break;
  1888. state = STATE_DATA_BUSY;
  1889. break;
  1890. }
  1891. } while (state != prev_state);
  1892. host->state = state;
  1893. unlock:
  1894. spin_unlock(&host->lock);
  1895. }
  1896. /* push final bytes to part_buf, only use during push */
  1897. static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1898. {
  1899. memcpy((void *)&host->part_buf, buf, cnt);
  1900. host->part_buf_count = cnt;
  1901. }
  1902. /* append bytes to part_buf, only use during push */
  1903. static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1904. {
  1905. cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
  1906. memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
  1907. host->part_buf_count += cnt;
  1908. return cnt;
  1909. }
  1910. /* pull first bytes from part_buf, only use during pull */
  1911. static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
  1912. {
  1913. cnt = min_t(int, cnt, host->part_buf_count);
  1914. if (cnt) {
  1915. memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
  1916. cnt);
  1917. host->part_buf_count -= cnt;
  1918. host->part_buf_start += cnt;
  1919. }
  1920. return cnt;
  1921. }
  1922. /* pull final bytes from the part_buf, assuming it's just been filled */
  1923. static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
  1924. {
  1925. memcpy(buf, &host->part_buf, cnt);
  1926. host->part_buf_start = cnt;
  1927. host->part_buf_count = (1 << host->data_shift) - cnt;
  1928. }
  1929. static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
  1930. {
  1931. struct mmc_data *data = host->data;
  1932. int init_cnt = cnt;
  1933. /* try and push anything in the part_buf */
  1934. if (unlikely(host->part_buf_count)) {
  1935. int len = dw_mci_push_part_bytes(host, buf, cnt);
  1936. buf += len;
  1937. cnt -= len;
  1938. if (host->part_buf_count == 2) {
  1939. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1940. host->part_buf_count = 0;
  1941. }
  1942. }
  1943. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1944. if (unlikely((unsigned long)buf & 0x1)) {
  1945. while (cnt >= 2) {
  1946. u16 aligned_buf[64];
  1947. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1948. int items = len >> 1;
  1949. int i;
  1950. /* memcpy from input buffer into aligned buffer */
  1951. memcpy(aligned_buf, buf, len);
  1952. buf += len;
  1953. cnt -= len;
  1954. /* push data from aligned buffer into fifo */
  1955. for (i = 0; i < items; ++i)
  1956. mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
  1957. }
  1958. } else
  1959. #endif
  1960. {
  1961. u16 *pdata = buf;
  1962. for (; cnt >= 2; cnt -= 2)
  1963. mci_fifo_writew(host->fifo_reg, *pdata++);
  1964. buf = pdata;
  1965. }
  1966. /* put anything remaining in the part_buf */
  1967. if (cnt) {
  1968. dw_mci_set_part_bytes(host, buf, cnt);
  1969. /* Push data if we have reached the expected data length */
  1970. if ((data->bytes_xfered + init_cnt) ==
  1971. (data->blksz * data->blocks))
  1972. mci_fifo_writew(host->fifo_reg, host->part_buf16);
  1973. }
  1974. }
  1975. static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
  1976. {
  1977. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  1978. if (unlikely((unsigned long)buf & 0x1)) {
  1979. while (cnt >= 2) {
  1980. /* pull data from fifo into aligned buffer */
  1981. u16 aligned_buf[64];
  1982. int len = min(cnt & -2, (int)sizeof(aligned_buf));
  1983. int items = len >> 1;
  1984. int i;
  1985. for (i = 0; i < items; ++i)
  1986. aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
  1987. /* memcpy from aligned buffer into output buffer */
  1988. memcpy(buf, aligned_buf, len);
  1989. buf += len;
  1990. cnt -= len;
  1991. }
  1992. } else
  1993. #endif
  1994. {
  1995. u16 *pdata = buf;
  1996. for (; cnt >= 2; cnt -= 2)
  1997. *pdata++ = mci_fifo_readw(host->fifo_reg);
  1998. buf = pdata;
  1999. }
  2000. if (cnt) {
  2001. host->part_buf16 = mci_fifo_readw(host->fifo_reg);
  2002. dw_mci_pull_final_bytes(host, buf, cnt);
  2003. }
  2004. }
  2005. static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
  2006. {
  2007. struct mmc_data *data = host->data;
  2008. int init_cnt = cnt;
  2009. /* try and push anything in the part_buf */
  2010. if (unlikely(host->part_buf_count)) {
  2011. int len = dw_mci_push_part_bytes(host, buf, cnt);
  2012. buf += len;
  2013. cnt -= len;
  2014. if (host->part_buf_count == 4) {
  2015. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  2016. host->part_buf_count = 0;
  2017. }
  2018. }
  2019. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2020. if (unlikely((unsigned long)buf & 0x3)) {
  2021. while (cnt >= 4) {
  2022. u32 aligned_buf[32];
  2023. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  2024. int items = len >> 2;
  2025. int i;
  2026. /* memcpy from input buffer into aligned buffer */
  2027. memcpy(aligned_buf, buf, len);
  2028. buf += len;
  2029. cnt -= len;
  2030. /* push data from aligned buffer into fifo */
  2031. for (i = 0; i < items; ++i)
  2032. mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
  2033. }
  2034. } else
  2035. #endif
  2036. {
  2037. u32 *pdata = buf;
  2038. for (; cnt >= 4; cnt -= 4)
  2039. mci_fifo_writel(host->fifo_reg, *pdata++);
  2040. buf = pdata;
  2041. }
  2042. /* put anything remaining in the part_buf */
  2043. if (cnt) {
  2044. dw_mci_set_part_bytes(host, buf, cnt);
  2045. /* Push data if we have reached the expected data length */
  2046. if ((data->bytes_xfered + init_cnt) ==
  2047. (data->blksz * data->blocks))
  2048. mci_fifo_writel(host->fifo_reg, host->part_buf32);
  2049. }
  2050. }
  2051. static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
  2052. {
  2053. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2054. if (unlikely((unsigned long)buf & 0x3)) {
  2055. while (cnt >= 4) {
  2056. /* pull data from fifo into aligned buffer */
  2057. u32 aligned_buf[32];
  2058. int len = min(cnt & -4, (int)sizeof(aligned_buf));
  2059. int items = len >> 2;
  2060. int i;
  2061. for (i = 0; i < items; ++i)
  2062. aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
  2063. /* memcpy from aligned buffer into output buffer */
  2064. memcpy(buf, aligned_buf, len);
  2065. buf += len;
  2066. cnt -= len;
  2067. }
  2068. } else
  2069. #endif
  2070. {
  2071. u32 *pdata = buf;
  2072. for (; cnt >= 4; cnt -= 4)
  2073. *pdata++ = mci_fifo_readl(host->fifo_reg);
  2074. buf = pdata;
  2075. }
  2076. if (cnt) {
  2077. host->part_buf32 = mci_fifo_readl(host->fifo_reg);
  2078. dw_mci_pull_final_bytes(host, buf, cnt);
  2079. }
  2080. }
  2081. static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
  2082. {
  2083. struct mmc_data *data = host->data;
  2084. int init_cnt = cnt;
  2085. /* try and push anything in the part_buf */
  2086. if (unlikely(host->part_buf_count)) {
  2087. int len = dw_mci_push_part_bytes(host, buf, cnt);
  2088. buf += len;
  2089. cnt -= len;
  2090. if (host->part_buf_count == 8) {
  2091. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  2092. host->part_buf_count = 0;
  2093. }
  2094. }
  2095. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2096. if (unlikely((unsigned long)buf & 0x7)) {
  2097. while (cnt >= 8) {
  2098. u64 aligned_buf[16];
  2099. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  2100. int items = len >> 3;
  2101. int i;
  2102. /* memcpy from input buffer into aligned buffer */
  2103. memcpy(aligned_buf, buf, len);
  2104. buf += len;
  2105. cnt -= len;
  2106. /* push data from aligned buffer into fifo */
  2107. for (i = 0; i < items; ++i)
  2108. mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
  2109. }
  2110. } else
  2111. #endif
  2112. {
  2113. u64 *pdata = buf;
  2114. for (; cnt >= 8; cnt -= 8)
  2115. mci_fifo_writeq(host->fifo_reg, *pdata++);
  2116. buf = pdata;
  2117. }
  2118. /* put anything remaining in the part_buf */
  2119. if (cnt) {
  2120. dw_mci_set_part_bytes(host, buf, cnt);
  2121. /* Push data if we have reached the expected data length */
  2122. if ((data->bytes_xfered + init_cnt) ==
  2123. (data->blksz * data->blocks))
  2124. mci_fifo_writeq(host->fifo_reg, host->part_buf);
  2125. }
  2126. }
  2127. static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
  2128. {
  2129. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2130. if (unlikely((unsigned long)buf & 0x7)) {
  2131. while (cnt >= 8) {
  2132. /* pull data from fifo into aligned buffer */
  2133. u64 aligned_buf[16];
  2134. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  2135. int items = len >> 3;
  2136. int i;
  2137. for (i = 0; i < items; ++i)
  2138. aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
  2139. /* memcpy from aligned buffer into output buffer */
  2140. memcpy(buf, aligned_buf, len);
  2141. buf += len;
  2142. cnt -= len;
  2143. }
  2144. } else
  2145. #endif
  2146. {
  2147. u64 *pdata = buf;
  2148. for (; cnt >= 8; cnt -= 8)
  2149. *pdata++ = mci_fifo_readq(host->fifo_reg);
  2150. buf = pdata;
  2151. }
  2152. if (cnt) {
  2153. host->part_buf = mci_fifo_readq(host->fifo_reg);
  2154. dw_mci_pull_final_bytes(host, buf, cnt);
  2155. }
  2156. }
  2157. static void dw_mci_push_data64_32(struct dw_mci *host, void *buf, int cnt)
  2158. {
  2159. struct mmc_data *data = host->data;
  2160. int init_cnt = cnt;
  2161. /* try and push anything in the part_buf */
  2162. if (unlikely(host->part_buf_count)) {
  2163. int len = dw_mci_push_part_bytes(host, buf, cnt);
  2164. buf += len;
  2165. cnt -= len;
  2166. if (host->part_buf_count == 8) {
  2167. mci_fifo_l_writeq(host->fifo_reg, host->part_buf);
  2168. host->part_buf_count = 0;
  2169. }
  2170. }
  2171. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2172. if (unlikely((unsigned long)buf & 0x7)) {
  2173. while (cnt >= 8) {
  2174. u64 aligned_buf[16];
  2175. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  2176. int items = len >> 3;
  2177. int i;
  2178. /* memcpy from input buffer into aligned buffer */
  2179. memcpy(aligned_buf, buf, len);
  2180. buf += len;
  2181. cnt -= len;
  2182. /* push data from aligned buffer into fifo */
  2183. for (i = 0; i < items; ++i)
  2184. mci_fifo_l_writeq(host->fifo_reg, aligned_buf[i]);
  2185. }
  2186. } else
  2187. #endif
  2188. {
  2189. u64 *pdata = buf;
  2190. for (; cnt >= 8; cnt -= 8)
  2191. mci_fifo_l_writeq(host->fifo_reg, *pdata++);
  2192. buf = pdata;
  2193. }
  2194. /* put anything remaining in the part_buf */
  2195. if (cnt) {
  2196. dw_mci_set_part_bytes(host, buf, cnt);
  2197. /* Push data if we have reached the expected data length */
  2198. if ((data->bytes_xfered + init_cnt) ==
  2199. (data->blksz * data->blocks))
  2200. mci_fifo_l_writeq(host->fifo_reg, host->part_buf);
  2201. }
  2202. }
  2203. static void dw_mci_pull_data64_32(struct dw_mci *host, void *buf, int cnt)
  2204. {
  2205. #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
  2206. if (unlikely((unsigned long)buf & 0x7)) {
  2207. while (cnt >= 8) {
  2208. /* pull data from fifo into aligned buffer */
  2209. u64 aligned_buf[16];
  2210. int len = min(cnt & -8, (int)sizeof(aligned_buf));
  2211. int items = len >> 3;
  2212. int i;
  2213. for (i = 0; i < items; ++i)
  2214. aligned_buf[i] = mci_fifo_l_readq(host->fifo_reg);
  2215. /* memcpy from aligned buffer into output buffer */
  2216. memcpy(buf, aligned_buf, len);
  2217. buf += len;
  2218. cnt -= len;
  2219. }
  2220. } else
  2221. #endif
  2222. {
  2223. u64 *pdata = buf;
  2224. for (; cnt >= 8; cnt -= 8)
  2225. *pdata++ = mci_fifo_l_readq(host->fifo_reg);
  2226. buf = pdata;
  2227. }
  2228. if (cnt) {
  2229. host->part_buf = mci_fifo_l_readq(host->fifo_reg);
  2230. dw_mci_pull_final_bytes(host, buf, cnt);
  2231. }
  2232. }
  2233. static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
  2234. {
  2235. int len;
  2236. /* get remaining partial bytes */
  2237. len = dw_mci_pull_part_bytes(host, buf, cnt);
  2238. if (unlikely(len == cnt))
  2239. return;
  2240. buf += len;
  2241. cnt -= len;
  2242. /* get the rest of the data */
  2243. host->pull_data(host, buf, cnt);
  2244. }
  2245. static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
  2246. {
  2247. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  2248. void *buf;
  2249. unsigned int offset;
  2250. struct mmc_data *data = host->data;
  2251. int shift = host->data_shift;
  2252. u32 status;
  2253. unsigned int len;
  2254. unsigned int remain, fcnt;
  2255. do {
  2256. if (!sg_miter_next(sg_miter))
  2257. goto done;
  2258. host->sg = sg_miter->piter.sg;
  2259. buf = sg_miter->addr;
  2260. remain = sg_miter->length;
  2261. offset = 0;
  2262. do {
  2263. fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
  2264. << shift) + host->part_buf_count;
  2265. len = min(remain, fcnt);
  2266. if (!len)
  2267. break;
  2268. dw_mci_pull_data(host, (void *)(buf + offset), len);
  2269. data->bytes_xfered += len;
  2270. offset += len;
  2271. remain -= len;
  2272. } while (remain);
  2273. sg_miter->consumed = offset;
  2274. status = mci_readl(host, MINTSTS);
  2275. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  2276. /* if the RXDR is ready read again */
  2277. } while ((status & SDMMC_INT_RXDR) ||
  2278. (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
  2279. if (!remain) {
  2280. if (!sg_miter_next(sg_miter))
  2281. goto done;
  2282. sg_miter->consumed = 0;
  2283. }
  2284. sg_miter_stop(sg_miter);
  2285. return;
  2286. done:
  2287. sg_miter_stop(sg_miter);
  2288. host->sg = NULL;
  2289. smp_wmb(); /* drain writebuffer */
  2290. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  2291. }
  2292. static void dw_mci_write_data_pio(struct dw_mci *host)
  2293. {
  2294. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  2295. void *buf;
  2296. unsigned int offset;
  2297. struct mmc_data *data = host->data;
  2298. int shift = host->data_shift;
  2299. u32 status;
  2300. unsigned int len;
  2301. unsigned int fifo_depth = host->fifo_depth;
  2302. unsigned int remain, fcnt;
  2303. do {
  2304. if (!sg_miter_next(sg_miter))
  2305. goto done;
  2306. host->sg = sg_miter->piter.sg;
  2307. buf = sg_miter->addr;
  2308. remain = sg_miter->length;
  2309. offset = 0;
  2310. do {
  2311. fcnt = ((fifo_depth -
  2312. SDMMC_GET_FCNT(mci_readl(host, STATUS)))
  2313. << shift) - host->part_buf_count;
  2314. len = min(remain, fcnt);
  2315. if (!len)
  2316. break;
  2317. host->push_data(host, (void *)(buf + offset), len);
  2318. data->bytes_xfered += len;
  2319. offset += len;
  2320. remain -= len;
  2321. } while (remain);
  2322. sg_miter->consumed = offset;
  2323. status = mci_readl(host, MINTSTS);
  2324. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  2325. } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
  2326. if (!remain) {
  2327. if (!sg_miter_next(sg_miter))
  2328. goto done;
  2329. sg_miter->consumed = 0;
  2330. }
  2331. sg_miter_stop(sg_miter);
  2332. return;
  2333. done:
  2334. sg_miter_stop(sg_miter);
  2335. host->sg = NULL;
  2336. smp_wmb(); /* drain writebuffer */
  2337. set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
  2338. }
  2339. static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
  2340. {
  2341. timer_delete(&host->cto_timer);
  2342. if (!host->cmd_status)
  2343. host->cmd_status = status;
  2344. smp_wmb(); /* drain writebuffer */
  2345. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2346. queue_work(system_bh_wq, &host->bh_work);
  2347. dw_mci_start_fault_timer(host);
  2348. }
  2349. static void dw_mci_handle_cd(struct dw_mci *host)
  2350. {
  2351. struct dw_mci_slot *slot = host->slot;
  2352. mmc_detect_change(slot->mmc,
  2353. msecs_to_jiffies(host->pdata->detect_delay_ms));
  2354. }
  2355. static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
  2356. {
  2357. struct dw_mci *host = dev_id;
  2358. u32 pending;
  2359. struct dw_mci_slot *slot = host->slot;
  2360. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  2361. if (pending) {
  2362. /* Check volt switch first, since it can look like an error */
  2363. if ((host->state == STATE_SENDING_CMD11) &&
  2364. (pending & SDMMC_INT_VOLT_SWITCH)) {
  2365. mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
  2366. pending &= ~SDMMC_INT_VOLT_SWITCH;
  2367. /*
  2368. * Hold the lock; we know cmd11_timer can't be kicked
  2369. * off after the lock is released, so safe to delete.
  2370. */
  2371. spin_lock(&host->irq_lock);
  2372. dw_mci_cmd_interrupt(host, pending);
  2373. spin_unlock(&host->irq_lock);
  2374. timer_delete(&host->cmd11_timer);
  2375. }
  2376. if (pending & DW_MCI_CMD_ERROR_FLAGS) {
  2377. spin_lock(&host->irq_lock);
  2378. timer_delete(&host->cto_timer);
  2379. mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
  2380. host->cmd_status = pending;
  2381. smp_wmb(); /* drain writebuffer */
  2382. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2383. spin_unlock(&host->irq_lock);
  2384. }
  2385. if (pending & DW_MCI_DATA_ERROR_FLAGS) {
  2386. spin_lock(&host->irq_lock);
  2387. if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
  2388. timer_delete(&host->dto_timer);
  2389. /* if there is an error report DATA_ERROR */
  2390. mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
  2391. host->data_status = pending;
  2392. smp_wmb(); /* drain writebuffer */
  2393. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  2394. if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT)
  2395. /* In case of error, we cannot expect a DTO */
  2396. set_bit(EVENT_DATA_COMPLETE,
  2397. &host->pending_events);
  2398. queue_work(system_bh_wq, &host->bh_work);
  2399. spin_unlock(&host->irq_lock);
  2400. }
  2401. if (pending & SDMMC_INT_DATA_OVER) {
  2402. spin_lock(&host->irq_lock);
  2403. timer_delete(&host->dto_timer);
  2404. mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
  2405. if (!host->data_status)
  2406. host->data_status = pending;
  2407. smp_wmb(); /* drain writebuffer */
  2408. if (host->dir_status == DW_MCI_RECV_STATUS) {
  2409. if (host->sg != NULL)
  2410. dw_mci_read_data_pio(host, true);
  2411. }
  2412. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  2413. queue_work(system_bh_wq, &host->bh_work);
  2414. spin_unlock(&host->irq_lock);
  2415. }
  2416. if (pending & SDMMC_INT_RXDR) {
  2417. mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
  2418. if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
  2419. dw_mci_read_data_pio(host, false);
  2420. }
  2421. if (pending & SDMMC_INT_TXDR) {
  2422. mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
  2423. if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
  2424. dw_mci_write_data_pio(host);
  2425. }
  2426. if (pending & SDMMC_INT_CMD_DONE) {
  2427. spin_lock(&host->irq_lock);
  2428. mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
  2429. dw_mci_cmd_interrupt(host, pending);
  2430. spin_unlock(&host->irq_lock);
  2431. }
  2432. if (pending & SDMMC_INT_CD) {
  2433. mci_writel(host, RINTSTS, SDMMC_INT_CD);
  2434. dw_mci_handle_cd(host);
  2435. }
  2436. if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
  2437. mci_writel(host, RINTSTS,
  2438. SDMMC_INT_SDIO(slot->sdio_id));
  2439. __dw_mci_enable_sdio_irq(slot, 0);
  2440. sdio_signal_irq(slot->mmc);
  2441. }
  2442. }
  2443. if (host->use_dma != TRANS_MODE_IDMAC)
  2444. return IRQ_HANDLED;
  2445. /* Handle IDMA interrupts */
  2446. if (host->dma_64bit_address == 1) {
  2447. pending = mci_readl(host, IDSTS64);
  2448. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  2449. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
  2450. SDMMC_IDMAC_INT_RI);
  2451. mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
  2452. if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
  2453. host->dma_ops->complete((void *)host);
  2454. }
  2455. } else {
  2456. pending = mci_readl(host, IDSTS);
  2457. if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
  2458. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
  2459. SDMMC_IDMAC_INT_RI);
  2460. mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
  2461. if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
  2462. host->dma_ops->complete((void *)host);
  2463. }
  2464. }
  2465. return IRQ_HANDLED;
  2466. }
  2467. static int dw_mci_init_slot_caps(struct dw_mci_slot *slot)
  2468. {
  2469. struct dw_mci *host = slot->host;
  2470. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2471. struct mmc_host *mmc = slot->mmc;
  2472. int ctrl_id;
  2473. if (host->pdata->caps)
  2474. mmc->caps = host->pdata->caps;
  2475. if (host->pdata->pm_caps)
  2476. mmc->pm_caps = host->pdata->pm_caps;
  2477. if (drv_data)
  2478. mmc->caps |= drv_data->common_caps;
  2479. if (host->dev->of_node) {
  2480. ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
  2481. if (ctrl_id < 0)
  2482. ctrl_id = 0;
  2483. } else {
  2484. ctrl_id = to_platform_device(host->dev)->id;
  2485. }
  2486. if (drv_data && drv_data->caps) {
  2487. if (ctrl_id >= drv_data->num_caps) {
  2488. dev_err(host->dev, "invalid controller id %d\n",
  2489. ctrl_id);
  2490. return -EINVAL;
  2491. }
  2492. mmc->caps |= drv_data->caps[ctrl_id];
  2493. }
  2494. if (host->pdata->caps2)
  2495. mmc->caps2 = host->pdata->caps2;
  2496. /* if host has set a minimum_freq, we should respect it */
  2497. if (host->minimum_speed)
  2498. mmc->f_min = host->minimum_speed;
  2499. else
  2500. mmc->f_min = DW_MCI_FREQ_MIN;
  2501. if (!mmc->f_max)
  2502. mmc->f_max = DW_MCI_FREQ_MAX;
  2503. /* Process SDIO IRQs through the sdio_irq_work. */
  2504. if (mmc->caps & MMC_CAP_SDIO_IRQ)
  2505. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  2506. return 0;
  2507. }
  2508. static int dw_mci_init_slot(struct dw_mci *host)
  2509. {
  2510. struct mmc_host *mmc;
  2511. struct dw_mci_slot *slot;
  2512. int ret;
  2513. mmc = devm_mmc_alloc_host(host->dev, sizeof(*slot));
  2514. if (!mmc)
  2515. return -ENOMEM;
  2516. slot = mmc_priv(mmc);
  2517. slot->id = 0;
  2518. slot->sdio_id = host->sdio_id0 + slot->id;
  2519. slot->mmc = mmc;
  2520. slot->host = host;
  2521. host->slot = slot;
  2522. mmc->ops = &dw_mci_ops;
  2523. /*if there are external regulators, get them*/
  2524. ret = mmc_regulator_get_supply(mmc);
  2525. if (ret)
  2526. return ret;
  2527. if (!mmc->ocr_avail)
  2528. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  2529. ret = mmc_of_parse(mmc);
  2530. if (ret)
  2531. return ret;
  2532. ret = dw_mci_init_slot_caps(slot);
  2533. if (ret)
  2534. return ret;
  2535. /* Useful defaults if platform data is unset. */
  2536. if (host->use_dma == TRANS_MODE_IDMAC) {
  2537. mmc->max_segs = host->ring_size;
  2538. mmc->max_blk_size = 65535;
  2539. mmc->max_seg_size = 0x1000;
  2540. mmc->max_req_size = mmc->max_seg_size * host->ring_size;
  2541. mmc->max_blk_count = mmc->max_req_size / 512;
  2542. } else if (host->use_dma == TRANS_MODE_EDMAC) {
  2543. mmc->max_segs = 64;
  2544. mmc->max_blk_size = 65535;
  2545. mmc->max_blk_count = 65535;
  2546. mmc->max_req_size =
  2547. mmc->max_blk_size * mmc->max_blk_count;
  2548. mmc->max_seg_size = mmc->max_req_size;
  2549. } else {
  2550. /* TRANS_MODE_PIO */
  2551. mmc->max_segs = 64;
  2552. mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
  2553. mmc->max_blk_count = 512;
  2554. mmc->max_req_size = mmc->max_blk_size *
  2555. mmc->max_blk_count;
  2556. mmc->max_seg_size = mmc->max_req_size;
  2557. }
  2558. dw_mci_get_cd(mmc);
  2559. ret = mmc_add_host(mmc);
  2560. if (ret)
  2561. return ret;
  2562. #if defined(CONFIG_DEBUG_FS)
  2563. dw_mci_init_debugfs(slot);
  2564. #endif
  2565. return 0;
  2566. }
  2567. static void dw_mci_cleanup_slot(struct dw_mci_slot *slot)
  2568. {
  2569. /* Debugfs stuff is cleaned up by mmc core */
  2570. mmc_remove_host(slot->mmc);
  2571. slot->host->slot = NULL;
  2572. }
  2573. static void dw_mci_init_dma(struct dw_mci *host)
  2574. {
  2575. int addr_config;
  2576. struct device *dev = host->dev;
  2577. /*
  2578. * Check tansfer mode from HCON[17:16]
  2579. * Clear the ambiguous description of dw_mmc databook:
  2580. * 2b'00: No DMA Interface -> Actually means using Internal DMA block
  2581. * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
  2582. * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
  2583. * 2b'11: Non DW DMA Interface -> pio only
  2584. * Compared to DesignWare DMA Interface, Generic DMA Interface has a
  2585. * simpler request/acknowledge handshake mechanism and both of them
  2586. * are regarded as external dma master for dw_mmc.
  2587. */
  2588. host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
  2589. if (host->use_dma == DMA_INTERFACE_IDMA) {
  2590. host->use_dma = TRANS_MODE_IDMAC;
  2591. } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
  2592. host->use_dma == DMA_INTERFACE_GDMA) {
  2593. host->use_dma = TRANS_MODE_EDMAC;
  2594. } else {
  2595. goto no_dma;
  2596. }
  2597. /* Determine which DMA interface to use */
  2598. if (host->use_dma == TRANS_MODE_IDMAC) {
  2599. /*
  2600. * Check ADDR_CONFIG bit in HCON to find
  2601. * IDMAC address bus width
  2602. */
  2603. addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
  2604. if (addr_config == 1) {
  2605. /* host supports IDMAC in 64-bit address mode */
  2606. host->dma_64bit_address = 1;
  2607. dev_info(host->dev,
  2608. "IDMAC supports 64-bit address mode.\n");
  2609. if (dma_set_mask_and_coherent(host->dev, DMA_BIT_MASK(64)))
  2610. dev_info(host->dev, "Fail to set 64-bit DMA mask");
  2611. } else {
  2612. /* host supports IDMAC in 32-bit address mode */
  2613. host->dma_64bit_address = 0;
  2614. dev_info(host->dev,
  2615. "IDMAC supports 32-bit address mode.\n");
  2616. }
  2617. /* Alloc memory for sg translation */
  2618. host->sg_cpu = dmam_alloc_coherent(host->dev,
  2619. DESC_RING_BUF_SZ,
  2620. &host->sg_dma, GFP_KERNEL);
  2621. if (!host->sg_cpu) {
  2622. dev_err(host->dev,
  2623. "%s: could not alloc DMA memory\n",
  2624. __func__);
  2625. goto no_dma;
  2626. }
  2627. host->dma_ops = &dw_mci_idmac_ops;
  2628. dev_info(host->dev, "Using internal DMA controller.\n");
  2629. } else {
  2630. /* TRANS_MODE_EDMAC: check dma bindings again */
  2631. if ((device_property_string_array_count(dev, "dma-names") < 0) ||
  2632. !device_property_present(dev, "dmas")) {
  2633. goto no_dma;
  2634. }
  2635. host->dma_ops = &dw_mci_edmac_ops;
  2636. dev_info(host->dev, "Using external DMA controller.\n");
  2637. }
  2638. if (host->dma_ops->init && host->dma_ops->start &&
  2639. host->dma_ops->stop && host->dma_ops->cleanup) {
  2640. if (host->dma_ops->init(host)) {
  2641. dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
  2642. __func__);
  2643. goto no_dma;
  2644. }
  2645. } else {
  2646. dev_err(host->dev, "DMA initialization not found.\n");
  2647. goto no_dma;
  2648. }
  2649. return;
  2650. no_dma:
  2651. dev_info(host->dev, "Using PIO mode.\n");
  2652. host->use_dma = TRANS_MODE_PIO;
  2653. }
  2654. static void dw_mci_cmd11_timer(struct timer_list *t)
  2655. {
  2656. struct dw_mci *host = timer_container_of(host, t, cmd11_timer);
  2657. if (host->state != STATE_SENDING_CMD11) {
  2658. dev_warn(host->dev, "Unexpected CMD11 timeout\n");
  2659. return;
  2660. }
  2661. host->cmd_status = SDMMC_INT_RTO;
  2662. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2663. queue_work(system_bh_wq, &host->bh_work);
  2664. }
  2665. static void dw_mci_cto_timer(struct timer_list *t)
  2666. {
  2667. struct dw_mci *host = timer_container_of(host, t, cto_timer);
  2668. unsigned long irqflags;
  2669. u32 pending;
  2670. spin_lock_irqsave(&host->irq_lock, irqflags);
  2671. /*
  2672. * If somehow we have very bad interrupt latency it's remotely possible
  2673. * that the timer could fire while the interrupt is still pending or
  2674. * while the interrupt is midway through running. Let's be paranoid
  2675. * and detect those two cases. Note that this is paranoia is somewhat
  2676. * justified because in this function we don't actually cancel the
  2677. * pending command in the controller--we just assume it will never come.
  2678. */
  2679. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  2680. if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) {
  2681. /* The interrupt should fire; no need to act but we can warn */
  2682. dev_warn(host->dev, "Unexpected interrupt latency\n");
  2683. goto exit;
  2684. }
  2685. if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) {
  2686. /* Presumably interrupt handler couldn't delete the timer */
  2687. dev_warn(host->dev, "CTO timeout when already completed\n");
  2688. goto exit;
  2689. }
  2690. /*
  2691. * Continued paranoia to make sure we're in the state we expect.
  2692. * This paranoia isn't really justified but it seems good to be safe.
  2693. */
  2694. switch (host->state) {
  2695. case STATE_SENDING_CMD11:
  2696. case STATE_SENDING_CMD:
  2697. case STATE_SENDING_STOP:
  2698. /*
  2699. * If CMD_DONE interrupt does NOT come in sending command
  2700. * state, we should notify the driver to terminate current
  2701. * transfer and report a command timeout to the core.
  2702. */
  2703. host->cmd_status = SDMMC_INT_RTO;
  2704. set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
  2705. queue_work(system_bh_wq, &host->bh_work);
  2706. break;
  2707. default:
  2708. dev_warn(host->dev, "Unexpected command timeout, state %d\n",
  2709. host->state);
  2710. break;
  2711. }
  2712. exit:
  2713. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2714. }
  2715. static void dw_mci_dto_timer(struct timer_list *t)
  2716. {
  2717. struct dw_mci *host = timer_container_of(host, t, dto_timer);
  2718. unsigned long irqflags;
  2719. u32 pending;
  2720. spin_lock_irqsave(&host->irq_lock, irqflags);
  2721. /*
  2722. * The DTO timer is much longer than the CTO timer, so it's even less
  2723. * likely that we'll these cases, but it pays to be paranoid.
  2724. */
  2725. pending = mci_readl(host, MINTSTS); /* read-only mask reg */
  2726. if (pending & SDMMC_INT_DATA_OVER) {
  2727. /* The interrupt should fire; no need to act but we can warn */
  2728. dev_warn(host->dev, "Unexpected data interrupt latency\n");
  2729. goto exit;
  2730. }
  2731. if (test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) {
  2732. /* Presumably interrupt handler couldn't delete the timer */
  2733. dev_warn(host->dev, "DTO timeout when already completed\n");
  2734. goto exit;
  2735. }
  2736. /*
  2737. * Continued paranoia to make sure we're in the state we expect.
  2738. * This paranoia isn't really justified but it seems good to be safe.
  2739. */
  2740. switch (host->state) {
  2741. case STATE_SENDING_DATA:
  2742. case STATE_DATA_BUSY:
  2743. /*
  2744. * If DTO interrupt does NOT come in sending data state,
  2745. * we should notify the driver to terminate current transfer
  2746. * and report a data timeout to the core.
  2747. */
  2748. host->data_status = SDMMC_INT_DRTO;
  2749. set_bit(EVENT_DATA_ERROR, &host->pending_events);
  2750. set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
  2751. queue_work(system_bh_wq, &host->bh_work);
  2752. break;
  2753. default:
  2754. dev_warn(host->dev, "Unexpected data timeout, state %d\n",
  2755. host->state);
  2756. break;
  2757. }
  2758. exit:
  2759. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2760. }
  2761. #ifdef CONFIG_OF
  2762. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2763. {
  2764. struct dw_mci_board *pdata;
  2765. struct device *dev = host->dev;
  2766. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2767. int ret;
  2768. u32 clock_frequency;
  2769. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  2770. if (!pdata)
  2771. return ERR_PTR(-ENOMEM);
  2772. /* find reset controller when exist */
  2773. pdata->rstc = devm_reset_control_get_optional_exclusive(dev, "reset");
  2774. if (IS_ERR(pdata->rstc))
  2775. return ERR_CAST(pdata->rstc);
  2776. if (device_property_read_u32(dev, "fifo-depth", &pdata->fifo_depth))
  2777. dev_info(dev,
  2778. "fifo-depth property not found, using value of FIFOTH register as default\n");
  2779. device_property_read_u32(dev, "card-detect-delay",
  2780. &pdata->detect_delay_ms);
  2781. device_property_read_u32(dev, "data-addr", &host->data_addr_override);
  2782. if (device_property_present(dev, "fifo-watermark-aligned"))
  2783. host->wm_aligned = true;
  2784. if (!device_property_read_u32(dev, "clock-frequency", &clock_frequency))
  2785. pdata->bus_hz = clock_frequency;
  2786. if (drv_data && drv_data->parse_dt) {
  2787. ret = drv_data->parse_dt(host);
  2788. if (ret)
  2789. return ERR_PTR(ret);
  2790. }
  2791. return pdata;
  2792. }
  2793. #else /* CONFIG_OF */
  2794. static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
  2795. {
  2796. return ERR_PTR(-EINVAL);
  2797. }
  2798. #endif /* CONFIG_OF */
  2799. static void dw_mci_enable_cd(struct dw_mci *host)
  2800. {
  2801. unsigned long irqflags;
  2802. u32 temp;
  2803. /*
  2804. * No need for CD if all slots have a non-error GPIO
  2805. * as well as broken card detection is found.
  2806. */
  2807. if (host->slot->mmc->caps & MMC_CAP_NEEDS_POLL)
  2808. return;
  2809. if (mmc_gpio_get_cd(host->slot->mmc) < 0) {
  2810. spin_lock_irqsave(&host->irq_lock, irqflags);
  2811. temp = mci_readl(host, INTMASK);
  2812. temp |= SDMMC_INT_CD;
  2813. mci_writel(host, INTMASK, temp);
  2814. spin_unlock_irqrestore(&host->irq_lock, irqflags);
  2815. }
  2816. }
  2817. int dw_mci_probe(struct dw_mci *host)
  2818. {
  2819. const struct dw_mci_drv_data *drv_data = host->drv_data;
  2820. int width, i, ret = 0;
  2821. u32 fifo_size;
  2822. if (!host->pdata) {
  2823. host->pdata = dw_mci_parse_dt(host);
  2824. if (IS_ERR(host->pdata))
  2825. return dev_err_probe(host->dev, PTR_ERR(host->pdata),
  2826. "platform data not available\n");
  2827. }
  2828. host->biu_clk = devm_clk_get(host->dev, "biu");
  2829. if (IS_ERR(host->biu_clk)) {
  2830. dev_dbg(host->dev, "biu clock not available\n");
  2831. ret = PTR_ERR(host->biu_clk);
  2832. if (ret == -EPROBE_DEFER)
  2833. return ret;
  2834. } else {
  2835. ret = clk_prepare_enable(host->biu_clk);
  2836. if (ret) {
  2837. dev_err(host->dev, "failed to enable biu clock\n");
  2838. return ret;
  2839. }
  2840. }
  2841. host->ciu_clk = devm_clk_get(host->dev, "ciu");
  2842. if (IS_ERR(host->ciu_clk)) {
  2843. dev_dbg(host->dev, "ciu clock not available\n");
  2844. ret = PTR_ERR(host->ciu_clk);
  2845. if (ret == -EPROBE_DEFER)
  2846. goto err_clk_biu;
  2847. host->bus_hz = host->pdata->bus_hz;
  2848. } else {
  2849. ret = clk_prepare_enable(host->ciu_clk);
  2850. if (ret) {
  2851. dev_err(host->dev, "failed to enable ciu clock\n");
  2852. goto err_clk_biu;
  2853. }
  2854. if (host->pdata->bus_hz) {
  2855. ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
  2856. if (ret)
  2857. dev_warn(host->dev,
  2858. "Unable to set bus rate to %uHz\n",
  2859. host->pdata->bus_hz);
  2860. }
  2861. host->bus_hz = clk_get_rate(host->ciu_clk);
  2862. }
  2863. if (!host->bus_hz) {
  2864. dev_err(host->dev,
  2865. "Platform data must supply bus speed\n");
  2866. ret = -ENODEV;
  2867. goto err_clk_ciu;
  2868. }
  2869. if (host->pdata->rstc) {
  2870. reset_control_assert(host->pdata->rstc);
  2871. usleep_range(10, 50);
  2872. reset_control_deassert(host->pdata->rstc);
  2873. }
  2874. if (drv_data && drv_data->init) {
  2875. ret = drv_data->init(host);
  2876. if (ret) {
  2877. dev_err(host->dev,
  2878. "implementation specific init failed\n");
  2879. goto err_clk_ciu;
  2880. }
  2881. }
  2882. timer_setup(&host->cmd11_timer, dw_mci_cmd11_timer, 0);
  2883. timer_setup(&host->cto_timer, dw_mci_cto_timer, 0);
  2884. timer_setup(&host->dto_timer, dw_mci_dto_timer, 0);
  2885. spin_lock_init(&host->lock);
  2886. spin_lock_init(&host->irq_lock);
  2887. INIT_LIST_HEAD(&host->queue);
  2888. dw_mci_init_fault(host);
  2889. /*
  2890. * Get the host data width - this assumes that HCON has been set with
  2891. * the correct values.
  2892. */
  2893. i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
  2894. if (!i) {
  2895. host->push_data = dw_mci_push_data16;
  2896. host->pull_data = dw_mci_pull_data16;
  2897. width = 16;
  2898. host->data_shift = 1;
  2899. } else if (i == 2) {
  2900. if ((host->quirks & DW_MMC_QUIRK_FIFO64_32)) {
  2901. host->push_data = dw_mci_push_data64_32;
  2902. host->pull_data = dw_mci_pull_data64_32;
  2903. } else {
  2904. host->push_data = dw_mci_push_data64;
  2905. host->pull_data = dw_mci_pull_data64;
  2906. }
  2907. width = 64;
  2908. host->data_shift = 3;
  2909. } else {
  2910. /* Check for a reserved value, and warn if it is */
  2911. WARN((i != 1),
  2912. "HCON reports a reserved host data width!\n"
  2913. "Defaulting to 32-bit access.\n");
  2914. host->push_data = dw_mci_push_data32;
  2915. host->pull_data = dw_mci_pull_data32;
  2916. width = 32;
  2917. host->data_shift = 2;
  2918. }
  2919. /* Reset all blocks */
  2920. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  2921. ret = -ENODEV;
  2922. goto err_clk_ciu;
  2923. }
  2924. host->dma_ops = host->pdata->dma_ops;
  2925. dw_mci_init_dma(host);
  2926. /* Clear the interrupts for the host controller */
  2927. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  2928. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  2929. /* Put in max timeout */
  2930. mci_writel(host, TMOUT, 0xFFFFFFFF);
  2931. /*
  2932. * FIFO threshold settings RxMark = fifo_size / 2 - 1,
  2933. * Tx Mark = fifo_size / 2 DMA Size = 8
  2934. */
  2935. if (!host->pdata->fifo_depth) {
  2936. /*
  2937. * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
  2938. * have been overwritten by the bootloader, just like we're
  2939. * about to do, so if you know the value for your hardware, you
  2940. * should put it in the platform data.
  2941. */
  2942. fifo_size = mci_readl(host, FIFOTH);
  2943. fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
  2944. } else {
  2945. fifo_size = host->pdata->fifo_depth;
  2946. }
  2947. host->fifo_depth = fifo_size;
  2948. host->fifoth_val =
  2949. SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
  2950. mci_writel(host, FIFOTH, host->fifoth_val);
  2951. /* disable clock to CIU */
  2952. mci_writel(host, CLKENA, 0);
  2953. mci_writel(host, CLKSRC, 0);
  2954. /*
  2955. * In 2.40a spec, Data offset is changed.
  2956. * Need to check the version-id and set data-offset for DATA register.
  2957. */
  2958. host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
  2959. dev_info(host->dev, "Version ID is %04x\n", host->verid);
  2960. if (host->data_addr_override)
  2961. host->fifo_reg = host->regs + host->data_addr_override;
  2962. else if (host->verid < DW_MMC_240A)
  2963. host->fifo_reg = host->regs + DATA_OFFSET;
  2964. else
  2965. host->fifo_reg = host->regs + DATA_240A_OFFSET;
  2966. INIT_WORK(&host->bh_work, dw_mci_work_func);
  2967. ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
  2968. host->irq_flags, "dw-mci", host);
  2969. if (ret)
  2970. goto err_dmaunmap;
  2971. /*
  2972. * Enable interrupts for command done, data over, data empty,
  2973. * receive ready and error such as transmit, receive timeout, crc error
  2974. */
  2975. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  2976. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  2977. DW_MCI_ERROR_FLAGS);
  2978. /* Enable mci interrupt */
  2979. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  2980. dev_info(host->dev,
  2981. "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
  2982. host->irq, width, fifo_size);
  2983. /* We need at least one slot to succeed */
  2984. ret = dw_mci_init_slot(host);
  2985. if (ret) {
  2986. dev_dbg(host->dev, "slot %d init failed\n", i);
  2987. goto err_dmaunmap;
  2988. }
  2989. /* Now that slots are all setup, we can enable card detect */
  2990. dw_mci_enable_cd(host);
  2991. return 0;
  2992. err_dmaunmap:
  2993. if (host->use_dma && host->dma_ops->exit)
  2994. host->dma_ops->exit(host);
  2995. reset_control_assert(host->pdata->rstc);
  2996. err_clk_ciu:
  2997. clk_disable_unprepare(host->ciu_clk);
  2998. err_clk_biu:
  2999. clk_disable_unprepare(host->biu_clk);
  3000. return ret;
  3001. }
  3002. EXPORT_SYMBOL(dw_mci_probe);
  3003. void dw_mci_remove(struct dw_mci *host)
  3004. {
  3005. dev_dbg(host->dev, "remove slot\n");
  3006. if (host->slot)
  3007. dw_mci_cleanup_slot(host->slot);
  3008. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  3009. mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
  3010. /* disable clock to CIU */
  3011. mci_writel(host, CLKENA, 0);
  3012. mci_writel(host, CLKSRC, 0);
  3013. if (host->use_dma && host->dma_ops->exit)
  3014. host->dma_ops->exit(host);
  3015. reset_control_assert(host->pdata->rstc);
  3016. clk_disable_unprepare(host->ciu_clk);
  3017. clk_disable_unprepare(host->biu_clk);
  3018. }
  3019. EXPORT_SYMBOL(dw_mci_remove);
  3020. #ifdef CONFIG_PM
  3021. int dw_mci_runtime_suspend(struct device *dev)
  3022. {
  3023. struct dw_mci *host = dev_get_drvdata(dev);
  3024. if (host->use_dma && host->dma_ops->exit)
  3025. host->dma_ops->exit(host);
  3026. clk_disable_unprepare(host->ciu_clk);
  3027. if (host->slot &&
  3028. (mmc_host_can_gpio_cd(host->slot->mmc) ||
  3029. !mmc_card_is_removable(host->slot->mmc)))
  3030. clk_disable_unprepare(host->biu_clk);
  3031. return 0;
  3032. }
  3033. EXPORT_SYMBOL(dw_mci_runtime_suspend);
  3034. int dw_mci_runtime_resume(struct device *dev)
  3035. {
  3036. int ret = 0;
  3037. struct dw_mci *host = dev_get_drvdata(dev);
  3038. if (host->slot &&
  3039. (mmc_host_can_gpio_cd(host->slot->mmc) ||
  3040. !mmc_card_is_removable(host->slot->mmc))) {
  3041. ret = clk_prepare_enable(host->biu_clk);
  3042. if (ret)
  3043. return ret;
  3044. }
  3045. ret = clk_prepare_enable(host->ciu_clk);
  3046. if (ret)
  3047. goto err;
  3048. if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
  3049. clk_disable_unprepare(host->ciu_clk);
  3050. ret = -ENODEV;
  3051. goto err;
  3052. }
  3053. if (host->use_dma && host->dma_ops->init)
  3054. host->dma_ops->init(host);
  3055. /*
  3056. * Restore the initial value at FIFOTH register
  3057. * And Invalidate the prev_blksz with zero
  3058. */
  3059. mci_writel(host, FIFOTH, host->fifoth_val);
  3060. host->prev_blksz = 0;
  3061. /* Put in max timeout */
  3062. mci_writel(host, TMOUT, 0xFFFFFFFF);
  3063. mci_writel(host, RINTSTS, 0xFFFFFFFF);
  3064. mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
  3065. SDMMC_INT_TXDR | SDMMC_INT_RXDR |
  3066. DW_MCI_ERROR_FLAGS);
  3067. mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
  3068. if (host->slot && host->slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
  3069. dw_mci_set_ios(host->slot->mmc, &host->slot->mmc->ios);
  3070. /* Force setup bus to guarantee available clock output */
  3071. dw_mci_setup_bus(host->slot, true);
  3072. /* Re-enable SDIO interrupts. */
  3073. if (sdio_irq_claimed(host->slot->mmc))
  3074. __dw_mci_enable_sdio_irq(host->slot, 1);
  3075. /* Now that slots are all setup, we can enable card detect */
  3076. dw_mci_enable_cd(host);
  3077. return 0;
  3078. err:
  3079. if (host->slot &&
  3080. (mmc_host_can_gpio_cd(host->slot->mmc) ||
  3081. !mmc_card_is_removable(host->slot->mmc)))
  3082. clk_disable_unprepare(host->biu_clk);
  3083. return ret;
  3084. }
  3085. EXPORT_SYMBOL(dw_mci_runtime_resume);
  3086. #endif /* CONFIG_PM */
  3087. static int __init dw_mci_init(void)
  3088. {
  3089. pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
  3090. return 0;
  3091. }
  3092. static void __exit dw_mci_exit(void)
  3093. {
  3094. }
  3095. module_init(dw_mci_init);
  3096. module_exit(dw_mci_exit);
  3097. MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
  3098. MODULE_AUTHOR("NXP Semiconductor VietNam");
  3099. MODULE_AUTHOR("Imagination Technologies Ltd");
  3100. MODULE_LICENSE("GPL v2");