sdhci.c 108 KB

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