sd.c 272 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538
  1. #ifdef pr_fmt
  2. #undef pr_fmt
  3. #endif
  4. #define pr_fmt(fmt) "["KBUILD_MODNAME"]" fmt
  5. #include <generated/autoconf.h>
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/init.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/timer.h>
  11. #include <linux/ioport.h>
  12. #include <linux/device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/delay.h>
  16. #include <linux/blkdev.h>
  17. #include <linux/slab.h>
  18. #include <linux/wakelock.h>
  19. #include <linux/mmc/host.h>
  20. #include <linux/mmc/card.h>
  21. #include <linux/mmc/core.h>
  22. #include <linux/mmc/mmc.h>
  23. #include <linux/mmc/sd.h>
  24. #include <linux/mmc/sdio.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/irq.h>
  27. #include <linux/kthread.h>
  28. #include <linux/mm_types.h>
  29. #include <linux/mm.h>
  30. #include <linux/swap.h>
  31. #include <linux/mman.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/highmem.h>
  34. #include <linux/printk.h>
  35. #include <asm/page.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/fs.h>
  38. #include <linux/proc_fs.h>
  39. #ifdef CONFIG_OF
  40. #ifndef CONFIG_MTK_LEGACY
  41. #include <linux/regulator/consumer.h>
  42. #endif
  43. #include <linux/of.h>
  44. #include <linux/of_address.h>
  45. #include <linux/of_irq.h>
  46. #endif
  47. #ifdef CONFIG_MTK_AEE_FEATURE
  48. #include <mt-plat/aee.h>
  49. #endif
  50. #include "mt_sd.h"
  51. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  52. #include <mt_vcore_dvfs.h>
  53. #endif /* MTK_SDIO30_ONLINE_TUNING_SUPPORT */
  54. #include <queue.h>
  55. #include <linux/gpio.h>
  56. #include <mt-plat/mt_boot.h>
  57. #include <mt-plat/partition.h>
  58. #ifdef CONFIG_MTK_HIBERNATION
  59. #include <mtk_hibernate_dpm.h>
  60. #endif
  61. #include "msdc_hw_ett.h"
  62. #include "dbg.h"
  63. #define MET_USER_EVENT_SUPPORT
  64. #include<mt-plat/upmu_common.h>
  65. /* weiping fix */
  66. #if 0
  67. #include <mach/dma.h>
  68. #include <mach/devs.h>
  69. #include <mach/mt_typedefs.h>
  70. #include <linux/mmc/sd_misc.h>
  71. #include <mach/mt_chip.h>
  72. #include "dbg.h"
  73. #ifdef MTK_MSDC_BRINGUP_DEBUG
  74. #include <mach/mt_pmic_wrap.h>
  75. #endif
  76. #include <linux/met_drv.h>
  77. #include <mach/eint.h>
  78. #include <mach/mt_storage_logger.h>
  79. #include <mach/partition.h>
  80. #include <mach/emi_mpu.h>
  81. #include <mach/memory.h>
  82. #endif
  83. #ifdef CONFIG_MTK_CLKMGR
  84. #include <mach/mt_clkmgr.h>
  85. #else
  86. #include <mt_clk_id.h>
  87. struct clk *g_msdc0_pll_sel = NULL;
  88. struct clk *g_msdc0_pll_800m = NULL;
  89. struct clk *g_msdc0_pll_400m = NULL;
  90. struct clk *g_msdc0_pll_200m = NULL;
  91. #endif
  92. static int msdc_get_card_status(struct mmc_host *mmc,
  93. struct msdc_host *host, u32 *status);
  94. static void msdc_clksrc_onoff(struct msdc_host *host, u32 on);
  95. /* ========================= move from dbg.c start =========================*/
  96. /* for debug zone */
  97. unsigned int sd_debug_zone[HOST_MAX_NUM] = {
  98. 0,
  99. 0,
  100. 0,
  101. 0,
  102. };
  103. /* for enable/disable register dump */
  104. unsigned int sd_register_zone[HOST_MAX_NUM] = {
  105. 1,
  106. 1,
  107. 1,
  108. 1,
  109. };
  110. /* mode select */
  111. u32 dma_size[HOST_MAX_NUM] = {
  112. 512,
  113. 512,
  114. 512,
  115. 512,
  116. };
  117. int drv_mode[HOST_MAX_NUM] = {
  118. #define MODE_PIO (0)
  119. #define MODE_DMA (1)
  120. #define MODE_SIZE_DEP (2)
  121. MODE_SIZE_DEP, /* using DMA or not depend on the size */
  122. MODE_SIZE_DEP,
  123. MODE_SIZE_DEP,
  124. MODE_SIZE_DEP,
  125. };
  126. unsigned char msdc_clock_src[HOST_MAX_NUM] = {
  127. 0,
  128. 0,
  129. 0,
  130. 0,
  131. };
  132. u32 msdc_host_mode[HOST_MAX_NUM] = {
  133. 0,
  134. 0,
  135. 0,
  136. 0,
  137. };
  138. u32 msdc_host_mode2[HOST_MAX_NUM] = {
  139. 0,
  140. 0,
  141. 0,
  142. 0,
  143. };
  144. int g_ett_tune = 0; /* enable or disable the ETT tune */
  145. int g_ett_hs400_tune = 0; /* record the number of failed HS400 ETT settings */
  146. int g_ett_cmd_tune = 0; /* record the number of failed cmd ETT settings */
  147. int g_ett_read_tune = 0; /* record the number of failed read ETT settings */
  148. int g_ett_write_tune = 0; /* record the number of failed write ETT settings */
  149. /* do not record the pass settigns, but try the worst setting of each request */
  150. int g_reset_tune = 0;
  151. u32 sdio_tune_flag = 0;
  152. /* ========================= move from dbg.c end =========================*/
  153. #define CAPACITY_2G (2 * 1024 * 1024 * 1024ULL)
  154. u32 g_emmc_mode_switch = 0;
  155. static void msdc_init_hw(struct msdc_host *host);
  156. #ifndef FPGA_PLATFORM
  157. #ifndef CONFIG_MTK_LEGACY
  158. struct regulator *reg_vemc_3v3 = NULL;
  159. struct regulator *reg_vmc = NULL;
  160. struct regulator *reg_vmch = NULL;
  161. #endif
  162. #endif
  163. #ifdef MTK_MSDC_USE_CACHE
  164. #define MSDC_MAX_FLUSH_COUNT (3)
  165. #define CACHE_UN_FLUSHED (0)
  166. #define CACHE_FLUSHED (1)
  167. static unsigned int g_cache_status = CACHE_UN_FLUSHED;
  168. static unsigned long long g_flush_data_size;
  169. static unsigned int g_flush_error_count;
  170. static int g_flush_error_happend;
  171. static int g_bypass_flush;
  172. unsigned long long g_cache_part_start;
  173. unsigned long long g_cache_part_end;
  174. unsigned long long g_usrdata_part_start;
  175. unsigned long long g_usrdata_part_end;
  176. /* if disable cache by vendor fill CID.MID to g_emmc_cache_quirk[i] */
  177. unsigned char g_emmc_cache_quirk[256];
  178. #define CID_MANFID_SANDISK 0x2
  179. #define CID_MANFID_TOSHIBA 0x11
  180. #define CID_MANFID_MICRON 0x13
  181. #define CID_MANFID_SAMSUNG 0x15
  182. #define CID_MANFID_SANDISK_NEW 0x45
  183. #define CID_MANFID_HYNIX 0x90
  184. #define CID_MANFID_KSI 0x70
  185. #endif
  186. unsigned int g_emmc_cache_size = 0;
  187. unsigned long long msdc_print_start_time;
  188. unsigned long long msdc_print_end_time;
  189. unsigned int print_nums;
  190. static u8 emmc_id;
  191. #ifdef MTK_EMMC_ETT_TO_DRIVER
  192. #include "emmc_device_list.h"
  193. static u8 m_id; /* Manufacturer ID */
  194. static char pro_name[8] = { 0 }; /* Product name */
  195. #endif
  196. #if (MSDC_DATA1_INT == 1)
  197. static u16 u_sdio_irq_counter;
  198. static u16 u_msdc_irq_counter;
  199. /* static int int_sdio_irq_enable; */
  200. #endif
  201. struct msdc_host *ghost = NULL;
  202. int src_clk_control = 0;
  203. struct mmc_blk_data {
  204. spinlock_t lock;
  205. struct gendisk *disk;
  206. struct mmc_queue queue;
  207. unsigned int usage;
  208. unsigned int read_only;
  209. };
  210. static bool emmc_sleep_failed;
  211. static int emmc_do_sleep_awake;
  212. static struct workqueue_struct *wq_tune;
  213. #if defined(FEATURE_MET_MMC_INDEX)
  214. static unsigned int met_mmc_bdnum;
  215. #endif
  216. #define DRV_NAME "mtk-msdc"
  217. #define MSDC_COOKIE_PIO (1<<0)
  218. #define MSDC_COOKIE_ASYNC (1<<1)
  219. #define msdc_use_async_way(x) (x & MSDC_COOKIE_ASYNC)
  220. #define msdc_async_use_dma(x) ((x & MSDC_COOKIE_ASYNC) && (!(x & MSDC_COOKIE_PIO)))
  221. #define msdc_async_use_pio(x) ((x & MSDC_COOKIE_ASYNC) && ((x & MSDC_COOKIE_PIO)))
  222. #ifdef FPGA_PLATFORM
  223. #define HOST_MAX_MCLK (200000000)
  224. #else
  225. #define HOST_MAX_MCLK (200000000)
  226. #endif
  227. #define HOST_MIN_MCLK (260000)
  228. #define HOST_MAX_BLKSZ (2048)
  229. #define MSDC_OCR_AVAIL (MMC_VDD_28_29 | MMC_VDD_29_30 | \
  230. MMC_VDD_30_31 | MMC_VDD_31_32 | \
  231. MMC_VDD_32_33)
  232. /* #define MSDC_OCR_AVAIL (MMC_VDD_32_33 | MMC_VDD_33_34) */
  233. #define MSDC1_IRQ_SEL (1 << 9)
  234. #define DEFAULT_DEBOUNCE (8) /* 8 cycles */
  235. /* data timeout counter. 65536x40(75/77) /1048576 * 3(83/85) sclk. */
  236. #define DEFAULT_DTOC (3)
  237. #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */
  238. #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */
  239. #define CLK_TIMEOUT (HZ * 5) /* 5s */
  240. #define POLLING_BUSY (HZ * 3)
  241. /* a single transaction for WIFI may be 50K */
  242. #define MAX_DMA_CNT (64 * 1024 - 512)
  243. /*
  244. * a single transaction for LTE may be 128K
  245. * Basic DMA use 32 bits to store transfer size
  246. */
  247. #define MAX_DMA_CNT_SDIO (0xFFFFFFFF - 255)
  248. #define MAX_HW_SGMTS (MAX_BD_NUM)
  249. #define MAX_PHY_SGMTS (MAX_BD_NUM)
  250. #define MAX_SGMT_SZ (MAX_DMA_CNT)
  251. #define MAX_SGMT_SZ_SDIO (MAX_DMA_CNT_SDIO)
  252. #define CMD_TUNE_UHS_MAX_TIME (2*32*8*8)
  253. #define CMD_TUNE_HS_MAX_TIME (2*32)
  254. #define READ_TUNE_UHS_CLKMOD1_MAX_TIME (2*32*32*8)
  255. #define READ_TUNE_UHS_MAX_TIME (2*32*32)
  256. #define READ_TUNE_HS_MAX_TIME (2*32)
  257. #define WRITE_TUNE_HS_MAX_TIME (2*32)
  258. #define WRITE_TUNE_UHS_MAX_TIME (2*32*8)
  259. /* ================================= */
  260. #define MSDC_LOWER_FREQ
  261. #define MSDC_MAX_FREQ_DIV (2) /* 200 / (4 * 2) */
  262. #define MSDC_MAX_TIMEOUT_RETRY (1)
  263. #define MSDC_MAX_TIMEOUT_RETRY_EMMC (2)
  264. #define MSDC_MAX_W_TIMEOUT_TUNE (5)
  265. #define MSDC_MAX_W_TIMEOUT_TUNE_EMMC (64)
  266. #define MSDC_MAX_R_TIMEOUT_TUNE (3)
  267. #define MSDC_MAX_POWER_CYCLE (5)
  268. #define MSDC_MAX_CONTINUOUS_FAIL_REQUEST_COUNT (50)
  269. #ifdef CONFIG_OF
  270. static struct device_node *gpio_node;
  271. static struct device_node *infracfg_ao_node;
  272. static struct device_node *infracfg_node;
  273. static struct device_node *pericfg_node;
  274. static struct device_node *emi_node;
  275. static struct device_node *toprgu_node;
  276. static struct device_node *apmixed_node;
  277. static struct device_node *topckgen_node;
  278. static struct device_node *eint_node;
  279. static unsigned int cd_irq;
  280. static unsigned int cd_gpio;
  281. void __iomem *gpio_reg_base;
  282. void __iomem *infracfg_ao_reg_base;
  283. void __iomem *infracfg_reg_base;
  284. void __iomem *pericfg_reg_base;
  285. void __iomem *emi_reg_base;
  286. void __iomem *toprgu_reg_base;
  287. void __iomem *apmixed_reg_base1;
  288. void __iomem *topckgen_reg_base;
  289. #endif
  290. #ifdef FPGA_PLATFORM
  291. #ifdef CONFIG_OF
  292. static void __iomem *fpga_pwr_gpio;
  293. static void __iomem *fpga_pwr_gpio_eo;
  294. #define PWR_GPIO (fpga_pwr_gpio)
  295. #define PWR_GPIO_EO (fpga_pwr_gpio_eo)
  296. #else
  297. #define PWR_GPIO (0xF0000E84)
  298. #define PWR_GPIO_EO (0xF0000E88)
  299. #endif
  300. #define PWR_MASK_VOL_33 (1 << 10)
  301. #define PWR_MASK_VOL_18 (1 << 9)
  302. #define PWR_MASK_EN (1 << 8)
  303. #define PWR_MASK_VOL_33_MASK (~(1 << 10))
  304. #define PWR_MASK_EN_MASK (~(1 << 8))
  305. #define PWR_MASK_VOL_18_MASK (~(1 << 9))
  306. bool hwPowerOn_fpga(void)
  307. {
  308. u16 l_val;
  309. l_val = sdr_read16(PWR_GPIO);
  310. #ifdef MTK_EMMC_SUPPORT
  311. sdr_write16(PWR_GPIO, (l_val | PWR_MASK_VOL_18 | PWR_MASK_EN));
  312. /* | PWR_GPIO_L4_DIR)); */
  313. #else
  314. sdr_write16(PWR_GPIO, (l_val | PWR_MASK_VOL_33 | PWR_MASK_EN));
  315. /* | PWR_GPIO_L4_DIR)); */
  316. #endif
  317. l_val = sdr_read16(PWR_GPIO);
  318. pr_debug("[%s]: pwr gpio = 0x%x\n", __func__, l_val);
  319. return true;
  320. }
  321. EXPORT_SYMBOL(hwPowerOn_fpga);
  322. bool hwPowerSwitch_fpga(void)
  323. {
  324. u16 l_val;
  325. l_val = sdr_read16(PWR_GPIO);
  326. sdr_write16(PWR_GPIO, (l_val & PWR_MASK_VOL_33_MASK));
  327. l_val = sdr_read16(PWR_GPIO);
  328. sdr_write16(PWR_GPIO, (l_val | PWR_MASK_VOL_18));
  329. l_val = sdr_read16(PWR_GPIO);
  330. pr_debug("[%s]: pwr gpio = 0x%x\n", __func__, l_val);
  331. return true;
  332. }
  333. EXPORT_SYMBOL(hwPowerSwitch_fpga);
  334. bool hwPowerDown_fpga(void)
  335. {
  336. u16 l_val;
  337. l_val = sdr_read16(PWR_GPIO);
  338. #ifdef MTK_EMMC_SUPPORT
  339. sdr_write8(PWR_GPIO, (l_val & PWR_MASK_VOL_18_MASK & PWR_MASK_EN_MASK));
  340. #else
  341. sdr_write8(PWR_GPIO, (l_val & PWR_MASK_VOL_18_MASK & PWR_MASK_VOL_33_MASK
  342. & PWR_MASK_EN_MASK));
  343. #endif
  344. l_val = sdr_read16(PWR_GPIO);
  345. pr_debug("[%s]: pwr gpio = 0x%x\n", __func__, l_val);
  346. return true;
  347. }
  348. EXPORT_SYMBOL(hwPowerDown_fpga);
  349. #endif
  350. struct msdc_host *mtk_msdc_host[] = { NULL, NULL, NULL, NULL };
  351. EXPORT_SYMBOL(mtk_msdc_host);
  352. int g_dma_debug[HOST_MAX_NUM] = { 0, 0, 0, 0 };
  353. u32 latest_int_status[HOST_MAX_NUM] = { 0, 0, 0, 0 };
  354. /* 0 for PIO; 1 for DMA; 2 for nothing */
  355. int msdc_latest_transfer_mode[HOST_MAX_NUM] = {
  356. #define TRAN_MOD_PIO (0)
  357. #define TRAN_MOD_DMA (1)
  358. #define TRAN_MOD_NUM (2)
  359. TRAN_MOD_NUM,
  360. TRAN_MOD_NUM,
  361. TRAN_MOD_NUM,
  362. TRAN_MOD_NUM,
  363. };
  364. /* 0 for read; 1 for write; 2 for nothing */
  365. int msdc_latest_operation_type[HOST_MAX_NUM] = {
  366. #define OPER_TYPE_READ (0)
  367. #define OPER_TYPE_WRITE (1)
  368. #define OPER_TYPE_NUM (2)
  369. OPER_TYPE_NUM,
  370. OPER_TYPE_NUM,
  371. OPER_TYPE_NUM,
  372. OPER_TYPE_NUM,
  373. };
  374. #ifdef MSDC_DMA_ADDR_DEBUG
  375. struct dma_addr msdc_latest_dma_address[MAX_BD_PER_GPD];
  376. #endif
  377. static int msdc_rsp[] = {
  378. 0, /* RESP_NONE */
  379. 1, /* RESP_R1 */
  380. 2, /* RESP_R2 */
  381. 3, /* RESP_R3 */
  382. 4, /* RESP_R4 */
  383. 1, /* RESP_R5 */
  384. 1, /* RESP_R6 */
  385. 1, /* RESP_R7 */
  386. 7, /* RESP_R1b */
  387. };
  388. void msdc_dump_padctl(struct msdc_host *host)
  389. {
  390. switch (host->id) {
  391. case 0:
  392. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_MODE18_ADDR,
  393. sdr_read32(MSDC0_GPIO_MODE18_ADDR));
  394. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_MODE19_ADDR,
  395. sdr_read32(MSDC0_GPIO_MODE19_ADDR));
  396. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_IES_G5_ADDR,
  397. sdr_read32(MSDC0_GPIO_IES_G5_ADDR));
  398. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_SMT_G5_ADDR,
  399. sdr_read32(MSDC0_GPIO_SMT_G5_ADDR));
  400. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_TDSEL0_G5_ADDR,
  401. sdr_read32(MSDC0_GPIO_TDSEL0_G5_ADDR));
  402. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_RDSEL0_G5_ADDR,
  403. sdr_read32(MSDC0_GPIO_RDSEL0_G5_ADDR));
  404. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_DRV0_G5_ADDR,
  405. sdr_read32(MSDC0_GPIO_DRV0_G5_ADDR));
  406. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_PUPD0_G5_ADDR,
  407. sdr_read32(MSDC0_GPIO_PUPD0_G5_ADDR));
  408. pr_err("0:GPIO[%p]=0x%.8x\n", MSDC0_GPIO_PUPD1_G5_ADDR,
  409. sdr_read32(MSDC0_GPIO_PUPD1_G5_ADDR));
  410. break;
  411. case 1:
  412. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_MODE17_ADDR,
  413. sdr_read32(MSDC1_GPIO_MODE17_ADDR));
  414. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_MODE18_ADDR,
  415. sdr_read32(MSDC1_GPIO_MODE18_ADDR));
  416. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_IES_G4_ADDR,
  417. sdr_read32(MSDC1_GPIO_IES_G4_ADDR));
  418. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_SMT_G4_ADDR,
  419. sdr_read32(MSDC1_GPIO_SMT_G4_ADDR));
  420. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_TDSEL0_G4_ADDR,
  421. sdr_read32(MSDC1_GPIO_TDSEL0_G4_ADDR));
  422. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_RDSEL0_G4_ADDR,
  423. sdr_read32(MSDC1_GPIO_RDSEL0_G4_ADDR));
  424. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_DRV0_G4_ADDR,
  425. sdr_read32(MSDC1_GPIO_DRV0_G4_ADDR));
  426. pr_err("1:GPIO[%p]=0x%.8x\n", MSDC1_GPIO_PUPD0_G4_ADDR,
  427. sdr_read32(MSDC1_GPIO_PUPD0_G4_ADDR));
  428. break;
  429. #ifdef CFG_DEV_MSDC2
  430. case 2:
  431. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_MODE20_ADDR,
  432. sdr_read32(MSDC2_GPIO_MODE20_ADDR));
  433. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_MODE21_ADDR,
  434. sdr_read32(MSDC2_GPIO_MODE21_ADDR));
  435. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_IES_G0_ADDR,
  436. sdr_read32(MSDC2_GPIO_IES_G0_ADDR));
  437. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_SMT_G0_ADDR,
  438. sdr_read32(MSDC2_GPIO_SMT_G0_ADDR));
  439. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_TDSEL0_G0_ADDR,
  440. sdr_read32(MSDC2_GPIO_TDSEL0_G0_ADDR));
  441. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_RDSEL0_G0_ADDR,
  442. sdr_read32(MSDC2_GPIO_RDSEL0_G0_ADDR));
  443. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_DRV0_G0_ADDR,
  444. sdr_read32(MSDC2_GPIO_DRV0_G0_ADDR));
  445. pr_err("2:GPIO[%p]=0x%.8x\n", MSDC2_GPIO_PUPD0_G0_ADDR,
  446. sdr_read32(MSDC2_GPIO_PUPD0_G0_ADDR));
  447. break;
  448. #endif
  449. }
  450. }
  451. void msdc_dump_register(struct msdc_host *host)
  452. {
  453. void __iomem *base = host->base;
  454. int i = host->id;
  455. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  456. i, OFFSET_MSDC_CFG, sdr_read32(MSDC_CFG),
  457. OFFSET_MSDC_IOCON, sdr_read32(MSDC_IOCON),
  458. OFFSET_MSDC_PS, sdr_read32(MSDC_PS),
  459. OFFSET_MSDC_INT, sdr_read32(MSDC_INT)
  460. );
  461. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  462. i, OFFSET_MSDC_INTEN, sdr_read32(MSDC_INTEN),
  463. OFFSET_MSDC_FIFOCS, sdr_read32(MSDC_FIFOCS),
  464. OFFSET_SDC_CFG, sdr_read32(SDC_CFG),
  465. OFFSET_SDC_CMD, sdr_read32(SDC_CMD)
  466. );
  467. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  468. i, OFFSET_SDC_ARG, sdr_read32(SDC_ARG),
  469. OFFSET_SDC_STS, sdr_read32(SDC_STS),
  470. OFFSET_SDC_RESP0, sdr_read32(SDC_RESP0),
  471. OFFSET_SDC_RESP1, sdr_read32(SDC_RESP1)
  472. );
  473. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  474. i, OFFSET_SDC_RESP2, sdr_read32(SDC_RESP2),
  475. OFFSET_SDC_RESP3, sdr_read32(SDC_RESP3),
  476. OFFSET_SDC_BLK_NUM, sdr_read32(SDC_BLK_NUM),
  477. OFFSET_SDC_VOL_CHG, sdr_read32(SDC_VOL_CHG)
  478. );
  479. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  480. i, OFFSET_SDC_CSTS, sdr_read32(SDC_CSTS),
  481. OFFSET_SDC_CSTS_EN, sdr_read32(SDC_CSTS_EN),
  482. OFFSET_SDC_DCRC_STS, sdr_read32(SDC_DCRC_STS),
  483. OFFSET_EMMC_CFG0, sdr_read32(EMMC_CFG0)
  484. );
  485. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  486. i, OFFSET_EMMC_CFG1, sdr_read32(EMMC_CFG1),
  487. OFFSET_EMMC_STS, sdr_read32(EMMC_STS),
  488. OFFSET_EMMC_IOCON, sdr_read32(EMMC_IOCON),
  489. OFFSET_SDC_ACMD_RESP, sdr_read32(SDC_ACMD_RESP)
  490. );
  491. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  492. i, OFFSET_SDC_ACMD19_TRG, sdr_read32(SDC_ACMD19_TRG),
  493. OFFSET_SDC_ACMD19_STS, sdr_read32(SDC_ACMD19_STS),
  494. OFFSET_MSDC_DMA_SA_HIGH4BIT, sdr_read32(MSDC_DMA_SA_HIGH4BIT),
  495. OFFSET_MSDC_DMA_SA, sdr_read32(MSDC_DMA_SA)
  496. );
  497. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  498. i, OFFSET_MSDC_DMA_CA, sdr_read32(MSDC_DMA_CA),
  499. OFFSET_MSDC_DMA_CTRL, sdr_read32(MSDC_DMA_CTRL),
  500. OFFSET_MSDC_DMA_CFG, sdr_read32(MSDC_DMA_CFG),
  501. OFFSET_MSDC_DMA_LEN, sdr_read32(MSDC_DMA_LEN)
  502. );
  503. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  504. i, OFFSET_MSDC_DBG_SEL, sdr_read32(MSDC_DBG_SEL),
  505. OFFSET_MSDC_DBG_OUT, sdr_read32(MSDC_DBG_OUT),
  506. OFFSET_MSDC_PATCH_BIT0, sdr_read32(MSDC_PATCH_BIT0),
  507. OFFSET_MSDC_PATCH_BIT1, sdr_read32(MSDC_PATCH_BIT1)
  508. );
  509. pr_err("sd%d R[%x]=0x%.8x\n", i, OFFSET_MSDC_PATCH_BIT2,
  510. sdr_read32(MSDC_PATCH_BIT2));
  511. if ((host->id == 2) || (host->id == 3)) {
  512. pr_err("sd%d R[%x]=0x%.8x\n", i, OFFSET_DAT0_TUNE_CRC,
  513. sdr_read32(DAT0_TUNE_CRC));
  514. pr_err("sd%d R[%x]=0x%.8x\n", i, OFFSET_DAT0_TUNE_CRC,
  515. sdr_read32(DAT1_TUNE_CRC));
  516. pr_err("sd%d R[%x]=0x%.8x\n", i, OFFSET_DAT0_TUNE_CRC,
  517. sdr_read32(DAT2_TUNE_CRC));
  518. pr_err("sd%d R[%x]=0x%.8x\n", i, OFFSET_DAT0_TUNE_CRC,
  519. sdr_read32(DAT3_TUNE_CRC));
  520. pr_err("sd%d R[%x]=0x%.8x\n", i, OFFSET_CMD_TUNE_CRC,
  521. sdr_read32(CMD_TUNE_CRC));
  522. pr_err("sd%d R[%x]=0x%.8x\n", i, OFFSET_SDIO_TUNE_WIND,
  523. sdr_read32(SDIO_TUNE_WIND));
  524. }
  525. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  526. i, OFFSET_MSDC_PAD_TUNE0, sdr_read32(MSDC_PAD_TUNE0),
  527. OFFSET_MSDC_PAD_TUNE1, sdr_read32(MSDC_PAD_TUNE1),
  528. OFFSET_MSDC_DAT_RDDLY0, sdr_read32(MSDC_DAT_RDDLY0),
  529. OFFSET_MSDC_DAT_RDDLY1, sdr_read32(MSDC_DAT_RDDLY1)
  530. );
  531. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  532. i, OFFSET_MSDC_DAT_RDDLY2, sdr_read32(MSDC_DAT_RDDLY2),
  533. OFFSET_MSDC_DAT_RDDLY3, sdr_read32(MSDC_DAT_RDDLY3),
  534. OFFSET_MSDC_HW_DBG, sdr_read32(MSDC_HW_DBG),
  535. OFFSET_MSDC_VERSION, sdr_read32(MSDC_VERSION)
  536. );
  537. if (host->id == 0) {
  538. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  539. i, OFFSET_EMMC50_PAD_DS_TUNE, sdr_read32(EMMC50_PAD_DS_TUNE),
  540. OFFSET_EMMC50_PAD_CMD_TUNE, sdr_read32(EMMC50_PAD_CMD_TUNE),
  541. OFFSET_EMMC50_PAD_DAT01_TUNE, sdr_read32(EMMC50_PAD_DAT01_TUNE),
  542. OFFSET_EMMC50_PAD_DAT23_TUNE, sdr_read32(EMMC50_PAD_DAT23_TUNE)
  543. );
  544. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  545. i, OFFSET_EMMC50_PAD_DAT45_TUNE, sdr_read32(EMMC50_PAD_DAT45_TUNE),
  546. OFFSET_EMMC50_PAD_DAT67_TUNE, sdr_read32(EMMC50_PAD_DAT67_TUNE),
  547. OFFSET_EMMC50_CFG0, sdr_read32(EMMC50_CFG0),
  548. OFFSET_EMMC50_CFG1, sdr_read32(EMMC50_CFG1)
  549. );
  550. pr_err("sd%d R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x R[%x]=0x%.8x\n",
  551. i, OFFSET_EMMC50_CFG1, sdr_read32(EMMC50_CFG1),
  552. OFFSET_EMMC50_CFG2, sdr_read32(EMMC50_CFG2),
  553. OFFSET_EMMC50_CFG3, sdr_read32(EMMC50_CFG3),
  554. OFFSET_EMMC50_CFG4, sdr_read32(EMMC50_CFG4)
  555. );
  556. }
  557. }
  558. static void msdc_dump_dbg_register(struct msdc_host *host)
  559. {
  560. void __iomem *base = host->base;
  561. u32 i;
  562. for (i = 0; i <= 0xd; i++) {
  563. sdr_write32(MSDC_DBG_SEL, i);
  564. pr_err("sd%d SEL:r[%x]=0x%x OUT:r[%x]=0x%x\n",
  565. host->id, OFFSET_MSDC_DBG_SEL, i, OFFSET_MSDC_DBG_OUT,
  566. sdr_read32(MSDC_DBG_OUT));
  567. }
  568. sdr_write32(MSDC_DBG_SEL, 0);
  569. }
  570. static void msdc_dump_clock_sts(struct msdc_host *host)
  571. {
  572. #ifdef MTK_MSDC_BRINGUP_DEBUG
  573. if (!(apmixed_reg_base1 && topckgen_reg_base && pericfg_reg_base)) {
  574. pr_err("apmixed_reg_base=%p,topckgen_reg_base=%p,clk_pericfg_base=%p\n",
  575. apmixed_reg_base1, topckgen_reg_base, pericfg_reg_base);
  576. return;
  577. }
  578. pr_err("MSDCPLL_PWR_CON0[0x%p][bit0~1 should be 2b'01]=0x%x\n",
  579. (apmixed_reg_base1 + MSDC_MSDCPLL_PWR_CON0_OFFSET),
  580. sdr_read32(apmixed_reg_base1 + MSDC_MSDCPLL_PWR_CON0_OFFSET));
  581. pr_err("MSDCPLL_CON0 [0x%p][bit0 should be 1b'1]=0x%x\n",
  582. (apmixed_reg_base1 + MSDC_MSDCPLL_CON0_OFFSET),
  583. sdr_read32(apmixed_reg_base1 + MSDC_MSDCPLL_CON0_OFFSET));
  584. pr_err("CLK_CFG_2 [0x%p][bit[31:24]should be 0x01]=0x%x\n",
  585. (topckgen_reg_base + MSDC_CLK_CFG_2_OFFSET),
  586. sdr_read32(topckgen_reg_base + MSDC_CLK_CFG_2_OFFSET));
  587. pr_err("CLK_CFG_3 [0x%p][bit[15:0]should be 0x0202]=0x%x\n",
  588. (topckgen_reg_base + MSDC_CLK_CFG_3_OFFSET),
  589. sdr_read32(topckgen_reg_base + MSDC_CLK_CFG_3_OFFSET));
  590. pr_err("PERI_PDN_STA0 [0x%p][bit13=msdc0, bit14=msdc1,0:on,1:off]=0x%x\n",
  591. (pericfg_reg_base + MSDC_PERI_PDN_STA0_OFFSET),
  592. sdr_read32(pericfg_reg_base + MSDC_PERI_PDN_STA0_OFFSET));
  593. #endif
  594. }
  595. static void msdc_dump_ldo_sts(struct msdc_host *host)
  596. {
  597. #ifdef MTK_MSDC_BRINGUP_DEBUG
  598. #if 0
  599. u32 ldo_en = 0, ldo_vol = 0;
  600. switch (host->id) {
  601. case 0:
  602. pwrap_read(0x0A24, &ldo_en);
  603. pwrap_read(0x0A64, &ldo_vol);
  604. pr_err("VEMC_EN[0x0A24]=0x%x, should:bit1=1\n", ldo_en);
  605. pr_err("VEMC_VOL[0x0A64]=0x%x,should:[bit[5:4]=2b'10]\n", ldo_vol);
  606. break;
  607. case 1:
  608. pwrap_read(0x0A20, &ldo_en);
  609. pwrap_read(0x0A6A, &ldo_vol);
  610. pr_err("VMC_EN[0x0A20]=0x%x, should:bit1=1,", ldo_en);
  611. pr_err("VMC_VOL[0x0A6A]=0x%x,should:bit[5:4]=2b'11(3.3V),2b'00(1.8V)\n",
  612. ldo_vol);
  613. pwrap_read(0x0A1C, &ldo_en);
  614. pwrap_read(0x0A66, &ldo_vol);
  615. pr_err("VMCH_EN[0x0A1C]==0x%x,should:bit1=1", ldo_en);
  616. pr_err("VMCH_VOL[0x0A66]=0x%x,should:bit[5:4]=2b'10(3.3V)\n", ldo_vol);
  617. break;
  618. default:
  619. break;
  620. }
  621. #endif
  622. #endif
  623. }
  624. /*
  625. extern void Ana_Log_Print(void);
  626. extern void Afe_Log_Print(void);
  627. static void dump_audio_info(void)
  628. {
  629. pr_err("=============== AUDIO INFO =============");
  630. #ifndef CONFIG_MTK_FPGA
  631. Ana_Log_Print();
  632. Afe_Log_Print();
  633. #endif
  634. }
  635. */
  636. static void dump_axi_bus_info(void)
  637. {
  638. #if 0
  639. if (infracfg_ao_reg_base && infracfg_reg_base && pericfg_reg_base) {
  640. pr_err("=============== AXI BUS INFO =============");
  641. pr_err("reg[0x10001224]=0x%x", sdr_read32(infracfg_ao_reg_base + 0x224));
  642. pr_err("reg[0x10201000]=0x%x", sdr_read32(infracfg_reg_base + 0x000));
  643. pr_err("reg[0x10201018]=0x%x", sdr_read32(infracfg_reg_base + 0x018));
  644. pr_err("reg[0x1000320c]=0x%x", sdr_read32(pericfg_reg_base + 0x20c));
  645. pr_err("reg[0x10003210]=0x%x", sdr_read32(pericfg_reg_base + 0x210));
  646. pr_err("reg[0x10003214]=0x%x", sdr_read32(pericfg_reg_base + 0x214));
  647. } else
  648. pr_err("infracfg_ao_reg=%p,infracfg_reg_base=%p,pericfg_reg_base=%p\n",
  649. infracfg_ao_reg_base, infracfg_reg_base, pericfg_reg_base);
  650. #endif
  651. return;
  652. }
  653. static void dump_emi_info(void)
  654. {
  655. }
  656. void msdc_dump_info(u32 id)
  657. {
  658. struct msdc_host *host = mtk_msdc_host[id];
  659. void __iomem *base;
  660. if (host == NULL) {
  661. pr_err("msdc host<%d> null\n", id);
  662. return;
  663. }
  664. /* when detect card, cmd13 will be sent which timeout log is not needed */
  665. if (!sd_register_zone[id]) {
  666. pr_err("msdc host<%d> is timeout when detect, so don't dump register\n", id);
  667. return;
  668. }
  669. base = host->base;
  670. /* 1: dump msdc hw register */
  671. msdc_dump_register(host);
  672. pr_err("msdc%d latest_INT_status<0x%.8x>\n", id, latest_int_status[id]);
  673. /* 2: check msdc clock gate and clock source */
  674. mdelay(10);
  675. msdc_dump_clock_sts(host);
  676. /* 3: check msdc pmic ldo */
  677. msdc_dump_ldo_sts(host);
  678. /* 4: check msdc pad control */
  679. msdc_dump_padctl(host);
  680. /* 5: For designer */
  681. mdelay(10);
  682. msdc_dump_dbg_register(host);
  683. }
  684. void msdc_polling_axi_status(int line, int dead)
  685. {
  686. int i = 0;
  687. if (!pericfg_reg_base) {
  688. pr_err("pericfg_reg_base = %p\n", pericfg_reg_base);
  689. return;
  690. }
  691. while (sdr_read32(pericfg_reg_base + 0x214) & 0xc) {
  692. if (++i < 300) {
  693. mdelay(10);
  694. } else {
  695. pr_err("[%s]: check peri-bus: 0x%x at %d\n",
  696. __func__, sdr_read32(pericfg_reg_base + 0x214), line);
  697. pr_err("###### AXI bus hang! start ######");
  698. pr_err("======EMI======");
  699. dump_emi_info();
  700. mdelay(10);
  701. pr_err("======AXI======");
  702. dump_axi_bus_info();
  703. mdelay(10);
  704. pr_err("======AUDIO======");
  705. /* dump_audio_info(); */
  706. mdelay(10);
  707. pr_err("======MSDC======");
  708. msdc_dump_info(0);
  709. mdelay(10);
  710. pr_err("======GPD/BD======");
  711. msdc_dump_gpd_bd(0);
  712. pr_err("####### AXI bus hang! end ######");
  713. if (dead != 0)
  714. i = 0;
  715. else
  716. break;
  717. }
  718. }
  719. }
  720. /*
  721. * for AHB read / write debug
  722. * return DMA status.
  723. */
  724. int msdc_get_dma_status(int host_id)
  725. {
  726. int result = -1;
  727. if (host_id < 0 || host_id >= HOST_MAX_NUM) {
  728. pr_err("[%s] failed to get dma status, bad host_id %d\n",
  729. __func__, host_id);
  730. return result;
  731. }
  732. if (msdc_latest_transfer_mode[host_id] == TRAN_MOD_DMA) {
  733. switch (msdc_latest_operation_type[host_id]) {
  734. case OPER_TYPE_READ:
  735. result = 1; /* DMA read */
  736. break;
  737. case OPER_TYPE_WRITE:
  738. result = 2; /* DMA write */
  739. break;
  740. default:
  741. break;
  742. }
  743. } else if (msdc_latest_transfer_mode[host_id] == TRAN_MOD_PIO) {
  744. result = 0; /* PIO mode */
  745. }
  746. return result;
  747. }
  748. EXPORT_SYMBOL(msdc_get_dma_status);
  749. #ifdef MSDC_DMA_ADDR_DEBUG
  750. struct dma_addr *msdc_get_dma_address(int host_id)
  751. {
  752. struct bd_t *bd;
  753. int i = 0;
  754. int mode = -1;
  755. struct msdc_host *host;
  756. void __iomem *base;
  757. if (host_id < 0 || host_id >= HOST_MAX_NUM) {
  758. pr_err("[%s] failed to get dma status, bad host_id %d\n",
  759. __func__, host_id);
  760. return NULL;
  761. }
  762. if (!mtk_msdc_host[host_id]) {
  763. pr_err("[%s] failed to get dma status, msdc%d is not exist\n",
  764. __func__, host_id);
  765. return NULL;
  766. }
  767. host = mtk_msdc_host[host_id];
  768. base = host->base;
  769. /* spin_lock(&host->lock); */
  770. sdr_get_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, mode);
  771. if (mode == 1) {
  772. pr_crit("Desc.DMA\n");
  773. bd = host->dma.bd;
  774. i = 0;
  775. while (i < MAX_BD_PER_GPD) {
  776. msdc_latest_dma_address[i].start_address = (u32) bd[i].ptr;
  777. msdc_latest_dma_address[i].size = bd[i].buflen;
  778. msdc_latest_dma_address[i].end = bd[i].eol;
  779. if (i > 0)
  780. msdc_latest_dma_address[i - 1].next =
  781. &msdc_latest_dma_address[i];
  782. if (bd[i].eol)
  783. break;
  784. i++;
  785. }
  786. } else if (mode == 0) {
  787. pr_crit("Basic DMA\n");
  788. msdc_latest_dma_address[i].start_address = sdr_read32(MSDC_DMA_SA);
  789. msdc_latest_dma_address[i].size = sdr_read32(MSDC_DMA_LEN);
  790. msdc_latest_dma_address[i].end = 1;
  791. }
  792. /* spin_unlock(&host->lock); */
  793. return msdc_latest_dma_address;
  794. }
  795. EXPORT_SYMBOL(msdc_get_dma_address);
  796. #endif
  797. static void msdc_clr_fifo(unsigned int id)
  798. {
  799. int retry = 3, cnt = 1000;
  800. void __iomem *base;
  801. if (id < 0 || id >= HOST_MAX_NUM)
  802. return;
  803. base = mtk_msdc_host[id]->base;
  804. if (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS) {
  805. pr_err("WARN: msdc%d, clear FIFO when DMA active,MSDC_DMA_CFG=0x%x\n",
  806. id, sdr_read32(MSDC_DMA_CFG));
  807. show_stack(current, NULL);
  808. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
  809. msdc_retry((sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS),
  810. retry, cnt, id);
  811. }
  812. if (retry == 0) {
  813. pr_err("WARN:msdc%d,fail stop DMA before clear FIFO,MSDC_DMA_CFG=0x%x\n"
  814. , id, sdr_read32(MSDC_DMA_CFG));
  815. return;
  816. }
  817. retry = 3;
  818. cnt = 1000;
  819. sdr_set_bits(MSDC_FIFOCS, MSDC_FIFOCS_CLR);
  820. msdc_retry(sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_CLR, retry, cnt, id);
  821. }
  822. static void msdc_reset_hw(unsigned int id)
  823. {
  824. void __iomem *base;
  825. if (id < 0 || id >= HOST_MAX_NUM) {
  826. pr_err("invalid id: %d, HOST_MAX_NUM:%d", id, HOST_MAX_NUM);
  827. return;
  828. }
  829. base = mtk_msdc_host[id]->base;
  830. msdc_reset(id);
  831. msdc_clr_fifo(id);
  832. msdc_clr_int();
  833. }
  834. static int msdc_clk_stable(struct msdc_host *host, u32 mode, u32 div,
  835. u32 hs400_src)
  836. {
  837. void __iomem *base = host->base;
  838. int retry = 0;
  839. int cnt = 1000;
  840. int retry_cnt = 1;
  841. #if defined(CFG_DEV_MSDC3)
  842. /* MSDC3 is dedicated for C2K, need special clock setting */
  843. if (host->id == 3) {
  844. sdr_set_field(MSDC_CFG, MSDC_CFG_CKDIV, 0);
  845. sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD, 1);
  846. sdr_set_field(MSDC_IOCON, MSDC_IOCON_SDR104CKS, 1);
  847. return 0;
  848. }
  849. #endif
  850. do {
  851. retry = 3;
  852. sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD_HS400 | MSDC_CFG_CKMOD
  853. | MSDC_CFG_CKDIV,
  854. (hs400_src << 14) | (mode << 12) | ((div + retry_cnt) % 0xfff));
  855. /* sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD, mode); */
  856. msdc_retry(!(sdr_read32(MSDC_CFG) & MSDC_CFG_CKSTB),
  857. retry, cnt, host->id);
  858. if (retry == 0) {
  859. pr_err("msdc%d host->onclock(%d)\n", host->id, host->core_clkon);
  860. pr_err("msdc%d on clock failed ===> retry twice\n", host->id);
  861. #ifndef FPGA_PLATFORM
  862. #ifdef CONFIG_MTK_CLKMGR
  863. disable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
  864. enable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
  865. #else
  866. clk_disable(host->clock_control);
  867. clk_enable(host->clock_control);
  868. #endif
  869. #endif
  870. msdc_dump_info(host->id);
  871. }
  872. retry = 3;
  873. sdr_set_field(MSDC_CFG, MSDC_CFG_CKDIV, div);
  874. msdc_retry(!(sdr_read32(MSDC_CFG) & MSDC_CFG_CKSTB),
  875. retry, cnt, host->id);
  876. if (retry == 0)
  877. msdc_dump_info(host->id);
  878. msdc_reset_hw(host->id);
  879. if (retry_cnt == 2)
  880. break;
  881. retry_cnt += 1;
  882. } while (!retry);
  883. return 0;
  884. }
  885. /* clock source for host: global */
  886. #ifdef FPGA_PLATFORM
  887. static u32 hclks[] = { 12000000, 12000000, 12000000, 12000000, 12000000,
  888. 12000000, 12000000, 12000000, 0
  889. };
  890. #else
  891. static u32 hclks_msdc50[] = { 26000000, 800000000, 400000000, 200000000,
  892. 182000000, 136000000, 156000000, 416000000,
  893. 48000000, 91000000, 624000000
  894. };
  895. static u32 hclks_msdc30[] = { 26000000, 208000000, 200000000, 182000000,
  896. 136000000, 156000000, 48000000, 91000000
  897. };
  898. static u32 *hclks = hclks_msdc30;
  899. #endif
  900. /*
  901. * VMCH is for T-card main power.
  902. * VMC for T-card when no emmc, for eMMC when has emmc.
  903. * VGP for T-card when has emmc.
  904. */
  905. u32 g_msdc0_io = 0;
  906. u32 g_msdc0_flash = 0;
  907. u32 g_msdc1_io = 0;
  908. u32 g_msdc1_flash = 0;
  909. u32 g_msdc2_io = 0;
  910. u32 g_msdc2_flash = 0;
  911. u32 g_msdc3_io = 0;
  912. u32 g_msdc3_flash = 0;
  913. u32 g_msdc4_io = 0;
  914. u32 g_msdc4_flash = 0;
  915. /* set start bit of data sampling */
  916. void msdc_set_startbit(struct msdc_host *host, u8 start_bit)
  917. {
  918. void __iomem *base = host->base;
  919. /* set start bit */
  920. sdr_set_field(MSDC_CFG, MSDC_CFG_START_BIT, start_bit);
  921. /* ERR_MSG("finished, start_bit=%d\n", start_bit); */
  922. }
  923. /* set the edge of data sampling */
  924. void msdc_set_smpl(struct msdc_host *host, u8 HS400, u8 mode, u8 type, u8 *edge)
  925. {
  926. void __iomem *base = host->base;
  927. int i = 0;
  928. switch (type) {
  929. case TYPE_CMD_RESP_EDGE:
  930. /* eMMC5.0 only output resp at CLK pin, so no need to select DS pin*/
  931. if (HS400) {
  932. sdr_set_field(EMMC50_CFG0, MSDC_EMMC50_CFG_PADCMD_LATCHCK, 0);
  933. sdr_set_field(EMMC50_CFG0, MSDC_EMMC50_CFG_CMD_RESP_SEL, 0);
  934. }
  935. if (mode == MSDC_SMPL_RISING || mode == MSDC_SMPL_FALLING)
  936. #if 0
  937. if (HS400)
  938. sdr_set_field(EMMC50_CFG0, MSDC_EMMC50_CFG_CMD_EDGE_SEL, mode);
  939. else
  940. sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, mode);
  941. #else
  942. sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, mode);
  943. #endif
  944. else
  945. ERR_MSG("invalid resp parameter: HS400=%d, type=%d, mode=%d\n",
  946. HS400, type, mode);
  947. break;
  948. case TYPE_WRITE_CRC_EDGE:
  949. if (HS400) /* latch write crc status at DS pin */
  950. sdr_set_field(EMMC50_CFG0, MSDC_EMMC50_CFG_CRC_STS_SEL, 1);
  951. else /* latch write crc status at CLK pin */
  952. sdr_set_field(EMMC50_CFG0, MSDC_EMMC50_CFG_CRC_STS_SEL, 0);
  953. if (mode == MSDC_SMPL_RISING || mode == MSDC_SMPL_FALLING) {
  954. if (HS400)
  955. sdr_set_field(EMMC50_CFG0, MSDC_EMMC50_CFG_CRC_STS_EDGE, mode);
  956. else {
  957. sdr_set_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL_SEL, 0);
  958. sdr_set_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, mode);
  959. }
  960. } else if ((mode == MSDC_SMPL_SEPARATE) && !HS400 && (edge != NULL))
  961. /* only dat0 is for write crc status */
  962. sdr_set_field(MSDC_IOCON, MSDC_IOCON_W_D0SPL, edge[0]);
  963. else
  964. ERR_MSG("invalid crc parameter: HS400=%d, type=%d, mode=%d\n",
  965. HS400, type, mode);
  966. break;
  967. case TYPE_READ_DATA_EDGE:
  968. /*
  969. * for HS400, start bit is output both on rising and falling edge
  970. * for the other mode, start bit is only output on rising edge.
  971. * but DDR50 can try falling edge if error casued by pad delay
  972. */
  973. if (HS400)
  974. msdc_set_startbit(host, START_AT_RISING_AND_FALLING);
  975. else
  976. msdc_set_startbit(host, START_AT_RISING);
  977. if (mode == MSDC_SMPL_RISING || mode == MSDC_SMPL_FALLING) {
  978. sdr_set_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL_SEL, 0);
  979. sdr_set_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, mode);
  980. } else if ((mode == MSDC_SMPL_SEPARATE) && (edge != NULL)
  981. && (sizeof(edge) == 8)) {
  982. sdr_set_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL_SEL, 1);
  983. for (i = 0; i < 8; i++)
  984. sdr_set_field(MSDC_IOCON, (MSDC_IOCON_R_D0SPL << i), edge[i]);
  985. } else
  986. ERR_MSG("invalid read parameter: HS400=%d, type=%d, mode=%d\n",
  987. HS400, type, mode);
  988. break;
  989. case TYPE_WRITE_DATA_EDGE:
  990. sdr_set_field(EMMC50_CFG0, MSDC_EMMC50_CFG_CRC_STS_SEL, 0);
  991. /* latch write crc status at CLK pin */
  992. if (mode == MSDC_SMPL_RISING || mode == MSDC_SMPL_FALLING) {
  993. sdr_set_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL_SEL, 0);
  994. sdr_set_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, mode);
  995. } else if ((mode == MSDC_SMPL_SEPARATE) && (edge != NULL)
  996. && (sizeof(edge) >= 4)) {
  997. sdr_set_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL_SEL, 1);
  998. for (i = 0; i < 4; i++) { /* dat0~4 is for SDIO card */
  999. sdr_set_field(MSDC_IOCON, (MSDC_IOCON_W_D0SPL << i), edge[i]);
  1000. }
  1001. } else
  1002. ERR_MSG("invalid write parameter: HS400=%d, type=%d, mode=%d\n",
  1003. HS400, type, mode);
  1004. break;
  1005. default:
  1006. ERR_MSG("invalid parameter: HS400=%d, type=%d, mode=%d\n",
  1007. HS400, type, mode);
  1008. break;
  1009. }
  1010. }
  1011. #ifndef FPGA_PLATFORM
  1012. #ifndef CONFIG_MTK_LEGACY
  1013. enum MSDC_LDO_POWER {
  1014. POWER_LDO_VMCH,
  1015. POWER_LDO_VMC,
  1016. POWER_LDO_VEMC_3V3,
  1017. };
  1018. int msdc_hwPowerOn(unsigned int powerId, int powerVolt, char *mode_name)
  1019. {
  1020. int ret = -1;
  1021. struct regulator *reg = NULL;
  1022. if (powerId == POWER_LDO_VMCH)
  1023. reg = reg_vmch;
  1024. else if (powerId == POWER_LDO_VMC)
  1025. reg = reg_vmc;
  1026. else if (powerId == POWER_LDO_VEMC_3V3)
  1027. reg = reg_vemc_3v3;
  1028. if (reg == NULL) {
  1029. pr_err("power on failed, regulator is NULL\n");
  1030. goto out;
  1031. }
  1032. powerVolt = powerVolt * 1000;
  1033. /* New API voltage use micro V */
  1034. regulator_set_voltage(reg, powerVolt, powerVolt);
  1035. ret = regulator_enable(reg);
  1036. if (ret)
  1037. pr_err("power on failed, %s: %d\n", __func__, __LINE__);
  1038. out:
  1039. return ret;
  1040. }
  1041. EXPORT_SYMBOL(msdc_hwPowerOn);
  1042. bool msdc_hwPowerDown(unsigned int powerId, char *mode_name)
  1043. {
  1044. struct regulator *reg = NULL;
  1045. if (powerId == POWER_LDO_VMCH)
  1046. reg = reg_vmch;
  1047. else if (powerId == POWER_LDO_VMC)
  1048. reg = reg_vmc;
  1049. else if (powerId == POWER_LDO_VEMC_3V3)
  1050. reg = reg_vemc_3v3;
  1051. if (reg == NULL)
  1052. return false;
  1053. /* New API voltage use micro V */
  1054. regulator_disable(reg);
  1055. return true;
  1056. }
  1057. EXPORT_SYMBOL(msdc_hwPowerDown);
  1058. static u32 msdc_ldo_power(u32 on, unsigned int powerId, int voltage_uv,
  1059. u32 *status)
  1060. {
  1061. if (on) { /* want to power on */
  1062. if (*status == 0) { /* can power on */
  1063. msdc_hwPowerOn(powerId, voltage_uv, "msdc");
  1064. *status = voltage_uv;
  1065. } else if (*status == voltage_uv) {
  1066. pr_err("msdc LDO<%d><%d> power on again!\n",
  1067. powerId, voltage_uv);
  1068. } else { /* for sd3.0 later */
  1069. pr_warn("msdc LDO<%d> change<%d> to <%d>\n",
  1070. powerId, *status, voltage_uv);
  1071. msdc_hwPowerDown(powerId, "msdc");
  1072. msdc_hwPowerOn(powerId, voltage_uv, "msdc");
  1073. *status = voltage_uv;
  1074. }
  1075. } else { /* want to power off */
  1076. if (*status != 0) { /* has been powerred on */
  1077. msdc_hwPowerDown(powerId, "msdc");
  1078. *status = 0;
  1079. } else
  1080. pr_err("LDO<%d> not power on\n", powerId);
  1081. }
  1082. return 0;
  1083. }
  1084. #else
  1085. static u32 msdc_ldo_power(u32 on, MT65XX_POWER powerId, int voltage_uv,
  1086. u32 *status)
  1087. {
  1088. if (on) {
  1089. if (*status == 0) {
  1090. pr_warn("msdc LDO<%d> power on<%d>\n", powerId, voltage_uv);
  1091. hwPowerOn(powerId, voltage_uv, "msdc");
  1092. *status = voltage_uv;
  1093. } else if (*status == voltage_uv) {
  1094. pr_err("msdc LDO<%d><%d> power on again!\n", powerId, voltage_uv);
  1095. } else { /* for sd3.0 later */
  1096. pr_warn("msdc LDO<%d> change<%d> to <%d>\n",
  1097. powerId, *status, voltage_uv);
  1098. hwPowerDown(powerId, "msdc");
  1099. hwPowerOn(powerId, voltage_uv, "msdc");
  1100. *status = voltage_uv;
  1101. }
  1102. } else {
  1103. if (*status != 0) {
  1104. pr_warn("msdc LDO<%d> power off\n", powerId);
  1105. hwPowerDown(powerId, "msdc");
  1106. *status = 0;
  1107. } else
  1108. pr_err("LDO<%d> not power on\n", powerId);
  1109. }
  1110. return 0;
  1111. }
  1112. #endif
  1113. void msdc_sd_power_off(void)
  1114. {
  1115. pr_err("SD overheat,pmic Eint disable SD power!\n");
  1116. msdc_ldo_power(0, MT6328_POWER_LDO_VMC, VOL_3000, &g_msdc1_io);
  1117. msdc_ldo_power(0, MT6328_POWER_LDO_VMCH, VOL_3000, &g_msdc1_flash);
  1118. }
  1119. void msdc_set_smt(struct msdc_host *host, int set_smt)
  1120. {
  1121. switch (host->id) {
  1122. case 0:
  1123. if (set_smt)
  1124. sdr_set_field(MSDC0_GPIO_SMT_G5_ADDR, MSDC0_SMT_ALL_MASK, 0x1F);
  1125. else
  1126. sdr_set_field(MSDC0_GPIO_SMT_G5_ADDR, MSDC0_SMT_ALL_MASK, 0x0);
  1127. break;
  1128. case 1:
  1129. if (set_smt)
  1130. sdr_set_field(MSDC1_GPIO_SMT_G4_ADDR, MSDC1_SMT_ALL_MASK, 0x7);
  1131. else
  1132. sdr_set_field(MSDC1_GPIO_SMT_G4_ADDR, MSDC1_SMT_ALL_MASK, 0x0);
  1133. break;
  1134. #ifdef CFG_DEV_MSDC2 /* FIXME: For 6630 */
  1135. case 2:
  1136. if (set_smt)
  1137. sdr_set_field(MSDC2_GPIO_SMT_G0_ADDR, MSDC2_SMT_ALL_MASK, 0x7);
  1138. else
  1139. sdr_set_field(MSDC2_GPIO_SMT_G0_ADDR, MSDC2_SMT_ALL_MASK, 0x0);
  1140. break;
  1141. #endif
  1142. default:
  1143. pr_err("error...[%s] host->id out of range!!!\n", __func__);
  1144. break;
  1145. }
  1146. }
  1147. void msdc_set_tdsel(struct msdc_host *host, bool sleep)
  1148. {
  1149. switch (host->id) {
  1150. case 0:
  1151. sdr_set_field(MSDC0_GPIO_TDSEL0_G5_ADDR, MSDC0_TDSEL_ALL_MASK, 0);
  1152. break;
  1153. case 1:
  1154. if (sleep)
  1155. sdr_set_field(MSDC1_GPIO_TDSEL0_G4_ADDR,
  1156. MSDC1_TDSEL_ALL_MASK, 0xFFF);
  1157. else
  1158. sdr_set_field(MSDC1_GPIO_TDSEL0_G4_ADDR,
  1159. MSDC1_TDSEL_ALL_MASK, 0xAAA);
  1160. break;
  1161. #ifdef CFG_DEV_MSDC2 /* FIXME: For 6630 */
  1162. case 2:
  1163. sdr_set_field(MSDC2_GPIO_TDSEL0_G0_ADDR, MSDC2_TDSEL_ALL_MASK, 0);
  1164. break;
  1165. #endif
  1166. default:
  1167. pr_err("error...[%s] host->id out of range!!!\n", __func__);
  1168. break;
  1169. }
  1170. }
  1171. void msdc_set_rdsel(struct msdc_host *host, bool sd_18)
  1172. {
  1173. switch (host->id) {
  1174. case 0:
  1175. sdr_set_field(MSDC0_GPIO_RDSEL0_G5_ADDR, MSDC0_RDSEL_ALL_MASK, 0);
  1176. break;
  1177. case 1:
  1178. if (sd_18)
  1179. sdr_set_field(MSDC1_GPIO_RDSEL0_G4_ADDR, MSDC1_RDSEL_ALL_MASK, 0);
  1180. else
  1181. sdr_set_field(MSDC1_GPIO_RDSEL0_G4_ADDR,
  1182. MSDC1_RDSEL_ALL_MASK, 0xC30C);
  1183. break;
  1184. #ifdef CFG_DEV_MSDC2 /* FIXME: For 6630 */
  1185. case 2:
  1186. sdr_set_field(MSDC2_GPIO_RDSEL0_G0_ADDR, MSDC2_RDSEL_ALL_MASK, 0);
  1187. break;
  1188. #endif
  1189. default:
  1190. pr_err("error...[%s] host->id out of range!!!\n", __func__);
  1191. break;
  1192. }
  1193. }
  1194. void msdc_set_rdtdsel_dbg(struct msdc_host *host, bool rdsel, u32 value)
  1195. {
  1196. if (rdsel) {
  1197. switch (host->id) {
  1198. case 0:
  1199. sdr_set_field(MSDC0_GPIO_RDSEL0_G5_ADDR,
  1200. MSDC0_RDSEL_CMD_MASK, value);
  1201. sdr_set_field(MSDC0_GPIO_RDSEL0_G5_ADDR,
  1202. MSDC0_RDSEL_DSL_MASK, value);
  1203. sdr_set_field(MSDC0_GPIO_RDSEL0_G5_ADDR,
  1204. MSDC0_RDSEL_CLK_MASK, value);
  1205. sdr_set_field(MSDC0_GPIO_RDSEL0_G5_ADDR,
  1206. MSDC0_RDSEL_DAT_MASK, value);
  1207. sdr_set_field(MSDC0_GPIO_RDSEL0_G5_ADDR,
  1208. MSDC0_RDSEL_RSTB_MASK, value);
  1209. break;
  1210. case 1:
  1211. sdr_set_field(MSDC1_GPIO_RDSEL0_G4_ADDR,
  1212. MSDC1_RDSEL_CMD_MASK, value);
  1213. sdr_set_field(MSDC1_GPIO_RDSEL0_G4_ADDR,
  1214. MSDC1_RDSEL_CLK_MASK, value);
  1215. sdr_set_field(MSDC1_GPIO_RDSEL0_G4_ADDR,
  1216. MSDC1_RDSEL_DAT_MASK, value);
  1217. break;
  1218. case 2:
  1219. sdr_set_field(MSDC2_GPIO_RDSEL0_G0_ADDR,
  1220. MSDC2_RDSEL_CMD_MASK, value);
  1221. sdr_set_field(MSDC2_GPIO_RDSEL0_G0_ADDR,
  1222. MSDC2_RDSEL_CLK_MASK, value);
  1223. sdr_set_field(MSDC2_GPIO_RDSEL0_G0_ADDR,
  1224. MSDC2_RDSEL_DAT_MASK, value);
  1225. break;
  1226. }
  1227. } else {
  1228. switch (host->id) {
  1229. case 0:
  1230. sdr_set_field(MSDC0_GPIO_TDSEL0_G5_ADDR,
  1231. MSDC0_TDSEL_CMD_MASK, value);
  1232. sdr_set_field(MSDC0_GPIO_TDSEL0_G5_ADDR,
  1233. MSDC0_TDSEL_DSL_MASK, value);
  1234. sdr_set_field(MSDC0_GPIO_TDSEL0_G5_ADDR,
  1235. MSDC0_TDSEL_CLK_MASK, value);
  1236. sdr_set_field(MSDC0_GPIO_TDSEL0_G5_ADDR,
  1237. MSDC0_TDSEL_DAT_MASK, value);
  1238. sdr_set_field(MSDC0_GPIO_TDSEL0_G5_ADDR,
  1239. MSDC0_TDSEL_RSTB_MASK, value);
  1240. break;
  1241. case 1:
  1242. sdr_set_field(MSDC1_GPIO_TDSEL0_G4_ADDR,
  1243. MSDC1_TDSEL_CMD_MASK, value);
  1244. sdr_set_field(MSDC1_GPIO_TDSEL0_G4_ADDR,
  1245. MSDC1_TDSEL_CLK_MASK, value);
  1246. sdr_set_field(MSDC1_GPIO_TDSEL0_G4_ADDR,
  1247. MSDC1_TDSEL_DAT_MASK, value);
  1248. break;
  1249. case 2:
  1250. sdr_set_field(MSDC2_GPIO_TDSEL0_G0_ADDR,
  1251. MSDC2_TDSEL_CMD_MASK, value);
  1252. sdr_set_field(MSDC2_GPIO_TDSEL0_G0_ADDR,
  1253. MSDC2_TDSEL_CLK_MASK, value);
  1254. sdr_set_field(MSDC2_GPIO_TDSEL0_G0_ADDR,
  1255. MSDC2_TDSEL_DAT_MASK, value);
  1256. break;
  1257. }
  1258. }
  1259. }
  1260. void msdc_get_rdtdsel_dbg(struct msdc_host *host, bool rdsel, u32 *value)
  1261. {
  1262. if (rdsel) {
  1263. switch (host->id) {
  1264. case 0:
  1265. sdr_get_field(MSDC0_GPIO_RDSEL0_G5_ADDR,
  1266. MSDC0_RDSEL_CMD_MASK, *value);
  1267. break;
  1268. case 1:
  1269. sdr_get_field(MSDC1_GPIO_RDSEL0_G4_ADDR,
  1270. MSDC1_RDSEL_CMD_MASK, *value);
  1271. break;
  1272. case 2:
  1273. sdr_get_field(MSDC2_GPIO_RDSEL0_G0_ADDR,
  1274. MSDC2_RDSEL_CMD_MASK, *value);
  1275. break;
  1276. }
  1277. } else {
  1278. switch (host->id) {
  1279. case 0:
  1280. sdr_get_field(MSDC0_GPIO_TDSEL0_G5_ADDR,
  1281. MSDC0_TDSEL_CMD_MASK, *value);
  1282. break;
  1283. case 1:
  1284. sdr_get_field(MSDC1_GPIO_TDSEL0_G4_ADDR,
  1285. MSDC1_TDSEL_CMD_MASK, *value);
  1286. break;
  1287. case 2:
  1288. sdr_get_field(MSDC2_GPIO_TDSEL0_G0_ADDR,
  1289. MSDC2_TDSEL_CMD_MASK, *value);
  1290. break;
  1291. }
  1292. }
  1293. }
  1294. void msdc_set_sr(struct msdc_host *host, int clk, int cmd, int dat, int rst,
  1295. int ds)
  1296. {
  1297. switch (host->id) {
  1298. case 0:
  1299. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_SR_CMD_MASK, (cmd != 0));
  1300. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_SR_DSL_MASK, (ds != 0));
  1301. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_SR_CLK_MASK, (clk != 0));
  1302. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_SR_DAT_MASK, (dat != 0));
  1303. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR, MSDC0_SR_RSTB_MASK, (rst != 0));
  1304. break;
  1305. case 1:
  1306. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR, MSDC1_SR_CMD_MASK, (cmd != 0));
  1307. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR, MSDC1_SR_CLK_MASK, (clk != 0));
  1308. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR, MSDC1_SR_DAT_MASK, (dat != 0));
  1309. break;
  1310. #ifdef CFG_DEV_MSDC2 /* FIXME: For 6630 */
  1311. case 2:
  1312. sdr_set_field(MSDC2_GPIO_DRV0_G0_ADDR, MSDC2_SR_CMD_MASK, (cmd != 0));
  1313. sdr_set_field(MSDC2_GPIO_DRV0_G0_ADDR, MSDC2_SR_CLK_MASK, (clk != 0));
  1314. sdr_set_field(MSDC2_GPIO_DRV0_G0_ADDR, MSDC2_SR_DAT_MASK, (dat != 0));
  1315. break;
  1316. #endif
  1317. default:
  1318. pr_err("error...[%s] host->id out of range!!!\n", __func__);
  1319. break;
  1320. }
  1321. }
  1322. void msdc_set_driving(struct msdc_host *host, struct msdc_hw *hw, bool sd_18)
  1323. {
  1324. switch (host->id) {
  1325. case 0:
  1326. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR,
  1327. MSDC0_DRV_CMD_MASK, hw->cmd_drv);
  1328. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR,
  1329. MSDC0_DRV_DSL_MASK, hw->ds_drv);
  1330. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR,
  1331. MSDC0_DRV_CLK_MASK, hw->clk_drv);
  1332. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR,
  1333. MSDC0_DRV_DAT_MASK, hw->dat_drv);
  1334. sdr_set_field(MSDC0_GPIO_DRV0_G5_ADDR,
  1335. MSDC0_DRV_RSTB_MASK, hw->rst_drv);
  1336. break;
  1337. case 1:
  1338. if (sd_18) {
  1339. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR,
  1340. MSDC1_DRV_CMD_MASK, hw->cmd_drv_sd_18);
  1341. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR,
  1342. MSDC1_DRV_CLK_MASK, hw->clk_drv_sd_18);
  1343. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR,
  1344. MSDC1_DRV_DAT_MASK, hw->dat_drv_sd_18);
  1345. } else {
  1346. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR,
  1347. MSDC1_DRV_CMD_MASK, hw->cmd_drv);
  1348. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR,
  1349. MSDC1_DRV_CLK_MASK, hw->clk_drv);
  1350. sdr_set_field(MSDC1_GPIO_DRV0_G4_ADDR,
  1351. MSDC1_DRV_DAT_MASK, hw->dat_drv);
  1352. }
  1353. break;
  1354. #ifdef CFG_DEV_MSDC2
  1355. case 2:
  1356. sdr_set_field(MSDC2_GPIO_DRV0_G0_ADDR,
  1357. MSDC2_DRV_CMD_MASK, hw->cmd_drv);
  1358. sdr_set_field(MSDC2_GPIO_DRV0_G0_ADDR,
  1359. MSDC2_DRV_CLK_MASK, hw->clk_drv);
  1360. sdr_set_field(MSDC2_GPIO_DRV0_G0_ADDR,
  1361. MSDC2_DRV_DAT_MASK, hw->dat_drv);
  1362. break;
  1363. #endif
  1364. default:
  1365. pr_err("error...[%s] host->id out of range!!!\n", __func__);
  1366. break;
  1367. }
  1368. }
  1369. static void msdc_pin_pud(struct msdc_host *host, u32 mode)
  1370. {
  1371. switch (host->id) {
  1372. case 0:
  1373. /*
  1374. * High-Z
  1375. * cmd/clk/dat/(rstb)/dsl:pd-50k
  1376. * clk/dsl:pd-50k, cmd/dat:pu-10k, (rstb:pu-50k)
  1377. */
  1378. if (MSDC_PIN_PULL_NONE == mode) {
  1379. sdr_set_field(MSDC0_GPIO_PUPD0_G5_ADDR,
  1380. MSDC0_PUPD_CMD_DSL_CLK_DAT04_MASK, 0x44444444);
  1381. sdr_set_field(MSDC0_GPIO_PUPD1_G5_ADDR,
  1382. MSDC0_PUPD_DAT567_MASK, 0x444);
  1383. } else if (MSDC_PIN_PULL_DOWN == mode) {
  1384. sdr_set_field(MSDC0_GPIO_PUPD0_G5_ADDR,
  1385. MSDC0_PUPD_CMD_DSL_CLK_DAT04_MASK, 0x66666666);
  1386. sdr_set_field(MSDC0_GPIO_PUPD1_G5_ADDR,
  1387. MSDC0_PUPD_DAT567_MASK, 0x666);
  1388. } else if (MSDC_PIN_PULL_UP == mode) {
  1389. sdr_set_field(MSDC0_GPIO_PUPD0_G5_ADDR,
  1390. MSDC0_PUPD_CMD_DSL_CLK_DAT04_MASK, 0x11111661);
  1391. sdr_set_field(MSDC0_GPIO_PUPD1_G5_ADDR,
  1392. MSDC0_PUPD_DAT567_MASK, 0x111);
  1393. }
  1394. break;
  1395. case 1:
  1396. /*
  1397. * High-Z
  1398. * cmd/clk/dat:pd-50k
  1399. * cmd/dat:pu-50k, clk:pd-50k
  1400. */
  1401. if (MSDC_PIN_PULL_NONE == mode)
  1402. sdr_set_field(MSDC1_GPIO_PUPD0_G4_ADDR,
  1403. MSDC1_PUPD_CMD_CLK_DAT_MASK, 0x444444);
  1404. else if (MSDC_PIN_PULL_DOWN == mode)
  1405. sdr_set_field(MSDC1_GPIO_PUPD0_G4_ADDR,
  1406. MSDC1_PUPD_CMD_CLK_DAT_MASK, 0x666666);
  1407. else if (MSDC_PIN_PULL_UP == mode)
  1408. sdr_set_field(MSDC1_GPIO_PUPD0_G4_ADDR,
  1409. MSDC1_PUPD_CMD_CLK_DAT_MASK, 0x222262);
  1410. break;
  1411. #ifdef CFG_DEV_MSDC2
  1412. case 2:
  1413. if (MSDC_PIN_PULL_NONE == mode)
  1414. sdr_set_field(MSDC2_GPIO_PUPD0_G0_ADDR,
  1415. MSDC2_PUPD_CMD_CLK_DAT_MASK, 0x444444);
  1416. else if (MSDC_PIN_PULL_DOWN == mode)
  1417. sdr_set_field(MSDC2_GPIO_PUPD0_G0_ADDR,
  1418. MSDC2_PUPD_CMD_CLK_DAT_MASK, 0x666666);
  1419. else if (MSDC_PIN_PULL_UP == mode)
  1420. sdr_set_field(MSDC2_GPIO_PUPD0_G0_ADDR,
  1421. MSDC2_PUPD_CMD_CLK_DAT_MASK, 0x222262);
  1422. break;
  1423. #endif
  1424. default:
  1425. pr_err("error...[%s] host->id out of range!!!\n", __func__);
  1426. break;
  1427. }
  1428. }
  1429. #ifndef CONFIG_MTK_LEGACY
  1430. static void msdc_emmc_power(struct msdc_host *host, u32 on)
  1431. {
  1432. unsigned long tmo = 0;
  1433. void __iomem *base = host->base;
  1434. /* if MMC_CAP_WAIT_WHILE_BUSY not set,
  1435. * mmc core layer will loop for wait sa_timeout
  1436. */
  1437. if (host->mmc && host->mmc->card
  1438. && (host->mmc->caps & MMC_CAP_WAIT_WHILE_BUSY) && (on == 0)) {
  1439. /* max timeout: 1000ms */
  1440. if ((DIV_ROUND_UP(host->mmc->card->ext_csd.sa_timeout, 10000)) < 1000)
  1441. tmo = jiffies + DIV_ROUND_UP(host->mmc->card->ext_csd.sa_timeout,
  1442. 10000000 / HZ) + HZ / 100;
  1443. else
  1444. tmo = jiffies + HZ;
  1445. while ((sdr_read32(MSDC_PS) & 0x10000) != 0x10000) {
  1446. if (time_after(jiffies, tmo)) {
  1447. ERR_MSG("Dat0 keep low before power off, sa_timeout = 0x%x",
  1448. host->mmc->card->ext_csd.sa_timeout);
  1449. emmc_sleep_failed = 1;
  1450. break;
  1451. }
  1452. }
  1453. }
  1454. /* Set to 3.0V - 100mV
  1455. * 4'b0000: 0 mV
  1456. * 4'b0001: -20 mV
  1457. * 4'b0010: -40 mV
  1458. * 4'b0011: -60 mV
  1459. * 4'b0100: -80 mV
  1460. * 4'b0101: -100 mV
  1461. */
  1462. msdc_ldo_power(on, POWER_LDO_VEMC_3V3, VOL_3000, &g_msdc0_flash);
  1463. /* mt6325_upmu_set_rg_vemc_3v3_cal(0x5); */
  1464. msdc_dump_ldo_sts(host);
  1465. }
  1466. static void msdc_sd_power(struct msdc_host *host, u32 on)
  1467. {
  1468. switch (host->id) {
  1469. case 1:
  1470. msdc_set_driving(host, host->hw, 0);
  1471. msdc_set_rdsel(host, 0);
  1472. if (host->hw->flags & MSDC_SD_NEED_POWER)
  1473. msdc_ldo_power(1, POWER_LDO_VMCH, VOL_3000, &g_msdc1_flash);
  1474. else
  1475. msdc_ldo_power(on, POWER_LDO_VMCH, VOL_3000, &g_msdc1_flash);
  1476. msdc_ldo_power(on, POWER_LDO_VMC, VOL_3000, &g_msdc1_io);
  1477. if (on)
  1478. upmu_set_rg_vmc_184(0); /* workarond for denali */
  1479. break;
  1480. case 2:
  1481. msdc_set_driving(host, host->hw, 0);
  1482. msdc_set_rdsel(host, 0);
  1483. msdc_ldo_power(on, POWER_LDO_VMC, VOL_3000, &g_msdc2_io);
  1484. msdc_ldo_power(on, POWER_LDO_VMCH, VOL_3000, &g_msdc2_flash);
  1485. break;
  1486. default:
  1487. break;
  1488. }
  1489. msdc_dump_ldo_sts(host);
  1490. }
  1491. static void msdc_sd_power_switch(struct msdc_host *host, u32 on)
  1492. {
  1493. switch (host->id) {
  1494. case 1:
  1495. msdc_ldo_power(on, POWER_LDO_VMC, VOL_1800, &g_msdc1_io);
  1496. if (on)
  1497. upmu_set_rg_vmc_184(1); /* workarond for denali */
  1498. msdc_set_rdsel(host, 1);
  1499. msdc_set_driving(host, host->hw, 1);
  1500. break;
  1501. case 2:
  1502. msdc_ldo_power(on, POWER_LDO_VMC, VOL_1800, &g_msdc2_io);
  1503. msdc_set_rdsel(host, 1);
  1504. msdc_set_driving(host, host->hw, 1);
  1505. break;
  1506. default:
  1507. break;
  1508. }
  1509. }
  1510. #else
  1511. static void msdc_emmc_power(struct msdc_host *host, u32 on)
  1512. {
  1513. unsigned long tmo = 0;
  1514. void __iomem *base = host->base;
  1515. /* if MMC_CAP_WAIT_WHILE_BUSY not set,
  1516. * mmc core layer will loop for wait sa_timeout
  1517. */
  1518. if (host->mmc && host->mmc->card
  1519. && (host->mmc->caps & MMC_CAP_WAIT_WHILE_BUSY) && (on == 0)) {
  1520. /* max timeout: 1000ms */
  1521. if ((DIV_ROUND_UP(host->mmc->card->ext_csd.sa_timeout, 10000)) < 1000)
  1522. tmo = jiffies + DIV_ROUND_UP(host->mmc->card->ext_csd.sa_timeout,
  1523. 10000000 / HZ) + HZ / 100;
  1524. else
  1525. tmo = jiffies + HZ;
  1526. while ((sdr_read32(MSDC_PS) & 0x10000) != 0x10000) {
  1527. if (time_after(jiffies, tmo)) {
  1528. ERR_MSG("Dat0 keep low before power off, sa_timeout = 0x%x",
  1529. host->mmc->card->ext_csd.sa_timeout);
  1530. emmc_sleep_failed = 1;
  1531. break;
  1532. }
  1533. }
  1534. }
  1535. /* Set to 3.0V - 100mV
  1536. * 4'b0000: 0 mV
  1537. * 4'b0001: -20 mV
  1538. * 4'b0010: -40 mV
  1539. * 4'b0011: -60 mV
  1540. * 4'b0100: -80 mV
  1541. * 4'b0101: -100 mV
  1542. */
  1543. msdc_ldo_power(on, MT6328_POWER_LDO_VEMC33, VOL_3000, &g_msdc0_flash);
  1544. msdc_dump_ldo_sts(host);
  1545. }
  1546. static void msdc_sd_power(struct msdc_host *host, u32 on)
  1547. {
  1548. switch (host->id) {
  1549. case 1:
  1550. msdc_set_driving(host, host->hw, 0);
  1551. msdc_set_rdsel(host, 0);
  1552. if (host->hw->flags & MSDC_SD_NEED_POWER)
  1553. msdc_ldo_power(1, MT6328_POWER_LDO_VMCH, VOL_3300, &g_msdc1_flash);
  1554. else
  1555. msdc_ldo_power(on, MT6328_POWER_LDO_VMCH, VOL_3300, &g_msdc1_flash);
  1556. msdc_ldo_power(on, MT6328_POWER_LDO_VMC, VOL_3300, &g_msdc1_io);
  1557. if (on)
  1558. upmu_set_rg_vmc_184(0);
  1559. break;
  1560. case 2:
  1561. msdc_set_driving(host, host->hw, 0);
  1562. msdc_set_rdsel(host, 0);
  1563. msdc_ldo_power(on, MT6328_POWER_LDO_VMC, VOL_3300, &g_msdc2_io);
  1564. msdc_ldo_power(on, MT6328_POWER_LDO_VMCH, VOL_3300, &g_msdc2_flash);
  1565. break;
  1566. default:
  1567. break;
  1568. }
  1569. msdc_dump_ldo_sts(host);
  1570. }
  1571. static void msdc_sd_power_switch(struct msdc_host *host, u32 on)
  1572. {
  1573. switch (host->id) {
  1574. case 1:
  1575. msdc_ldo_power(on, MT6328_POWER_LDO_VMC, VOL_1800, &g_msdc1_io);
  1576. if (on)
  1577. upmu_set_rg_vmc_184(1);
  1578. msdc_set_rdsel(host, 1);
  1579. msdc_set_driving(host, host->hw, 1);
  1580. break;
  1581. case 2:
  1582. msdc_ldo_power(on, MT6328_POWER_LDO_VMC, VOL_1800, &g_msdc2_io);
  1583. msdc_set_rdsel(host, 1);
  1584. msdc_set_driving(host, host->hw, 1);
  1585. break;
  1586. default:
  1587. break;
  1588. }
  1589. }
  1590. #endif
  1591. static void msdc_sdio_power(struct msdc_host *host, u32 on)
  1592. {
  1593. switch (host->id) {
  1594. #if defined(CFG_DEV_MSDC2)
  1595. case 2:
  1596. if (MSDC_VIO18_MC2 == host->power_domain) {
  1597. if (on) {
  1598. #if 0 /* CHECK ME */ /* Bus & device keeps 1.8v */
  1599. msdc_ldo_power(on, MT_POWER_LDO_VGP6, VOL_1800, &g_msdc2_io);
  1600. #endif
  1601. } else {
  1602. #if 0 /* CHECK ME */ /* Bus & device keeps 3.3v */
  1603. msdc_ldo_power(on, MT_POWER_LDO_VGP6, VOL_3000, &g_msdc2_io);
  1604. #endif
  1605. }
  1606. } else if (MSDC_VIO28_MC2 == host->power_domain) {
  1607. /* Bus & device keeps 2.8v */
  1608. /*msdc_ldo_power(on, MT_POWER_LDO_VIO28, VOL_2800, &g_msdc2_io); */
  1609. }
  1610. g_msdc2_flash = g_msdc2_io;
  1611. break;
  1612. #endif
  1613. #if defined(CFG_DEV_MSDC3)
  1614. case 3:
  1615. break;
  1616. #endif
  1617. default:
  1618. /*if host_id is 3, it uses default 1.8v setting, which always turns on */
  1619. break;
  1620. }
  1621. }
  1622. #endif
  1623. static void msdc_reset_pwr_cycle_counter(struct msdc_host *host)
  1624. {
  1625. host->power_cycle = 0;
  1626. host->power_cycle_enable = 1;
  1627. }
  1628. #define CMD_TUNE_CNT (0)
  1629. #define READ_TUNE_CNT (1)
  1630. #define WRITE_TUNE_CNT (2)
  1631. #define ALL_TUNE_CNT (3)
  1632. static void msdc_reset_tmo_tune_counter(struct msdc_host *host, int index)
  1633. {
  1634. if (index >= 0 && index <= ALL_TUNE_CNT) {
  1635. switch (index) {
  1636. case CMD_TUNE_CNT:
  1637. if (host->rwcmd_time_tune != 0)
  1638. ERR_MSG("TMO TUNE CMD Times(%d)", host->rwcmd_time_tune);
  1639. host->rwcmd_time_tune = 0;
  1640. break;
  1641. case READ_TUNE_CNT:
  1642. if (host->read_time_tune != 0)
  1643. ERR_MSG("TMO TUNE READ Times(%d)", host->read_time_tune);
  1644. host->read_time_tune = 0;
  1645. break;
  1646. case WRITE_TUNE_CNT:
  1647. if (host->write_time_tune != 0)
  1648. ERR_MSG("TMO TUNE WRITE Times(%d)", host->write_time_tune);
  1649. host->write_time_tune = 0;
  1650. break;
  1651. case ALL_TUNE_CNT:
  1652. if (host->rwcmd_time_tune != 0)
  1653. ERR_MSG("TMO TUNE CMD Times(%d)", host->rwcmd_time_tune);
  1654. if (host->read_time_tune != 0)
  1655. ERR_MSG("TMO TUNE READ Times(%d)", host->read_time_tune);
  1656. if (host->write_time_tune != 0)
  1657. ERR_MSG("TMO TUNE WRITE Times(%d)", host->write_time_tune);
  1658. host->rwcmd_time_tune = 0;
  1659. host->read_time_tune = 0;
  1660. host->write_time_tune = 0;
  1661. break;
  1662. default:
  1663. break;
  1664. }
  1665. } else {
  1666. ERR_MSG("msdc%d ==> reset tmo counter index(%d) error!\n",
  1667. host->id, index);
  1668. }
  1669. }
  1670. static void msdc_reset_crc_tune_counter(struct msdc_host *host, int index)
  1671. {
  1672. void __iomem *base = host->base;
  1673. if (index >= 0 && index <= ALL_TUNE_CNT) {
  1674. switch (index) {
  1675. case CMD_TUNE_CNT:
  1676. if (host->t_counter.time_cmd != 0) {
  1677. ERR_MSG("CRC TUNE CMD Times(%d)", host->t_counter.time_cmd);
  1678. if (g_ett_tune)
  1679. g_ett_cmd_tune = host->t_counter.time_cmd;
  1680. }
  1681. host->t_counter.time_cmd = 0;
  1682. break;
  1683. case READ_TUNE_CNT:
  1684. if (host->t_counter.time_read != 0) {
  1685. ERR_MSG("CRC TUNE READ Times(%d)", host->t_counter.time_read);
  1686. if (g_ett_tune)
  1687. g_ett_read_tune = host->t_counter.time_read;
  1688. }
  1689. host->t_counter.time_read = 0;
  1690. break;
  1691. case WRITE_TUNE_CNT:
  1692. if (host->t_counter.time_write != 0) {
  1693. ERR_MSG("CRC TUNE WRITE Times(%d)", host->t_counter.time_write);
  1694. if (g_ett_tune)
  1695. g_ett_write_tune = host->t_counter.time_write;
  1696. }
  1697. host->t_counter.time_write = 0;
  1698. break;
  1699. case ALL_TUNE_CNT:
  1700. if (host->t_counter.time_cmd != 0) {
  1701. ERR_MSG("CRC TUNE CMD Times(%d)", host->t_counter.time_cmd);
  1702. if (g_ett_tune)
  1703. g_ett_cmd_tune = host->t_counter.time_cmd;
  1704. }
  1705. if (host->t_counter.time_read != 0) {
  1706. ERR_MSG("CRC TUNE READ Times(%d)", host->t_counter.time_read);
  1707. if (g_ett_tune)
  1708. g_ett_read_tune = host->t_counter.time_read;
  1709. }
  1710. if (host->t_counter.time_write != 0) {
  1711. ERR_MSG("CRC TUNE WRITE Times(%d)", host->t_counter.time_write);
  1712. if (g_ett_tune)
  1713. g_ett_write_tune = host->t_counter.time_write;
  1714. }
  1715. host->t_counter.time_cmd = 0;
  1716. host->t_counter.time_read = 0;
  1717. host->t_counter.time_write = 0;
  1718. if (host->t_counter.time_hs400 != 0) {
  1719. if (g_reset_tune) {
  1720. sdr_set_field(EMMC50_PAD_DS_TUNE,
  1721. MSDC_EMMC50_PAD_DS_TUNE_DLY1, 0x1c);
  1722. sdr_set_field(EMMC50_PAD_DS_TUNE,
  1723. MSDC_EMMC50_PAD_DS_TUNE_DLY3, 0xe);
  1724. }
  1725. ERR_MSG("TUNE HS400 Times(%d)", host->t_counter.time_hs400);
  1726. if (g_ett_tune)
  1727. g_ett_hs400_tune = host->t_counter.time_hs400;
  1728. }
  1729. host->t_counter.time_hs400 = 0;
  1730. break;
  1731. default:
  1732. break;
  1733. }
  1734. } else {
  1735. ERR_MSG("msdc%d ==> reset crc counter index(%d) error!\n",
  1736. host->id, index);
  1737. }
  1738. }
  1739. static void msdc_set_bad_card_and_remove(struct msdc_host *host)
  1740. {
  1741. unsigned long flags;
  1742. if (host == NULL) {
  1743. pr_err("WARN: host is NULL\n");
  1744. return;
  1745. }
  1746. host->card_inserted = 0;
  1747. if ((host->mmc == NULL) || (host->mmc->card == NULL)) {
  1748. ERR_MSG("WARN: mmc or card is NULL");
  1749. return;
  1750. }
  1751. if (host->mmc->card) {
  1752. spin_lock_irqsave(&host->remove_bad_card, flags);
  1753. host->block_bad_card = 1;
  1754. mmc_card_set_removed(host->mmc->card);
  1755. spin_unlock_irqrestore(&host->remove_bad_card, flags);
  1756. if (!(host->mmc->caps & MMC_CAP_NONREMOVABLE)
  1757. && (host->hw->cd_level == __gpio_get_value(cd_gpio))) {
  1758. /* do nothing*/
  1759. /*tasklet_hi_schedule(&host->card_tasklet);*/
  1760. } else {
  1761. mmc_remove_card(host->mmc->card);
  1762. host->mmc->card = NULL;
  1763. mmc_detach_bus(host->mmc);
  1764. mmc_power_off(host->mmc);
  1765. }
  1766. ERR_MSG("remove the bad card, block_bad_card=%d,card_inserted=%d",
  1767. host->block_bad_card, host->card_inserted);
  1768. }
  1769. }
  1770. /* host doesn't need the clock on */
  1771. void msdc_gate_clock(struct msdc_host *host, int delay)
  1772. {
  1773. unsigned long flags;
  1774. spin_lock_irqsave(&host->clk_gate_lock, flags);
  1775. if (host->clk_gate_count > 0)
  1776. host->clk_gate_count--;
  1777. if (delay) {
  1778. mod_timer(&host->timer, jiffies + CLK_TIMEOUT);
  1779. N_MSG(CLK, "[%s]: msdc%d, clk_gate_count=%d, delay=%d",
  1780. __func__, host->id, host->clk_gate_count, delay);
  1781. } else if (host->clk_gate_count == 0) {
  1782. del_timer(&host->timer);
  1783. msdc_clksrc_onoff(host, 0);
  1784. N_MSG(CLK, "[%s]:msdc%d,success gate clock,clk_gate_count=%d, delay=%d",
  1785. __func__, host->id, host->clk_gate_count, delay);
  1786. } else {
  1787. if (is_card_sdio(host))
  1788. host->error = -EBUSY;
  1789. ERR_MSG
  1790. ("[%s]:msdc%d clk still need,gate fail,clk_gate_count=%d,delay=%d",
  1791. __func__, host->id, host->clk_gate_count, delay);
  1792. }
  1793. spin_unlock_irqrestore(&host->clk_gate_lock, flags);
  1794. }
  1795. static void msdc_suspend_clock(struct msdc_host *host)
  1796. {
  1797. unsigned long flags;
  1798. spin_lock_irqsave(&host->clk_gate_lock, flags);
  1799. if (host->clk_gate_count == 0) {
  1800. del_timer(&host->timer);
  1801. msdc_clksrc_onoff(host, 0);
  1802. N_MSG(CLK, "[%s]: msdc%d, successfully gate clock,clk_gate_count=%d",
  1803. __func__, host->id, host->clk_gate_count);
  1804. } else {
  1805. if (is_card_sdio(host))
  1806. host->error = -EBUSY;
  1807. ERR_MSG("[%s]:msdc%d,clock is still needed by host,clk_gate_count=%d",
  1808. __func__, host->id, host->clk_gate_count);
  1809. }
  1810. spin_unlock_irqrestore(&host->clk_gate_lock, flags);
  1811. }
  1812. /* host does need the clock on */
  1813. void msdc_ungate_clock(struct msdc_host *host)
  1814. {
  1815. unsigned long flags;
  1816. spin_lock_irqsave(&host->clk_gate_lock, flags);
  1817. host->clk_gate_count++;
  1818. N_MSG(CLK, "[%s]: msdc%d, clk_gate_count=%d",
  1819. __func__, host->id, host->clk_gate_count);
  1820. if (host->clk_gate_count == 1)
  1821. msdc_clksrc_onoff(host, 1);
  1822. spin_unlock_irqrestore(&host->clk_gate_lock, flags);
  1823. }
  1824. /* do we need sync object or not */
  1825. void msdc_clk_status(int *status)
  1826. {
  1827. int g_clk_gate = 0;
  1828. int i = 0;
  1829. unsigned long flags;
  1830. for (i = 0; i < HOST_MAX_NUM; i++) {
  1831. if (!mtk_msdc_host[i])
  1832. continue;
  1833. spin_lock_irqsave(&mtk_msdc_host[i]->clk_gate_lock, flags);
  1834. if (mtk_msdc_host[i]->clk_gate_count > 0)
  1835. #ifndef FPGA_PLATFORM
  1836. g_clk_gate |= 1 << ((i) + MT_CG_PERI_MSDC30_0);
  1837. #endif
  1838. spin_unlock_irqrestore(&mtk_msdc_host[i]->clk_gate_lock, flags);
  1839. }
  1840. *status = g_clk_gate;
  1841. }
  1842. #if 0
  1843. static void msdc_dump_card_status(struct msdc_host *host, u32 status)
  1844. {
  1845. static const char * const state[] = {
  1846. "Idle", /* 0 */
  1847. "Ready", /* 1 */
  1848. "Ident", /* 2 */
  1849. "Stby", /* 3 */
  1850. "Tran", /* 4 */
  1851. "Data", /* 5 */
  1852. "Rcv", /* 6 */
  1853. "Prg", /* 7 */
  1854. "Dis", /* 8 */
  1855. "Reserved", /* 9 */
  1856. "Reserved", /* 10 */
  1857. "Reserved", /* 11 */
  1858. "Reserved", /* 12 */
  1859. "Reserved", /* 13 */
  1860. "Reserved", /* 14 */
  1861. "I/O mode", /* 15 */
  1862. };
  1863. if (status & R1_OUT_OF_RANGE)
  1864. N_MSG(RSP, "[CARD_STATUS] Out of Range");
  1865. if (status & R1_ADDRESS_ERROR)
  1866. N_MSG(RSP, "[CARD_STATUS] Address Error");
  1867. if (status & R1_BLOCK_LEN_ERROR)
  1868. N_MSG(RSP, "[CARD_STATUS] Block Len Error");
  1869. if (status & R1_ERASE_SEQ_ERROR)
  1870. N_MSG(RSP, "[CARD_STATUS] Erase Seq Error");
  1871. if (status & R1_ERASE_PARAM)
  1872. N_MSG(RSP, "[CARD_STATUS] Erase Param");
  1873. if (status & R1_WP_VIOLATION)
  1874. N_MSG(RSP, "[CARD_STATUS] WP Violation");
  1875. if (status & R1_CARD_IS_LOCKED)
  1876. N_MSG(RSP, "[CARD_STATUS] Card is Locked");
  1877. if (status & R1_LOCK_UNLOCK_FAILED)
  1878. N_MSG(RSP, "[CARD_STATUS] Lock/Unlock Failed");
  1879. if (status & R1_COM_CRC_ERROR)
  1880. N_MSG(RSP, "[CARD_STATUS] Command CRC Error");
  1881. if (status & R1_ILLEGAL_COMMAND)
  1882. N_MSG(RSP, "[CARD_STATUS] Illegal Command");
  1883. if (status & R1_CARD_ECC_FAILED)
  1884. N_MSG(RSP, "[CARD_STATUS] Card ECC Failed");
  1885. if (status & R1_CC_ERROR)
  1886. N_MSG(RSP, "[CARD_STATUS] CC Error");
  1887. if (status & R1_ERROR)
  1888. N_MSG(RSP, "[CARD_STATUS] Error");
  1889. if (status & R1_UNDERRUN)
  1890. N_MSG(RSP, "[CARD_STATUS] Underrun");
  1891. if (status & R1_OVERRUN)
  1892. N_MSG(RSP, "[CARD_STATUS] Overrun");
  1893. if (status & R1_CID_CSD_OVERWRITE)
  1894. N_MSG(RSP, "[CARD_STATUS] CID/CSD Overwrite");
  1895. if (status & R1_WP_ERASE_SKIP)
  1896. N_MSG(RSP, "[CARD_STATUS] WP Eraser Skip");
  1897. if (status & R1_CARD_ECC_DISABLED)
  1898. N_MSG(RSP, "[CARD_STATUS] Card ECC Disabled");
  1899. if (status & R1_ERASE_RESET)
  1900. N_MSG(RSP, "[CARD_STATUS] Erase Reset");
  1901. if ((status & R1_READY_FOR_DATA) == 0)
  1902. N_MSG(RSP, "[CARD_STATUS] Not Ready for Data");
  1903. if (status & R1_SWITCH_ERROR)
  1904. N_MSG(RSP, "[CARD_STATUS] Switch error");
  1905. if (status & R1_APP_CMD)
  1906. N_MSG(RSP, "[CARD_STATUS] App Command");
  1907. N_MSG(RSP, "[CARD_STATUS] '%s' State", state[R1_CURRENT_STATE(status)]);
  1908. }
  1909. #endif
  1910. static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
  1911. {
  1912. void __iomem *base = host->base;
  1913. u32 timeout, clk_ns;
  1914. u32 mode = 0;
  1915. host->timeout_ns = ns;
  1916. host->timeout_clks = clks;
  1917. if (host->sclk == 0) {
  1918. timeout = 0;
  1919. } else {
  1920. clk_ns = 1000000000UL / host->sclk;
  1921. timeout = (ns + clk_ns - 1) / clk_ns + clks;
  1922. /* in 1048576 sclk cycle unit */
  1923. timeout = (timeout + (1 << 20) - 1) >> 20;
  1924. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, mode);
  1925. /* DDR mode will double the clk cycles for data timeout */
  1926. timeout = mode >= 2 ? timeout * 2 : timeout;
  1927. timeout = timeout > 1 ? timeout - 1 : 0;
  1928. timeout = timeout > 255 ? 255 : timeout;
  1929. }
  1930. sdr_set_field(SDC_CFG, SDC_CFG_DTOC, timeout);
  1931. }
  1932. /* msdc_eirq_sdio() will be called when EIRQ(for WIFI) */
  1933. static void msdc_eirq_sdio(void *data)
  1934. {
  1935. struct msdc_host *host = (struct msdc_host *)data;
  1936. N_MSG(INT, "SDIO EINT");
  1937. #ifdef SDIO_ERROR_BYPASS
  1938. if (host->sdio_error != -EIO) {
  1939. #endif
  1940. mmc_signal_sdio_irq(host->mmc);
  1941. #ifdef SDIO_ERROR_BYPASS
  1942. }
  1943. #endif
  1944. }
  1945. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  1946. static void sdio_unreq_vcore(struct work_struct *work)
  1947. {
  1948. struct msdc_host *host = mtk_msdc_host[2]; /* 6630 in msdc2@Denali */
  1949. pr_warn("** sdio_unreq_vcore() irqs_disabled():%d\n", irqs_disabled());
  1950. might_sleep();
  1951. /* mmc_claim_host(host->mmc); */
  1952. if (vcorefs_request_dvfs_opp(KIR_SDIO, OPPI_UNREQ) == 0) {
  1953. pr_debug("unrequest vcore pass\n");
  1954. host->sdio_performance_vcore = 0;
  1955. } else {
  1956. pr_err("unrequest vcore fail\n");
  1957. }
  1958. /* mmc_release_host(host->mmc); */
  1959. }
  1960. static noinline void sdio_set_vcore_performance(struct msdc_host *host,
  1961. u32 enable)
  1962. {
  1963. if (atomic_read(&host->ot_work.ot_disable)) {
  1964. /* TODO: also return here when clock rate is not 200MHz */
  1965. pr_info("sdio_set_vcore_performance auto-K haven't done\n");
  1966. return;
  1967. }
  1968. if (enable) {
  1969. might_sleep();
  1970. /* true if dwork was pending, false otherwise */
  1971. if (cancel_delayed_work_sync(&(host->set_vcore_workq)) == 0) {
  1972. pr_warn("** cancel @ FALSE\n");
  1973. if (vcorefs_request_dvfs_opp(KIR_SDIO, OPPI_PERF) == 0) {
  1974. pr_debug("msdc%d -> request vcore pass\n", host->id);
  1975. host->sdio_performance_vcore = 1;
  1976. } else {
  1977. pr_err("msdc%d -> request vcore fail\n", host->id);
  1978. }
  1979. }
  1980. } else {
  1981. schedule_delayed_work(&(host->set_vcore_workq), CLK_TIMEOUT);
  1982. }
  1983. /* mmc_release_host(host->mmc); */
  1984. }
  1985. #endif /* MTK_SDIO30_ONLINE_TUNING_SUPPORT */
  1986. static void msdc_select_clksrc(struct msdc_host *host, int clksrc)
  1987. {
  1988. #ifndef FPGA_PLATFORM
  1989. char name[6];
  1990. #ifndef CONFIG_MTK_CLKMGR
  1991. int ret;
  1992. struct clk *clk;
  1993. #endif
  1994. if (host->id == 0)
  1995. hclks = hclks_msdc50;
  1996. else
  1997. hclks = hclks_msdc30;
  1998. #endif
  1999. pr_err("[%s]: msdc%d change clk_src from %dKHz to %d:%dKHz\n",
  2000. __func__, host->id, (host->hclk / 1000), clksrc,
  2001. (hclks[clksrc] / 1000));
  2002. #ifndef FPGA_PLATFORM
  2003. sprintf(name, "MSDC%d", host->id);
  2004. #ifdef CONFIG_MTK_CLKMGR
  2005. clkmux_sel(MT_MUX_MSDC30_0 - host->id, clksrc, name);
  2006. #else
  2007. if (host->id != 0) {
  2008. pr_err("NOT Support msdc%d switch pll souce[%s]%d\n",
  2009. host->id, __func__, __LINE__);
  2010. return;
  2011. }
  2012. if (clksrc == MSDC50_CLKSRC_800MHZ)
  2013. clk = g_msdc0_pll_800m;
  2014. else if (clksrc == MSDC50_CLKSRC_400MHZ)
  2015. clk = g_msdc0_pll_400m;
  2016. else if (clksrc == MSDC50_CLKSRC_200MHZ)
  2017. clk = g_msdc0_pll_200m;
  2018. else {
  2019. pr_err("NOT Support msdc%d switch pll souce[%s]%d\n",
  2020. host->id, __func__, __LINE__);
  2021. return;
  2022. }
  2023. clk_enable(g_msdc0_pll_sel);
  2024. ret = clk_set_parent(g_msdc0_pll_sel, clk);
  2025. if (ret)
  2026. pr_err("XXX MSDC%d switch clk source ERROR...[%s]%d\n",
  2027. host->id, __func__, __LINE__);
  2028. clk_disable(g_msdc0_pll_sel);
  2029. #endif
  2030. #endif
  2031. host->hclk = hclks[clksrc];
  2032. host->hw->clk_src = clksrc;
  2033. }
  2034. void msdc_sdio_set_long_timing_delay_by_freq(struct msdc_host *host, u32 clock)
  2035. {
  2036. #ifdef CONFIG_SDIOAUTOK_SUPPORT
  2037. void __iomem *base = host->base;
  2038. if (clock >= 200000000) {
  2039. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  2040. host->hw->wdatcrctactr_sdr200);
  2041. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  2042. host->hw->cmdrtactr_sdr200);
  2043. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  2044. host->hw->intdatlatcksel_sdr200);
  2045. host->saved_para.cmd_resp_ta_cntr = host->hw->cmdrtactr_sdr200;
  2046. host->saved_para.wrdat_crc_ta_cntr = host->hw->wdatcrctactr_sdr200;
  2047. host->saved_para.int_dat_latch_ck_sel = host->hw->intdatlatcksel_sdr200;
  2048. } else {
  2049. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  2050. host->hw->wdatcrctactr_sdr50);
  2051. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  2052. host->hw->cmdrtactr_sdr50);
  2053. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  2054. host->hw->intdatlatcksel_sdr50);
  2055. host->saved_para.cmd_resp_ta_cntr = host->hw->cmdrtactr_sdr50;
  2056. host->saved_para.wrdat_crc_ta_cntr = host->hw->wdatcrctactr_sdr50;
  2057. host->saved_para.int_dat_latch_ck_sel = host->hw->intdatlatcksel_sdr50;
  2058. }
  2059. #endif
  2060. }
  2061. int sdio_autok_processed = 0;
  2062. static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz)
  2063. {
  2064. struct msdc_hw *hw = host->hw;
  2065. void __iomem *base = host->base;
  2066. u32 mode;
  2067. u32 flags;
  2068. u32 div;
  2069. u32 sclk;
  2070. u32 hclk = host->hclk;
  2071. u32 hs400_src = 0;
  2072. u8 clksrc = hw->clk_src;
  2073. if (!hz) {
  2074. pr_err("msdc%d -> !!! Set<0 Hz>", host->id);
  2075. if (is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ)) {
  2076. host->saved_para.hz = hz;
  2077. #ifdef SDIO_ERROR_BYPASS
  2078. host->sdio_error = 0;
  2079. #endif
  2080. }
  2081. host->mclk = 0;
  2082. msdc_reset_hw(host->id);
  2083. return;
  2084. }
  2085. if (host->hw->host_function == MSDC_SDIO
  2086. && hz >= 100 * 1000 * 1000 && sdio_autok_processed == 0) {
  2087. hz = 50 * 1000 * 1000;
  2088. msdc_sdio_set_long_timing_delay_by_freq(host, hz);
  2089. }
  2090. msdc_irq_save(flags);
  2091. if (timing == MMC_TIMING_MMC_HS400) {
  2092. mode = 0x3; /* HS400 mode */
  2093. if (clksrc == MSDC50_CLKSRC_400MHZ) {
  2094. hs400_src = 1;
  2095. div = 0;
  2096. sclk = hclk / 2;
  2097. } else {
  2098. hs400_src = 0;
  2099. if (hz >= (hclk >> 2)) {
  2100. div = 0; /* mean div = 1/4 */
  2101. sclk = hclk >> 2; /* sclk = clk / 4 */
  2102. } else {
  2103. div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  2104. sclk = (hclk >> 2) / div;
  2105. div = (div >> 1);
  2106. }
  2107. }
  2108. } else if ((timing == MMC_TIMING_UHS_DDR50)
  2109. || (timing == MMC_TIMING_MMC_DDR52)) {
  2110. mode = 0x2; /* ddr mode and use divisor */
  2111. if (hz >= (hclk >> 2)) {
  2112. div = 0; /* mean div = 1/4 */
  2113. sclk = hclk >> 2; /* sclk = clk / 4 */
  2114. } else {
  2115. div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  2116. sclk = (hclk >> 2) / div;
  2117. div = (div >> 1);
  2118. }
  2119. } else if (hz >= hclk) {
  2120. #ifdef FPGA_PLATFORM
  2121. mode = 0x0; /* FPGA doesn't support no divisor */
  2122. #else
  2123. mode = 0x1; /* no divisor */
  2124. #endif
  2125. div = 0;
  2126. sclk = hclk;
  2127. } else {
  2128. mode = 0x0; /* use divisor */
  2129. if (hz >= (hclk >> 1)) {
  2130. div = 0; /* mean div = 1/2 */
  2131. sclk = hclk >> 1; /* sclk = clk / 2 */
  2132. } else {
  2133. div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  2134. sclk = (hclk >> 2) / div;
  2135. }
  2136. }
  2137. msdc_clk_stable(host, mode, div, hs400_src);
  2138. host->sclk = sclk;
  2139. #if 0
  2140. if (host->sclk > 100000000)
  2141. sdr_clr_bits(MSDC_PATCH_BIT0, CKGEN_RX_SDClKO_SEL);
  2142. else
  2143. sdr_set_bits(MSDC_PATCH_BIT0, CKGEN_RX_SDClKO_SEL);
  2144. #endif
  2145. /* need because clk changed */
  2146. msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
  2147. if (mode == 0x3) {
  2148. msdc_set_smpl(host, 1, host->hw->cmd_edge, TYPE_CMD_RESP_EDGE, NULL);
  2149. msdc_set_smpl(host, 1, host->hw->rdata_edge, TYPE_READ_DATA_EDGE, NULL);
  2150. msdc_set_smpl(host, 1, host->hw->wdata_edge, TYPE_WRITE_CRC_EDGE, NULL);
  2151. } else {
  2152. msdc_set_smpl(host, 0, host->hw->cmd_edge, TYPE_CMD_RESP_EDGE, NULL);
  2153. msdc_set_smpl(host, 0, host->hw->rdata_edge, TYPE_READ_DATA_EDGE, NULL);
  2154. msdc_set_smpl(host, 0, host->hw->wdata_edge, TYPE_WRITE_CRC_EDGE, NULL);
  2155. }
  2156. pr_err("msdc%d Set<%dK> src:<%dK> sclk:<%dK> timing<%d> mode:%d div:%d\n",
  2157. host->id, hz / 1000, hclk / 1000, sclk / 1000, timing, mode, div);
  2158. msdc_irq_restore(flags);
  2159. }
  2160. /* 0 means pass */
  2161. static u32 msdc_power_tuning(struct msdc_host *host)
  2162. {
  2163. struct mmc_host *mmc = host->mmc;
  2164. struct mmc_card *card;
  2165. struct mmc_request *mrq;
  2166. u32 power_cycle = 0;
  2167. int read_timeout_tune = 0;
  2168. int write_timeout_tune = 0;
  2169. u32 rwcmd_timeout_tune = 0;
  2170. u32 read_timeout_tune_uhs104 = 0;
  2171. u32 write_timeout_tune_uhs104 = 0;
  2172. u32 sw_timeout = 0;
  2173. u32 ret = 1;
  2174. u32 host_err = 0;
  2175. void __iomem *base = host->base;
  2176. if (!mmc)
  2177. return 1;
  2178. card = mmc->card;
  2179. if (card == NULL) {
  2180. ERR_MSG("mmc->card is NULL");
  2181. return 1;
  2182. }
  2183. /* eMMC first */
  2184. #ifdef CONFIG_MTK_EMMC_SUPPORT
  2185. if (mmc_card_mmc(card) && (host->hw->host_function == MSDC_EMMC))
  2186. return 1;
  2187. #endif
  2188. if ((host->sd_30_busy > 0) && (host->sd_30_busy <= MSDC_MAX_POWER_CYCLE))
  2189. host->power_cycle_enable = 1;
  2190. if (mmc_card_sd(card) && (host->hw->host_function == MSDC_SD)) {
  2191. if ((host->power_cycle < MSDC_MAX_POWER_CYCLE)
  2192. && (host->power_cycle_enable)) {
  2193. /* power cycle */
  2194. ERR_MSG("the %d time, Power cycle start", host->power_cycle);
  2195. spin_unlock(&host->lock);
  2196. #ifdef FPGA_PLATFORM
  2197. hwPowerDown_fpga();
  2198. #else
  2199. if (host->power_control)
  2200. host->power_control(host, 0);
  2201. else
  2202. pr_err("[ERROR]msdc%d No power control callback!\n", host->id);
  2203. #endif
  2204. mdelay(10);
  2205. #ifdef FPGA_PLATFORM
  2206. hwPowerOn_fpga();
  2207. #else
  2208. if (host->power_control)
  2209. host->power_control(host, 1);
  2210. else
  2211. pr_err("[ERROR]msdc%d No power control callback!\n", host->id);
  2212. #endif
  2213. spin_lock(&host->lock);
  2214. sdr_get_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, host->hw->ddlsel);
  2215. sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, host->hw->cmd_edge);
  2216. sdr_get_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, host->hw->rdata_edge);
  2217. sdr_get_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, host->hw->wdata_edge);
  2218. host->saved_para.pad_tune0 = sdr_read32(MSDC_PAD_TUNE0);
  2219. host->saved_para.ddly0 = sdr_read32(MSDC_DAT_RDDLY0);
  2220. host->saved_para.ddly1 = sdr_read32(MSDC_DAT_RDDLY1);
  2221. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  2222. host->saved_para.cmd_resp_ta_cntr);
  2223. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  2224. host->saved_para.wrdat_crc_ta_cntr);
  2225. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_BUSY_MA,
  2226. host->saved_para.write_busy_margin);
  2227. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_CRC_MA,
  2228. host->saved_para.write_crc_margin);
  2229. if ((host->sclk > 100000000) && (host->power_cycle >= 1))
  2230. mmc->caps &= ~MMC_CAP_UHS_SDR104;
  2231. if (((host->sclk <= 100000000) && ((host->sclk > 50000000)
  2232. || (host->timing == MMC_TIMING_UHS_DDR50)))
  2233. && (host->power_cycle >= 1)) {
  2234. mmc->caps &= ~(MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104
  2235. | MMC_CAP_UHS_DDR50);
  2236. }
  2237. msdc_host_mode[host->id] = mmc->caps;
  2238. msdc_host_mode2[host->id] = mmc->caps2;
  2239. /* clock should set to 260K */
  2240. mmc->ios.clock = HOST_MIN_MCLK;
  2241. mmc->ios.bus_width = MMC_BUS_WIDTH_1;
  2242. mmc->ios.timing = MMC_TIMING_LEGACY;
  2243. msdc_set_mclk(host, MMC_TIMING_LEGACY, HOST_MIN_MCLK);
  2244. /* zone_temp = sd_debug_zone[1]; */
  2245. /* sd_debug_zone[1] |= (DBG_EVT_NRW | DBG_EVT_RW); */
  2246. /* re-init the card */
  2247. mrq = host->mrq;
  2248. host->mrq = NULL;
  2249. power_cycle = host->power_cycle;
  2250. host->power_cycle = MSDC_MAX_POWER_CYCLE;
  2251. read_timeout_tune = host->read_time_tune;
  2252. write_timeout_tune = host->write_time_tune;
  2253. rwcmd_timeout_tune = host->rwcmd_time_tune;
  2254. read_timeout_tune_uhs104 = host->read_timeout_uhs104;
  2255. write_timeout_tune_uhs104 = host->write_timeout_uhs104;
  2256. sw_timeout = host->sw_timeout;
  2257. host_err = host->error;
  2258. spin_unlock(&host->lock);
  2259. ret = mmc_sd_power_cycle(mmc, card->ocr, card);
  2260. spin_lock(&host->lock);
  2261. host->mrq = mrq;
  2262. host->power_cycle = power_cycle;
  2263. host->read_time_tune = read_timeout_tune;
  2264. host->write_time_tune = write_timeout_tune;
  2265. host->rwcmd_time_tune = rwcmd_timeout_tune;
  2266. if (host->sclk > 100000000) {
  2267. host->write_timeout_uhs104 = write_timeout_tune_uhs104;
  2268. } else {
  2269. host->read_timeout_uhs104 = 0;
  2270. host->write_timeout_uhs104 = 0;
  2271. }
  2272. host->sw_timeout = sw_timeout;
  2273. host->error = host_err;
  2274. if (!ret)
  2275. host->power_cycle_enable = 0;
  2276. ERR_MSG("the %d time, Power cycle Done, host->error(0x%x), ret(%d)",
  2277. host->power_cycle, host->error, ret);
  2278. (host->power_cycle)++;
  2279. } else if (host->continuous_fail_request_count <
  2280. MSDC_MAX_CONTINUOUS_FAIL_REQUEST_COUNT)
  2281. host->continuous_fail_request_count++;
  2282. else {
  2283. ERR_MSG("[%d] > max continue fail request count %d,remove bad card",
  2284. host->continuous_fail_request_count,
  2285. MSDC_MAX_CONTINUOUS_FAIL_REQUEST_COUNT);
  2286. host->continuous_fail_request_count = 0;
  2287. /*release the lock in request */
  2288. spin_unlock(&host->lock);
  2289. /*card removing will define a new lock inside */
  2290. msdc_set_bad_card_and_remove(host);
  2291. /*restore the lock in request entry */
  2292. spin_lock(&host->lock);
  2293. }
  2294. }
  2295. return ret;
  2296. }
  2297. static void msdc_send_stop(struct msdc_host *host)
  2298. {
  2299. struct mmc_command stop = { 0 };
  2300. struct mmc_request mrq = { 0 };
  2301. u32 err = -1;
  2302. stop.opcode = MMC_STOP_TRANSMISSION;
  2303. stop.arg = 0;
  2304. stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
  2305. mrq.cmd = &stop;
  2306. stop.mrq = &mrq;
  2307. stop.data = NULL;
  2308. err = msdc_do_command(host, &stop, 0, CMD_TIMEOUT);
  2309. }
  2310. int msdc_reinit(struct msdc_host *host)
  2311. {
  2312. struct mmc_host *mmc;
  2313. struct mmc_card *card;
  2314. /* struct mmc_request *mrq; */
  2315. int ret = -1;
  2316. u32 err = 0;
  2317. u32 status = 0;
  2318. unsigned long tmo = 12;
  2319. if (!host) {
  2320. pr_err("msdc_host is NULL\n");
  2321. return -1;
  2322. }
  2323. if (host->hw->host_function != MSDC_SD)
  2324. return -1;
  2325. mmc = host->mmc;
  2326. if (!mmc) {
  2327. ERR_MSG("mmc is NULL");
  2328. return -1;
  2329. }
  2330. card = mmc->card;
  2331. if (card == NULL)
  2332. ERR_MSG("mmc->card is NULL");
  2333. if (host->block_bad_card)
  2334. ERR_MSG("Need block this bad SD card from re-initialization");
  2335. if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) && (host->block_bad_card == 0)) {
  2336. /* power cycle */
  2337. ERR_MSG("SD card Re-Init!");
  2338. mmc_claim_host(host->mmc);
  2339. ERR_MSG("SD card Re-Init get host!");
  2340. spin_lock(&host->lock);
  2341. ERR_MSG("SD card Re-Init get lock!");
  2342. msdc_clksrc_onoff(host, 1);
  2343. if (host->app_cmd_arg) {
  2344. while ((err = msdc_get_card_status(mmc, host, &status))) {
  2345. ERR_MSG("SD card Re-Init in get card status!err(%d)", err);
  2346. if (err == (unsigned int)-EIO) {
  2347. if (msdc_tune_cmdrsp(host)) {
  2348. ERR_MSG("update cmd para failed");
  2349. break;
  2350. }
  2351. } else {
  2352. break;
  2353. }
  2354. }
  2355. if (err == 0) {
  2356. if (status == 0) {
  2357. msdc_dump_info(host->id);
  2358. } else {
  2359. msdc_clksrc_onoff(host, 0);
  2360. spin_unlock(&host->lock);
  2361. mmc_release_host(host->mmc);
  2362. ERR_MSG("SD Card is ready.");
  2363. return 0;
  2364. }
  2365. }
  2366. }
  2367. msdc_clksrc_onoff(host, 0);
  2368. ERR_MSG("Reinit start..");
  2369. mmc->ios.clock = HOST_MIN_MCLK;
  2370. mmc->ios.bus_width = MMC_BUS_WIDTH_1;
  2371. mmc->ios.timing = MMC_TIMING_LEGACY;
  2372. host->card_inserted = 1;
  2373. msdc_clksrc_onoff(host, 1);
  2374. msdc_set_mclk(host, MMC_TIMING_LEGACY, HOST_MIN_MCLK);
  2375. msdc_clksrc_onoff(host, 0);
  2376. spin_unlock(&host->lock);
  2377. mmc_release_host(host->mmc);
  2378. if (host->mmc->card) {
  2379. mmc_remove_card(host->mmc->card);
  2380. host->mmc->card = NULL;
  2381. mmc_claim_host(host->mmc);
  2382. mmc_detach_bus(host->mmc);
  2383. mmc_release_host(host->mmc);
  2384. }
  2385. mmc_power_off(host->mmc);
  2386. mmc_detect_change(host->mmc, 0);
  2387. while (tmo) {
  2388. if (host->mmc->card && mmc_card_present(host->mmc->card)) {
  2389. ret = 0;
  2390. break;
  2391. }
  2392. msleep(50);
  2393. tmo--;
  2394. }
  2395. ERR_MSG("Reinit %s", ret == 0 ? "success" : "fail");
  2396. }
  2397. if ((!(host->mmc->caps & MMC_CAP_NONREMOVABLE)) && (host->mmc->card)
  2398. && mmc_card_present(host->mmc->card)
  2399. && (!mmc_card_removed(host->mmc->card))
  2400. && (host->block_bad_card == 0))
  2401. ret = 0;
  2402. return ret;
  2403. }
  2404. static u32 msdc_status_verify_case1(struct msdc_host *host,
  2405. struct mmc_command *cmd)
  2406. {
  2407. struct mmc_host *mmc = host->mmc;
  2408. u32 status = 0;
  2409. u32 state = 0;
  2410. u32 err = 0;
  2411. unsigned long tmo = jiffies + POLLING_BUSY;
  2412. while (state != 4) { /* until status to "tran" */
  2413. msdc_reset_hw(host->id);
  2414. while ((err = msdc_get_card_status(mmc, host, &status))) {
  2415. ERR_MSG("CMD13 ERR<%d>", err);
  2416. if (err != (unsigned int)-EIO) {
  2417. return msdc_power_tuning(host);
  2418. } else if (msdc_tune_cmdrsp(host)) {
  2419. ERR_MSG("update cmd para failed");
  2420. return MSDC_VERIFY_ERROR;
  2421. }
  2422. }
  2423. state = R1_CURRENT_STATE(status);
  2424. if (state == 5 || state == 6) {
  2425. ERR_MSG("state<%d> need cmd12 to stop", state);
  2426. msdc_send_stop(host); /* don't tuning */
  2427. } else if (state == 7) { /* busy in programing */
  2428. ERR_MSG("state<%d> card is busy", state);
  2429. spin_unlock(&host->lock);
  2430. msleep(100);
  2431. spin_lock(&host->lock);
  2432. } else if (state != 4) {
  2433. ERR_MSG("state<%d> ??? ", state);
  2434. return msdc_power_tuning(host);
  2435. }
  2436. if (time_after(jiffies, tmo)) {
  2437. ERR_MSG("abort timeout. Do power cycle");
  2438. if ((host->hw->host_function == MSDC_SD)
  2439. && (host->sclk >= 100000000
  2440. || (host->timing == MMC_TIMING_UHS_DDR50)))
  2441. host->sd_30_busy++;
  2442. return msdc_power_tuning(host);
  2443. }
  2444. }
  2445. msdc_reset_hw(host->id);
  2446. return MSDC_VERIFY_NEED_TUNE;
  2447. }
  2448. static u32 msdc_status_verify_case2(struct msdc_host *host,
  2449. struct mmc_command *cmd)
  2450. {
  2451. struct mmc_host *mmc = host->mmc;
  2452. u32 status = 0;
  2453. u32 state = 0;
  2454. u32 err = 0; /*0: can tune normaly; 1: err hapen; 2: tune pass; */
  2455. struct mmc_card *card = host->mmc->card;
  2456. while (1) {
  2457. msdc_reset_hw(host->id);
  2458. err = msdc_get_card_status(mmc, host, &status);
  2459. if (!err) {
  2460. break;
  2461. } else if (err != (unsigned int)-EIO) {
  2462. ERR_MSG("CMD13 ERR<%d>", err);
  2463. return msdc_power_tuning(host);
  2464. } else if (msdc_tune_cmdrsp(host)) {
  2465. ERR_MSG("update cmd para failed");
  2466. return MSDC_VERIFY_ERROR;
  2467. }
  2468. ERR_MSG("CMD13 ERR<%d>", err);
  2469. }
  2470. state = R1_CURRENT_STATE(status);
  2471. /*wether is right RCA */
  2472. if (cmd->arg == card->rca << 16) {
  2473. return (3 == state || 8 == state) ?
  2474. MSDC_VERIFY_NEED_TUNE : MSDC_VERIFY_NEED_NOT_TUNE;
  2475. } else {
  2476. return (4 == state || 5 == state || 6 == state
  2477. || 7 == state) ? MSDC_VERIFY_NEED_TUNE : MSDC_VERIFY_NEED_NOT_TUNE;
  2478. }
  2479. }
  2480. static u32 msdc_status_verify_case3(struct msdc_host *host,
  2481. struct mmc_command *cmd)
  2482. {
  2483. struct mmc_host *mmc = host->mmc;
  2484. u32 status = 0;
  2485. u32 state = 0;
  2486. u32 err = 0; /*0: can tune normaly; 1: tune pass; */
  2487. while (1) {
  2488. msdc_reset_hw(host->id);
  2489. err = msdc_get_card_status(mmc, host, &status);
  2490. if (!err) {
  2491. break;
  2492. } else if (err != (unsigned int)-EIO) {
  2493. ERR_MSG("CMD13 ERR<%d>", err);
  2494. return msdc_power_tuning(host);
  2495. } else if (msdc_tune_cmdrsp(host)) {
  2496. ERR_MSG("update cmd para failed");
  2497. return MSDC_VERIFY_ERROR;
  2498. }
  2499. ERR_MSG("CMD13 ERR<%d>", err);
  2500. }
  2501. state = R1_CURRENT_STATE(status);
  2502. return (5 == state || 6 == state) ?
  2503. MSDC_VERIFY_NEED_TUNE : MSDC_VERIFY_NEED_NOT_TUNE;
  2504. }
  2505. static u32 msdc_status_verify_case4(struct msdc_host *host,
  2506. struct mmc_command *cmd)
  2507. {
  2508. struct mmc_host *mmc = host->mmc;
  2509. u32 status = 0;
  2510. u32 state = 0;
  2511. u32 err = 0; /*0: can tune normaly; 1: tune pass; */
  2512. if (cmd->arg & (0x1UL << 15))
  2513. return MSDC_VERIFY_NEED_NOT_TUNE;
  2514. while (1) {
  2515. msdc_reset_hw(host->id);
  2516. err = msdc_get_card_status(mmc, host, &status);
  2517. if (!err) {
  2518. break;
  2519. } else if (err != (unsigned int)-EIO) {
  2520. ERR_MSG("CMD13 ERR<%d>", err);
  2521. break;
  2522. } else if (msdc_tune_cmdrsp(host)) {
  2523. ERR_MSG("update cmd para failed");
  2524. return MSDC_VERIFY_ERROR;
  2525. }
  2526. ERR_MSG("CMD13 ERR<%d>", err);
  2527. }
  2528. state = R1_CURRENT_STATE(status);
  2529. return 3 == state ? MSDC_VERIFY_NEED_NOT_TUNE : MSDC_VERIFY_NEED_TUNE;
  2530. }
  2531. #if 0
  2532. static u32 msdc_status_verify_case5(struct msdc_host *host,
  2533. struct mmc_command *cmd)
  2534. {
  2535. struct mmc_host *mmc = host->mmc;
  2536. u32 status = 0;
  2537. u32 state = 0;
  2538. u32 err = 0; /*0: can tune normaly; 1: tune pass; */
  2539. struct mmc_card *card = host->mmc->card;
  2540. struct mmc_command cmd_bus_test = { 0 };
  2541. struct mmc_request mrq_bus_sest = { 0 };
  2542. while ((err = msdc_get_card_status(mmc, host, &status))) {
  2543. ERR_MSG("CMD13 ERR<%d>", err);
  2544. if (err != (unsigned int)-EIO) {
  2545. return msdc_power_tuning(host);
  2546. } else if (msdc_tune_cmdrsp(host)) {
  2547. ERR_MSG("update cmd para failed");
  2548. return MSDC_VERIFY_ERROR;
  2549. }
  2550. }
  2551. state = R1_CURRENT_STATE(status);
  2552. if (MMC_SEND_TUNING_BLOCK == cmd->opcode) {
  2553. if (state == 9) {
  2554. /* send cmd14 */
  2555. /*u32 err = -1; */
  2556. cmd_bus_test.opcode = MMC_BUS_TEST_R;
  2557. cmd_bus_test.arg = 0;
  2558. cmd_bus_test.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  2559. mrq_bus_sest.cmd = &cmd_bus_test;
  2560. cmd_bus_test.mrq = &mrq_bus_sest;
  2561. cmd_bus_test.data = NULL;
  2562. msdc_do_command(host, &cmd_bus_test, 0, CMD_TIMEOUT);
  2563. }
  2564. } else {
  2565. if (state == 4) {
  2566. /* send cmd19 */
  2567. /*u32 err = -1; */
  2568. cmd_bus_test.opcode = MMC_BUS_TEST_W;
  2569. cmd_bus_test.arg = 0;
  2570. cmd_bus_test.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  2571. mrq_bus_sest.cmd = &cmd_bus_test;
  2572. cmd_bus_test.mrq = &mrq_bus_sest;
  2573. cmd_bus_test.data = NULL;
  2574. msdc_do_command(host, &cmd_bus_test, 0, CMD_TIMEOUT);
  2575. }
  2576. }
  2577. return MSDC_VERIFY_NEED_TUNE;
  2578. }
  2579. #endif
  2580. static u32 msdc_status_verify(struct msdc_host *host, struct mmc_command *cmd)
  2581. {
  2582. /* card is not identify */
  2583. if (!host->mmc || !host->mmc->card || !host->mmc->card->rca)
  2584. return MSDC_VERIFY_NEED_TUNE;
  2585. if (((host->hw->host_function == MSDC_EMMC)
  2586. && IS_IN_CMD_SET(cmd->opcode, CMD_SET_FOR_MMC_TUNE_CASE1))
  2587. || ((host->hw->host_function == MSDC_SD)
  2588. && IS_IN_CMD_SET(cmd->opcode, CMD_SET_FOR_SD_TUNE_CASE1))
  2589. || (host->app_cmd
  2590. && IS_IN_CMD_SET(cmd->opcode, CMD_SET_FOR_APP_TUNE_CASE1))) {
  2591. return msdc_status_verify_case1(host, cmd);
  2592. } else if (IS_IN_CMD_SET(cmd->opcode, CMD_SET_FOR_MMC_TUNE_CASE2)) {
  2593. return msdc_status_verify_case2(host, cmd);
  2594. } else if (IS_IN_CMD_SET(cmd->opcode, CMD_SET_FOR_MMC_TUNE_CASE3)) {
  2595. return msdc_status_verify_case3(host, cmd);
  2596. } else if ((host->hw->host_function == MSDC_EMMC)
  2597. && IS_IN_CMD_SET(cmd->opcode, CMD_SET_FOR_MMC_TUNE_CASE4)) {
  2598. return msdc_status_verify_case4(host, cmd);
  2599. #if 0
  2600. } else if ((host->hw->host_function == MSDC_EMMC)
  2601. && IS_IN_CMD_SET(cmd->opcode, CMD_SET_FOR_MMC_TUNE_CASE5)) {
  2602. return msdc_status_verify_case5(host, cmd);
  2603. #endif
  2604. } else {
  2605. return MSDC_VERIFY_NEED_TUNE;
  2606. }
  2607. }
  2608. #ifndef FPGA_PLATFORM
  2609. static void msdc_pin_config(struct msdc_host *host, int mode)
  2610. {
  2611. msdc_pin_pud(host, mode);
  2612. }
  2613. #else
  2614. static void msdc_pin_config(struct msdc_host *host, int mode)
  2615. {
  2616. }
  2617. #endif
  2618. static void msdc_pin_reset(struct msdc_host *host, int mode)
  2619. {
  2620. struct msdc_hw *hw = (struct msdc_hw *)host->hw;
  2621. void __iomem *base = host->base;
  2622. int pull = (mode == MSDC_PIN_PULL_UP) ?
  2623. MSDC_GPIO_PULL_UP : MSDC_GPIO_PULL_DOWN;
  2624. /* Config reset pin */
  2625. if (hw->flags & MSDC_RST_PIN_EN) {
  2626. if (hw->config_gpio_pin) /* NULL */
  2627. hw->config_gpio_pin(MSDC_RST_PIN, pull);
  2628. if (mode == MSDC_PIN_PULL_UP)
  2629. sdr_clr_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2630. else
  2631. sdr_set_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2632. }
  2633. }
  2634. #if 0 /* need fix */
  2635. static void msdc_pin_reset_force(struct msdc_host *host, int mode)
  2636. {
  2637. struct msdc_hw *hw = (struct msdc_hw *)host->hw;
  2638. void __iomem *base = host->base;
  2639. int pull = (mode == MSDC_PIN_PULL_UP) ?
  2640. MSDC_GPIO_PULL_UP : MSDC_GPIO_PULL_DOWN;
  2641. if (hw->config_gpio_pin) /* NULL */
  2642. hw->config_gpio_pin(MSDC_RST_PIN, pull);
  2643. if (mode == MSDC_PIN_PULL_UP)
  2644. sdr_clr_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2645. else
  2646. sdr_set_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2647. }
  2648. #endif
  2649. static void msdc_set_power_mode(struct msdc_host *host, u8 mode)
  2650. {
  2651. N_MSG(CFG, "Set power mode(%d)", mode);
  2652. if (host->power_mode == MMC_POWER_OFF && mode != MMC_POWER_OFF) {
  2653. msdc_pin_reset(host, MSDC_PIN_PULL_UP);
  2654. msdc_pin_config(host, MSDC_PIN_PULL_UP);
  2655. #ifdef FPGA_PLATFORM
  2656. hwPowerOn_fpga();
  2657. #else
  2658. if (host->power_control)
  2659. host->power_control(host, 1);
  2660. else
  2661. ERR_MSG
  2662. ("No power control, host_function<%d> & Power_domain<%d>",
  2663. host->hw->host_function, host->power_domain);
  2664. #endif
  2665. mdelay(10);
  2666. } else if (host->power_mode != MMC_POWER_OFF && mode == MMC_POWER_OFF) {
  2667. if (is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ)) {
  2668. msdc_pin_config(host, MSDC_PIN_PULL_UP);
  2669. } else {
  2670. #ifdef FPGA_PLATFORM
  2671. hwPowerDown_fpga();
  2672. #else
  2673. if (host->power_control)
  2674. host->power_control(host, 0);
  2675. else
  2676. ERR_MSG("No power control,host_function<%d>&Power_domain<%d>"
  2677. , host->hw->host_function, host->power_domain);
  2678. #endif
  2679. msdc_pin_config(host, MSDC_PIN_PULL_DOWN);
  2680. }
  2681. mdelay(10);
  2682. msdc_pin_reset(host, MSDC_PIN_PULL_DOWN);
  2683. }
  2684. host->power_mode = mode;
  2685. }
  2686. #ifdef MTK_EMMC_ETT_TO_DRIVER
  2687. static int msdc_ett_offline_to_driver(struct msdc_host *host)
  2688. {
  2689. int ret = 1; /* 1 means failed */
  2690. int size = sizeof(g_mmcTable) / sizeof(mmcdev_info);
  2691. int i, temp;
  2692. void __iomem *base = host->base;
  2693. u32 clkmode;
  2694. int hs400 = 0;
  2695. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, clkmode);
  2696. hs400 = (clkmode == 3) ? 1 : 0;
  2697. /* pr_err("msdc_ett_offline_to_driver size<%d>\n", size); */
  2698. for (i = 0; i < size; i++) {
  2699. /*
  2700. * pr_err("msdc <%d> <%s> <%s>\n", i, g_mmcTable[i].pro_name, pro_name);
  2701. */
  2702. if ((g_mmcTable[i].m_id == m_id)
  2703. && (!strncmp(g_mmcTable[i].pro_name, pro_name, 6))) {
  2704. pr_err("msdc ett index<%d>: <%d> <%d> <0x%x> <0x%x> <0x%x>\n", i,
  2705. g_mmcTable[i].r_smpl, g_mmcTable[i].d_smpl,
  2706. g_mmcTable[i].cmd_rxdly, g_mmcTable[i].rd_rxdly,
  2707. g_mmcTable[i].wr_rxdly);
  2708. /* set to msdc0 */
  2709. msdc_set_smpl(host, hs400, g_mmcTable[i].r_smpl,
  2710. TYPE_CMD_RESP_EDGE, NULL);
  2711. msdc_set_smpl(host, hs400, g_mmcTable[i].d_smpl,
  2712. TYPE_READ_DATA_EDGE, NULL);
  2713. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY,
  2714. g_mmcTable[i].cmd_rxdly);
  2715. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATRRDLY,
  2716. g_mmcTable[i].rd_rxdly);
  2717. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY,
  2718. g_mmcTable[i].wr_rxdly);
  2719. temp = g_mmcTable[i].rd_rxdly;
  2720. temp &= 0x1F;
  2721. sdr_write32(MSDC_DAT_RDDLY0,
  2722. (temp << 0 | temp << 8 | temp << 16 | temp << 24));
  2723. sdr_write32(MSDC_DAT_RDDLY1,
  2724. (temp << 0 | temp << 8 | temp << 16 | temp << 24));
  2725. ret = 0;
  2726. break;
  2727. }
  2728. }
  2729. /* if (ret) pr_err("msdc failed to find\n"); */
  2730. return ret;
  2731. }
  2732. #endif
  2733. static void msdc_clksrc_onoff(struct msdc_host *host, u32 on)
  2734. {
  2735. void __iomem *base = host->base;
  2736. u32 div, mode, hs400_src;
  2737. if (on) {
  2738. if (0 == host->core_clkon) {
  2739. #ifndef FPGA_PLATFORM
  2740. #ifdef CONFIG_MTK_CLKMGR
  2741. if (enable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD")) {
  2742. pr_err("msdc%d on clock failed ===> retry once\n", host->id);
  2743. disable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
  2744. enable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
  2745. }
  2746. #else
  2747. if (clk_enable(host->clock_control)) {
  2748. pr_err("msdc%d on clock failed ===> retry once\n", host->id);
  2749. clk_disable(host->clock_control);
  2750. clk_enable(host->clock_control);
  2751. }
  2752. #endif
  2753. #endif
  2754. host->core_clkon = 1;
  2755. udelay(10);
  2756. sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC);
  2757. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, mode);
  2758. sdr_get_field(MSDC_CFG, MSDC_CFG_CKDIV, div);
  2759. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD_HS400, hs400_src);
  2760. msdc_clk_stable(host, mode, div, hs400_src);
  2761. #if 0
  2762. if (is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))
  2763. /* mdelay(1000); wait for WIFI stable. */
  2764. #ifndef FPGA_PLATFORM
  2765. /* freq_meter(0xf, 0); */
  2766. #endif
  2767. #endif
  2768. }
  2769. } else {
  2770. if (!((host->hw->flags & MSDC_SDIO_IRQ) && src_clk_control)) {
  2771. if (1 == host->core_clkon) {
  2772. sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_MS);
  2773. #ifndef FPGA_PLATFORM
  2774. #ifdef CONFIG_MTK_CLKMGR
  2775. disable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
  2776. #else
  2777. clk_disable(host->clock_control);
  2778. #endif
  2779. #endif
  2780. host->core_clkon = 0;
  2781. #ifndef FPGA_PLATFORM
  2782. /* freq_meter(0xf, 0); */
  2783. #endif
  2784. }
  2785. }
  2786. }
  2787. }
  2788. /*
  2789. * register as callback function of WIFI(combo_sdio_register_pm) .
  2790. * can called by msdc_drv_suspend/resume too.
  2791. */
  2792. #ifdef CONFIG_PM
  2793. static void msdc_save_emmc_setting(struct msdc_host *host)
  2794. {
  2795. void __iomem *base = host->base;
  2796. u32 clkmode;
  2797. host->saved_para.hz = host->mclk;
  2798. host->saved_para.sdc_cfg = sdr_read32(SDC_CFG);
  2799. sdr_get_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, host->hw->ddlsel);
  2800. sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, host->hw->cmd_edge);
  2801. sdr_get_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, host->hw->rdata_edge);
  2802. sdr_get_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, host->hw->wdata_edge);
  2803. host->saved_para.pad_tune0 = sdr_read32(MSDC_PAD_TUNE0);
  2804. host->saved_para.ddly0 = sdr_read32(MSDC_DAT_RDDLY0);
  2805. host->saved_para.ddly1 = sdr_read32(MSDC_DAT_RDDLY1);
  2806. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  2807. host->saved_para.cmd_resp_ta_cntr);
  2808. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  2809. host->saved_para.wrdat_crc_ta_cntr);
  2810. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1,
  2811. host->saved_para.ds_dly1);
  2812. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3,
  2813. host->saved_para.ds_dly3);
  2814. host->saved_para.emmc50_pad_cmd_tune = sdr_read32(EMMC50_PAD_CMD_TUNE);
  2815. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, clkmode);
  2816. host->saved_para.timing = host->timing;
  2817. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_BUSY_MA,
  2818. host->saved_para.write_busy_margin);
  2819. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_CRC_MA,
  2820. host->saved_para.write_crc_margin);
  2821. sdr_get_field(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTS,
  2822. host->saved_para.cfg_crcsts_path);
  2823. sdr_get_field(MSDC_PATCH_BIT2, MSDC_PB2_CFGRESP,
  2824. host->saved_para.cfg_cmdrsp_path);
  2825. sdr_get_field(MSDC_PATCH_BIT2, MSDC_PB2_RESPWAITCNT,
  2826. host->saved_para.resp_wait_cnt);
  2827. }
  2828. static void msdc_restore_emmc_setting(struct msdc_host *host)
  2829. {
  2830. void __iomem *base = host->base;
  2831. msdc_set_mclk(host, host->saved_para.timing, host->mclk);
  2832. sdr_write32(SDC_CFG, host->saved_para.sdc_cfg);
  2833. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, host->hw->ddlsel);
  2834. msdc_set_smpl(host, host->saved_para.timing == MMC_TIMING_MMC_HS400,
  2835. host->hw->cmd_edge, TYPE_CMD_RESP_EDGE, NULL);
  2836. msdc_set_smpl(host, host->saved_para.timing == MMC_TIMING_MMC_HS400,
  2837. host->hw->rdata_edge, TYPE_READ_DATA_EDGE, NULL);
  2838. msdc_set_smpl(host, host->saved_para.timing == MMC_TIMING_MMC_HS400,
  2839. host->hw->wdata_edge, TYPE_WRITE_CRC_EDGE, NULL);
  2840. sdr_write32(MSDC_PAD_TUNE0, host->saved_para.pad_tune0);
  2841. sdr_write32(MSDC_DAT_RDDLY0, host->saved_para.ddly0);
  2842. sdr_write32(MSDC_DAT_RDDLY1, host->saved_para.ddly1);
  2843. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  2844. host->saved_para.wrdat_crc_ta_cntr);
  2845. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  2846. host->saved_para.cmd_resp_ta_cntr);
  2847. sdr_set_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1,
  2848. host->saved_para.ds_dly1);
  2849. sdr_set_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3,
  2850. host->saved_para.ds_dly3);
  2851. sdr_write32(EMMC50_PAD_CMD_TUNE, host->saved_para.emmc50_pad_cmd_tune);
  2852. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_BUSY_MA,
  2853. host->saved_para.write_busy_margin);
  2854. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_CRC_MA,
  2855. host->saved_para.write_crc_margin);
  2856. sdr_set_field(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTS,
  2857. host->saved_para.cfg_crcsts_path);
  2858. sdr_set_field(MSDC_PATCH_BIT2, MSDC_PB2_CFGRESP,
  2859. host->saved_para.cfg_cmdrsp_path);
  2860. sdr_set_field(MSDC_PATCH_BIT2, MSDC_PB2_RESPWAITCNT,
  2861. host->saved_para.resp_wait_cnt);
  2862. }
  2863. static void msdc_pm(pm_message_t state, void *data)
  2864. {
  2865. struct msdc_host *host = (struct msdc_host *)data;
  2866. int evt = state.event;
  2867. msdc_ungate_clock(host);
  2868. if (host->hw->host_function == MSDC_EMMC)
  2869. emmc_do_sleep_awake = 1;
  2870. if (evt == PM_EVENT_SUSPEND || evt == PM_EVENT_USER_SUSPEND) {
  2871. if (host->suspend)
  2872. goto end;
  2873. if (evt == PM_EVENT_SUSPEND && host->power_mode == MMC_POWER_OFF)
  2874. goto end;
  2875. host->suspend = 1;
  2876. host->pm_state = state;
  2877. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  2878. if (host->id == 2) {
  2879. atomic_set(&host->ot_work.autok_done, 0);
  2880. /* true if dwork was pending, false otherwise */
  2881. if (cancel_delayed_work_sync(&(host->set_vcore_workq)) == 0)
  2882. pr_warn("** suspend- no pending vcore_workq\n");
  2883. else
  2884. pr_warn("** suspend- cancel vcore_workq\n");
  2885. }
  2886. #endif
  2887. pr_err("msdc%d -> %s Suspend",
  2888. host->id, evt == PM_EVENT_SUSPEND ? "PM" : "USR");
  2889. if (host->hw->flags & MSDC_SYS_SUSPEND) {
  2890. if (host->hw->host_function == MSDC_EMMC) {
  2891. msdc_save_emmc_setting(host);
  2892. host->power_control(host, 0);
  2893. msdc_pin_config(host, MSDC_PIN_PULL_DOWN);
  2894. msdc_pin_reset(host, MSDC_PIN_PULL_DOWN);
  2895. }
  2896. #ifndef FPGA_PLATFORM
  2897. msdc_set_tdsel(host, 1);
  2898. #endif
  2899. } else {
  2900. host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
  2901. mmc_remove_host(host->mmc);
  2902. }
  2903. } else if (evt == PM_EVENT_RESUME || evt == PM_EVENT_USER_RESUME) {
  2904. if (!host->suspend)
  2905. goto end;
  2906. if (evt == PM_EVENT_RESUME
  2907. && host->pm_state.event == PM_EVENT_USER_SUSPEND) {
  2908. ERR_MSG("PM Resume when in USR Suspend");
  2909. goto end;
  2910. }
  2911. host->suspend = 0;
  2912. host->pm_state = state;
  2913. pr_err("msdc%d -> %s Resume",
  2914. host->id, evt == PM_EVENT_RESUME ? "PM" : "USR");
  2915. if (host->hw->flags & MSDC_SYS_SUSPEND) {
  2916. #ifndef FPGA_PLATFORM
  2917. msdc_set_tdsel(host, 0);
  2918. #endif
  2919. if (host->hw->host_function == MSDC_EMMC) {
  2920. msdc_reset_hw(host->id);
  2921. msdc_pin_reset(host, MSDC_PIN_PULL_UP);
  2922. msdc_pin_config(host, MSDC_PIN_PULL_UP);
  2923. host->power_control(host, 1);
  2924. mdelay(10);
  2925. msdc_restore_emmc_setting(host);
  2926. }
  2927. } else {
  2928. host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
  2929. mmc_add_host(host->mmc);
  2930. }
  2931. }
  2932. end:
  2933. #ifdef SDIO_ERROR_BYPASS
  2934. if (is_card_sdio(host))
  2935. host->sdio_error = 0;
  2936. #endif
  2937. if ((evt == PM_EVENT_SUSPEND) || (evt == PM_EVENT_USER_SUSPEND)) {
  2938. if ((host->hw->host_function == MSDC_SDIO)
  2939. && (evt == PM_EVENT_USER_SUSPEND))
  2940. pr_debug("msdc%d -> MSDC Device Request Suspend", host->id);
  2941. msdc_gate_clock(host, 0);
  2942. } else {
  2943. msdc_gate_clock(host, 1);
  2944. }
  2945. if (host->hw->host_function == MSDC_SDIO) {
  2946. host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
  2947. host->mmc->rescan_entered = 0;
  2948. }
  2949. if (host->hw->host_function == MSDC_EMMC)
  2950. emmc_do_sleep_awake = 0;
  2951. }
  2952. #endif
  2953. struct msdc_host *msdc_get_host(int host_function, bool boot, bool secondary)
  2954. {
  2955. int host_index = 0;
  2956. struct msdc_host *host = NULL;
  2957. for (; host_index < HOST_MAX_NUM; ++host_index) {
  2958. if (!mtk_msdc_host[host_index])
  2959. continue;
  2960. if ((host_function == mtk_msdc_host[host_index]->hw->host_function)
  2961. && (boot == mtk_msdc_host[host_index]->hw->boot)) {
  2962. host = mtk_msdc_host[host_index];
  2963. break;
  2964. }
  2965. }
  2966. if (secondary && (host_function == MSDC_SD))
  2967. host = mtk_msdc_host[2];
  2968. if (host == NULL) {
  2969. pr_err("[%s]MSDC-ERROR host_function:%d,boot:%d,secondary:%d\n"
  2970. , __func__, host_function, boot, secondary);
  2971. /* BUG(); */
  2972. }
  2973. return host;
  2974. }
  2975. EXPORT_SYMBOL(msdc_get_host);
  2976. #ifdef CONFIG_MTK_EMMC_SUPPORT
  2977. static int msdc_get_ext_csd(struct mmc_data *data, struct msdc_host *host)
  2978. {
  2979. u8 *ptr;
  2980. int i;
  2981. #ifdef MTK_MSDC_USE_CACHE
  2982. enum boot_mode_t mode;
  2983. #endif
  2984. struct scatterlist *sg;
  2985. sg = data->sg;
  2986. ptr = (u8 *) sg_virt(sg);
  2987. g_emmc_cache_size = (*(ptr + 252) << 24) +
  2988. (*(ptr + 251) << 16) +
  2989. (*(ptr + 250) << 8) +
  2990. (*(ptr + 249) << 0);
  2991. #ifdef MTK_MSDC_USE_CACHE
  2992. /*
  2993. * only enable the emmc cache feature for normal boot up,
  2994. * alarm boot up, and sw reboot
  2995. */
  2996. mode = get_boot_mode();
  2997. if ((mode == NORMAL_BOOT) || (mode == ALARM_BOOT) || (mode == SW_REBOOT)) {
  2998. for (i = 0; i < sizeof(g_emmc_cache_quirk); i++) {
  2999. if (g_emmc_cache_quirk[i] == emmc_id)
  3000. *(ptr + 252) = *(ptr + 251) = *(ptr + 250) = *(ptr + 249) = 0;
  3001. }
  3002. }
  3003. #else
  3004. *(ptr + 252) = *(ptr + 251) = *(ptr + 250) = *(ptr + 249) = 0;
  3005. #endif
  3006. return 0;
  3007. }
  3008. #ifdef MTK_MSDC_USE_CACHE
  3009. static void msdc_set_cache_quirk(struct msdc_host *host)
  3010. {
  3011. /*
  3012. * if need disable emmc cache feature for some vendor, plese add quirk here
  3013. * exmple:
  3014. * g_emmc_cache_quirk[0] = CID_MANFID_HYNIX;
  3015. * g_emmc_cache_quirk[1] = CID_MANFID_SAMSUNG;
  3016. */
  3017. int i;
  3018. for (i = 0; i < sizeof(g_emmc_cache_quirk); i++) {
  3019. if (g_emmc_cache_quirk[i] == 0) {
  3020. pr_debug("msdc%d total emmc cache quirk count=%d\n", host->id, i);
  3021. break;
  3022. }
  3023. pr_debug("msdc%d,add emmc cache quirk[%d]=0x%x\n",
  3024. host->id, i, g_emmc_cache_quirk[i]);
  3025. }
  3026. }
  3027. static int msdc_can_apply_cache(unsigned long long start_addr,
  3028. unsigned int size)
  3029. {
  3030. if (!g_cache_part_start && !g_cache_part_end &&
  3031. !g_usrdata_part_start && !g_usrdata_part_end)
  3032. return 0;
  3033. /* since cache, userdata partition are connected,
  3034. * so check it as an area, else do check them separately
  3035. */
  3036. if (g_cache_part_end == g_usrdata_part_start) {
  3037. if (!((start_addr >= g_cache_part_start)
  3038. && (start_addr + size < g_usrdata_part_end))) {
  3039. return 0;
  3040. }
  3041. } else {
  3042. if (!(((start_addr >= g_cache_part_start)
  3043. && (start_addr + size < g_cache_part_end))
  3044. || ((start_addr >= g_usrdata_part_start)
  3045. && (start_addr + size < g_usrdata_part_end)))) {
  3046. return 0;
  3047. }
  3048. }
  3049. return 1;
  3050. }
  3051. #endif
  3052. int msdc_cache_ctrl(struct msdc_host *host, unsigned int enable,
  3053. u32 *status)
  3054. {
  3055. struct mmc_command cmd;
  3056. struct mmc_request mrq;
  3057. u32 err;
  3058. memset(&cmd, 0, sizeof(struct mmc_command));
  3059. cmd.opcode = MMC_SWITCH; /* CMD6 */
  3060. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) | (EXT_CSD_CACHE_CTRL << 16)
  3061. | ((!!enable) << 8) | EXT_CSD_CMD_SET_NORMAL;
  3062. cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  3063. memset(&mrq, 0, sizeof(struct mmc_request));
  3064. mrq.cmd = &cmd;
  3065. cmd.mrq = &mrq;
  3066. cmd.data = NULL;
  3067. ERR_MSG("do disable Cache, cmd=0x%x, arg=0x%x\n", cmd.opcode, cmd.arg);
  3068. err = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);
  3069. /* tune until CMD13 pass. */
  3070. if (status)
  3071. *status = cmd.resp[0];
  3072. if (!err) {
  3073. host->mmc->card->ext_csd.cache_ctrl = !!enable;
  3074. host->autocmd |= MSDC_AUTOCMD23;
  3075. N_MSG(CHE, "enable AUTO_CMD23 because Cache feature is disabled\n");
  3076. }
  3077. return err;
  3078. }
  3079. void get_emmc_cache_info(struct work_struct *work)
  3080. {
  3081. #ifdef MTK_MSDC_USE_CACHE
  3082. struct msdc_host *host;
  3083. struct hd_struct *lp_hd_struct;
  3084. host = msdc_get_host(MSDC_EMMC, MSDC_BOOT_EN, 0);
  3085. lp_hd_struct = get_part("cache");
  3086. if (likely(lp_hd_struct)) {
  3087. g_cache_part_start = lp_hd_struct->start_sect;
  3088. g_cache_part_end = g_cache_part_start + lp_hd_struct->nr_sects;
  3089. put_part(lp_hd_struct);
  3090. } else {
  3091. g_cache_part_start = (sector_t) (-1);
  3092. g_cache_part_end = (sector_t) (-1);
  3093. pr_err("There is no cache info\n");
  3094. }
  3095. lp_hd_struct = NULL;
  3096. lp_hd_struct = get_part("userdata");
  3097. if (likely(lp_hd_struct)) {
  3098. g_usrdata_part_start = lp_hd_struct->start_sect;
  3099. g_usrdata_part_end = g_usrdata_part_start + lp_hd_struct->nr_sects;
  3100. put_part(lp_hd_struct);
  3101. } else {
  3102. g_usrdata_part_start = (sector_t) (-1);
  3103. g_usrdata_part_end = (sector_t) (-1);
  3104. pr_err("There is no userdata info\n");
  3105. }
  3106. pr_debug("msdc0:cache(0x%lld~0x%lld), usrdata(0x%lld~0x%lld)\n",
  3107. g_cache_part_start, g_cache_part_end,
  3108. g_usrdata_part_start, g_usrdata_part_end);
  3109. #endif
  3110. }
  3111. #endif
  3112. static struct delayed_work get_cache_info;
  3113. static int __init init_get_cache_work(void)
  3114. {
  3115. INIT_DELAYED_WORK(&get_cache_info, get_emmc_cache_info);
  3116. schedule_delayed_work(&get_cache_info, 100);
  3117. return 0;
  3118. }
  3119. /*--------------------------------------------------------------------------*/
  3120. /* mmc_host_ops members */
  3121. /*--------------------------------------------------------------------------*/
  3122. static u32 wints_cmd = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO |
  3123. MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO;
  3124. static unsigned int msdc_command_start(struct msdc_host *host,
  3125. struct mmc_command *cmd, int tune,
  3126. unsigned long timeout)
  3127. {
  3128. void __iomem *base = host->base;
  3129. u32 opcode = cmd->opcode;
  3130. u32 rawcmd;
  3131. u32 rawarg;
  3132. u32 resp;
  3133. unsigned long tmo;
  3134. struct mmc_command *sbc = NULL;
  3135. if (host->data && host->data->mrq && host->data->mrq->sbc)
  3136. sbc = host->data->mrq->sbc;
  3137. /* Protocol layer does not provide response type, but our hardware needs
  3138. * to know exact type, not just size!
  3139. */
  3140. if (opcode == MMC_SEND_OP_COND || opcode == SD_APP_OP_COND)
  3141. resp = RESP_R3;
  3142. else if (opcode == MMC_SET_RELATIVE_ADDR || opcode == SD_SEND_RELATIVE_ADDR)
  3143. resp = (mmc_cmd_type(cmd) == MMC_CMD_BCR) ? RESP_R6 : RESP_R1;
  3144. else if (opcode == MMC_FAST_IO)
  3145. resp = RESP_R4;
  3146. else if (opcode == MMC_GO_IRQ_STATE)
  3147. resp = RESP_R5;
  3148. else if (opcode == MMC_SELECT_CARD) {
  3149. resp = (cmd->arg != 0) ? RESP_R1 : RESP_NONE;
  3150. host->app_cmd_arg = cmd->arg;
  3151. //pr_warn("msdc%d select card<0x%.8x>", host->id, cmd->arg);
  3152. } else if (opcode == SD_IO_RW_DIRECT || opcode == SD_IO_RW_EXTENDED)
  3153. resp = RESP_R1; /* SDIO workaround. */
  3154. else if (opcode == SD_SEND_IF_COND && (mmc_cmd_type(cmd) == MMC_CMD_BCR))
  3155. resp = RESP_R1;
  3156. else if (opcode == MMC_SEND_STATUS) /* workaround for ignore crc */
  3157. resp = RESP_R1;
  3158. else {
  3159. switch (mmc_resp_type(cmd)) {
  3160. case MMC_RSP_R1:
  3161. resp = RESP_R1;
  3162. break;
  3163. case MMC_RSP_R1B:
  3164. resp = RESP_R1B;
  3165. break;
  3166. case MMC_RSP_R2:
  3167. resp = RESP_R2;
  3168. break;
  3169. case MMC_RSP_R3:
  3170. resp = RESP_R3;
  3171. break;
  3172. case MMC_RSP_NONE:
  3173. default:
  3174. resp = RESP_NONE;
  3175. break;
  3176. }
  3177. }
  3178. cmd->error = 0;
  3179. /* rawcmd :
  3180. * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
  3181. * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
  3182. */
  3183. rawcmd = opcode | msdc_rsp[resp] << 7 | host->blksz << 16;
  3184. if (opcode == MMC_READ_MULTIPLE_BLOCK) {
  3185. rawcmd |= (2 << 11);
  3186. if (host->autocmd & MSDC_AUTOCMD12)
  3187. rawcmd |= (1 << 28);
  3188. #ifdef MTK_MSDC_USE_CMD23
  3189. else if ((host->autocmd & MSDC_AUTOCMD23)) {
  3190. rawcmd |= (1 << 29);
  3191. if (sbc) {
  3192. /* if the block number is bigger than 0xFFFF,
  3193. * then CMD23 arg will be failed to set it
  3194. */
  3195. if (sdr_read32(SDC_BLK_NUM) != (sbc->arg & 0xFFFF))
  3196. pr_err("msdc%d: acmd23 arg(0x%x) != read blocks(0x%x),SDC_BLK_NUM(0x%x)\n",
  3197. host->id, sbc->arg, host->mrq->cmd->data->blocks,
  3198. sdr_read32(SDC_BLK_NUM));
  3199. else
  3200. sdr_write32(SDC_BLK_NUM, sbc->arg);
  3201. CMD_MSG("CMD<23> arg<0x%.8x> @ addr<0x%.8x>",
  3202. sbc->arg, cmd->arg);
  3203. }
  3204. }
  3205. #endif /* end of MTK_MSDC_USE_CMD23 */
  3206. } else if (opcode == MMC_READ_SINGLE_BLOCK) {
  3207. rawcmd |= (1 << 11);
  3208. } else if (opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  3209. rawcmd |= ((2 << 11) | (1 << 13));
  3210. if (host->autocmd & MSDC_AUTOCMD12)
  3211. rawcmd |= (1 << 28);
  3212. #ifdef MTK_MSDC_USE_CMD23
  3213. else if ((host->autocmd & MSDC_AUTOCMD23)) {
  3214. rawcmd |= (1 << 29);
  3215. if (sbc) {
  3216. if (sdr_read32(SDC_BLK_NUM) != (sbc->arg & 0xFFFF))
  3217. pr_err
  3218. ("msdc%d: acmd23 arg(0x%x) != write blocks(0x%x),SDC_BLK_NUM(0x%x)\n",
  3219. host->id, sbc->arg, host->mrq->cmd->data->blocks,
  3220. sdr_read32(SDC_BLK_NUM));
  3221. else
  3222. sdr_write32(SDC_BLK_NUM, sbc->arg);
  3223. CMD_MSG("CMD<23> arg<0x%.8x> @ addr<0x%.8x>",
  3224. sbc->arg, cmd->arg);
  3225. }
  3226. }
  3227. #endif /* end of MTK_MSDC_USE_CMD23 */
  3228. } else if (opcode == MMC_WRITE_BLOCK) {
  3229. rawcmd |= ((1 << 11) | (1 << 13));
  3230. } else if (opcode == SD_IO_RW_EXTENDED) {
  3231. if (cmd->data->flags & MMC_DATA_WRITE)
  3232. rawcmd |= (1 << 13);
  3233. if (cmd->data->blocks > 1)
  3234. rawcmd |= (2 << 11);
  3235. else
  3236. rawcmd |= (1 << 11);
  3237. } else if (opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int)-1) {
  3238. rawcmd |= (1 << 14);
  3239. } else if (opcode == SD_SWITCH_VOLTAGE) {
  3240. rawcmd |= (1 << 30);
  3241. } else if ((opcode == SD_APP_SEND_SCR)
  3242. || (opcode == SD_APP_SEND_NUM_WR_BLKS)
  3243. || (opcode == SD_SWITCH && (mmc_cmd_type(cmd) == MMC_CMD_ADTC))
  3244. || (opcode == SD_APP_SD_STATUS && (mmc_cmd_type(cmd) == MMC_CMD_ADTC))
  3245. || (opcode == MMC_SEND_EXT_CSD && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)))
  3246. rawcmd |= (1 << 11);
  3247. else if (opcode == MMC_STOP_TRANSMISSION) {
  3248. rawcmd |= (1 << 14);
  3249. rawcmd &= ~(0x0FFF << 16);
  3250. }
  3251. CMD_MSG("CMD<%d> arg<0x%.8x>", cmd->opcode, cmd->arg);
  3252. tmo = jiffies + timeout;
  3253. if (opcode == MMC_SEND_STATUS) {
  3254. for (;;) {
  3255. if (!sdc_is_cmd_busy())
  3256. break;
  3257. if (time_after(jiffies, tmo)) {
  3258. ERR_MSG("XXX cmd_busy timeout: before CMD<%d>", opcode);
  3259. cmd->error = (unsigned int)-ETIMEDOUT;
  3260. msdc_dump_register(host);
  3261. msdc_reset_hw(host->id);
  3262. return cmd->error;
  3263. }
  3264. }
  3265. } else {
  3266. for (;;) {
  3267. if (!sdc_is_busy())
  3268. break;
  3269. if (time_after(jiffies, tmo)) {
  3270. ERR_MSG("XXX sdc_busy timeout: before CMD<%d>", opcode);
  3271. cmd->error = (unsigned int)-ETIMEDOUT;
  3272. msdc_dump_register(host);
  3273. msdc_reset_hw(host->id);
  3274. return cmd->error;
  3275. }
  3276. }
  3277. }
  3278. /* BUG_ON(in_interrupt()); */
  3279. host->cmd = cmd;
  3280. host->cmd_rsp = resp;
  3281. /* use polling way */
  3282. sdr_clr_bits(MSDC_INTEN, wints_cmd);
  3283. rawarg = cmd->arg;
  3284. sdc_send_cmd(rawcmd, rawarg);
  3285. /*end:*/
  3286. /* irq too fast, then cmd->error has value,
  3287. * and don't call msdc_command_resp, don't tune.
  3288. */
  3289. return 0;
  3290. }
  3291. static unsigned int msdc_command_resp_polling(struct msdc_host *host,
  3292. struct mmc_command *cmd, int tune,
  3293. unsigned long timeout)
  3294. {
  3295. void __iomem *base = host->base;
  3296. u32 intsts;
  3297. u32 resp;
  3298. /* u32 status; */
  3299. unsigned long tmo;
  3300. /* struct mmc_data *data = host->data; */
  3301. u32 cmdsts = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO;
  3302. #ifdef MTK_MSDC_USE_CMD23
  3303. struct mmc_command *sbc = NULL;
  3304. #endif
  3305. #ifdef MTK_MSDC_USE_CMD23
  3306. if (host->autocmd & MSDC_AUTOCMD23) {
  3307. if (host->data && host->data->mrq && host->data->mrq->sbc)
  3308. sbc = host->data->mrq->sbc;
  3309. /* autocmd interrupt disabled, used polling way */
  3310. cmdsts |= MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO;
  3311. }
  3312. #endif
  3313. resp = host->cmd_rsp;
  3314. /*polling */
  3315. tmo = jiffies + timeout;
  3316. while (1) {
  3317. intsts = sdr_read32(MSDC_INT);
  3318. if (intsts & cmdsts) {
  3319. /* clear all int flag */
  3320. #ifdef MTK_MSDC_USE_CMD23
  3321. /* need clear autocmd23 command ready interrupt */
  3322. intsts &= (cmdsts | MSDC_INT_ACMDRDY);
  3323. #else
  3324. intsts &= cmdsts;
  3325. #endif
  3326. sdr_write32(MSDC_INT, intsts);
  3327. break;
  3328. }
  3329. if (time_after(jiffies, tmo)) {
  3330. pr_err("[%s]: msdc%d XXX CMD<%d> polling resp timeout ARG<0x%.8x>\n"
  3331. , __func__, host->id, cmd->opcode, cmd->arg);
  3332. cmd->error = (unsigned int)-ETIMEDOUT;
  3333. host->sw_timeout++;
  3334. msdc_dump_info(host->id);
  3335. msdc_reset_hw(host->id);
  3336. goto out;
  3337. }
  3338. }
  3339. #ifdef MTK_MSDC_ERROR_TUNE_DEBUG
  3340. if (g_err_tune_dbg_error && (g_err_tune_dbg_count > 0)
  3341. && (g_err_tune_dbg_host == host->id)) {
  3342. if (g_err_tune_dbg_cmd == cmd->opcode) {
  3343. if ((g_err_tune_dbg_cmd != MMC_SWITCH) ||
  3344. ((g_err_tune_dbg_cmd == MMC_SWITCH)
  3345. && (g_err_tune_dbg_arg == ((cmd->arg >> 16) & 0xff)))) {
  3346. if (g_err_tune_dbg_error & MTK_MSDC_ERROR_CMD_TMO) {
  3347. intsts = MSDC_INT_CMDTMO;
  3348. g_err_tune_dbg_count--;
  3349. } else if (g_err_tune_dbg_error & MTK_MSDC_ERROR_CMD_CRC) {
  3350. intsts = MSDC_INT_RSPCRCERR;
  3351. g_err_tune_dbg_count--;
  3352. }
  3353. pr_err("[%s]:make error cmd:%d,arg=%d,error type=%d, count=%d\n"
  3354. , __func__, g_err_tune_dbg_cmd, g_err_tune_dbg_arg,
  3355. g_err_tune_dbg_error, g_err_tune_dbg_count);
  3356. }
  3357. }
  3358. #ifdef MTK_MSDC_USE_CMD23
  3359. if ((g_err_tune_dbg_cmd == MMC_SET_BLOCK_COUNT)
  3360. && sbc && (host->autocmd & MSDC_AUTOCMD23)) {
  3361. if (g_err_tune_dbg_error & MTK_MSDC_ERROR_ACMD_TMO) {
  3362. intsts = MSDC_INT_ACMDTMO;
  3363. g_err_tune_dbg_count--;
  3364. pr_err("[%s]:make ACMD23 timeout error, count=%d\n",
  3365. __func__, g_err_tune_dbg_count);
  3366. } else if (g_err_tune_dbg_error & MTK_MSDC_ERROR_ACMD_CRC) {
  3367. intsts = MSDC_INT_ACMDCRCERR;
  3368. g_err_tune_dbg_count--;
  3369. pr_err("[%s]:make ACMD23 crc error, count=%d\n",
  3370. __func__, g_err_tune_dbg_count);
  3371. }
  3372. }
  3373. #endif
  3374. }
  3375. #endif
  3376. /* command interrupts */
  3377. if (intsts & cmdsts) {
  3378. #ifdef MTK_MSDC_USE_CMD23
  3379. if ((intsts & MSDC_INT_CMDRDY) || (intsts & MSDC_INT_ACMD19_DONE)) {
  3380. #else
  3381. if ((intsts & MSDC_INT_CMDRDY) || (intsts & MSDC_INT_ACMDRDY)
  3382. || (intsts & MSDC_INT_ACMD19_DONE)) {
  3383. #endif
  3384. u32 *rsp = NULL;
  3385. rsp = &cmd->resp[0];
  3386. switch (host->cmd_rsp) {
  3387. case RESP_NONE:
  3388. break;
  3389. case RESP_R2:
  3390. *rsp++ = sdr_read32(SDC_RESP3);
  3391. *rsp++ = sdr_read32(SDC_RESP2);
  3392. *rsp++ = sdr_read32(SDC_RESP1);
  3393. *rsp++ = sdr_read32(SDC_RESP0);
  3394. break;
  3395. default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  3396. *rsp = sdr_read32(SDC_RESP0);
  3397. /* workaround for latch error */
  3398. if (((cmd->opcode == 13) || (cmd->opcode == 25)) &&
  3399. (*rsp & R1_OUT_OF_RANGE) &&
  3400. (host->hw->host_function == MSDC_EMMC)) {
  3401. pr_err("[%s]: msdc%d XXX CMD<%d> resp<0x%.8x>,bit31=1,force make crc error\n",
  3402. __func__, host->id, cmd->opcode, *rsp);
  3403. cmd->error = (unsigned int)-EIO;
  3404. if (cmd->opcode == 25)
  3405. msdc_reset_hw(host->id);
  3406. }
  3407. break;
  3408. }
  3409. } else if (intsts & MSDC_INT_RSPCRCERR) {
  3410. cmd->error = (unsigned int)-EIO;
  3411. pr_err("[%s]: msdc%d XXX CMD<%d> MSDC_INT_RSPCRCERR Arg<0x%.8x>",
  3412. __func__, host->id, cmd->opcode, cmd->arg);
  3413. if ((MMC_RSP_R1B == mmc_resp_type(cmd))
  3414. && (host->hw->host_function != MSDC_SDIO)) {
  3415. pr_err("[%s]: msdc%d XXX CMD<%d> ARG<0x%.8X> is R1B, CRC not reset hw\n",
  3416. __func__, host->id, cmd->opcode, cmd->arg);
  3417. } else {
  3418. msdc_reset_hw(host->id);
  3419. }
  3420. } else if (intsts & MSDC_INT_CMDTMO) {
  3421. cmd->error = (unsigned int)-ETIMEDOUT;
  3422. if ((host->id != 1) || ((host->id =1) && (cmd->opcode !=1)
  3423. && (cmd->opcode !=52) && (cmd->opcode != 5)))
  3424. pr_err("[%s]: msdc%d XXX CMD<%d> MSDC_INT_CMDTMO Arg<0x%.8x>",
  3425. __func__, host->id, cmd->opcode, cmd->arg);
  3426. if ((cmd->opcode != 52) && (cmd->opcode != 8) && (cmd->opcode != 5)
  3427. && (cmd->opcode != 55) && (cmd->opcode != 1))
  3428. msdc_dump_info(host->id);
  3429. if ((cmd->opcode == 5) && emmc_do_sleep_awake)
  3430. msdc_dump_info(host->id);
  3431. if (((MMC_RSP_R1B == mmc_resp_type(cmd)) || (cmd->opcode == 13))
  3432. && (host->hw->host_function != MSDC_SDIO)) {
  3433. pr_err("[%s]: msdc%d XXX CMD<%d> ARG<0x%.8X> is R1B, TMO not reset hw\n",
  3434. __func__, host->id, cmd->opcode, cmd->arg);
  3435. } else {
  3436. msdc_reset_hw(host->id);
  3437. }
  3438. }
  3439. #ifdef MTK_MSDC_USE_CMD23
  3440. if ((sbc != NULL) && (host->autocmd & MSDC_AUTOCMD23)) {
  3441. if (intsts & MSDC_INT_ACMDRDY) {
  3442. u32 *arsp = &sbc->resp[0];
  3443. *arsp = sdr_read32(SDC_ACMD_RESP);
  3444. CMD_MSG("CMD<23> arg<0x%.8x> @ addr<0x%.8x> resp<0x%.8x>",
  3445. sbc->arg, cmd->arg, sbc->resp[0]);
  3446. } else if (intsts & MSDC_INT_ACMDCRCERR) {
  3447. pr_err("[%s]: msdc%d, autocmd23 crc error\n",
  3448. __func__, host->id);
  3449. sbc->error = (unsigned int)-EIO;
  3450. cmd->error = (unsigned int)-EIO;
  3451. /* host->error |= REQ_CMD23_EIO; */
  3452. /* record the error info in current cmd struct */
  3453. msdc_reset_hw(host->id);
  3454. } else if (intsts & MSDC_INT_ACMDTMO) {
  3455. pr_err("[%s]: msdc%d, autocmd23 tmo error\n",
  3456. __func__, host->id);
  3457. sbc->error = (unsigned int)-ETIMEDOUT;
  3458. cmd->error = (unsigned int)-ETIMEDOUT;
  3459. msdc_dump_info(host->id);
  3460. /* host->error |= REQ_CMD23_TMO; */
  3461. /* record the error info in current cmd struct */
  3462. msdc_reset_hw(host->id);
  3463. }
  3464. }
  3465. #endif /* end of MTK_MSDC_USE_CMD23 */
  3466. }
  3467. out:
  3468. host->cmd = NULL;
  3469. if (!cmd->error)
  3470. CMD_MSG("CMD<%d> arg<0x%.8x> resp<0x%.8x>",
  3471. cmd->opcode, cmd->arg, cmd->resp[0]);
  3472. else
  3473. CMD_MSG("CMD<%d> arg<0x%.8x> resp<0x%.8x>,error=%d",
  3474. cmd->opcode, cmd->arg, cmd->resp[0], cmd->error);
  3475. return cmd->error;
  3476. }
  3477. unsigned int msdc_do_command(struct msdc_host *host, struct mmc_command *cmd,
  3478. int tune, unsigned long timeout)
  3479. {
  3480. if ((cmd->opcode == MMC_GO_IDLE_STATE)
  3481. && (host->hw->host_function == MSDC_SD))
  3482. mdelay(10);
  3483. if (msdc_command_start(host, cmd, tune, timeout))
  3484. goto end;
  3485. if (msdc_command_resp_polling(host, cmd, tune, timeout))
  3486. goto end;
  3487. end:
  3488. return cmd->error;
  3489. }
  3490. /* The abort condition when PIO read/write
  3491. tmo:
  3492. */
  3493. static int msdc_pio_abort(struct msdc_host *host, struct mmc_data *data,
  3494. unsigned long tmo)
  3495. {
  3496. int ret = 0;
  3497. if (atomic_read(&host->abort))
  3498. ret = 1;
  3499. if (time_after(jiffies, tmo)) {
  3500. data->error = (unsigned int)-ETIMEDOUT;
  3501. ERR_MSG("XXX PIO Data Timeout: CMD<%d>", host->mrq->cmd->opcode);
  3502. msdc_dump_info(host->id);
  3503. ret = 1;
  3504. }
  3505. if (ret) {
  3506. msdc_reset_hw(host->id);
  3507. ERR_MSG("msdc pio find abort");
  3508. }
  3509. return ret;
  3510. }
  3511. /*
  3512. * Need to add a timeout, or WDT timeout, system reboot.
  3513. */
  3514. /* pio mode data read/write */
  3515. #define COMBINE_HM
  3516. int msdc_pio_read(struct msdc_host *host, struct mmc_data *data)
  3517. {
  3518. struct scatterlist *sg = data->sg;
  3519. void __iomem *base = host->base;
  3520. u32 num = data->sg_len;
  3521. u32 *ptr;
  3522. u8 *u8ptr;
  3523. u32 left = 0;
  3524. u32 count, size = 0;
  3525. u32 wints = MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR
  3526. | MSDC_INTEN_XFER_COMPL;
  3527. u32 ints = 0;
  3528. bool get_xfer_done = 0;
  3529. unsigned long tmo = jiffies + DAT_TIMEOUT;
  3530. struct page *hmpage = NULL;
  3531. int i = 0, subpage = 0, totalpages = 0;
  3532. int flag = 0;
  3533. ulong kaddr[DIV_ROUND_UP(MAX_SGMT_SZ, PAGE_SIZE)];
  3534. BUG_ON(sg == NULL);
  3535. /*MSDC_CLR_BIT32(MSDC_INTEN, wints);*/
  3536. while (1) {
  3537. if (!get_xfer_done) {
  3538. ints = sdr_read32(MSDC_INT);
  3539. latest_int_status[host->id] = ints;
  3540. ints &= wints;
  3541. sdr_write32(MSDC_INT, ints);
  3542. }
  3543. if (ints & MSDC_INT_DATTMO) {
  3544. data->error = (unsigned int)-ETIMEDOUT;
  3545. msdc_dump_info(host->id);
  3546. msdc_reset_hw(host->id);
  3547. break;
  3548. } else if (ints & MSDC_INT_DATCRCERR) {
  3549. data->error = (unsigned int)-EIO;
  3550. msdc_reset_hw(host->id);
  3551. break;
  3552. } else if (ints & MSDC_INT_XFER_COMPL) {
  3553. get_xfer_done = 1;
  3554. }
  3555. if (get_xfer_done && (num == 0) && (left == 0))
  3556. break;
  3557. if (msdc_pio_abort(host, data, tmo))
  3558. goto end;
  3559. if ((num == 0) && (left == 0))
  3560. continue;
  3561. left = msdc_sg_len(sg, host->dma_xfer);
  3562. ptr = sg_virt(sg);
  3563. flag = 0;
  3564. if ((ptr != NULL) &&
  3565. !(PageHighMem((struct page *)(sg->page_link & ~0x3))))
  3566. #ifndef COMBINE_HM
  3567. goto check_fifo2;
  3568. #else
  3569. goto check_fifo1;
  3570. #endif
  3571. hmpage = (struct page *)(sg->page_link & ~0x3);
  3572. totalpages = DIV_ROUND_UP((left + sg->offset), PAGE_SIZE);
  3573. subpage = (left + sg->offset) % PAGE_SIZE;
  3574. if (subpage != 0 || (sg->offset != 0))
  3575. N_MSG(OPS, "msdc%d: read size or start not align %x, %x, hmpage %lx,sg offset %x\n",
  3576. host->id,
  3577. subpage, left, (ulong)hmpage, sg->offset);
  3578. for (i = 0; i < totalpages; i++) {
  3579. kaddr[i] = (ulong) kmap(hmpage + i);
  3580. if ((i > 0) && ((kaddr[i] - kaddr[i - 1]) != PAGE_SIZE))
  3581. flag = 1;
  3582. if (!kaddr[i])
  3583. ERR_MSG("msdc0:kmap failed %lx", kaddr[i]);
  3584. }
  3585. ptr = sg_virt(sg);
  3586. if (ptr == NULL)
  3587. ERR_MSG("msdc0:sg_virt %p", ptr);
  3588. if (flag == 0)
  3589. #ifndef COMBINE_HM
  3590. goto check_fifo2;
  3591. #else
  3592. goto check_fifo1;
  3593. #endif
  3594. /* High memory and more than 1 va address va
  3595. and not continuous */
  3596. /* pr_err("msdc0: kmap not continuous %x %x %x\n",
  3597. left,kaddr[i],kaddr[i-1]); */
  3598. for (i = 0; i < totalpages; i++) {
  3599. left = PAGE_SIZE;
  3600. ptr = (u32 *) kaddr[i];
  3601. if (i == 0) {
  3602. left = PAGE_SIZE - sg->offset;
  3603. ptr = (u32 *) (kaddr[i] + sg->offset);
  3604. }
  3605. if ((subpage != 0) && (i == (totalpages-1)))
  3606. left = subpage;
  3607. #ifndef COMBINE_HM
  3608. check_fifo1:
  3609. if (left == 0)
  3610. continue;
  3611. #else
  3612. check_fifo1:
  3613. if ((flag == 1) && (left == 0))
  3614. continue;
  3615. else if ((flag == 0) && (left == 0))
  3616. goto check_fifo_end;
  3617. #endif
  3618. if ((msdc_rxfifocnt() >= MSDC_FIFO_THD) &&
  3619. (left >= MSDC_FIFO_THD)) {
  3620. count = MSDC_FIFO_THD >> 2;
  3621. do {
  3622. #ifdef MTK_MSDC_DUMP_FIFO
  3623. pr_debug("0x%x ", msdc_fifo_read32());
  3624. #else
  3625. *ptr++ = msdc_fifo_read32();
  3626. #endif
  3627. } while (--count);
  3628. left -= MSDC_FIFO_THD;
  3629. } else if ((left < MSDC_FIFO_THD) &&
  3630. msdc_rxfifocnt() >= left) {
  3631. while (left > 3) {
  3632. #ifdef MTK_MSDC_DUMP_FIFO
  3633. pr_debug("0x%x ", msdc_fifo_read32());
  3634. #else
  3635. *ptr++ = msdc_fifo_read32();
  3636. #endif
  3637. left -= 4;
  3638. }
  3639. u8ptr = (u8 *) ptr;
  3640. while (left) {
  3641. #ifdef MTK_MSDC_DUMP_FIFO
  3642. pr_debug("0x%x ", msdc_fifo_read8());
  3643. #else
  3644. *u8ptr++ = msdc_fifo_read8();
  3645. #endif
  3646. left--;
  3647. }
  3648. } else {
  3649. ints = sdr_read32(MSDC_INT);
  3650. latest_int_status[host->id] = ints;
  3651. if (ints & MSDC_INT_DATCRCERR) {
  3652. ERR_MSG("[msdc%d] DAT CRC error (0x%x), Left DAT: %d bytes\n",
  3653. host->id, ints, left);
  3654. data->error = (unsigned int)-EIO;
  3655. } else if (ints & MSDC_INT_DATTMO) {
  3656. ERR_MSG("[msdc%d] DAT TMO error (0x%x), Left DAT: %d bytes\n",
  3657. host->id, ints, left);
  3658. data->error = (unsigned int)-ETIMEDOUT;
  3659. } else {
  3660. goto skip_msdc_dump_and_reset1;
  3661. }
  3662. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  3663. if ((atomic_read(&host->ot_work.autok_done)
  3664. != 0) ||
  3665. (host->hw->host_function != MSDC_SDIO) ||
  3666. (host->mclk <= 50*1000*1000))
  3667. msdc_dump_info(host->id);
  3668. #else
  3669. if (ints & MSDC_INT_DATTMO)
  3670. msdc_dump_info(host->id);
  3671. #endif
  3672. sdr_write32(MSDC_INT, ints);
  3673. msdc_reset_hw(host->id);
  3674. goto end;
  3675. }
  3676. skip_msdc_dump_and_reset1:
  3677. if (msdc_pio_abort(host, data, tmo))
  3678. goto end;
  3679. goto check_fifo1;
  3680. }
  3681. check_fifo_end:
  3682. if (hmpage != NULL) {
  3683. /* pr_err("read msdc0:unmap %x\n", hmpage); */
  3684. for (i = 0; i < totalpages; i++)
  3685. kunmap(hmpage + i);
  3686. hmpage = NULL;
  3687. }
  3688. size += msdc_sg_len(sg, host->dma_xfer);
  3689. sg = sg_next(sg);
  3690. num--;
  3691. }
  3692. end:
  3693. if (hmpage != NULL) {
  3694. for (i = 0; i < totalpages; i++)
  3695. kunmap(hmpage + i);
  3696. /* pr_err("msdc0 read unmap:\n"); */
  3697. }
  3698. data->bytes_xfered += size;
  3699. N_MSG(FIO, " PIO Read<%d>bytes", size);
  3700. /* MSDC_CLR_BIT32(MSDC_INTEN, wints); */
  3701. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  3702. /* auto-K have not done or finished */
  3703. if ((atomic_read(&host->ot_work.autok_done) == 0) &&
  3704. (is_card_sdio(host)))
  3705. return data->error;
  3706. #endif
  3707. if (data->error)
  3708. ERR_MSG("read pio data->error<%d> left<%d> size<%d>",
  3709. data->error, left, size);
  3710. return data->error;
  3711. }
  3712. /* please make sure won't using PIO when size >= 512
  3713. which means, memory card block read/write won't using pio
  3714. then don't need to handle the CMD12 when data error.
  3715. */
  3716. int msdc_pio_write(struct msdc_host *host, struct mmc_data *data)
  3717. {
  3718. void __iomem *base = host->base;
  3719. struct scatterlist *sg = data->sg;
  3720. u32 num = data->sg_len;
  3721. u32 *ptr;
  3722. u8 *u8ptr;
  3723. u32 left = 0;
  3724. u32 count, size = 0;
  3725. u32 wints = MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR
  3726. | MSDC_INTEN_XFER_COMPL;
  3727. bool get_xfer_done = 0;
  3728. unsigned long tmo = jiffies + DAT_TIMEOUT;
  3729. u32 ints = 0;
  3730. struct page *hmpage = NULL;
  3731. int i = 0, totalpages = 0;
  3732. int flag, subpage = 0;
  3733. ulong kaddr[DIV_ROUND_UP(MAX_SGMT_SZ, PAGE_SIZE)];
  3734. /* MSDC_CLR_BIT32(MSDC_INTEN, wints); */
  3735. while (1) {
  3736. if (!get_xfer_done) {
  3737. ints = sdr_read32(MSDC_INT);
  3738. latest_int_status[host->id] = ints;
  3739. ints &= wints;
  3740. sdr_write32(MSDC_INT, ints);
  3741. }
  3742. if (ints & MSDC_INT_DATTMO) {
  3743. data->error = (unsigned int)-ETIMEDOUT;
  3744. msdc_dump_info(host->id);
  3745. msdc_reset_hw(host->id);
  3746. break;
  3747. } else if (ints & MSDC_INT_DATCRCERR) {
  3748. data->error = (unsigned int)-EIO;
  3749. msdc_reset_hw(host->id);
  3750. break;
  3751. } else if (ints & MSDC_INT_XFER_COMPL) {
  3752. get_xfer_done = 1;
  3753. if ((num == 0) && (left == 0))
  3754. break;
  3755. }
  3756. if (msdc_pio_abort(host, data, tmo))
  3757. goto end;
  3758. if ((num == 0) && (left == 0))
  3759. continue;
  3760. left = msdc_sg_len(sg, host->dma_xfer);
  3761. ptr = sg_virt(sg);
  3762. flag = 0;
  3763. /* High memory must kmap, if already mapped,
  3764. only add counter */
  3765. if ((ptr != NULL) &&
  3766. !(PageHighMem((struct page *)(sg->page_link & ~0x3))))
  3767. #ifndef COMBINE_HM
  3768. goto check_fifo2;
  3769. #else
  3770. goto check_fifo1;
  3771. #endif
  3772. hmpage = (struct page *)(sg->page_link & ~0x3);
  3773. totalpages = DIV_ROUND_UP(left + sg->offset, PAGE_SIZE);
  3774. subpage = (left + sg->offset) % PAGE_SIZE;
  3775. if ((subpage != 0) || (sg->offset != 0))
  3776. N_MSG(OPS, "msdc%d: write size or start not align %x, %x, hmpage %lx,sg offset %x\n",
  3777. host->id,
  3778. subpage, left, (ulong)hmpage, sg->offset);
  3779. /* Kmap all need pages, */
  3780. for (i = 0; i < totalpages; i++) {
  3781. kaddr[i] = (ulong) kmap(hmpage + i);
  3782. if ((i > 0) && ((kaddr[i] - kaddr[i - 1]) != PAGE_SIZE))
  3783. flag = 1;
  3784. if (!kaddr[i])
  3785. ERR_MSG("msdc0:kmap failed %lx\n", kaddr[i]);
  3786. }
  3787. ptr = sg_virt(sg);
  3788. if (ptr == NULL)
  3789. ERR_MSG("msdc0:write sg_virt %p\n", ptr);
  3790. if (flag == 0)
  3791. #ifndef COMBINE_HM
  3792. goto check_fifo2;
  3793. #else
  3794. goto check_fifo1;
  3795. #endif
  3796. /* High memory and more than 1 va address va
  3797. may be not continuous */
  3798. /*pr_err(ERR "msdc0:w kmap not continuous %x %x %x\n",
  3799. left, kaddr[i], kaddr[i-1]);*/
  3800. for (i = 0; i < totalpages; i++) {
  3801. left = PAGE_SIZE;
  3802. ptr = (u32 *) kaddr[i];
  3803. if (i == 0) {
  3804. left = PAGE_SIZE - sg->offset;
  3805. ptr = (u32 *) (kaddr[i] + sg->offset);
  3806. }
  3807. if (subpage != 0 && (i == (totalpages - 1)))
  3808. left = subpage;
  3809. #ifndef COMBINE_HM
  3810. check_fifo1:
  3811. if (left == 0)
  3812. continue;
  3813. #else
  3814. check_fifo1:
  3815. if ((flag == 1) && (left == 0))
  3816. continue;
  3817. else if ((flag == 0) && (left == 0))
  3818. goto check_fifo_end;
  3819. #endif
  3820. if (left >= MSDC_FIFO_SZ && msdc_txfifocnt() == 0) {
  3821. count = MSDC_FIFO_SZ >> 2;
  3822. do {
  3823. msdc_fifo_write32(*ptr);
  3824. ptr++;
  3825. } while (--count);
  3826. left -= MSDC_FIFO_SZ;
  3827. } else if (left < MSDC_FIFO_SZ &&
  3828. msdc_txfifocnt() == 0) {
  3829. while (left > 3) {
  3830. msdc_fifo_write32(*ptr);
  3831. ptr++;
  3832. left -= 4;
  3833. }
  3834. u8ptr = (u8 *) ptr;
  3835. while (left) {
  3836. msdc_fifo_write8(*u8ptr);
  3837. u8ptr++;
  3838. left--;
  3839. }
  3840. } else {
  3841. ints = sdr_read32(MSDC_INT);
  3842. latest_int_status[host->id] = ints;
  3843. if (ints & MSDC_INT_DATCRCERR) {
  3844. ERR_MSG("[msdc%d] DAT CRC error (0x%x), Left DAT: %d bytes\n",
  3845. host->id, ints, left);
  3846. data->error = (unsigned int)-EIO;
  3847. } else if (ints & MSDC_INT_DATTMO) {
  3848. ERR_MSG("[msdc%d] DAT TMO error (0x%x), Left DAT: %d bytes\n",
  3849. host->id, ints, left);
  3850. data->error = (unsigned int)-ETIMEDOUT;
  3851. } else {
  3852. goto skip_msdc_dump_and_reset1;
  3853. }
  3854. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  3855. if ((atomic_read(&host->ot_work.autok_done)
  3856. != 0) ||
  3857. (host->hw->host_function != MSDC_SDIO) ||
  3858. (host->mclk <= 50*1000*1000))
  3859. msdc_dump_info(host->id);
  3860. #else
  3861. if (ints & MSDC_INT_DATTMO)
  3862. msdc_dump_info(host->id);
  3863. #endif
  3864. sdr_write32(MSDC_INT, ints);
  3865. msdc_reset_hw(host->id);
  3866. goto end;
  3867. }
  3868. skip_msdc_dump_and_reset1:
  3869. if (msdc_pio_abort(host, data, tmo))
  3870. goto end;
  3871. goto check_fifo1;
  3872. }
  3873. check_fifo_end:
  3874. if (hmpage != NULL) {
  3875. for (i = 0; i < totalpages; i++)
  3876. kunmap(hmpage + i);
  3877. hmpage = NULL;
  3878. }
  3879. size += msdc_sg_len(sg, host->dma_xfer);
  3880. sg = sg_next(sg);
  3881. num--;
  3882. }
  3883. end:
  3884. if (hmpage != NULL) {
  3885. for (i = 0; i < totalpages; i++)
  3886. kunmap(hmpage + i);
  3887. pr_err("msdc0 write unmap 0x%x:\n", left);
  3888. }
  3889. data->bytes_xfered += size;
  3890. N_MSG(FIO, " PIO Write<%d>bytes", size);
  3891. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  3892. /* auto-K have not done or finished */
  3893. if ((is_card_sdio(host)) &&
  3894. (atomic_read(&host->ot_work.autok_done) == 0))
  3895. return data->error;
  3896. #endif
  3897. if (data->error)
  3898. ERR_MSG("write pio data->error<%d> left<%d> size<%d>",
  3899. data->error, left, size);
  3900. /*MSDC_CLR_BIT32(MSDC_INTEN, wints);*/
  3901. return data->error;
  3902. }
  3903. static void msdc_dma_start(struct msdc_host *host)
  3904. {
  3905. void __iomem *base = host->base;
  3906. u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO
  3907. | MSDC_INTEN_DATCRCERR;
  3908. if (host->autocmd & MSDC_AUTOCMD12)
  3909. wints |= MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | MSDC_INT_ACMDRDY;
  3910. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
  3911. sdr_set_bits(MSDC_INTEN, wints);
  3912. N_MSG(DMA, "DMA start");
  3913. if (host->data && host->data->flags & MMC_DATA_WRITE) {
  3914. host->write_timeout_ms = min_t(u32, max_t(u32, host->data->blocks * 500,
  3915. host->data->timeout_ns / 1000000), 270 * 1000);
  3916. schedule_delayed_work(&host->write_timeout,
  3917. msecs_to_jiffies(host->write_timeout_ms));
  3918. N_MSG(DMA, "DMA Data Busy Timeout:%u ms, schedule_delayed_work",
  3919. host->write_timeout_ms);
  3920. }
  3921. }
  3922. static void msdc_dma_stop(struct msdc_host *host)
  3923. {
  3924. void __iomem *base = host->base;
  3925. int retry = 500;
  3926. int count = 1000;
  3927. u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO
  3928. | MSDC_INTEN_DATCRCERR;
  3929. if (host->data && host->data->flags & MMC_DATA_WRITE) {
  3930. cancel_delayed_work(&host->write_timeout);
  3931. N_MSG(DMA, "DMA Data Busy Timeout:%u ms, cancel_delayed_work",
  3932. host->write_timeout_ms);
  3933. host->write_timeout_ms = 0;
  3934. }
  3935. /* handle autocmd12 error in msdc_irq */
  3936. if (host->autocmd & MSDC_AUTOCMD12)
  3937. wints |= MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | MSDC_INT_ACMDRDY;
  3938. N_MSG(DMA, "DMA status: 0x%.8x", sdr_read32(MSDC_DMA_CFG));
  3939. /* while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);*/
  3940. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
  3941. /* while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS); */
  3942. msdc_retry((sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS),
  3943. retry, count, host->id);
  3944. if (retry == 0) {
  3945. ERR_MSG("###### Failed to stop DMA! start dump######");
  3946. dump_emi_info();
  3947. mdelay(10);
  3948. dump_axi_bus_info();
  3949. mdelay(10);
  3950. #ifndef FPGA_PLATFORM
  3951. /* dump_audio_info(); */
  3952. #endif
  3953. mdelay(10);
  3954. ERR_MSG("####### Failed to stop DMA! finish dump######");
  3955. msdc_polling_axi_status(__LINE__, 1);
  3956. }
  3957. sdr_clr_bits(MSDC_INTEN, wints); /* Not just xfer_comp */
  3958. N_MSG(DMA, "DMA stop");
  3959. }
  3960. /* calc checksum */
  3961. static u8 msdc_dma_calcs(u8 *buf, u32 len)
  3962. {
  3963. u32 i, sum = 0;
  3964. for (i = 0; i < len; i++)
  3965. sum += buf[i];
  3966. return 0xFF - (u8) sum;
  3967. }
  3968. /* gpd bd setup + dma registers */
  3969. static int msdc_dma_config(struct msdc_host *host, struct msdc_dma *dma)
  3970. {
  3971. void __iomem *base = host->base;
  3972. u32 sglen = dma->sglen;
  3973. /* u32 i, j, num, bdlen, arg, xfersz; */
  3974. u32 j, num, bdlen;
  3975. dma_addr_t dma_address;
  3976. u32 dma_len;
  3977. u8 blkpad, dwpad, chksum;
  3978. struct scatterlist *sg = dma->sg;
  3979. struct gpd_t *gpd;
  3980. struct bd_t *bd;
  3981. switch (dma->mode) {
  3982. case MSDC_MODE_DMA_BASIC:
  3983. #if defined(FEATURE_MET_MMC_INDEX)
  3984. met_mmc_bdnum = 1;
  3985. #endif
  3986. if (host->hw->host_function == MSDC_SDIO)
  3987. BUG_ON(dma->xfersz > 0xFFFFFFFF);
  3988. else
  3989. BUG_ON(dma->xfersz > 65535);
  3990. BUG_ON(dma->sglen != 1);
  3991. dma_address = sg_dma_address(sg);
  3992. dma_len = msdc_sg_len(sg, host->dma_xfer);
  3993. N_MSG(DMA, "DMA BASIC mode dma_len<%x> dma_address<%llx>",
  3994. dma_len, (u64) dma_address);
  3995. sdr_write32(MSDC_DMA_SA, dma_address);
  3996. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_LASTBUF, 1);
  3997. sdr_write32(MSDC_DMA_LEN, dma_len);
  3998. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ, dma->burstsz);
  3999. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 0);
  4000. break;
  4001. case MSDC_MODE_DMA_DESC:
  4002. blkpad = (dma->flags & DMA_FLAG_PAD_BLOCK) ? 1 : 0;
  4003. dwpad = (dma->flags & DMA_FLAG_PAD_DWORD) ? 1 : 0;
  4004. chksum = (dma->flags & DMA_FLAG_EN_CHKSUM) ? 1 : 0;
  4005. /* calculate the required number of gpd */
  4006. num = (sglen + MAX_BD_PER_GPD - 1) / MAX_BD_PER_GPD;
  4007. BUG_ON(num != 1);
  4008. gpd = dma->gpd;
  4009. bd = dma->bd;
  4010. bdlen = sglen;
  4011. #if defined(FEATURE_MET_MMC_INDEX)
  4012. met_mmc_bdnum = bdlen;
  4013. #endif
  4014. /* modify gpd */
  4015. /* gpd->intr = 0; */
  4016. gpd->hwo = 1; /* hw will clear it */
  4017. gpd->bdp = 1;
  4018. gpd->chksum = 0; /* need to clear first. */
  4019. gpd->chksum = (chksum ? msdc_dma_calcs((u8 *) gpd, 16) : 0);
  4020. /* modify bd */
  4021. for (j = 0; j < bdlen; j++) {
  4022. #ifdef MSDC_DMA_VIOLATION_DEBUG
  4023. if (g_dma_debug[host->id]
  4024. && (msdc_latest_operation_type[host->id] == OPER_TYPE_READ)) {
  4025. pr_debug("[%s] msdc%d do write 0x10000\n", __func__, host->id);
  4026. dma_address = 0x10000;
  4027. } else
  4028. dma_address = sg_dma_address(sg);
  4029. #else
  4030. dma_address = sg_dma_address(sg);
  4031. #endif
  4032. dma_len = msdc_sg_len(sg, host->dma_xfer);
  4033. N_MSG(DMA, "DMA DESC mode dma_len<%x> dma_address<%llx>",
  4034. dma_len, (u64) dma_address);
  4035. msdc_init_bd(&bd[j], blkpad, dwpad, dma_address, dma_len);
  4036. if (j == bdlen - 1)
  4037. bd[j].eol = 1; /* the last bd */
  4038. else
  4039. bd[j].eol = 0;
  4040. bd[j].chksum = 0; /* checksume need to clear first */
  4041. bd[j].chksum = (chksum ? msdc_dma_calcs((u8 *) (&bd[j]), 16) : 0);
  4042. sg++;
  4043. }
  4044. #ifdef MSDC_DMA_VIOLATION_DEBUG
  4045. if (g_dma_debug[host->id]
  4046. && (msdc_latest_operation_type[host->id] == OPER_TYPE_READ))
  4047. g_dma_debug[host->id] = 0;
  4048. #endif
  4049. dma->used_gpd += 2;
  4050. dma->used_bd += bdlen;
  4051. sdr_set_field(MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, chksum);
  4052. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ, dma->burstsz);
  4053. sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 1);
  4054. sdr_write32(MSDC_DMA_SA, (u32) dma->gpd_addr);
  4055. break;
  4056. default:
  4057. break;
  4058. }
  4059. N_MSG(DMA, "DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
  4060. N_MSG(DMA, "DMA_CFG = 0x%x", sdr_read32(MSDC_DMA_CFG));
  4061. N_MSG(DMA, "DMA_SA = 0x%x", sdr_read32(MSDC_DMA_SA));
  4062. return 0;
  4063. }
  4064. static void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
  4065. struct scatterlist *sg, unsigned int sglen)
  4066. {
  4067. u32 max_dma_len = 0;
  4068. BUG_ON(sglen > MAX_BD_NUM); /* not support currently */
  4069. dma->sg = sg;
  4070. dma->flags = DMA_FLAG_EN_CHKSUM;
  4071. /* dma->flags = DMA_FLAG_NONE; */ /* CHECKME */
  4072. dma->sglen = sglen;
  4073. dma->xfersz = host->xfer_size;
  4074. dma->burstsz = MSDC_BRUST_64B;
  4075. if (host->hw->host_function == MSDC_SDIO)
  4076. max_dma_len = MAX_DMA_CNT_SDIO;
  4077. else
  4078. max_dma_len = MAX_DMA_CNT;
  4079. if (sglen == 1 && msdc_sg_len(sg, host->dma_xfer) <= max_dma_len)
  4080. dma->mode = MSDC_MODE_DMA_BASIC;
  4081. else
  4082. dma->mode = MSDC_MODE_DMA_DESC;
  4083. N_MSG(DMA, "DMA mode<%d> sglen<%d> xfersz<%d>",
  4084. dma->mode, dma->sglen, dma->xfersz);
  4085. msdc_dma_config(host, dma);
  4086. }
  4087. /* set block number before send command */
  4088. static void msdc_set_blknum(struct msdc_host *host, u32 blknum)
  4089. {
  4090. void __iomem *base = host->base;
  4091. sdr_write32(SDC_BLK_NUM, blknum);
  4092. }
  4093. #define REQ_CMD_EIO (0x1 << 0)
  4094. #define REQ_CMD_TMO (0x1 << 1)
  4095. #define REQ_DAT_ERR (0x1 << 2)
  4096. #define REQ_STOP_EIO (0x1 << 3)
  4097. #define REQ_STOP_TMO (0x1 << 4)
  4098. #define REQ_CMD23_EIO (0x1 << 5)
  4099. #define REQ_CMD23_TMO (0x1 << 6)
  4100. static void msdc_restore_info(struct msdc_host *host)
  4101. {
  4102. void __iomem *base = host->base;
  4103. int retry = 3;
  4104. msdc_reset_hw(host->id);
  4105. /* force bit5(BV18SDT) to 0 */
  4106. host->saved_para.msdc_cfg = host->saved_para.msdc_cfg & 0xFFFFFFDF;
  4107. sdr_write32(MSDC_CFG, host->saved_para.msdc_cfg);
  4108. while (retry--) {
  4109. msdc_set_mclk(host, host->saved_para.timing, host->saved_para.hz);
  4110. if ((sdr_read32(MSDC_CFG) & 0xFFFFFF9F) !=
  4111. (host->saved_para.msdc_cfg & 0xFFFFFF9F)) {
  4112. ERR_MSG("msdc set_mclk is unstable (cur_cfg=%x,save_cfg=%x, cur_hz=%d, save_hz=%d)."
  4113. , sdr_read32(MSDC_CFG), host->saved_para.msdc_cfg, host->mclk,
  4114. host->saved_para.hz);
  4115. } else
  4116. break;
  4117. }
  4118. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  4119. host->saved_para.int_dat_latch_ck_sel); /* for SDIO 3.0 */
  4120. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_CKGEN_MSDC_DLY_SEL,
  4121. host->saved_para.ckgen_msdc_dly_sel); /* for SDIO 3.0 */
  4122. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  4123. host->saved_para.cmd_resp_ta_cntr); /* for SDIO 3.0 */
  4124. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  4125. host->saved_para.wrdat_crc_ta_cntr); /* for SDIO 3.0 */
  4126. sdr_write32(MSDC_DAT_RDDLY0, host->saved_para.ddly0);
  4127. sdr_write32(MSDC_PAD_TUNE0, host->saved_para.pad_tune0);
  4128. sdr_write32(SDC_CFG, host->saved_para.sdc_cfg);
  4129. sdr_set_field(MSDC_INTEN, MSDC_INT_SDIOIRQ,
  4130. host->saved_para.inten_sdio_irq); /* get INTEN status for SDIO */
  4131. sdr_write32(MSDC_IOCON, host->saved_para.iocon);
  4132. if (host->hw->host_function == MSDC_SDIO) {
  4133. host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
  4134. host->mmc->rescan_entered = 0;
  4135. }
  4136. }
  4137. static void msdc_update_cahce_status(struct msdc_host *host,
  4138. struct mmc_request *mrq)
  4139. {
  4140. #ifdef MTK_MSDC_USE_CACHE
  4141. struct mmc_command *cmd;
  4142. struct mmc_data *data;
  4143. cmd = mrq->cmd;
  4144. if ((host->hw->host_function == MSDC_EMMC)
  4145. && host->mmc->card && (host->mmc->card->ext_csd.cache_ctrl & 0x1)) {
  4146. if (cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  4147. data = mrq->cmd->data;
  4148. if ((host->error == 0) && mrq->sbc
  4149. && (((mrq->sbc->arg >> 24) & 0x1)
  4150. || ((mrq->sbc->arg >> 31) & 0x1))) {
  4151. /* if reliable write, or force prg write emmc device success,
  4152. * do set cache flushed status.
  4153. */
  4154. if (g_cache_status == CACHE_UN_FLUSHED) {
  4155. g_cache_status = CACHE_FLUSHED;
  4156. g_flush_data_size = 0;
  4157. }
  4158. } else if (host->error == 0) {
  4159. /* if normal write emmc device successfully,
  4160. * do clear the cache flushed status
  4161. */
  4162. if (g_cache_status == CACHE_FLUSHED) {
  4163. g_cache_status = CACHE_UN_FLUSHED;
  4164. N_MSG(CHE, "normal write happen,update g_cache_status = %d",
  4165. g_cache_status);
  4166. }
  4167. g_flush_data_size += data->blocks;
  4168. } else if (host->error) {
  4169. g_flush_data_size += data->blocks;
  4170. ERR_MSG("write error happend, g_flush_data_size=%lld",
  4171. g_flush_data_size);
  4172. }
  4173. } else if ((cmd->opcode == MMC_SWITCH)
  4174. && (((cmd->arg >> 16) & 0xFF) == EXT_CSD_FLUSH_CACHE)
  4175. && (((cmd->arg >> 8) & 0x1)) && !g_bypass_flush) {
  4176. if (host->error == 0) {
  4177. /* if flush cache of emmc device successfully,
  4178. * do set the cache flushed status
  4179. */
  4180. g_cache_status = CACHE_FLUSHED;
  4181. N_MSG(CHE, "flush happend, update g_cache_status = %d;"
  4182. "g_flush_data_size=%lld", g_cache_status,
  4183. g_flush_data_size);
  4184. g_flush_data_size = 0;
  4185. } else {
  4186. g_flush_error_happend = 1;
  4187. }
  4188. }
  4189. }
  4190. #endif
  4191. }
  4192. static int msdc_do_request(struct mmc_host *mmc, struct mmc_request *mrq)
  4193. {
  4194. struct msdc_host *host = mmc_priv(mmc);
  4195. struct mmc_command *cmd;
  4196. struct mmc_data *data;
  4197. u32 l_autocmd23_is_set = 0;
  4198. #ifdef MTK_MSDC_USE_CMD23
  4199. u32 l_card_no_cmd23 = 0;
  4200. #endif
  4201. void __iomem *base = host->base;
  4202. /* u32 intsts = 0; */
  4203. int dma = 0, read = 1, dir = DMA_FROM_DEVICE, send_type = 0;
  4204. u32 map_sg = 0;
  4205. unsigned long pio_tmo;
  4206. unsigned int left = 0;
  4207. #ifdef MTK_MSDC_USE_CACHE
  4208. u32 l_force_prg = 0;
  4209. g_bypass_flush = 0;
  4210. #endif
  4211. #define SND_DAT 0
  4212. #define SND_CMD 1
  4213. if (is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ)) {
  4214. /* mb(); */
  4215. if (host->saved_para.hz) {
  4216. if (host->saved_para.suspend_flag) {
  4217. ERR_MSG("msdc resume[s] cur_cfg=%x, save_cfg=%x,cur_hz=%d, save_hz=%d"
  4218. , sdr_read32(MSDC_CFG), host->saved_para.msdc_cfg,
  4219. host->mclk, host->saved_para.hz);
  4220. host->saved_para.suspend_flag = 0;
  4221. msdc_restore_info(host);
  4222. } else if ((host->saved_para.msdc_cfg != 0) &&
  4223. ((sdr_read32(MSDC_CFG) & 0xFFFFFF9F) !=
  4224. (host->saved_para.msdc_cfg & 0xFFFFFF9F))) {
  4225. ERR_MSG("msdc resume[ns] cur_cfg=%x, save_cfg=%x,cur_hz=%d, save_hz=%d"
  4226. , sdr_read32(MSDC_CFG), host->saved_para.msdc_cfg,
  4227. host->mclk, host->saved_para.hz);
  4228. msdc_restore_info(host);
  4229. }
  4230. }
  4231. }
  4232. #if (MSDC_DATA1_INT == 1)
  4233. if (host->hw->flags & MSDC_SDIO_IRQ) {
  4234. /* if((!u_sdio_irq_counter) && (!u_msdc_irq_counter))
  4235. ERR_MSG("Ahsin u_sdio_irq_counter=%d, u_msdc_irq_counter=%d "
  4236. "int_sdio_irq_enable=%d SDC_CFG=%x MSDC_INTEN=%x MSDC_INT=%x "
  4237. "MSDC_PATCH_BIT0=%x", u_sdio_irq_counter, u_msdc_irq_counter,
  4238. int_sdio_irq_enable, sdr_read32(SDC_CFG),sdr_read32(MSDC_INTEN),
  4239. sdr_read32(MSDC_INT),sdr_read32(MSDC_PATCH_BIT0)); */
  4240. if ((u_sdio_irq_counter > 0) && ((u_sdio_irq_counter % 800) == 0))
  4241. ERR_MSG("Ahsin sdio_irq=%d, msdc_irq=%d SDC_CFG=%x MSDC_INTEN=%x MSDC_INT=%x",
  4242. u_sdio_irq_counter, u_msdc_irq_counter, sdr_read32(SDC_CFG),
  4243. sdr_read32(MSDC_INTEN), sdr_read32(MSDC_INT));
  4244. }
  4245. #endif
  4246. BUG_ON(mmc == NULL);
  4247. BUG_ON(mrq == NULL);
  4248. host->error = 0;
  4249. atomic_set(&host->abort, 0);
  4250. cmd = mrq->cmd;
  4251. data = mrq->cmd->data;
  4252. /* check msdc is work ok
  4253. * rule is RX/TX fifocnt must be zero after last request
  4254. * if find abnormal, try to reset msdc first
  4255. */
  4256. if (msdc_txfifocnt() || msdc_rxfifocnt()) {
  4257. pr_err("[SD%d] register abnormal,please check!\n", host->id);
  4258. msdc_reset_hw(host->id);
  4259. }
  4260. if (!data) {
  4261. send_type = SND_CMD;
  4262. #ifdef MTK_MSDC_USE_CACHE
  4263. if ((host->hw->host_function == MSDC_EMMC)
  4264. && (cmd->opcode == MMC_SWITCH)
  4265. && (((cmd->arg >> 16) & 0xFF) == EXT_CSD_FLUSH_CACHE)
  4266. && (((cmd->arg >> 8) & 0x1))) {
  4267. if (g_cache_status == CACHE_FLUSHED) {
  4268. N_MSG(CHE, "bypass flush command, g_cache_status=%d",
  4269. g_cache_status);
  4270. g_bypass_flush = 1;
  4271. goto done;
  4272. }
  4273. }
  4274. #endif
  4275. if (msdc_do_command(host, cmd, 0, CMD_TIMEOUT))
  4276. goto done;
  4277. if ((host->hw->host_function == MSDC_EMMC) &&
  4278. (cmd->opcode == MMC_ALL_SEND_CID))
  4279. emmc_id = UNSTUFF_BITS(cmd->resp, 120, 8);
  4280. #ifdef MTK_EMMC_ETT_TO_DRIVER
  4281. if ((host->hw->host_function == MSDC_EMMC)
  4282. && (cmd->opcode == MMC_ALL_SEND_CID)) {
  4283. m_id = UNSTUFF_BITS(cmd->resp, 120, 8);
  4284. pro_name[0] = UNSTUFF_BITS(cmd->resp, 96, 8);
  4285. pro_name[1] = UNSTUFF_BITS(cmd->resp, 88, 8);
  4286. pro_name[2] = UNSTUFF_BITS(cmd->resp, 80, 8);
  4287. pro_name[3] = UNSTUFF_BITS(cmd->resp, 72, 8);
  4288. pro_name[4] = UNSTUFF_BITS(cmd->resp, 64, 8);
  4289. pro_name[5] = UNSTUFF_BITS(cmd->resp, 56, 8);
  4290. /* pro_name[6] = '\0'; */
  4291. }
  4292. #endif
  4293. } else {
  4294. BUG_ON(data->blksz > HOST_MAX_BLKSZ);
  4295. send_type = SND_DAT;
  4296. #ifdef MTK_MSDC_USE_CACHE
  4297. if ((host->hw->host_function == MSDC_EMMC) && host->mmc->card
  4298. && (host->mmc->card->ext_csd.cache_ctrl & 0x1)
  4299. && (cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK))
  4300. l_force_prg = !msdc_can_apply_cache(cmd->arg, data->blocks);
  4301. #endif
  4302. data->error = 0;
  4303. read = data->flags & MMC_DATA_READ ? 1 : 0;
  4304. msdc_latest_operation_type[host->id] =
  4305. read ? OPER_TYPE_READ : OPER_TYPE_WRITE;
  4306. host->data = data;
  4307. host->xfer_size = data->blocks * data->blksz;
  4308. host->blksz = data->blksz;
  4309. /* deside the transfer mode */
  4310. if (drv_mode[host->id] == MODE_PIO) {
  4311. host->dma_xfer = dma = 0;
  4312. msdc_latest_transfer_mode[host->id] = TRAN_MOD_PIO;
  4313. } else if (drv_mode[host->id] == MODE_DMA) {
  4314. host->dma_xfer = dma = 1;
  4315. msdc_latest_transfer_mode[host->id] = TRAN_MOD_DMA;
  4316. } else if (drv_mode[host->id] == MODE_SIZE_DEP) {
  4317. host->dma_xfer = dma =
  4318. ((host->xfer_size >= dma_size[host->id]) ? 1 : 0);
  4319. msdc_latest_transfer_mode[host->id] =
  4320. dma ? TRAN_MOD_DMA : TRAN_MOD_PIO;
  4321. }
  4322. if (read) {
  4323. if ((host->timeout_ns != data->timeout_ns)
  4324. || (host->timeout_clks != data->timeout_clks)) {
  4325. msdc_set_timeout(host, data->timeout_ns, data->timeout_clks);
  4326. }
  4327. }
  4328. msdc_set_blknum(host, data->blocks);
  4329. /* msdc_clr_fifo(); */ /* no need */
  4330. #ifdef MTK_MSDC_USE_CMD23
  4331. if (0 == (host->autocmd & MSDC_AUTOCMD23)) {
  4332. /* start the cmd23 first, mrq->sbc is NULL with single r/w */
  4333. if (mrq->sbc) {
  4334. host->autocmd &= ~MSDC_AUTOCMD12;
  4335. if (host->hw->host_function == MSDC_EMMC) {
  4336. #ifdef MTK_MSDC_USE_CACHE
  4337. if (l_force_prg && !((mrq->sbc->arg >> 31) & 0x1))
  4338. mrq->sbc->arg |= (1 << 24);
  4339. #endif
  4340. }
  4341. if (msdc_command_start(host, mrq->sbc, 0, CMD_TIMEOUT) != 0)
  4342. goto done;
  4343. /* then wait command done */
  4344. if (msdc_command_resp_polling(host, mrq->sbc, 0, CMD_TIMEOUT))
  4345. goto stop;
  4346. } else {
  4347. /* some sd card may not support cmd23,
  4348. * some emmc card have problem with cmd23, so use cmd12 here */
  4349. if (host->hw->host_function != MSDC_SDIO)
  4350. host->autocmd |= MSDC_AUTOCMD12;
  4351. }
  4352. } else {
  4353. /* enable auto cmd23 */
  4354. if (mrq->sbc) {
  4355. host->autocmd &= ~MSDC_AUTOCMD12;
  4356. if (host->hw->host_function == MSDC_EMMC) {
  4357. #ifdef MTK_MSDC_USE_CACHE
  4358. if (l_force_prg && !((mrq->sbc->arg >> 31) & 0x1))
  4359. mrq->sbc->arg |= (1 << 24);
  4360. #endif
  4361. }
  4362. } else {
  4363. /* some sd card may not support cmd23,
  4364. * some emmc card have problem with cmd23, so use cmd12 here */
  4365. if (host->hw->host_function != MSDC_SDIO) {
  4366. host->autocmd &= ~MSDC_AUTOCMD23;
  4367. host->autocmd |= MSDC_AUTOCMD12;
  4368. l_card_no_cmd23 = 1;
  4369. }
  4370. }
  4371. }
  4372. #endif /* end of MTK_MSDC_USE_CMD23 */
  4373. if (dma) {
  4374. msdc_dma_on(); /* enable DMA mode first!! */
  4375. init_completion(&host->xfer_done);
  4376. #ifndef MTK_MSDC_USE_CMD23
  4377. /* start the command first */
  4378. if (host->hw->host_function != MSDC_SDIO)
  4379. host->autocmd |= MSDC_AUTOCMD12;
  4380. #endif
  4381. if (msdc_command_start(host, cmd, 0, CMD_TIMEOUT) != 0)
  4382. goto done;
  4383. dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  4384. (void)dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
  4385. map_sg = 1;
  4386. /* then wait command done */
  4387. if (msdc_command_resp_polling(host, cmd, 0, CMD_TIMEOUT))
  4388. goto stop;
  4389. /* for read, the data coming too fast, then CRC error
  4390. * start DMA no business with CRC.
  4391. * init_completion(&host->xfer_done); */
  4392. msdc_dma_setup(host, &host->dma, data->sg, data->sg_len);
  4393. msdc_dma_start(host);
  4394. #ifdef STO_LOG
  4395. if (unlikely(dumpMSDC()))
  4396. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_do_request,
  4397. "msdc_dma_start", host->xfer_size);
  4398. #endif
  4399. spin_unlock(&host->lock);
  4400. if (!wait_for_completion_timeout(&host->xfer_done, DAT_TIMEOUT)) {
  4401. ERR_MSG("XXX CMD<%d> ARG<0x%x> wait xfer_done<%d> timeout!!",
  4402. cmd->opcode, cmd->arg, data->blocks * data->blksz);
  4403. host->sw_timeout++;
  4404. #ifdef STO_LOG
  4405. if (unlikely(dumpMSDC()))
  4406. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_do_request,
  4407. "msdc_dma ERR", host->xfer_size);
  4408. #endif
  4409. msdc_dump_info(host->id);
  4410. data->error = (unsigned int)-ETIMEDOUT;
  4411. msdc_reset(host->id);
  4412. }
  4413. spin_lock(&host->lock);
  4414. msdc_dma_stop(host);
  4415. if ((mrq->data && mrq->data->error)
  4416. || ((host->autocmd & MSDC_AUTOCMD12)
  4417. && mrq->stop && mrq->stop->error)
  4418. || (mrq->sbc && (mrq->sbc->error != 0)
  4419. && (host->autocmd & MSDC_AUTOCMD23))) {
  4420. msdc_clr_fifo(host->id);
  4421. msdc_clr_int();
  4422. }
  4423. #ifdef STO_LOG
  4424. if (unlikely(dumpMSDC()))
  4425. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_do_request,
  4426. "msdc_dma_stop");
  4427. #endif
  4428. } else {
  4429. /* Turn off dma */
  4430. if (is_card_sdio(host)) {
  4431. msdc_reset_hw(host->id);
  4432. msdc_dma_off();
  4433. data->error = 0;
  4434. }
  4435. /* Firstly: send command
  4436. * need ask the designer, how about autocmd12
  4437. * or autocmd23 with pio mode
  4438. */
  4439. host->autocmd &= ~MSDC_AUTOCMD12;
  4440. l_autocmd23_is_set = 0;
  4441. if (host->autocmd & MSDC_AUTOCMD23) {
  4442. l_autocmd23_is_set = 1;
  4443. host->autocmd &= ~MSDC_AUTOCMD23;
  4444. }
  4445. host->dma_xfer = 0;
  4446. if (msdc_do_command(host, cmd, 0, CMD_TIMEOUT))
  4447. goto stop;
  4448. /* Secondly: pio data phase */
  4449. if (read) {
  4450. #ifdef MTK_MSDC_DUMP_FIFO
  4451. pr_debug("[%s]: start pio read\n", __func__);
  4452. #endif
  4453. if (msdc_pio_read(host, data)) {
  4454. msdc_gate_clock(host, 0);
  4455. msdc_ungate_clock(host);
  4456. goto stop; /* need cmd12 */
  4457. }
  4458. } else {
  4459. #ifdef MTK_MSDC_DUMP_FIFO
  4460. pr_debug("[%s]: start pio write\n", __func__);
  4461. #endif
  4462. if (msdc_pio_write(host, data)) {
  4463. msdc_gate_clock(host, 0);
  4464. msdc_ungate_clock(host);
  4465. goto stop;
  4466. }
  4467. }
  4468. /* For write case: make sure contents in fifo flushed to device */
  4469. if (!read) {
  4470. pio_tmo = jiffies + DAT_TIMEOUT;
  4471. while (1) {
  4472. left = msdc_txfifocnt();
  4473. if (left == 0)
  4474. break;
  4475. if (msdc_pio_abort(host, data, pio_tmo))
  4476. break;
  4477. }
  4478. }
  4479. } /* PIO mode */
  4480. stop:
  4481. /* pio mode will disable autocmd23 */
  4482. if (l_autocmd23_is_set == 1) {
  4483. l_autocmd23_is_set = 0;
  4484. host->autocmd |= MSDC_AUTOCMD23;
  4485. }
  4486. #ifndef MTK_MSDC_USE_CMD23
  4487. /* Last: stop transfer */
  4488. if (data && data->stop) {
  4489. if (!((cmd->error == 0) && (data->error == 0)
  4490. && (host->autocmd & MSDC_AUTOCMD12)
  4491. && (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  4492. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK))) {
  4493. if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT))
  4494. goto done;
  4495. }
  4496. }
  4497. #else
  4498. if (host->hw->host_function == MSDC_EMMC) {
  4499. if (data && data->stop) {
  4500. /* multi r/w with no cmd23 and no autocmd12,need send cmd12
  4501. * manual if PIO mode and autocmd23 enable, cmd12 need send,
  4502. * because autocmd23 is disable under PIO
  4503. */
  4504. if ((((mrq->sbc == NULL) && !(host->autocmd & MSDC_AUTOCMD12))
  4505. || (!dma && mrq->sbc && (host->autocmd & MSDC_AUTOCMD23)))
  4506. && (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  4507. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)) {
  4508. if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT))
  4509. goto done;
  4510. }
  4511. }
  4512. } else {
  4513. /* for non emmc card, use old flow */
  4514. if (data && data->stop) {
  4515. if (!((cmd->error == 0) && (data->error == 0)
  4516. && (host->autocmd & MSDC_AUTOCMD12)
  4517. && (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  4518. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK))) {
  4519. if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT))
  4520. goto done;
  4521. }
  4522. }
  4523. }
  4524. #endif
  4525. }
  4526. done:
  4527. #ifdef MTK_MSDC_USE_CMD23
  4528. /* for msdc use cmd23, but card not supported(sbc is NULL),
  4529. * need enable autocmd23 for next request
  4530. */
  4531. if (1 == l_card_no_cmd23) {
  4532. if (host->hw->host_function != MSDC_SDIO) {
  4533. host->autocmd |= MSDC_AUTOCMD23;
  4534. host->autocmd &= ~MSDC_AUTOCMD12;
  4535. l_card_no_cmd23 = 0;
  4536. }
  4537. }
  4538. #endif
  4539. if (data != NULL) {
  4540. host->data = NULL;
  4541. host->dma_xfer = 0;
  4542. if (dma != 0) {
  4543. msdc_dma_off();
  4544. host->dma.used_bd = 0;
  4545. host->dma.used_gpd = 0;
  4546. if (map_sg == 1) {
  4547. /*if(data->error == 0){
  4548. int retry = 3;
  4549. int count = 1000;
  4550. msdc_retry(host->dma.gpd->hwo,retry,count,host->id);
  4551. } */
  4552. dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
  4553. }
  4554. }
  4555. if ((cmd->opcode == MMC_SEND_EXT_CSD) &&
  4556. (host->hw->host_function == MSDC_EMMC))
  4557. msdc_get_ext_csd(data, host);
  4558. host->blksz = 0;
  4559. N_MSG(OPS, "CMD<%d> data<%s %s> blksz<%d> block<%d> error<%d>",
  4560. cmd->opcode, (dma ? "dma" : "pio"), (read ? "read " : "write"),
  4561. data->blksz, data->blocks, data->error);
  4562. if (!(is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))) {
  4563. if ((cmd->opcode != 17) && (cmd->opcode != 18)
  4564. && (cmd->opcode != 24) && (cmd->opcode != 25)) {
  4565. N_MSG(NRW, "CMD<%3d> arg<0x%8x> Resp<0x%8x> data<%s> size<%d>",
  4566. cmd->opcode, cmd->arg, cmd->resp[0],
  4567. (read ? "read " : "write"), data->blksz * data->blocks);
  4568. } else {
  4569. N_MSG(RW, "CMD<%3d> arg<0x%8x> Resp<0x%8x> block<%d>",
  4570. cmd->opcode, cmd->arg, cmd->resp[0], data->blocks);
  4571. }
  4572. }
  4573. } else {
  4574. if (!(is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))) {
  4575. if (cmd->opcode != 13) /* by pass CMD13 */
  4576. N_MSG(NRW, "CMD<%3d> arg<0x%8x> resp<%8x %8x %8x %8x>",
  4577. cmd->opcode, cmd->arg, cmd->resp[0], cmd->resp[1],
  4578. cmd->resp[2], cmd->resp[3]);
  4579. }
  4580. }
  4581. if (mrq->cmd->error == (unsigned int)-EIO) {
  4582. if (((cmd->opcode == MMC_SELECT_CARD)
  4583. || (cmd->opcode == MMC_SLEEP_AWAKE))
  4584. && ((host->hw->host_function == MSDC_EMMC)
  4585. || (host->hw->host_function == MSDC_SD))) {
  4586. /* should be deleted in new platform,
  4587. * as the state verify function has applied
  4588. */
  4589. mrq->cmd->error = 0x0;
  4590. } else {
  4591. host->error |= REQ_CMD_EIO;
  4592. sdio_tune_flag |= 0x1;
  4593. if (mrq->cmd->opcode == SD_IO_RW_EXTENDED)
  4594. sdio_tune_flag |= 0x1;
  4595. }
  4596. }
  4597. if (mrq->cmd->error == (unsigned int)-ETIMEDOUT) {
  4598. if ((mrq->cmd->opcode == MMC_SLEEP_AWAKE) && emmc_do_sleep_awake) {
  4599. emmc_sleep_failed = 1;
  4600. if (mrq->cmd->arg & (1 << 15)) {
  4601. mrq->cmd->error = 0x0;
  4602. pr_err("eMMC sleep CMD5 TMO will reinit...\n");
  4603. } else {
  4604. host->error |= REQ_CMD_TMO;
  4605. }
  4606. } else {
  4607. host->error |= REQ_CMD_TMO;
  4608. }
  4609. }
  4610. if (mrq->data && mrq->data->error) {
  4611. host->error |= REQ_DAT_ERR;
  4612. sdio_tune_flag |= 0x10;
  4613. if (mrq->data->flags & MMC_DATA_READ)
  4614. sdio_tune_flag |= 0x80;
  4615. else
  4616. sdio_tune_flag |= 0x40;
  4617. }
  4618. #ifdef MTK_MSDC_USE_CMD23
  4619. if (mrq->sbc && (mrq->sbc->error == (unsigned int)-EIO))
  4620. host->error |= REQ_CMD_EIO;
  4621. if (mrq->sbc && (mrq->sbc->error == (unsigned int)-ETIMEDOUT)) {
  4622. #ifdef CONFIG_MTK_AEE_FEATURE
  4623. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_NE_JBT_TRACES
  4624. | DB_OPT_DISPLAY_HANG_DUMP, "\n@eMMC FATAL ERROR@\n",
  4625. "eMMC fatal error");
  4626. #endif
  4627. host->error |= REQ_CMD_TMO;
  4628. }
  4629. #endif
  4630. if (mrq->stop && (mrq->stop->error == (unsigned int)-EIO))
  4631. host->error |= REQ_STOP_EIO;
  4632. if (mrq->stop && (mrq->stop->error == (unsigned int)-ETIMEDOUT))
  4633. host->error |= REQ_STOP_TMO;
  4634. /* if (host->error) ERR_MSG("host->error<%d>", host->error); */
  4635. #ifdef SDIO_ERROR_BYPASS
  4636. if (is_card_sdio(host) && !host->error)
  4637. host->sdio_error = 0;
  4638. #endif
  4639. msdc_update_cahce_status(host, mrq);
  4640. return host->error;
  4641. }
  4642. static int msdc_tune_rw_request(struct mmc_host *mmc, struct mmc_request *mrq)
  4643. {
  4644. struct msdc_host *host = mmc_priv(mmc);
  4645. struct mmc_command *cmd;
  4646. struct mmc_data *data;
  4647. #ifdef MTK_MSDC_USE_CMD23
  4648. u32 l_autocmd23_is_set = 0;
  4649. #endif
  4650. void __iomem *base = host->base;
  4651. /* u32 intsts = 0; */
  4652. /* unsigned int left=0; */
  4653. int read = 1, dma = 1; /* dir = DMA_FROM_DEVICE, send_type=0, */
  4654. #define SND_DAT 0
  4655. #define SND_CMD 1
  4656. BUG_ON(mmc == NULL);
  4657. BUG_ON(mrq == NULL);
  4658. /* host->error = 0; */
  4659. atomic_set(&host->abort, 0);
  4660. cmd = mrq->cmd;
  4661. data = mrq->cmd->data;
  4662. /* check msdc is work ok.
  4663. * rule is RX/TX fifocnt must be zero after last request
  4664. * if find abnormal, try to reset msdc first
  4665. */
  4666. if (msdc_txfifocnt() || msdc_rxfifocnt()) {
  4667. pr_err("[SD%d] register abnormal,please check!\n", host->id);
  4668. msdc_reset_hw(host->id);
  4669. }
  4670. BUG_ON(data->blksz > HOST_MAX_BLKSZ);
  4671. /* send_type=SND_DAT; */
  4672. data->error = 0;
  4673. read = data->flags & MMC_DATA_READ ? 1 : 0;
  4674. msdc_latest_operation_type[host->id] = read ?
  4675. OPER_TYPE_READ : OPER_TYPE_WRITE;
  4676. host->data = data;
  4677. host->xfer_size = data->blocks * data->blksz;
  4678. host->blksz = data->blksz;
  4679. host->dma_xfer = 1;
  4680. /* deside the transfer mode */
  4681. /*
  4682. if (drv_mode[host->id] == MODE_PIO) {
  4683. host->dma_xfer = dma = 0;
  4684. msdc_latest_transfer_mode[host->id] = TRAN_MOD_PIO;
  4685. } else if (drv_mode[host->id] == MODE_DMA) {
  4686. host->dma_xfer = dma = 1;
  4687. msdc_latest_transfer_mode[host->id] = TRAN_MOD_DMA;
  4688. } else if (drv_mode[host->id] == MODE_SIZE_DEP) {
  4689. host->dma_xfer = dma = ((host->xfer_size >= dma_size[host->id]) ? 1 : 0);
  4690. msdc_latest_transfer_mode[host->id] = dma ? TRAN_MOD_DMA: TRAN_MOD_PIO;
  4691. }
  4692. */
  4693. if (read) {
  4694. if ((host->timeout_ns != data->timeout_ns)
  4695. || (host->timeout_clks != data->timeout_clks)) {
  4696. msdc_set_timeout(host, data->timeout_ns, data->timeout_clks);
  4697. }
  4698. }
  4699. msdc_set_blknum(host, data->blocks);
  4700. /* msdc_clr_fifo(); */ /* no need */
  4701. msdc_dma_on(); /* enable DMA mode first!! */
  4702. init_completion(&host->xfer_done);
  4703. /* start the command first */
  4704. #ifndef MTK_MSDC_USE_CMD23
  4705. if (host->hw->host_function != MSDC_SDIO)
  4706. host->autocmd |= MSDC_AUTOCMD12;
  4707. #else
  4708. if (host->hw->host_function != MSDC_SDIO) {
  4709. host->autocmd |= MSDC_AUTOCMD12;
  4710. /* disable autocmd23 in error tuning flow */
  4711. l_autocmd23_is_set = 0;
  4712. if (host->autocmd & MSDC_AUTOCMD23) {
  4713. l_autocmd23_is_set = 1;
  4714. host->autocmd &= ~MSDC_AUTOCMD23;
  4715. }
  4716. }
  4717. #endif
  4718. if (msdc_command_start(host, cmd, 0, CMD_TIMEOUT))
  4719. goto done;
  4720. /* then wait command done */
  4721. if (msdc_command_resp_polling(host, cmd, 0, CMD_TIMEOUT))
  4722. goto stop;
  4723. /* for read, the data coming too fast, then CRC error
  4724. * start DMA no business with CRC. */
  4725. msdc_dma_setup(host, &host->dma, data->sg, data->sg_len);
  4726. msdc_dma_start(host);
  4727. /* ERR_MSG("1.Power cycle enable(%d)",host->power_cycle_enable); */
  4728. #ifdef STO_LOG
  4729. if (unlikely(dumpMSDC()))
  4730. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_tune_rw_request,
  4731. "msdc_dma_start", host->xfer_size);
  4732. #endif
  4733. spin_unlock(&host->lock);
  4734. if (!wait_for_completion_timeout(&host->xfer_done, DAT_TIMEOUT)) {
  4735. ERR_MSG("XXX CMD<%d> ARG<0x%x> wait xfer_done<%d> timeout!!",
  4736. cmd->opcode, cmd->arg, data->blocks * data->blksz);
  4737. host->sw_timeout++;
  4738. #ifdef STO_LOG
  4739. if (unlikely(dumpMSDC()))
  4740. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_tune_rw_request,
  4741. "msdc_dma ERR", host->xfer_size);
  4742. #endif
  4743. msdc_dump_info(host->id);
  4744. data->error = (unsigned int)-ETIMEDOUT;
  4745. msdc_reset(host->id);
  4746. }
  4747. spin_lock(&host->lock);
  4748. /* ERR_MSG("2.Power cycle enable(%d)",host->power_cycle_enable); */
  4749. msdc_dma_stop(host);
  4750. if ((mrq->data && mrq->data->error)
  4751. || (host->autocmd & MSDC_AUTOCMD12 && mrq->stop && mrq->stop->error)
  4752. || (mrq->sbc && (mrq->sbc->error != 0)
  4753. && (host->autocmd & MSDC_AUTOCMD23))) {
  4754. msdc_clr_fifo(host->id);
  4755. msdc_clr_int();
  4756. }
  4757. #ifdef STO_LOG
  4758. if (unlikely(dumpMSDC()))
  4759. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_tune_rw_request,
  4760. "msdc_dma_stop");
  4761. #endif
  4762. stop:
  4763. /* Last: stop transfer */
  4764. if (data->stop) {
  4765. if (!((cmd->error == 0) && (data->error == 0)
  4766. && (host->autocmd == MSDC_AUTOCMD12)
  4767. && (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  4768. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK))) {
  4769. if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT) != 0)
  4770. goto done;
  4771. }
  4772. }
  4773. done:
  4774. host->data = NULL;
  4775. host->dma_xfer = 0;
  4776. msdc_dma_off();
  4777. host->dma.used_bd = 0;
  4778. host->dma.used_gpd = 0;
  4779. host->blksz = 0;
  4780. N_MSG(OPS, "CMD<%d> data<%s %s> blksz<%d> block<%d> error<%d>", cmd->opcode,
  4781. (dma ? "dma" : "pio"), (read ? "read " : "write"), data->blksz,
  4782. data->blocks, data->error);
  4783. if (!(is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))) {
  4784. if ((cmd->opcode != 17) && (cmd->opcode != 18) && (cmd->opcode != 24)
  4785. && (cmd->opcode != 25))
  4786. N_MSG(NRW, "CMD<%3d> arg<0x%8x> Resp<0x%8x> data<%s> size<%d>",
  4787. cmd->opcode, cmd->arg, cmd->resp[0], (read ? "read " : "write"),
  4788. data->blksz * data->blocks);
  4789. else
  4790. N_MSG(RW, "CMD<%3d> arg<0x%8x> Resp<0x%8x> block<%d>", cmd->opcode,
  4791. cmd->arg, cmd->resp[0], data->blocks);
  4792. } else {
  4793. if (!(is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))) {
  4794. if (cmd->opcode != 13) /* by pass CMD13 */
  4795. N_MSG(NRW, "CMD<%3d> arg<0x%8x> resp<%8x %8x %8x %8x>",
  4796. cmd->opcode, cmd->arg, cmd->resp[0], cmd->resp[1],
  4797. cmd->resp[2], cmd->resp[3]);
  4798. }
  4799. }
  4800. host->error = 0;
  4801. if (mrq->cmd->error == (unsigned int)-EIO) {
  4802. if (((cmd->opcode == MMC_SELECT_CARD)
  4803. || (cmd->opcode == MMC_SLEEP_AWAKE))
  4804. && ((host->hw->host_function == MSDC_EMMC)
  4805. || (host->hw->host_function == MSDC_SD))) {
  4806. /* should be deleted in new platform,
  4807. * as the state verify function has applied.
  4808. */
  4809. mrq->cmd->error = 0x0;
  4810. } else {
  4811. host->error |= REQ_CMD_EIO;
  4812. }
  4813. }
  4814. if (mrq->cmd->error == (unsigned int)-ETIMEDOUT)
  4815. host->error |= REQ_CMD_TMO;
  4816. if (mrq->data && (mrq->data->error))
  4817. host->error |= REQ_DAT_ERR;
  4818. if (mrq->stop && (mrq->stop->error == (unsigned int)-EIO))
  4819. host->error |= REQ_STOP_EIO;
  4820. if (mrq->stop && (mrq->stop->error == (unsigned int)-ETIMEDOUT))
  4821. host->error |= REQ_STOP_TMO;
  4822. #ifdef MTK_MSDC_USE_CMD23
  4823. if (l_autocmd23_is_set == 1) {
  4824. /* restore the value */
  4825. host->autocmd |= MSDC_AUTOCMD23;
  4826. }
  4827. #endif
  4828. return host->error;
  4829. }
  4830. static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
  4831. bool is_first_req)
  4832. {
  4833. struct msdc_host *host = mmc_priv(mmc);
  4834. struct mmc_data *data;
  4835. struct mmc_command *cmd = mrq->cmd;
  4836. int read = 1, dir = DMA_FROM_DEVICE;
  4837. BUG_ON(!cmd);
  4838. data = mrq->data;
  4839. if (data)
  4840. data->host_cookie = MSDC_COOKIE_ASYNC;
  4841. if (data && (cmd->opcode == MMC_READ_SINGLE_BLOCK
  4842. || cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  4843. || cmd->opcode == MMC_WRITE_BLOCK
  4844. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)) {
  4845. host->xfer_size = data->blocks * data->blksz;
  4846. read = data->flags & MMC_DATA_READ ? 1 : 0;
  4847. if (drv_mode[host->id] == MODE_PIO) {
  4848. data->host_cookie |= MSDC_COOKIE_PIO;
  4849. msdc_latest_transfer_mode[host->id] = TRAN_MOD_PIO;
  4850. } else if (drv_mode[host->id] == MODE_DMA) {
  4851. msdc_latest_transfer_mode[host->id] = TRAN_MOD_DMA;
  4852. } else if (drv_mode[host->id] == MODE_SIZE_DEP) {
  4853. if (host->xfer_size < dma_size[host->id]) {
  4854. data->host_cookie |= MSDC_COOKIE_PIO;
  4855. msdc_latest_transfer_mode[host->id] = TRAN_MOD_PIO;
  4856. } else {
  4857. msdc_latest_transfer_mode[host->id] = TRAN_MOD_DMA;
  4858. }
  4859. }
  4860. if (msdc_async_use_dma(data->host_cookie)) {
  4861. dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  4862. (void)dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
  4863. }
  4864. N_MSG(OPS, "CMD<%d> ARG<0x%x>data<%s %s> blksz<%d> block<%d> error<%d>",
  4865. mrq->cmd->opcode, mrq->cmd->arg,
  4866. (data->host_cookie ? "dma" : "pio"), (read ? "read " : "write"),
  4867. data->blksz, data->blocks, data->error);
  4868. }
  4869. }
  4870. static void msdc_dma_clear(struct msdc_host *host)
  4871. {
  4872. void __iomem *base = host->base;
  4873. host->data = NULL;
  4874. host->mrq = NULL;
  4875. host->dma_xfer = 0;
  4876. msdc_dma_off();
  4877. host->dma.used_bd = 0;
  4878. host->dma.used_gpd = 0;
  4879. host->blksz = 0;
  4880. }
  4881. static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  4882. int err)
  4883. {
  4884. struct msdc_host *host = mmc_priv(mmc);
  4885. struct mmc_data *data;
  4886. /* struct mmc_command *cmd = mrq->cmd; */
  4887. int read = 1, dir = DMA_FROM_DEVICE;
  4888. data = mrq->data;
  4889. if (data && (msdc_async_use_dma(data->host_cookie))) {
  4890. host->xfer_size = data->blocks * data->blksz;
  4891. read = data->flags & MMC_DATA_READ ? 1 : 0;
  4892. dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  4893. dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
  4894. data->host_cookie = 0;
  4895. N_MSG(OPS, "CMD<%d> ARG<0x%x> blksz<%d> block<%d> error<%d>",
  4896. mrq->cmd->opcode, mrq->cmd->arg, data->blksz, data->blocks,
  4897. data->error);
  4898. }
  4899. data->host_cookie = 0;
  4900. }
  4901. static int msdc_do_request_async(struct mmc_host *mmc, struct mmc_request *mrq)
  4902. {
  4903. struct msdc_host *host = mmc_priv(mmc);
  4904. struct mmc_command *cmd;
  4905. struct mmc_data *data;
  4906. void __iomem *base = host->base;
  4907. #ifdef MTK_MSDC_USE_CMD23
  4908. u32 l_card_no_cmd23 = 0;
  4909. #endif
  4910. #ifdef MTK_MSDC_USE_CACHE
  4911. u32 l_force_prg = 0;
  4912. #endif
  4913. /* u32 intsts = 0; */
  4914. /* unsigned int left=0; */
  4915. int dma = 0, read = 1; /* , dir = DMA_FROM_DEVICE; */
  4916. BUG_ON(mmc == NULL);
  4917. BUG_ON(mrq == NULL);
  4918. if (!is_card_present(host) || host->power_mode == MMC_POWER_OFF) {
  4919. ERR_MSG("cmd<%d> arg<0x%x> card<%d> power<%d>",
  4920. mrq->cmd->opcode, mrq->cmd->arg,
  4921. is_card_present(host), host->power_mode);
  4922. mrq->cmd->error = (unsigned int)-ENOMEDIUM;
  4923. if (mrq->done)
  4924. mrq->done(mrq); /* call done directly. */
  4925. return 0;
  4926. }
  4927. msdc_ungate_clock(host);
  4928. host->tune = 0;
  4929. host->error = 0;
  4930. atomic_set(&host->abort, 0);
  4931. spin_lock(&host->lock);
  4932. cmd = mrq->cmd;
  4933. data = mrq->cmd->data;
  4934. host->mrq = mrq;
  4935. /* check msdc is work ok.
  4936. * rule is RX/TX fifocnt must be zero after last request
  4937. * if find abnormal, try to reset msdc first
  4938. */
  4939. if (msdc_txfifocnt() || msdc_rxfifocnt()) {
  4940. pr_err("[SD%d] register abnormal,please check!\n", host->id);
  4941. msdc_reset_hw(host->id);
  4942. }
  4943. BUG_ON(data->blksz > HOST_MAX_BLKSZ);
  4944. /* send_type=SND_DAT; */
  4945. #ifdef MTK_MSDC_USE_CACHE
  4946. if ((host->hw->host_function == MSDC_EMMC)
  4947. && host->mmc->card && (host->mmc->card->ext_csd.cache_ctrl & 0x1)
  4948. && (cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK))
  4949. l_force_prg = !msdc_can_apply_cache(cmd->arg, data->blocks);
  4950. #endif
  4951. data->error = 0;
  4952. read = data->flags & MMC_DATA_READ ? 1 : 0;
  4953. msdc_latest_operation_type[host->id] = read ?
  4954. OPER_TYPE_READ : OPER_TYPE_WRITE;
  4955. host->data = data;
  4956. host->xfer_size = data->blocks * data->blksz;
  4957. host->blksz = data->blksz;
  4958. host->dma_xfer = 1;
  4959. /* deside the transfer mode */
  4960. if ((read) && ((host->timeout_ns != data->timeout_ns)
  4961. || (host->timeout_clks != data->timeout_clks)))
  4962. msdc_set_timeout(host, data->timeout_ns, data->timeout_clks);
  4963. msdc_set_blknum(host, data->blocks);
  4964. msdc_dma_on(); /* enable DMA mode first!! */
  4965. /* init_completion(&host->xfer_done); */
  4966. #ifdef MTK_MSDC_USE_CMD23
  4967. /* if tuning flow run here, no problem?? need check!!!!!!! */
  4968. if (0 == (host->autocmd & MSDC_AUTOCMD23)) {
  4969. /* start the cmd23 first */
  4970. if (mrq->sbc) {
  4971. host->autocmd &= ~MSDC_AUTOCMD12;
  4972. if (host->hw->host_function == MSDC_EMMC) {
  4973. #ifdef MTK_MSDC_USE_CACHE
  4974. if (l_force_prg && !((mrq->sbc->arg >> 31) & 0x1))
  4975. mrq->sbc->arg |= (1 << 24);
  4976. #endif
  4977. }
  4978. if (msdc_command_start(host, mrq->sbc, 0, CMD_TIMEOUT) != 0)
  4979. goto done;
  4980. /* then wait command done */
  4981. if (msdc_command_resp_polling(host, mrq->sbc, 0, CMD_TIMEOUT) != 0)
  4982. goto stop;
  4983. } else {
  4984. /* some sd card may not support cmd23,
  4985. * some emmc card have problem with cmd23, so use cmd12 here */
  4986. if (host->hw->host_function != MSDC_SDIO)
  4987. host->autocmd |= MSDC_AUTOCMD12;
  4988. }
  4989. } else {
  4990. if (mrq->sbc) {
  4991. host->autocmd &= ~MSDC_AUTOCMD12;
  4992. if (host->hw->host_function == MSDC_EMMC) {
  4993. #ifdef MTK_MSDC_USE_CACHE
  4994. if (l_force_prg && !((mrq->sbc->arg >> 31) & 0x1))
  4995. mrq->sbc->arg |= (1 << 24);
  4996. #endif
  4997. }
  4998. } else {
  4999. /* some sd card may not support cmd23,
  5000. * some emmc card have problem with cmd23, so use cmd12 here */
  5001. if (host->hw->host_function != MSDC_SDIO) {
  5002. host->autocmd &= ~MSDC_AUTOCMD23;
  5003. host->autocmd |= MSDC_AUTOCMD12;
  5004. l_card_no_cmd23 = 1;
  5005. }
  5006. }
  5007. }
  5008. #else
  5009. /* start the command first */
  5010. if (host->hw->host_function != MSDC_SDIO)
  5011. host->autocmd |= MSDC_AUTOCMD12;
  5012. #endif /* end of MTK_MSDC_USE_CMD23 */
  5013. if (msdc_command_start(host, cmd, 0, CMD_TIMEOUT) != 0)
  5014. goto done;
  5015. /* then wait command done */
  5016. if (msdc_command_resp_polling(host, cmd, 0, CMD_TIMEOUT) != 0)
  5017. goto stop;
  5018. /* for read, the data coming too fast, then CRC error
  5019. start DMA no business with CRC. */
  5020. /* init_completion(&host->xfer_done); */
  5021. msdc_dma_setup(host, &host->dma, data->sg, data->sg_len);
  5022. msdc_dma_start(host);
  5023. spin_unlock(&host->lock);
  5024. #if defined(FEATURE_MET_MMC_INDEX)
  5025. met_mmc_issue(host->mmc, host->mrq);
  5026. #endif
  5027. #ifdef MTK_MSDC_USE_CMD23
  5028. /* for msdc use cmd23, but card not supported(sbc is NULL),
  5029. * need enable autocmd23 for next request.
  5030. */
  5031. if (1 == l_card_no_cmd23) {
  5032. if (host->hw->host_function != MSDC_SDIO) {
  5033. host->autocmd |= MSDC_AUTOCMD23;
  5034. host->autocmd &= ~MSDC_AUTOCMD12;
  5035. l_card_no_cmd23 = 0;
  5036. }
  5037. }
  5038. #endif
  5039. msdc_update_cahce_status(host, mrq);
  5040. return 0;
  5041. stop:
  5042. #ifndef MTK_MSDC_USE_CMD23
  5043. /* Last: stop transfer */
  5044. if (data && data->stop) {
  5045. if (!((cmd->error == 0) && (data->error == 0)
  5046. && (host->autocmd & MSDC_AUTOCMD12)
  5047. && (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  5048. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK))) {
  5049. if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT) != 0)
  5050. goto done;
  5051. }
  5052. }
  5053. #else
  5054. if (host->hw->host_function == MSDC_EMMC) {
  5055. /* error handle will do msdc_abort_data() */
  5056. } else {
  5057. if (data && data->stop) {
  5058. if (!((cmd->error == 0) && (data->error == 0)
  5059. && (host->autocmd & MSDC_AUTOCMD12)
  5060. && (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  5061. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK))) {
  5062. if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT) != 0)
  5063. goto done;
  5064. }
  5065. }
  5066. }
  5067. #endif
  5068. done:
  5069. #ifdef MTK_MSDC_USE_CMD23
  5070. /* for msdc use cmd23, but card not supported(sbc is NULL),
  5071. * need enable autocmd23 for next request
  5072. */
  5073. if (1 == l_card_no_cmd23) {
  5074. if (host->hw->host_function != MSDC_SDIO) {
  5075. host->autocmd |= MSDC_AUTOCMD23;
  5076. host->autocmd &= ~MSDC_AUTOCMD12;
  5077. l_card_no_cmd23 = 0;
  5078. }
  5079. }
  5080. #endif
  5081. msdc_dma_clear(host);
  5082. N_MSG(OPS, "CMD<%d> data<%s %s> blksz<%d> block<%d> error<%d>", cmd->opcode,
  5083. (dma ? "dma" : "pio"), (read ? "read " : "write"), data->blksz,
  5084. data->blocks, data->error);
  5085. if (!(is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))) {
  5086. if ((cmd->opcode != 17) && (cmd->opcode != 18) && (cmd->opcode != 24)
  5087. && (cmd->opcode != 25)) {
  5088. N_MSG(NRW, "CMD<%3d> arg<0x%8x> Resp<0x%8x> data<%s> size<%d>",
  5089. cmd->opcode, cmd->arg, cmd->resp[0], (read ? "read " : "write"),
  5090. data->blksz * data->blocks);
  5091. } else {
  5092. N_MSG(RW, "CMD<%3d> arg<0x%8x> Resp<0x%8x> block<%d>", cmd->opcode,
  5093. cmd->arg, cmd->resp[0], data->blocks);
  5094. }
  5095. }
  5096. #ifdef MTK_MSDC_USE_CMD23
  5097. if (mrq->sbc && (mrq->sbc->error == (unsigned int)-EIO))
  5098. host->error |= REQ_CMD_EIO;
  5099. if (mrq->sbc && (mrq->sbc->error == (unsigned int)-ETIMEDOUT)) {
  5100. #ifdef CONFIG_MTK_AEE_FEATURE
  5101. aee_kernel_warning_api(__FILE__, __LINE__, DB_OPT_NE_JBT_TRACES
  5102. | DB_OPT_DISPLAY_HANG_DUMP, "\n@eMMC FATAL ERROR@\n",
  5103. "eMMC fatal error ");
  5104. #endif
  5105. host->error |= REQ_CMD_TMO;
  5106. }
  5107. #endif
  5108. if (mrq->cmd->error == (unsigned int)-EIO) {
  5109. if (((cmd->opcode == MMC_SELECT_CARD)
  5110. || (cmd->opcode == MMC_SLEEP_AWAKE)) &&
  5111. ((host->hw->host_function == MSDC_EMMC)
  5112. || (host->hw->host_function == MSDC_SD))) {
  5113. /* should be deleted in new platform,
  5114. * as the state verify function has applied
  5115. */
  5116. mrq->cmd->error = 0x0;
  5117. } else {
  5118. host->error |= REQ_CMD_EIO;
  5119. }
  5120. }
  5121. if (mrq->cmd->error == (unsigned int)-ETIMEDOUT)
  5122. host->error |= REQ_CMD_TMO;
  5123. if (mrq->stop && (mrq->stop->error == (unsigned int)-EIO))
  5124. host->error |= REQ_STOP_EIO;
  5125. if (mrq->stop && (mrq->stop->error == (unsigned int)-ETIMEDOUT))
  5126. host->error |= REQ_STOP_TMO;
  5127. msdc_update_cahce_status(host, mrq);
  5128. msdc_gate_clock(host, 1);
  5129. spin_unlock(&host->lock);
  5130. return host->error;
  5131. }
  5132. static int msdc_app_cmd(struct mmc_host *mmc, struct msdc_host *host)
  5133. {
  5134. struct mmc_command cmd = { 0 };
  5135. struct mmc_request mrq = { 0 };
  5136. u32 err = -1;
  5137. cmd.opcode = MMC_APP_CMD;
  5138. cmd.arg = host->app_cmd_arg; /* meet mmc->card is null when ACMD6 */
  5139. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  5140. mrq.cmd = &cmd;
  5141. cmd.mrq = &mrq;
  5142. cmd.data = NULL;
  5143. err = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);
  5144. return err;
  5145. }
  5146. static int msdc_lower_freq(struct msdc_host *host)
  5147. {
  5148. u32 div, mode, hs400_src;
  5149. void __iomem *base = host->base;
  5150. ERR_MSG("need to lower freq");
  5151. msdc_reset_crc_tune_counter(host, ALL_TUNE_CNT);
  5152. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, mode);
  5153. sdr_get_field(MSDC_CFG, MSDC_CFG_CKDIV, div);
  5154. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD_HS400, hs400_src);
  5155. #ifndef FPGA_PLATFORM
  5156. if (host->id == 0)
  5157. hclks = hclks_msdc50;
  5158. else
  5159. hclks = hclks_msdc30;
  5160. #endif
  5161. if (div >= MSDC_MAX_FREQ_DIV) {
  5162. ERR_MSG("but, div<%d> power tuning", div);
  5163. return msdc_power_tuning(host);
  5164. } else if ((mode == 3) && (host->id == 0)) {
  5165. /* when HS400 low freq, you cannot change to mode 2 (DDR mode),
  5166. * else read data will be latched by clk, but not ds pin
  5167. * when card speed mode is still HS400.
  5168. */
  5169. if (hs400_src == 1) {
  5170. hs400_src = 0;
  5171. /* change from 400Mhz to 800Mhz,
  5172. * because CCKDIV is invalid when 400Mhz clk src
  5173. */
  5174. msdc_clock_src[host->id] = MSDC50_CLKSRC_800MHZ;
  5175. host->hw->clk_src = msdc_clock_src[host->id];
  5176. msdc_select_clksrc(host, host->hw->clk_src);
  5177. }
  5178. msdc_clk_stable(host, mode, div + 1, hs400_src);
  5179. host->sclk = hclks[host->hw->clk_src] / (2 * 4 * (div + 1));
  5180. ERR_MSG("new div<%d>, mode<%d> new freq.<%dKHz>",
  5181. div + 1, mode, host->sclk / 1000);
  5182. } else if (mode == 1) {
  5183. mode = 0;
  5184. msdc_clk_stable(host, mode, div + 1, hs400_src);
  5185. host->sclk = (div == 0) ? hclks[host->hw->clk_src] / 2 :
  5186. hclks[host->hw->clk_src] / (4 * div);
  5187. ERR_MSG("new div<%d>, mode<%d> new freq.<%dKHz>",
  5188. div, mode, host->sclk / 1000);
  5189. } else {
  5190. msdc_clk_stable(host, mode, div + 1, hs400_src);
  5191. host->sclk = (mode == 2) ? hclks[host->hw->clk_src] /
  5192. (2 * 4 * (div + 1)) : hclks[host->hw->clk_src] / (4 * (div + 1));
  5193. ERR_MSG("new div<%d>, mode<%d> new freq.<%dKHz>",
  5194. div + 1, mode, host->sclk / 1000);
  5195. }
  5196. return 0;
  5197. }
  5198. int msdc_tune_cmdrsp(struct msdc_host *host)
  5199. {
  5200. int result = 0;
  5201. void __iomem *base = host->base;
  5202. u32 sel = 0;
  5203. u32 cur_rsmpl = 0, orig_rsmpl;
  5204. u32 cur_rrdly = 0, orig_rrdly;
  5205. u32 cur_cntr = 0, orig_cmdrtc;
  5206. u32 cur_dl_cksel = 0, orig_dl_cksel;
  5207. u32 clkmode;
  5208. int hs400 = 0;
  5209. sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, orig_rsmpl);
  5210. sdr_get_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, orig_rrdly);
  5211. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR, orig_cmdrtc);
  5212. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  5213. orig_dl_cksel);
  5214. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, clkmode);
  5215. hs400 = (clkmode == 3) ? 1 : 0;
  5216. #ifdef STO_LOG
  5217. if (unlikely(dumpMSDC())) {
  5218. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_do_request,
  5219. "sd_tune_ori RSPL", orig_rsmpl);
  5220. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_do_request,
  5221. "sd_tune_ori RRDLY", orig_rrdly);
  5222. }
  5223. #endif
  5224. #if 1
  5225. if (host->mclk >= 100000000) {
  5226. sel = 1;
  5227. /* sdr_set_field(MSDC_PATCH_BIT0, MSDC_CKGEN_RX_SDCLKO_SEL,0); */
  5228. } else {
  5229. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR, 1);
  5230. /* sdr_set_field(MSDC_PATCH_BIT0, MSDC_CKGEN_RX_SDCLKO_SEL,1); */
  5231. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL, 0);
  5232. }
  5233. cur_rsmpl = (orig_rsmpl + 1);
  5234. msdc_set_smpl(host, hs400, cur_rsmpl % 2, TYPE_CMD_RESP_EDGE, NULL);
  5235. if (host->mclk <= 400000) {
  5236. msdc_set_smpl(host, hs400, 0, TYPE_CMD_RESP_EDGE, NULL);
  5237. cur_rsmpl = 2;
  5238. }
  5239. if (cur_rsmpl >= 2) {
  5240. cur_rrdly = (orig_rrdly + 1);
  5241. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, cur_rrdly % 32);
  5242. }
  5243. if (cur_rrdly >= 32) {
  5244. if (sel) {
  5245. cur_cntr = (orig_cmdrtc + 1);
  5246. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  5247. cur_cntr % 8);
  5248. }
  5249. }
  5250. if (cur_cntr >= 8) {
  5251. if (sel) {
  5252. cur_dl_cksel = (orig_dl_cksel + 1);
  5253. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  5254. cur_dl_cksel % 8);
  5255. }
  5256. }
  5257. ++(host->t_counter.time_cmd);
  5258. if ((sel && host->t_counter.time_cmd == CMD_TUNE_UHS_MAX_TIME)
  5259. || (sel == 0 && host->t_counter.time_cmd == CMD_TUNE_HS_MAX_TIME)) {
  5260. #ifdef MSDC_LOWER_FREQ
  5261. result = msdc_lower_freq(host);
  5262. #else
  5263. result = 1;
  5264. #endif
  5265. host->t_counter.time_cmd = 0;
  5266. }
  5267. #else
  5268. if (orig_rsmpl == 0) {
  5269. cur_rsmpl = 1;
  5270. msdc_set_smpl(host, hs400, cur_rsmpl, TYPE_CMD_RESP_EDGE, NULL);
  5271. } else {
  5272. cur_rsmpl = 0;
  5273. /* need second layer */
  5274. msdc_set_smpl(host, hs400, cur_rsmpl, TYPE_CMD_RESP_EDGE, NULL);
  5275. cur_rrdly = (orig_rrdly + 1);
  5276. if (cur_rrdly >= 32) {
  5277. ERR_MSG("failed to update rrdly<%d>", cur_rrdly);
  5278. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, 0);
  5279. #ifdef MSDC_LOWER_FREQ
  5280. return msdc_lower_freq(host);
  5281. #else
  5282. return 1;
  5283. #endif
  5284. }
  5285. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, cur_rrdly);
  5286. }
  5287. #endif
  5288. sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, orig_rsmpl);
  5289. sdr_get_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, orig_rrdly);
  5290. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR, orig_cmdrtc);
  5291. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  5292. orig_dl_cksel);
  5293. pr_err("msdc%d TUNE_CMD: rsmpl<%d> rrdly<%d> cmdrtc<%d> dl_cksel<%d> sfreq.<%d>",
  5294. host->id, orig_rsmpl, orig_rrdly, orig_cmdrtc, orig_dl_cksel, host->sclk);
  5295. #ifdef STO_LOG
  5296. if (unlikely(dumpMSDC())) {
  5297. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_do_request,
  5298. "sd_tune_ok RSPL", orig_rsmpl);
  5299. AddStorageTrace(STORAGE_LOGGER_MSG_MSDC_DO, msdc_do_request,
  5300. "sd_tune_ok RRDLY", orig_rrdly);
  5301. }
  5302. #endif
  5303. return result;
  5304. }
  5305. int hs400_restore_pad_tune(int restore)
  5306. {
  5307. void __iomem *base = 0;
  5308. if (mtk_msdc_host[0]) {
  5309. base = mtk_msdc_host[0]->base;
  5310. mtk_msdc_host[0]->saved_para.pad_tune0 = sdr_read32(MSDC_PAD_TUNE0);
  5311. }
  5312. return 0;
  5313. }
  5314. int hs400_restore_pb1(int restore)
  5315. {
  5316. void __iomem *base = 0;
  5317. if (mtk_msdc_host[0]) {
  5318. base = mtk_msdc_host[0]->base;
  5319. if (!restore)
  5320. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR, 0x2);
  5321. sdr_get_field((MSDC_PATCH_BIT1), MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  5322. mtk_msdc_host[0]->saved_para.wrdat_crc_ta_cntr);
  5323. }
  5324. return 0;
  5325. }
  5326. int hs400_restore_ddly0(int restore)
  5327. {
  5328. void __iomem *base = 0;
  5329. if (mtk_msdc_host[0]) {
  5330. base = mtk_msdc_host[0]->base;
  5331. mtk_msdc_host[0]->saved_para.ddly0 = sdr_read32(MSDC_DAT_RDDLY0);
  5332. }
  5333. return 0;
  5334. }
  5335. int hs400_restore_ddly1(int restore)
  5336. {
  5337. void __iomem *base = 0;
  5338. if (mtk_msdc_host[0]) {
  5339. base = mtk_msdc_host[0]->base;
  5340. mtk_msdc_host[0]->saved_para.ddly1 = sdr_read32(MSDC_DAT_RDDLY1);
  5341. }
  5342. return 0;
  5343. }
  5344. int hs400_restore_cmd_tune(int restore)
  5345. {
  5346. void __iomem *base = 0;
  5347. if (mtk_msdc_host[0]) {
  5348. base = mtk_msdc_host[0]->base;
  5349. if (!restore) {
  5350. sdr_set_field(EMMC50_PAD_CMD_TUNE, MSDC_EMMC50_PAD_CMD_TUNE_TXDLY,
  5351. 0x4);
  5352. }
  5353. }
  5354. return 0;
  5355. }
  5356. int hs400_restore_dat01_tune(int restore)
  5357. {
  5358. void __iomem *base = 0;
  5359. if (mtk_msdc_host[0]) {
  5360. base = mtk_msdc_host[0]->base;
  5361. if (0) { /* (!restore){ */
  5362. sdr_set_field(EMMC50_PAD_DAT01_TUNE, MSDC_EMMC50_PAD_DAT0_TXDLY,
  5363. 0x4);
  5364. sdr_set_field(EMMC50_PAD_DAT01_TUNE, MSDC_EMMC50_PAD_DAT1_TXDLY,
  5365. 0x4);
  5366. }
  5367. }
  5368. return 0;
  5369. }
  5370. int hs400_restore_dat23_tune(int restore)
  5371. {
  5372. void __iomem *base = 0;
  5373. if (mtk_msdc_host[0]) {
  5374. base = mtk_msdc_host[0]->base;
  5375. if (0) { /* (!restore){ */
  5376. sdr_set_field(EMMC50_PAD_DAT23_TUNE, MSDC_EMMC50_PAD_DAT2_TXDLY,
  5377. 0x4);
  5378. sdr_set_field(EMMC50_PAD_DAT23_TUNE, MSDC_EMMC50_PAD_DAT3_TXDLY,
  5379. 0x4);
  5380. }
  5381. }
  5382. return 0;
  5383. }
  5384. int hs400_restore_dat45_tune(int restore)
  5385. {
  5386. void __iomem *base = 0;
  5387. if (mtk_msdc_host[0]) {
  5388. base = mtk_msdc_host[0]->base;
  5389. if (0) { /* (!restore){ */
  5390. sdr_set_field(EMMC50_PAD_DAT45_TUNE, MSDC_EMMC50_PAD_DAT4_TXDLY,
  5391. 0x4);
  5392. sdr_set_field(EMMC50_PAD_DAT45_TUNE, MSDC_EMMC50_PAD_DAT5_TXDLY,
  5393. 0x4);
  5394. }
  5395. }
  5396. return 0;
  5397. }
  5398. int hs400_restore_dat67_tune(int restore)
  5399. {
  5400. void __iomem *base = 0;
  5401. if (mtk_msdc_host[0]) {
  5402. base = mtk_msdc_host[0]->base;
  5403. if (0) { /* (!restore){ */
  5404. sdr_set_field(EMMC50_PAD_DAT67_TUNE, MSDC_EMMC50_PAD_DAT6_TXDLY,
  5405. 0x4);
  5406. sdr_set_field(EMMC50_PAD_DAT67_TUNE, MSDC_EMMC50_PAD_DAT7_TXDLY,
  5407. 0x4);
  5408. }
  5409. }
  5410. return 0;
  5411. }
  5412. int hs400_restore_ds_tune(int restore)
  5413. {
  5414. void __iomem *base = 0;
  5415. if (mtk_msdc_host[0]) {
  5416. base = mtk_msdc_host[0]->base;
  5417. if (0) { /* (!restore){ */
  5418. sdr_set_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1,
  5419. 0x7);
  5420. sdr_set_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3,
  5421. 0x18);
  5422. }
  5423. }
  5424. return 0;
  5425. }
  5426. /*
  5427. * 2013-12-09
  5428. * different register settings between eMMC 4.5 backward speed mode
  5429. * and HS400 speed mode
  5430. */
  5431. #define HS400_BACKUP_REG_NUM (12)
  5432. static struct msdc_reg_control hs400_backup_reg_list[HS400_BACKUP_REG_NUM] = {
  5433. /* addr mask value default restore_func */
  5434. {(OFFSET_MSDC_PATCH_BIT0), (MSDC_PB0_INT_DAT_LATCH_CK_SEL), 0x0, 0x0, NULL},
  5435. /* the defalut init value is 0x1, but HS400 need the value be 0x0 */
  5436. {(OFFSET_MSDC_PATCH_BIT1), (MSDC_PB1_WRDAT_CRCS_TA_CNTR), 0x0, 0x0,
  5437. hs400_restore_pb1},
  5438. {(OFFSET_MSDC_IOCON),
  5439. (MSDC_IOCON_R_D_SMPL | MSDC_IOCON_DDLSEL | MSDC_IOCON_R_D_SMPL_SEL
  5440. | MSDC_IOCON_R_D0SPL | MSDC_IOCON_W_D_SMPL_SEL | MSDC_IOCON_W_D_SMPL),
  5441. 0x0, 0x0, NULL},
  5442. {(OFFSET_MSDC_PAD_TUNE0), (MSDC_PAD_TUNE0_DATWRDLY
  5443. | MSDC_PAD_TUNE0_DATRRDLY), 0x0, 0x0, hs400_restore_pad_tune},
  5444. {(OFFSET_MSDC_DAT_RDDLY0), (MSDC_DAT_RDDLY0_D3 | MSDC_DAT_RDDLY0_D2
  5445. | MSDC_DAT_RDDLY0_D1 | MSDC_DAT_RDDLY0_D0), 0x0, 0x0,
  5446. hs400_restore_ddly0},
  5447. {(OFFSET_MSDC_DAT_RDDLY1), (MSDC_DAT_RDDLY1_D7 | MSDC_DAT_RDDLY1_D6
  5448. | MSDC_DAT_RDDLY1_D5 | MSDC_DAT_RDDLY1_D4), 0x0, 0x0,
  5449. hs400_restore_ddly1},
  5450. {(OFFSET_EMMC50_PAD_CMD_TUNE), (MSDC_EMMC50_PAD_CMD_TUNE_TXDLY),
  5451. 0x0, 0x00000200, hs400_restore_cmd_tune},
  5452. {(OFFSET_EMMC50_PAD_DS_TUNE), (MSDC_EMMC50_PAD_DS_TUNE_DLY1
  5453. | MSDC_EMMC50_PAD_DS_TUNE_DLY3), 0x0, 0x0, hs400_restore_ds_tune},
  5454. {(OFFSET_EMMC50_PAD_DAT01_TUNE), (MSDC_EMMC50_PAD_DAT0_TXDLY
  5455. | MSDC_EMMC50_PAD_DAT1_TXDLY), 0x0, 0x01000100,
  5456. hs400_restore_dat01_tune},
  5457. {(OFFSET_EMMC50_PAD_DAT23_TUNE), (MSDC_EMMC50_PAD_DAT2_TXDLY
  5458. | MSDC_EMMC50_PAD_DAT3_TXDLY), 0x0, 0x01000100,
  5459. hs400_restore_dat23_tune},
  5460. {(OFFSET_EMMC50_PAD_DAT45_TUNE), (MSDC_EMMC50_PAD_DAT4_TXDLY
  5461. | MSDC_EMMC50_PAD_DAT5_TXDLY), 0x0, 0x01000100,
  5462. hs400_restore_dat45_tune},
  5463. {(OFFSET_EMMC50_PAD_DAT67_TUNE), (MSDC_EMMC50_PAD_DAT6_TXDLY
  5464. | MSDC_EMMC50_PAD_DAT7_TXDLY), 0x0, 0x01000100,
  5465. hs400_restore_dat67_tune},
  5466. };
  5467. /*
  5468. * 2013-12-09
  5469. * when switch from eMMC 4.5 backward speed mode to HS400 speed mode
  5470. * do back up the eMMC 4.5 backward speed mode tunning result,
  5471. * and init them with defalut value for HS400 speed mode
  5472. */
  5473. static void emmc_hs400_backup(void)
  5474. {
  5475. int i = 0, err = 0;
  5476. for (i = 0; i < HS400_BACKUP_REG_NUM; i++) {
  5477. sdr_get_field((hs400_backup_reg_list[i].addr + mtk_msdc_host[0]->base),
  5478. hs400_backup_reg_list[i].mask,
  5479. hs400_backup_reg_list[i].value);
  5480. if (hs400_backup_reg_list[i].restore_func) {
  5481. err = hs400_backup_reg_list[i].restore_func(0);
  5482. if (err) {
  5483. pr_err
  5484. ("[%s]: failed to restore reg[%p][0x%x];"
  5485. "expected value[0x%x], actual value[0x%x] err=0x%x",
  5486. __func__, (hs400_backup_reg_list[i].addr
  5487. + mtk_msdc_host[0]->base), hs400_backup_reg_list[i].mask,
  5488. hs400_backup_reg_list[i].default_value,
  5489. sdr_read32((hs400_backup_reg_list[i].addr +
  5490. mtk_msdc_host[0]->base)), err);
  5491. }
  5492. }
  5493. }
  5494. }
  5495. /*
  5496. * 2013-12-09
  5497. * when switch from HS400 speed mode to eMMC 4.5 backward speed mode
  5498. * do restore the eMMC 4.5 backward speed mode tunning result
  5499. */
  5500. static void emmc_hs400_restore(void)
  5501. {
  5502. int i = 0, err = 0;
  5503. if (!mtk_msdc_host[0]) {
  5504. pr_err("[%s] msdc%d is not exist\n", __func__, 0);
  5505. return;
  5506. }
  5507. for (i = 0; i < HS400_BACKUP_REG_NUM; i++) {
  5508. sdr_set_field((hs400_backup_reg_list[i].addr + mtk_msdc_host[0]->base),
  5509. hs400_backup_reg_list[i].mask,
  5510. hs400_backup_reg_list[i].value);
  5511. if (hs400_backup_reg_list[i].restore_func) {
  5512. err = hs400_backup_reg_list[i].restore_func(1);
  5513. if (err) {
  5514. pr_err
  5515. ("[%s]:failed to restore reg[%p][0x%x];"
  5516. "expected value[0x%x], actual value[0x%x] err=0x%x",
  5517. __func__, (hs400_backup_reg_list[i].addr
  5518. + mtk_msdc_host[0]->base), hs400_backup_reg_list[i].mask,
  5519. hs400_backup_reg_list[i].value,
  5520. sdr_read32(hs400_backup_reg_list[i].addr), err);
  5521. }
  5522. }
  5523. pr_debug("[%s]:i:%d, reg=%p, value=0x%x\n", __func__, i,
  5524. (hs400_backup_reg_list[i].addr + mtk_msdc_host[0]->base),
  5525. sdr_read32((hs400_backup_reg_list[i].addr
  5526. + mtk_msdc_host[0]->base)));
  5527. }
  5528. }
  5529. /*
  5530. * 2015-01-09
  5531. * Runtime reducing to legacy speed or slower clock, clear eMMC ett timing
  5532. */
  5533. #ifdef CONFIG_MMC_FFU
  5534. static void emmc_clear_timing(void)
  5535. {
  5536. int i = 0;
  5537. if (!mtk_msdc_host[0]) {
  5538. pr_err("[%s] msdc%d is not exist\n", __func__, 0);
  5539. return;
  5540. }
  5541. pr_err("emmc_clear_timing msdc0\n");
  5542. for (i = 0; i < HS400_BACKUP_REG_NUM; i++)
  5543. sdr_set_field((hs400_backup_reg_list[i].addr + mtk_msdc_host[0]->base),
  5544. hs400_backup_reg_list[i].mask,
  5545. hs400_backup_reg_list[i].default_value);
  5546. }
  5547. #endif
  5548. /*
  5549. * 2013-12-09
  5550. * HS400 error tune flow of read/write data error
  5551. * HS400 error tune flow of cmd error is same as eMMC4.5 backward speed mode.
  5552. */
  5553. int emmc_hs400_tune_rw(struct msdc_host *host)
  5554. {
  5555. void __iomem *base = host->base;
  5556. int cur_ds_dly1 = 0, cur_ds_dly3 = 0, orig_ds_dly1 = 0, orig_ds_dly3 = 0;
  5557. int err = 0;
  5558. if ((host->id != 0) || (host->timing != MMC_TIMING_MMC_HS400))
  5559. return err;
  5560. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1,
  5561. orig_ds_dly1);
  5562. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3,
  5563. orig_ds_dly3);
  5564. if (g_ett_tune) {
  5565. cur_ds_dly3 = orig_ds_dly3 + 1;
  5566. cur_ds_dly1 = orig_ds_dly1;
  5567. if (cur_ds_dly3 >= 32) {
  5568. cur_ds_dly3 = 0;
  5569. cur_ds_dly1 = orig_ds_dly1 + 1;
  5570. if (cur_ds_dly1 >= 32)
  5571. cur_ds_dly1 = 0;
  5572. }
  5573. } else {
  5574. cur_ds_dly1 = orig_ds_dly1 - 1;
  5575. cur_ds_dly3 = orig_ds_dly3;
  5576. if (cur_ds_dly1 < 0) {
  5577. cur_ds_dly1 = 17;
  5578. cur_ds_dly3 = orig_ds_dly3 + 1;
  5579. if (cur_ds_dly3 >= 32)
  5580. cur_ds_dly3 = 0;
  5581. }
  5582. }
  5583. if (++host->t_counter.time_hs400 ==
  5584. (g_ett_tune ? (32 * 32) : MAX_HS400_TUNE_COUNT)) {
  5585. ERR_MSG("Failed to update EMMC50_PAD_DS_TUNE_DLY;"
  5586. "cur_ds_dly3=0x%x, cur_ds_dly1=0x%x", cur_ds_dly3, cur_ds_dly1);
  5587. #ifdef MSDC_LOWER_FREQ
  5588. err = msdc_lower_freq(host);
  5589. #else
  5590. err = 1;
  5591. #endif
  5592. goto out;
  5593. } else {
  5594. sdr_set_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1,
  5595. cur_ds_dly1);
  5596. if (cur_ds_dly3 != orig_ds_dly3) {
  5597. sdr_set_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3,
  5598. cur_ds_dly3);
  5599. }
  5600. pr_err("msdc%d HS400_TUNE: orig_ds_dly1<0x%x>, orig_ds_dly3<0x%x>;"
  5601. "cur_ds_dly1<0x%x>, cur_ds_dly3<0x%x>", host->id, orig_ds_dly1,
  5602. orig_ds_dly3, cur_ds_dly1, cur_ds_dly3);
  5603. }
  5604. out:
  5605. return err;
  5606. }
  5607. int msdc_tune_read(struct msdc_host *host)
  5608. {
  5609. void __iomem *base = host->base;
  5610. u32 sel = 0;
  5611. u32 ddr = 0, hs400 = 0;
  5612. u32 dcrc;
  5613. u32 clkmode = 0;
  5614. u32 cur_rxdly0, cur_rxdly1;
  5615. u32 cur_dsmpl = 0, orig_dsmpl;
  5616. u32 cur_dsel = 0, orig_dsel;
  5617. u32 cur_dl_cksel = 0, orig_dl_cksel;
  5618. u32 cur_dat0 = 0, cur_dat1 = 0, cur_dat2 = 0, cur_dat3 = 0,
  5619. cur_dat4 = 0, cur_dat5 = 0, cur_dat6 = 0, cur_dat7 = 0;
  5620. u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3, orig_dat4,
  5621. orig_dat5, orig_dat6, orig_dat7;
  5622. int result = 0;
  5623. #if 1
  5624. if (host->mclk >= 100000000)
  5625. sel = 1;
  5626. else
  5627. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_CKGEN_MSDC_DLY_SEL, 0);
  5628. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, clkmode);
  5629. ddr = (clkmode == 2) ? 1 : 0;
  5630. hs400 = (clkmode == 3) ? 1 : 0;
  5631. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_CKGEN_MSDC_DLY_SEL, orig_dsel);
  5632. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  5633. orig_dl_cksel);
  5634. sdr_get_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, orig_dsmpl);
  5635. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  5636. cur_dsmpl = (orig_dsmpl + 1);
  5637. msdc_set_smpl(host, hs400, cur_dsmpl % 2, TYPE_READ_DATA_EDGE, NULL);
  5638. if (cur_dsmpl >= 2) {
  5639. sdr_get_field(SDC_DCRC_STS, SDC_DCRC_STS_POS | SDC_DCRC_STS_NEG, dcrc);
  5640. if (!ddr)
  5641. dcrc &= ~SDC_DCRC_STS_NEG;
  5642. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  5643. cur_rxdly1 = sdr_read32(MSDC_DAT_RDDLY1);
  5644. orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  5645. orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  5646. orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  5647. orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  5648. orig_dat4 = (cur_rxdly1 >> 24) & 0x1F;
  5649. orig_dat5 = (cur_rxdly1 >> 16) & 0x1F;
  5650. orig_dat6 = (cur_rxdly1 >> 8) & 0x1F;
  5651. orig_dat7 = (cur_rxdly1 >> 0) & 0x1F;
  5652. if (ddr) {
  5653. cur_dat0 = (dcrc & (1 << 0)
  5654. || dcrc & (1 << 8)) ? (orig_dat0 + 1) : orig_dat0;
  5655. cur_dat1 = (dcrc & (1 << 1)
  5656. || dcrc & (1 << 9)) ? (orig_dat1 + 1) : orig_dat1;
  5657. cur_dat2 = (dcrc & (1 << 2)
  5658. || dcrc & (1 << 10)) ? (orig_dat2 + 1) : orig_dat2;
  5659. cur_dat3 = (dcrc & (1 << 3)
  5660. || dcrc & (1 << 11)) ? (orig_dat3 + 1) : orig_dat3;
  5661. cur_dat4 = (dcrc & (1 << 4)
  5662. || dcrc & (1 << 12)) ? (orig_dat4 + 1) : orig_dat4;
  5663. cur_dat5 = (dcrc & (1 << 5)
  5664. || dcrc & (1 << 13)) ? (orig_dat5 + 1) : orig_dat5;
  5665. cur_dat6 = (dcrc & (1 << 6)
  5666. || dcrc & (1 << 14)) ? (orig_dat6 + 1) : orig_dat6;
  5667. cur_dat7 = (dcrc & (1 << 7)
  5668. || dcrc & (1 << 15)) ? (orig_dat7 + 1) : orig_dat7;
  5669. } else {
  5670. cur_dat0 = (dcrc & (1 << 0)) ? (orig_dat0 + 1) : orig_dat0;
  5671. cur_dat1 = (dcrc & (1 << 1)) ? (orig_dat1 + 1) : orig_dat1;
  5672. cur_dat2 = (dcrc & (1 << 2)) ? (orig_dat2 + 1) : orig_dat2;
  5673. cur_dat3 = (dcrc & (1 << 3)) ? (orig_dat3 + 1) : orig_dat3;
  5674. cur_dat4 = (dcrc & (1 << 4)) ? (orig_dat4 + 1) : orig_dat4;
  5675. cur_dat5 = (dcrc & (1 << 5)) ? (orig_dat5 + 1) : orig_dat5;
  5676. cur_dat6 = (dcrc & (1 << 6)) ? (orig_dat6 + 1) : orig_dat6;
  5677. cur_dat7 = (dcrc & (1 << 7)) ? (orig_dat7 + 1) : orig_dat7;
  5678. }
  5679. cur_rxdly0 = ((cur_dat0 & 0x1F) << 24) | ((cur_dat1 & 0x1F) << 16) |
  5680. ((cur_dat2 & 0x1F) << 8) | ((cur_dat3 & 0x1F) << 0);
  5681. cur_rxdly1 = ((cur_dat4 & 0x1F) << 24) | ((cur_dat5 & 0x1F) << 16) |
  5682. ((cur_dat6 & 0x1F) << 8) | ((cur_dat7 & 0x1F) << 0);
  5683. sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  5684. sdr_write32(MSDC_DAT_RDDLY1, cur_rxdly1);
  5685. }
  5686. if ((cur_dat0 >= 32) || (cur_dat1 >= 32) || (cur_dat2 >= 32)
  5687. || (cur_dat3 >= 32) || (cur_dat4 >= 32) || (cur_dat5 >= 32)
  5688. || (cur_dat6 >= 32) || (cur_dat7 >= 32)) {
  5689. if (sel) {
  5690. sdr_write32(MSDC_DAT_RDDLY0, 0);
  5691. sdr_write32(MSDC_DAT_RDDLY1, 0);
  5692. cur_dsel = (orig_dsel + 1);
  5693. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_CKGEN_MSDC_DLY_SEL,
  5694. cur_dsel % 32);
  5695. }
  5696. }
  5697. if (cur_dsel >= 32) {
  5698. if (clkmode == 1 && sel) {
  5699. cur_dl_cksel = (orig_dl_cksel + 1);
  5700. sdr_set_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  5701. cur_dl_cksel % 8);
  5702. }
  5703. }
  5704. ++(host->t_counter.time_read);
  5705. if ((sel == 1 && clkmode == 1
  5706. && host->t_counter.time_read == READ_TUNE_UHS_CLKMOD1_MAX_TIME)
  5707. || (sel == 1 && (clkmode == 0 || clkmode == 2)
  5708. && host->t_counter.time_read == READ_TUNE_UHS_MAX_TIME)
  5709. || (sel == 0 && (clkmode == 0 || clkmode == 2)
  5710. && host->t_counter.time_read == READ_TUNE_HS_MAX_TIME)) {
  5711. #ifdef MSDC_LOWER_FREQ
  5712. result = msdc_lower_freq(host);
  5713. #else
  5714. result = 1;
  5715. #endif
  5716. host->t_counter.time_read = 0;
  5717. }
  5718. #else
  5719. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  5720. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  5721. cur_rxdly1 = sdr_read32(MSDC_DAT_RDDLY1);
  5722. sdr_get_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, orig_dsmpl);
  5723. if (orig_dsmpl == 0) {
  5724. cur_dsmpl = 1;
  5725. msdc_set_smpl(host, hs400, cur_dsmpl, TYPE_READ_DATA_EDGE, NULL);
  5726. } else {
  5727. cur_dsmpl = 0;
  5728. /* need second layer */
  5729. msdc_set_smpl(host, hs400, cur_dsmpl, TYPE_READ_DATA_EDGE, NULL);
  5730. sdr_get_field(SDC_DCRC_STS, SDC_DCRC_STS_POS | SDC_DCRC_STS_NEG, dcrc);
  5731. if (!ddr)
  5732. dcrc &= ~SDC_DCRC_STS_NEG;
  5733. if (sdr_read32(MSDC_ECO_VER) >= 4) {
  5734. orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  5735. orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  5736. orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  5737. orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  5738. orig_dat4 = (cur_rxdly1 >> 24) & 0x1F;
  5739. orig_dat5 = (cur_rxdly1 >> 16) & 0x1F;
  5740. orig_dat6 = (cur_rxdly1 >> 8) & 0x1F;
  5741. orig_dat7 = (cur_rxdly1 >> 0) & 0x1F;
  5742. } else {
  5743. orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
  5744. orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
  5745. orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
  5746. orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
  5747. orig_dat4 = (cur_rxdly1 >> 0) & 0x1F;
  5748. orig_dat5 = (cur_rxdly1 >> 8) & 0x1F;
  5749. orig_dat6 = (cur_rxdly1 >> 16) & 0x1F;
  5750. orig_dat7 = (cur_rxdly1 >> 24) & 0x1F;
  5751. }
  5752. if (ddr) {
  5753. cur_dat0 = (dcrc & (1 << 0)
  5754. || dcrc & (1 << 8)) ? (orig_dat0 + 1) : orig_dat0;
  5755. cur_dat1 = (dcrc & (1 << 1)
  5756. || dcrc & (1 << 9)) ? (orig_dat1 + 1) : orig_dat1;
  5757. cur_dat2 = (dcrc & (1 << 2)
  5758. || dcrc & (1 << 10)) ? (orig_dat2 + 1) : orig_dat2;
  5759. cur_dat3 = (dcrc & (1 << 3)
  5760. || dcrc & (1 << 11)) ? (orig_dat3 + 1) : orig_dat3;
  5761. } else {
  5762. cur_dat0 = (dcrc & (1 << 0)) ? (orig_dat0 + 1) : orig_dat0;
  5763. cur_dat1 = (dcrc & (1 << 1)) ? (orig_dat1 + 1) : orig_dat1;
  5764. cur_dat2 = (dcrc & (1 << 2)) ? (orig_dat2 + 1) : orig_dat2;
  5765. cur_dat3 = (dcrc & (1 << 3)) ? (orig_dat3 + 1) : orig_dat3;
  5766. }
  5767. cur_dat4 = (dcrc & (1 << 4)) ? (orig_dat4 + 1) : orig_dat4;
  5768. cur_dat5 = (dcrc & (1 << 5)) ? (orig_dat5 + 1) : orig_dat5;
  5769. cur_dat6 = (dcrc & (1 << 6)) ? (orig_dat6 + 1) : orig_dat6;
  5770. cur_dat7 = (dcrc & (1 << 7)) ? (orig_dat7 + 1) : orig_dat7;
  5771. if (cur_dat0 >= 32 || cur_dat1 >= 32
  5772. || cur_dat2 >= 32 || cur_dat3 >= 32) {
  5773. ERR_MSG("failed to update <%xh><%xh><%xh><%xh>",
  5774. cur_dat0, cur_dat1, cur_dat2, cur_dat3);
  5775. sdr_write32(MSDC_DAT_RDDLY0, 0);
  5776. sdr_write32(MSDC_DAT_RDDLY1, 0);
  5777. #ifdef MSDC_LOWER_FREQ
  5778. return msdc_lower_freq(host);
  5779. #else
  5780. return 1;
  5781. #endif
  5782. }
  5783. if (cur_dat4 >= 32 || cur_dat5 >= 32
  5784. || cur_dat6 >= 32 || cur_dat7 >= 32) {
  5785. ERR_MSG("failed to update <%xh><%xh><%xh><%xh>",
  5786. cur_dat4, cur_dat5, cur_dat6, cur_dat7);
  5787. sdr_write32(MSDC_DAT_RDDLY0, 0);
  5788. sdr_write32(MSDC_DAT_RDDLY1, 0);
  5789. #ifdef MSDC_LOWER_FREQ
  5790. return msdc_lower_freq(host);
  5791. #else
  5792. return 1;
  5793. #endif
  5794. }
  5795. cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16)
  5796. | (cur_dat2 << 8) | (cur_dat3 << 0);
  5797. cur_rxdly1 = (cur_dat4 << 24) | (cur_dat5 << 16)
  5798. | (cur_dat6 << 8) | (cur_dat7 << 0);
  5799. sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  5800. sdr_write32(MSDC_DAT_RDDLY1, cur_rxdly1);
  5801. }
  5802. #endif
  5803. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_CKGEN_MSDC_DLY_SEL, orig_dsel);
  5804. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  5805. orig_dl_cksel);
  5806. sdr_get_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, orig_dsmpl);
  5807. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  5808. cur_rxdly1 = sdr_read32(MSDC_DAT_RDDLY1);
  5809. /*pr_err("msdc%d TUNE_READ: dsmpl<%d> rxdly0<0x%x> rxdly1<0x%x>;"
  5810. "dsel<%d> dl_cksel<%d> sfreq.<%d>", host->id, orig_dsmpl, cur_rxdly0,
  5811. cur_rxdly1, orig_dsel, orig_dl_cksel, host->sclk);*/
  5812. return result;
  5813. }
  5814. int msdc_tune_write(struct msdc_host *host)
  5815. {
  5816. void __iomem *base = host->base;
  5817. /* u32 cur_wrrdly = 0, orig_wrrdly; */
  5818. u32 cur_dsmpl = 0, orig_dsmpl;
  5819. u32 cur_rxdly0 = 0;
  5820. u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
  5821. u32 cur_dat0 = 0, cur_dat1 = 0, cur_dat2 = 0, cur_dat3 = 0;
  5822. u32 cur_d_cntr = 0, orig_d_cntr;
  5823. int result = 0;
  5824. int sel = 0;
  5825. int clkmode = 0;
  5826. int hs400 = 0;
  5827. #if 1
  5828. if (host->mclk >= 100000000)
  5829. sel = 1;
  5830. else
  5831. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR, 1);
  5832. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, clkmode);
  5833. /* sdr_get_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, orig_wrrdly);*/
  5834. sdr_get_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, orig_dsmpl);
  5835. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR, orig_d_cntr);
  5836. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  5837. cur_dsmpl = (orig_dsmpl + 1);
  5838. hs400 = (clkmode == 3) ? 1 : 0;
  5839. msdc_set_smpl(host, hs400, cur_dsmpl % 2, TYPE_WRITE_CRC_EDGE, NULL);
  5840. #if 0
  5841. if (cur_dsmpl >= 2) {
  5842. cur_wrrdly = (orig_wrrdly + 1);
  5843. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, cur_wrrdly % 32);
  5844. }
  5845. #endif
  5846. if (cur_dsmpl >= 2) {
  5847. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  5848. orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  5849. orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  5850. orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  5851. orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  5852. cur_dat0 = (orig_dat0 + 1); /* only adjust bit-1 for crc */
  5853. cur_dat1 = orig_dat1;
  5854. cur_dat2 = orig_dat2;
  5855. cur_dat3 = orig_dat3;
  5856. cur_rxdly0 = ((cur_dat0 & 0x1F) << 24) | ((cur_dat1 & 0x1F) << 16) |
  5857. ((cur_dat2 & 0x1F) << 8) | ((cur_dat3 & 0x1F) << 0);
  5858. sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  5859. }
  5860. if (cur_dat0 >= 32) {
  5861. if (sel) {
  5862. cur_d_cntr = (orig_d_cntr + 1);
  5863. sdr_set_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  5864. cur_d_cntr % 8);
  5865. }
  5866. }
  5867. ++(host->t_counter.time_write);
  5868. if ((sel == 0 && host->t_counter.time_write == WRITE_TUNE_HS_MAX_TIME)
  5869. || (sel && host->t_counter.time_write == WRITE_TUNE_UHS_MAX_TIME)) {
  5870. #ifdef MSDC_LOWER_FREQ
  5871. result = msdc_lower_freq(host);
  5872. #else
  5873. result = 1;
  5874. #endif
  5875. host->t_counter.time_write = 0;
  5876. }
  5877. #else
  5878. /* Tune Method 2. just DAT0 */
  5879. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  5880. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  5881. if (sdr_read32(MSDC_ECO_VER) >= 4) {
  5882. orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  5883. orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  5884. orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  5885. orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  5886. } else {
  5887. orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
  5888. orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
  5889. orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
  5890. orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
  5891. }
  5892. sdr_get_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, orig_wrrdly);
  5893. cur_wrrdly = orig_wrrdly;
  5894. sdr_get_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, orig_dsmpl);
  5895. if (orig_dsmpl == 0) {
  5896. cur_dsmpl = 1;
  5897. msdc_set_smpl(host, hs400, cur_dsmpl, TYPE_WRITE_CRC_EDGE, NULL);
  5898. } else {
  5899. cur_dsmpl = 0;
  5900. /* need the second layer */
  5901. sdr_set_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, cur_dsmpl);
  5902. cur_wrrdly = (orig_wrrdly + 1);
  5903. if (cur_wrrdly < 32) {
  5904. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, cur_wrrdly);
  5905. } else {
  5906. cur_wrrdly = 0;
  5907. /* need third */
  5908. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, cur_wrrdly);
  5909. cur_dat0 = orig_dat0 + 1; /* only adjust bit-1 for crc */
  5910. cur_dat1 = orig_dat1;
  5911. cur_dat2 = orig_dat2;
  5912. cur_dat3 = orig_dat3;
  5913. if (cur_dat0 >= 32) {
  5914. ERR_MSG("update failed <%xh>", cur_dat0);
  5915. sdr_write32(MSDC_DAT_RDDLY0, 0);
  5916. #ifdef MSDC_LOWER_FREQ
  5917. return msdc_lower_freq(host);
  5918. #else
  5919. return 1;
  5920. #endif
  5921. }
  5922. cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16)
  5923. | (cur_dat2 << 8) | (cur_dat3 << 0);
  5924. sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  5925. }
  5926. }
  5927. #endif
  5928. /* sdr_get_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, orig_wrrdly); */
  5929. sdr_get_field(MSDC_IOCON, MSDC_IOCON_W_D_SMPL, orig_dsmpl);
  5930. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR, orig_d_cntr);
  5931. cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  5932. pr_err("msdc%d TUNE_WRITE: dsmpl<%d> rxdly0<0x%x> d_cntr<%d> sfreq.<%d>",
  5933. host->id, orig_dsmpl, cur_rxdly0, orig_d_cntr, host->sclk);
  5934. return result;
  5935. }
  5936. static int msdc_get_card_status(struct mmc_host *mmc, struct msdc_host *host,
  5937. u32 *status)
  5938. {
  5939. struct mmc_command cmd;
  5940. struct mmc_request mrq;
  5941. u32 err;
  5942. memset(&cmd, 0, sizeof(struct mmc_command));
  5943. cmd.opcode = MMC_SEND_STATUS; /* CMD13 */
  5944. cmd.arg = host->app_cmd_arg;
  5945. cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  5946. memset(&mrq, 0, sizeof(struct mmc_request));
  5947. mrq.cmd = &cmd;
  5948. cmd.mrq = &mrq;
  5949. cmd.data = NULL;
  5950. /* tune until CMD13 pass. */
  5951. err = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);
  5952. if (status)
  5953. *status = cmd.resp[0];
  5954. return err;
  5955. }
  5956. /* #define TUNE_FLOW_TEST */
  5957. #ifdef TUNE_FLOW_TEST
  5958. static void msdc_reset_para(struct msdc_host *host)
  5959. {
  5960. void __iomem *base = host->base;
  5961. u32 dsmpl, rsmpl, clkmode;
  5962. int hs400 = 0;
  5963. /* because we have a card, which must work at dsmpl<0> and rsmpl<0> */
  5964. sdr_get_field(MSDC_IOCON, MSDC_IOCON_R_D_SMPL, dsmpl);
  5965. sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, rsmpl);
  5966. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, clkmode);
  5967. hs400 = (clkmode == 3) ? 1 : 0;
  5968. if (dsmpl == 0) {
  5969. msdc_set_smpl(host, hs400, 1, TYPE_READ_DATA_EDGE, NULL);
  5970. ERR_MSG("set dspl<0>");
  5971. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, 0);
  5972. }
  5973. if (rsmpl == 0) {
  5974. msdc_set_smpl(host, hs400, 1, TYPE_CMD_RESP_EDGE, NULL);
  5975. ERR_MSG("set rspl<0>");
  5976. sdr_write32(MSDC_DAT_RDDLY0, 0);
  5977. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, 0);
  5978. }
  5979. }
  5980. #endif
  5981. static void msdc_dump_trans_error(struct msdc_host *host,
  5982. struct mmc_command *cmd,
  5983. struct mmc_data *data,
  5984. struct mmc_command *stop,
  5985. struct mmc_command *sbc)
  5986. {
  5987. /* void __iomem *base = host->base; */
  5988. if ((cmd->opcode == 52) && (cmd->arg == 0xc00))
  5989. return;
  5990. if ((cmd->opcode == 52) && (cmd->arg == 0x80000c08))
  5991. return;
  5992. /* by pass the SDIO CMD TO for SD/eMMC */
  5993. if (!(is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))) {
  5994. if ((host->hw->host_function == MSDC_SD) && (cmd->opcode == 5))
  5995. return;
  5996. } else {
  5997. if (cmd->opcode == 8)
  5998. return;
  5999. }
  6000. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  6001. /* auto-K have not done or finished */
  6002. if (is_card_sdio(host)) {
  6003. if (atomic_read(&host->ot_work.autok_done) == 0
  6004. && (cmd->opcode == 52 || cmd->opcode == 53))
  6005. return;
  6006. }
  6007. #endif
  6008. if (data)
  6009. ERR_MSG("XXX DAT block<%d> Error<%d>", data->blocks, data->error);
  6010. if (stop)
  6011. ERR_MSG("XXX STOP<%d><0x%x> Error<%d> Resp<0x%x>",
  6012. stop->opcode, stop->arg, stop->error, stop->resp[0]);
  6013. if (sbc)
  6014. ERR_MSG("XXX SBC<%d><0x%x> Error<%d> Resp<0x%x>",
  6015. sbc->opcode, sbc->arg, sbc->error, sbc->resp[0]);
  6016. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  6017. if ((host->hw->host_function == MSDC_SDIO) && (cmd) && (data) &&
  6018. ((cmd->error == -EIO) || (data->error == -EIO))) {
  6019. u32 vcore_uv_off = autok_get_current_vcore_offset();
  6020. /* ccyeh@FIXME */
  6021. /*int cur_temperature = mtk_thermal_get_temp(MTK_THERMAL_SENSOR_CPU); */
  6022. int cur_temperature = 0; /* ccyeh@FIXME */
  6023. ERR_MSG("XXX Vcore<0x%x> CPU_Temperature<%d>",
  6024. vcore_uv_off, cur_temperature);
  6025. }
  6026. #endif
  6027. if ((host->hw->host_function == MSDC_SD)
  6028. && (host->sclk > 100000000) && (data)
  6029. && (data->error != (unsigned int)-ETIMEDOUT)) {
  6030. if ((data->flags & MMC_DATA_WRITE) && (host->write_timeout_uhs104))
  6031. host->write_timeout_uhs104 = 0;
  6032. if ((data->flags & MMC_DATA_READ) && (host->read_timeout_uhs104))
  6033. host->read_timeout_uhs104 = 0;
  6034. }
  6035. if ((host->hw->host_function == MSDC_EMMC) && (data)
  6036. && (data->error != (unsigned int)-ETIMEDOUT)) {
  6037. if ((data->flags & MMC_DATA_WRITE) && (host->write_timeout_emmc))
  6038. host->write_timeout_emmc = 0;
  6039. if ((data->flags & MMC_DATA_READ) && (host->read_timeout_emmc))
  6040. host->read_timeout_emmc = 0;
  6041. }
  6042. #ifdef SDIO_ERROR_BYPASS
  6043. if (is_card_sdio(host) && (host->sdio_error != -EIO) && (cmd->opcode == 53)
  6044. && (msdc_sg_len(data->sg, host->dma_xfer) > 4)) {
  6045. host->sdio_error = -EIO;
  6046. ERR_MSG("XXX SDIO Error ByPass");
  6047. }
  6048. #endif
  6049. }
  6050. /* ops.request */
  6051. static void msdc_ops_request_legacy(struct mmc_host *mmc,
  6052. struct mmc_request *mrq)
  6053. {
  6054. struct msdc_host *host = mmc_priv(mmc);
  6055. struct mmc_command *cmd;
  6056. struct mmc_data *data;
  6057. struct mmc_command *stop = NULL;
  6058. struct mmc_command *sbc = NULL;
  6059. /* === for sdio profile === */
  6060. /* CCJ fix */
  6061. #if 0
  6062. u32 old_H32 = 0, old_L32 = 0, new_H32 = 0, new_L32 = 0;
  6063. u32 ticks = 0, opcode = 0, sizes = 0, bRx = 0;
  6064. #endif
  6065. u32 status_verify = 0;
  6066. msdc_reset_crc_tune_counter(host, ALL_TUNE_CNT);
  6067. if (host->mrq) {
  6068. ERR_MSG("XXX host->mrq<0x%p> cmd<%d>arg<0x%x>",
  6069. host->mrq, host->mrq->cmd->opcode, host->mrq->cmd->arg);
  6070. BUG();
  6071. }
  6072. if (!is_card_present(host) || host->power_mode == MMC_POWER_OFF) {
  6073. ERR_MSG("cmd<%d> arg<0x%x> card<%d> power<%d>",
  6074. mrq->cmd->opcode, mrq->cmd->arg,
  6075. is_card_present(host), host->power_mode);
  6076. mrq->cmd->error = (unsigned int)-ENOMEDIUM;
  6077. #if 1
  6078. if (mrq->done)
  6079. mrq->done(mrq); /* call done directly. */
  6080. #else
  6081. mrq->cmd->retries = 0; /* please don't retry. */
  6082. mmc_request_done(mmc, mrq);
  6083. #endif
  6084. return;
  6085. }
  6086. /* start to process */
  6087. spin_lock(&host->lock);
  6088. host->power_cycle_enable = 1;
  6089. cmd = mrq->cmd;
  6090. data = mrq->cmd->data;
  6091. if (data)
  6092. stop = data->stop;
  6093. #ifdef MTK_MSDC_USE_CMD23
  6094. if (data)
  6095. sbc = mrq->sbc;
  6096. #endif
  6097. msdc_ungate_clock(host); /* set sw flag */
  6098. #if 0
  6099. if (sdio_pro_enable) { /* === for sdio profile === */
  6100. if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53)
  6101. /* GPT_GetCounter64(&old_L32, &old_H32); */
  6102. }
  6103. #endif
  6104. host->mrq = mrq;
  6105. while (msdc_do_request(mmc, mrq)) {
  6106. /* there is some error
  6107. * because ISR execute time will be monitor, try to dump info here
  6108. */
  6109. msdc_dump_trans_error(host, cmd, data, stop, sbc);
  6110. if (is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))
  6111. goto out; /* sdio not tuning */
  6112. #ifdef MTK_MSDC_USE_CMD23
  6113. if ((sbc != NULL) && (sbc->error == (unsigned int)-ETIMEDOUT)) {
  6114. if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  6115. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  6116. /* not tuning, go out directly */
  6117. pr_err("===[%s:%d]==cmd23 timeout==\n", __func__, __LINE__);
  6118. goto out;
  6119. }
  6120. }
  6121. #endif
  6122. #ifdef MTK_MSDC_USE_CMD23
  6123. /* cmd->error also set when autocmd23 crc error */
  6124. if ((cmd->error == (unsigned int)-EIO)
  6125. || (stop && (stop->error == (unsigned int)-EIO))
  6126. || (sbc && (sbc->error == (unsigned int)-EIO))) {
  6127. #else
  6128. if ((cmd->error == (unsigned int)-EIO)
  6129. || (stop && (stop->error == (unsigned int)-EIO))) {
  6130. #endif
  6131. if (msdc_tune_cmdrsp(host)) {
  6132. ERR_MSG("failed to updata cmd para");
  6133. goto out;
  6134. }
  6135. }
  6136. if (data && (data->error == (unsigned int)-EIO)) {
  6137. if ((host->id == 0) && (host->timing == MMC_TIMING_MMC_HS400)) {
  6138. if (emmc_hs400_tune_rw(host)) {
  6139. ERR_MSG("failed to updata write para");
  6140. goto out;
  6141. }
  6142. } else if (data->flags & MMC_DATA_READ) { /* read */
  6143. if (msdc_tune_read(host)) {
  6144. ERR_MSG("failed to updata read para");
  6145. goto out;
  6146. }
  6147. } else {
  6148. if (msdc_tune_write(host)) {
  6149. ERR_MSG("failed to updata write para");
  6150. goto out;
  6151. }
  6152. }
  6153. }
  6154. status_verify = msdc_status_verify(host, cmd);
  6155. if (MSDC_VERIFY_ERROR == status_verify) {
  6156. ERR_MSG("status verify failed");
  6157. /*data_abort = 1; */
  6158. if (host->hw->host_function == MSDC_SD)
  6159. goto out;
  6160. } else if (MSDC_VERIFY_NEED_NOT_TUNE == status_verify) {
  6161. /* clear the error condition. */
  6162. ERR_MSG("need not error tune");
  6163. cmd->error = 0;
  6164. goto out;
  6165. }
  6166. /* CMD TO -> not tuning */
  6167. if (cmd->error == (unsigned int)-ETIMEDOUT
  6168. && cmd->opcode != MMC_READ_SINGLE_BLOCK
  6169. && cmd->opcode != MMC_READ_MULTIPLE_BLOCK
  6170. && cmd->opcode != MMC_WRITE_BLOCK
  6171. && cmd->opcode != MMC_WRITE_MULTIPLE_BLOCK) {
  6172. goto out;
  6173. }
  6174. if (cmd->error == (unsigned int)-ENOMEDIUM)
  6175. goto out;
  6176. /* [ALPS114710] Patch for data timeout issue */
  6177. if (data && (data->error == (unsigned int)-ETIMEDOUT)) {
  6178. if (data->flags & MMC_DATA_READ) {
  6179. if (!(host->sw_timeout) && (host->hw->host_function == MSDC_SD)
  6180. && (host->sclk > 100000000)
  6181. && (host->read_timeout_uhs104 < MSDC_MAX_R_TIMEOUT_TUNE)) {
  6182. if (host->t_counter.time_read)
  6183. host->t_counter.time_read--;
  6184. host->read_timeout_uhs104++;
  6185. msdc_tune_read(host);
  6186. } else if ((host->sw_timeout)
  6187. || (host->read_timeout_uhs104 >= MSDC_MAX_R_TIMEOUT_TUNE)
  6188. || (++(host->read_time_tune) > MSDC_MAX_TIMEOUT_RETRY)) {
  6189. ERR_MSG
  6190. ("msdc%d exceed max read timeout retry times(%d) or;"
  6191. "SW timeout(%d) or read timeout tune(%d),Power cycle",
  6192. host->id, host->read_time_tune, host->sw_timeout,
  6193. host->read_timeout_uhs104);
  6194. if (msdc_power_tuning(host))
  6195. goto out;
  6196. }
  6197. } else if (data->flags & MMC_DATA_WRITE) {
  6198. if ((!(host->sw_timeout)) &&
  6199. (host->hw->host_function == MSDC_SD) &&
  6200. (host->sclk > 100000000) &&
  6201. (host->write_timeout_uhs104 < MSDC_MAX_W_TIMEOUT_TUNE)) {
  6202. if (host->t_counter.time_write)
  6203. host->t_counter.time_write--;
  6204. host->write_timeout_uhs104++;
  6205. msdc_tune_write(host);
  6206. } else if (!(host->sw_timeout) &&
  6207. (host->hw->host_function == MSDC_EMMC) &&
  6208. (host->write_timeout_emmc <
  6209. MSDC_MAX_W_TIMEOUT_TUNE_EMMC)) {
  6210. if (host->t_counter.time_write)
  6211. host->t_counter.time_write--;
  6212. host->write_timeout_emmc++;
  6213. if ((host->id == 0)
  6214. && (host->timing == MMC_TIMING_MMC_HS400))
  6215. emmc_hs400_tune_rw(host);
  6216. else
  6217. msdc_tune_write(host);
  6218. } else if ((host->hw->host_function == MSDC_SD)
  6219. && ((host->sw_timeout)
  6220. || (host->write_timeout_uhs104 >= MSDC_MAX_W_TIMEOUT_TUNE)
  6221. || (++(host->write_time_tune) > MSDC_MAX_TIMEOUT_RETRY))) {
  6222. ERR_MSG
  6223. ("msdc%d exceed max write timeout retry times(%d) or;"
  6224. "SW timeout(%d) or write timeout tune (%d),Power cycle"
  6225. , host->id, host->write_time_tune, host->sw_timeout,
  6226. host->write_timeout_uhs104);
  6227. if (!(host->sd_30_busy) && msdc_power_tuning(host))
  6228. goto out;
  6229. } else if ((host->hw->host_function == MSDC_EMMC)
  6230. && ((host->sw_timeout) ||
  6231. (++(host->write_time_tune) >
  6232. MSDC_MAX_TIMEOUT_RETRY_EMMC))) {
  6233. ERR_MSG
  6234. ("msdc%d exceed max write timeout retry times(%d) or;"
  6235. "SW timeout(%d) or write timeout tune (%d),Power cycle"
  6236. , host->id, host->write_time_tune, host->sw_timeout,
  6237. host->write_timeout_emmc);
  6238. host->write_timeout_emmc = 0;
  6239. goto out;
  6240. }
  6241. }
  6242. }
  6243. /* clear the error condition. */
  6244. cmd->error = 0;
  6245. if (data)
  6246. data->error = 0;
  6247. if (stop)
  6248. stop->error = 0;
  6249. #ifdef MTK_MSDC_USE_CMD23
  6250. if (sbc)
  6251. sbc->error = 0;
  6252. #endif
  6253. /* check if an app commmand. */
  6254. if (host->app_cmd) {
  6255. while (msdc_app_cmd(host->mmc, host)) {
  6256. if (msdc_tune_cmdrsp(host)) {
  6257. ERR_MSG("failed to updata cmd para for app");
  6258. goto out;
  6259. }
  6260. }
  6261. }
  6262. if (!is_card_present(host))
  6263. goto out;
  6264. }
  6265. if ((host->read_time_tune)
  6266. && (cmd->opcode == MMC_READ_SINGLE_BLOCK
  6267. || cmd->opcode == MMC_READ_MULTIPLE_BLOCK)) {
  6268. host->read_time_tune = 0;
  6269. ERR_MSG("Read recover");
  6270. msdc_dump_trans_error(host, cmd, data, stop, sbc);
  6271. }
  6272. if ((host->write_time_tune)
  6273. && (cmd->opcode == MMC_WRITE_BLOCK
  6274. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)) {
  6275. host->write_time_tune = 0;
  6276. ERR_MSG("Write recover");
  6277. msdc_dump_trans_error(host, cmd, data, stop, sbc);
  6278. }
  6279. host->sw_timeout = 0;
  6280. if (host->hw->host_function == MSDC_SD)
  6281. host->continuous_fail_request_count = 0;
  6282. out:
  6283. msdc_reset_crc_tune_counter(host, ALL_TUNE_CNT);
  6284. #ifdef MTK_MSDC_USE_CACHE
  6285. if (g_flush_error_happend && (host->hw->host_function == MSDC_EMMC)
  6286. && host->mmc->card && (host->mmc->card->ext_csd.cache_ctrl & 0x1)) {
  6287. if ((cmd->opcode == MMC_SWITCH)
  6288. && (((cmd->arg >> 16) & 0xFF) == EXT_CSD_FLUSH_CACHE)
  6289. && (((cmd->arg >> 8) & 0x1))) {
  6290. g_flush_error_count++;
  6291. g_flush_error_happend = 0;
  6292. ERR_MSG("the %d time flush error happned, g_flush_data_size=%lld",
  6293. g_flush_error_count, g_flush_data_size);
  6294. /*
  6295. * if reinit emmc at resume,cache should not be enabled
  6296. * because too much flush error. so add cache quirk for this emmmc.
  6297. * if awake emmc at resume,cache should not be enabled
  6298. * because too much flush error, so force set cache_size=0
  6299. */
  6300. if (g_flush_error_count >= MSDC_MAX_FLUSH_COUNT) {
  6301. if (!msdc_cache_ctrl(host, 0, NULL)) {
  6302. g_emmc_cache_quirk[0] = emmc_id;
  6303. host->mmc->card->ext_csd.cache_size = 0;
  6304. }
  6305. pr_err("msdc%d:flush cache error count=%d,Disable cache\n",
  6306. host->id, g_flush_error_count);
  6307. }
  6308. }
  6309. }
  6310. #endif
  6311. #ifdef TUNE_FLOW_TEST
  6312. if (!is_card_sdio(host))
  6313. msdc_reset_para(host);
  6314. #endif
  6315. /* ==== when request done, check if app_cmd ==== */
  6316. if (mrq->cmd->opcode == MMC_APP_CMD) {
  6317. host->app_cmd = 1;
  6318. host->app_cmd_arg = mrq->cmd->arg; /* save the RCA */
  6319. } else {
  6320. host->app_cmd = 0;
  6321. /* host->app_cmd_arg = 0; */
  6322. }
  6323. host->mrq = NULL;
  6324. /* CCJ fix */
  6325. #if 0
  6326. /* === for sdio profile === */
  6327. if (sdio_pro_enable) {
  6328. if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53) {
  6329. /* GPT_GetCounter64(&new_L32, &new_H32); */
  6330. ticks = msdc_time_calc(old_L32, old_H32, new_L32, new_H32);
  6331. opcode = mrq->cmd->opcode;
  6332. if (mrq->cmd->data) {
  6333. sizes = mrq->cmd->data->blocks * mrq->cmd->data->blksz;
  6334. bRx = mrq->cmd->data->flags & MMC_DATA_READ ? 1 : 0;
  6335. } else {
  6336. bRx = mrq->cmd->arg & 0x80000000 ? 1 : 0;
  6337. }
  6338. if (!mrq->cmd->error)
  6339. msdc_performance(opcode, sizes, bRx, ticks);
  6340. }
  6341. }
  6342. #endif
  6343. msdc_gate_clock(host, 1); /* clear flag. */
  6344. spin_unlock(&host->lock);
  6345. mmc_request_done(mmc, mrq);
  6346. }
  6347. static void msdc_tune_async_request(struct mmc_host *mmc,
  6348. struct mmc_request *mrq)
  6349. {
  6350. struct msdc_host *host = mmc_priv(mmc);
  6351. struct mmc_command *cmd;
  6352. struct mmc_data *data;
  6353. struct mmc_command *stop = NULL;
  6354. struct mmc_command *sbc = NULL;
  6355. u32 status_verify = 0;
  6356. /* msdc_reset_crc_tune_counter(host,ALL_TUNE_CNT) */
  6357. if (host->mrq) {
  6358. #ifdef CONFIG_MTK_AEE_FEATURE
  6359. aee_kernel_warning("MSDC",
  6360. "MSDC request not clear.\n"
  6361. "host attached<0x%p> current<0x%p>\n", host->mrq, mrq);
  6362. #else
  6363. WARN_ON(host->mrq);
  6364. #endif
  6365. ERR_MSG("XXX host->mrq<0x%p> cmd<%d>arg<0x%x>",
  6366. host->mrq, host->mrq->cmd->opcode, host->mrq->cmd->arg);
  6367. if (host->mrq->data) {
  6368. ERR_MSG("XXX request data size<%d>",
  6369. host->mrq->data->blocks * host->mrq->data->blksz);
  6370. ERR_MSG("XXX request attach to host force data timeout and retry");
  6371. host->mrq->data->error = (unsigned int)-ETIMEDOUT;
  6372. } else {
  6373. ERR_MSG("XXX request attach to host force cmd timeout and retry");
  6374. host->mrq->cmd->error = (unsigned int)-ETIMEDOUT;
  6375. }
  6376. ERR_MSG("XXX current request <0x%p> cmd<%d>arg<0x%x>",
  6377. mrq, mrq->cmd->opcode, mrq->cmd->arg);
  6378. if (mrq->data)
  6379. ERR_MSG("XXX current request data size<%d>",
  6380. mrq->data->blocks * mrq->data->blksz);
  6381. }
  6382. if (!is_card_present(host) || host->power_mode == MMC_POWER_OFF) {
  6383. ERR_MSG("cmd<%d> arg<0x%x> card<%d> power<%d>",
  6384. mrq->cmd->opcode, mrq->cmd->arg,
  6385. is_card_present(host), host->power_mode);
  6386. mrq->cmd->error = (unsigned int)-ENOMEDIUM;
  6387. /* should call done for this request */
  6388. goto done;
  6389. }
  6390. cmd = mrq->cmd;
  6391. data = mrq->cmd->data;
  6392. if (data)
  6393. stop = data->stop;
  6394. #ifdef MTK_MSDC_USE_CMD23
  6395. if (data)
  6396. sbc = mrq->sbc;
  6397. #endif
  6398. /* start to process */
  6399. spin_lock(&host->lock);
  6400. /*if(host->error & REQ_CMD_EIO)
  6401. cmd->error = (unsigned int)-EIO;
  6402. else if(host->error & REQ_CMD_TMO)
  6403. cmd->error = (unsigned int)-ETIMEDOUT;
  6404. */
  6405. msdc_ungate_clock(host); /* set sw flag */
  6406. host->tune = 1;
  6407. host->mrq = mrq;
  6408. /* because ISR executing time will be monitor, try to dump the info here. */
  6409. do {
  6410. msdc_dump_trans_error(host, cmd, data, stop, sbc);
  6411. /*if((host->t_counter.time_cmd % 16 == 15)
  6412. || (host->t_counter.time_read % 16 == 15)
  6413. || (host->t_counter.time_write % 16 == 15))
  6414. {
  6415. spin_unlock(&host->lock);
  6416. msleep(150);
  6417. ERR_MSG("sleep 150ms here!");
  6418. spin_lock(&host->lock);
  6419. goto out;
  6420. } */
  6421. #ifdef MTK_MSDC_USE_CMD23
  6422. if ((sbc != NULL) && (sbc->error == (unsigned int)-ETIMEDOUT)) {
  6423. if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  6424. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  6425. /* not tuning, go out directly */
  6426. pr_err("===[%s:%d]==cmd23 timeout==\n", __func__, __LINE__);
  6427. goto out;
  6428. }
  6429. }
  6430. #endif
  6431. #ifdef MTK_MSDC_USE_CMD23
  6432. /* cmd->error also set when autocmd23 crc error */
  6433. if ((cmd->error == (unsigned int)-EIO)
  6434. || (stop && (stop->error == (unsigned int)-EIO))
  6435. || (sbc && (sbc->error == (unsigned int)-EIO))) {
  6436. #else
  6437. if ((cmd->error == (unsigned int)-EIO)
  6438. || (stop && (stop->error == (unsigned int)-EIO))) {
  6439. #endif
  6440. if (msdc_tune_cmdrsp(host)) {
  6441. ERR_MSG("failed to updata cmd para");
  6442. goto out;
  6443. }
  6444. }
  6445. if (data && (data->error == (unsigned int)-EIO)) {
  6446. if ((host->id == 0) && (host->timing == MMC_TIMING_MMC_HS400)) {
  6447. if (emmc_hs400_tune_rw(host)) {
  6448. ERR_MSG("failed to updata write para");
  6449. goto out;
  6450. }
  6451. } else if (data->flags & MMC_DATA_READ) { /* read */
  6452. if (msdc_tune_read(host)) {
  6453. ERR_MSG("failed to updata read para");
  6454. goto out;
  6455. }
  6456. } else {
  6457. if (msdc_tune_write(host)) {
  6458. ERR_MSG("failed to updata write para");
  6459. goto out;
  6460. }
  6461. }
  6462. }
  6463. status_verify = msdc_status_verify(host, cmd);
  6464. if (MSDC_VERIFY_ERROR == status_verify) {
  6465. ERR_MSG("status verify failed");
  6466. /*data_abort = 1; */
  6467. if (host->hw->host_function == MSDC_SD)
  6468. goto out;
  6469. } else if (MSDC_VERIFY_NEED_NOT_TUNE == status_verify) {
  6470. /* clear the error condition. */
  6471. ERR_MSG("need not error tune");
  6472. cmd->error = 0;
  6473. goto out;
  6474. }
  6475. /* CMD TO -> not tuning. cmd->error also set when autocmd23 TO error */
  6476. if (cmd->error == (unsigned int)-ETIMEDOUT) {
  6477. if (cmd->opcode == MMC_READ_SINGLE_BLOCK
  6478. || cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  6479. || cmd->opcode == MMC_WRITE_BLOCK
  6480. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  6481. if ((host->sw_timeout)
  6482. || (++(host->rwcmd_time_tune) > MSDC_MAX_TIMEOUT_RETRY)) {
  6483. ERR_MSG
  6484. ("msdc%d exceed max r/w cmd timeout tune times(%d);"
  6485. " or SW timeout(%d),Power cycle",
  6486. host->id, host->rwcmd_time_tune, host->sw_timeout);
  6487. if (!(host->sd_30_busy) && msdc_power_tuning(host))
  6488. goto out;
  6489. }
  6490. } else {
  6491. goto out;
  6492. }
  6493. }
  6494. if (cmd->error == (unsigned int)-ENOMEDIUM)
  6495. goto out;
  6496. /* [ALPS114710] Patch for data timeout issue. */
  6497. if (data && (data->error == (unsigned int)-ETIMEDOUT)) {
  6498. if (data->flags & MMC_DATA_READ) {
  6499. if (!(host->sw_timeout) &&
  6500. (host->hw->host_function == MSDC_SD)
  6501. && (host->sclk > 100000000)
  6502. && (host->read_timeout_uhs104 < MSDC_MAX_R_TIMEOUT_TUNE)) {
  6503. if (host->t_counter.time_read)
  6504. host->t_counter.time_read--;
  6505. host->read_timeout_uhs104++;
  6506. msdc_tune_read(host);
  6507. } else if ((host->sw_timeout)
  6508. || (host->read_timeout_uhs104 >= MSDC_MAX_R_TIMEOUT_TUNE)
  6509. || (++(host->read_time_tune) > MSDC_MAX_TIMEOUT_RETRY)) {
  6510. ERR_MSG
  6511. ("msdc%d exceed max read timeout retry times(%d) or ;"
  6512. "SW timeout(%d) or read timeout tune(%d),Power cycle",
  6513. host->id, host->read_time_tune, host->sw_timeout,
  6514. host->read_timeout_uhs104);
  6515. if (!(host->sd_30_busy) && msdc_power_tuning(host))
  6516. goto out;
  6517. }
  6518. } else if (data->flags & MMC_DATA_WRITE) {
  6519. if (!(host->sw_timeout) &&
  6520. (host->hw->host_function == MSDC_SD) &&
  6521. (host->sclk > 100000000) &&
  6522. (host->write_timeout_uhs104 < MSDC_MAX_W_TIMEOUT_TUNE)) {
  6523. if (host->t_counter.time_write)
  6524. host->t_counter.time_write--;
  6525. host->write_timeout_uhs104++;
  6526. msdc_tune_write(host);
  6527. } else if (!(host->sw_timeout)
  6528. && (host->hw->host_function == MSDC_EMMC) &&
  6529. (host->write_timeout_emmc < MSDC_MAX_W_TIMEOUT_TUNE_EMMC)) {
  6530. if (host->t_counter.time_write)
  6531. host->t_counter.time_write--;
  6532. host->write_timeout_emmc++;
  6533. if ((host->id == 0)
  6534. && (host->timing == MMC_TIMING_MMC_HS400))
  6535. emmc_hs400_tune_rw(host);
  6536. else
  6537. msdc_tune_write(host);
  6538. } else if ((host->hw->host_function == MSDC_SD) &&
  6539. ((host->sw_timeout)
  6540. || (host->write_timeout_uhs104 >= MSDC_MAX_W_TIMEOUT_TUNE)
  6541. || (++(host->write_time_tune) > MSDC_MAX_TIMEOUT_RETRY))) {
  6542. ERR_MSG
  6543. ("msdc%d exceed max write timeout retry times(%d) or ;"
  6544. "SW timeout(%d) or write timeout tune (%d),Power cycle"
  6545. , host->id, host->write_time_tune, host->sw_timeout,
  6546. host->write_timeout_uhs104);
  6547. if (!(host->sd_30_busy) && msdc_power_tuning(host))
  6548. goto out;
  6549. } else if ((host->hw->host_function == MSDC_EMMC)
  6550. && ((host->sw_timeout)
  6551. || (++(host->write_time_tune) > MSDC_MAX_TIMEOUT_RETRY_EMMC))) {
  6552. ERR_MSG
  6553. ("msdc%d exceed max write timeout retry times(%d) or ;"
  6554. "SW timeout(%d) or write timeout tune (%d),Power cycle"
  6555. , host->id, host->write_time_tune, host->sw_timeout,
  6556. host->write_timeout_emmc);
  6557. host->write_timeout_emmc = 0;
  6558. goto out;
  6559. }
  6560. }
  6561. }
  6562. /* clear the error condition. */
  6563. cmd->error = 0;
  6564. if (data)
  6565. data->error = 0;
  6566. if (stop)
  6567. stop->error = 0;
  6568. #ifdef MTK_MSDC_USE_CMD23
  6569. if (sbc)
  6570. sbc->error = 0;
  6571. #endif
  6572. host->sw_timeout = 0;
  6573. if (!is_card_present(host))
  6574. goto out;
  6575. } while (msdc_tune_rw_request(mmc, mrq));
  6576. if ((host->rwcmd_time_tune) && (cmd->opcode == MMC_READ_SINGLE_BLOCK
  6577. || cmd->opcode == MMC_READ_MULTIPLE_BLOCK
  6578. || cmd->opcode == MMC_WRITE_BLOCK
  6579. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)) {
  6580. host->rwcmd_time_tune = 0;
  6581. ERR_MSG("RW cmd recover");
  6582. msdc_dump_trans_error(host, cmd, data, stop, sbc);
  6583. }
  6584. if ((host->read_time_tune) && (cmd->opcode == MMC_READ_SINGLE_BLOCK
  6585. || cmd->opcode == MMC_READ_MULTIPLE_BLOCK)) {
  6586. host->read_time_tune = 0;
  6587. ERR_MSG("Read recover");
  6588. msdc_dump_trans_error(host, cmd, data, stop, sbc);
  6589. }
  6590. if ((host->write_time_tune) && (cmd->opcode == MMC_WRITE_BLOCK
  6591. || cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)) {
  6592. host->write_time_tune = 0;
  6593. ERR_MSG("Write recover");
  6594. msdc_dump_trans_error(host, cmd, data, stop, sbc);
  6595. }
  6596. host->power_cycle_enable = 1;
  6597. host->sw_timeout = 0;
  6598. if (host->hw->host_function == MSDC_SD)
  6599. host->continuous_fail_request_count = 0;
  6600. out:
  6601. if (host->sclk <= 50000000 && (host->timing != MMC_TIMING_UHS_DDR50))
  6602. host->sd_30_busy = 0;
  6603. msdc_reset_crc_tune_counter(host, ALL_TUNE_CNT);
  6604. host->mrq = NULL;
  6605. msdc_gate_clock(host, 1); /* clear flag. */
  6606. host->tune = 0;
  6607. spin_unlock(&host->lock);
  6608. done:
  6609. host->mrq_tune = NULL;
  6610. mmc_request_done(mmc, mrq);
  6611. }
  6612. /* new thread tune */
  6613. static void msdc_async_tune(struct work_struct *work)
  6614. {
  6615. struct msdc_host *host = NULL;
  6616. struct mmc_host *mmc = NULL;
  6617. host = container_of(work, struct msdc_host, work_tune);
  6618. BUG_ON(!host);
  6619. mmc = host->mmc;
  6620. BUG_ON(!mmc);
  6621. msdc_tune_async_request(mmc, host->mrq_tune);
  6622. }
  6623. static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
  6624. {
  6625. struct mmc_data *data;
  6626. int host_cookie = 0;
  6627. struct msdc_host *host = mmc_priv(mmc);
  6628. BUG_ON(mmc == NULL);
  6629. BUG_ON(mrq == NULL);
  6630. if ((host->hw->host_function == MSDC_SDIO) && !(host->trans_lock.active))
  6631. __pm_stay_awake(&host->trans_lock);
  6632. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT /* same as CONFIG_SDIOAUTOK_SUPPORT */
  6633. if (host->id == 2) /* 6630 in msdc2@Denali */
  6634. sdio_set_vcore_performance(host, 1);
  6635. #endif
  6636. data = mrq->data;
  6637. if (data)
  6638. host_cookie = data->host_cookie;
  6639. /*
  6640. * Asyn only support DMA and asyc CMD flow
  6641. * if cmd send error occur, dma not start yet, return error,
  6642. * msdc_tune_async_request() will call at msdc_ops_request
  6643. */
  6644. if (msdc_async_use_dma(host_cookie)) {
  6645. if (msdc_do_request_async(mmc, mrq))
  6646. msdc_tune_async_request(mmc, mrq);
  6647. } else
  6648. msdc_ops_request_legacy(mmc, mrq);
  6649. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT /* same as CONFIG_SDIOAUTOK_SUPPORT */
  6650. if (host->id == 2) { /* 6630 in msdc2@Denali */
  6651. sdio_set_vcore_performance(host, 0); /* disable */
  6652. }
  6653. #endif
  6654. if ((host->hw->host_function == MSDC_SDIO) && (host->trans_lock.active))
  6655. __pm_relax(&host->trans_lock);
  6656. }
  6657. /* called by ops.set_ios */
  6658. static void msdc_set_buswidth(struct msdc_host *host, u32 width)
  6659. {
  6660. void __iomem *base = host->base;
  6661. u32 val = sdr_read32(SDC_CFG);
  6662. val &= ~SDC_CFG_BUSWIDTH;
  6663. switch (width) {
  6664. default:
  6665. case MMC_BUS_WIDTH_1:
  6666. width = 1;
  6667. val |= (MSDC_BUS_1BITS << 16);
  6668. break;
  6669. case MMC_BUS_WIDTH_4:
  6670. val |= (MSDC_BUS_4BITS << 16);
  6671. break;
  6672. case MMC_BUS_WIDTH_8:
  6673. val |= (MSDC_BUS_8BITS << 16);
  6674. break;
  6675. }
  6676. sdr_write32(SDC_CFG, val);
  6677. N_MSG(CFG, "Bus Width = %d", width);
  6678. }
  6679. static void msdc_apply_ett_settings(struct msdc_host *host, int mode)
  6680. {
  6681. unsigned int i = 0;
  6682. void __iomem *base = host->base;
  6683. struct msdc_ett_settings *ett = NULL, *ett_item = NULL;
  6684. unsigned int ett_count = 0;
  6685. switch (emmc_id) {
  6686. #ifdef MSDC_SUPPORT_SANDISK_COMBO_ETT
  6687. case SANDISK_EMMC_CHIP:
  6688. pr_err("--- apply sandisk emmc ett settings\n");
  6689. host->hw->ett_hs200_settings = msdc0_ett_hs200_settings_for_sandisk;
  6690. host->hw->ett_hs400_settings = msdc0_ett_hs400_settings_for_sandisk;
  6691. break;
  6692. #endif
  6693. #ifdef MSDC_SUPPORT_SAMSUNG_COMBO_ETT
  6694. case SAMSUNG_EMMC_CHIP:
  6695. pr_err("--- apply samsung emmc ett settings\n");
  6696. host->hw->ett_hs200_settings = msdc0_ett_hs200_settings_for_samsung;
  6697. host->hw->ett_hs400_settings = msdc0_ett_hs400_settings_for_samsung;
  6698. break;
  6699. #endif
  6700. default:
  6701. pr_err("--- apply default emmc ett settings\n");
  6702. break;
  6703. }
  6704. if (MSDC_HS200_MODE == mode) {
  6705. ett_count = host->hw->ett_hs200_count;
  6706. ett = host->hw->ett_hs200_settings;
  6707. pr_err("[MSDC, %s] hs200 ett, ett_count=%d\n", __func__,
  6708. host->hw->ett_hs200_count);
  6709. } else if (MSDC_HS400_MODE == mode) {
  6710. /* clear hs200 setting */
  6711. ett_count = host->hw->ett_hs200_count;
  6712. ett = host->hw->ett_hs200_settings;
  6713. for (i = 0; i < ett_count; i++) {
  6714. ett_item = (struct msdc_ett_settings *)(ett + i);
  6715. sdr_set_field((base + ett_item->reg_addr), ett_item->reg_offset, 0);
  6716. }
  6717. ett_count = host->hw->ett_hs400_count;
  6718. ett = host->hw->ett_hs400_settings;
  6719. pr_err("[MSDC, %s] hs400 ett, ett_count=%d\n", __func__,
  6720. host->hw->ett_hs400_count);
  6721. }
  6722. for (i = 0; i < ett_count; i++) {
  6723. ett_item = (struct msdc_ett_settings *)(ett + i);
  6724. sdr_set_field((base + ett_item->reg_addr),
  6725. ett_item->reg_offset, ett_item->value);
  6726. pr_err("%s:msdc%d,reg[0x%x],offset[0x%x],val[0x%x],readback[0x%x]\n"
  6727. , __func__, host->id, ett_item->reg_addr, ett_item->reg_offset,
  6728. ett_item->value, sdr_read32(base + ett_item->reg_addr));
  6729. }
  6730. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  6731. host->saved_para.cmd_resp_ta_cntr);
  6732. host->saved_para.pad_tune0 = sdr_read32(MSDC_PAD_TUNE0);
  6733. host->saved_para.ddly0 = sdr_read32(MSDC_DAT_RDDLY0);
  6734. host->saved_para.ddly1 = sdr_read32(MSDC_DAT_RDDLY1);
  6735. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  6736. host->saved_para.wrdat_crc_ta_cntr);
  6737. if ((host->id == 0) && (mode == MSDC_HS400_MODE)) {
  6738. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1,
  6739. host->saved_para.ds_dly1);
  6740. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3,
  6741. host->saved_para.ds_dly3);
  6742. }
  6743. /* workaround for denali */
  6744. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, 0);
  6745. }
  6746. /* ops.set_ios */
  6747. static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  6748. {
  6749. struct msdc_host *host = mmc_priv(mmc);
  6750. spin_lock(&host->lock);
  6751. msdc_ungate_clock(host);
  6752. if (host->power_mode != ios->power_mode) {
  6753. switch (ios->power_mode) {
  6754. case MMC_POWER_OFF:
  6755. case MMC_POWER_UP:
  6756. spin_unlock(&host->lock);
  6757. msdc_init_hw(host);
  6758. msdc_set_power_mode(host, ios->power_mode);
  6759. spin_lock(&host->lock);
  6760. break;
  6761. case MMC_POWER_ON:
  6762. default:
  6763. break;
  6764. }
  6765. host->power_mode = ios->power_mode;
  6766. }
  6767. if (host->bus_width != ios->bus_width) {
  6768. msdc_set_buswidth(host, ios->bus_width);
  6769. host->bus_width = ios->bus_width;
  6770. }
  6771. if (host->timing != ios->timing) {
  6772. if (host->id == 0) {
  6773. if (ios->timing == MMC_TIMING_MMC_HS200) {
  6774. msdc_apply_ett_settings(host, MSDC_HS200_MODE);
  6775. } else if (ios->timing == MMC_TIMING_MMC_HS400) {
  6776. /* switch from eMMC 4.5 backward speed mode to HS400 */
  6777. emmc_hs400_backup();
  6778. msdc_apply_ett_settings(host, MSDC_HS400_MODE);
  6779. }
  6780. /* switch from HS400 to eMMC 4.5 backward speed mode */
  6781. if (host->timing == MMC_TIMING_MMC_HS400)
  6782. emmc_hs400_restore();
  6783. }
  6784. if (ios->timing == MMC_TIMING_MMC_DDR52)
  6785. msdc_set_mclk(host, ios->timing, ios->clock);
  6786. #ifdef CONFIG_MMC_FFU
  6787. if ((host->hw->host_function == MSDC_EMMC) &&
  6788. ((ios->timing == MMC_TIMING_LEGACY) && (ios->clock <= 25000000)))
  6789. emmc_clear_timing();
  6790. #endif
  6791. host->timing = ios->timing;
  6792. }
  6793. /* reserve for FFU */
  6794. #ifdef CONFIG_MMC_FFU
  6795. if ((ios->timing != MSDC_STATE_HS400) &&
  6796. (host->hw->host_function == MSDC_EMMC))
  6797. msdc_clock_src[host->id] = MSDC50_CLKSRC_200MHZ;
  6798. #endif
  6799. if (msdc_clock_src[host->id] != host->hw->clk_src) {
  6800. host->hw->clk_src = msdc_clock_src[host->id];
  6801. msdc_select_clksrc(host, host->hw->clk_src);
  6802. }
  6803. if (host->mclk != ios->clock) {
  6804. msdc_set_mclk(host, ios->timing, ios->clock);
  6805. host->mclk = ios->clock;
  6806. }
  6807. msdc_gate_clock(host, 1);
  6808. spin_unlock(&host->lock);
  6809. }
  6810. /* ops.get_ro */
  6811. static int msdc_ops_get_ro(struct mmc_host *mmc)
  6812. {
  6813. struct msdc_host *host = mmc_priv(mmc);
  6814. void __iomem *base = host->base;
  6815. unsigned long flags;
  6816. int ro = 0;
  6817. spin_lock_irqsave(&host->lock, flags);
  6818. msdc_ungate_clock(host);
  6819. if (host->hw->flags & MSDC_WP_PIN_EN) { /* set for card */
  6820. ro = (sdr_read32(MSDC_PS) >> 31);
  6821. }
  6822. msdc_gate_clock(host, 1);
  6823. spin_unlock_irqrestore(&host->lock, flags);
  6824. return ro;
  6825. }
  6826. /* ops.get_cd */
  6827. static int msdc_ops_get_cd(struct mmc_host *mmc)
  6828. {
  6829. struct msdc_host *host = mmc_priv(mmc);
  6830. void __iomem *base;
  6831. int level = 0;
  6832. /* int present = 1; */
  6833. base = host->base;
  6834. /* spin_lock_irqsave(&host->lock, flags); */
  6835. /* for sdio, depends on USER_RESUME */
  6836. if (is_card_sdio(host)) {
  6837. if (!(host->hw->flags & MSDC_SDIO_IRQ)) {
  6838. host->card_inserted =
  6839. (host->pm_state.event == PM_EVENT_USER_RESUME) ? 1 : 0;
  6840. /* pr_err("sdio ops_get_cd<%d>\n", host->card_inserted); */
  6841. goto end;
  6842. }
  6843. }
  6844. /* for emmc, MSDC_REMOVABLE not set, always return 1 */
  6845. if (mmc->caps & MMC_CAP_NONREMOVABLE) {
  6846. host->card_inserted = 1;
  6847. goto end;
  6848. }
  6849. /* msdc_ungate_clock(host); */
  6850. if (!(mmc->caps & MMC_CAP_NONREMOVABLE)) {
  6851. level = __gpio_get_value(cd_gpio);
  6852. if (host->hw->cd_level)
  6853. host->card_inserted = (level == 0) ? 0 : 1;
  6854. else
  6855. host->card_inserted = (level == 0) ? 1 : 0;
  6856. } else { /* TODO Check DAT3 pins for card detection */
  6857. host->card_inserted = 1;
  6858. }
  6859. /* host->card_inserted = 1; */
  6860. #if 0
  6861. if (host->card_inserted == 0)
  6862. msdc_gate_clock(host, 0);
  6863. else
  6864. msdc_gate_clock(host, 1);
  6865. #endif
  6866. if (host->hw->host_function == MSDC_SD && host->block_bad_card)
  6867. host->card_inserted = 0;
  6868. pr_debug("Card insert<%d> Block bad card<%d>\n", host->card_inserted,
  6869. host->block_bad_card);
  6870. end:
  6871. /* enable msdc register dump */
  6872. sd_register_zone[host->id] = 1;
  6873. /* spin_unlock_irqrestore(&host->lock, flags); */
  6874. return host->card_inserted;
  6875. }
  6876. static void msdc_ops_card_event(struct mmc_host *mmc)
  6877. {
  6878. struct msdc_host *host = mmc_priv(mmc);
  6879. host->block_bad_card = 0;
  6880. msdc_reset_pwr_cycle_counter(host);
  6881. msdc_reset_crc_tune_counter(host, ALL_TUNE_CNT);
  6882. msdc_reset_tmo_tune_counter(host, ALL_TUNE_CNT);
  6883. /* check cd first */
  6884. msdc_ops_get_cd(mmc);
  6885. /* when detect card, cmd13 will be sent which timeout log is not needed */
  6886. sd_register_zone[host->id] = 0;
  6887. }
  6888. /* ops.enable_sdio_irq */
  6889. static void msdc_ops_enable_sdio_irq(struct mmc_host *mmc, int enable)
  6890. {
  6891. struct msdc_host *host = mmc_priv(mmc);
  6892. struct msdc_hw *hw = host->hw;
  6893. void __iomem *base = host->base;
  6894. unsigned long flags;
  6895. if (hw->flags & MSDC_EXT_SDIO_IRQ) { /* yes for sdio */
  6896. if (enable)
  6897. hw->enable_sdio_eirq(); /* combo_sdio_enable_eirq */
  6898. else
  6899. hw->disable_sdio_eirq(); /* combo_sdio_disable_eirq */
  6900. } else if (hw->flags & MSDC_SDIO_IRQ) {
  6901. spin_lock_irqsave(&host->sdio_irq_lock, flags);
  6902. if (enable) {
  6903. #if (MSDC_DATA1_INT == 1)
  6904. while (1) {
  6905. sdr_set_bits(MSDC_INTEN, MSDC_INT_SDIOIRQ);
  6906. pr_debug("@#0x%08x @e >%d<\n", (sdr_read32(MSDC_INTEN)),
  6907. host->mmc->sdio_irq_pending);
  6908. if ((sdr_read32(MSDC_INTEN) & MSDC_INT_SDIOIRQ) == 0)
  6909. pr_debug("Should never ever get into this >%d<\n",
  6910. host->mmc->sdio_irq_pending);
  6911. else
  6912. break;
  6913. }
  6914. #endif
  6915. } else {
  6916. #if (MSDC_DATA1_INT == 1)
  6917. sdr_clr_bits(MSDC_INTEN, MSDC_INT_SDIOIRQ);
  6918. pr_debug("@#0x%08x @d\n", (sdr_read32(MSDC_INTEN)));
  6919. #endif
  6920. }
  6921. spin_unlock_irqrestore(&host->sdio_irq_lock, flags);
  6922. }
  6923. }
  6924. static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios)
  6925. {
  6926. struct msdc_host *host = mmc_priv(mmc);
  6927. void __iomem *base = host->base;
  6928. int err = 0;
  6929. u32 timeout = 100;
  6930. u32 retry = 10;
  6931. u32 status;
  6932. if (host->hw->host_function == MSDC_EMMC)
  6933. return 0;
  6934. if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330) {
  6935. /* make sure SDC is not busy (TBC) */
  6936. /* WAIT_COND(!SDC_IS_BUSY(), timeout, timeout); */
  6937. err = (unsigned int)-EIO;
  6938. msdc_retry(sdc_is_busy(), retry, timeout, host->id);
  6939. if (timeout == 0 && retry == 0) {
  6940. err = (unsigned int)-ETIMEDOUT;
  6941. goto out;
  6942. }
  6943. /* pull up disabled CMD and DAT[3:0] to allow card drives them to low */
  6944. /* check if CMD/DATA lines both 0 */
  6945. if ((sdr_read32(MSDC_PS) & ((1 << 24) | (0xF << 16))) == 0) {
  6946. /* pull up disabled in CMD and DAT[3:0] */
  6947. msdc_pin_config(host, MSDC_PIN_PULL_NONE);
  6948. /* change signal from 3.3v to 1.8v for FPGA this can not work */
  6949. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
  6950. #ifdef FPGA_PLATFORM
  6951. hwPowerSwitch_fpga();
  6952. #else
  6953. if (host->power_switch)
  6954. host->power_switch(host, 1);
  6955. else
  6956. ERR_MSG("[%s]msdc%d ERROR: No power switch callback,L%d\n",
  6957. __func__, host->id, __LINE__);
  6958. #endif
  6959. }
  6960. /* wait at least 5ms for 1.8v signal switching in card */
  6961. mdelay(10);
  6962. /* config clock 10~12MHz mode for volt switch detection by host*/
  6963. /*For FPGA 13MHz clock,this not work */
  6964. msdc_set_mclk(host, MMC_TIMING_LEGACY, 260000);
  6965. /* pull up enabled in CMD and DAT[3:0] */
  6966. msdc_pin_config(host, MSDC_PIN_PULL_UP);
  6967. mdelay(105);
  6968. /* start to detect volt change by providing 1.8v signal to card */
  6969. sdr_set_bits(MSDC_CFG, MSDC_CFG_BV18SDT);
  6970. /* wait at max. 1ms */
  6971. mdelay(1);
  6972. /* ERR_MSG("before read status"); */
  6973. while ((status = sdr_read32(MSDC_CFG)) & MSDC_CFG_BV18SDT)
  6974. ;
  6975. if (status & MSDC_CFG_BV18PSS)
  6976. err = 0;
  6977. }
  6978. }
  6979. out:
  6980. return err;
  6981. }
  6982. int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  6983. {
  6984. #ifdef CONFIG_SDIOAUTOK_SUPPORT
  6985. struct msdc_host *host = mmc_priv(mmc);
  6986. if ((host->hw->host_function == MSDC_SDIO) && (host->id == 2))
  6987. init_tune_sdio(host);
  6988. #endif
  6989. return 0;
  6990. }
  6991. static int msdc_card_busy(struct mmc_host *mmc)
  6992. {
  6993. struct msdc_host *host = mmc_priv(mmc);
  6994. void __iomem *base = host->base;
  6995. u32 status = sdr_read32(MSDC_PS);
  6996. /* check if any pin between dat[0:3] is low */
  6997. if (((status >> 16) & 0xf) != 0xf)
  6998. return 1;
  6999. return 0;
  7000. }
  7001. static struct mmc_host_ops mt_msdc_ops = {
  7002. .post_req = msdc_post_req,
  7003. .pre_req = msdc_pre_req,
  7004. .request = msdc_ops_request,
  7005. .set_ios = msdc_ops_set_ios,
  7006. .get_ro = msdc_ops_get_ro,
  7007. .get_cd = msdc_ops_get_cd,
  7008. .card_event = msdc_ops_card_event,
  7009. .enable_sdio_irq = msdc_ops_enable_sdio_irq,
  7010. .start_signal_voltage_switch = msdc_ops_switch_volt,
  7011. .execute_tuning = msdc_execute_tuning,
  7012. .card_busy = msdc_card_busy,
  7013. };
  7014. /*--------------------------------------------------------------------------*/
  7015. /* interrupt handler */
  7016. /*--------------------------------------------------------------------------*/
  7017. /* static __tcmfunc irqreturn_t msdc_irq(int irq, void *dev_id) */
  7018. static irqreturn_t msdc_irq(int irq, void *dev_id)
  7019. {
  7020. struct msdc_host *host = (struct msdc_host *)dev_id;
  7021. struct mmc_host *mmc = host->mmc;
  7022. struct mmc_data *data = host->data;
  7023. struct mmc_command *cmd = host->cmd;
  7024. struct mmc_command *stop = NULL;
  7025. struct mmc_request *mrq = host->mrq;
  7026. void __iomem *base = host->base;
  7027. u32 cmd_arg = host->mrq->cmd->arg;
  7028. u32 cmdsts = MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO | MSDC_INT_CMDRDY |
  7029. MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | MSDC_INT_ACMDRDY
  7030. | MSDC_INT_ACMD19_DONE;
  7031. u32 datsts = MSDC_INT_DATCRCERR | MSDC_INT_DATTMO;
  7032. u32 intsts, inten;
  7033. if (host->hw->flags & MSDC_SDIO_IRQ)
  7034. spin_lock(&host->sdio_irq_lock); /* ccyeh */
  7035. if (0 == host->core_clkon) {
  7036. #ifndef FPGA_PLATFORM
  7037. #ifdef CONFIG_MTK_CLKMGR
  7038. enable_clock(MT_CG_PERI_MSDC30_0 + host->id, "SD");
  7039. #else
  7040. clk_enable(host->clock_control);
  7041. #endif
  7042. #endif
  7043. host->core_clkon = 1;
  7044. sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC);
  7045. intsts = sdr_read32(MSDC_INT);
  7046. #if 0
  7047. if (sdr_read32(MSDC_ECO_VER) >= 4) {
  7048. sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC); /* E2 */
  7049. intsts = sdr_read32(MSDC_INT);
  7050. sdr_set_field(MSDC_CLKSRC_REG, MSDC1_IRQ_SEL, 0);
  7051. } else {
  7052. intsts = sdr_read32(MSDC_INT);
  7053. }
  7054. #endif
  7055. } else {
  7056. intsts = sdr_read32(MSDC_INT);
  7057. }
  7058. latest_int_status[host->id] = intsts;
  7059. inten = sdr_read32(MSDC_INTEN);
  7060. #if (MSDC_DATA1_INT == 1)
  7061. if (host->hw->flags & MSDC_SDIO_IRQ) {
  7062. intsts &= inten;
  7063. } else
  7064. #endif
  7065. {
  7066. inten &= intsts;
  7067. }
  7068. sdr_write32(MSDC_INT, intsts); /* clear interrupts */
  7069. /* MSG will cause fatal error */
  7070. #if 0
  7071. /* card change interrupt */
  7072. if (intsts & MSDC_INT_CDSC) {
  7073. IRQ_MSG("MSDC_INT_CDSC irq<0x%.8x>", intsts);
  7074. tasklet_hi_schedule(&host->card_tasklet);
  7075. /* tuning when plug card ? */
  7076. }
  7077. #endif
  7078. /* sdio interrupt */
  7079. if (host->hw->flags & MSDC_SDIO_IRQ) {
  7080. spin_unlock(&host->sdio_irq_lock); /* ccyeh */
  7081. #if (MSDC_DATA1_INT == 1)
  7082. if (intsts & MSDC_INT_SDIOIRQ)
  7083. mmc_signal_sdio_irq(host->mmc);
  7084. #endif
  7085. }
  7086. /* transfer complete interrupt */
  7087. if (data != NULL) {
  7088. #ifdef MTK_MSDC_ERROR_TUNE_DEBUG
  7089. if (g_err_tune_dbg_error &&
  7090. (g_err_tune_dbg_count > 0)
  7091. && (g_err_tune_dbg_host == host->id)) {
  7092. if (g_err_tune_dbg_cmd == (sdr_read32(SDC_CMD) & 0x3f)) {
  7093. if (g_err_tune_dbg_error & MTK_MSDC_ERROR_DAT_TMO) {
  7094. intsts = MSDC_INT_DATTMO;
  7095. g_err_tune_dbg_count--;
  7096. } else if (g_err_tune_dbg_error & MTK_MSDC_ERROR_DAT_CRC) {
  7097. intsts = MSDC_INT_DATCRCERR;
  7098. g_err_tune_dbg_count--;
  7099. }
  7100. pr_err("%s:make error cmd:%d,arg=%d,error type=%d,count=%d\n",
  7101. __func__, g_err_tune_dbg_cmd, g_err_tune_dbg_arg,
  7102. g_err_tune_dbg_error, g_err_tune_dbg_count);
  7103. }
  7104. if ((g_err_tune_dbg_cmd == MMC_STOP_TRANSMISSION)
  7105. && stop && (host->autocmd & MSDC_AUTOCMD12)) {
  7106. if (g_err_tune_dbg_error & MTK_MSDC_ERROR_ACMD_TMO) {
  7107. intsts = MSDC_INT_ACMDTMO;
  7108. g_err_tune_dbg_count--;
  7109. } else if (g_err_tune_dbg_error & MTK_MSDC_ERROR_ACMD_CRC) {
  7110. intsts = MSDC_INT_ACMDCRCERR;
  7111. g_err_tune_dbg_count--;
  7112. }
  7113. pr_err("[%s]:make CMD12 error,error type=%d,count=%d\n",
  7114. __func__, g_err_tune_dbg_error, g_err_tune_dbg_count);
  7115. }
  7116. }
  7117. #endif
  7118. stop = data->stop;
  7119. #if (MSDC_DATA1_INT == 1)
  7120. if ((host->hw->flags & MSDC_SDIO_IRQ) && (intsts & MSDC_INT_XFER_COMPL))
  7121. goto done;
  7122. else
  7123. #endif
  7124. if (inten & MSDC_INT_XFER_COMPL)
  7125. goto done;
  7126. if (intsts & datsts) {
  7127. /* do basic reset, or stop command will sdc_busy */
  7128. if (intsts & MSDC_INT_DATTMO)
  7129. msdc_dump_info(host->id);
  7130. if (host->dma_xfer)
  7131. msdc_reset(host->id);
  7132. else
  7133. msdc_reset_hw(host->id);
  7134. atomic_set(&host->abort, 1); /* For PIO mode exit */
  7135. if (intsts & MSDC_INT_DATTMO) {
  7136. data->error = (unsigned int)-ETIMEDOUT;
  7137. ERR_MSG("XXX CMD<%d> Arg<0x%.8x> MSDC_INT_DATTMO",
  7138. host->mrq->cmd->opcode, host->mrq->cmd->arg);
  7139. } else if (intsts & MSDC_INT_DATCRCERR) {
  7140. data->error = (unsigned int)-EIO;
  7141. /*ERR_MSG("XXX CMD<%d> Arg<0x%.8x> MSDC_INT_DATCRCERR,SDC_DCRC_STS<0x%x>",
  7142. host->mrq->cmd->opcode, host->mrq->cmd->arg,
  7143. sdr_read32(SDC_DCRC_STS));*/
  7144. }
  7145. goto tune;
  7146. }
  7147. if ((stop != NULL) && (host->autocmd & MSDC_AUTOCMD12)
  7148. && (intsts & cmdsts)) {
  7149. if (intsts & MSDC_INT_ACMDRDY) {
  7150. u32 *arsp = &stop->resp[0];
  7151. *arsp = sdr_read32(SDC_ACMD_RESP);
  7152. CMD_MSG("CMD<12> @ addr<0x%8x> resp<0x%.8x>",
  7153. cmd_arg, stop->resp[0]);
  7154. } else if (intsts & MSDC_INT_ACMDCRCERR) {
  7155. stop->error = (unsigned int)-EIO;
  7156. host->error |= REQ_STOP_EIO;
  7157. if (host->dma_xfer)
  7158. msdc_reset(host->id);
  7159. else
  7160. msdc_reset_hw(host->id);
  7161. } else if (intsts & MSDC_INT_ACMDTMO) {
  7162. stop->error = (unsigned int)-ETIMEDOUT;
  7163. host->error |= REQ_STOP_TMO;
  7164. if (host->dma_xfer)
  7165. msdc_reset(host->id);
  7166. else
  7167. msdc_reset_hw(host->id);
  7168. }
  7169. if ((intsts & MSDC_INT_ACMDCRCERR) || (intsts & MSDC_INT_ACMDTMO))
  7170. goto tune;
  7171. }
  7172. }
  7173. /* command interrupts */
  7174. if ((cmd != NULL) && (intsts & cmdsts)) {
  7175. #ifdef MTK_MSDC_ERROR_TUNE_DEBUG
  7176. if (g_err_tune_dbg_error && (g_err_tune_dbg_count > 0)
  7177. && (g_err_tune_dbg_host == host->id)
  7178. && (g_err_tune_dbg_cmd == cmd->opcode)) {
  7179. if ((g_err_tune_dbg_cmd != MMC_SWITCH)
  7180. || ((g_err_tune_dbg_cmd == MMC_SWITCH)
  7181. && (g_err_tune_dbg_arg == ((cmd->arg >> 16) & 0xff)))) {
  7182. if (g_err_tune_dbg_error & MTK_MSDC_ERROR_CMD_TMO)
  7183. intsts = MSDC_INT_CMDTMO;
  7184. else if (g_err_tune_dbg_error & MTK_MSDC_ERROR_CMD_CRC)
  7185. intsts = MSDC_INT_RSPCRCERR;
  7186. g_err_tune_dbg_count--;
  7187. pr_debug("%s:make error cmd:%d,arg=%d,error type=%d,count=%d\n",
  7188. __func__, g_err_tune_dbg_cmd, g_err_tune_dbg_arg,
  7189. g_err_tune_dbg_error, g_err_tune_dbg_count);
  7190. }
  7191. }
  7192. #endif
  7193. if (intsts & MSDC_INT_CMDRDY) {
  7194. u32 *rsp = NULL;
  7195. rsp = &cmd->resp[0];
  7196. switch (host->cmd_rsp) {
  7197. case RESP_NONE:
  7198. break;
  7199. case RESP_R2:
  7200. *rsp++ = sdr_read32(SDC_RESP3);
  7201. *rsp++ = sdr_read32(SDC_RESP2);
  7202. *rsp++ = sdr_read32(SDC_RESP1);
  7203. *rsp++ = sdr_read32(SDC_RESP0);
  7204. break;
  7205. default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  7206. *rsp = sdr_read32(SDC_RESP0);
  7207. break;
  7208. }
  7209. if (host->hw->host_function == MSDC_SD)
  7210. host->continuous_fail_request_count = 0;
  7211. } else if (intsts & MSDC_INT_RSPCRCERR) {
  7212. cmd->error = (unsigned int)-EIO;
  7213. ERR_MSG("XXX CMD<%d> MSDC_INT_RSPCRCERR Arg<0x%.8x>",
  7214. cmd->opcode, cmd->arg);
  7215. msdc_reset_hw(host->id);
  7216. } else if (intsts & MSDC_INT_CMDTMO) {
  7217. cmd->error = (unsigned int)-ETIMEDOUT;
  7218. ERR_MSG("XXX CMD<%d> MSDC_INT_CMDTMO Arg<0x%.8x>",
  7219. cmd->opcode, cmd->arg);
  7220. msdc_reset_hw(host->id);
  7221. }
  7222. if (intsts & (MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO))
  7223. complete(&host->cmd_done);
  7224. }
  7225. /* mmc irq interrupts */
  7226. if (intsts & MSDC_INT_MMCIRQ)
  7227. /* pr_debug("msdc[%d] MMCIRQ: SDC_CSTS=0x%.8x\r\n",
  7228. host->id, sdr_read32(SDC_CSTS)); */
  7229. latest_int_status[host->id] = 0;
  7230. return IRQ_HANDLED; /* only for normal cmd*/
  7231. done:
  7232. data->bytes_xfered = host->dma.xfersz;
  7233. /* if sync request or tune async request use host->xfer_done */
  7234. if (!(msdc_async_use_dma(data->host_cookie)) || !(host->tune == 0)) {
  7235. complete(&host->xfer_done);
  7236. } else {
  7237. msdc_dma_stop(host);
  7238. msdc_dma_clear(host);
  7239. mmc_request_done(mmc, mrq);
  7240. msdc_gate_clock(host, 1);
  7241. host->error &= ~REQ_DAT_ERR;
  7242. }
  7243. #if defined(FEATURE_MET_MMC_INDEX)
  7244. if ((data->mrq != NULL) && (data->mrq->cmd != NULL)) {
  7245. met_mmc_dma_stop(host->mmc, data->mrq->cmd->arg, data->blocks,
  7246. data->mrq->cmd->opcode, met_mmc_bdnum);
  7247. }
  7248. #endif
  7249. if (host->hw->host_function == MSDC_SD)
  7250. host->continuous_fail_request_count = 0;
  7251. return IRQ_HANDLED;
  7252. tune:
  7253. if (host->dma_xfer) {
  7254. if ((msdc_async_use_dma(data->host_cookie)) && (host->tune == 0)) {
  7255. msdc_dma_stop(host);
  7256. msdc_clr_fifo(host->id);
  7257. /*msdc_clr_int(); interrupt has been cleared before*/
  7258. /*if msdc_irq too fast to set mrq to host->areq at mmc_start_req */
  7259. host->mrq_tune = host->mrq;
  7260. msdc_dma_clear(host);
  7261. msdc_gate_clock(host, 1);
  7262. /*begin tune:dat/acmd crc/tmo for first time async request*/
  7263. if (!queue_work(wq_tune, &host->work_tune)) {
  7264. pr_err("msdc%d queue work failed BUG_ON,[%s]L:%d\n",
  7265. host->id, __func__, __LINE__);
  7266. BUG();
  7267. }
  7268. } else {
  7269. /* Autocmd12 issued but error, data transfer done INT will not issue,
  7270. * so cmplete is need here
  7271. */
  7272. complete(&host->xfer_done);
  7273. }
  7274. #if defined(FEATURE_MET_MMC_INDEX)
  7275. if ((data->mrq != NULL) && (data->mrq->cmd != NULL)) {
  7276. met_mmc_dma_stop(host->mmc, data->mrq->cmd->arg, data->blocks,
  7277. data->mrq->cmd->opcode, met_mmc_bdnum);
  7278. }
  7279. #endif
  7280. } /* PIO mode can't do complete, because not init */
  7281. return IRQ_HANDLED;
  7282. }
  7283. /*--------------------------------------------------------------------------*/
  7284. /* platform_driver members */
  7285. /*--------------------------------------------------------------------------*/
  7286. /* Add this function to check if no interrupt back after write. *
  7287. * It may occur when write crc revice, but busy over data->timeout_ns */
  7288. static void msdc_check_write_timeout(struct work_struct *work)
  7289. {
  7290. struct msdc_host *host =
  7291. container_of(work, struct msdc_host, write_timeout.work);
  7292. void __iomem *base = host->base;
  7293. struct mmc_data *data = host->data;
  7294. struct mmc_request *mrq = host->mrq;
  7295. struct mmc_host *mmc = host->mmc;
  7296. u32 status = 0;
  7297. u32 state = 0;
  7298. u32 err = 0;
  7299. unsigned long tmo;
  7300. u32 intsts;
  7301. if (!data || !mrq || !mmc)
  7302. return;
  7303. pr_err("[%s]: XXX DMA Data Write Busy Timeout: %u ms, CMD<%d>",
  7304. __func__, host->write_timeout_ms, mrq->cmd->opcode);
  7305. intsts = sdr_read32(MSDC_INT);
  7306. /* MSDC have received int,but delay by system. Just print warning */
  7307. if (intsts) {
  7308. pr_err("[%s]: Warning msdc%d ints are delayed by system, ints: %x\n",
  7309. __func__, host->id, intsts);
  7310. msdc_dump_info(host->id);
  7311. return;
  7312. }
  7313. if (msdc_async_use_dma(data->host_cookie) && (host->tune == 0)) {
  7314. msdc_dump_info(host->id);
  7315. msdc_dma_stop(host);
  7316. msdc_dma_clear(host);
  7317. msdc_reset_hw(host->id);
  7318. tmo = jiffies + POLLING_BUSY;
  7319. /* check the card state, try to bring back to trans state */
  7320. spin_lock(&host->lock);
  7321. do {
  7322. /* if anything wrong, let block driver do error
  7323. handling. */
  7324. err = msdc_get_card_status(mmc, host, &status);
  7325. if (err) {
  7326. ERR_MSG("CMD13 ERR<%d>", err);
  7327. break;
  7328. }
  7329. state = R1_CURRENT_STATE(status);
  7330. ERR_MSG("check card state<%d>", state);
  7331. if (state == R1_STATE_DATA || state == R1_STATE_RCV) {
  7332. ERR_MSG("state<%d> need cmd12 to stop", state);
  7333. msdc_send_stop(host);
  7334. } else if (state == R1_STATE_PRG) {
  7335. ERR_MSG("state<%d> card is busy", state);
  7336. spin_unlock(&host->lock);
  7337. msleep(100);
  7338. spin_lock(&host->lock);
  7339. }
  7340. if (time_after(jiffies, tmo)) {
  7341. ERR_MSG("card stuck in %d state, remove such bad card!" , state);
  7342. spin_unlock(&host->lock);
  7343. msdc_set_bad_card_and_remove(host);
  7344. spin_lock(&host->lock);
  7345. break;
  7346. }
  7347. } while (state != R1_STATE_TRAN);
  7348. spin_unlock(&host->lock);
  7349. data->error = (unsigned int)-ETIMEDOUT;
  7350. host->sw_timeout++;
  7351. if (mrq->done)
  7352. mrq->done(mrq);
  7353. msdc_gate_clock(host, 1);
  7354. host->error |= REQ_DAT_ERR;
  7355. } else {
  7356. /* do nothing, since legacy mode or async tuning
  7357. have it own timeout. */
  7358. /* complete(&host->xfer_done); */
  7359. }
  7360. }
  7361. /* called by msdc_drv_probe */
  7362. static void msdc_init_hw(struct msdc_host *host)
  7363. {
  7364. void __iomem *base = host->base;
  7365. struct msdc_hw *hw = host->hw;
  7366. u32 cur_rxdly0, cur_rxdly1;
  7367. /* Power on */
  7368. msdc_pin_reset(host, MSDC_PIN_PULL_UP);
  7369. /* Configure to MMC/SD mode */
  7370. sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC);
  7371. /* Reset */
  7372. msdc_reset_hw(host->id);
  7373. /* Disable card detection */
  7374. sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  7375. /* Disable and clear all interrupts */
  7376. sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
  7377. sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
  7378. /* reset tuning parameter */
  7379. #if defined(CONFIG_ARCH_MT6735) || defined(CONFIG_ARCH_MT6753) || defined(CONFIG_ARCH_MT6737T)
  7380. sdr_write32(MSDC_PAD_TUNE0, 0x00000000);
  7381. #else
  7382. sdr_write32(MSDC_PAD_TUNE0, 0x00008000);
  7383. #endif
  7384. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_DATWRDLY, hw->datwrddly);
  7385. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRRDLY, hw->cmdrrddly);
  7386. sdr_set_field(MSDC_PAD_TUNE0, MSDC_PAD_TUNE0_CMDRDLY, hw->cmdrddly);
  7387. sdr_write32(MSDC_IOCON, 0x00000000);
  7388. sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  7389. cur_rxdly0 = ((hw->dat0rddly & 0x1F) << 24) |
  7390. ((hw->dat1rddly & 0x1F) << 16) | ((hw->dat2rddly & 0x1F) << 8) |
  7391. ((hw->dat3rddly & 0x1F) << 0);
  7392. cur_rxdly1 = ((hw->dat4rddly & 0x1F) << 24) |
  7393. ((hw->dat5rddly & 0x1F) << 16) | ((hw->dat6rddly & 0x1F) << 8) |
  7394. ((hw->dat7rddly & 0x1F) << 0);
  7395. sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  7396. sdr_write32(MSDC_DAT_RDDLY1, cur_rxdly1);
  7397. /*lapm:bit6,7 must set to 1,if default not mach,20150515 */
  7398. sdr_write32(MSDC_PATCH_BIT1, 0xFFFE00C9);
  7399. host->saved_para.pad_tune0 = sdr_read32(MSDC_PAD_TUNE0);
  7400. host->saved_para.ddly0 = sdr_read32(MSDC_DAT_RDDLY0);
  7401. host->saved_para.ddly1 = sdr_read32(MSDC_DAT_RDDLY1);
  7402. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  7403. host->saved_para.cmd_resp_ta_cntr);
  7404. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  7405. host->saved_para.wrdat_crc_ta_cntr);
  7406. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_BUSY_MA,
  7407. host->saved_para.write_busy_margin);
  7408. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_GET_CRC_MA,
  7409. host->saved_para.write_crc_margin);
  7410. /* disable async fifo use interl delay */
  7411. sdr_clr_bits(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTS);
  7412. sdr_set_bits(MSDC_PATCH_BIT2, MSDC_PB2_CFGRESP);
  7413. sdr_get_field(MSDC_PATCH_BIT2, MSDC_PB2_CFGCRCSTS,
  7414. host->saved_para.cfg_crcsts_path);
  7415. sdr_get_field(MSDC_PATCH_BIT2, MSDC_PB2_CFGRESP,
  7416. host->saved_para.cfg_cmdrsp_path);
  7417. /* 64T + 48T cmd <-> resp */
  7418. sdr_set_field(MSDC_PATCH_BIT2, MSDC_PB2_RESPWAITCNT, 3);
  7419. sdr_get_field(MSDC_PATCH_BIT2, MSDC_PB2_RESPWAITCNT,
  7420. host->saved_para.resp_wait_cnt);
  7421. /* disable support 64G */
  7422. /* sdr_clr_bits(MSDC_PATCH_BIT2,MSDC_PB2_SUPPORT64G); */
  7423. if (is_card_sdio(host))
  7424. msdc_sdio_set_long_timing_delay_by_freq(host, 50 * 1000 * 1000);
  7425. if (host->id == 0) {
  7426. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY1,
  7427. host->saved_para.ds_dly1);
  7428. sdr_get_field(EMMC50_PAD_DS_TUNE, MSDC_EMMC50_PAD_DS_TUNE_DLY3,
  7429. host->saved_para.ds_dly3);
  7430. }
  7431. /* internal clock: latch read data, not apply to sdio */
  7432. if (!(is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ))) {
  7433. host->hw->cmd_edge = 0; /* tuning from 0 */
  7434. host->hw->rdata_edge = 0;
  7435. host->hw->wdata_edge = 0;
  7436. } else if (hw->flags & MSDC_INTERNAL_CLK) {
  7437. /* sdr_set_bits(MSDC_PATCH_BIT0, MSDC_PATCH_BIT_CKGEN_CK); */
  7438. }
  7439. /* for safety, should clear SDC_CFG.SDIO_INT_DET_EN & set SDC_CFG.SDIO in
  7440. * pre-loader,uboot,kernel drivers. and SDC_CFG.SDIO_INT_DET_EN will be only
  7441. * set when kernel driver wants to use SDIO bus interrupt */
  7442. /* Configure to enable SDIO mode. it's must otherwise sdio cmd5 failed */
  7443. sdr_set_bits(SDC_CFG, SDC_CFG_SDIO);
  7444. /* disable detect SDIO device interrupt function */
  7445. sdr_clr_bits(SDC_CFG, SDC_CFG_SDIOIDE);
  7446. #ifndef FPGA_PLATFORM
  7447. msdc_set_smt(host, 1);
  7448. msdc_set_driving(host, hw, 0);
  7449. #endif
  7450. /* write crc timeout detection */
  7451. sdr_set_field(MSDC_PATCH_BIT0, 1 << 30, 1);
  7452. /* Configure to default data timeout */
  7453. sdr_set_field(SDC_CFG, SDC_CFG_DTOC, DEFAULT_DTOC);
  7454. msdc_set_buswidth(host, MMC_BUS_WIDTH_1);
  7455. N_MSG(FUC, "init hardware done!");
  7456. }
  7457. /* called by msdc_drv_remove */
  7458. static void msdc_deinit_hw(struct msdc_host *host)
  7459. {
  7460. void __iomem *base = host->base;
  7461. /* Disable and clear all interrupts */
  7462. sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
  7463. sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
  7464. msdc_set_power_mode(host, MMC_POWER_OFF); /* make sure power down */
  7465. }
  7466. void msdc_dump_gpd_bd(int id)
  7467. {
  7468. struct msdc_host *host;
  7469. int i = 0;
  7470. struct gpd_t *gpd;
  7471. struct bd_t *bd;
  7472. if (id < 0 || id >= HOST_MAX_NUM) {
  7473. pr_err("[%s]: invalid host id: %d\n", __func__, id);
  7474. return;
  7475. }
  7476. host = mtk_msdc_host[id];
  7477. if (host == NULL) {
  7478. pr_err("[%s]: host0 or host0->dma is NULL\n", __func__);
  7479. return;
  7480. }
  7481. gpd = host->dma.gpd;
  7482. bd = host->dma.bd;
  7483. pr_err("========== MSDC GPD INFO ==========\n");
  7484. if (gpd == NULL) {
  7485. pr_err("GPD is NULL\n");
  7486. return;
  7487. }
  7488. pr_err("gpd addr:0x%lx\n", (ulong) (host->dma.gpd_addr));
  7489. pr_err("hwo:0x%x, bdp:0x%x, rsv0:0x%x, chksum:0x%x,intr:0x%x,rsv1:0x%x\n",
  7490. gpd->hwo, gpd->bdp, gpd->rsv0, gpd->chksum, gpd->intr, gpd->rsv1);
  7491. pr_err("nexth4:0x%x,ptrh4:0x%x, next:0x%x, ptr:0x%x, buflen:0x%x,\n",
  7492. (unsigned int)gpd->nexth4, (unsigned int)gpd->ptrh4,
  7493. (unsigned int)gpd->next, (unsigned int)gpd->ptr, gpd->buflen);
  7494. pr_err("extlen:0x%x, arg:0x%x,blknum:0x%x,cmd:0x%x\n",
  7495. gpd->extlen, gpd->arg, gpd->blknum, gpd->cmd);
  7496. pr_err("========== MSDC BD INFO ==========\n");
  7497. if (bd == NULL) {
  7498. pr_err("BD is NULL\n");
  7499. return;
  7500. }
  7501. pr_err("bd addr:0x%lx\n", (ulong) (host->dma.bd_addr));
  7502. for (i = 0; i < host->dma.sglen; i++) {
  7503. pr_err("the %d BD\n", i);
  7504. pr_err("eol:0x%x,rsv0:0x%x,chksum:0x%x,rsv1:0x%x,blkpad:0x%x\n",
  7505. bd->eol, bd->rsv0, bd->chksum, bd->rsv1, bd->blkpad);
  7506. pr_err("dwpad:0x%x,rsv2:0x%x,nexth4:0x%x, ptrh4:0x%x, next:0x%x\n",
  7507. bd->dwpad, bd->rsv2, (unsigned int)bd->nexth4,
  7508. (unsigned int)bd->ptrh4, (unsigned int)bd->next);
  7509. pr_err("ptr:0x%x,buflen:0x%x, rsv3:0x%x\n",
  7510. (unsigned int)bd->ptr, bd->buflen, bd->rsv3);
  7511. }
  7512. }
  7513. /* init gpd and bd list in msdc_drv_probe */
  7514. static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
  7515. {
  7516. struct gpd_t *gpd = dma->gpd;
  7517. struct bd_t *bd = dma->bd;
  7518. struct bd_t *ptr, *prev;
  7519. /* we just support one gpd */
  7520. int bdlen = MAX_BD_PER_GPD;
  7521. /* init the 2 gpd */
  7522. memset(gpd, 0, sizeof(struct gpd_t) * 2);
  7523. gpd->next = (u32) dma->gpd_addr + sizeof(struct gpd_t);
  7524. /* gpd->intr = 0; */
  7525. gpd->bdp = 1; /* hwo, cs, bd pointer */
  7526. /* gpd->ptr = (void*)virt_to_phys(bd); */
  7527. gpd->ptr = (u32) dma->bd_addr; /* physical address */
  7528. memset(bd, 0, sizeof(struct bd_t) * bdlen);
  7529. ptr = bd + bdlen - 1;
  7530. while (ptr != bd) {
  7531. prev = ptr - 1;
  7532. prev->next = ((u32) dma->bd_addr + sizeof(struct bd_t) * (ptr - bd));
  7533. ptr = prev;
  7534. }
  7535. }
  7536. #ifdef MSDC_DMA_ADDR_DEBUG
  7537. static void msdc_init_dma_latest_address(void)
  7538. {
  7539. struct dma_addr *ptr, *prev;
  7540. int bdlen = MAX_BD_PER_GPD;
  7541. memset(msdc_latest_dma_address, 0, sizeof(struct dma_addr) * bdlen);
  7542. ptr = msdc_latest_dma_address + bdlen - 1;
  7543. while (ptr != msdc_latest_dma_address) {
  7544. prev = ptr - 1;
  7545. prev->next = (void *)(msdc_latest_dma_address + sizeof(struct dma_addr)
  7546. * (ptr - msdc_latest_dma_address));
  7547. ptr = prev;
  7548. }
  7549. }
  7550. #endif
  7551. /* This is called by run_timer_softirq */
  7552. static void msdc_timer_pm(unsigned long data)
  7553. {
  7554. struct msdc_host *host = (struct msdc_host *)data;
  7555. unsigned long flags;
  7556. spin_lock_irqsave(&host->clk_gate_lock, flags);
  7557. if (host->clk_gate_count == 0) {
  7558. msdc_clksrc_onoff(host, 0);
  7559. N_MSG(CLK, "time out, dsiable clock, clk_gate_count=%d",
  7560. host->clk_gate_count);
  7561. }
  7562. spin_unlock_irqrestore(&host->clk_gate_lock, flags);
  7563. }
  7564. #ifndef FPGA_PLATFORM
  7565. static void msdc_set_host_power_control(struct msdc_host *host)
  7566. {
  7567. switch (host->id) {
  7568. case 0:
  7569. if (MSDC_EMMC == host->hw->host_function) {
  7570. host->power_control = msdc_emmc_power;
  7571. } else {
  7572. ERR_MSG("Host function error.Please check host_function<%d>",
  7573. host->hw->host_function);
  7574. BUG();
  7575. }
  7576. break;
  7577. case 1:
  7578. if (MSDC_SD == host->hw->host_function) {
  7579. host->power_control = msdc_sd_power;
  7580. host->power_switch = msdc_sd_power_switch;
  7581. } else {
  7582. ERR_MSG("Host function error.Please check host_function<%d>",
  7583. host->hw->host_function);
  7584. BUG();
  7585. }
  7586. break;
  7587. case 2:
  7588. if (MSDC_SDIO == host->hw->host_function) {
  7589. host->power_control = msdc_sdio_power;
  7590. } else {
  7591. ERR_MSG("Host function error,Please check host_function<%d>",
  7592. host->hw->host_function);
  7593. BUG();
  7594. }
  7595. break;
  7596. default:
  7597. break;
  7598. }
  7599. }
  7600. #endif /* end of FPGA_PLATFORM */
  7601. void SRC_trigger_signal(int i_on)
  7602. {
  7603. if ((ghost != NULL) && (ghost->hw->flags & MSDC_SDIO_IRQ)) {
  7604. pr_debug("msdc2 SRC_trigger_signal %d\n", i_on);
  7605. src_clk_control = i_on;
  7606. if (src_clk_control) {
  7607. msdc_clksrc_onoff(ghost, 1);
  7608. /* mb(); */
  7609. /* if (ghost->mmc->sdio_irq_thread) */
  7610. if (ghost->mmc->sdio_irq_thread &&
  7611. (!atomic_read(&ghost->mmc->sdio_irq_thread_abort))) {
  7612. mmc_signal_sdio_irq(ghost->mmc);
  7613. if (u_msdc_irq_counter < 3)
  7614. pr_debug("msdc2 SRC_trigger_signal mmc_signal_sdio_irq\n");
  7615. }
  7616. /* pr_debug("msdc2 SRC_trigger_signal ghost->id=%d\n",ghost->id); */
  7617. }
  7618. }
  7619. }
  7620. EXPORT_SYMBOL(SRC_trigger_signal);
  7621. #ifdef CONFIG_MTK_HIBERNATION
  7622. int msdc_drv_pm_restore_noirq(struct device *device)
  7623. {
  7624. struct platform_device *pdev = to_platform_device(device);
  7625. struct mmc_host *mmc = NULL;
  7626. struct msdc_host *host = NULL;
  7627. u32 l_polarity = 0;
  7628. BUG_ON(pdev == NULL);
  7629. mmc = platform_get_drvdata(pdev);
  7630. host = mmc_priv(mmc);
  7631. if (host->hw->host_function == MSDC_SD) {
  7632. if ((host->id == 1) && (!(mmc->caps & MMC_CAP_NONREMOVABLE))) {
  7633. l_polarity = mt_eint_get_polarity_external(mmc->slot.cd_irq);
  7634. #ifdef MT_POLARITY_LOW
  7635. if (l_polarity == MT_POLARITY_LOW)
  7636. host->sd_cd_polarity = 0;
  7637. else
  7638. host->sd_cd_polarity = 1;
  7639. #endif
  7640. if (!(host->hw->cd_level ^ host->sd_cd_polarity)
  7641. && host->mmc->card) {
  7642. mmc_card_set_removed(host->mmc->card);
  7643. host->card_inserted = 0;
  7644. }
  7645. } else if ((host->id == 2) && (!(mmc->caps & MMC_CAP_NONREMOVABLE))) {
  7646. /* sdio need handle here */
  7647. }
  7648. host->block_bad_card = 0;
  7649. }
  7650. return 0;
  7651. }
  7652. #endif
  7653. #ifndef CONFIG_MTK_CLKMGR
  7654. static int msdc_get_ccf_clk_pointer(struct platform_device *pdev,
  7655. struct msdc_host *host)
  7656. {
  7657. int ret = 0;
  7658. if (pdev->id == 0) {
  7659. host->clock_control = devm_clk_get(&pdev->dev, "MSDC0-CLOCK");
  7660. g_msdc0_pll_sel = devm_clk_get(&pdev->dev, "MSDC0_PLL_SEL");
  7661. g_msdc0_pll_800m = devm_clk_get(&pdev->dev, "MSDC0_PLL_800M");
  7662. g_msdc0_pll_400m = devm_clk_get(&pdev->dev, "MSDC0_PLL_400M");
  7663. g_msdc0_pll_200m = devm_clk_get(&pdev->dev, "MSDC0_PLL_200M");
  7664. } else if (pdev->id == 1) {
  7665. host->clock_control = devm_clk_get(&pdev->dev, "MSDC1-CLOCK");
  7666. } else if (pdev->id == 2) {
  7667. host->clock_control = devm_clk_get(&pdev->dev, "MSDC2-CLOCK");
  7668. } else if (pdev->id == 3) {
  7669. host->clock_control = devm_clk_get(&pdev->dev, "MSDC3-CLOCK");
  7670. }
  7671. if (IS_ERR(host->clock_control)) {
  7672. pr_err("can not get msdc%d clock control\n", pdev->id);
  7673. ret = 1;
  7674. goto out;
  7675. } else {
  7676. if (clk_prepare(host->clock_control)) {
  7677. pr_err("can not prepare msdc%d clock control\n", pdev->id);
  7678. ret = 1;
  7679. goto out;
  7680. }
  7681. }
  7682. if (host->id == 0) {
  7683. if (IS_ERR(g_msdc0_pll_sel) || IS_ERR(g_msdc0_pll_800m) ||
  7684. IS_ERR(g_msdc0_pll_400m) || IS_ERR(g_msdc0_pll_200m)) {
  7685. pr_err("msdc0 error,pll_sel=%p,pll_800=%p,pll_400=%p,pll_200=%p\n",
  7686. g_msdc0_pll_sel, g_msdc0_pll_800m, g_msdc0_pll_400m,
  7687. g_msdc0_pll_200m);
  7688. ret = 1;
  7689. goto out;
  7690. } else {
  7691. if (clk_prepare(g_msdc0_pll_sel)) {
  7692. pr_err("msdc%d can not prepare g_msdc0_pll_sel\n", pdev->id);
  7693. ret = 1;
  7694. goto out;
  7695. }
  7696. }
  7697. }
  7698. out:
  7699. return ret;
  7700. }
  7701. #endif
  7702. static int msdc_get_pinctl_settings(struct msdc_host *host)
  7703. {
  7704. struct mmc_host *mmc = host->mmc;
  7705. struct device_node *np = mmc->parent->of_node;
  7706. struct device_node *pinctl_node;
  7707. struct device_node *pins_cmd_node;
  7708. struct device_node *pins_dat_node;
  7709. struct device_node *pins_clk_node;
  7710. struct device_node *pins_rst_node;
  7711. struct device_node *pins_ds_node;
  7712. struct device_node *pinctl_sdr104_node;
  7713. struct device_node *pinctl_sdr50_node;
  7714. struct device_node *pinctl_ddr50_node;
  7715. /*parse pinctl settings*/
  7716. pinctl_node = of_parse_phandle(np, "pinctl", 0);
  7717. pins_cmd_node = of_get_child_by_name(pinctl_node, "pins_cmd");
  7718. of_property_read_u8(pins_cmd_node, "drive-strength", &host->hw->cmd_drv);
  7719. pins_dat_node = of_get_child_by_name(pinctl_node, "pins_dat");
  7720. of_property_read_u8(pins_dat_node, "drive-strength", &host->hw->dat_drv);
  7721. pins_clk_node = of_get_child_by_name(pinctl_node, "pins_clk");
  7722. of_property_read_u8(pins_clk_node, "drive-strength", &host->hw->clk_drv);
  7723. pins_rst_node = of_get_child_by_name(pinctl_node, "pins_rst");
  7724. of_property_read_u8(pins_rst_node, "drive-strength", &host->hw->rst_drv);
  7725. pins_ds_node = of_get_child_by_name(pinctl_node, "pins_ds");
  7726. of_property_read_u8(pins_ds_node, "drive-strength", &host->hw->ds_drv);
  7727. /******************************************************************************/
  7728. pinctl_sdr104_node = of_parse_phandle(np, "pinctl_sdr104", 0);
  7729. pins_cmd_node = of_get_child_by_name(pinctl_sdr104_node, "pins_cmd");
  7730. of_property_read_u8(pins_cmd_node, "drive-strength",
  7731. &host->hw->cmd_drv_sd_18);
  7732. pins_dat_node = of_get_child_by_name(pinctl_sdr104_node, "pins_dat");
  7733. of_property_read_u8(pins_dat_node, "drive-strength",
  7734. &host->hw->dat_drv_sd_18);
  7735. pins_clk_node = of_get_child_by_name(pinctl_sdr104_node, "pins_clk");
  7736. of_property_read_u8(pins_clk_node, "drive-strength",
  7737. &host->hw->clk_drv_sd_18);
  7738. /******************************************************************************/
  7739. pinctl_sdr50_node = of_parse_phandle(np, "pinctl_sdr50", 0);
  7740. pins_cmd_node = of_get_child_by_name(pinctl_sdr50_node, "pins_cmd");
  7741. of_property_read_u8(pins_cmd_node, "drive-strength",
  7742. &host->hw->cmd_drv_sd_18_sdr50);
  7743. pins_dat_node = of_get_child_by_name(pinctl_sdr50_node, "pins_dat");
  7744. of_property_read_u8(pins_dat_node, "drive-strength",
  7745. &host->hw->dat_drv_sd_18_sdr50);
  7746. pins_clk_node = of_get_child_by_name(pinctl_sdr50_node, "pins_clk");
  7747. of_property_read_u8(pins_clk_node, "drive-strength",
  7748. &host->hw->clk_drv_sd_18_sdr50);
  7749. /******************************************************************************/
  7750. pinctl_ddr50_node = of_parse_phandle(np, "pinctl_ddr50", 0);
  7751. pins_cmd_node = of_get_child_by_name(pinctl_ddr50_node, "pins_cmd");
  7752. of_property_read_u8(pins_cmd_node, "drive-strength",
  7753. &host->hw->cmd_drv_sd_18_ddr50);
  7754. pins_dat_node = of_get_child_by_name(pinctl_ddr50_node, "pins_dat");
  7755. of_property_read_u8(pins_dat_node, "drive-strength",
  7756. &host->hw->dat_drv_sd_18_ddr50);
  7757. pins_clk_node = of_get_child_by_name(pinctl_ddr50_node, "pins_clk");
  7758. of_property_read_u8(pins_clk_node, "drive-strength",
  7759. &host->hw->clk_drv_sd_18_ddr50);
  7760. return 0;
  7761. }
  7762. static void msdc_get_rigister_settings(struct msdc_host *host)
  7763. {
  7764. struct mmc_host *mmc = host->mmc;
  7765. struct device_node *np = mmc->parent->of_node;
  7766. struct device_node *register_setting_node = NULL;
  7767. /*parse hw property settings*/
  7768. register_setting_node = of_parse_phandle(np, "register_setting", 0);
  7769. if (register_setting_node) {
  7770. of_property_read_u8(register_setting_node, "dat0rddly", &host->hw->dat0rddly);
  7771. of_property_read_u8(register_setting_node, "dat1rddly", &host->hw->dat1rddly);
  7772. of_property_read_u8(register_setting_node, "dat2rddly", &host->hw->dat2rddly);
  7773. of_property_read_u8(register_setting_node, "dat3rddly", &host->hw->dat3rddly);
  7774. of_property_read_u8(register_setting_node, "dat4rddly", &host->hw->dat4rddly);
  7775. of_property_read_u8(register_setting_node, "dat5rddly", &host->hw->dat5rddly);
  7776. of_property_read_u8(register_setting_node, "dat6rddly", &host->hw->dat6rddly);
  7777. of_property_read_u8(register_setting_node, "dat7rddly", &host->hw->dat7rddly);
  7778. of_property_read_u8(register_setting_node, "datwrddly", &host->hw->datwrddly);
  7779. of_property_read_u8(register_setting_node, "cmdrrddly", &host->hw->cmdrrddly);
  7780. of_property_read_u8(register_setting_node, "cmdrddly", &host->hw->cmdrddly);
  7781. of_property_read_u8(register_setting_node, "cmd_edge", &host->hw->cmd_edge);
  7782. of_property_read_u8(register_setting_node, "rdata_edge", &host->hw->rdata_edge);
  7783. of_property_read_u8(register_setting_node, "wdata_edge", &host->hw->wdata_edge);
  7784. } else {
  7785. pr_err("[MSDC%d] register_setting is not found in DT.\n", host->id);
  7786. return;
  7787. }
  7788. /*parse ett*/
  7789. if (of_property_read_u32(register_setting_node, "ett-hs200-cells",
  7790. &host->hw->ett_hs200_count))
  7791. pr_err("[MSDC] ett-hs200-cells is not found in DT.\n");
  7792. host->hw->ett_hs200_settings =
  7793. kzalloc(sizeof(struct msdc_ett_settings) * host->hw->ett_hs200_count, GFP_KERNEL);
  7794. if (MSDC_EMMC == host->hw->host_function
  7795. && !of_property_read_u32_array(register_setting_node, "ett-hs200-customer",
  7796. (u32 *)host->hw->ett_hs200_settings, host->hw->ett_hs200_count * 3)) {
  7797. pr_err("[MSDC%d] hs200 ett setting for customer is found in DT.\n", host->id);
  7798. } else if (MSDC_EMMC == host->hw->host_function
  7799. && !of_property_read_u32_array(register_setting_node, "ett-hs200-default",
  7800. (u32 *)host->hw->ett_hs200_settings, host->hw->ett_hs200_count * 3)) {
  7801. pr_err("[MSDC%d] hs200 ett setting for default is found in DT.\n", host->id);
  7802. } else if (MSDC_EMMC == host->hw->host_function) {
  7803. pr_err("[MSDC%d]error: hs200 ett setting is not found in DT.\n", host->id);
  7804. }
  7805. if (of_property_read_u32(register_setting_node, "ett-hs400-cells",
  7806. &host->hw->ett_hs400_count))
  7807. pr_err("[MSDC] ett-hs400-cells is not found in DT.\n");
  7808. host->hw->ett_hs400_settings =
  7809. kzalloc(sizeof(struct msdc_ett_settings) * host->hw->ett_hs400_count, GFP_KERNEL);
  7810. if (MSDC_EMMC == host->hw->host_function
  7811. && !of_property_read_u32_array(register_setting_node, "ett-hs400-customer",
  7812. (u32 *)host->hw->ett_hs400_settings, host->hw->ett_hs400_count * 3)) {
  7813. pr_err("[MSDC%d] hs400 ett setting for customer is found in DT.\n", host->id);
  7814. } else if (MSDC_EMMC == host->hw->host_function
  7815. && !of_property_read_u32_array(register_setting_node, "ett-hs400-default",
  7816. (u32 *)host->hw->ett_hs400_settings, host->hw->ett_hs400_count * 3)) {
  7817. pr_err("[MSDC%d] hs400 ett setting for default is found in DT.\n", host->id);
  7818. } else if (MSDC_EMMC == host->hw->host_function) {
  7819. pr_err("[MSDC%d]error: hs400 ett setting is not found in DT.\n", host->id);
  7820. }
  7821. }
  7822. /**
  7823. * msdc_of_parse() - parse host's device-tree node
  7824. * @host: host whose node should be parsed.
  7825. *
  7826. */
  7827. int msdc_of_parse(struct mmc_host *mmc)
  7828. {
  7829. struct device_node *np;
  7830. struct msdc_host *host = mmc_priv(mmc);
  7831. int len;
  7832. if (!mmc->parent || !mmc->parent->of_node)
  7833. return 1;
  7834. np = mmc->parent->of_node;
  7835. host->mmc = mmc; /* msdc_check_init_done() need */
  7836. host->hw = kzalloc(sizeof(struct msdc_hw), GFP_KERNEL);
  7837. /*basic settings*/
  7838. if (0 == strcmp(np->name, "msdc0"))
  7839. host->id = 0;
  7840. else if (0 == strcmp(np->name, "msdc1"))
  7841. host->id = 1;
  7842. else if (0 == strcmp(np->name, "msdc2"))
  7843. host->id = 2;
  7844. else
  7845. host->id = 3;
  7846. pr_err("of msdc DT probe %s!\n", np->name);
  7847. /* iomap register */
  7848. host->base = of_iomap(np, 0);
  7849. if (!host->base) {
  7850. pr_err("can't of_iomap for msdc!!\n");
  7851. return -ENOMEM;
  7852. }
  7853. pr_err("of_iomap for msdc @ 0x%p\n", host->base);
  7854. /* get irq # */
  7855. host->irq = irq_of_parse_and_map(np, 0);
  7856. pr_err("msdc get irq # %d\n", host->irq);
  7857. BUG_ON(host->irq < 0);
  7858. /* get clk_src */
  7859. if (of_property_read_u8(np, "clk_src", &host->hw->clk_src))
  7860. pr_err("[MDSC%d] error: clk_src isn't found in DT.\n", host->id);
  7861. /* get msdc flag(caps)*/
  7862. if (of_find_property(np, "msdc-sys-suspend", &len))
  7863. host->hw->flags |= MSDC_SYS_SUSPEND;
  7864. if (of_find_property(np, "sd_need_power", &len))
  7865. host->hw->flags |= MSDC_SD_NEED_POWER;
  7866. /*Returns 0 on success, -EINVAL if the property does not exist,
  7867. * -ENODATA if property does not have a value, and -EOVERFLOW if the
  7868. * property data isn't large enough.*/
  7869. if (of_property_read_u8(np, "host_function", &host->hw->host_function))
  7870. pr_err("[MSDC%d] host_function isn't found in DT\n", host->id);
  7871. if (of_find_property(np, "bootable", &len))
  7872. host->hw->boot = 1;
  7873. /*get cd_level*/
  7874. of_property_read_u8(np, "cd_level", (u8 *)&host->hw->cd_level);
  7875. /*get cd_gpio*/
  7876. of_property_read_u32_index(np, "cd-gpios", 1, &cd_gpio);
  7877. msdc_get_rigister_settings(host);
  7878. msdc_get_pinctl_settings(host);
  7879. return 0;
  7880. }
  7881. static int msdc_drv_probe(struct platform_device *pdev)
  7882. {
  7883. struct mmc_host *mmc;
  7884. #ifndef CONFIG_MTK_LEGACY
  7885. struct device_node *msdc_node;
  7886. #endif
  7887. struct msdc_host *host;
  7888. void __iomem *base;
  7889. int ret;
  7890. struct irq_data l_irq_data;
  7891. #ifdef FPGA_PLATFORM
  7892. u16 l_val;
  7893. #endif
  7894. if (0 == strcmp(pdev->dev.of_node->name, "msdc2")) {
  7895. #ifndef CFG_DEV_MSDC2
  7896. return 1;
  7897. #endif
  7898. } else if (0 == strcmp(pdev->dev.of_node->name, "msdc3")) {
  7899. #ifndef CFG_DEV_MSDC3
  7900. return 1;
  7901. #endif
  7902. }
  7903. /* Allocate MMC host for this device */
  7904. mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
  7905. if (!mmc)
  7906. return -ENOMEM;
  7907. if (mmc_of_parse(mmc) || msdc_of_parse(mmc)) {
  7908. pr_err("DT happens error for msdc!!\n");
  7909. mmc_free_host(mmc);
  7910. return 1;
  7911. }
  7912. /*remove denali DDR mode*/
  7913. if ((0 == strcmp(pdev->dev.of_node->name, "msdc0"))
  7914. || (0 == strcmp(pdev->dev.of_node->name, "msdc1"))) {
  7915. mmc->caps &= ~MMC_CAP_UHS_DDR50;
  7916. mmc->caps &= ~MMC_CAP_1_8V_DDR;
  7917. mmc->caps &= ~MMC_CAP_1_2V_DDR;
  7918. }
  7919. host = mmc_priv(mmc);
  7920. base = host->base;
  7921. l_irq_data.irq = host->irq;
  7922. if (gpio_node == NULL) {
  7923. gpio_node = of_find_compatible_node(NULL, NULL, "mediatek,GPIO");
  7924. gpio_reg_base = of_iomap(gpio_node, 0);
  7925. pr_err("of_iomap for gpio base @ 0x%p\n", gpio_reg_base);
  7926. }
  7927. if (infracfg_ao_node == NULL) {
  7928. infracfg_ao_node = of_find_compatible_node(NULL, NULL,
  7929. "mediatek,INFRACFG_AO");
  7930. infracfg_ao_reg_base = of_iomap(infracfg_ao_node, 0);
  7931. pr_debug("of_iomap for infracfg_ao base @ 0x%p\n",
  7932. infracfg_ao_reg_base);
  7933. }
  7934. if (infracfg_node == NULL) {
  7935. infracfg_node = of_find_compatible_node(NULL, NULL,
  7936. "mediatek,INFRACFG");
  7937. infracfg_reg_base = of_iomap(infracfg_node, 0);
  7938. pr_debug("of_iomap for infracfg base @ 0x%p\n", infracfg_reg_base);
  7939. }
  7940. if (pericfg_node == NULL) {
  7941. pericfg_node = of_find_compatible_node(NULL, NULL, "mediatek,PERICFG");
  7942. pericfg_reg_base = of_iomap(pericfg_node, 0);
  7943. pr_debug("of_iomap for pericfg base @ 0x%p\n", pericfg_reg_base);
  7944. }
  7945. if (emi_node == NULL) {
  7946. emi_node = of_find_compatible_node(NULL, NULL, "mediatek,EMI");
  7947. emi_reg_base = of_iomap(emi_node, 0);
  7948. pr_debug("of_iomap for emi base @ 0x%p\n", emi_reg_base);
  7949. }
  7950. if (toprgu_node == NULL) {
  7951. toprgu_node = of_find_compatible_node(NULL, NULL, "mediatek,TOPRGU");
  7952. toprgu_reg_base = of_iomap(toprgu_node, 0);
  7953. pr_debug("of_iomap for toprgu base @ 0x%p\n", toprgu_reg_base);
  7954. }
  7955. if (apmixed_node == NULL) {
  7956. apmixed_node = of_find_compatible_node(NULL, NULL, "mediatek,APMIXED");
  7957. apmixed_reg_base1 = of_iomap(apmixed_node, 0);
  7958. pr_err("of_iomap for APMIXED base @ 0x%p\n", apmixed_reg_base1);
  7959. }
  7960. if (topckgen_node == NULL) {
  7961. topckgen_node = of_find_compatible_node(NULL, NULL, "mediatek,CKSYS");
  7962. topckgen_reg_base = of_iomap(topckgen_node, 0);
  7963. pr_err("of_iomap for TOPCKGEN base @ 0x%p\n", topckgen_reg_base);
  7964. }
  7965. #ifndef CONFIG_MTK_LEGACY
  7966. /* backup original dev.of_node */
  7967. msdc_node = pdev->dev.of_node;
  7968. /* get regulator supply node */
  7969. pdev->dev.of_node = of_find_compatible_node(NULL, NULL,
  7970. "mediatek,mt_pmic_regulator_supply");
  7971. if (reg_vemc_3v3 == NULL)
  7972. reg_vemc_3v3 = regulator_get(&(pdev->dev), "vemc_3v3");
  7973. if (reg_vmc == NULL)
  7974. reg_vmc = regulator_get(&(pdev->dev), "vmc");
  7975. if (reg_vmch == NULL)
  7976. reg_vmch = regulator_get(&(pdev->dev), "vmch");
  7977. /* restore original dev.of_node */
  7978. pdev->dev.of_node = msdc_node;
  7979. #endif
  7980. #ifdef FPGA_PLATFORM
  7981. if (fpga_pwr_gpio == NULL) {
  7982. fpga_pwr_gpio = of_iomap(pdev->dev.of_node, 1);
  7983. fpga_pwr_gpio_eo = fpga_pwr_gpio + 0x4;
  7984. pr_err("FPAG PWR_GPIO, PWR_GPIO_EO address 0x%p, 0x%p\n",
  7985. fpga_pwr_gpio, fpga_pwr_gpio_eo);
  7986. }
  7987. l_val = sdr_read16(PWR_GPIO_EO);
  7988. sdr_write16(PWR_GPIO_EO, (l_val | /* PWR_GPIO_L4_DIR | */
  7989. PWR_MASK_EN | PWR_MASK_VOL_33 | PWR_MASK_VOL_18));
  7990. l_val = sdr_read16(PWR_GPIO_EO);
  7991. pr_debug("[%s]: pwr gpio dir = 0x%x\n", __func__, l_val);
  7992. #endif
  7993. if (strcmp(pdev->dev.of_node->name, "msdc0") == 0)
  7994. pdev->id = 0;
  7995. else if (strcmp(pdev->dev.of_node->name, "msdc1") == 0)
  7996. pdev->id = 1;
  7997. #if defined(CFG_DEV_MSDC2)
  7998. if (strcmp(pdev->dev.of_node->name, "msdc2") == 0) {
  7999. host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
  8000. host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
  8001. /* FIXME: host->hw = &msdc2_hw; */
  8002. host->hw->clk_src = MSDC30_CLKSRC_200MHZ;
  8003. host->hw->cmd_edge = MSDC_SMPL_FALLING;
  8004. host->hw->rdata_edge = MSDC_SMPL_FALLING;
  8005. host->hw->wdata_edge = MSDC_SMPL_FALLING;
  8006. host->hw->clk_drv = 2;
  8007. host->hw->cmd_drv = 2;
  8008. host->hw->dat_drv = 2;
  8009. host->hw->data_pins = 4;
  8010. host->hw->data_offset = 0;
  8011. host->hw->flags = MSDC_EXT_SDIO_IRQ | MSDC_HIGHSPEED | MSDC_UHS1;
  8012. host->hw->dat0rddly = 0;
  8013. host->hw->dat1rddly = 0;
  8014. host->hw->dat2rddly = 0;
  8015. host->hw->dat3rddly = 0;
  8016. host->hw->dat4rddly = 0;
  8017. host->hw->dat5rddly = 0;
  8018. host->hw->dat6rddly = 0;
  8019. host->hw->dat7rddly = 0;
  8020. host->hw->datwrddly = 0;
  8021. host->hw->cmdrrddly = 0;
  8022. host->hw->cmdrddly = 0;
  8023. host->hw->cmdrtactr_sdr50 = 0x1;
  8024. host->hw->wdatcrctactr_sdr50 = 0x1;
  8025. host->hw->intdatlatcksel_sdr50 = 0x0;
  8026. host->hw->cmdrtactr_sdr200 = 0x3;
  8027. host->hw->wdatcrctactr_sdr200 = 0x3;
  8028. host->hw->intdatlatcksel_sdr200 = 0x0;
  8029. host->hw->host_function = MSDC_SDIO;
  8030. host->hw->boot = 0;
  8031. host->hw->request_sdio_eirq = mt_sdio_ops[2].sdio_request_eirq;
  8032. host->hw->enable_sdio_eirq = mt_sdio_ops[2].sdio_enable_eirq;
  8033. host->hw->disable_sdio_eirq = mt_sdio_ops[2].sdio_disable_eirq;
  8034. host->hw->register_pm = mt_sdio_ops[2].sdio_register_pm;
  8035. pdev->id = 2;
  8036. pr_debug("platform_data hw:0x%p @ msdc2_hw\n", host->hw);
  8037. }
  8038. #endif
  8039. #if defined(CFG_DEV_MSDC3)
  8040. if (strcmp(pdev->dev.of_node->name, "msdc3") == 0) {
  8041. host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
  8042. host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
  8043. host->hw->clk_src = MSDC30_CLKSRC_200MHZ;
  8044. host->hw->cmd_edge = MSDC_SMPL_RISING;
  8045. host->hw->rdata_edge = MSDC_SMPL_RISING;
  8046. host->hw->wdata_edge = MSDC_SMPL_RISING;
  8047. host->hw->clk_drv = 0;
  8048. host->hw->cmd_drv = 0;
  8049. host->hw->dat_drv = 0;
  8050. host->hw->data_pins = 4;
  8051. host->hw->data_offset = 0;
  8052. #ifdef C2K_USE_EINT
  8053. host->hw->flags = MSDC_EXT_SDIO_IRQ | MSDC_HIGHSPEED;
  8054. #else
  8055. host->hw->flags = MSDC_SDIO_IRQ | MSDC_HIGHSPEED;
  8056. #endif
  8057. host->hw->dat0rddly = 0;
  8058. host->hw->dat1rddly = 0;
  8059. host->hw->dat2rddly = 0;
  8060. host->hw->dat3rddly = 0;
  8061. host->hw->dat4rddly = 0;
  8062. host->hw->dat5rddly = 0;
  8063. host->hw->dat6rddly = 0;
  8064. host->hw->dat7rddly = 0;
  8065. host->hw->datwrddly = 0;
  8066. host->hw->cmdrrddly = 0;
  8067. host->hw->cmdrddly = 0;
  8068. host->hw->cmdrtactr_sdr50 = 0x0;
  8069. host->hw->wdatcrctactr_sdr50 = 0x0;
  8070. host->hw->intdatlatcksel_sdr50 = 0x0;
  8071. host->hw->cmdrtactr_sdr200 = 0x0;
  8072. host->hw->wdatcrctactr_sdr200 = 0x0;
  8073. host->hw->intdatlatcksel_sdr200 = 0x0;
  8074. host->hw->host_function = MSDC_SDIO;
  8075. host->hw->boot = 0;
  8076. #ifdef C2K_USE_EINT
  8077. host->hw->request_sdio_eirq = c2k_sdio_request_eirq;
  8078. host->hw->enable_sdio_eirq = c2k_sdio_enable_eirq;
  8079. host->hw->disable_sdio_eirq = c2k_sdio_disable_eirq;
  8080. #endif
  8081. host->hw->register_pm = c2k_sdio_register_pm;
  8082. pdev->id = 3;
  8083. pr_debug("platform_data hw:0x%p @ msdc3_hw\n", host->hw);
  8084. }
  8085. #endif
  8086. if ((pdev->id == 1) && (host->hw->host_function == MSDC_SD)
  8087. && (eint_node == NULL)) {
  8088. eint_node = of_find_compatible_node(NULL, NULL,
  8089. "mediatek, MSDC1_INS-eint");
  8090. if (eint_node) {
  8091. pr_debug("find MSDC1_INS-eint node!!\n");
  8092. /* get irq # */
  8093. if (!cd_irq)
  8094. cd_irq = irq_of_parse_and_map(eint_node, 0);
  8095. if (!cd_irq)
  8096. pr_debug("can't irq_of_parse_and_map for card detect eint!!\n");
  8097. else
  8098. pr_debug("msdc1 EINT get irq # %d\n", cd_irq);
  8099. } else
  8100. pr_debug("can't find MSDC1_INS-eint compatible node\n");
  8101. }
  8102. /* Set host parameters to mmc */
  8103. mmc->ops = &mt_msdc_ops;
  8104. mmc->f_min = HOST_MIN_MCLK;
  8105. mmc->ocr_avail = MSDC_OCR_AVAIL;
  8106. /* For sd card: MSDC_SYS_SUSPEND | MSDC_WP_PIN_EN | MSDC_CD_PIN_EN |
  8107. MSDC_REMOVABLE | MSDC_HIGHSPEED,
  8108. For sdio : MSDC_EXT_SDIO_IRQ | MSDC_HIGHSPEED */
  8109. if ((host->hw->flags & MSDC_SDIO_IRQ) || (host->hw->flags & MSDC_EXT_SDIO_IRQ))
  8110. mmc->caps |= MMC_CAP_SDIO_IRQ; /* yes for sdio */
  8111. #ifdef MTK_MSDC_USE_CMD23
  8112. if (host->hw->host_function == MSDC_EMMC)
  8113. mmc->caps |= MMC_CAP_ERASE | MMC_CAP_CMD23;
  8114. else
  8115. mmc->caps |= MMC_CAP_ERASE;
  8116. #else
  8117. mmc->caps |= MMC_CAP_ERASE;
  8118. #endif
  8119. mmc->max_busy_timeout = 0;
  8120. /* MMC core transfer sizes tunable parameters */
  8121. mmc->max_segs = MAX_HW_SGMTS;
  8122. if (host->hw->host_function == MSDC_SDIO)
  8123. mmc->max_seg_size = MAX_SGMT_SZ_SDIO;
  8124. else
  8125. mmc->max_seg_size = MAX_SGMT_SZ;
  8126. mmc->max_blk_size = HOST_MAX_BLKSZ;
  8127. mmc->max_req_size = MAX_REQ_SZ;
  8128. mmc->max_blk_count = MAX_REQ_SZ / 512; /*mmc->max_req_size; */
  8129. #ifndef FPGA_PLATFORM
  8130. if (pdev->id == 0)
  8131. hclks = hclks_msdc50;
  8132. else
  8133. hclks = hclks_msdc30;
  8134. #endif
  8135. host->error = 0;
  8136. /* mclk: the request clock of mmc sub-system */
  8137. host->mclk = 0;
  8138. /* hclk: clock of clock source to msdc controller */
  8139. host->hclk = hclks[host->hw->clk_src];
  8140. /* sclk: the really clock after divition */
  8141. host->sclk = 0;
  8142. host->pm_state = PMSG_RESUME;
  8143. host->suspend = 0;
  8144. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT /* same as CONFIG_SDIOAUTOK_SUPPORT */
  8145. host->sdio_performance_vcore = 0;
  8146. INIT_DELAYED_WORK(&(host->set_vcore_workq), sdio_unreq_vcore);
  8147. #endif
  8148. host->core_clkon = 0;
  8149. host->card_clkon = 0;
  8150. host->clk_gate_count = 0;
  8151. host->core_power = 0;
  8152. host->power_mode = MMC_POWER_OFF;
  8153. host->power_control = NULL;
  8154. host->power_switch = NULL;
  8155. #ifndef CONFIG_MTK_CLKMGR
  8156. if (msdc_get_ccf_clk_pointer(pdev, host))
  8157. return 1;
  8158. #endif
  8159. #ifndef FPGA_PLATFORM
  8160. msdc_set_host_power_control(host);
  8161. /* work around:hot-plug project SD card LDO alway on if no SD card insert */
  8162. if ((host->hw->host_function == MSDC_SD)
  8163. && (!(host->mmc->caps & MMC_CAP_NONREMOVABLE))) {
  8164. msdc_sd_power(host, 1);
  8165. msdc_sd_power(host, 0);
  8166. }
  8167. #endif
  8168. /*
  8169. * mmc_rescan if check host->caps & NONREMOVABLE not call host->ops->get_cd
  8170. * host->card_inserted cat not be set to 1 for eMMC, so set this flag here
  8171. */
  8172. host->card_inserted = (host->mmc->caps & MMC_CAP_NONREMOVABLE) ? 1 : 0;
  8173. host->timeout_ns = 0;
  8174. host->timeout_clks = DEFAULT_DTOC * 1048576;
  8175. #ifndef MTK_MSDC_USE_CMD23
  8176. if (host->hw->host_function != MSDC_SDIO)
  8177. host->autocmd |= MSDC_AUTOCMD12;
  8178. else
  8179. host->autocmd &= ~MSDC_AUTOCMD12;
  8180. #else
  8181. if (host->hw->host_function == MSDC_EMMC) {
  8182. host->autocmd &= ~MSDC_AUTOCMD12;
  8183. #if (1 == MSDC_USE_AUTO_CMD23)
  8184. host->autocmd |= MSDC_AUTOCMD23;
  8185. #endif
  8186. } else if (host->hw->host_function == MSDC_SD) {
  8187. host->autocmd |= MSDC_AUTOCMD12;
  8188. } else {
  8189. host->autocmd &= ~MSDC_AUTOCMD12;
  8190. }
  8191. #endif /* end of MTK_MSDC_USE_CMD23 */
  8192. #ifdef MTK_MSDC_USE_CACHE
  8193. if (host->hw->host_function == MSDC_EMMC)
  8194. msdc_set_cache_quirk(host);
  8195. #endif
  8196. host->mrq = NULL;
  8197. /* init_MUTEX(&host->sem); */
  8198. /* we don't need to support multiple threads access */
  8199. host->dma.used_gpd = 0;
  8200. host->dma.used_bd = 0;
  8201. /* using dma_alloc_coherent */
  8202. /* todo: using 1, for all 4 slots */
  8203. host->dma.gpd = dma_alloc_coherent(&pdev->dev,
  8204. MAX_GPD_NUM * sizeof(struct gpd_t), &host->dma.gpd_addr, GFP_KERNEL);
  8205. host->dma.bd = dma_alloc_coherent(&pdev->dev,
  8206. MAX_BD_NUM * sizeof(struct bd_t), &host->dma.bd_addr, GFP_KERNEL);
  8207. BUG_ON((!host->dma.gpd) || (!host->dma.bd));
  8208. msdc_init_gpd_bd(host, &host->dma);
  8209. msdc_clock_src[host->id] = host->hw->clk_src;
  8210. msdc_host_mode[host->id] = mmc->caps;
  8211. msdc_host_mode2[host->id] = mmc->caps2;
  8212. /*for emmc */
  8213. mtk_msdc_host[pdev->id] = host;
  8214. host->write_timeout_uhs104 = 0;
  8215. host->write_timeout_emmc = 0;
  8216. host->read_timeout_uhs104 = 0;
  8217. host->read_timeout_emmc = 0;
  8218. host->sw_timeout = 0;
  8219. host->tune = 0;
  8220. host->timing = 0;
  8221. host->sd_cd_insert_work = 0;
  8222. host->block_bad_card = 0;
  8223. host->sd_30_busy = 0;
  8224. msdc_reset_tmo_tune_counter(host, ALL_TUNE_CNT);
  8225. msdc_reset_pwr_cycle_counter(host);
  8226. if (is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ)) {
  8227. host->saved_para.suspend_flag = 0;
  8228. host->saved_para.msdc_cfg = 0;
  8229. host->saved_para.mode = 0;
  8230. host->saved_para.div = 0;
  8231. host->saved_para.sdc_cfg = 0;
  8232. host->saved_para.iocon = 0;
  8233. host->saved_para.timing = 0;
  8234. host->saved_para.hz = 0;
  8235. host->saved_para.cmd_resp_ta_cntr = 0; /* for SDIO 3.0 */
  8236. host->saved_para.wrdat_crc_ta_cntr = 0; /* for SDIO 3.0 */
  8237. host->saved_para.int_dat_latch_ck_sel = 0; /* for SDIO 3.0 */
  8238. host->saved_para.ckgen_msdc_dly_sel = 0; /* for SDIO 3.0 */
  8239. host->saved_para.inten_sdio_irq = 0; /* default disable */
  8240. host->saved_para.cfg_cmdrsp_path = 0;
  8241. host->saved_para.cfg_crcsts_path = 0;
  8242. wakeup_source_init(&host->trans_lock, "MSDC Transfer Lock");
  8243. }
  8244. #ifdef MTK_SDIO30_ONLINE_TUNING_SUPPORT
  8245. atomic_set(&host->ot_done, 1);
  8246. atomic_set(&host->sdio_stopping, 0);
  8247. host->ot_work.host = host;
  8248. host->ot_work.chg_volt = 0;
  8249. /* ccyeh */ atomic_set(&host->ot_work.ot_disable, 0);
  8250. atomic_set(&host->ot_work.ot_disable, 1); /* ccyeh */
  8251. atomic_set(&host->ot_work.autok_done, 0);
  8252. #endif
  8253. INIT_DELAYED_WORK(&host->write_timeout, msdc_check_write_timeout);
  8254. spin_lock_init(&host->lock);
  8255. spin_lock_init(&host->clk_gate_lock);
  8256. spin_lock_init(&host->remove_bad_card);
  8257. spin_lock_init(&host->sdio_irq_lock);
  8258. /* init dynamtic timer */
  8259. init_timer(&host->timer);
  8260. /* host->timer.expires = jiffies + HZ; */
  8261. host->timer.function = msdc_timer_pm;
  8262. host->timer.data = (unsigned long)host;
  8263. ret = request_irq((unsigned int)host->irq, msdc_irq, IRQF_TRIGGER_NONE,
  8264. DRV_NAME, host);
  8265. if (ret)
  8266. goto release;
  8267. /* not set for sdio */
  8268. /* set to combo_sdio_request_eirq() for WIFI */
  8269. /* msdc_eirq_sdio() will be called when EIRQ */
  8270. if (host->hw->request_sdio_eirq)
  8271. host->hw->request_sdio_eirq(msdc_eirq_sdio, (void *)host);
  8272. #ifdef CONFIG_PM
  8273. if (host->hw->register_pm) { /* yes for sdio */
  8274. host->hw->register_pm(msdc_pm, (void *)host); /* combo_sdio_register_pm() */
  8275. if (host->hw->flags & MSDC_SYS_SUSPEND) { /* will not set for WIFI */
  8276. ERR_MSG("MSDC_SYS_SUSPEND and register_pm both set");
  8277. }
  8278. /* pm not controlled by system but by client. */
  8279. mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
  8280. }
  8281. #endif
  8282. platform_set_drvdata(pdev, mmc);
  8283. #ifdef CONFIG_MTK_HIBERNATION
  8284. if (pdev->id == 1)
  8285. register_swsusp_restore_noirq_func(ID_M_MSDC, msdc_drv_pm_restore_noirq,
  8286. &(pdev->dev));
  8287. #endif
  8288. /* Config card detection pin and enable interrupts */
  8289. if (!(host->mmc->caps & MMC_CAP_NONREMOVABLE)) { /* set for card */
  8290. sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  8291. sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
  8292. sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
  8293. }
  8294. /*config tune at workqueue*/
  8295. INIT_WORK(&host->work_tune, msdc_async_tune);
  8296. host->mrq_tune = NULL;
  8297. ret = mmc_add_host(mmc);
  8298. if (ret)
  8299. goto free_irq;
  8300. if (host->hw->flags & MSDC_SDIO_IRQ) {
  8301. ghost = host;
  8302. sdr_set_bits(SDC_CFG, SDC_CFG_SDIOIDE); /* enable sdio detection */
  8303. }
  8304. /* if (hw->flags & MSDC_CD_PIN_EN) */
  8305. host->sd_cd_insert_work = 1;
  8306. #ifdef DEBUG_TEST_FOR_SIGNAL
  8307. /* use EINT1 for trigger signal */
  8308. /* need to remove gpio warning log at
  8309. * mediatek/kernel/include/mach/mt_gpio_core.h
  8310. * mediatek/platform/{project}/kernel/drivers/gpio/mt_gpio_affix.c */
  8311. mt_set_gpio_mode(1, GPIO_MODE_00);
  8312. mt_set_gpio_dir(1, GPIO_DIR_OUT);
  8313. mt_set_gpio_pull_enable(1, 1);
  8314. mt_set_gpio_out(1, 0); /* 1-high, 0-low */
  8315. #endif
  8316. #ifdef MTK_MSDC_BRINGUP_DEBUG
  8317. pr_debug("[%s]: msdc%d, mmc->caps=0x%x, mmc->caps2=0x%x\n",
  8318. __func__, host->id, mmc->caps, mmc->caps2);
  8319. msdc_dump_clock_sts(host);
  8320. #endif
  8321. if (host->hw->host_function == MSDC_EMMC)
  8322. host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
  8323. #ifdef FPGA_PLATFORM
  8324. #if 0 /*def CONFIG_MTK_EMMC_SUPPORT */
  8325. pr_debug("[%s]: waiting emmc init complete\n", __func__);
  8326. host->mmc->card_init_wait(host->mmc);
  8327. pr_debug("[%s]: start read write compare test\n", __func__);
  8328. emmc_multi_rw_compare(0, 0x200, 0xf);
  8329. pr_debug("[%s]: finish read write compare test\n", __func__);
  8330. #endif
  8331. #endif
  8332. return 0;
  8333. free_irq:
  8334. free_irq(host->irq, host);
  8335. pr_err("[%s]: msdc%d init fail free irq!\n", __func__, host->id);
  8336. release:
  8337. platform_set_drvdata(pdev, NULL);
  8338. msdc_deinit_hw(host);
  8339. pr_err("[%s]: msdc%d init fail release!\n", __func__, host->id);
  8340. tasklet_kill(&host->card_tasklet);
  8341. mmc_free_host(mmc);
  8342. return ret;
  8343. }
  8344. /* 4 device share one driver, using "drvdata" to show difference */
  8345. static int msdc_drv_remove(struct platform_device *pdev)
  8346. {
  8347. struct mmc_host *mmc;
  8348. struct msdc_host *host;
  8349. struct resource *mem;
  8350. mmc = platform_get_drvdata(pdev);
  8351. BUG_ON(!mmc);
  8352. host = mmc_priv(mmc);
  8353. BUG_ON(!host);
  8354. ERR_MSG("removed !!!");
  8355. #ifndef CONFIG_MTK_CLKMGR
  8356. /* clock unprepare */
  8357. if (host->clock_control)
  8358. clk_unprepare(host->clock_control);
  8359. if ((host->hw->host_function == MSDC_EMMC) && g_msdc0_pll_sel)
  8360. clk_unprepare(g_msdc0_pll_sel);
  8361. #endif
  8362. platform_set_drvdata(pdev, NULL);
  8363. mmc_remove_host(host->mmc);
  8364. msdc_deinit_hw(host);
  8365. tasklet_kill(&host->card_tasklet);
  8366. free_irq(host->irq, host);
  8367. dma_free_coherent(NULL, MAX_GPD_NUM * sizeof(struct gpd_t), host->dma.gpd,
  8368. host->dma.gpd_addr);
  8369. dma_free_coherent(NULL, MAX_BD_NUM * sizeof(struct bd_t), host->dma.bd,
  8370. host->dma.bd_addr);
  8371. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  8372. if (mem)
  8373. release_mem_region(mem->start, mem->end - mem->start + 1);
  8374. mmc_free_host(host->mmc);
  8375. return 0;
  8376. }
  8377. #ifdef CONFIG_PM
  8378. static int msdc_drv_suspend(struct platform_device *pdev, pm_message_t state)
  8379. {
  8380. int ret = 0;
  8381. struct mmc_host *mmc = platform_get_drvdata(pdev);
  8382. struct msdc_host *host = mmc_priv(mmc);
  8383. void __iomem *base = host->base;
  8384. if (mmc && state.event == PM_EVENT_SUSPEND
  8385. && (host->hw->flags & MSDC_SYS_SUSPEND))
  8386. msdc_pm(state, (void *)host);
  8387. /* WIFI slot should be off when enter suspend */
  8388. if (mmc && state.event == PM_EVENT_SUSPEND
  8389. && (!(host->hw->flags & MSDC_SYS_SUSPEND))) {
  8390. msdc_suspend_clock(host);
  8391. if (host->error == -EBUSY) {
  8392. ret = host->error;
  8393. host->error = 0;
  8394. }
  8395. }
  8396. if (is_card_sdio(host) || (host->hw->flags & MSDC_SDIO_IRQ)) {
  8397. if (host->clk_gate_count > 0) {
  8398. host->error = 0;
  8399. return -EBUSY;
  8400. }
  8401. if (host->saved_para.suspend_flag == 0) {
  8402. host->saved_para.hz = host->mclk;
  8403. if (host->saved_para.hz) {
  8404. host->saved_para.suspend_flag = 1;
  8405. /* mb(); */
  8406. msdc_ungate_clock(host);
  8407. sdr_get_field(MSDC_CFG, MSDC_CFG_CKMOD, host->saved_para.mode);
  8408. sdr_get_field(MSDC_CFG, MSDC_CFG_CKDIV, host->saved_para.div);
  8409. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_INT_DAT_LATCH_CK_SEL,
  8410. host->saved_para.int_dat_latch_ck_sel); /* for SDIO 3.0 */
  8411. sdr_get_field(MSDC_PATCH_BIT0, MSDC_PB0_CKGEN_MSDC_DLY_SEL,
  8412. host->saved_para.ckgen_msdc_dly_sel); /* for SDIO 3.0 */
  8413. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_CMD_RSP_TA_CNTR,
  8414. host->saved_para.cmd_resp_ta_cntr); /* for SDIO 3.0 */
  8415. sdr_get_field(MSDC_PATCH_BIT1, MSDC_PB1_WRDAT_CRCS_TA_CNTR,
  8416. host->saved_para.wrdat_crc_ta_cntr); /* for SDIO 3.0 */
  8417. /* get INTEN status for SDIO */
  8418. sdr_get_field(MSDC_INTEN, MSDC_INT_SDIOIRQ,
  8419. host->saved_para.inten_sdio_irq);
  8420. host->saved_para.msdc_cfg = sdr_read32(MSDC_CFG);
  8421. host->saved_para.ddly0 = sdr_read32(MSDC_DAT_RDDLY0);
  8422. host->saved_para.pad_tune0 = sdr_read32(MSDC_PAD_TUNE0);
  8423. host->saved_para.sdc_cfg = sdr_read32(SDC_CFG);
  8424. host->saved_para.iocon = sdr_read32(MSDC_IOCON);
  8425. host->saved_para.timing = host->timing;
  8426. msdc_gate_clock(host, 0);
  8427. if (host->error == -EBUSY) {
  8428. ret = host->error;
  8429. host->error = 0;
  8430. }
  8431. }
  8432. ERR_MSG("msdc suspend cur_cfg=%x, save_cfg=%x, cur_hz=%d,save_hz=%d"
  8433. , sdr_read32(MSDC_CFG), host->saved_para.msdc_cfg,
  8434. host->mclk, host->saved_para.hz);
  8435. }
  8436. }
  8437. return ret;
  8438. }
  8439. static int msdc_drv_resume(struct platform_device *pdev)
  8440. {
  8441. int ret = 0;
  8442. struct mmc_host *mmc = platform_get_drvdata(pdev);
  8443. struct msdc_host *host = mmc_priv(mmc);
  8444. struct pm_message state;
  8445. if (host->hw->flags & MSDC_SDIO_IRQ)
  8446. pr_debug("msdc msdc_drv_resume\n");
  8447. state.event = PM_EVENT_RESUME;
  8448. if (mmc && (host->hw->flags & MSDC_SYS_SUSPEND)) { /* will set for card */
  8449. msdc_pm(state, (void *)host);
  8450. }
  8451. /* This mean WIFI not controller by PM */
  8452. if (host->hw->host_function == MSDC_SDIO) {
  8453. host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
  8454. host->mmc->rescan_entered = 0;
  8455. }
  8456. return ret;
  8457. }
  8458. #endif
  8459. #ifdef CONFIG_OF
  8460. static const struct of_device_id msdc_of_ids[] = {
  8461. {.compatible = "mediatek,mt6735-mmc",},
  8462. {.compatible = "mediatek,mt6735m-mmc",},
  8463. {.compatible = "mediatek,mt6753-mmc",},
  8464. {.compatible = "mediatek,MSDC2",},
  8465. {.compatible = "mediatek,MSDC3",},
  8466. {},
  8467. };
  8468. #endif
  8469. static struct platform_driver mt_msdc_driver = {
  8470. .probe = msdc_drv_probe,
  8471. .remove = msdc_drv_remove,
  8472. #ifdef CONFIG_PM
  8473. .suspend = msdc_drv_suspend,
  8474. .resume = msdc_drv_resume,
  8475. #endif
  8476. .driver = {
  8477. .name = DRV_NAME,
  8478. .owner = THIS_MODULE,
  8479. #ifdef CONFIG_OF
  8480. .of_match_table = msdc_of_ids,
  8481. #endif
  8482. },
  8483. };
  8484. /*--------------------------------------------------------------------------*/
  8485. /* module init/exit */
  8486. /*--------------------------------------------------------------------------*/
  8487. static int __init mt_msdc_init(void)
  8488. {
  8489. int ret;
  8490. /*config tune at workqueue*/
  8491. wq_tune = create_workqueue("msdc-tune");
  8492. if (!wq_tune) {
  8493. pr_err("msdc create work_queue failed.[%s]:%d", __func__, __LINE__);
  8494. BUG();
  8495. }
  8496. ret = platform_driver_register(&mt_msdc_driver);
  8497. if (ret) {
  8498. pr_err(DRV_NAME ": Can't register driver");
  8499. return ret;
  8500. }
  8501. pr_debug(DRV_NAME ": MediaTek MSDC Driver\n");
  8502. msdc_debug_proc_init();
  8503. #ifdef MSDC_DMA_ADDR_DEBUG
  8504. msdc_init_dma_latest_address();
  8505. #endif
  8506. return 0;
  8507. }
  8508. static void __exit mt_msdc_exit(void)
  8509. {
  8510. platform_driver_unregister(&mt_msdc_driver);
  8511. if (wq_tune) {
  8512. destroy_workqueue(wq_tune);
  8513. wq_tune = NULL;
  8514. }
  8515. #ifdef CONFIG_MTK_HIBERNATION
  8516. unregister_swsusp_restore_noirq_func(ID_M_MSDC);
  8517. #endif
  8518. }
  8519. module_init(mt_msdc_init);
  8520. module_exit(mt_msdc_exit);
  8521. #ifdef CONFIG_MTK_EMMC_SUPPORT
  8522. late_initcall_sync(init_get_cache_work);
  8523. #endif
  8524. MODULE_LICENSE("GPL");
  8525. MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver");