sdhci.c 129 KB

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