sdhci.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099
  1. /*
  2. * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
  3. *
  4. * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * Thanks to the following companies for their support:
  12. *
  13. * - JMicron (hardware and technical support)
  14. */
  15. #include <linux/delay.h>
  16. #include <linux/ktime.h>
  17. #include <linux/highmem.h>
  18. #include <linux/io.h>
  19. #include <linux/module.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/slab.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/sizes.h>
  24. #include <linux/swiotlb.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/of.h>
  28. #include <linux/leds.h>
  29. #include <linux/mmc/mmc.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/card.h>
  32. #include <linux/mmc/sdio.h>
  33. #include <linux/mmc/slot-gpio.h>
  34. #include "sdhci.h"
  35. #define DRIVER_NAME "sdhci"
  36. #define DBG(f, x...) \
  37. pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  38. #define SDHCI_DUMP(f, x...) \
  39. pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  40. #define MAX_TUNING_LOOP 40
  41. static unsigned int debug_quirks = 0;
  42. static unsigned int debug_quirks2;
  43. static void sdhci_finish_data(struct sdhci_host *);
  44. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
  45. void sdhci_dumpregs(struct sdhci_host *host)
  46. {
  47. SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
  48. SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
  49. sdhci_readl(host, SDHCI_DMA_ADDRESS),
  50. sdhci_readw(host, SDHCI_HOST_VERSION));
  51. SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
  52. sdhci_readw(host, SDHCI_BLOCK_SIZE),
  53. sdhci_readw(host, SDHCI_BLOCK_COUNT));
  54. SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
  55. sdhci_readl(host, SDHCI_ARGUMENT),
  56. sdhci_readw(host, SDHCI_TRANSFER_MODE));
  57. SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
  58. sdhci_readl(host, SDHCI_PRESENT_STATE),
  59. sdhci_readb(host, SDHCI_HOST_CONTROL));
  60. SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
  61. sdhci_readb(host, SDHCI_POWER_CONTROL),
  62. sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
  63. SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
  64. sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
  65. sdhci_readw(host, SDHCI_CLOCK_CONTROL));
  66. SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
  67. sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
  68. sdhci_readl(host, SDHCI_INT_STATUS));
  69. SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
  70. sdhci_readl(host, SDHCI_INT_ENABLE),
  71. sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
  72. SDHCI_DUMP("AC12 err: 0x%08x | Slot int: 0x%08x\n",
  73. sdhci_readw(host, SDHCI_ACMD12_ERR),
  74. sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
  75. SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
  76. sdhci_readl(host, SDHCI_CAPABILITIES),
  77. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  78. SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
  79. sdhci_readw(host, SDHCI_COMMAND),
  80. sdhci_readl(host, SDHCI_MAX_CURRENT));
  81. SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
  82. sdhci_readl(host, SDHCI_RESPONSE),
  83. sdhci_readl(host, SDHCI_RESPONSE + 4));
  84. SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
  85. sdhci_readl(host, SDHCI_RESPONSE + 8),
  86. sdhci_readl(host, SDHCI_RESPONSE + 12));
  87. SDHCI_DUMP("Host ctl2: 0x%08x\n",
  88. sdhci_readw(host, SDHCI_HOST_CONTROL2));
  89. if (host->flags & SDHCI_USE_ADMA) {
  90. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  91. SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
  92. sdhci_readl(host, SDHCI_ADMA_ERROR),
  93. sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
  94. sdhci_readl(host, SDHCI_ADMA_ADDRESS));
  95. } else {
  96. SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
  97. sdhci_readl(host, SDHCI_ADMA_ERROR),
  98. sdhci_readl(host, SDHCI_ADMA_ADDRESS));
  99. }
  100. }
  101. SDHCI_DUMP("============================================\n");
  102. }
  103. EXPORT_SYMBOL_GPL(sdhci_dumpregs);
  104. /*****************************************************************************\
  105. * *
  106. * Low level functions *
  107. * *
  108. \*****************************************************************************/
  109. static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
  110. {
  111. return cmd->data || cmd->flags & MMC_RSP_BUSY;
  112. }
  113. static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
  114. {
  115. u32 present;
  116. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
  117. !mmc_card_is_removable(host->mmc))
  118. return;
  119. if (enable) {
  120. present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  121. SDHCI_CARD_PRESENT;
  122. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  123. SDHCI_INT_CARD_INSERT;
  124. } else {
  125. host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
  126. }
  127. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  128. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  129. }
  130. static void sdhci_enable_card_detection(struct sdhci_host *host)
  131. {
  132. sdhci_set_card_detection(host, true);
  133. }
  134. static void sdhci_disable_card_detection(struct sdhci_host *host)
  135. {
  136. sdhci_set_card_detection(host, false);
  137. }
  138. static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
  139. {
  140. if (host->bus_on)
  141. return;
  142. host->bus_on = true;
  143. pm_runtime_get_noresume(host->mmc->parent);
  144. }
  145. static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
  146. {
  147. if (!host->bus_on)
  148. return;
  149. host->bus_on = false;
  150. pm_runtime_put_noidle(host->mmc->parent);
  151. }
  152. void sdhci_reset(struct sdhci_host *host, u8 mask)
  153. {
  154. ktime_t timeout;
  155. sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
  156. if (mask & SDHCI_RESET_ALL) {
  157. host->clock = 0;
  158. /* Reset-all turns off SD Bus Power */
  159. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  160. sdhci_runtime_pm_bus_off(host);
  161. }
  162. /* Wait max 100 ms */
  163. timeout = ktime_add_ms(ktime_get(), 100);
  164. /* hw clears the bit when it's done */
  165. while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
  166. if (ktime_after(ktime_get(), timeout)) {
  167. pr_err("%s: Reset 0x%x never completed.\n",
  168. mmc_hostname(host->mmc), (int)mask);
  169. sdhci_dumpregs(host);
  170. return;
  171. }
  172. udelay(10);
  173. }
  174. }
  175. EXPORT_SYMBOL_GPL(sdhci_reset);
  176. static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
  177. {
  178. if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
  179. struct mmc_host *mmc = host->mmc;
  180. if (!mmc->ops->get_cd(mmc))
  181. return;
  182. }
  183. host->ops->reset(host, mask);
  184. if (mask & SDHCI_RESET_ALL) {
  185. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  186. if (host->ops->enable_dma)
  187. host->ops->enable_dma(host);
  188. }
  189. /* Resetting the controller clears many */
  190. host->preset_enabled = false;
  191. }
  192. }
  193. static void sdhci_set_default_irqs(struct sdhci_host *host)
  194. {
  195. host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
  196. SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
  197. SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
  198. SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
  199. SDHCI_INT_RESPONSE;
  200. if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
  201. host->tuning_mode == SDHCI_TUNING_MODE_3)
  202. host->ier |= SDHCI_INT_RETUNE;
  203. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  204. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  205. }
  206. static void sdhci_init(struct sdhci_host *host, int soft)
  207. {
  208. struct mmc_host *mmc = host->mmc;
  209. if (soft)
  210. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  211. else
  212. sdhci_do_reset(host, SDHCI_RESET_ALL);
  213. sdhci_set_default_irqs(host);
  214. host->cqe_on = false;
  215. if (soft) {
  216. /* force clock reconfiguration */
  217. host->clock = 0;
  218. mmc->ops->set_ios(mmc, &mmc->ios);
  219. }
  220. }
  221. static void sdhci_reinit(struct sdhci_host *host)
  222. {
  223. sdhci_init(host, 0);
  224. sdhci_enable_card_detection(host);
  225. }
  226. static void __sdhci_led_activate(struct sdhci_host *host)
  227. {
  228. u8 ctrl;
  229. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  230. ctrl |= SDHCI_CTRL_LED;
  231. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  232. }
  233. static void __sdhci_led_deactivate(struct sdhci_host *host)
  234. {
  235. u8 ctrl;
  236. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  237. ctrl &= ~SDHCI_CTRL_LED;
  238. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  239. }
  240. #if IS_REACHABLE(CONFIG_LEDS_CLASS)
  241. static void sdhci_led_control(struct led_classdev *led,
  242. enum led_brightness brightness)
  243. {
  244. struct sdhci_host *host = container_of(led, struct sdhci_host, led);
  245. unsigned long flags;
  246. spin_lock_irqsave(&host->lock, flags);
  247. if (host->runtime_suspended)
  248. goto out;
  249. if (brightness == LED_OFF)
  250. __sdhci_led_deactivate(host);
  251. else
  252. __sdhci_led_activate(host);
  253. out:
  254. spin_unlock_irqrestore(&host->lock, flags);
  255. }
  256. static int sdhci_led_register(struct sdhci_host *host)
  257. {
  258. struct mmc_host *mmc = host->mmc;
  259. snprintf(host->led_name, sizeof(host->led_name),
  260. "%s::", mmc_hostname(mmc));
  261. host->led.name = host->led_name;
  262. host->led.brightness = LED_OFF;
  263. host->led.default_trigger = mmc_hostname(mmc);
  264. host->led.brightness_set = sdhci_led_control;
  265. return led_classdev_register(mmc_dev(mmc), &host->led);
  266. }
  267. static void sdhci_led_unregister(struct sdhci_host *host)
  268. {
  269. led_classdev_unregister(&host->led);
  270. }
  271. static inline void sdhci_led_activate(struct sdhci_host *host)
  272. {
  273. }
  274. static inline void sdhci_led_deactivate(struct sdhci_host *host)
  275. {
  276. }
  277. #else
  278. static inline int sdhci_led_register(struct sdhci_host *host)
  279. {
  280. return 0;
  281. }
  282. static inline void sdhci_led_unregister(struct sdhci_host *host)
  283. {
  284. }
  285. static inline void sdhci_led_activate(struct sdhci_host *host)
  286. {
  287. __sdhci_led_activate(host);
  288. }
  289. static inline void sdhci_led_deactivate(struct sdhci_host *host)
  290. {
  291. __sdhci_led_deactivate(host);
  292. }
  293. #endif
  294. /*****************************************************************************\
  295. * *
  296. * Core functions *
  297. * *
  298. \*****************************************************************************/
  299. static void sdhci_read_block_pio(struct sdhci_host *host)
  300. {
  301. unsigned long flags;
  302. size_t blksize, len, chunk;
  303. u32 uninitialized_var(scratch);
  304. u8 *buf;
  305. DBG("PIO reading\n");
  306. blksize = host->data->blksz;
  307. chunk = 0;
  308. local_irq_save(flags);
  309. while (blksize) {
  310. BUG_ON(!sg_miter_next(&host->sg_miter));
  311. len = min(host->sg_miter.length, blksize);
  312. blksize -= len;
  313. host->sg_miter.consumed = len;
  314. buf = host->sg_miter.addr;
  315. while (len) {
  316. if (chunk == 0) {
  317. scratch = sdhci_readl(host, SDHCI_BUFFER);
  318. chunk = 4;
  319. }
  320. *buf = scratch & 0xFF;
  321. buf++;
  322. scratch >>= 8;
  323. chunk--;
  324. len--;
  325. }
  326. }
  327. sg_miter_stop(&host->sg_miter);
  328. local_irq_restore(flags);
  329. }
  330. static void sdhci_write_block_pio(struct sdhci_host *host)
  331. {
  332. unsigned long flags;
  333. size_t blksize, len, chunk;
  334. u32 scratch;
  335. u8 *buf;
  336. DBG("PIO writing\n");
  337. blksize = host->data->blksz;
  338. chunk = 0;
  339. scratch = 0;
  340. local_irq_save(flags);
  341. while (blksize) {
  342. BUG_ON(!sg_miter_next(&host->sg_miter));
  343. len = min(host->sg_miter.length, blksize);
  344. blksize -= len;
  345. host->sg_miter.consumed = len;
  346. buf = host->sg_miter.addr;
  347. while (len) {
  348. scratch |= (u32)*buf << (chunk * 8);
  349. buf++;
  350. chunk++;
  351. len--;
  352. if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
  353. sdhci_writel(host, scratch, SDHCI_BUFFER);
  354. chunk = 0;
  355. scratch = 0;
  356. }
  357. }
  358. }
  359. sg_miter_stop(&host->sg_miter);
  360. local_irq_restore(flags);
  361. }
  362. static void sdhci_transfer_pio(struct sdhci_host *host)
  363. {
  364. u32 mask;
  365. if (host->blocks == 0)
  366. return;
  367. if (host->data->flags & MMC_DATA_READ)
  368. mask = SDHCI_DATA_AVAILABLE;
  369. else
  370. mask = SDHCI_SPACE_AVAILABLE;
  371. /*
  372. * Some controllers (JMicron JMB38x) mess up the buffer bits
  373. * for transfers < 4 bytes. As long as it is just one block,
  374. * we can ignore the bits.
  375. */
  376. if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
  377. (host->data->blocks == 1))
  378. mask = ~0;
  379. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  380. if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
  381. udelay(100);
  382. if (host->data->flags & MMC_DATA_READ)
  383. sdhci_read_block_pio(host);
  384. else
  385. sdhci_write_block_pio(host);
  386. host->blocks--;
  387. if (host->blocks == 0)
  388. break;
  389. }
  390. DBG("PIO transfer complete.\n");
  391. }
  392. static int sdhci_pre_dma_transfer(struct sdhci_host *host,
  393. struct mmc_data *data, int cookie)
  394. {
  395. int sg_count;
  396. /*
  397. * If the data buffers are already mapped, return the previous
  398. * dma_map_sg() result.
  399. */
  400. if (data->host_cookie == COOKIE_PRE_MAPPED)
  401. return data->sg_count;
  402. /* Bounce write requests to the bounce buffer */
  403. if (host->bounce_buffer) {
  404. unsigned int length = data->blksz * data->blocks;
  405. if (length > host->bounce_buffer_size) {
  406. pr_err("%s: asked for transfer of %u bytes exceeds bounce buffer %u bytes\n",
  407. mmc_hostname(host->mmc), length,
  408. host->bounce_buffer_size);
  409. return -EIO;
  410. }
  411. if (mmc_get_dma_dir(data) == DMA_TO_DEVICE) {
  412. /* Copy the data to the bounce buffer */
  413. sg_copy_to_buffer(data->sg, data->sg_len,
  414. host->bounce_buffer,
  415. length);
  416. }
  417. /* Switch ownership to the DMA */
  418. dma_sync_single_for_device(host->mmc->parent,
  419. host->bounce_addr,
  420. host->bounce_buffer_size,
  421. mmc_get_dma_dir(data));
  422. /* Just a dummy value */
  423. sg_count = 1;
  424. } else {
  425. /* Just access the data directly from memory */
  426. sg_count = dma_map_sg(mmc_dev(host->mmc),
  427. data->sg, data->sg_len,
  428. mmc_get_dma_dir(data));
  429. }
  430. if (sg_count == 0)
  431. return -ENOSPC;
  432. data->sg_count = sg_count;
  433. data->host_cookie = cookie;
  434. return sg_count;
  435. }
  436. static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
  437. {
  438. local_irq_save(*flags);
  439. return kmap_atomic(sg_page(sg)) + sg->offset;
  440. }
  441. static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
  442. {
  443. kunmap_atomic(buffer);
  444. local_irq_restore(*flags);
  445. }
  446. static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
  447. dma_addr_t addr, int len, unsigned cmd)
  448. {
  449. struct sdhci_adma2_64_desc *dma_desc = desc;
  450. /* 32-bit and 64-bit descriptors have these members in same position */
  451. dma_desc->cmd = cpu_to_le16(cmd);
  452. dma_desc->len = cpu_to_le16(len);
  453. dma_desc->addr_lo = cpu_to_le32((u32)addr);
  454. if (host->flags & SDHCI_USE_64_BIT_DMA)
  455. dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
  456. }
  457. static void sdhci_adma_mark_end(void *desc)
  458. {
  459. struct sdhci_adma2_64_desc *dma_desc = desc;
  460. /* 32-bit and 64-bit descriptors have 'cmd' in same position */
  461. dma_desc->cmd |= cpu_to_le16(ADMA2_END);
  462. }
  463. static void sdhci_adma_table_pre(struct sdhci_host *host,
  464. struct mmc_data *data, int sg_count)
  465. {
  466. struct scatterlist *sg;
  467. unsigned long flags;
  468. dma_addr_t addr, align_addr;
  469. void *desc, *align;
  470. char *buffer;
  471. int len, offset, i;
  472. /*
  473. * The spec does not specify endianness of descriptor table.
  474. * We currently guess that it is LE.
  475. */
  476. host->sg_count = sg_count;
  477. desc = host->adma_table;
  478. align = host->align_buffer;
  479. align_addr = host->align_addr;
  480. for_each_sg(data->sg, sg, host->sg_count, i) {
  481. addr = sg_dma_address(sg);
  482. len = sg_dma_len(sg);
  483. /*
  484. * The SDHCI specification states that ADMA addresses must
  485. * be 32-bit aligned. If they aren't, then we use a bounce
  486. * buffer for the (up to three) bytes that screw up the
  487. * alignment.
  488. */
  489. offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
  490. SDHCI_ADMA2_MASK;
  491. if (offset) {
  492. if (data->flags & MMC_DATA_WRITE) {
  493. buffer = sdhci_kmap_atomic(sg, &flags);
  494. memcpy(align, buffer, offset);
  495. sdhci_kunmap_atomic(buffer, &flags);
  496. }
  497. /* tran, valid */
  498. sdhci_adma_write_desc(host, desc, align_addr, offset,
  499. ADMA2_TRAN_VALID);
  500. BUG_ON(offset > 65536);
  501. align += SDHCI_ADMA2_ALIGN;
  502. align_addr += SDHCI_ADMA2_ALIGN;
  503. desc += host->desc_sz;
  504. addr += offset;
  505. len -= offset;
  506. }
  507. BUG_ON(len > 65536);
  508. if (len) {
  509. /* tran, valid */
  510. sdhci_adma_write_desc(host, desc, addr, len,
  511. ADMA2_TRAN_VALID);
  512. desc += host->desc_sz;
  513. }
  514. /*
  515. * If this triggers then we have a calculation bug
  516. * somewhere. :/
  517. */
  518. WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
  519. }
  520. if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
  521. /* Mark the last descriptor as the terminating descriptor */
  522. if (desc != host->adma_table) {
  523. desc -= host->desc_sz;
  524. sdhci_adma_mark_end(desc);
  525. }
  526. } else {
  527. /* Add a terminating entry - nop, end, valid */
  528. sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
  529. }
  530. }
  531. static void sdhci_adma_table_post(struct sdhci_host *host,
  532. struct mmc_data *data)
  533. {
  534. struct scatterlist *sg;
  535. int i, size;
  536. void *align;
  537. char *buffer;
  538. unsigned long flags;
  539. if (data->flags & MMC_DATA_READ) {
  540. bool has_unaligned = false;
  541. /* Do a quick scan of the SG list for any unaligned mappings */
  542. for_each_sg(data->sg, sg, host->sg_count, i)
  543. if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
  544. has_unaligned = true;
  545. break;
  546. }
  547. if (has_unaligned) {
  548. dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
  549. data->sg_len, DMA_FROM_DEVICE);
  550. align = host->align_buffer;
  551. for_each_sg(data->sg, sg, host->sg_count, i) {
  552. if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
  553. size = SDHCI_ADMA2_ALIGN -
  554. (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
  555. buffer = sdhci_kmap_atomic(sg, &flags);
  556. memcpy(buffer, align, size);
  557. sdhci_kunmap_atomic(buffer, &flags);
  558. align += SDHCI_ADMA2_ALIGN;
  559. }
  560. }
  561. }
  562. }
  563. }
  564. static u32 sdhci_sdma_address(struct sdhci_host *host)
  565. {
  566. if (host->bounce_buffer)
  567. return host->bounce_addr;
  568. else
  569. return sg_dma_address(host->data->sg);
  570. }
  571. static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  572. {
  573. u8 count;
  574. struct mmc_data *data = cmd->data;
  575. unsigned target_timeout, current_timeout;
  576. /*
  577. * If the host controller provides us with an incorrect timeout
  578. * value, just skip the check and use 0xE. The hardware may take
  579. * longer to time out, but that's much better than having a too-short
  580. * timeout value.
  581. */
  582. if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
  583. return 0xE;
  584. /* Unspecified timeout, assume max */
  585. if (!data && !cmd->busy_timeout)
  586. return 0xE;
  587. /* timeout in us */
  588. if (!data)
  589. target_timeout = cmd->busy_timeout * 1000;
  590. else {
  591. target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
  592. if (host->clock && data->timeout_clks) {
  593. unsigned long long val;
  594. /*
  595. * data->timeout_clks is in units of clock cycles.
  596. * host->clock is in Hz. target_timeout is in us.
  597. * Hence, us = 1000000 * cycles / Hz. Round up.
  598. */
  599. val = 1000000ULL * data->timeout_clks;
  600. if (do_div(val, host->clock))
  601. target_timeout++;
  602. target_timeout += val;
  603. }
  604. }
  605. /*
  606. * Figure out needed cycles.
  607. * We do this in steps in order to fit inside a 32 bit int.
  608. * The first step is the minimum timeout, which will have a
  609. * minimum resolution of 6 bits:
  610. * (1) 2^13*1000 > 2^22,
  611. * (2) host->timeout_clk < 2^16
  612. * =>
  613. * (1) / (2) > 2^6
  614. */
  615. count = 0;
  616. current_timeout = (1 << 13) * 1000 / host->timeout_clk;
  617. while (current_timeout < target_timeout) {
  618. count++;
  619. current_timeout <<= 1;
  620. if (count >= 0xF)
  621. break;
  622. }
  623. if (count >= 0xF) {
  624. DBG("Too large timeout 0x%x requested for CMD%d!\n",
  625. count, cmd->opcode);
  626. count = 0xE;
  627. }
  628. return count;
  629. }
  630. static void sdhci_set_transfer_irqs(struct sdhci_host *host)
  631. {
  632. u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
  633. u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
  634. if (host->flags & SDHCI_REQ_USE_DMA)
  635. host->ier = (host->ier & ~pio_irqs) | dma_irqs;
  636. else
  637. host->ier = (host->ier & ~dma_irqs) | pio_irqs;
  638. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  639. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  640. }
  641. static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  642. {
  643. u8 count;
  644. if (host->ops->set_timeout) {
  645. host->ops->set_timeout(host, cmd);
  646. } else {
  647. count = sdhci_calc_timeout(host, cmd);
  648. sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
  649. }
  650. }
  651. static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
  652. {
  653. u8 ctrl;
  654. struct mmc_data *data = cmd->data;
  655. if (sdhci_data_line_cmd(cmd))
  656. sdhci_set_timeout(host, cmd);
  657. if (!data)
  658. return;
  659. WARN_ON(host->data);
  660. /* Sanity checks */
  661. BUG_ON(data->blksz * data->blocks > 524288);
  662. BUG_ON(data->blksz > host->mmc->max_blk_size);
  663. BUG_ON(data->blocks > 65535);
  664. host->data = data;
  665. host->data_early = 0;
  666. host->data->bytes_xfered = 0;
  667. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  668. struct scatterlist *sg;
  669. unsigned int length_mask, offset_mask;
  670. int i;
  671. host->flags |= SDHCI_REQ_USE_DMA;
  672. /*
  673. * FIXME: This doesn't account for merging when mapping the
  674. * scatterlist.
  675. *
  676. * The assumption here being that alignment and lengths are
  677. * the same after DMA mapping to device address space.
  678. */
  679. length_mask = 0;
  680. offset_mask = 0;
  681. if (host->flags & SDHCI_USE_ADMA) {
  682. if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
  683. length_mask = 3;
  684. /*
  685. * As we use up to 3 byte chunks to work
  686. * around alignment problems, we need to
  687. * check the offset as well.
  688. */
  689. offset_mask = 3;
  690. }
  691. } else {
  692. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
  693. length_mask = 3;
  694. if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
  695. offset_mask = 3;
  696. }
  697. if (unlikely(length_mask | offset_mask)) {
  698. for_each_sg(data->sg, sg, data->sg_len, i) {
  699. if (sg->length & length_mask) {
  700. DBG("Reverting to PIO because of transfer size (%d)\n",
  701. sg->length);
  702. host->flags &= ~SDHCI_REQ_USE_DMA;
  703. break;
  704. }
  705. if (sg->offset & offset_mask) {
  706. DBG("Reverting to PIO because of bad alignment\n");
  707. host->flags &= ~SDHCI_REQ_USE_DMA;
  708. break;
  709. }
  710. }
  711. }
  712. }
  713. if (host->flags & SDHCI_REQ_USE_DMA) {
  714. int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
  715. if (sg_cnt <= 0) {
  716. /*
  717. * This only happens when someone fed
  718. * us an invalid request.
  719. */
  720. WARN_ON(1);
  721. host->flags &= ~SDHCI_REQ_USE_DMA;
  722. } else if (host->flags & SDHCI_USE_ADMA) {
  723. sdhci_adma_table_pre(host, data, sg_cnt);
  724. sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
  725. if (host->flags & SDHCI_USE_64_BIT_DMA)
  726. sdhci_writel(host,
  727. (u64)host->adma_addr >> 32,
  728. SDHCI_ADMA_ADDRESS_HI);
  729. } else {
  730. WARN_ON(sg_cnt != 1);
  731. sdhci_writel(host, sdhci_sdma_address(host),
  732. SDHCI_DMA_ADDRESS);
  733. }
  734. }
  735. /*
  736. * Always adjust the DMA selection as some controllers
  737. * (e.g. JMicron) can't do PIO properly when the selection
  738. * is ADMA.
  739. */
  740. if (host->version >= SDHCI_SPEC_200) {
  741. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  742. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  743. if ((host->flags & SDHCI_REQ_USE_DMA) &&
  744. (host->flags & SDHCI_USE_ADMA)) {
  745. if (host->flags & SDHCI_USE_64_BIT_DMA)
  746. ctrl |= SDHCI_CTRL_ADMA64;
  747. else
  748. ctrl |= SDHCI_CTRL_ADMA32;
  749. } else {
  750. ctrl |= SDHCI_CTRL_SDMA;
  751. }
  752. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  753. }
  754. if (!(host->flags & SDHCI_REQ_USE_DMA)) {
  755. int flags;
  756. flags = SG_MITER_ATOMIC;
  757. if (host->data->flags & MMC_DATA_READ)
  758. flags |= SG_MITER_TO_SG;
  759. else
  760. flags |= SG_MITER_FROM_SG;
  761. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  762. host->blocks = data->blocks;
  763. }
  764. sdhci_set_transfer_irqs(host);
  765. /* Set the DMA boundary value and block size */
  766. sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, data->blksz),
  767. SDHCI_BLOCK_SIZE);
  768. sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
  769. }
  770. static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
  771. struct mmc_request *mrq)
  772. {
  773. return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
  774. !mrq->cap_cmd_during_tfr;
  775. }
  776. static void sdhci_set_transfer_mode(struct sdhci_host *host,
  777. struct mmc_command *cmd)
  778. {
  779. u16 mode = 0;
  780. struct mmc_data *data = cmd->data;
  781. if (data == NULL) {
  782. if (host->quirks2 &
  783. SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
  784. sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
  785. } else {
  786. /* clear Auto CMD settings for no data CMDs */
  787. mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
  788. sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
  789. SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
  790. }
  791. return;
  792. }
  793. WARN_ON(!host->data);
  794. if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
  795. mode = SDHCI_TRNS_BLK_CNT_EN;
  796. if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
  797. mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
  798. /*
  799. * If we are sending CMD23, CMD12 never gets sent
  800. * on successful completion (so no Auto-CMD12).
  801. */
  802. if (sdhci_auto_cmd12(host, cmd->mrq) &&
  803. (cmd->opcode != SD_IO_RW_EXTENDED))
  804. mode |= SDHCI_TRNS_AUTO_CMD12;
  805. else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
  806. mode |= SDHCI_TRNS_AUTO_CMD23;
  807. sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
  808. }
  809. }
  810. if (data->flags & MMC_DATA_READ)
  811. mode |= SDHCI_TRNS_READ;
  812. if (host->flags & SDHCI_REQ_USE_DMA)
  813. mode |= SDHCI_TRNS_DMA;
  814. sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
  815. }
  816. static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
  817. {
  818. return (!(host->flags & SDHCI_DEVICE_DEAD) &&
  819. ((mrq->cmd && mrq->cmd->error) ||
  820. (mrq->sbc && mrq->sbc->error) ||
  821. (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
  822. (mrq->data->stop && mrq->data->stop->error))) ||
  823. (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
  824. }
  825. static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
  826. {
  827. int i;
  828. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  829. if (host->mrqs_done[i] == mrq) {
  830. WARN_ON(1);
  831. return;
  832. }
  833. }
  834. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  835. if (!host->mrqs_done[i]) {
  836. host->mrqs_done[i] = mrq;
  837. break;
  838. }
  839. }
  840. WARN_ON(i >= SDHCI_MAX_MRQS);
  841. tasklet_schedule(&host->finish_tasklet);
  842. }
  843. static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
  844. {
  845. if (host->cmd && host->cmd->mrq == mrq)
  846. host->cmd = NULL;
  847. if (host->data_cmd && host->data_cmd->mrq == mrq)
  848. host->data_cmd = NULL;
  849. if (host->data && host->data->mrq == mrq)
  850. host->data = NULL;
  851. if (sdhci_needs_reset(host, mrq))
  852. host->pending_reset = true;
  853. __sdhci_finish_mrq(host, mrq);
  854. }
  855. static void sdhci_finish_data(struct sdhci_host *host)
  856. {
  857. struct mmc_command *data_cmd = host->data_cmd;
  858. struct mmc_data *data = host->data;
  859. host->data = NULL;
  860. host->data_cmd = NULL;
  861. if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
  862. (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
  863. sdhci_adma_table_post(host, data);
  864. /*
  865. * The specification states that the block count register must
  866. * be updated, but it does not specify at what point in the
  867. * data flow. That makes the register entirely useless to read
  868. * back so we have to assume that nothing made it to the card
  869. * in the event of an error.
  870. */
  871. if (data->error)
  872. data->bytes_xfered = 0;
  873. else
  874. data->bytes_xfered = data->blksz * data->blocks;
  875. /*
  876. * Need to send CMD12 if -
  877. * a) open-ended multiblock transfer (no CMD23)
  878. * b) error in multiblock transfer
  879. */
  880. if (data->stop &&
  881. (data->error ||
  882. !data->mrq->sbc)) {
  883. /*
  884. * The controller needs a reset of internal state machines
  885. * upon error conditions.
  886. */
  887. if (data->error) {
  888. if (!host->cmd || host->cmd == data_cmd)
  889. sdhci_do_reset(host, SDHCI_RESET_CMD);
  890. sdhci_do_reset(host, SDHCI_RESET_DATA);
  891. }
  892. /*
  893. * 'cap_cmd_during_tfr' request must not use the command line
  894. * after mmc_command_done() has been called. It is upper layer's
  895. * responsibility to send the stop command if required.
  896. */
  897. if (data->mrq->cap_cmd_during_tfr) {
  898. sdhci_finish_mrq(host, data->mrq);
  899. } else {
  900. /* Avoid triggering warning in sdhci_send_command() */
  901. host->cmd = NULL;
  902. sdhci_send_command(host, data->stop);
  903. }
  904. } else {
  905. sdhci_finish_mrq(host, data->mrq);
  906. }
  907. }
  908. static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
  909. unsigned long timeout)
  910. {
  911. if (sdhci_data_line_cmd(mrq->cmd))
  912. mod_timer(&host->data_timer, timeout);
  913. else
  914. mod_timer(&host->timer, timeout);
  915. }
  916. static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
  917. {
  918. if (sdhci_data_line_cmd(mrq->cmd))
  919. del_timer(&host->data_timer);
  920. else
  921. del_timer(&host->timer);
  922. }
  923. void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
  924. {
  925. int flags;
  926. u32 mask;
  927. unsigned long timeout;
  928. WARN_ON(host->cmd);
  929. /* Initially, a command has no error */
  930. cmd->error = 0;
  931. if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
  932. cmd->opcode == MMC_STOP_TRANSMISSION)
  933. cmd->flags |= MMC_RSP_BUSY;
  934. /* Wait max 10 ms */
  935. timeout = 10;
  936. mask = SDHCI_CMD_INHIBIT;
  937. if (sdhci_data_line_cmd(cmd))
  938. mask |= SDHCI_DATA_INHIBIT;
  939. /* We shouldn't wait for data inihibit for stop commands, even
  940. though they might use busy signaling */
  941. if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
  942. mask &= ~SDHCI_DATA_INHIBIT;
  943. while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
  944. if (timeout == 0) {
  945. pr_err("%s: Controller never released inhibit bit(s).\n",
  946. mmc_hostname(host->mmc));
  947. sdhci_dumpregs(host);
  948. cmd->error = -EIO;
  949. sdhci_finish_mrq(host, cmd->mrq);
  950. return;
  951. }
  952. timeout--;
  953. mdelay(1);
  954. }
  955. timeout = jiffies;
  956. if (!cmd->data && cmd->busy_timeout > 9000)
  957. timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
  958. else
  959. timeout += 10 * HZ;
  960. sdhci_mod_timer(host, cmd->mrq, timeout);
  961. host->cmd = cmd;
  962. if (sdhci_data_line_cmd(cmd)) {
  963. WARN_ON(host->data_cmd);
  964. host->data_cmd = cmd;
  965. }
  966. sdhci_prepare_data(host, cmd);
  967. sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
  968. sdhci_set_transfer_mode(host, cmd);
  969. if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
  970. pr_err("%s: Unsupported response type!\n",
  971. mmc_hostname(host->mmc));
  972. cmd->error = -EINVAL;
  973. sdhci_finish_mrq(host, cmd->mrq);
  974. return;
  975. }
  976. if (!(cmd->flags & MMC_RSP_PRESENT))
  977. flags = SDHCI_CMD_RESP_NONE;
  978. else if (cmd->flags & MMC_RSP_136)
  979. flags = SDHCI_CMD_RESP_LONG;
  980. else if (cmd->flags & MMC_RSP_BUSY)
  981. flags = SDHCI_CMD_RESP_SHORT_BUSY;
  982. else
  983. flags = SDHCI_CMD_RESP_SHORT;
  984. if (cmd->flags & MMC_RSP_CRC)
  985. flags |= SDHCI_CMD_CRC;
  986. if (cmd->flags & MMC_RSP_OPCODE)
  987. flags |= SDHCI_CMD_INDEX;
  988. /* CMD19 is special in that the Data Present Select should be set */
  989. if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
  990. cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
  991. flags |= SDHCI_CMD_DATA;
  992. sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
  993. }
  994. EXPORT_SYMBOL_GPL(sdhci_send_command);
  995. static void sdhci_read_rsp_136(struct sdhci_host *host, struct mmc_command *cmd)
  996. {
  997. int i, reg;
  998. for (i = 0; i < 4; i++) {
  999. reg = SDHCI_RESPONSE + (3 - i) * 4;
  1000. cmd->resp[i] = sdhci_readl(host, reg);
  1001. }
  1002. if (host->quirks2 & SDHCI_QUIRK2_RSP_136_HAS_CRC)
  1003. return;
  1004. /* CRC is stripped so we need to do some shifting */
  1005. for (i = 0; i < 4; i++) {
  1006. cmd->resp[i] <<= 8;
  1007. if (i != 3)
  1008. cmd->resp[i] |= cmd->resp[i + 1] >> 24;
  1009. }
  1010. }
  1011. static void sdhci_finish_command(struct sdhci_host *host)
  1012. {
  1013. struct mmc_command *cmd = host->cmd;
  1014. host->cmd = NULL;
  1015. if (cmd->flags & MMC_RSP_PRESENT) {
  1016. if (cmd->flags & MMC_RSP_136) {
  1017. sdhci_read_rsp_136(host, cmd);
  1018. } else {
  1019. cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
  1020. }
  1021. }
  1022. if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
  1023. mmc_command_done(host->mmc, cmd->mrq);
  1024. /*
  1025. * The host can send and interrupt when the busy state has
  1026. * ended, allowing us to wait without wasting CPU cycles.
  1027. * The busy signal uses DAT0 so this is similar to waiting
  1028. * for data to complete.
  1029. *
  1030. * Note: The 1.0 specification is a bit ambiguous about this
  1031. * feature so there might be some problems with older
  1032. * controllers.
  1033. */
  1034. if (cmd->flags & MMC_RSP_BUSY) {
  1035. if (cmd->data) {
  1036. DBG("Cannot wait for busy signal when also doing a data transfer");
  1037. } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
  1038. cmd == host->data_cmd) {
  1039. /* Command complete before busy is ended */
  1040. return;
  1041. }
  1042. }
  1043. /* Finished CMD23, now send actual command. */
  1044. if (cmd == cmd->mrq->sbc) {
  1045. sdhci_send_command(host, cmd->mrq->cmd);
  1046. } else {
  1047. /* Processed actual command. */
  1048. if (host->data && host->data_early)
  1049. sdhci_finish_data(host);
  1050. if (!cmd->data)
  1051. sdhci_finish_mrq(host, cmd->mrq);
  1052. }
  1053. }
  1054. static u16 sdhci_get_preset_value(struct sdhci_host *host)
  1055. {
  1056. u16 preset = 0;
  1057. switch (host->timing) {
  1058. case MMC_TIMING_UHS_SDR12:
  1059. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  1060. break;
  1061. case MMC_TIMING_UHS_SDR25:
  1062. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
  1063. break;
  1064. case MMC_TIMING_UHS_SDR50:
  1065. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
  1066. break;
  1067. case MMC_TIMING_UHS_SDR104:
  1068. case MMC_TIMING_MMC_HS200:
  1069. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
  1070. break;
  1071. case MMC_TIMING_UHS_DDR50:
  1072. case MMC_TIMING_MMC_DDR52:
  1073. preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
  1074. break;
  1075. case MMC_TIMING_MMC_HS400:
  1076. preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
  1077. break;
  1078. default:
  1079. pr_warn("%s: Invalid UHS-I mode selected\n",
  1080. mmc_hostname(host->mmc));
  1081. preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
  1082. break;
  1083. }
  1084. return preset;
  1085. }
  1086. u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
  1087. unsigned int *actual_clock)
  1088. {
  1089. int div = 0; /* Initialized for compiler warning */
  1090. int real_div = div, clk_mul = 1;
  1091. u16 clk = 0;
  1092. bool switch_base_clk = false;
  1093. if (host->version >= SDHCI_SPEC_300) {
  1094. if (host->preset_enabled) {
  1095. u16 pre_val;
  1096. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1097. pre_val = sdhci_get_preset_value(host);
  1098. div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
  1099. >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
  1100. if (host->clk_mul &&
  1101. (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
  1102. clk = SDHCI_PROG_CLOCK_MODE;
  1103. real_div = div + 1;
  1104. clk_mul = host->clk_mul;
  1105. } else {
  1106. real_div = max_t(int, 1, div << 1);
  1107. }
  1108. goto clock_set;
  1109. }
  1110. /*
  1111. * Check if the Host Controller supports Programmable Clock
  1112. * Mode.
  1113. */
  1114. if (host->clk_mul) {
  1115. for (div = 1; div <= 1024; div++) {
  1116. if ((host->max_clk * host->clk_mul / div)
  1117. <= clock)
  1118. break;
  1119. }
  1120. if ((host->max_clk * host->clk_mul / div) <= clock) {
  1121. /*
  1122. * Set Programmable Clock Mode in the Clock
  1123. * Control register.
  1124. */
  1125. clk = SDHCI_PROG_CLOCK_MODE;
  1126. real_div = div;
  1127. clk_mul = host->clk_mul;
  1128. div--;
  1129. } else {
  1130. /*
  1131. * Divisor can be too small to reach clock
  1132. * speed requirement. Then use the base clock.
  1133. */
  1134. switch_base_clk = true;
  1135. }
  1136. }
  1137. if (!host->clk_mul || switch_base_clk) {
  1138. /* Version 3.00 divisors must be a multiple of 2. */
  1139. if (host->max_clk <= clock)
  1140. div = 1;
  1141. else {
  1142. for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
  1143. div += 2) {
  1144. if ((host->max_clk / div) <= clock)
  1145. break;
  1146. }
  1147. }
  1148. real_div = div;
  1149. div >>= 1;
  1150. if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
  1151. && !div && host->max_clk <= 25000000)
  1152. div = 1;
  1153. }
  1154. } else {
  1155. /* Version 2.00 divisors must be a power of 2. */
  1156. for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
  1157. if ((host->max_clk / div) <= clock)
  1158. break;
  1159. }
  1160. real_div = div;
  1161. div >>= 1;
  1162. }
  1163. clock_set:
  1164. if (real_div)
  1165. *actual_clock = (host->max_clk * clk_mul) / real_div;
  1166. clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
  1167. clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
  1168. << SDHCI_DIVIDER_HI_SHIFT;
  1169. return clk;
  1170. }
  1171. EXPORT_SYMBOL_GPL(sdhci_calc_clk);
  1172. void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
  1173. {
  1174. ktime_t timeout;
  1175. clk |= SDHCI_CLOCK_INT_EN;
  1176. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1177. /* Wait max 20 ms */
  1178. timeout = ktime_add_ms(ktime_get(), 20);
  1179. while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
  1180. & SDHCI_CLOCK_INT_STABLE)) {
  1181. if (ktime_after(ktime_get(), timeout)) {
  1182. pr_err("%s: Internal clock never stabilised.\n",
  1183. mmc_hostname(host->mmc));
  1184. sdhci_dumpregs(host);
  1185. return;
  1186. }
  1187. udelay(10);
  1188. }
  1189. clk |= SDHCI_CLOCK_CARD_EN;
  1190. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1191. }
  1192. EXPORT_SYMBOL_GPL(sdhci_enable_clk);
  1193. void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
  1194. {
  1195. u16 clk;
  1196. host->mmc->actual_clock = 0;
  1197. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  1198. if (clock == 0)
  1199. return;
  1200. clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
  1201. sdhci_enable_clk(host, clk);
  1202. }
  1203. EXPORT_SYMBOL_GPL(sdhci_set_clock);
  1204. static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
  1205. unsigned short vdd)
  1206. {
  1207. struct mmc_host *mmc = host->mmc;
  1208. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
  1209. if (mode != MMC_POWER_OFF)
  1210. sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
  1211. else
  1212. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1213. }
  1214. void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
  1215. unsigned short vdd)
  1216. {
  1217. u8 pwr = 0;
  1218. if (mode != MMC_POWER_OFF) {
  1219. switch (1 << vdd) {
  1220. case MMC_VDD_165_195:
  1221. /*
  1222. * Without a regulator, SDHCI does not support 2.0v
  1223. * so we only get here if the driver deliberately
  1224. * added the 2.0v range to ocr_avail. Map it to 1.8v
  1225. * for the purpose of turning on the power.
  1226. */
  1227. case MMC_VDD_20_21:
  1228. pwr = SDHCI_POWER_180;
  1229. break;
  1230. case MMC_VDD_29_30:
  1231. case MMC_VDD_30_31:
  1232. pwr = SDHCI_POWER_300;
  1233. break;
  1234. case MMC_VDD_32_33:
  1235. case MMC_VDD_33_34:
  1236. pwr = SDHCI_POWER_330;
  1237. break;
  1238. default:
  1239. WARN(1, "%s: Invalid vdd %#x\n",
  1240. mmc_hostname(host->mmc), vdd);
  1241. break;
  1242. }
  1243. }
  1244. if (host->pwr == pwr)
  1245. return;
  1246. host->pwr = pwr;
  1247. if (pwr == 0) {
  1248. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1249. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1250. sdhci_runtime_pm_bus_off(host);
  1251. } else {
  1252. /*
  1253. * Spec says that we should clear the power reg before setting
  1254. * a new value. Some controllers don't seem to like this though.
  1255. */
  1256. if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
  1257. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1258. /*
  1259. * At least the Marvell CaFe chip gets confused if we set the
  1260. * voltage and set turn on power at the same time, so set the
  1261. * voltage first.
  1262. */
  1263. if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
  1264. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1265. pwr |= SDHCI_POWER_ON;
  1266. sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
  1267. if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
  1268. sdhci_runtime_pm_bus_on(host);
  1269. /*
  1270. * Some controllers need an extra 10ms delay of 10ms before
  1271. * they can apply clock after applying power
  1272. */
  1273. if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
  1274. mdelay(10);
  1275. }
  1276. }
  1277. EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
  1278. void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
  1279. unsigned short vdd)
  1280. {
  1281. if (IS_ERR(host->mmc->supply.vmmc))
  1282. sdhci_set_power_noreg(host, mode, vdd);
  1283. else
  1284. sdhci_set_power_reg(host, mode, vdd);
  1285. }
  1286. EXPORT_SYMBOL_GPL(sdhci_set_power);
  1287. /*****************************************************************************\
  1288. * *
  1289. * MMC callbacks *
  1290. * *
  1291. \*****************************************************************************/
  1292. static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1293. {
  1294. struct sdhci_host *host;
  1295. int present;
  1296. unsigned long flags;
  1297. host = mmc_priv(mmc);
  1298. /* Firstly check card presence */
  1299. present = mmc->ops->get_cd(mmc);
  1300. spin_lock_irqsave(&host->lock, flags);
  1301. sdhci_led_activate(host);
  1302. /*
  1303. * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
  1304. * requests if Auto-CMD12 is enabled.
  1305. */
  1306. if (sdhci_auto_cmd12(host, mrq)) {
  1307. if (mrq->stop) {
  1308. mrq->data->stop = NULL;
  1309. mrq->stop = NULL;
  1310. }
  1311. }
  1312. if (!present || host->flags & SDHCI_DEVICE_DEAD) {
  1313. mrq->cmd->error = -ENOMEDIUM;
  1314. sdhci_finish_mrq(host, mrq);
  1315. } else {
  1316. if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
  1317. sdhci_send_command(host, mrq->sbc);
  1318. else
  1319. sdhci_send_command(host, mrq->cmd);
  1320. }
  1321. mmiowb();
  1322. spin_unlock_irqrestore(&host->lock, flags);
  1323. }
  1324. void sdhci_set_bus_width(struct sdhci_host *host, int width)
  1325. {
  1326. u8 ctrl;
  1327. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1328. if (width == MMC_BUS_WIDTH_8) {
  1329. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1330. ctrl |= SDHCI_CTRL_8BITBUS;
  1331. } else {
  1332. if (host->mmc->caps & MMC_CAP_8_BIT_DATA)
  1333. ctrl &= ~SDHCI_CTRL_8BITBUS;
  1334. if (width == MMC_BUS_WIDTH_4)
  1335. ctrl |= SDHCI_CTRL_4BITBUS;
  1336. else
  1337. ctrl &= ~SDHCI_CTRL_4BITBUS;
  1338. }
  1339. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1340. }
  1341. EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
  1342. void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
  1343. {
  1344. u16 ctrl_2;
  1345. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1346. /* Select Bus Speed Mode for host */
  1347. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1348. if ((timing == MMC_TIMING_MMC_HS200) ||
  1349. (timing == MMC_TIMING_UHS_SDR104))
  1350. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  1351. else if (timing == MMC_TIMING_UHS_SDR12)
  1352. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1353. else if (timing == MMC_TIMING_UHS_SDR25)
  1354. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1355. else if (timing == MMC_TIMING_UHS_SDR50)
  1356. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1357. else if ((timing == MMC_TIMING_UHS_DDR50) ||
  1358. (timing == MMC_TIMING_MMC_DDR52))
  1359. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  1360. else if (timing == MMC_TIMING_MMC_HS400)
  1361. ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
  1362. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1363. }
  1364. EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
  1365. void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1366. {
  1367. struct sdhci_host *host = mmc_priv(mmc);
  1368. u8 ctrl;
  1369. if (ios->power_mode == MMC_POWER_UNDEFINED)
  1370. return;
  1371. if (host->flags & SDHCI_DEVICE_DEAD) {
  1372. if (!IS_ERR(mmc->supply.vmmc) &&
  1373. ios->power_mode == MMC_POWER_OFF)
  1374. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1375. return;
  1376. }
  1377. /*
  1378. * Reset the chip on each power off.
  1379. * Should clear out any weird states.
  1380. */
  1381. if (ios->power_mode == MMC_POWER_OFF) {
  1382. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  1383. sdhci_reinit(host);
  1384. }
  1385. if (host->version >= SDHCI_SPEC_300 &&
  1386. (ios->power_mode == MMC_POWER_UP) &&
  1387. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
  1388. sdhci_enable_preset_value(host, false);
  1389. if (!ios->clock || ios->clock != host->clock) {
  1390. host->ops->set_clock(host, ios->clock);
  1391. host->clock = ios->clock;
  1392. if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
  1393. host->clock) {
  1394. host->timeout_clk = host->mmc->actual_clock ?
  1395. host->mmc->actual_clock / 1000 :
  1396. host->clock / 1000;
  1397. host->mmc->max_busy_timeout =
  1398. host->ops->get_max_timeout_count ?
  1399. host->ops->get_max_timeout_count(host) :
  1400. 1 << 27;
  1401. host->mmc->max_busy_timeout /= host->timeout_clk;
  1402. }
  1403. }
  1404. if (host->ops->set_power)
  1405. host->ops->set_power(host, ios->power_mode, ios->vdd);
  1406. else
  1407. sdhci_set_power(host, ios->power_mode, ios->vdd);
  1408. if (host->ops->platform_send_init_74_clocks)
  1409. host->ops->platform_send_init_74_clocks(host, ios->power_mode);
  1410. host->ops->set_bus_width(host, ios->bus_width);
  1411. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  1412. if (!(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) {
  1413. if (ios->timing == MMC_TIMING_SD_HS ||
  1414. ios->timing == MMC_TIMING_MMC_HS ||
  1415. ios->timing == MMC_TIMING_MMC_HS400 ||
  1416. ios->timing == MMC_TIMING_MMC_HS200 ||
  1417. ios->timing == MMC_TIMING_MMC_DDR52 ||
  1418. ios->timing == MMC_TIMING_UHS_SDR50 ||
  1419. ios->timing == MMC_TIMING_UHS_SDR104 ||
  1420. ios->timing == MMC_TIMING_UHS_DDR50 ||
  1421. ios->timing == MMC_TIMING_UHS_SDR25)
  1422. ctrl |= SDHCI_CTRL_HISPD;
  1423. else
  1424. ctrl &= ~SDHCI_CTRL_HISPD;
  1425. }
  1426. if (host->version >= SDHCI_SPEC_300) {
  1427. u16 clk, ctrl_2;
  1428. if (!host->preset_enabled) {
  1429. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1430. /*
  1431. * We only need to set Driver Strength if the
  1432. * preset value enable is not set.
  1433. */
  1434. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1435. ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
  1436. if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
  1437. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
  1438. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
  1439. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
  1440. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
  1441. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
  1442. else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
  1443. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
  1444. else {
  1445. pr_warn("%s: invalid driver type, default to driver type B\n",
  1446. mmc_hostname(mmc));
  1447. ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
  1448. }
  1449. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1450. } else {
  1451. /*
  1452. * According to SDHC Spec v3.00, if the Preset Value
  1453. * Enable in the Host Control 2 register is set, we
  1454. * need to reset SD Clock Enable before changing High
  1455. * Speed Enable to avoid generating clock gliches.
  1456. */
  1457. /* Reset SD Clock Enable */
  1458. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1459. clk &= ~SDHCI_CLOCK_CARD_EN;
  1460. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1461. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1462. /* Re-enable SD Clock */
  1463. host->ops->set_clock(host, host->clock);
  1464. }
  1465. /* Reset SD Clock Enable */
  1466. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1467. clk &= ~SDHCI_CLOCK_CARD_EN;
  1468. sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
  1469. host->ops->set_uhs_signaling(host, ios->timing);
  1470. host->timing = ios->timing;
  1471. if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
  1472. ((ios->timing == MMC_TIMING_UHS_SDR12) ||
  1473. (ios->timing == MMC_TIMING_UHS_SDR25) ||
  1474. (ios->timing == MMC_TIMING_UHS_SDR50) ||
  1475. (ios->timing == MMC_TIMING_UHS_SDR104) ||
  1476. (ios->timing == MMC_TIMING_UHS_DDR50) ||
  1477. (ios->timing == MMC_TIMING_MMC_DDR52))) {
  1478. u16 preset;
  1479. sdhci_enable_preset_value(host, true);
  1480. preset = sdhci_get_preset_value(host);
  1481. ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
  1482. >> SDHCI_PRESET_DRV_SHIFT;
  1483. }
  1484. /* Re-enable SD Clock */
  1485. host->ops->set_clock(host, host->clock);
  1486. } else
  1487. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  1488. /*
  1489. * Some (ENE) controllers go apeshit on some ios operation,
  1490. * signalling timeout and CRC errors even on CMD0. Resetting
  1491. * it on each ios seems to solve the problem.
  1492. */
  1493. if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
  1494. sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
  1495. mmiowb();
  1496. }
  1497. EXPORT_SYMBOL_GPL(sdhci_set_ios);
  1498. static int sdhci_get_cd(struct mmc_host *mmc)
  1499. {
  1500. struct sdhci_host *host = mmc_priv(mmc);
  1501. int gpio_cd = mmc_gpio_get_cd(mmc);
  1502. if (host->flags & SDHCI_DEVICE_DEAD)
  1503. return 0;
  1504. /* If nonremovable, assume that the card is always present. */
  1505. if (!mmc_card_is_removable(host->mmc))
  1506. return 1;
  1507. /*
  1508. * Try slot gpio detect, if defined it take precedence
  1509. * over build in controller functionality
  1510. */
  1511. if (gpio_cd >= 0)
  1512. return !!gpio_cd;
  1513. /* If polling, assume that the card is always present. */
  1514. if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
  1515. return 1;
  1516. /* Host native card detect */
  1517. return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
  1518. }
  1519. static int sdhci_check_ro(struct sdhci_host *host)
  1520. {
  1521. unsigned long flags;
  1522. int is_readonly;
  1523. spin_lock_irqsave(&host->lock, flags);
  1524. if (host->flags & SDHCI_DEVICE_DEAD)
  1525. is_readonly = 0;
  1526. else if (host->ops->get_ro)
  1527. is_readonly = host->ops->get_ro(host);
  1528. else
  1529. is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
  1530. & SDHCI_WRITE_PROTECT);
  1531. spin_unlock_irqrestore(&host->lock, flags);
  1532. /* This quirk needs to be replaced by a callback-function later */
  1533. return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
  1534. !is_readonly : is_readonly;
  1535. }
  1536. #define SAMPLE_COUNT 5
  1537. static int sdhci_get_ro(struct mmc_host *mmc)
  1538. {
  1539. struct sdhci_host *host = mmc_priv(mmc);
  1540. int i, ro_count;
  1541. if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
  1542. return sdhci_check_ro(host);
  1543. ro_count = 0;
  1544. for (i = 0; i < SAMPLE_COUNT; i++) {
  1545. if (sdhci_check_ro(host)) {
  1546. if (++ro_count > SAMPLE_COUNT / 2)
  1547. return 1;
  1548. }
  1549. msleep(30);
  1550. }
  1551. return 0;
  1552. }
  1553. static void sdhci_hw_reset(struct mmc_host *mmc)
  1554. {
  1555. struct sdhci_host *host = mmc_priv(mmc);
  1556. if (host->ops && host->ops->hw_reset)
  1557. host->ops->hw_reset(host);
  1558. }
  1559. static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
  1560. {
  1561. if (!(host->flags & SDHCI_DEVICE_DEAD)) {
  1562. if (enable)
  1563. host->ier |= SDHCI_INT_CARD_INT;
  1564. else
  1565. host->ier &= ~SDHCI_INT_CARD_INT;
  1566. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  1567. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  1568. mmiowb();
  1569. }
  1570. }
  1571. void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
  1572. {
  1573. struct sdhci_host *host = mmc_priv(mmc);
  1574. unsigned long flags;
  1575. if (enable)
  1576. pm_runtime_get_noresume(host->mmc->parent);
  1577. spin_lock_irqsave(&host->lock, flags);
  1578. if (enable)
  1579. host->flags |= SDHCI_SDIO_IRQ_ENABLED;
  1580. else
  1581. host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
  1582. sdhci_enable_sdio_irq_nolock(host, enable);
  1583. spin_unlock_irqrestore(&host->lock, flags);
  1584. if (!enable)
  1585. pm_runtime_put_noidle(host->mmc->parent);
  1586. }
  1587. EXPORT_SYMBOL_GPL(sdhci_enable_sdio_irq);
  1588. int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
  1589. struct mmc_ios *ios)
  1590. {
  1591. struct sdhci_host *host = mmc_priv(mmc);
  1592. u16 ctrl;
  1593. int ret;
  1594. /*
  1595. * Signal Voltage Switching is only applicable for Host Controllers
  1596. * v3.00 and above.
  1597. */
  1598. if (host->version < SDHCI_SPEC_300)
  1599. return 0;
  1600. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1601. switch (ios->signal_voltage) {
  1602. case MMC_SIGNAL_VOLTAGE_330:
  1603. if (!(host->flags & SDHCI_SIGNALING_330))
  1604. return -EINVAL;
  1605. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  1606. ctrl &= ~SDHCI_CTRL_VDD_180;
  1607. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1608. if (!IS_ERR(mmc->supply.vqmmc)) {
  1609. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1610. if (ret) {
  1611. pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
  1612. mmc_hostname(mmc));
  1613. return -EIO;
  1614. }
  1615. }
  1616. /* Wait for 5ms */
  1617. usleep_range(5000, 5500);
  1618. /* 3.3V regulator output should be stable within 5 ms */
  1619. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1620. if (!(ctrl & SDHCI_CTRL_VDD_180))
  1621. return 0;
  1622. pr_warn("%s: 3.3V regulator output did not became stable\n",
  1623. mmc_hostname(mmc));
  1624. return -EAGAIN;
  1625. case MMC_SIGNAL_VOLTAGE_180:
  1626. if (!(host->flags & SDHCI_SIGNALING_180))
  1627. return -EINVAL;
  1628. if (!IS_ERR(mmc->supply.vqmmc)) {
  1629. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1630. if (ret) {
  1631. pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
  1632. mmc_hostname(mmc));
  1633. return -EIO;
  1634. }
  1635. }
  1636. /*
  1637. * Enable 1.8V Signal Enable in the Host Control2
  1638. * register
  1639. */
  1640. ctrl |= SDHCI_CTRL_VDD_180;
  1641. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1642. /* Some controller need to do more when switching */
  1643. if (host->ops->voltage_switch)
  1644. host->ops->voltage_switch(host);
  1645. /* 1.8V regulator output should be stable within 5 ms */
  1646. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1647. if (ctrl & SDHCI_CTRL_VDD_180)
  1648. return 0;
  1649. pr_warn("%s: 1.8V regulator output did not became stable\n",
  1650. mmc_hostname(mmc));
  1651. return -EAGAIN;
  1652. case MMC_SIGNAL_VOLTAGE_120:
  1653. if (!(host->flags & SDHCI_SIGNALING_120))
  1654. return -EINVAL;
  1655. if (!IS_ERR(mmc->supply.vqmmc)) {
  1656. ret = mmc_regulator_set_vqmmc(mmc, ios);
  1657. if (ret) {
  1658. pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
  1659. mmc_hostname(mmc));
  1660. return -EIO;
  1661. }
  1662. }
  1663. return 0;
  1664. default:
  1665. /* No signal voltage switch required */
  1666. return 0;
  1667. }
  1668. }
  1669. EXPORT_SYMBOL_GPL(sdhci_start_signal_voltage_switch);
  1670. static int sdhci_card_busy(struct mmc_host *mmc)
  1671. {
  1672. struct sdhci_host *host = mmc_priv(mmc);
  1673. u32 present_state;
  1674. /* Check whether DAT[0] is 0 */
  1675. present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
  1676. return !(present_state & SDHCI_DATA_0_LVL_MASK);
  1677. }
  1678. static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
  1679. {
  1680. struct sdhci_host *host = mmc_priv(mmc);
  1681. unsigned long flags;
  1682. spin_lock_irqsave(&host->lock, flags);
  1683. host->flags |= SDHCI_HS400_TUNING;
  1684. spin_unlock_irqrestore(&host->lock, flags);
  1685. return 0;
  1686. }
  1687. static void sdhci_start_tuning(struct sdhci_host *host)
  1688. {
  1689. u16 ctrl;
  1690. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1691. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  1692. if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
  1693. ctrl |= SDHCI_CTRL_TUNED_CLK;
  1694. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1695. /*
  1696. * As per the Host Controller spec v3.00, tuning command
  1697. * generates Buffer Read Ready interrupt, so enable that.
  1698. *
  1699. * Note: The spec clearly says that when tuning sequence
  1700. * is being performed, the controller does not generate
  1701. * interrupts other than Buffer Read Ready interrupt. But
  1702. * to make sure we don't hit a controller bug, we _only_
  1703. * enable Buffer Read Ready interrupt here.
  1704. */
  1705. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
  1706. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
  1707. }
  1708. static void sdhci_end_tuning(struct sdhci_host *host)
  1709. {
  1710. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  1711. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  1712. }
  1713. static void sdhci_reset_tuning(struct sdhci_host *host)
  1714. {
  1715. u16 ctrl;
  1716. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1717. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  1718. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  1719. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1720. }
  1721. static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
  1722. {
  1723. sdhci_reset_tuning(host);
  1724. sdhci_do_reset(host, SDHCI_RESET_CMD);
  1725. sdhci_do_reset(host, SDHCI_RESET_DATA);
  1726. sdhci_end_tuning(host);
  1727. mmc_abort_tuning(host->mmc, opcode);
  1728. }
  1729. /*
  1730. * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
  1731. * tuning command does not have a data payload (or rather the hardware does it
  1732. * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
  1733. * interrupt setup is different to other commands and there is no timeout
  1734. * interrupt so special handling is needed.
  1735. */
  1736. static void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
  1737. {
  1738. struct mmc_host *mmc = host->mmc;
  1739. struct mmc_command cmd = {};
  1740. struct mmc_request mrq = {};
  1741. unsigned long flags;
  1742. u32 b = host->sdma_boundary;
  1743. spin_lock_irqsave(&host->lock, flags);
  1744. cmd.opcode = opcode;
  1745. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  1746. cmd.mrq = &mrq;
  1747. mrq.cmd = &cmd;
  1748. /*
  1749. * In response to CMD19, the card sends 64 bytes of tuning
  1750. * block to the Host Controller. So we set the block size
  1751. * to 64 here.
  1752. */
  1753. if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
  1754. mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  1755. sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 128), SDHCI_BLOCK_SIZE);
  1756. else
  1757. sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 64), SDHCI_BLOCK_SIZE);
  1758. /*
  1759. * The tuning block is sent by the card to the host controller.
  1760. * So we set the TRNS_READ bit in the Transfer Mode register.
  1761. * This also takes care of setting DMA Enable and Multi Block
  1762. * Select in the same register to 0.
  1763. */
  1764. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  1765. sdhci_send_command(host, &cmd);
  1766. host->cmd = NULL;
  1767. sdhci_del_timer(host, &mrq);
  1768. host->tuning_done = 0;
  1769. mmiowb();
  1770. spin_unlock_irqrestore(&host->lock, flags);
  1771. /* Wait for Buffer Read Ready interrupt */
  1772. wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
  1773. msecs_to_jiffies(50));
  1774. }
  1775. static void __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
  1776. {
  1777. int i;
  1778. /*
  1779. * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
  1780. * of loops reaches 40 times.
  1781. */
  1782. for (i = 0; i < MAX_TUNING_LOOP; i++) {
  1783. u16 ctrl;
  1784. sdhci_send_tuning(host, opcode);
  1785. if (!host->tuning_done) {
  1786. pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
  1787. mmc_hostname(host->mmc));
  1788. sdhci_abort_tuning(host, opcode);
  1789. return;
  1790. }
  1791. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1792. if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
  1793. if (ctrl & SDHCI_CTRL_TUNED_CLK)
  1794. return; /* Success! */
  1795. break;
  1796. }
  1797. /* Spec does not require a delay between tuning cycles */
  1798. if (host->tuning_delay > 0)
  1799. mdelay(host->tuning_delay);
  1800. }
  1801. pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
  1802. mmc_hostname(host->mmc));
  1803. sdhci_reset_tuning(host);
  1804. }
  1805. int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1806. {
  1807. struct sdhci_host *host = mmc_priv(mmc);
  1808. int err = 0;
  1809. unsigned int tuning_count = 0;
  1810. bool hs400_tuning;
  1811. hs400_tuning = host->flags & SDHCI_HS400_TUNING;
  1812. if (host->tuning_mode == SDHCI_TUNING_MODE_1)
  1813. tuning_count = host->tuning_count;
  1814. /*
  1815. * The Host Controller needs tuning in case of SDR104 and DDR50
  1816. * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
  1817. * the Capabilities register.
  1818. * If the Host Controller supports the HS200 mode then the
  1819. * tuning function has to be executed.
  1820. */
  1821. switch (host->timing) {
  1822. /* HS400 tuning is done in HS200 mode */
  1823. case MMC_TIMING_MMC_HS400:
  1824. err = -EINVAL;
  1825. goto out;
  1826. case MMC_TIMING_MMC_HS200:
  1827. /*
  1828. * Periodic re-tuning for HS400 is not expected to be needed, so
  1829. * disable it here.
  1830. */
  1831. if (hs400_tuning)
  1832. tuning_count = 0;
  1833. break;
  1834. case MMC_TIMING_UHS_SDR104:
  1835. case MMC_TIMING_UHS_DDR50:
  1836. break;
  1837. case MMC_TIMING_UHS_SDR50:
  1838. if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
  1839. break;
  1840. /* FALLTHROUGH */
  1841. default:
  1842. goto out;
  1843. }
  1844. if (host->ops->platform_execute_tuning) {
  1845. err = host->ops->platform_execute_tuning(host, opcode);
  1846. goto out;
  1847. }
  1848. host->mmc->retune_period = tuning_count;
  1849. if (host->tuning_delay < 0)
  1850. host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK;
  1851. sdhci_start_tuning(host);
  1852. __sdhci_execute_tuning(host, opcode);
  1853. sdhci_end_tuning(host);
  1854. out:
  1855. host->flags &= ~SDHCI_HS400_TUNING;
  1856. return err;
  1857. }
  1858. EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
  1859. static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
  1860. {
  1861. /* Host Controller v3.00 defines preset value registers */
  1862. if (host->version < SDHCI_SPEC_300)
  1863. return;
  1864. /*
  1865. * We only enable or disable Preset Value if they are not already
  1866. * enabled or disabled respectively. Otherwise, we bail out.
  1867. */
  1868. if (host->preset_enabled != enable) {
  1869. u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1870. if (enable)
  1871. ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
  1872. else
  1873. ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
  1874. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1875. if (enable)
  1876. host->flags |= SDHCI_PV_ENABLED;
  1877. else
  1878. host->flags &= ~SDHCI_PV_ENABLED;
  1879. host->preset_enabled = enable;
  1880. }
  1881. }
  1882. static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  1883. int err)
  1884. {
  1885. struct sdhci_host *host = mmc_priv(mmc);
  1886. struct mmc_data *data = mrq->data;
  1887. if (data->host_cookie != COOKIE_UNMAPPED)
  1888. dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
  1889. mmc_get_dma_dir(data));
  1890. data->host_cookie = COOKIE_UNMAPPED;
  1891. }
  1892. static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
  1893. {
  1894. struct sdhci_host *host = mmc_priv(mmc);
  1895. mrq->data->host_cookie = COOKIE_UNMAPPED;
  1896. /*
  1897. * No pre-mapping in the pre hook if we're using the bounce buffer,
  1898. * for that we would need two bounce buffers since one buffer is
  1899. * in flight when this is getting called.
  1900. */
  1901. if (host->flags & SDHCI_REQ_USE_DMA && !host->bounce_buffer)
  1902. sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
  1903. }
  1904. static inline bool sdhci_has_requests(struct sdhci_host *host)
  1905. {
  1906. return host->cmd || host->data_cmd;
  1907. }
  1908. static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
  1909. {
  1910. if (host->data_cmd) {
  1911. host->data_cmd->error = err;
  1912. sdhci_finish_mrq(host, host->data_cmd->mrq);
  1913. }
  1914. if (host->cmd) {
  1915. host->cmd->error = err;
  1916. sdhci_finish_mrq(host, host->cmd->mrq);
  1917. }
  1918. }
  1919. static void sdhci_card_event(struct mmc_host *mmc)
  1920. {
  1921. struct sdhci_host *host = mmc_priv(mmc);
  1922. unsigned long flags;
  1923. int present;
  1924. /* First check if client has provided their own card event */
  1925. if (host->ops->card_event)
  1926. host->ops->card_event(host);
  1927. present = mmc->ops->get_cd(mmc);
  1928. spin_lock_irqsave(&host->lock, flags);
  1929. /* Check sdhci_has_requests() first in case we are runtime suspended */
  1930. if (sdhci_has_requests(host) && !present) {
  1931. pr_err("%s: Card removed during transfer!\n",
  1932. mmc_hostname(host->mmc));
  1933. pr_err("%s: Resetting controller.\n",
  1934. mmc_hostname(host->mmc));
  1935. sdhci_do_reset(host, SDHCI_RESET_CMD);
  1936. sdhci_do_reset(host, SDHCI_RESET_DATA);
  1937. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  1938. }
  1939. spin_unlock_irqrestore(&host->lock, flags);
  1940. }
  1941. static const struct mmc_host_ops sdhci_ops = {
  1942. .request = sdhci_request,
  1943. .post_req = sdhci_post_req,
  1944. .pre_req = sdhci_pre_req,
  1945. .set_ios = sdhci_set_ios,
  1946. .get_cd = sdhci_get_cd,
  1947. .get_ro = sdhci_get_ro,
  1948. .hw_reset = sdhci_hw_reset,
  1949. .enable_sdio_irq = sdhci_enable_sdio_irq,
  1950. .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
  1951. .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
  1952. .execute_tuning = sdhci_execute_tuning,
  1953. .card_event = sdhci_card_event,
  1954. .card_busy = sdhci_card_busy,
  1955. };
  1956. /*****************************************************************************\
  1957. * *
  1958. * Tasklets *
  1959. * *
  1960. \*****************************************************************************/
  1961. static bool sdhci_request_done(struct sdhci_host *host)
  1962. {
  1963. unsigned long flags;
  1964. struct mmc_request *mrq;
  1965. int i;
  1966. spin_lock_irqsave(&host->lock, flags);
  1967. for (i = 0; i < SDHCI_MAX_MRQS; i++) {
  1968. mrq = host->mrqs_done[i];
  1969. if (mrq)
  1970. break;
  1971. }
  1972. if (!mrq) {
  1973. spin_unlock_irqrestore(&host->lock, flags);
  1974. return true;
  1975. }
  1976. sdhci_del_timer(host, mrq);
  1977. /*
  1978. * Always unmap the data buffers if they were mapped by
  1979. * sdhci_prepare_data() whenever we finish with a request.
  1980. * This avoids leaking DMA mappings on error.
  1981. */
  1982. if (host->flags & SDHCI_REQ_USE_DMA) {
  1983. struct mmc_data *data = mrq->data;
  1984. if (data && data->host_cookie == COOKIE_MAPPED) {
  1985. if (host->bounce_buffer) {
  1986. /*
  1987. * On reads, copy the bounced data into the
  1988. * sglist
  1989. */
  1990. if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE) {
  1991. unsigned int length = data->bytes_xfered;
  1992. if (length > host->bounce_buffer_size) {
  1993. pr_err("%s: bounce buffer is %u bytes but DMA claims to have transferred %u bytes\n",
  1994. mmc_hostname(host->mmc),
  1995. host->bounce_buffer_size,
  1996. data->bytes_xfered);
  1997. /* Cap it down and continue */
  1998. length = host->bounce_buffer_size;
  1999. }
  2000. dma_sync_single_for_cpu(
  2001. host->mmc->parent,
  2002. host->bounce_addr,
  2003. host->bounce_buffer_size,
  2004. DMA_FROM_DEVICE);
  2005. sg_copy_from_buffer(data->sg,
  2006. data->sg_len,
  2007. host->bounce_buffer,
  2008. length);
  2009. } else {
  2010. /* No copying, just switch ownership */
  2011. dma_sync_single_for_cpu(
  2012. host->mmc->parent,
  2013. host->bounce_addr,
  2014. host->bounce_buffer_size,
  2015. mmc_get_dma_dir(data));
  2016. }
  2017. } else {
  2018. /* Unmap the raw data */
  2019. dma_unmap_sg(mmc_dev(host->mmc), data->sg,
  2020. data->sg_len,
  2021. mmc_get_dma_dir(data));
  2022. }
  2023. data->host_cookie = COOKIE_UNMAPPED;
  2024. }
  2025. }
  2026. /*
  2027. * The controller needs a reset of internal state machines
  2028. * upon error conditions.
  2029. */
  2030. if (sdhci_needs_reset(host, mrq)) {
  2031. /*
  2032. * Do not finish until command and data lines are available for
  2033. * reset. Note there can only be one other mrq, so it cannot
  2034. * also be in mrqs_done, otherwise host->cmd and host->data_cmd
  2035. * would both be null.
  2036. */
  2037. if (host->cmd || host->data_cmd) {
  2038. spin_unlock_irqrestore(&host->lock, flags);
  2039. return true;
  2040. }
  2041. /* Some controllers need this kick or reset won't work here */
  2042. if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
  2043. /* This is to force an update */
  2044. host->ops->set_clock(host, host->clock);
  2045. /* Spec says we should do both at the same time, but Ricoh
  2046. controllers do not like that. */
  2047. sdhci_do_reset(host, SDHCI_RESET_CMD);
  2048. sdhci_do_reset(host, SDHCI_RESET_DATA);
  2049. host->pending_reset = false;
  2050. }
  2051. if (!sdhci_has_requests(host))
  2052. sdhci_led_deactivate(host);
  2053. host->mrqs_done[i] = NULL;
  2054. mmiowb();
  2055. spin_unlock_irqrestore(&host->lock, flags);
  2056. mmc_request_done(host->mmc, mrq);
  2057. return false;
  2058. }
  2059. static void sdhci_tasklet_finish(unsigned long param)
  2060. {
  2061. struct sdhci_host *host = (struct sdhci_host *)param;
  2062. while (!sdhci_request_done(host))
  2063. ;
  2064. }
  2065. static void sdhci_timeout_timer(struct timer_list *t)
  2066. {
  2067. struct sdhci_host *host;
  2068. unsigned long flags;
  2069. host = from_timer(host, t, timer);
  2070. spin_lock_irqsave(&host->lock, flags);
  2071. if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
  2072. pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
  2073. mmc_hostname(host->mmc));
  2074. sdhci_dumpregs(host);
  2075. host->cmd->error = -ETIMEDOUT;
  2076. sdhci_finish_mrq(host, host->cmd->mrq);
  2077. }
  2078. mmiowb();
  2079. spin_unlock_irqrestore(&host->lock, flags);
  2080. }
  2081. static void sdhci_timeout_data_timer(struct timer_list *t)
  2082. {
  2083. struct sdhci_host *host;
  2084. unsigned long flags;
  2085. host = from_timer(host, t, data_timer);
  2086. spin_lock_irqsave(&host->lock, flags);
  2087. if (host->data || host->data_cmd ||
  2088. (host->cmd && sdhci_data_line_cmd(host->cmd))) {
  2089. pr_err("%s: Timeout waiting for hardware interrupt.\n",
  2090. mmc_hostname(host->mmc));
  2091. sdhci_dumpregs(host);
  2092. if (host->data) {
  2093. host->data->error = -ETIMEDOUT;
  2094. sdhci_finish_data(host);
  2095. } else if (host->data_cmd) {
  2096. host->data_cmd->error = -ETIMEDOUT;
  2097. sdhci_finish_mrq(host, host->data_cmd->mrq);
  2098. } else {
  2099. host->cmd->error = -ETIMEDOUT;
  2100. sdhci_finish_mrq(host, host->cmd->mrq);
  2101. }
  2102. }
  2103. mmiowb();
  2104. spin_unlock_irqrestore(&host->lock, flags);
  2105. }
  2106. /*****************************************************************************\
  2107. * *
  2108. * Interrupt handling *
  2109. * *
  2110. \*****************************************************************************/
  2111. static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
  2112. {
  2113. if (!host->cmd) {
  2114. /*
  2115. * SDHCI recovers from errors by resetting the cmd and data
  2116. * circuits. Until that is done, there very well might be more
  2117. * interrupts, so ignore them in that case.
  2118. */
  2119. if (host->pending_reset)
  2120. return;
  2121. pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
  2122. mmc_hostname(host->mmc), (unsigned)intmask);
  2123. sdhci_dumpregs(host);
  2124. return;
  2125. }
  2126. if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
  2127. SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
  2128. if (intmask & SDHCI_INT_TIMEOUT)
  2129. host->cmd->error = -ETIMEDOUT;
  2130. else
  2131. host->cmd->error = -EILSEQ;
  2132. /*
  2133. * If this command initiates a data phase and a response
  2134. * CRC error is signalled, the card can start transferring
  2135. * data - the card may have received the command without
  2136. * error. We must not terminate the mmc_request early.
  2137. *
  2138. * If the card did not receive the command or returned an
  2139. * error which prevented it sending data, the data phase
  2140. * will time out.
  2141. */
  2142. if (host->cmd->data &&
  2143. (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
  2144. SDHCI_INT_CRC) {
  2145. host->cmd = NULL;
  2146. return;
  2147. }
  2148. sdhci_finish_mrq(host, host->cmd->mrq);
  2149. return;
  2150. }
  2151. if (intmask & SDHCI_INT_RESPONSE)
  2152. sdhci_finish_command(host);
  2153. }
  2154. static void sdhci_adma_show_error(struct sdhci_host *host)
  2155. {
  2156. void *desc = host->adma_table;
  2157. sdhci_dumpregs(host);
  2158. while (true) {
  2159. struct sdhci_adma2_64_desc *dma_desc = desc;
  2160. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2161. DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2162. desc, le32_to_cpu(dma_desc->addr_hi),
  2163. le32_to_cpu(dma_desc->addr_lo),
  2164. le16_to_cpu(dma_desc->len),
  2165. le16_to_cpu(dma_desc->cmd));
  2166. else
  2167. DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
  2168. desc, le32_to_cpu(dma_desc->addr_lo),
  2169. le16_to_cpu(dma_desc->len),
  2170. le16_to_cpu(dma_desc->cmd));
  2171. desc += host->desc_sz;
  2172. if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
  2173. break;
  2174. }
  2175. }
  2176. static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
  2177. {
  2178. u32 command;
  2179. /* CMD19 generates _only_ Buffer Read Ready interrupt */
  2180. if (intmask & SDHCI_INT_DATA_AVAIL) {
  2181. command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
  2182. if (command == MMC_SEND_TUNING_BLOCK ||
  2183. command == MMC_SEND_TUNING_BLOCK_HS200) {
  2184. host->tuning_done = 1;
  2185. wake_up(&host->buf_ready_int);
  2186. return;
  2187. }
  2188. }
  2189. if (!host->data) {
  2190. struct mmc_command *data_cmd = host->data_cmd;
  2191. /*
  2192. * The "data complete" interrupt is also used to
  2193. * indicate that a busy state has ended. See comment
  2194. * above in sdhci_cmd_irq().
  2195. */
  2196. if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
  2197. if (intmask & SDHCI_INT_DATA_TIMEOUT) {
  2198. host->data_cmd = NULL;
  2199. data_cmd->error = -ETIMEDOUT;
  2200. sdhci_finish_mrq(host, data_cmd->mrq);
  2201. return;
  2202. }
  2203. if (intmask & SDHCI_INT_DATA_END) {
  2204. host->data_cmd = NULL;
  2205. /*
  2206. * Some cards handle busy-end interrupt
  2207. * before the command completed, so make
  2208. * sure we do things in the proper order.
  2209. */
  2210. if (host->cmd == data_cmd)
  2211. return;
  2212. sdhci_finish_mrq(host, data_cmd->mrq);
  2213. return;
  2214. }
  2215. }
  2216. /*
  2217. * SDHCI recovers from errors by resetting the cmd and data
  2218. * circuits. Until that is done, there very well might be more
  2219. * interrupts, so ignore them in that case.
  2220. */
  2221. if (host->pending_reset)
  2222. return;
  2223. pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
  2224. mmc_hostname(host->mmc), (unsigned)intmask);
  2225. sdhci_dumpregs(host);
  2226. return;
  2227. }
  2228. if (intmask & SDHCI_INT_DATA_TIMEOUT)
  2229. host->data->error = -ETIMEDOUT;
  2230. else if (intmask & SDHCI_INT_DATA_END_BIT)
  2231. host->data->error = -EILSEQ;
  2232. else if ((intmask & SDHCI_INT_DATA_CRC) &&
  2233. SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
  2234. != MMC_BUS_TEST_R)
  2235. host->data->error = -EILSEQ;
  2236. else if (intmask & SDHCI_INT_ADMA_ERROR) {
  2237. pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
  2238. sdhci_adma_show_error(host);
  2239. host->data->error = -EIO;
  2240. if (host->ops->adma_workaround)
  2241. host->ops->adma_workaround(host, intmask);
  2242. }
  2243. if (host->data->error)
  2244. sdhci_finish_data(host);
  2245. else {
  2246. if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
  2247. sdhci_transfer_pio(host);
  2248. /*
  2249. * We currently don't do anything fancy with DMA
  2250. * boundaries, but as we can't disable the feature
  2251. * we need to at least restart the transfer.
  2252. *
  2253. * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
  2254. * should return a valid address to continue from, but as
  2255. * some controllers are faulty, don't trust them.
  2256. */
  2257. if (intmask & SDHCI_INT_DMA_END) {
  2258. u32 dmastart, dmanow;
  2259. dmastart = sdhci_sdma_address(host);
  2260. dmanow = dmastart + host->data->bytes_xfered;
  2261. /*
  2262. * Force update to the next DMA block boundary.
  2263. */
  2264. dmanow = (dmanow &
  2265. ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
  2266. SDHCI_DEFAULT_BOUNDARY_SIZE;
  2267. host->data->bytes_xfered = dmanow - dmastart;
  2268. DBG("DMA base 0x%08x, transferred 0x%06x bytes, next 0x%08x\n",
  2269. dmastart, host->data->bytes_xfered, dmanow);
  2270. sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
  2271. }
  2272. if (intmask & SDHCI_INT_DATA_END) {
  2273. if (host->cmd == host->data_cmd) {
  2274. /*
  2275. * Data managed to finish before the
  2276. * command completed. Make sure we do
  2277. * things in the proper order.
  2278. */
  2279. host->data_early = 1;
  2280. } else {
  2281. sdhci_finish_data(host);
  2282. }
  2283. }
  2284. }
  2285. }
  2286. static irqreturn_t sdhci_irq(int irq, void *dev_id)
  2287. {
  2288. irqreturn_t result = IRQ_NONE;
  2289. struct sdhci_host *host = dev_id;
  2290. u32 intmask, mask, unexpected = 0;
  2291. int max_loops = 16;
  2292. spin_lock(&host->lock);
  2293. if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
  2294. spin_unlock(&host->lock);
  2295. return IRQ_NONE;
  2296. }
  2297. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2298. if (!intmask || intmask == 0xffffffff) {
  2299. result = IRQ_NONE;
  2300. goto out;
  2301. }
  2302. do {
  2303. DBG("IRQ status 0x%08x\n", intmask);
  2304. if (host->ops->irq) {
  2305. intmask = host->ops->irq(host, intmask);
  2306. if (!intmask)
  2307. goto cont;
  2308. }
  2309. /* Clear selected interrupts. */
  2310. mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  2311. SDHCI_INT_BUS_POWER);
  2312. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  2313. if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2314. u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
  2315. SDHCI_CARD_PRESENT;
  2316. /*
  2317. * There is a observation on i.mx esdhc. INSERT
  2318. * bit will be immediately set again when it gets
  2319. * cleared, if a card is inserted. We have to mask
  2320. * the irq to prevent interrupt storm which will
  2321. * freeze the system. And the REMOVE gets the
  2322. * same situation.
  2323. *
  2324. * More testing are needed here to ensure it works
  2325. * for other platforms though.
  2326. */
  2327. host->ier &= ~(SDHCI_INT_CARD_INSERT |
  2328. SDHCI_INT_CARD_REMOVE);
  2329. host->ier |= present ? SDHCI_INT_CARD_REMOVE :
  2330. SDHCI_INT_CARD_INSERT;
  2331. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2332. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2333. sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
  2334. SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
  2335. host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
  2336. SDHCI_INT_CARD_REMOVE);
  2337. result = IRQ_WAKE_THREAD;
  2338. }
  2339. if (intmask & SDHCI_INT_CMD_MASK)
  2340. sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
  2341. if (intmask & SDHCI_INT_DATA_MASK)
  2342. sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
  2343. if (intmask & SDHCI_INT_BUS_POWER)
  2344. pr_err("%s: Card is consuming too much power!\n",
  2345. mmc_hostname(host->mmc));
  2346. if (intmask & SDHCI_INT_RETUNE)
  2347. mmc_retune_needed(host->mmc);
  2348. if ((intmask & SDHCI_INT_CARD_INT) &&
  2349. (host->ier & SDHCI_INT_CARD_INT)) {
  2350. sdhci_enable_sdio_irq_nolock(host, false);
  2351. host->thread_isr |= SDHCI_INT_CARD_INT;
  2352. result = IRQ_WAKE_THREAD;
  2353. }
  2354. intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
  2355. SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
  2356. SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
  2357. SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
  2358. if (intmask) {
  2359. unexpected |= intmask;
  2360. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  2361. }
  2362. cont:
  2363. if (result == IRQ_NONE)
  2364. result = IRQ_HANDLED;
  2365. intmask = sdhci_readl(host, SDHCI_INT_STATUS);
  2366. } while (intmask && --max_loops);
  2367. out:
  2368. spin_unlock(&host->lock);
  2369. if (unexpected) {
  2370. pr_err("%s: Unexpected interrupt 0x%08x.\n",
  2371. mmc_hostname(host->mmc), unexpected);
  2372. sdhci_dumpregs(host);
  2373. }
  2374. return result;
  2375. }
  2376. static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
  2377. {
  2378. struct sdhci_host *host = dev_id;
  2379. unsigned long flags;
  2380. u32 isr;
  2381. spin_lock_irqsave(&host->lock, flags);
  2382. isr = host->thread_isr;
  2383. host->thread_isr = 0;
  2384. spin_unlock_irqrestore(&host->lock, flags);
  2385. if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
  2386. struct mmc_host *mmc = host->mmc;
  2387. mmc->ops->card_event(mmc);
  2388. mmc_detect_change(mmc, msecs_to_jiffies(200));
  2389. }
  2390. if (isr & SDHCI_INT_CARD_INT) {
  2391. sdio_run_irqs(host->mmc);
  2392. spin_lock_irqsave(&host->lock, flags);
  2393. if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  2394. sdhci_enable_sdio_irq_nolock(host, true);
  2395. spin_unlock_irqrestore(&host->lock, flags);
  2396. }
  2397. return isr ? IRQ_HANDLED : IRQ_NONE;
  2398. }
  2399. /*****************************************************************************\
  2400. * *
  2401. * Suspend/resume *
  2402. * *
  2403. \*****************************************************************************/
  2404. #ifdef CONFIG_PM
  2405. static bool sdhci_cd_irq_can_wakeup(struct sdhci_host *host)
  2406. {
  2407. return mmc_card_is_removable(host->mmc) &&
  2408. !(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  2409. !mmc_can_gpio_cd(host->mmc);
  2410. }
  2411. /*
  2412. * To enable wakeup events, the corresponding events have to be enabled in
  2413. * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
  2414. * Table' in the SD Host Controller Standard Specification.
  2415. * It is useless to restore SDHCI_INT_ENABLE state in
  2416. * sdhci_disable_irq_wakeups() since it will be set by
  2417. * sdhci_enable_card_detection() or sdhci_init().
  2418. */
  2419. static bool sdhci_enable_irq_wakeups(struct sdhci_host *host)
  2420. {
  2421. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
  2422. SDHCI_WAKE_ON_INT;
  2423. u32 irq_val = 0;
  2424. u8 wake_val = 0;
  2425. u8 val;
  2426. if (sdhci_cd_irq_can_wakeup(host)) {
  2427. wake_val |= SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE;
  2428. irq_val |= SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE;
  2429. }
  2430. if (mmc_card_wake_sdio_irq(host->mmc)) {
  2431. wake_val |= SDHCI_WAKE_ON_INT;
  2432. irq_val |= SDHCI_INT_CARD_INT;
  2433. }
  2434. if (!irq_val)
  2435. return false;
  2436. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2437. val &= ~mask;
  2438. val |= wake_val;
  2439. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2440. sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
  2441. host->irq_wake_enabled = !enable_irq_wake(host->irq);
  2442. return host->irq_wake_enabled;
  2443. }
  2444. static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
  2445. {
  2446. u8 val;
  2447. u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
  2448. | SDHCI_WAKE_ON_INT;
  2449. val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
  2450. val &= ~mask;
  2451. sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
  2452. disable_irq_wake(host->irq);
  2453. host->irq_wake_enabled = false;
  2454. }
  2455. int sdhci_suspend_host(struct sdhci_host *host)
  2456. {
  2457. sdhci_disable_card_detection(host);
  2458. mmc_retune_timer_stop(host->mmc);
  2459. if (!device_may_wakeup(mmc_dev(host->mmc)) ||
  2460. !sdhci_enable_irq_wakeups(host)) {
  2461. host->ier = 0;
  2462. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  2463. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  2464. free_irq(host->irq, host);
  2465. }
  2466. return 0;
  2467. }
  2468. EXPORT_SYMBOL_GPL(sdhci_suspend_host);
  2469. int sdhci_resume_host(struct sdhci_host *host)
  2470. {
  2471. struct mmc_host *mmc = host->mmc;
  2472. int ret = 0;
  2473. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2474. if (host->ops->enable_dma)
  2475. host->ops->enable_dma(host);
  2476. }
  2477. if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
  2478. (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
  2479. /* Card keeps power but host controller does not */
  2480. sdhci_init(host, 0);
  2481. host->pwr = 0;
  2482. host->clock = 0;
  2483. mmc->ops->set_ios(mmc, &mmc->ios);
  2484. } else {
  2485. sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
  2486. mmiowb();
  2487. }
  2488. if (host->irq_wake_enabled) {
  2489. sdhci_disable_irq_wakeups(host);
  2490. } else {
  2491. ret = request_threaded_irq(host->irq, sdhci_irq,
  2492. sdhci_thread_irq, IRQF_SHARED,
  2493. mmc_hostname(host->mmc), host);
  2494. if (ret)
  2495. return ret;
  2496. }
  2497. sdhci_enable_card_detection(host);
  2498. return ret;
  2499. }
  2500. EXPORT_SYMBOL_GPL(sdhci_resume_host);
  2501. int sdhci_runtime_suspend_host(struct sdhci_host *host)
  2502. {
  2503. unsigned long flags;
  2504. mmc_retune_timer_stop(host->mmc);
  2505. spin_lock_irqsave(&host->lock, flags);
  2506. host->ier &= SDHCI_INT_CARD_INT;
  2507. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2508. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2509. spin_unlock_irqrestore(&host->lock, flags);
  2510. synchronize_hardirq(host->irq);
  2511. spin_lock_irqsave(&host->lock, flags);
  2512. host->runtime_suspended = true;
  2513. spin_unlock_irqrestore(&host->lock, flags);
  2514. return 0;
  2515. }
  2516. EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
  2517. int sdhci_runtime_resume_host(struct sdhci_host *host)
  2518. {
  2519. struct mmc_host *mmc = host->mmc;
  2520. unsigned long flags;
  2521. int host_flags = host->flags;
  2522. if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2523. if (host->ops->enable_dma)
  2524. host->ops->enable_dma(host);
  2525. }
  2526. sdhci_init(host, 0);
  2527. if (mmc->ios.power_mode != MMC_POWER_UNDEFINED &&
  2528. mmc->ios.power_mode != MMC_POWER_OFF) {
  2529. /* Force clock and power re-program */
  2530. host->pwr = 0;
  2531. host->clock = 0;
  2532. mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
  2533. mmc->ops->set_ios(mmc, &mmc->ios);
  2534. if ((host_flags & SDHCI_PV_ENABLED) &&
  2535. !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
  2536. spin_lock_irqsave(&host->lock, flags);
  2537. sdhci_enable_preset_value(host, true);
  2538. spin_unlock_irqrestore(&host->lock, flags);
  2539. }
  2540. if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
  2541. mmc->ops->hs400_enhanced_strobe)
  2542. mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
  2543. }
  2544. spin_lock_irqsave(&host->lock, flags);
  2545. host->runtime_suspended = false;
  2546. /* Enable SDIO IRQ */
  2547. if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
  2548. sdhci_enable_sdio_irq_nolock(host, true);
  2549. /* Enable Card Detection */
  2550. sdhci_enable_card_detection(host);
  2551. spin_unlock_irqrestore(&host->lock, flags);
  2552. return 0;
  2553. }
  2554. EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
  2555. #endif /* CONFIG_PM */
  2556. /*****************************************************************************\
  2557. * *
  2558. * Command Queue Engine (CQE) helpers *
  2559. * *
  2560. \*****************************************************************************/
  2561. void sdhci_cqe_enable(struct mmc_host *mmc)
  2562. {
  2563. struct sdhci_host *host = mmc_priv(mmc);
  2564. unsigned long flags;
  2565. u8 ctrl;
  2566. spin_lock_irqsave(&host->lock, flags);
  2567. ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
  2568. ctrl &= ~SDHCI_CTRL_DMA_MASK;
  2569. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2570. ctrl |= SDHCI_CTRL_ADMA64;
  2571. else
  2572. ctrl |= SDHCI_CTRL_ADMA32;
  2573. sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
  2574. sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, 512),
  2575. SDHCI_BLOCK_SIZE);
  2576. /* Set maximum timeout */
  2577. sdhci_writeb(host, 0xE, SDHCI_TIMEOUT_CONTROL);
  2578. host->ier = host->cqe_ier;
  2579. sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
  2580. sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
  2581. host->cqe_on = true;
  2582. pr_debug("%s: sdhci: CQE on, IRQ mask %#x, IRQ status %#x\n",
  2583. mmc_hostname(mmc), host->ier,
  2584. sdhci_readl(host, SDHCI_INT_STATUS));
  2585. mmiowb();
  2586. spin_unlock_irqrestore(&host->lock, flags);
  2587. }
  2588. EXPORT_SYMBOL_GPL(sdhci_cqe_enable);
  2589. void sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
  2590. {
  2591. struct sdhci_host *host = mmc_priv(mmc);
  2592. unsigned long flags;
  2593. spin_lock_irqsave(&host->lock, flags);
  2594. sdhci_set_default_irqs(host);
  2595. host->cqe_on = false;
  2596. if (recovery) {
  2597. sdhci_do_reset(host, SDHCI_RESET_CMD);
  2598. sdhci_do_reset(host, SDHCI_RESET_DATA);
  2599. }
  2600. pr_debug("%s: sdhci: CQE off, IRQ mask %#x, IRQ status %#x\n",
  2601. mmc_hostname(mmc), host->ier,
  2602. sdhci_readl(host, SDHCI_INT_STATUS));
  2603. mmiowb();
  2604. spin_unlock_irqrestore(&host->lock, flags);
  2605. }
  2606. EXPORT_SYMBOL_GPL(sdhci_cqe_disable);
  2607. bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error,
  2608. int *data_error)
  2609. {
  2610. u32 mask;
  2611. if (!host->cqe_on)
  2612. return false;
  2613. if (intmask & (SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC))
  2614. *cmd_error = -EILSEQ;
  2615. else if (intmask & SDHCI_INT_TIMEOUT)
  2616. *cmd_error = -ETIMEDOUT;
  2617. else
  2618. *cmd_error = 0;
  2619. if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
  2620. *data_error = -EILSEQ;
  2621. else if (intmask & SDHCI_INT_DATA_TIMEOUT)
  2622. *data_error = -ETIMEDOUT;
  2623. else if (intmask & SDHCI_INT_ADMA_ERROR)
  2624. *data_error = -EIO;
  2625. else
  2626. *data_error = 0;
  2627. /* Clear selected interrupts. */
  2628. mask = intmask & host->cqe_ier;
  2629. sdhci_writel(host, mask, SDHCI_INT_STATUS);
  2630. if (intmask & SDHCI_INT_BUS_POWER)
  2631. pr_err("%s: Card is consuming too much power!\n",
  2632. mmc_hostname(host->mmc));
  2633. intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR);
  2634. if (intmask) {
  2635. sdhci_writel(host, intmask, SDHCI_INT_STATUS);
  2636. pr_err("%s: CQE: Unexpected interrupt 0x%08x.\n",
  2637. mmc_hostname(host->mmc), intmask);
  2638. sdhci_dumpregs(host);
  2639. }
  2640. return true;
  2641. }
  2642. EXPORT_SYMBOL_GPL(sdhci_cqe_irq);
  2643. /*****************************************************************************\
  2644. * *
  2645. * Device allocation/registration *
  2646. * *
  2647. \*****************************************************************************/
  2648. struct sdhci_host *sdhci_alloc_host(struct device *dev,
  2649. size_t priv_size)
  2650. {
  2651. struct mmc_host *mmc;
  2652. struct sdhci_host *host;
  2653. WARN_ON(dev == NULL);
  2654. mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
  2655. if (!mmc)
  2656. return ERR_PTR(-ENOMEM);
  2657. host = mmc_priv(mmc);
  2658. host->mmc = mmc;
  2659. host->mmc_host_ops = sdhci_ops;
  2660. mmc->ops = &host->mmc_host_ops;
  2661. host->flags = SDHCI_SIGNALING_330;
  2662. host->cqe_ier = SDHCI_CQE_INT_MASK;
  2663. host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK;
  2664. host->tuning_delay = -1;
  2665. host->sdma_boundary = SDHCI_DEFAULT_BOUNDARY_ARG;
  2666. return host;
  2667. }
  2668. EXPORT_SYMBOL_GPL(sdhci_alloc_host);
  2669. static int sdhci_set_dma_mask(struct sdhci_host *host)
  2670. {
  2671. struct mmc_host *mmc = host->mmc;
  2672. struct device *dev = mmc_dev(mmc);
  2673. int ret = -EINVAL;
  2674. if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
  2675. host->flags &= ~SDHCI_USE_64_BIT_DMA;
  2676. /* Try 64-bit mask if hardware is capable of it */
  2677. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  2678. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
  2679. if (ret) {
  2680. pr_warn("%s: Failed to set 64-bit DMA mask.\n",
  2681. mmc_hostname(mmc));
  2682. host->flags &= ~SDHCI_USE_64_BIT_DMA;
  2683. }
  2684. }
  2685. /* 32-bit mask as default & fallback */
  2686. if (ret) {
  2687. ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
  2688. if (ret)
  2689. pr_warn("%s: Failed to set 32-bit DMA mask.\n",
  2690. mmc_hostname(mmc));
  2691. }
  2692. return ret;
  2693. }
  2694. void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
  2695. {
  2696. u16 v;
  2697. u64 dt_caps_mask = 0;
  2698. u64 dt_caps = 0;
  2699. if (host->read_caps)
  2700. return;
  2701. host->read_caps = true;
  2702. if (debug_quirks)
  2703. host->quirks = debug_quirks;
  2704. if (debug_quirks2)
  2705. host->quirks2 = debug_quirks2;
  2706. sdhci_do_reset(host, SDHCI_RESET_ALL);
  2707. of_property_read_u64(mmc_dev(host->mmc)->of_node,
  2708. "sdhci-caps-mask", &dt_caps_mask);
  2709. of_property_read_u64(mmc_dev(host->mmc)->of_node,
  2710. "sdhci-caps", &dt_caps);
  2711. v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
  2712. host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
  2713. if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
  2714. return;
  2715. if (caps) {
  2716. host->caps = *caps;
  2717. } else {
  2718. host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
  2719. host->caps &= ~lower_32_bits(dt_caps_mask);
  2720. host->caps |= lower_32_bits(dt_caps);
  2721. }
  2722. if (host->version < SDHCI_SPEC_300)
  2723. return;
  2724. if (caps1) {
  2725. host->caps1 = *caps1;
  2726. } else {
  2727. host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
  2728. host->caps1 &= ~upper_32_bits(dt_caps_mask);
  2729. host->caps1 |= upper_32_bits(dt_caps);
  2730. }
  2731. }
  2732. EXPORT_SYMBOL_GPL(__sdhci_read_caps);
  2733. static int sdhci_allocate_bounce_buffer(struct sdhci_host *host)
  2734. {
  2735. struct mmc_host *mmc = host->mmc;
  2736. unsigned int max_blocks;
  2737. unsigned int bounce_size;
  2738. int ret;
  2739. /*
  2740. * Cap the bounce buffer at 64KB. Using a bigger bounce buffer
  2741. * has diminishing returns, this is probably because SD/MMC
  2742. * cards are usually optimized to handle this size of requests.
  2743. */
  2744. bounce_size = SZ_64K;
  2745. /*
  2746. * Adjust downwards to maximum request size if this is less
  2747. * than our segment size, else hammer down the maximum
  2748. * request size to the maximum buffer size.
  2749. */
  2750. if (mmc->max_req_size < bounce_size)
  2751. bounce_size = mmc->max_req_size;
  2752. max_blocks = bounce_size / 512;
  2753. /*
  2754. * When we just support one segment, we can get significant
  2755. * speedups by the help of a bounce buffer to group scattered
  2756. * reads/writes together.
  2757. */
  2758. host->bounce_buffer = devm_kmalloc(mmc->parent,
  2759. bounce_size,
  2760. GFP_KERNEL);
  2761. if (!host->bounce_buffer) {
  2762. pr_err("%s: failed to allocate %u bytes for bounce buffer, falling back to single segments\n",
  2763. mmc_hostname(mmc),
  2764. bounce_size);
  2765. /*
  2766. * Exiting with zero here makes sure we proceed with
  2767. * mmc->max_segs == 1.
  2768. */
  2769. return 0;
  2770. }
  2771. host->bounce_addr = dma_map_single(mmc->parent,
  2772. host->bounce_buffer,
  2773. bounce_size,
  2774. DMA_BIDIRECTIONAL);
  2775. ret = dma_mapping_error(mmc->parent, host->bounce_addr);
  2776. if (ret)
  2777. /* Again fall back to max_segs == 1 */
  2778. return 0;
  2779. host->bounce_buffer_size = bounce_size;
  2780. /* Lie about this since we're bouncing */
  2781. mmc->max_segs = max_blocks;
  2782. mmc->max_seg_size = bounce_size;
  2783. mmc->max_req_size = bounce_size;
  2784. pr_info("%s bounce up to %u segments into one, max segment size %u bytes\n",
  2785. mmc_hostname(mmc), max_blocks, bounce_size);
  2786. return 0;
  2787. }
  2788. int sdhci_setup_host(struct sdhci_host *host)
  2789. {
  2790. struct mmc_host *mmc;
  2791. u32 max_current_caps;
  2792. unsigned int ocr_avail;
  2793. unsigned int override_timeout_clk;
  2794. u32 max_clk;
  2795. int ret;
  2796. WARN_ON(host == NULL);
  2797. if (host == NULL)
  2798. return -EINVAL;
  2799. mmc = host->mmc;
  2800. /*
  2801. * If there are external regulators, get them. Note this must be done
  2802. * early before resetting the host and reading the capabilities so that
  2803. * the host can take the appropriate action if regulators are not
  2804. * available.
  2805. */
  2806. ret = mmc_regulator_get_supply(mmc);
  2807. if (ret)
  2808. return ret;
  2809. DBG("Version: 0x%08x | Present: 0x%08x\n",
  2810. sdhci_readw(host, SDHCI_HOST_VERSION),
  2811. sdhci_readl(host, SDHCI_PRESENT_STATE));
  2812. DBG("Caps: 0x%08x | Caps_1: 0x%08x\n",
  2813. sdhci_readl(host, SDHCI_CAPABILITIES),
  2814. sdhci_readl(host, SDHCI_CAPABILITIES_1));
  2815. sdhci_read_caps(host);
  2816. override_timeout_clk = host->timeout_clk;
  2817. if (host->version > SDHCI_SPEC_300) {
  2818. pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
  2819. mmc_hostname(mmc), host->version);
  2820. }
  2821. if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
  2822. host->flags |= SDHCI_USE_SDMA;
  2823. else if (!(host->caps & SDHCI_CAN_DO_SDMA))
  2824. DBG("Controller doesn't have SDMA capability\n");
  2825. else
  2826. host->flags |= SDHCI_USE_SDMA;
  2827. if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
  2828. (host->flags & SDHCI_USE_SDMA)) {
  2829. DBG("Disabling DMA as it is marked broken\n");
  2830. host->flags &= ~SDHCI_USE_SDMA;
  2831. }
  2832. if ((host->version >= SDHCI_SPEC_200) &&
  2833. (host->caps & SDHCI_CAN_DO_ADMA2))
  2834. host->flags |= SDHCI_USE_ADMA;
  2835. if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
  2836. (host->flags & SDHCI_USE_ADMA)) {
  2837. DBG("Disabling ADMA as it is marked broken\n");
  2838. host->flags &= ~SDHCI_USE_ADMA;
  2839. }
  2840. /*
  2841. * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
  2842. * and *must* do 64-bit DMA. A driver has the opportunity to change
  2843. * that during the first call to ->enable_dma(). Similarly
  2844. * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
  2845. * implement.
  2846. */
  2847. if (host->caps & SDHCI_CAN_64BIT)
  2848. host->flags |= SDHCI_USE_64_BIT_DMA;
  2849. if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
  2850. ret = sdhci_set_dma_mask(host);
  2851. if (!ret && host->ops->enable_dma)
  2852. ret = host->ops->enable_dma(host);
  2853. if (ret) {
  2854. pr_warn("%s: No suitable DMA available - falling back to PIO\n",
  2855. mmc_hostname(mmc));
  2856. host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
  2857. ret = 0;
  2858. }
  2859. }
  2860. /* SDMA does not support 64-bit DMA */
  2861. if (host->flags & SDHCI_USE_64_BIT_DMA)
  2862. host->flags &= ~SDHCI_USE_SDMA;
  2863. if (host->flags & SDHCI_USE_ADMA) {
  2864. dma_addr_t dma;
  2865. void *buf;
  2866. /*
  2867. * The DMA descriptor table size is calculated as the maximum
  2868. * number of segments times 2, to allow for an alignment
  2869. * descriptor for each segment, plus 1 for a nop end descriptor,
  2870. * all multipled by the descriptor size.
  2871. */
  2872. if (host->flags & SDHCI_USE_64_BIT_DMA) {
  2873. host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
  2874. SDHCI_ADMA2_64_DESC_SZ;
  2875. host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
  2876. } else {
  2877. host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
  2878. SDHCI_ADMA2_32_DESC_SZ;
  2879. host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
  2880. }
  2881. host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
  2882. buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
  2883. host->adma_table_sz, &dma, GFP_KERNEL);
  2884. if (!buf) {
  2885. pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
  2886. mmc_hostname(mmc));
  2887. host->flags &= ~SDHCI_USE_ADMA;
  2888. } else if ((dma + host->align_buffer_sz) &
  2889. (SDHCI_ADMA2_DESC_ALIGN - 1)) {
  2890. pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
  2891. mmc_hostname(mmc));
  2892. host->flags &= ~SDHCI_USE_ADMA;
  2893. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  2894. host->adma_table_sz, buf, dma);
  2895. } else {
  2896. host->align_buffer = buf;
  2897. host->align_addr = dma;
  2898. host->adma_table = buf + host->align_buffer_sz;
  2899. host->adma_addr = dma + host->align_buffer_sz;
  2900. }
  2901. }
  2902. /*
  2903. * If we use DMA, then it's up to the caller to set the DMA
  2904. * mask, but PIO does not need the hw shim so we set a new
  2905. * mask here in that case.
  2906. */
  2907. if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
  2908. host->dma_mask = DMA_BIT_MASK(64);
  2909. mmc_dev(mmc)->dma_mask = &host->dma_mask;
  2910. }
  2911. if (host->version >= SDHCI_SPEC_300)
  2912. host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
  2913. >> SDHCI_CLOCK_BASE_SHIFT;
  2914. else
  2915. host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
  2916. >> SDHCI_CLOCK_BASE_SHIFT;
  2917. host->max_clk *= 1000000;
  2918. if (host->max_clk == 0 || host->quirks &
  2919. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
  2920. if (!host->ops->get_max_clock) {
  2921. pr_err("%s: Hardware doesn't specify base clock frequency.\n",
  2922. mmc_hostname(mmc));
  2923. ret = -ENODEV;
  2924. goto undma;
  2925. }
  2926. host->max_clk = host->ops->get_max_clock(host);
  2927. }
  2928. /*
  2929. * In case of Host Controller v3.00, find out whether clock
  2930. * multiplier is supported.
  2931. */
  2932. host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
  2933. SDHCI_CLOCK_MUL_SHIFT;
  2934. /*
  2935. * In case the value in Clock Multiplier is 0, then programmable
  2936. * clock mode is not supported, otherwise the actual clock
  2937. * multiplier is one more than the value of Clock Multiplier
  2938. * in the Capabilities Register.
  2939. */
  2940. if (host->clk_mul)
  2941. host->clk_mul += 1;
  2942. /*
  2943. * Set host parameters.
  2944. */
  2945. max_clk = host->max_clk;
  2946. if (host->ops->get_min_clock)
  2947. mmc->f_min = host->ops->get_min_clock(host);
  2948. else if (host->version >= SDHCI_SPEC_300) {
  2949. if (host->clk_mul) {
  2950. mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
  2951. max_clk = host->max_clk * host->clk_mul;
  2952. } else
  2953. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
  2954. } else
  2955. mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
  2956. if (!mmc->f_max || mmc->f_max > max_clk)
  2957. mmc->f_max = max_clk;
  2958. if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
  2959. host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
  2960. SDHCI_TIMEOUT_CLK_SHIFT;
  2961. if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
  2962. host->timeout_clk *= 1000;
  2963. if (host->timeout_clk == 0) {
  2964. if (!host->ops->get_timeout_clock) {
  2965. pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
  2966. mmc_hostname(mmc));
  2967. ret = -ENODEV;
  2968. goto undma;
  2969. }
  2970. host->timeout_clk =
  2971. DIV_ROUND_UP(host->ops->get_timeout_clock(host),
  2972. 1000);
  2973. }
  2974. if (override_timeout_clk)
  2975. host->timeout_clk = override_timeout_clk;
  2976. mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
  2977. host->ops->get_max_timeout_count(host) : 1 << 27;
  2978. mmc->max_busy_timeout /= host->timeout_clk;
  2979. }
  2980. mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
  2981. mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
  2982. if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
  2983. host->flags |= SDHCI_AUTO_CMD12;
  2984. /* Auto-CMD23 stuff only works in ADMA or PIO. */
  2985. if ((host->version >= SDHCI_SPEC_300) &&
  2986. ((host->flags & SDHCI_USE_ADMA) ||
  2987. !(host->flags & SDHCI_USE_SDMA)) &&
  2988. !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
  2989. host->flags |= SDHCI_AUTO_CMD23;
  2990. DBG("Auto-CMD23 available\n");
  2991. } else {
  2992. DBG("Auto-CMD23 unavailable\n");
  2993. }
  2994. /*
  2995. * A controller may support 8-bit width, but the board itself
  2996. * might not have the pins brought out. Boards that support
  2997. * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
  2998. * their platform code before calling sdhci_add_host(), and we
  2999. * won't assume 8-bit width for hosts without that CAP.
  3000. */
  3001. if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
  3002. mmc->caps |= MMC_CAP_4_BIT_DATA;
  3003. if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
  3004. mmc->caps &= ~MMC_CAP_CMD23;
  3005. if (host->caps & SDHCI_CAN_DO_HISPD)
  3006. mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
  3007. if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
  3008. mmc_card_is_removable(mmc) &&
  3009. mmc_gpio_get_cd(host->mmc) < 0)
  3010. mmc->caps |= MMC_CAP_NEEDS_POLL;
  3011. /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
  3012. if (!IS_ERR(mmc->supply.vqmmc)) {
  3013. ret = regulator_enable(mmc->supply.vqmmc);
  3014. if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
  3015. 1950000))
  3016. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
  3017. SDHCI_SUPPORT_SDR50 |
  3018. SDHCI_SUPPORT_DDR50);
  3019. if (ret) {
  3020. pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
  3021. mmc_hostname(mmc), ret);
  3022. mmc->supply.vqmmc = ERR_PTR(-EINVAL);
  3023. }
  3024. }
  3025. if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
  3026. host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  3027. SDHCI_SUPPORT_DDR50);
  3028. }
  3029. /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
  3030. if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
  3031. SDHCI_SUPPORT_DDR50))
  3032. mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
  3033. /* SDR104 supports also implies SDR50 support */
  3034. if (host->caps1 & SDHCI_SUPPORT_SDR104) {
  3035. mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
  3036. /* SD3.0: SDR104 is supported so (for eMMC) the caps2
  3037. * field can be promoted to support HS200.
  3038. */
  3039. if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
  3040. mmc->caps2 |= MMC_CAP2_HS200;
  3041. } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
  3042. mmc->caps |= MMC_CAP_UHS_SDR50;
  3043. }
  3044. if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
  3045. (host->caps1 & SDHCI_SUPPORT_HS400))
  3046. mmc->caps2 |= MMC_CAP2_HS400;
  3047. if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
  3048. (IS_ERR(mmc->supply.vqmmc) ||
  3049. !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
  3050. 1300000)))
  3051. mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
  3052. if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
  3053. !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
  3054. mmc->caps |= MMC_CAP_UHS_DDR50;
  3055. /* Does the host need tuning for SDR50? */
  3056. if (host->caps1 & SDHCI_USE_SDR50_TUNING)
  3057. host->flags |= SDHCI_SDR50_NEEDS_TUNING;
  3058. /* Driver Type(s) (A, C, D) supported by the host */
  3059. if (host->caps1 & SDHCI_DRIVER_TYPE_A)
  3060. mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
  3061. if (host->caps1 & SDHCI_DRIVER_TYPE_C)
  3062. mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
  3063. if (host->caps1 & SDHCI_DRIVER_TYPE_D)
  3064. mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
  3065. /* Initial value for re-tuning timer count */
  3066. host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
  3067. SDHCI_RETUNING_TIMER_COUNT_SHIFT;
  3068. /*
  3069. * In case Re-tuning Timer is not disabled, the actual value of
  3070. * re-tuning timer will be 2 ^ (n - 1).
  3071. */
  3072. if (host->tuning_count)
  3073. host->tuning_count = 1 << (host->tuning_count - 1);
  3074. /* Re-tuning mode supported by the Host Controller */
  3075. host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
  3076. SDHCI_RETUNING_MODE_SHIFT;
  3077. ocr_avail = 0;
  3078. /*
  3079. * According to SD Host Controller spec v3.00, if the Host System
  3080. * can afford more than 150mA, Host Driver should set XPC to 1. Also
  3081. * the value is meaningful only if Voltage Support in the Capabilities
  3082. * register is set. The actual current value is 4 times the register
  3083. * value.
  3084. */
  3085. max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
  3086. if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
  3087. int curr = regulator_get_current_limit(mmc->supply.vmmc);
  3088. if (curr > 0) {
  3089. /* convert to SDHCI_MAX_CURRENT format */
  3090. curr = curr/1000; /* convert to mA */
  3091. curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
  3092. curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
  3093. max_current_caps =
  3094. (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
  3095. (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
  3096. (curr << SDHCI_MAX_CURRENT_180_SHIFT);
  3097. }
  3098. }
  3099. if (host->caps & SDHCI_CAN_VDD_330) {
  3100. ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
  3101. mmc->max_current_330 = ((max_current_caps &
  3102. SDHCI_MAX_CURRENT_330_MASK) >>
  3103. SDHCI_MAX_CURRENT_330_SHIFT) *
  3104. SDHCI_MAX_CURRENT_MULTIPLIER;
  3105. }
  3106. if (host->caps & SDHCI_CAN_VDD_300) {
  3107. ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
  3108. mmc->max_current_300 = ((max_current_caps &
  3109. SDHCI_MAX_CURRENT_300_MASK) >>
  3110. SDHCI_MAX_CURRENT_300_SHIFT) *
  3111. SDHCI_MAX_CURRENT_MULTIPLIER;
  3112. }
  3113. if (host->caps & SDHCI_CAN_VDD_180) {
  3114. ocr_avail |= MMC_VDD_165_195;
  3115. mmc->max_current_180 = ((max_current_caps &
  3116. SDHCI_MAX_CURRENT_180_MASK) >>
  3117. SDHCI_MAX_CURRENT_180_SHIFT) *
  3118. SDHCI_MAX_CURRENT_MULTIPLIER;
  3119. }
  3120. /* If OCR set by host, use it instead. */
  3121. if (host->ocr_mask)
  3122. ocr_avail = host->ocr_mask;
  3123. /* If OCR set by external regulators, give it highest prio. */
  3124. if (mmc->ocr_avail)
  3125. ocr_avail = mmc->ocr_avail;
  3126. mmc->ocr_avail = ocr_avail;
  3127. mmc->ocr_avail_sdio = ocr_avail;
  3128. if (host->ocr_avail_sdio)
  3129. mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
  3130. mmc->ocr_avail_sd = ocr_avail;
  3131. if (host->ocr_avail_sd)
  3132. mmc->ocr_avail_sd &= host->ocr_avail_sd;
  3133. else /* normal SD controllers don't support 1.8V */
  3134. mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
  3135. mmc->ocr_avail_mmc = ocr_avail;
  3136. if (host->ocr_avail_mmc)
  3137. mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
  3138. if (mmc->ocr_avail == 0) {
  3139. pr_err("%s: Hardware doesn't report any support voltages.\n",
  3140. mmc_hostname(mmc));
  3141. ret = -ENODEV;
  3142. goto unreg;
  3143. }
  3144. if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
  3145. MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
  3146. MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
  3147. (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
  3148. host->flags |= SDHCI_SIGNALING_180;
  3149. if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
  3150. host->flags |= SDHCI_SIGNALING_120;
  3151. spin_lock_init(&host->lock);
  3152. /*
  3153. * Maximum number of sectors in one transfer. Limited by SDMA boundary
  3154. * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
  3155. * is less anyway.
  3156. */
  3157. mmc->max_req_size = 524288;
  3158. /*
  3159. * Maximum number of segments. Depends on if the hardware
  3160. * can do scatter/gather or not.
  3161. */
  3162. if (host->flags & SDHCI_USE_ADMA) {
  3163. mmc->max_segs = SDHCI_MAX_SEGS;
  3164. } else if (host->flags & SDHCI_USE_SDMA) {
  3165. mmc->max_segs = 1;
  3166. if (swiotlb_max_segment()) {
  3167. unsigned int max_req_size = (1 << IO_TLB_SHIFT) *
  3168. IO_TLB_SEGSIZE;
  3169. mmc->max_req_size = min(mmc->max_req_size,
  3170. max_req_size);
  3171. }
  3172. } else { /* PIO */
  3173. mmc->max_segs = SDHCI_MAX_SEGS;
  3174. }
  3175. /*
  3176. * Maximum segment size. Could be one segment with the maximum number
  3177. * of bytes. When doing hardware scatter/gather, each entry cannot
  3178. * be larger than 64 KiB though.
  3179. */
  3180. if (host->flags & SDHCI_USE_ADMA) {
  3181. if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
  3182. mmc->max_seg_size = 65535;
  3183. else
  3184. mmc->max_seg_size = 65536;
  3185. } else {
  3186. mmc->max_seg_size = mmc->max_req_size;
  3187. }
  3188. /*
  3189. * Maximum block size. This varies from controller to controller and
  3190. * is specified in the capabilities register.
  3191. */
  3192. if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
  3193. mmc->max_blk_size = 2;
  3194. } else {
  3195. mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
  3196. SDHCI_MAX_BLOCK_SHIFT;
  3197. if (mmc->max_blk_size >= 3) {
  3198. pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
  3199. mmc_hostname(mmc));
  3200. mmc->max_blk_size = 0;
  3201. }
  3202. }
  3203. mmc->max_blk_size = 512 << mmc->max_blk_size;
  3204. /*
  3205. * Maximum block count.
  3206. */
  3207. mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
  3208. if (mmc->max_segs == 1) {
  3209. /* This may alter mmc->*_blk_* parameters */
  3210. ret = sdhci_allocate_bounce_buffer(host);
  3211. if (ret)
  3212. return ret;
  3213. }
  3214. return 0;
  3215. unreg:
  3216. if (!IS_ERR(mmc->supply.vqmmc))
  3217. regulator_disable(mmc->supply.vqmmc);
  3218. undma:
  3219. if (host->align_buffer)
  3220. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3221. host->adma_table_sz, host->align_buffer,
  3222. host->align_addr);
  3223. host->adma_table = NULL;
  3224. host->align_buffer = NULL;
  3225. return ret;
  3226. }
  3227. EXPORT_SYMBOL_GPL(sdhci_setup_host);
  3228. void sdhci_cleanup_host(struct sdhci_host *host)
  3229. {
  3230. struct mmc_host *mmc = host->mmc;
  3231. if (!IS_ERR(mmc->supply.vqmmc))
  3232. regulator_disable(mmc->supply.vqmmc);
  3233. if (host->align_buffer)
  3234. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3235. host->adma_table_sz, host->align_buffer,
  3236. host->align_addr);
  3237. host->adma_table = NULL;
  3238. host->align_buffer = NULL;
  3239. }
  3240. EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
  3241. int __sdhci_add_host(struct sdhci_host *host)
  3242. {
  3243. struct mmc_host *mmc = host->mmc;
  3244. int ret;
  3245. /*
  3246. * Init tasklets.
  3247. */
  3248. tasklet_init(&host->finish_tasklet,
  3249. sdhci_tasklet_finish, (unsigned long)host);
  3250. timer_setup(&host->timer, sdhci_timeout_timer, 0);
  3251. timer_setup(&host->data_timer, sdhci_timeout_data_timer, 0);
  3252. init_waitqueue_head(&host->buf_ready_int);
  3253. sdhci_init(host, 0);
  3254. ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
  3255. IRQF_SHARED, mmc_hostname(mmc), host);
  3256. if (ret) {
  3257. pr_err("%s: Failed to request IRQ %d: %d\n",
  3258. mmc_hostname(mmc), host->irq, ret);
  3259. goto untasklet;
  3260. }
  3261. ret = sdhci_led_register(host);
  3262. if (ret) {
  3263. pr_err("%s: Failed to register LED device: %d\n",
  3264. mmc_hostname(mmc), ret);
  3265. goto unirq;
  3266. }
  3267. mmiowb();
  3268. ret = mmc_add_host(mmc);
  3269. if (ret)
  3270. goto unled;
  3271. pr_info("%s: SDHCI controller on %s [%s] using %s\n",
  3272. mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
  3273. (host->flags & SDHCI_USE_ADMA) ?
  3274. (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
  3275. (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
  3276. sdhci_enable_card_detection(host);
  3277. return 0;
  3278. unled:
  3279. sdhci_led_unregister(host);
  3280. unirq:
  3281. sdhci_do_reset(host, SDHCI_RESET_ALL);
  3282. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  3283. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  3284. free_irq(host->irq, host);
  3285. untasklet:
  3286. tasklet_kill(&host->finish_tasklet);
  3287. return ret;
  3288. }
  3289. EXPORT_SYMBOL_GPL(__sdhci_add_host);
  3290. int sdhci_add_host(struct sdhci_host *host)
  3291. {
  3292. int ret;
  3293. ret = sdhci_setup_host(host);
  3294. if (ret)
  3295. return ret;
  3296. ret = __sdhci_add_host(host);
  3297. if (ret)
  3298. goto cleanup;
  3299. return 0;
  3300. cleanup:
  3301. sdhci_cleanup_host(host);
  3302. return ret;
  3303. }
  3304. EXPORT_SYMBOL_GPL(sdhci_add_host);
  3305. void sdhci_remove_host(struct sdhci_host *host, int dead)
  3306. {
  3307. struct mmc_host *mmc = host->mmc;
  3308. unsigned long flags;
  3309. if (dead) {
  3310. spin_lock_irqsave(&host->lock, flags);
  3311. host->flags |= SDHCI_DEVICE_DEAD;
  3312. if (sdhci_has_requests(host)) {
  3313. pr_err("%s: Controller removed during "
  3314. " transfer!\n", mmc_hostname(mmc));
  3315. sdhci_error_out_mrqs(host, -ENOMEDIUM);
  3316. }
  3317. spin_unlock_irqrestore(&host->lock, flags);
  3318. }
  3319. sdhci_disable_card_detection(host);
  3320. mmc_remove_host(mmc);
  3321. sdhci_led_unregister(host);
  3322. if (!dead)
  3323. sdhci_do_reset(host, SDHCI_RESET_ALL);
  3324. sdhci_writel(host, 0, SDHCI_INT_ENABLE);
  3325. sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
  3326. free_irq(host->irq, host);
  3327. del_timer_sync(&host->timer);
  3328. del_timer_sync(&host->data_timer);
  3329. tasklet_kill(&host->finish_tasklet);
  3330. if (!IS_ERR(mmc->supply.vqmmc))
  3331. regulator_disable(mmc->supply.vqmmc);
  3332. if (host->align_buffer)
  3333. dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
  3334. host->adma_table_sz, host->align_buffer,
  3335. host->align_addr);
  3336. host->adma_table = NULL;
  3337. host->align_buffer = NULL;
  3338. }
  3339. EXPORT_SYMBOL_GPL(sdhci_remove_host);
  3340. void sdhci_free_host(struct sdhci_host *host)
  3341. {
  3342. mmc_free_host(host->mmc);
  3343. }
  3344. EXPORT_SYMBOL_GPL(sdhci_free_host);
  3345. /*****************************************************************************\
  3346. * *
  3347. * Driver init/exit *
  3348. * *
  3349. \*****************************************************************************/
  3350. static int __init sdhci_drv_init(void)
  3351. {
  3352. pr_info(DRIVER_NAME
  3353. ": Secure Digital Host Controller Interface driver\n");
  3354. pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
  3355. return 0;
  3356. }
  3357. static void __exit sdhci_drv_exit(void)
  3358. {
  3359. }
  3360. module_init(sdhci_drv_init);
  3361. module_exit(sdhci_drv_exit);
  3362. module_param(debug_quirks, uint, 0444);
  3363. module_param(debug_quirks2, uint, 0444);
  3364. MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
  3365. MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
  3366. MODULE_LICENSE("GPL");
  3367. MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
  3368. MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");