1
0

0046-mmc-MIPS-ralink-add-sdhci-for-mt7620a-SoC.patch 164 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830
  1. From 23147af14531cbdada194b94120ef8774f46292d Mon Sep 17 00:00:00 2001
  2. From: John Crispin <blogic@openwrt.org>
  3. Date: Thu, 13 Nov 2014 19:08:40 +0100
  4. Subject: [PATCH 46/53] mmc: MIPS: ralink: add sdhci for mt7620a SoC
  5. Signed-off-by: John Crispin <blogic@openwrt.org>
  6. ---
  7. drivers/mmc/host/Kconfig | 2 +
  8. drivers/mmc/host/Makefile | 1 +
  9. drivers/mmc/host/mtk-mmc/Kconfig | 16 +
  10. drivers/mmc/host/mtk-mmc/Makefile | 42 +
  11. drivers/mmc/host/mtk-mmc/board.h | 137 ++
  12. drivers/mmc/host/mtk-mmc/dbg.c | 347 ++++
  13. drivers/mmc/host/mtk-mmc/dbg.h | 156 ++
  14. drivers/mmc/host/mtk-mmc/mt6575_sd.h | 1001 +++++++++++
  15. drivers/mmc/host/mtk-mmc/sd.c | 3060 ++++++++++++++++++++++++++++++++++
  16. 9 files changed, 4762 insertions(+)
  17. create mode 100644 drivers/mmc/host/mtk-mmc/Kconfig
  18. create mode 100644 drivers/mmc/host/mtk-mmc/Makefile
  19. create mode 100644 drivers/mmc/host/mtk-mmc/board.h
  20. create mode 100644 drivers/mmc/host/mtk-mmc/dbg.c
  21. create mode 100644 drivers/mmc/host/mtk-mmc/dbg.h
  22. create mode 100644 drivers/mmc/host/mtk-mmc/mt6575_sd.h
  23. create mode 100644 drivers/mmc/host/mtk-mmc/sd.c
  24. --- a/drivers/mmc/host/Kconfig
  25. +++ b/drivers/mmc/host/Kconfig
  26. @@ -786,3 +786,5 @@ config MMC_MTK
  27. If you have a machine with a integrated SD/MMC card reader, say Y or M here.
  28. This is needed if support for any SD/SDIO/MMC devices is required.
  29. If unsure, say N.
  30. +
  31. +source "drivers/mmc/host/mtk-mmc/Kconfig"
  32. --- a/drivers/mmc/host/Makefile
  33. +++ b/drivers/mmc/host/Makefile
  34. @@ -2,6 +2,7 @@
  35. # Makefile for MMC/SD host controller drivers
  36. #
  37. +obj-$(CONFIG_MTK_MMC) += mtk-mmc/
  38. obj-$(CONFIG_MMC_ARMMMCI) += mmci.o
  39. obj-$(CONFIG_MMC_QCOM_DML) += mmci_qcom_dml.o
  40. obj-$(CONFIG_MMC_PXA) += pxamci.o
  41. --- /dev/null
  42. +++ b/drivers/mmc/host/mtk-mmc/Kconfig
  43. @@ -0,0 +1,16 @@
  44. +config MTK_MMC
  45. + tristate "MTK SD/MMC"
  46. + depends on !MTD_NAND_RALINK
  47. +
  48. +config MTK_AEE_KDUMP
  49. + bool "MTK AEE KDUMP"
  50. + depends on MTK_MMC
  51. +
  52. +config MTK_MMC_CD_POLL
  53. + bool "Card Detect with Polling"
  54. + depends on MTK_MMC
  55. +
  56. +config MTK_MMC_EMMC_8BIT
  57. + bool "eMMC 8-bit support"
  58. + depends on MTK_MMC && RALINK_MT7628
  59. +
  60. --- /dev/null
  61. +++ b/drivers/mmc/host/mtk-mmc/Makefile
  62. @@ -0,0 +1,42 @@
  63. +# Copyright Statement:
  64. +#
  65. +# This software/firmware and related documentation ("MediaTek Software") are
  66. +# protected under relevant copyright laws. The information contained herein
  67. +# is confidential and proprietary to MediaTek Inc. and/or its licensors.
  68. +# Without the prior written permission of MediaTek inc. and/or its licensors,
  69. +# any reproduction, modification, use or disclosure of MediaTek Software,
  70. +# and information contained herein, in whole or in part, shall be strictly prohibited.
  71. +#
  72. +# MediaTek Inc. (C) 2010. All rights reserved.
  73. +#
  74. +# BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  75. +# THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  76. +# RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  77. +# AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  78. +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  79. +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  80. +# NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  81. +# SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  82. +# SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  83. +# THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  84. +# THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  85. +# CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  86. +# SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  87. +# STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  88. +# CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  89. +# AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  90. +# OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  91. +# MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  92. +#
  93. +# The following software/firmware and/or related documentation ("MediaTek Software")
  94. +# have been modified by MediaTek Inc. All revisions are subject to any receiver's
  95. +# applicable license agreements with MediaTek Inc.
  96. +
  97. +obj-$(CONFIG_MTK_MMC) += mtk_sd.o
  98. +mtk_sd-objs := sd.o dbg.o
  99. +ifeq ($(CONFIG_MTK_AEE_KDUMP),y)
  100. +EXTRA_CFLAGS += -DMT6575_SD_DEBUG
  101. +endif
  102. +
  103. +clean:
  104. + @rm -f *.o modules.order .*.cmd
  105. --- /dev/null
  106. +++ b/drivers/mmc/host/mtk-mmc/board.h
  107. @@ -0,0 +1,137 @@
  108. +/* Copyright Statement:
  109. + *
  110. + * This software/firmware and related documentation ("MediaTek Software") are
  111. + * protected under relevant copyright laws. The information contained herein
  112. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  113. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  114. + * any reproduction, modification, use or disclosure of MediaTek Software,
  115. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  116. + */
  117. +/* MediaTek Inc. (C) 2010. All rights reserved.
  118. + *
  119. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  120. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  121. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  122. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  123. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  124. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  125. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  126. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  127. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  128. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  129. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  130. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  131. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  132. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  133. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  134. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  135. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  136. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  137. + *
  138. + * The following software/firmware and/or related documentation ("MediaTek Software")
  139. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  140. + * applicable license agreements with MediaTek Inc.
  141. + */
  142. +
  143. +#ifndef __ARCH_ARM_MACH_BOARD_H
  144. +#define __ARCH_ARM_MACH_BOARD_H
  145. +
  146. +#include <generated/autoconf.h>
  147. +#include <linux/pm.h>
  148. +/* --- chhung */
  149. +// #include <mach/mt6575.h>
  150. +// #include <board-custom.h>
  151. +/* end of chhung */
  152. +
  153. +typedef void (*sdio_irq_handler_t)(void*); /* external irq handler */
  154. +typedef void (*pm_callback_t)(pm_message_t state, void *data);
  155. +
  156. +#define MSDC_CD_PIN_EN (1 << 0) /* card detection pin is wired */
  157. +#define MSDC_WP_PIN_EN (1 << 1) /* write protection pin is wired */
  158. +#define MSDC_RST_PIN_EN (1 << 2) /* emmc reset pin is wired */
  159. +#define MSDC_SDIO_IRQ (1 << 3) /* use internal sdio irq (bus) */
  160. +#define MSDC_EXT_SDIO_IRQ (1 << 4) /* use external sdio irq */
  161. +#define MSDC_REMOVABLE (1 << 5) /* removable slot */
  162. +#define MSDC_SYS_SUSPEND (1 << 6) /* suspended by system */
  163. +#define MSDC_HIGHSPEED (1 << 7) /* high-speed mode support */
  164. +#define MSDC_UHS1 (1 << 8) /* uhs-1 mode support */
  165. +#define MSDC_DDR (1 << 9) /* ddr mode support */
  166. +
  167. +
  168. +#define MSDC_SMPL_RISING (0)
  169. +#define MSDC_SMPL_FALLING (1)
  170. +
  171. +#define MSDC_CMD_PIN (0)
  172. +#define MSDC_DAT_PIN (1)
  173. +#define MSDC_CD_PIN (2)
  174. +#define MSDC_WP_PIN (3)
  175. +#define MSDC_RST_PIN (4)
  176. +
  177. +enum {
  178. + MSDC_CLKSRC_48MHZ = 0,
  179. +// MSDC_CLKSRC_26MHZ = 0,
  180. +// MSDC_CLKSRC_197MHZ = 1,
  181. +// MSDC_CLKSRC_208MHZ = 2
  182. +};
  183. +
  184. +struct msdc_hw {
  185. + unsigned char clk_src; /* host clock source */
  186. + unsigned char cmd_edge; /* command latch edge */
  187. + unsigned char data_edge; /* data latch edge */
  188. + unsigned char clk_drv; /* clock pad driving */
  189. + unsigned char cmd_drv; /* command pad driving */
  190. + unsigned char dat_drv; /* data pad driving */
  191. + unsigned long flags; /* hardware capability flags */
  192. + unsigned long data_pins; /* data pins */
  193. + unsigned long data_offset; /* data address offset */
  194. +
  195. + /* config gpio pull mode */
  196. + void (*config_gpio_pin)(int type, int pull);
  197. +
  198. + /* external power control for card */
  199. + void (*ext_power_on)(void);
  200. + void (*ext_power_off)(void);
  201. +
  202. + /* external sdio irq operations */
  203. + void (*request_sdio_eirq)(sdio_irq_handler_t sdio_irq_handler, void *data);
  204. + void (*enable_sdio_eirq)(void);
  205. + void (*disable_sdio_eirq)(void);
  206. +
  207. + /* external cd irq operations */
  208. + void (*request_cd_eirq)(sdio_irq_handler_t cd_irq_handler, void *data);
  209. + void (*enable_cd_eirq)(void);
  210. + void (*disable_cd_eirq)(void);
  211. + int (*get_cd_status)(void);
  212. +
  213. + /* power management callback for external module */
  214. + void (*register_pm)(pm_callback_t pm_cb, void *data);
  215. +};
  216. +
  217. +extern struct msdc_hw msdc0_hw;
  218. +extern struct msdc_hw msdc1_hw;
  219. +extern struct msdc_hw msdc2_hw;
  220. +extern struct msdc_hw msdc3_hw;
  221. +
  222. +/*GPS driver*/
  223. +#define GPS_FLAG_FORCE_OFF 0x0001
  224. +struct mt3326_gps_hardware {
  225. + int (*ext_power_on)(int);
  226. + int (*ext_power_off)(int);
  227. +};
  228. +extern struct mt3326_gps_hardware mt3326_gps_hw;
  229. +
  230. +/* NAND driver */
  231. +struct mt6575_nand_host_hw {
  232. + unsigned int nfi_bus_width; /* NFI_BUS_WIDTH */
  233. + unsigned int nfi_access_timing; /* NFI_ACCESS_TIMING */
  234. + unsigned int nfi_cs_num; /* NFI_CS_NUM */
  235. + unsigned int nand_sec_size; /* NAND_SECTOR_SIZE */
  236. + unsigned int nand_sec_shift; /* NAND_SECTOR_SHIFT */
  237. + unsigned int nand_ecc_size;
  238. + unsigned int nand_ecc_bytes;
  239. + unsigned int nand_ecc_mode;
  240. +};
  241. +extern struct mt6575_nand_host_hw mt6575_nand_hw;
  242. +
  243. +#endif /* __ARCH_ARM_MACH_BOARD_H */
  244. +
  245. --- /dev/null
  246. +++ b/drivers/mmc/host/mtk-mmc/dbg.c
  247. @@ -0,0 +1,347 @@
  248. +/* Copyright Statement:
  249. + *
  250. + * This software/firmware and related documentation ("MediaTek Software") are
  251. + * protected under relevant copyright laws. The information contained herein
  252. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  253. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  254. + * any reproduction, modification, use or disclosure of MediaTek Software,
  255. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  256. + *
  257. + * MediaTek Inc. (C) 2010. All rights reserved.
  258. + *
  259. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  260. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  261. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  262. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  263. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  264. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  265. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  266. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  267. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  268. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  269. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  270. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  271. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  272. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  273. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  274. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  275. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  276. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  277. + *
  278. + * The following software/firmware and/or related documentation ("MediaTek Software")
  279. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  280. + * applicable license agreements with MediaTek Inc.
  281. + */
  282. +
  283. +#include <linux/version.h>
  284. +#include <linux/kernel.h>
  285. +#include <linux/sched.h>
  286. +#include <linux/kthread.h>
  287. +#include <linux/delay.h>
  288. +#include <linux/module.h>
  289. +#include <linux/init.h>
  290. +#include <linux/proc_fs.h>
  291. +#include <linux/string.h>
  292. +#include <linux/uaccess.h>
  293. +// #include <mach/mt6575_gpt.h> /* --- by chhung */
  294. +#include "dbg.h"
  295. +#include "mt6575_sd.h"
  296. +#include <linux/seq_file.h>
  297. +
  298. +static char cmd_buf[256];
  299. +
  300. +/* for debug zone */
  301. +unsigned int sd_debug_zone[4]={
  302. + 0,
  303. + 0,
  304. + 0,
  305. + 0
  306. +};
  307. +
  308. +/* mode select */
  309. +u32 dma_size[4]={
  310. + 512,
  311. + 512,
  312. + 512,
  313. + 512
  314. +};
  315. +msdc_mode drv_mode[4]={
  316. + MODE_SIZE_DEP, /* using DMA or not depend on the size */
  317. + MODE_SIZE_DEP,
  318. + MODE_SIZE_DEP,
  319. + MODE_SIZE_DEP
  320. +};
  321. +
  322. +#if defined (MT6575_SD_DEBUG)
  323. +/* for driver profile */
  324. +#define TICKS_ONE_MS (13000)
  325. +u32 gpt_enable = 0;
  326. +u32 sdio_pro_enable = 0; /* make sure gpt is enabled */
  327. +u32 sdio_pro_time = 0; /* no more than 30s */
  328. +struct sdio_profile sdio_perfomance = {0};
  329. +
  330. +#if 0 /* --- chhung */
  331. +void msdc_init_gpt(void)
  332. +{
  333. + GPT_CONFIG config;
  334. +
  335. + config.num = GPT6;
  336. + config.mode = GPT_FREE_RUN;
  337. + config.clkSrc = GPT_CLK_SRC_SYS;
  338. + config.clkDiv = GPT_CLK_DIV_1; /* 13MHz GPT6 */
  339. +
  340. + if (GPT_Config(config) == FALSE )
  341. + return;
  342. +
  343. + GPT_Start(GPT6);
  344. +}
  345. +#endif /* end of --- */
  346. +
  347. +u32 msdc_time_calc(u32 old_L32, u32 old_H32, u32 new_L32, u32 new_H32)
  348. +{
  349. + u32 ret = 0;
  350. +
  351. + if (new_H32 == old_H32) {
  352. + ret = new_L32 - old_L32;
  353. + } else if(new_H32 == (old_H32 + 1)) {
  354. + if (new_L32 > old_L32) {
  355. + printk("msdc old_L<0x%x> new_L<0x%x>\n", old_L32, new_L32);
  356. + }
  357. + ret = (0xffffffff - old_L32);
  358. + ret += new_L32;
  359. + } else {
  360. + printk("msdc old_H<0x%x> new_H<0x%x>\n", old_H32, new_H32);
  361. + }
  362. +
  363. + return ret;
  364. +}
  365. +
  366. +void msdc_sdio_profile(struct sdio_profile* result)
  367. +{
  368. + struct cmd_profile* cmd;
  369. + u32 i;
  370. +
  371. + printk("sdio === performance dump ===\n");
  372. + printk("sdio === total execute tick<%d> time<%dms> Tx<%dB> Rx<%dB>\n",
  373. + result->total_tc, result->total_tc / TICKS_ONE_MS,
  374. + result->total_tx_bytes, result->total_rx_bytes);
  375. +
  376. + /* CMD52 Dump */
  377. + cmd = &result->cmd52_rx;
  378. + printk("sdio === CMD52 Rx <%d>times tick<%d> Max<%d> Min<%d> Aver<%d>\n", cmd->count, cmd->tot_tc,
  379. + cmd->max_tc, cmd->min_tc, cmd->tot_tc/cmd->count);
  380. + cmd = &result->cmd52_tx;
  381. + printk("sdio === CMD52 Tx <%d>times tick<%d> Max<%d> Min<%d> Aver<%d>\n", cmd->count, cmd->tot_tc,
  382. + cmd->max_tc, cmd->min_tc, cmd->tot_tc/cmd->count);
  383. +
  384. + /* CMD53 Rx bytes + block mode */
  385. + for (i=0; i<512; i++) {
  386. + cmd = &result->cmd53_rx_byte[i];
  387. + if (cmd->count) {
  388. + printk("sdio<%6d><%3dB>_Rx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
  389. + cmd->max_tc, cmd->min_tc, cmd->tot_tc/cmd->count,
  390. + cmd->tot_bytes, (cmd->tot_bytes/10)*13 / (cmd->tot_tc/10));
  391. + }
  392. + }
  393. + for (i=0; i<100; i++) {
  394. + cmd = &result->cmd53_rx_blk[i];
  395. + if (cmd->count) {
  396. + printk("sdio<%6d><%3d>B_Rx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
  397. + cmd->max_tc, cmd->min_tc, cmd->tot_tc/cmd->count,
  398. + cmd->tot_bytes, (cmd->tot_bytes/10)*13 / (cmd->tot_tc/10));
  399. + }
  400. + }
  401. +
  402. + /* CMD53 Tx bytes + block mode */
  403. + for (i=0; i<512; i++) {
  404. + cmd = &result->cmd53_tx_byte[i];
  405. + if (cmd->count) {
  406. + printk("sdio<%6d><%3dB>_Tx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
  407. + cmd->max_tc, cmd->min_tc, cmd->tot_tc/cmd->count,
  408. + cmd->tot_bytes, (cmd->tot_bytes/10)*13 / (cmd->tot_tc/10));
  409. + }
  410. + }
  411. + for (i=0; i<100; i++) {
  412. + cmd = &result->cmd53_tx_blk[i];
  413. + if (cmd->count) {
  414. + printk("sdio<%6d><%3d>B_Tx_<%9d><%9d><%6d><%6d>_<%9dB><%2dM>\n", cmd->count, i, cmd->tot_tc,
  415. + cmd->max_tc, cmd->min_tc, cmd->tot_tc/cmd->count,
  416. + cmd->tot_bytes, (cmd->tot_bytes/10)*13 / (cmd->tot_tc/10));
  417. + }
  418. + }
  419. +
  420. + printk("sdio === performance dump done ===\n");
  421. +}
  422. +
  423. +//========= sdio command table ===========
  424. +void msdc_performance(u32 opcode, u32 sizes, u32 bRx, u32 ticks)
  425. +{
  426. + struct sdio_profile* result = &sdio_perfomance;
  427. + struct cmd_profile* cmd;
  428. + u32 block;
  429. +
  430. + if (sdio_pro_enable == 0) {
  431. + return;
  432. + }
  433. +
  434. + if (opcode == 52) {
  435. + cmd = bRx ? &result->cmd52_rx : &result->cmd52_tx;
  436. + } else if (opcode == 53) {
  437. + if (sizes < 512) {
  438. + cmd = bRx ? &result->cmd53_rx_byte[sizes] : &result->cmd53_tx_byte[sizes];
  439. + } else {
  440. + block = sizes / 512;
  441. + if (block >= 99) {
  442. + printk("cmd53 error blocks\n");
  443. + while(1);
  444. + }
  445. + cmd = bRx ? &result->cmd53_rx_blk[block] : &result->cmd53_tx_blk[block];
  446. + }
  447. + } else {
  448. + return;
  449. + }
  450. +
  451. + /* update the members */
  452. + if (ticks > cmd->max_tc){
  453. + cmd->max_tc = ticks;
  454. + }
  455. + if (cmd->min_tc == 0 || ticks < cmd->min_tc) {
  456. + cmd->min_tc = ticks;
  457. + }
  458. + cmd->tot_tc += ticks;
  459. + cmd->tot_bytes += sizes;
  460. + cmd->count ++;
  461. +
  462. + if (bRx) {
  463. + result->total_rx_bytes += sizes;
  464. + } else {
  465. + result->total_tx_bytes += sizes;
  466. + }
  467. + result->total_tc += ticks;
  468. +
  469. + /* dump when total_tc > 30s */
  470. + if (result->total_tc >= sdio_pro_time * TICKS_ONE_MS * 1000) {
  471. + msdc_sdio_profile(result);
  472. + memset(result, 0 , sizeof(struct sdio_profile));
  473. + }
  474. +}
  475. +
  476. +//========== driver proc interface ===========
  477. +static int msdc_debug_proc_read(struct seq_file *s, void *p)
  478. +{
  479. + seq_printf(s, "\n=========================================\n");
  480. + seq_printf(s, "Index<0> + Id + Zone\n");
  481. + seq_printf(s, "-> PWR<9> WRN<8> | FIO<7> OPS<6> FUN<5> CFG<4> | INT<3> RSP<2> CMD<1> DMA<0>\n");
  482. + seq_printf(s, "-> echo 0 3 0x3ff >msdc_bebug -> host[3] debug zone set to 0x3ff\n");
  483. + seq_printf(s, "-> MSDC[0] Zone: 0x%.8x\n", sd_debug_zone[0]);
  484. + seq_printf(s, "-> MSDC[1] Zone: 0x%.8x\n", sd_debug_zone[1]);
  485. + seq_printf(s, "-> MSDC[2] Zone: 0x%.8x\n", sd_debug_zone[2]);
  486. + seq_printf(s, "-> MSDC[3] Zone: 0x%.8x\n", sd_debug_zone[3]);
  487. +
  488. + seq_printf(s, "Index<1> + ID:4|Mode:4 + DMA_SIZE\n");
  489. + seq_printf(s, "-> 0)PIO 1)DMA 2)SIZE\n");
  490. + seq_printf(s, "-> echo 1 22 0x200 >msdc_bebug -> host[2] size mode, dma when >= 512\n");
  491. + seq_printf(s, "-> MSDC[0] mode<%d> size<%d>\n", drv_mode[0], dma_size[0]);
  492. + seq_printf(s, "-> MSDC[1] mode<%d> size<%d>\n", drv_mode[1], dma_size[1]);
  493. + seq_printf(s, "-> MSDC[2] mode<%d> size<%d>\n", drv_mode[2], dma_size[2]);
  494. + seq_printf(s, "-> MSDC[3] mode<%d> size<%d>\n", drv_mode[3], dma_size[3]);
  495. +
  496. + seq_printf(s, "Index<3> + SDIO_PROFILE + TIME\n");
  497. + seq_printf(s, "-> echo 3 1 0x1E >msdc_bebug -> enable sdio_profile, 30s\n");
  498. + seq_printf(s, "-> SDIO_PROFILE<%d> TIME<%ds>\n", sdio_pro_enable, sdio_pro_time);
  499. + seq_printf(s, "=========================================\n\n");
  500. +
  501. + return 0;
  502. +}
  503. +
  504. +static ssize_t msdc_debug_proc_write(struct file *file,
  505. + const char __user *buf, size_t count, loff_t *data)
  506. +{
  507. + int ret;
  508. +
  509. + int cmd, p1, p2;
  510. + int id, zone;
  511. + int mode, size;
  512. +
  513. + if (count == 0)return -1;
  514. + if(count > 255)count = 255;
  515. +
  516. + ret = copy_from_user(cmd_buf, buf, count);
  517. + if (ret < 0)return -1;
  518. +
  519. + cmd_buf[count] = '\0';
  520. + printk("msdc Write %s\n", cmd_buf);
  521. +
  522. + sscanf(cmd_buf, "%x %x %x", &cmd, &p1, &p2);
  523. +
  524. + if(cmd == SD_TOOL_ZONE) {
  525. + id = p1; zone = p2; zone &= 0x3ff;
  526. + printk("msdc host_id<%d> zone<0x%.8x>\n", id, zone);
  527. + if(id >=0 && id<=3){
  528. + sd_debug_zone[id] = zone;
  529. + }
  530. + else if(id == 4){
  531. + sd_debug_zone[0] = sd_debug_zone[1] = zone;
  532. + sd_debug_zone[2] = sd_debug_zone[3] = zone;
  533. + }
  534. + else{
  535. + printk("msdc host_id error when set debug zone\n");
  536. + }
  537. + } else if (cmd == SD_TOOL_DMA_SIZE) {
  538. + id = p1>>4; mode = (p1&0xf); size = p2;
  539. + if(id >=0 && id<=3){
  540. + drv_mode[id] = mode;
  541. + dma_size[id] = p2;
  542. + }
  543. + else if(id == 4){
  544. + drv_mode[0] = drv_mode[1] = mode;
  545. + drv_mode[2] = drv_mode[3] = mode;
  546. + dma_size[0] = dma_size[1] = p2;
  547. + dma_size[2] = dma_size[3] = p2;
  548. + }
  549. + else{
  550. + printk("msdc host_id error when select mode\n");
  551. + }
  552. + } else if (cmd == SD_TOOL_SDIO_PROFILE) {
  553. + if (p1 == 1) { /* enable profile */
  554. + if (gpt_enable == 0) {
  555. + // msdc_init_gpt(); /* --- by chhung */
  556. + gpt_enable = 1;
  557. + }
  558. + sdio_pro_enable = 1;
  559. + if (p2 == 0) p2 = 1; if (p2 >= 30) p2 = 30;
  560. + sdio_pro_time = p2 ;
  561. + } else if (p1 == 0) {
  562. + /* todo */
  563. + sdio_pro_enable = 0;
  564. + }
  565. + }
  566. +
  567. + return count;
  568. +}
  569. +
  570. +static int msdc_debug_show(struct inode *inode, struct file *file)
  571. +{
  572. + return single_open(file, msdc_debug_proc_read, NULL);
  573. +}
  574. +
  575. +static const struct file_operations msdc_debug_fops = {
  576. + .owner = THIS_MODULE,
  577. + .open = msdc_debug_show,
  578. + .read = seq_read,
  579. + .write = msdc_debug_proc_write,
  580. + .llseek = seq_lseek,
  581. + .release = single_release,
  582. +};
  583. +
  584. +int msdc_debug_proc_init(void)
  585. +{
  586. + struct proc_dir_entry *de = proc_create("msdc_debug", 0667, NULL, &msdc_debug_fops);
  587. +
  588. + if (!de || IS_ERR(de))
  589. + printk("!! Create MSDC debug PROC fail !!\n");
  590. +
  591. + return 0 ;
  592. +}
  593. +EXPORT_SYMBOL_GPL(msdc_debug_proc_init);
  594. +#endif
  595. --- /dev/null
  596. +++ b/drivers/mmc/host/mtk-mmc/dbg.h
  597. @@ -0,0 +1,156 @@
  598. +/* Copyright Statement:
  599. + *
  600. + * This software/firmware and related documentation ("MediaTek Software") are
  601. + * protected under relevant copyright laws. The information contained herein
  602. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  603. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  604. + * any reproduction, modification, use or disclosure of MediaTek Software,
  605. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  606. + *
  607. + * MediaTek Inc. (C) 2010. All rights reserved.
  608. + *
  609. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  610. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  611. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  612. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  613. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  614. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  615. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  616. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  617. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  618. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  619. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  620. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  621. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  622. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  623. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  624. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  625. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  626. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  627. + *
  628. + * The following software/firmware and/or related documentation ("MediaTek Software")
  629. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  630. + * applicable license agreements with MediaTek Inc.
  631. + */
  632. +#ifndef __MT_MSDC_DEUBG__
  633. +#define __MT_MSDC_DEUBG__
  634. +
  635. +//==========================
  636. +extern u32 sdio_pro_enable;
  637. +/* for a type command, e.g. CMD53, 2 blocks */
  638. +struct cmd_profile {
  639. + u32 max_tc; /* Max tick count */
  640. + u32 min_tc;
  641. + u32 tot_tc; /* total tick count */
  642. + u32 tot_bytes;
  643. + u32 count; /* the counts of the command */
  644. +};
  645. +
  646. +/* dump when total_tc and total_bytes */
  647. +struct sdio_profile {
  648. + u32 total_tc; /* total tick count of CMD52 and CMD53 */
  649. + u32 total_tx_bytes; /* total bytes of CMD53 Tx */
  650. + u32 total_rx_bytes; /* total bytes of CMD53 Rx */
  651. +
  652. + /*CMD52*/
  653. + struct cmd_profile cmd52_tx;
  654. + struct cmd_profile cmd52_rx;
  655. +
  656. + /*CMD53 in byte unit */
  657. + struct cmd_profile cmd53_tx_byte[512];
  658. + struct cmd_profile cmd53_rx_byte[512];
  659. +
  660. + /*CMD53 in block unit */
  661. + struct cmd_profile cmd53_tx_blk[100];
  662. + struct cmd_profile cmd53_rx_blk[100];
  663. +};
  664. +
  665. +//==========================
  666. +typedef enum {
  667. + SD_TOOL_ZONE = 0,
  668. + SD_TOOL_DMA_SIZE = 1,
  669. + SD_TOOL_PM_ENABLE = 2,
  670. + SD_TOOL_SDIO_PROFILE = 3,
  671. +} msdc_dbg;
  672. +
  673. +typedef enum {
  674. + MODE_PIO = 0,
  675. + MODE_DMA = 1,
  676. + MODE_SIZE_DEP = 2,
  677. +} msdc_mode;
  678. +extern msdc_mode drv_mode[4];
  679. +extern u32 dma_size[4];
  680. +
  681. +/* Debug message event */
  682. +#define DBG_EVT_NONE (0) /* No event */
  683. +#define DBG_EVT_DMA (1 << 0) /* DMA related event */
  684. +#define DBG_EVT_CMD (1 << 1) /* MSDC CMD related event */
  685. +#define DBG_EVT_RSP (1 << 2) /* MSDC CMD RSP related event */
  686. +#define DBG_EVT_INT (1 << 3) /* MSDC INT event */
  687. +#define DBG_EVT_CFG (1 << 4) /* MSDC CFG event */
  688. +#define DBG_EVT_FUC (1 << 5) /* Function event */
  689. +#define DBG_EVT_OPS (1 << 6) /* Read/Write operation event */
  690. +#define DBG_EVT_FIO (1 << 7) /* FIFO operation event */
  691. +#define DBG_EVT_WRN (1 << 8) /* Warning event */
  692. +#define DBG_EVT_PWR (1 << 9) /* Power event */
  693. +#define DBG_EVT_ALL (0xffffffff)
  694. +
  695. +#define DBG_EVT_MASK (DBG_EVT_ALL)
  696. +
  697. +extern unsigned int sd_debug_zone[4];
  698. +#define TAG "msdc"
  699. +#if 0 /* +++ chhung */
  700. +#define BUG_ON(x) \
  701. +do { \
  702. + if (x) { \
  703. + printk("[BUG] %s LINE:%d FILE:%s\n", #x, __LINE__, __FILE__); \
  704. + while(1); \
  705. + } \
  706. +}while(0)
  707. +#endif /* end of +++ */
  708. +
  709. +#define N_MSG(evt, fmt, args...)
  710. +/*
  711. +do { \
  712. + if ((DBG_EVT_##evt) & sd_debug_zone[host->id]) { \
  713. + printk(KERN_ERR TAG"%d -> "fmt" <- %s() : L<%d> PID<%s><0x%x>\n", \
  714. + host->id, ##args , __FUNCTION__, __LINE__, current->comm, current->pid); \
  715. + } \
  716. +} while(0)
  717. +*/
  718. +
  719. +#define ERR_MSG(fmt, args...) \
  720. +do { \
  721. + printk(KERN_ERR TAG"%d -> "fmt" <- %s() : L<%d> PID<%s><0x%x>\n", \
  722. + host->id, ##args , __FUNCTION__, __LINE__, current->comm, current->pid); \
  723. +} while(0);
  724. +
  725. +#if 1
  726. +//defined CONFIG_MTK_MMC_CD_POLL
  727. +#define INIT_MSG(fmt, args...)
  728. +#define IRQ_MSG(fmt, args...)
  729. +#else
  730. +#define INIT_MSG(fmt, args...) \
  731. +do { \
  732. + printk(KERN_ERR TAG"%d -> "fmt" <- %s() : L<%d> PID<%s><0x%x>\n", \
  733. + host->id, ##args , __FUNCTION__, __LINE__, current->comm, current->pid); \
  734. +} while(0);
  735. +
  736. +/* PID in ISR in not corrent */
  737. +#define IRQ_MSG(fmt, args...) \
  738. +do { \
  739. + printk(KERN_ERR TAG"%d -> "fmt" <- %s() : L<%d>\n", \
  740. + host->id, ##args , __FUNCTION__, __LINE__); \
  741. +} while(0);
  742. +#endif
  743. +
  744. +int msdc_debug_proc_init(void);
  745. +
  746. +#if 0 /* --- chhung */
  747. +void msdc_init_gpt(void);
  748. +extern void GPT_GetCounter64(UINT32 *cntL32, UINT32 *cntH32);
  749. +#endif /* end of --- */
  750. +u32 msdc_time_calc(u32 old_L32, u32 old_H32, u32 new_L32, u32 new_H32);
  751. +void msdc_performance(u32 opcode, u32 sizes, u32 bRx, u32 ticks);
  752. +
  753. +#endif
  754. --- /dev/null
  755. +++ b/drivers/mmc/host/mtk-mmc/mt6575_sd.h
  756. @@ -0,0 +1,1001 @@
  757. +/* Copyright Statement:
  758. + *
  759. + * This software/firmware and related documentation ("MediaTek Software") are
  760. + * protected under relevant copyright laws. The information contained herein
  761. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  762. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  763. + * any reproduction, modification, use or disclosure of MediaTek Software,
  764. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  765. + */
  766. +/* MediaTek Inc. (C) 2010. All rights reserved.
  767. + *
  768. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  769. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  770. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  771. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  772. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  773. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  774. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  775. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  776. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  777. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  778. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  779. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  780. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  781. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  782. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  783. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  784. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  785. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  786. + *
  787. + * The following software/firmware and/or related documentation ("MediaTek Software")
  788. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  789. + * applicable license agreements with MediaTek Inc.
  790. + */
  791. +
  792. +#ifndef MT6575_SD_H
  793. +#define MT6575_SD_H
  794. +
  795. +#include <linux/bitops.h>
  796. +#include <linux/mmc/host.h>
  797. +
  798. +// #include <mach/mt6575_reg_base.h> /* --- by chhung */
  799. +
  800. +/*--------------------------------------------------------------------------*/
  801. +/* Common Macro */
  802. +/*--------------------------------------------------------------------------*/
  803. +#define REG_ADDR(x) ((volatile u32*)(base + OFFSET_##x))
  804. +
  805. +/*--------------------------------------------------------------------------*/
  806. +/* Common Definition */
  807. +/*--------------------------------------------------------------------------*/
  808. +#define MSDC_FIFO_SZ (128)
  809. +#define MSDC_FIFO_THD (64) // (128)
  810. +#define MSDC_NUM (4)
  811. +
  812. +#define MSDC_MS (0)
  813. +#define MSDC_SDMMC (1)
  814. +
  815. +#define MSDC_MODE_UNKNOWN (0)
  816. +#define MSDC_MODE_PIO (1)
  817. +#define MSDC_MODE_DMA_BASIC (2)
  818. +#define MSDC_MODE_DMA_DESC (3)
  819. +#define MSDC_MODE_DMA_ENHANCED (4)
  820. +#define MSDC_MODE_MMC_STREAM (5)
  821. +
  822. +#define MSDC_BUS_1BITS (0)
  823. +#define MSDC_BUS_4BITS (1)
  824. +#define MSDC_BUS_8BITS (2)
  825. +
  826. +#define MSDC_BRUST_8B (3)
  827. +#define MSDC_BRUST_16B (4)
  828. +#define MSDC_BRUST_32B (5)
  829. +#define MSDC_BRUST_64B (6)
  830. +
  831. +#define MSDC_PIN_PULL_NONE (0)
  832. +#define MSDC_PIN_PULL_DOWN (1)
  833. +#define MSDC_PIN_PULL_UP (2)
  834. +#define MSDC_PIN_KEEP (3)
  835. +
  836. +#define MSDC_MAX_SCLK (48000000) /* +/- by chhung */
  837. +#define MSDC_MIN_SCLK (260000)
  838. +
  839. +#define MSDC_AUTOCMD12 (0x0001)
  840. +#define MSDC_AUTOCMD23 (0x0002)
  841. +#define MSDC_AUTOCMD19 (0x0003)
  842. +
  843. +#define MSDC_EMMC_BOOTMODE0 (0) /* Pull low CMD mode */
  844. +#define MSDC_EMMC_BOOTMODE1 (1) /* Reset CMD mode */
  845. +
  846. +enum {
  847. + RESP_NONE = 0,
  848. + RESP_R1,
  849. + RESP_R2,
  850. + RESP_R3,
  851. + RESP_R4,
  852. + RESP_R5,
  853. + RESP_R6,
  854. + RESP_R7,
  855. + RESP_R1B
  856. +};
  857. +
  858. +/*--------------------------------------------------------------------------*/
  859. +/* Register Offset */
  860. +/*--------------------------------------------------------------------------*/
  861. +#define OFFSET_MSDC_CFG (0x0)
  862. +#define OFFSET_MSDC_IOCON (0x04)
  863. +#define OFFSET_MSDC_PS (0x08)
  864. +#define OFFSET_MSDC_INT (0x0c)
  865. +#define OFFSET_MSDC_INTEN (0x10)
  866. +#define OFFSET_MSDC_FIFOCS (0x14)
  867. +#define OFFSET_MSDC_TXDATA (0x18)
  868. +#define OFFSET_MSDC_RXDATA (0x1c)
  869. +#define OFFSET_SDC_CFG (0x30)
  870. +#define OFFSET_SDC_CMD (0x34)
  871. +#define OFFSET_SDC_ARG (0x38)
  872. +#define OFFSET_SDC_STS (0x3c)
  873. +#define OFFSET_SDC_RESP0 (0x40)
  874. +#define OFFSET_SDC_RESP1 (0x44)
  875. +#define OFFSET_SDC_RESP2 (0x48)
  876. +#define OFFSET_SDC_RESP3 (0x4c)
  877. +#define OFFSET_SDC_BLK_NUM (0x50)
  878. +#define OFFSET_SDC_CSTS (0x58)
  879. +#define OFFSET_SDC_CSTS_EN (0x5c)
  880. +#define OFFSET_SDC_DCRC_STS (0x60)
  881. +#define OFFSET_EMMC_CFG0 (0x70)
  882. +#define OFFSET_EMMC_CFG1 (0x74)
  883. +#define OFFSET_EMMC_STS (0x78)
  884. +#define OFFSET_EMMC_IOCON (0x7c)
  885. +#define OFFSET_SDC_ACMD_RESP (0x80)
  886. +#define OFFSET_SDC_ACMD19_TRG (0x84)
  887. +#define OFFSET_SDC_ACMD19_STS (0x88)
  888. +#define OFFSET_MSDC_DMA_SA (0x90)
  889. +#define OFFSET_MSDC_DMA_CA (0x94)
  890. +#define OFFSET_MSDC_DMA_CTRL (0x98)
  891. +#define OFFSET_MSDC_DMA_CFG (0x9c)
  892. +#define OFFSET_MSDC_DBG_SEL (0xa0)
  893. +#define OFFSET_MSDC_DBG_OUT (0xa4)
  894. +#define OFFSET_MSDC_PATCH_BIT (0xb0)
  895. +#define OFFSET_MSDC_PATCH_BIT1 (0xb4)
  896. +#define OFFSET_MSDC_PAD_CTL0 (0xe0)
  897. +#define OFFSET_MSDC_PAD_CTL1 (0xe4)
  898. +#define OFFSET_MSDC_PAD_CTL2 (0xe8)
  899. +#define OFFSET_MSDC_PAD_TUNE (0xec)
  900. +#define OFFSET_MSDC_DAT_RDDLY0 (0xf0)
  901. +#define OFFSET_MSDC_DAT_RDDLY1 (0xf4)
  902. +#define OFFSET_MSDC_HW_DBG (0xf8)
  903. +#define OFFSET_MSDC_VERSION (0x100)
  904. +#define OFFSET_MSDC_ECO_VER (0x104)
  905. +
  906. +/*--------------------------------------------------------------------------*/
  907. +/* Register Address */
  908. +/*--------------------------------------------------------------------------*/
  909. +
  910. +/* common register */
  911. +#define MSDC_CFG REG_ADDR(MSDC_CFG)
  912. +#define MSDC_IOCON REG_ADDR(MSDC_IOCON)
  913. +#define MSDC_PS REG_ADDR(MSDC_PS)
  914. +#define MSDC_INT REG_ADDR(MSDC_INT)
  915. +#define MSDC_INTEN REG_ADDR(MSDC_INTEN)
  916. +#define MSDC_FIFOCS REG_ADDR(MSDC_FIFOCS)
  917. +#define MSDC_TXDATA REG_ADDR(MSDC_TXDATA)
  918. +#define MSDC_RXDATA REG_ADDR(MSDC_RXDATA)
  919. +#define MSDC_PATCH_BIT0 REG_ADDR(MSDC_PATCH_BIT)
  920. +
  921. +/* sdmmc register */
  922. +#define SDC_CFG REG_ADDR(SDC_CFG)
  923. +#define SDC_CMD REG_ADDR(SDC_CMD)
  924. +#define SDC_ARG REG_ADDR(SDC_ARG)
  925. +#define SDC_STS REG_ADDR(SDC_STS)
  926. +#define SDC_RESP0 REG_ADDR(SDC_RESP0)
  927. +#define SDC_RESP1 REG_ADDR(SDC_RESP1)
  928. +#define SDC_RESP2 REG_ADDR(SDC_RESP2)
  929. +#define SDC_RESP3 REG_ADDR(SDC_RESP3)
  930. +#define SDC_BLK_NUM REG_ADDR(SDC_BLK_NUM)
  931. +#define SDC_CSTS REG_ADDR(SDC_CSTS)
  932. +#define SDC_CSTS_EN REG_ADDR(SDC_CSTS_EN)
  933. +#define SDC_DCRC_STS REG_ADDR(SDC_DCRC_STS)
  934. +
  935. +/* emmc register*/
  936. +#define EMMC_CFG0 REG_ADDR(EMMC_CFG0)
  937. +#define EMMC_CFG1 REG_ADDR(EMMC_CFG1)
  938. +#define EMMC_STS REG_ADDR(EMMC_STS)
  939. +#define EMMC_IOCON REG_ADDR(EMMC_IOCON)
  940. +
  941. +/* auto command register */
  942. +#define SDC_ACMD_RESP REG_ADDR(SDC_ACMD_RESP)
  943. +#define SDC_ACMD19_TRG REG_ADDR(SDC_ACMD19_TRG)
  944. +#define SDC_ACMD19_STS REG_ADDR(SDC_ACMD19_STS)
  945. +
  946. +/* dma register */
  947. +#define MSDC_DMA_SA REG_ADDR(MSDC_DMA_SA)
  948. +#define MSDC_DMA_CA REG_ADDR(MSDC_DMA_CA)
  949. +#define MSDC_DMA_CTRL REG_ADDR(MSDC_DMA_CTRL)
  950. +#define MSDC_DMA_CFG REG_ADDR(MSDC_DMA_CFG)
  951. +
  952. +/* pad ctrl register */
  953. +#define MSDC_PAD_CTL0 REG_ADDR(MSDC_PAD_CTL0)
  954. +#define MSDC_PAD_CTL1 REG_ADDR(MSDC_PAD_CTL1)
  955. +#define MSDC_PAD_CTL2 REG_ADDR(MSDC_PAD_CTL2)
  956. +
  957. +/* data read delay */
  958. +#define MSDC_DAT_RDDLY0 REG_ADDR(MSDC_DAT_RDDLY0)
  959. +#define MSDC_DAT_RDDLY1 REG_ADDR(MSDC_DAT_RDDLY1)
  960. +
  961. +/* debug register */
  962. +#define MSDC_DBG_SEL REG_ADDR(MSDC_DBG_SEL)
  963. +#define MSDC_DBG_OUT REG_ADDR(MSDC_DBG_OUT)
  964. +
  965. +/* misc register */
  966. +#define MSDC_PATCH_BIT REG_ADDR(MSDC_PATCH_BIT)
  967. +#define MSDC_PATCH_BIT1 REG_ADDR(MSDC_PATCH_BIT1)
  968. +#define MSDC_PAD_TUNE REG_ADDR(MSDC_PAD_TUNE)
  969. +#define MSDC_HW_DBG REG_ADDR(MSDC_HW_DBG)
  970. +#define MSDC_VERSION REG_ADDR(MSDC_VERSION)
  971. +#define MSDC_ECO_VER REG_ADDR(MSDC_ECO_VER) /* ECO Version */
  972. +
  973. +/*--------------------------------------------------------------------------*/
  974. +/* Register Mask */
  975. +/*--------------------------------------------------------------------------*/
  976. +
  977. +/* MSDC_CFG mask */
  978. +#define MSDC_CFG_MODE (0x1 << 0) /* RW */
  979. +#define MSDC_CFG_CKPDN (0x1 << 1) /* RW */
  980. +#define MSDC_CFG_RST (0x1 << 2) /* RW */
  981. +#define MSDC_CFG_PIO (0x1 << 3) /* RW */
  982. +#define MSDC_CFG_CKDRVEN (0x1 << 4) /* RW */
  983. +#define MSDC_CFG_BV18SDT (0x1 << 5) /* RW */
  984. +#define MSDC_CFG_BV18PSS (0x1 << 6) /* R */
  985. +#define MSDC_CFG_CKSTB (0x1 << 7) /* R */
  986. +#define MSDC_CFG_CKDIV (0xff << 8) /* RW */
  987. +#define MSDC_CFG_CKMOD (0x3 << 16) /* RW */
  988. +
  989. +/* MSDC_IOCON mask */
  990. +#define MSDC_IOCON_SDR104CKS (0x1 << 0) /* RW */
  991. +#define MSDC_IOCON_RSPL (0x1 << 1) /* RW */
  992. +#define MSDC_IOCON_DSPL (0x1 << 2) /* RW */
  993. +#define MSDC_IOCON_DDLSEL (0x1 << 3) /* RW */
  994. +#define MSDC_IOCON_DDR50CKD (0x1 << 4) /* RW */
  995. +#define MSDC_IOCON_DSPLSEL (0x1 << 5) /* RW */
  996. +#define MSDC_IOCON_D0SPL (0x1 << 16) /* RW */
  997. +#define MSDC_IOCON_D1SPL (0x1 << 17) /* RW */
  998. +#define MSDC_IOCON_D2SPL (0x1 << 18) /* RW */
  999. +#define MSDC_IOCON_D3SPL (0x1 << 19) /* RW */
  1000. +#define MSDC_IOCON_D4SPL (0x1 << 20) /* RW */
  1001. +#define MSDC_IOCON_D5SPL (0x1 << 21) /* RW */
  1002. +#define MSDC_IOCON_D6SPL (0x1 << 22) /* RW */
  1003. +#define MSDC_IOCON_D7SPL (0x1 << 23) /* RW */
  1004. +#define MSDC_IOCON_RISCSZ (0x3 << 24) /* RW */
  1005. +
  1006. +/* MSDC_PS mask */
  1007. +#define MSDC_PS_CDEN (0x1 << 0) /* RW */
  1008. +#define MSDC_PS_CDSTS (0x1 << 1) /* R */
  1009. +#define MSDC_PS_CDDEBOUNCE (0xf << 12) /* RW */
  1010. +#define MSDC_PS_DAT (0xff << 16) /* R */
  1011. +#define MSDC_PS_CMD (0x1 << 24) /* R */
  1012. +#define MSDC_PS_WP (0x1UL<< 31) /* R */
  1013. +
  1014. +/* MSDC_INT mask */
  1015. +#define MSDC_INT_MMCIRQ (0x1 << 0) /* W1C */
  1016. +#define MSDC_INT_CDSC (0x1 << 1) /* W1C */
  1017. +#define MSDC_INT_ACMDRDY (0x1 << 3) /* W1C */
  1018. +#define MSDC_INT_ACMDTMO (0x1 << 4) /* W1C */
  1019. +#define MSDC_INT_ACMDCRCERR (0x1 << 5) /* W1C */
  1020. +#define MSDC_INT_DMAQ_EMPTY (0x1 << 6) /* W1C */
  1021. +#define MSDC_INT_SDIOIRQ (0x1 << 7) /* W1C */
  1022. +#define MSDC_INT_CMDRDY (0x1 << 8) /* W1C */
  1023. +#define MSDC_INT_CMDTMO (0x1 << 9) /* W1C */
  1024. +#define MSDC_INT_RSPCRCERR (0x1 << 10) /* W1C */
  1025. +#define MSDC_INT_CSTA (0x1 << 11) /* R */
  1026. +#define MSDC_INT_XFER_COMPL (0x1 << 12) /* W1C */
  1027. +#define MSDC_INT_DXFER_DONE (0x1 << 13) /* W1C */
  1028. +#define MSDC_INT_DATTMO (0x1 << 14) /* W1C */
  1029. +#define MSDC_INT_DATCRCERR (0x1 << 15) /* W1C */
  1030. +#define MSDC_INT_ACMD19_DONE (0x1 << 16) /* W1C */
  1031. +
  1032. +/* MSDC_INTEN mask */
  1033. +#define MSDC_INTEN_MMCIRQ (0x1 << 0) /* RW */
  1034. +#define MSDC_INTEN_CDSC (0x1 << 1) /* RW */
  1035. +#define MSDC_INTEN_ACMDRDY (0x1 << 3) /* RW */
  1036. +#define MSDC_INTEN_ACMDTMO (0x1 << 4) /* RW */
  1037. +#define MSDC_INTEN_ACMDCRCERR (0x1 << 5) /* RW */
  1038. +#define MSDC_INTEN_DMAQ_EMPTY (0x1 << 6) /* RW */
  1039. +#define MSDC_INTEN_SDIOIRQ (0x1 << 7) /* RW */
  1040. +#define MSDC_INTEN_CMDRDY (0x1 << 8) /* RW */
  1041. +#define MSDC_INTEN_CMDTMO (0x1 << 9) /* RW */
  1042. +#define MSDC_INTEN_RSPCRCERR (0x1 << 10) /* RW */
  1043. +#define MSDC_INTEN_CSTA (0x1 << 11) /* RW */
  1044. +#define MSDC_INTEN_XFER_COMPL (0x1 << 12) /* RW */
  1045. +#define MSDC_INTEN_DXFER_DONE (0x1 << 13) /* RW */
  1046. +#define MSDC_INTEN_DATTMO (0x1 << 14) /* RW */
  1047. +#define MSDC_INTEN_DATCRCERR (0x1 << 15) /* RW */
  1048. +#define MSDC_INTEN_ACMD19_DONE (0x1 << 16) /* RW */
  1049. +
  1050. +/* MSDC_FIFOCS mask */
  1051. +#define MSDC_FIFOCS_RXCNT (0xff << 0) /* R */
  1052. +#define MSDC_FIFOCS_TXCNT (0xff << 16) /* R */
  1053. +#define MSDC_FIFOCS_CLR (0x1UL<< 31) /* RW */
  1054. +
  1055. +/* SDC_CFG mask */
  1056. +#define SDC_CFG_SDIOINTWKUP (0x1 << 0) /* RW */
  1057. +#define SDC_CFG_INSWKUP (0x1 << 1) /* RW */
  1058. +#define SDC_CFG_BUSWIDTH (0x3 << 16) /* RW */
  1059. +#define SDC_CFG_SDIO (0x1 << 19) /* RW */
  1060. +#define SDC_CFG_SDIOIDE (0x1 << 20) /* RW */
  1061. +#define SDC_CFG_INTATGAP (0x1 << 21) /* RW */
  1062. +#define SDC_CFG_DTOC (0xffUL << 24) /* RW */
  1063. +
  1064. +/* SDC_CMD mask */
  1065. +#define SDC_CMD_OPC (0x3f << 0) /* RW */
  1066. +#define SDC_CMD_BRK (0x1 << 6) /* RW */
  1067. +#define SDC_CMD_RSPTYP (0x7 << 7) /* RW */
  1068. +#define SDC_CMD_DTYP (0x3 << 11) /* RW */
  1069. +#define SDC_CMD_DTYP (0x3 << 11) /* RW */
  1070. +#define SDC_CMD_RW (0x1 << 13) /* RW */
  1071. +#define SDC_CMD_STOP (0x1 << 14) /* RW */
  1072. +#define SDC_CMD_GOIRQ (0x1 << 15) /* RW */
  1073. +#define SDC_CMD_BLKLEN (0xfff<< 16) /* RW */
  1074. +#define SDC_CMD_AUTOCMD (0x3 << 28) /* RW */
  1075. +#define SDC_CMD_VOLSWTH (0x1 << 30) /* RW */
  1076. +
  1077. +/* SDC_STS mask */
  1078. +#define SDC_STS_SDCBUSY (0x1 << 0) /* RW */
  1079. +#define SDC_STS_CMDBUSY (0x1 << 1) /* RW */
  1080. +#define SDC_STS_SWR_COMPL (0x1 << 31) /* RW */
  1081. +
  1082. +/* SDC_DCRC_STS mask */
  1083. +#define SDC_DCRC_STS_NEG (0xf << 8) /* RO */
  1084. +#define SDC_DCRC_STS_POS (0xff << 0) /* RO */
  1085. +
  1086. +/* EMMC_CFG0 mask */
  1087. +#define EMMC_CFG0_BOOTSTART (0x1 << 0) /* W */
  1088. +#define EMMC_CFG0_BOOTSTOP (0x1 << 1) /* W */
  1089. +#define EMMC_CFG0_BOOTMODE (0x1 << 2) /* RW */
  1090. +#define EMMC_CFG0_BOOTACKDIS (0x1 << 3) /* RW */
  1091. +#define EMMC_CFG0_BOOTWDLY (0x7 << 12) /* RW */
  1092. +#define EMMC_CFG0_BOOTSUPP (0x1 << 15) /* RW */
  1093. +
  1094. +/* EMMC_CFG1 mask */
  1095. +#define EMMC_CFG1_BOOTDATTMC (0xfffff << 0) /* RW */
  1096. +#define EMMC_CFG1_BOOTACKTMC (0xfffUL << 20) /* RW */
  1097. +
  1098. +/* EMMC_STS mask */
  1099. +#define EMMC_STS_BOOTCRCERR (0x1 << 0) /* W1C */
  1100. +#define EMMC_STS_BOOTACKERR (0x1 << 1) /* W1C */
  1101. +#define EMMC_STS_BOOTDATTMO (0x1 << 2) /* W1C */
  1102. +#define EMMC_STS_BOOTACKTMO (0x1 << 3) /* W1C */
  1103. +#define EMMC_STS_BOOTUPSTATE (0x1 << 4) /* R */
  1104. +#define EMMC_STS_BOOTACKRCV (0x1 << 5) /* W1C */
  1105. +#define EMMC_STS_BOOTDATRCV (0x1 << 6) /* R */
  1106. +
  1107. +/* EMMC_IOCON mask */
  1108. +#define EMMC_IOCON_BOOTRST (0x1 << 0) /* RW */
  1109. +
  1110. +/* SDC_ACMD19_TRG mask */
  1111. +#define SDC_ACMD19_TRG_TUNESEL (0xf << 0) /* RW */
  1112. +
  1113. +/* MSDC_DMA_CTRL mask */
  1114. +#define MSDC_DMA_CTRL_START (0x1 << 0) /* W */
  1115. +#define MSDC_DMA_CTRL_STOP (0x1 << 1) /* W */
  1116. +#define MSDC_DMA_CTRL_RESUME (0x1 << 2) /* W */
  1117. +#define MSDC_DMA_CTRL_MODE (0x1 << 8) /* RW */
  1118. +#define MSDC_DMA_CTRL_LASTBUF (0x1 << 10) /* RW */
  1119. +#define MSDC_DMA_CTRL_BRUSTSZ (0x7 << 12) /* RW */
  1120. +#define MSDC_DMA_CTRL_XFERSZ (0xffffUL << 16)/* RW */
  1121. +
  1122. +/* MSDC_DMA_CFG mask */
  1123. +#define MSDC_DMA_CFG_STS (0x1 << 0) /* R */
  1124. +#define MSDC_DMA_CFG_DECSEN (0x1 << 1) /* RW */
  1125. +#define MSDC_DMA_CFG_BDCSERR (0x1 << 4) /* R */
  1126. +#define MSDC_DMA_CFG_GPDCSERR (0x1 << 5) /* R */
  1127. +
  1128. +/* MSDC_PATCH_BIT mask */
  1129. +#define MSDC_PATCH_BIT_WFLSMODE (0x1 << 0) /* RW */
  1130. +#define MSDC_PATCH_BIT_ODDSUPP (0x1 << 1) /* RW */
  1131. +#define MSDC_PATCH_BIT_CKGEN_CK (0x1 << 6) /* E2: Fixed to 1 */
  1132. +#define MSDC_PATCH_BIT_IODSSEL (0x1 << 16) /* RW */
  1133. +#define MSDC_PATCH_BIT_IOINTSEL (0x1 << 17) /* RW */
  1134. +#define MSDC_PATCH_BIT_BUSYDLY (0xf << 18) /* RW */
  1135. +#define MSDC_PATCH_BIT_WDOD (0xf << 22) /* RW */
  1136. +#define MSDC_PATCH_BIT_IDRTSEL (0x1 << 26) /* RW */
  1137. +#define MSDC_PATCH_BIT_CMDFSEL (0x1 << 27) /* RW */
  1138. +#define MSDC_PATCH_BIT_INTDLSEL (0x1 << 28) /* RW */
  1139. +#define MSDC_PATCH_BIT_SPCPUSH (0x1 << 29) /* RW */
  1140. +#define MSDC_PATCH_BIT_DECRCTMO (0x1 << 30) /* RW */
  1141. +
  1142. +/* MSDC_PATCH_BIT1 mask */
  1143. +#define MSDC_PATCH_BIT1_WRDAT_CRCS (0x7 << 3)
  1144. +#define MSDC_PATCH_BIT1_CMD_RSP (0x7 << 0)
  1145. +
  1146. +/* MSDC_PAD_CTL0 mask */
  1147. +#define MSDC_PAD_CTL0_CLKDRVN (0x7 << 0) /* RW */
  1148. +#define MSDC_PAD_CTL0_CLKDRVP (0x7 << 4) /* RW */
  1149. +#define MSDC_PAD_CTL0_CLKSR (0x1 << 8) /* RW */
  1150. +#define MSDC_PAD_CTL0_CLKPD (0x1 << 16) /* RW */
  1151. +#define MSDC_PAD_CTL0_CLKPU (0x1 << 17) /* RW */
  1152. +#define MSDC_PAD_CTL0_CLKSMT (0x1 << 18) /* RW */
  1153. +#define MSDC_PAD_CTL0_CLKIES (0x1 << 19) /* RW */
  1154. +#define MSDC_PAD_CTL0_CLKTDSEL (0xf << 20) /* RW */
  1155. +#define MSDC_PAD_CTL0_CLKRDSEL (0xffUL<< 24) /* RW */
  1156. +
  1157. +/* MSDC_PAD_CTL1 mask */
  1158. +#define MSDC_PAD_CTL1_CMDDRVN (0x7 << 0) /* RW */
  1159. +#define MSDC_PAD_CTL1_CMDDRVP (0x7 << 4) /* RW */
  1160. +#define MSDC_PAD_CTL1_CMDSR (0x1 << 8) /* RW */
  1161. +#define MSDC_PAD_CTL1_CMDPD (0x1 << 16) /* RW */
  1162. +#define MSDC_PAD_CTL1_CMDPU (0x1 << 17) /* RW */
  1163. +#define MSDC_PAD_CTL1_CMDSMT (0x1 << 18) /* RW */
  1164. +#define MSDC_PAD_CTL1_CMDIES (0x1 << 19) /* RW */
  1165. +#define MSDC_PAD_CTL1_CMDTDSEL (0xf << 20) /* RW */
  1166. +#define MSDC_PAD_CTL1_CMDRDSEL (0xffUL<< 24) /* RW */
  1167. +
  1168. +/* MSDC_PAD_CTL2 mask */
  1169. +#define MSDC_PAD_CTL2_DATDRVN (0x7 << 0) /* RW */
  1170. +#define MSDC_PAD_CTL2_DATDRVP (0x7 << 4) /* RW */
  1171. +#define MSDC_PAD_CTL2_DATSR (0x1 << 8) /* RW */
  1172. +#define MSDC_PAD_CTL2_DATPD (0x1 << 16) /* RW */
  1173. +#define MSDC_PAD_CTL2_DATPU (0x1 << 17) /* RW */
  1174. +#define MSDC_PAD_CTL2_DATIES (0x1 << 19) /* RW */
  1175. +#define MSDC_PAD_CTL2_DATSMT (0x1 << 18) /* RW */
  1176. +#define MSDC_PAD_CTL2_DATTDSEL (0xf << 20) /* RW */
  1177. +#define MSDC_PAD_CTL2_DATRDSEL (0xffUL<< 24) /* RW */
  1178. +
  1179. +/* MSDC_PAD_TUNE mask */
  1180. +#define MSDC_PAD_TUNE_DATWRDLY (0x1F << 0) /* RW */
  1181. +#define MSDC_PAD_TUNE_DATRRDLY (0x1F << 8) /* RW */
  1182. +#define MSDC_PAD_TUNE_CMDRDLY (0x1F << 16) /* RW */
  1183. +#define MSDC_PAD_TUNE_CMDRRDLY (0x1FUL << 22) /* RW */
  1184. +#define MSDC_PAD_TUNE_CLKTXDLY (0x1FUL << 27) /* RW */
  1185. +
  1186. +/* MSDC_DAT_RDDLY0/1 mask */
  1187. +#define MSDC_DAT_RDDLY0_D0 (0x1F << 0) /* RW */
  1188. +#define MSDC_DAT_RDDLY0_D1 (0x1F << 8) /* RW */
  1189. +#define MSDC_DAT_RDDLY0_D2 (0x1F << 16) /* RW */
  1190. +#define MSDC_DAT_RDDLY0_D3 (0x1F << 24) /* RW */
  1191. +
  1192. +#define MSDC_DAT_RDDLY1_D4 (0x1F << 0) /* RW */
  1193. +#define MSDC_DAT_RDDLY1_D5 (0x1F << 8) /* RW */
  1194. +#define MSDC_DAT_RDDLY1_D6 (0x1F << 16) /* RW */
  1195. +#define MSDC_DAT_RDDLY1_D7 (0x1F << 24) /* RW */
  1196. +
  1197. +#define MSDC_CKGEN_MSDC_DLY_SEL (0x1F<<10)
  1198. +#define MSDC_INT_DAT_LATCH_CK_SEL (0x7<<7)
  1199. +#define MSDC_CKGEN_MSDC_CK_SEL (0x1<<6)
  1200. +#define CARD_READY_FOR_DATA (1<<8)
  1201. +#define CARD_CURRENT_STATE(x) ((x&0x00001E00)>>9)
  1202. +
  1203. +/*--------------------------------------------------------------------------*/
  1204. +/* Descriptor Structure */
  1205. +/*--------------------------------------------------------------------------*/
  1206. +typedef struct {
  1207. + u32 hwo:1; /* could be changed by hw */
  1208. + u32 bdp:1;
  1209. + u32 rsv0:6;
  1210. + u32 chksum:8;
  1211. + u32 intr:1;
  1212. + u32 rsv1:15;
  1213. + void *next;
  1214. + void *ptr;
  1215. + u32 buflen:16;
  1216. + u32 extlen:8;
  1217. + u32 rsv2:8;
  1218. + u32 arg;
  1219. + u32 blknum;
  1220. + u32 cmd;
  1221. +} gpd_t;
  1222. +
  1223. +typedef struct {
  1224. + u32 eol:1;
  1225. + u32 rsv0:7;
  1226. + u32 chksum:8;
  1227. + u32 rsv1:1;
  1228. + u32 blkpad:1;
  1229. + u32 dwpad:1;
  1230. + u32 rsv2:13;
  1231. + void *next;
  1232. + void *ptr;
  1233. + u32 buflen:16;
  1234. + u32 rsv3:16;
  1235. +} bd_t;
  1236. +
  1237. +/*--------------------------------------------------------------------------*/
  1238. +/* Register Debugging Structure */
  1239. +/*--------------------------------------------------------------------------*/
  1240. +
  1241. +typedef struct {
  1242. + u32 msdc:1;
  1243. + u32 ckpwn:1;
  1244. + u32 rst:1;
  1245. + u32 pio:1;
  1246. + u32 ckdrven:1;
  1247. + u32 start18v:1;
  1248. + u32 pass18v:1;
  1249. + u32 ckstb:1;
  1250. + u32 ckdiv:8;
  1251. + u32 ckmod:2;
  1252. + u32 pad:14;
  1253. +} msdc_cfg_reg;
  1254. +typedef struct {
  1255. + u32 sdr104cksel:1;
  1256. + u32 rsmpl:1;
  1257. + u32 dsmpl:1;
  1258. + u32 ddlysel:1;
  1259. + u32 ddr50ckd:1;
  1260. + u32 dsplsel:1;
  1261. + u32 pad1:10;
  1262. + u32 d0spl:1;
  1263. + u32 d1spl:1;
  1264. + u32 d2spl:1;
  1265. + u32 d3spl:1;
  1266. + u32 d4spl:1;
  1267. + u32 d5spl:1;
  1268. + u32 d6spl:1;
  1269. + u32 d7spl:1;
  1270. + u32 riscsz:1;
  1271. + u32 pad2:7;
  1272. +} msdc_iocon_reg;
  1273. +typedef struct {
  1274. + u32 cden:1;
  1275. + u32 cdsts:1;
  1276. + u32 pad1:10;
  1277. + u32 cddebounce:4;
  1278. + u32 dat:8;
  1279. + u32 cmd:1;
  1280. + u32 pad2:6;
  1281. + u32 wp:1;
  1282. +} msdc_ps_reg;
  1283. +typedef struct {
  1284. + u32 mmcirq:1;
  1285. + u32 cdsc:1;
  1286. + u32 pad1:1;
  1287. + u32 atocmdrdy:1;
  1288. + u32 atocmdtmo:1;
  1289. + u32 atocmdcrc:1;
  1290. + u32 dmaqempty:1;
  1291. + u32 sdioirq:1;
  1292. + u32 cmdrdy:1;
  1293. + u32 cmdtmo:1;
  1294. + u32 rspcrc:1;
  1295. + u32 csta:1;
  1296. + u32 xfercomp:1;
  1297. + u32 dxferdone:1;
  1298. + u32 dattmo:1;
  1299. + u32 datcrc:1;
  1300. + u32 atocmd19done:1;
  1301. + u32 pad2:15;
  1302. +} msdc_int_reg;
  1303. +typedef struct {
  1304. + u32 mmcirq:1;
  1305. + u32 cdsc:1;
  1306. + u32 pad1:1;
  1307. + u32 atocmdrdy:1;
  1308. + u32 atocmdtmo:1;
  1309. + u32 atocmdcrc:1;
  1310. + u32 dmaqempty:1;
  1311. + u32 sdioirq:1;
  1312. + u32 cmdrdy:1;
  1313. + u32 cmdtmo:1;
  1314. + u32 rspcrc:1;
  1315. + u32 csta:1;
  1316. + u32 xfercomp:1;
  1317. + u32 dxferdone:1;
  1318. + u32 dattmo:1;
  1319. + u32 datcrc:1;
  1320. + u32 atocmd19done:1;
  1321. + u32 pad2:15;
  1322. +} msdc_inten_reg;
  1323. +typedef struct {
  1324. + u32 rxcnt:8;
  1325. + u32 pad1:8;
  1326. + u32 txcnt:8;
  1327. + u32 pad2:7;
  1328. + u32 clr:1;
  1329. +} msdc_fifocs_reg;
  1330. +typedef struct {
  1331. + u32 val;
  1332. +} msdc_txdat_reg;
  1333. +typedef struct {
  1334. + u32 val;
  1335. +} msdc_rxdat_reg;
  1336. +typedef struct {
  1337. + u32 sdiowkup:1;
  1338. + u32 inswkup:1;
  1339. + u32 pad1:14;
  1340. + u32 buswidth:2;
  1341. + u32 pad2:1;
  1342. + u32 sdio:1;
  1343. + u32 sdioide:1;
  1344. + u32 intblkgap:1;
  1345. + u32 pad4:2;
  1346. + u32 dtoc:8;
  1347. +} sdc_cfg_reg;
  1348. +typedef struct {
  1349. + u32 cmd:6;
  1350. + u32 brk:1;
  1351. + u32 rsptyp:3;
  1352. + u32 pad1:1;
  1353. + u32 dtype:2;
  1354. + u32 rw:1;
  1355. + u32 stop:1;
  1356. + u32 goirq:1;
  1357. + u32 blklen:12;
  1358. + u32 atocmd:2;
  1359. + u32 volswth:1;
  1360. + u32 pad2:1;
  1361. +} sdc_cmd_reg;
  1362. +typedef struct {
  1363. + u32 arg;
  1364. +} sdc_arg_reg;
  1365. +typedef struct {
  1366. + u32 sdcbusy:1;
  1367. + u32 cmdbusy:1;
  1368. + u32 pad:29;
  1369. + u32 swrcmpl:1;
  1370. +} sdc_sts_reg;
  1371. +typedef struct {
  1372. + u32 val;
  1373. +} sdc_resp0_reg;
  1374. +typedef struct {
  1375. + u32 val;
  1376. +} sdc_resp1_reg;
  1377. +typedef struct {
  1378. + u32 val;
  1379. +} sdc_resp2_reg;
  1380. +typedef struct {
  1381. + u32 val;
  1382. +} sdc_resp3_reg;
  1383. +typedef struct {
  1384. + u32 num;
  1385. +} sdc_blknum_reg;
  1386. +typedef struct {
  1387. + u32 sts;
  1388. +} sdc_csts_reg;
  1389. +typedef struct {
  1390. + u32 sts;
  1391. +} sdc_cstsen_reg;
  1392. +typedef struct {
  1393. + u32 datcrcsts:8;
  1394. + u32 ddrcrcsts:4;
  1395. + u32 pad:20;
  1396. +} sdc_datcrcsts_reg;
  1397. +typedef struct {
  1398. + u32 bootstart:1;
  1399. + u32 bootstop:1;
  1400. + u32 bootmode:1;
  1401. + u32 pad1:9;
  1402. + u32 bootwaidly:3;
  1403. + u32 bootsupp:1;
  1404. + u32 pad2:16;
  1405. +} emmc_cfg0_reg;
  1406. +typedef struct {
  1407. + u32 bootcrctmc:16;
  1408. + u32 pad:4;
  1409. + u32 bootacktmc:12;
  1410. +} emmc_cfg1_reg;
  1411. +typedef struct {
  1412. + u32 bootcrcerr:1;
  1413. + u32 bootackerr:1;
  1414. + u32 bootdattmo:1;
  1415. + u32 bootacktmo:1;
  1416. + u32 bootupstate:1;
  1417. + u32 bootackrcv:1;
  1418. + u32 bootdatrcv:1;
  1419. + u32 pad:25;
  1420. +} emmc_sts_reg;
  1421. +typedef struct {
  1422. + u32 bootrst:1;
  1423. + u32 pad:31;
  1424. +} emmc_iocon_reg;
  1425. +typedef struct {
  1426. + u32 val;
  1427. +} msdc_acmd_resp_reg;
  1428. +typedef struct {
  1429. + u32 tunesel:4;
  1430. + u32 pad:28;
  1431. +} msdc_acmd19_trg_reg;
  1432. +typedef struct {
  1433. + u32 val;
  1434. +} msdc_acmd19_sts_reg;
  1435. +typedef struct {
  1436. + u32 addr;
  1437. +} msdc_dma_sa_reg;
  1438. +typedef struct {
  1439. + u32 addr;
  1440. +} msdc_dma_ca_reg;
  1441. +typedef struct {
  1442. + u32 start:1;
  1443. + u32 stop:1;
  1444. + u32 resume:1;
  1445. + u32 pad1:5;
  1446. + u32 mode:1;
  1447. + u32 pad2:1;
  1448. + u32 lastbuf:1;
  1449. + u32 pad3:1;
  1450. + u32 brustsz:3;
  1451. + u32 pad4:1;
  1452. + u32 xfersz:16;
  1453. +} msdc_dma_ctrl_reg;
  1454. +typedef struct {
  1455. + u32 status:1;
  1456. + u32 decsen:1;
  1457. + u32 pad1:2;
  1458. + u32 bdcsen:1;
  1459. + u32 gpdcsen:1;
  1460. + u32 pad2:26;
  1461. +} msdc_dma_cfg_reg;
  1462. +typedef struct {
  1463. + u32 sel:16;
  1464. + u32 pad2:16;
  1465. +} msdc_dbg_sel_reg;
  1466. +typedef struct {
  1467. + u32 val;
  1468. +} msdc_dbg_out_reg;
  1469. +typedef struct {
  1470. + u32 clkdrvn:3;
  1471. + u32 rsv0:1;
  1472. + u32 clkdrvp:3;
  1473. + u32 rsv1:1;
  1474. + u32 clksr:1;
  1475. + u32 rsv2:7;
  1476. + u32 clkpd:1;
  1477. + u32 clkpu:1;
  1478. + u32 clksmt:1;
  1479. + u32 clkies:1;
  1480. + u32 clktdsel:4;
  1481. + u32 clkrdsel:8;
  1482. +} msdc_pad_ctl0_reg;
  1483. +typedef struct {
  1484. + u32 cmddrvn:3;
  1485. + u32 rsv0:1;
  1486. + u32 cmddrvp:3;
  1487. + u32 rsv1:1;
  1488. + u32 cmdsr:1;
  1489. + u32 rsv2:7;
  1490. + u32 cmdpd:1;
  1491. + u32 cmdpu:1;
  1492. + u32 cmdsmt:1;
  1493. + u32 cmdies:1;
  1494. + u32 cmdtdsel:4;
  1495. + u32 cmdrdsel:8;
  1496. +} msdc_pad_ctl1_reg;
  1497. +typedef struct {
  1498. + u32 datdrvn:3;
  1499. + u32 rsv0:1;
  1500. + u32 datdrvp:3;
  1501. + u32 rsv1:1;
  1502. + u32 datsr:1;
  1503. + u32 rsv2:7;
  1504. + u32 datpd:1;
  1505. + u32 datpu:1;
  1506. + u32 datsmt:1;
  1507. + u32 daties:1;
  1508. + u32 dattdsel:4;
  1509. + u32 datrdsel:8;
  1510. +} msdc_pad_ctl2_reg;
  1511. +typedef struct {
  1512. + u32 wrrxdly:3;
  1513. + u32 pad1:5;
  1514. + u32 rdrxdly:8;
  1515. + u32 pad2:16;
  1516. +} msdc_pad_tune_reg;
  1517. +typedef struct {
  1518. + u32 dat0:5;
  1519. + u32 rsv0:3;
  1520. + u32 dat1:5;
  1521. + u32 rsv1:3;
  1522. + u32 dat2:5;
  1523. + u32 rsv2:3;
  1524. + u32 dat3:5;
  1525. + u32 rsv3:3;
  1526. +} msdc_dat_rddly0;
  1527. +typedef struct {
  1528. + u32 dat4:5;
  1529. + u32 rsv4:3;
  1530. + u32 dat5:5;
  1531. + u32 rsv5:3;
  1532. + u32 dat6:5;
  1533. + u32 rsv6:3;
  1534. + u32 dat7:5;
  1535. + u32 rsv7:3;
  1536. +} msdc_dat_rddly1;
  1537. +typedef struct {
  1538. + u32 dbg0sel:8;
  1539. + u32 dbg1sel:6;
  1540. + u32 pad1:2;
  1541. + u32 dbg2sel:6;
  1542. + u32 pad2:2;
  1543. + u32 dbg3sel:6;
  1544. + u32 pad3:2;
  1545. +} msdc_hw_dbg_reg;
  1546. +typedef struct {
  1547. + u32 val;
  1548. +} msdc_version_reg;
  1549. +typedef struct {
  1550. + u32 val;
  1551. +} msdc_eco_ver_reg;
  1552. +
  1553. +struct msdc_regs {
  1554. + msdc_cfg_reg msdc_cfg; /* base+0x00h */
  1555. + msdc_iocon_reg msdc_iocon; /* base+0x04h */
  1556. + msdc_ps_reg msdc_ps; /* base+0x08h */
  1557. + msdc_int_reg msdc_int; /* base+0x0ch */
  1558. + msdc_inten_reg msdc_inten; /* base+0x10h */
  1559. + msdc_fifocs_reg msdc_fifocs; /* base+0x14h */
  1560. + msdc_txdat_reg msdc_txdat; /* base+0x18h */
  1561. + msdc_rxdat_reg msdc_rxdat; /* base+0x1ch */
  1562. + u32 rsv1[4];
  1563. + sdc_cfg_reg sdc_cfg; /* base+0x30h */
  1564. + sdc_cmd_reg sdc_cmd; /* base+0x34h */
  1565. + sdc_arg_reg sdc_arg; /* base+0x38h */
  1566. + sdc_sts_reg sdc_sts; /* base+0x3ch */
  1567. + sdc_resp0_reg sdc_resp0; /* base+0x40h */
  1568. + sdc_resp1_reg sdc_resp1; /* base+0x44h */
  1569. + sdc_resp2_reg sdc_resp2; /* base+0x48h */
  1570. + sdc_resp3_reg sdc_resp3; /* base+0x4ch */
  1571. + sdc_blknum_reg sdc_blknum; /* base+0x50h */
  1572. + u32 rsv2[1];
  1573. + sdc_csts_reg sdc_csts; /* base+0x58h */
  1574. + sdc_cstsen_reg sdc_cstsen; /* base+0x5ch */
  1575. + sdc_datcrcsts_reg sdc_dcrcsta; /* base+0x60h */
  1576. + u32 rsv3[3];
  1577. + emmc_cfg0_reg emmc_cfg0; /* base+0x70h */
  1578. + emmc_cfg1_reg emmc_cfg1; /* base+0x74h */
  1579. + emmc_sts_reg emmc_sts; /* base+0x78h */
  1580. + emmc_iocon_reg emmc_iocon; /* base+0x7ch */
  1581. + msdc_acmd_resp_reg acmd_resp; /* base+0x80h */
  1582. + msdc_acmd19_trg_reg acmd19_trg; /* base+0x84h */
  1583. + msdc_acmd19_sts_reg acmd19_sts; /* base+0x88h */
  1584. + u32 rsv4[1];
  1585. + msdc_dma_sa_reg dma_sa; /* base+0x90h */
  1586. + msdc_dma_ca_reg dma_ca; /* base+0x94h */
  1587. + msdc_dma_ctrl_reg dma_ctrl; /* base+0x98h */
  1588. + msdc_dma_cfg_reg dma_cfg; /* base+0x9ch */
  1589. + msdc_dbg_sel_reg dbg_sel; /* base+0xa0h */
  1590. + msdc_dbg_out_reg dbg_out; /* base+0xa4h */
  1591. + u32 rsv5[2];
  1592. + u32 patch0; /* base+0xb0h */
  1593. + u32 patch1; /* base+0xb4h */
  1594. + u32 rsv6[10];
  1595. + msdc_pad_ctl0_reg pad_ctl0; /* base+0xe0h */
  1596. + msdc_pad_ctl1_reg pad_ctl1; /* base+0xe4h */
  1597. + msdc_pad_ctl2_reg pad_ctl2; /* base+0xe8h */
  1598. + msdc_pad_tune_reg pad_tune; /* base+0xech */
  1599. + msdc_dat_rddly0 dat_rddly0; /* base+0xf0h */
  1600. + msdc_dat_rddly1 dat_rddly1; /* base+0xf4h */
  1601. + msdc_hw_dbg_reg hw_dbg; /* base+0xf8h */
  1602. + u32 rsv7[1];
  1603. + msdc_version_reg version; /* base+0x100h */
  1604. + msdc_eco_ver_reg eco_ver; /* base+0x104h */
  1605. +};
  1606. +
  1607. +struct scatterlist_ex {
  1608. + u32 cmd;
  1609. + u32 arg;
  1610. + u32 sglen;
  1611. + struct scatterlist *sg;
  1612. +};
  1613. +
  1614. +#define DMA_FLAG_NONE (0x00000000)
  1615. +#define DMA_FLAG_EN_CHKSUM (0x00000001)
  1616. +#define DMA_FLAG_PAD_BLOCK (0x00000002)
  1617. +#define DMA_FLAG_PAD_DWORD (0x00000004)
  1618. +
  1619. +struct msdc_dma {
  1620. + u32 flags; /* flags */
  1621. + u32 xfersz; /* xfer size in bytes */
  1622. + u32 sglen; /* size of scatter list */
  1623. + u32 blklen; /* block size */
  1624. + struct scatterlist *sg; /* I/O scatter list */
  1625. + struct scatterlist_ex *esg; /* extended I/O scatter list */
  1626. + u8 mode; /* dma mode */
  1627. + u8 burstsz; /* burst size */
  1628. + u8 intr; /* dma done interrupt */
  1629. + u8 padding; /* padding */
  1630. + u32 cmd; /* enhanced mode command */
  1631. + u32 arg; /* enhanced mode arg */
  1632. + u32 rsp; /* enhanced mode command response */
  1633. + u32 autorsp; /* auto command response */
  1634. +
  1635. + gpd_t *gpd; /* pointer to gpd array */
  1636. + bd_t *bd; /* pointer to bd array */
  1637. + dma_addr_t gpd_addr; /* the physical address of gpd array */
  1638. + dma_addr_t bd_addr; /* the physical address of bd array */
  1639. + u32 used_gpd; /* the number of used gpd elements */
  1640. + u32 used_bd; /* the number of used bd elements */
  1641. +};
  1642. +
  1643. +struct msdc_host
  1644. +{
  1645. + struct msdc_hw *hw;
  1646. +
  1647. + struct mmc_host *mmc; /* mmc structure */
  1648. + struct mmc_command *cmd;
  1649. + struct mmc_data *data;
  1650. + struct mmc_request *mrq;
  1651. + int cmd_rsp;
  1652. + int cmd_rsp_done;
  1653. + int cmd_r1b_done;
  1654. +
  1655. + int error;
  1656. + spinlock_t lock; /* mutex */
  1657. + struct semaphore sem;
  1658. +
  1659. + u32 blksz; /* host block size */
  1660. + u32 base; /* host base address */
  1661. + int id; /* host id */
  1662. + int pwr_ref; /* core power reference count */
  1663. +
  1664. + u32 xfer_size; /* total transferred size */
  1665. +
  1666. + struct msdc_dma dma; /* dma channel */
  1667. + u32 dma_addr; /* dma transfer address */
  1668. + u32 dma_left_size; /* dma transfer left size */
  1669. + u32 dma_xfer_size; /* dma transfer size in bytes */
  1670. + int dma_xfer; /* dma transfer mode */
  1671. +
  1672. + u32 timeout_ns; /* data timeout ns */
  1673. + u32 timeout_clks; /* data timeout clks */
  1674. +
  1675. + atomic_t abort; /* abort transfer */
  1676. +
  1677. + int irq; /* host interrupt */
  1678. +
  1679. + struct tasklet_struct card_tasklet;
  1680. +#if 0
  1681. + struct work_struct card_workqueue;
  1682. +#else
  1683. + struct delayed_work card_delaywork;
  1684. +#endif
  1685. +
  1686. + struct completion cmd_done;
  1687. + struct completion xfer_done;
  1688. + struct pm_message pm_state;
  1689. +
  1690. + u32 mclk; /* mmc subsystem clock */
  1691. + u32 hclk; /* host clock speed */
  1692. + u32 sclk; /* SD/MS clock speed */
  1693. + u8 core_clkon; /* Host core clock on ? */
  1694. + u8 card_clkon; /* Card clock on ? */
  1695. + u8 core_power; /* core power */
  1696. + u8 power_mode; /* host power mode */
  1697. + u8 card_inserted; /* card inserted ? */
  1698. + u8 suspend; /* host suspended ? */
  1699. + u8 reserved;
  1700. + u8 app_cmd; /* for app command */
  1701. + u32 app_cmd_arg;
  1702. + u64 starttime;
  1703. +};
  1704. +
  1705. +static inline unsigned int uffs(unsigned int x)
  1706. +{
  1707. + unsigned int r = 1;
  1708. +
  1709. + if (!x)
  1710. + return 0;
  1711. + if (!(x & 0xffff)) {
  1712. + x >>= 16;
  1713. + r += 16;
  1714. + }
  1715. + if (!(x & 0xff)) {
  1716. + x >>= 8;
  1717. + r += 8;
  1718. + }
  1719. + if (!(x & 0xf)) {
  1720. + x >>= 4;
  1721. + r += 4;
  1722. + }
  1723. + if (!(x & 3)) {
  1724. + x >>= 2;
  1725. + r += 2;
  1726. + }
  1727. + if (!(x & 1)) {
  1728. + x >>= 1;
  1729. + r += 1;
  1730. + }
  1731. + return r;
  1732. +}
  1733. +#define sdr_read8(reg) __raw_readb(reg)
  1734. +#define sdr_read16(reg) __raw_readw(reg)
  1735. +#define sdr_read32(reg) __raw_readl(reg)
  1736. +#define sdr_write8(reg,val) __raw_writeb(val,reg)
  1737. +#define sdr_write16(reg,val) __raw_writew(val,reg)
  1738. +#define sdr_write32(reg,val) __raw_writel(val,reg)
  1739. +
  1740. +#define sdr_set_bits(reg,bs) ((*(volatile u32*)(reg)) |= (u32)(bs))
  1741. +#define sdr_clr_bits(reg,bs) ((*(volatile u32*)(reg)) &= ~((u32)(bs)))
  1742. +
  1743. +#define sdr_set_field(reg,field,val) \
  1744. + do { \
  1745. + volatile unsigned int tv = sdr_read32(reg); \
  1746. + tv &= ~(field); \
  1747. + tv |= ((val) << (uffs((unsigned int)field) - 1)); \
  1748. + sdr_write32(reg,tv); \
  1749. + } while(0)
  1750. +#define sdr_get_field(reg,field,val) \
  1751. + do { \
  1752. + volatile unsigned int tv = sdr_read32(reg); \
  1753. + val = ((tv & (field)) >> (uffs((unsigned int)field) - 1)); \
  1754. + } while(0)
  1755. +
  1756. +#endif
  1757. +
  1758. --- /dev/null
  1759. +++ b/drivers/mmc/host/mtk-mmc/sd.c
  1760. @@ -0,0 +1,3067 @@
  1761. +/* Copyright Statement:
  1762. + *
  1763. + * This software/firmware and related documentation ("MediaTek Software") are
  1764. + * protected under relevant copyright laws. The information contained herein
  1765. + * is confidential and proprietary to MediaTek Inc. and/or its licensors.
  1766. + * Without the prior written permission of MediaTek inc. and/or its licensors,
  1767. + * any reproduction, modification, use or disclosure of MediaTek Software,
  1768. + * and information contained herein, in whole or in part, shall be strictly prohibited.
  1769. + *
  1770. + * MediaTek Inc. (C) 2010. All rights reserved.
  1771. + *
  1772. + * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  1773. + * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
  1774. + * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
  1775. + * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
  1776. + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
  1777. + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
  1778. + * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
  1779. + * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
  1780. + * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
  1781. + * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
  1782. + * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
  1783. + * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
  1784. + * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  1785. + * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
  1786. + * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
  1787. + * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
  1788. + * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
  1789. + * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
  1790. + *
  1791. + * The following software/firmware and/or related documentation ("MediaTek Software")
  1792. + * have been modified by MediaTek Inc. All revisions are subject to any receiver's
  1793. + * applicable license agreements with MediaTek Inc.
  1794. + */
  1795. +
  1796. +#include <linux/module.h>
  1797. +#include <linux/moduleparam.h>
  1798. +#include <linux/init.h>
  1799. +#include <linux/spinlock.h>
  1800. +#include <linux/timer.h>
  1801. +#include <linux/ioport.h>
  1802. +#include <linux/device.h>
  1803. +#include <linux/platform_device.h>
  1804. +#include <linux/interrupt.h>
  1805. +#include <linux/delay.h>
  1806. +#include <linux/blkdev.h>
  1807. +#include <linux/slab.h>
  1808. +#include <linux/mmc/host.h>
  1809. +#include <linux/mmc/card.h>
  1810. +#include <linux/mmc/core.h>
  1811. +#include <linux/mmc/mmc.h>
  1812. +#include <linux/mmc/sd.h>
  1813. +#include <linux/mmc/sdio.h>
  1814. +#include <linux/dma-mapping.h>
  1815. +
  1816. +/* +++ by chhung */
  1817. +#include <linux/types.h>
  1818. +#include <linux/kernel.h>
  1819. +#include <linux/version.h>
  1820. +#include <linux/pm.h>
  1821. +#include <linux/of.h>
  1822. +
  1823. +#define MSDC_SMPL_FALLING (1)
  1824. +#define MSDC_CD_PIN_EN (1 << 0) /* card detection pin is wired */
  1825. +#define MSDC_WP_PIN_EN (1 << 1) /* write protection pin is wired */
  1826. +#define MSDC_REMOVABLE (1 << 5) /* removable slot */
  1827. +#define MSDC_SYS_SUSPEND (1 << 6) /* suspended by system */
  1828. +#define MSDC_HIGHSPEED (1 << 7)
  1829. +
  1830. +//#define IRQ_SDC 14 //MT7620 /*FIXME*/
  1831. +#ifdef CONFIG_SOC_MT7621
  1832. +#define RALINK_SYSCTL_BASE 0xbe000000
  1833. +#define RALINK_MSDC_BASE 0xbe130000
  1834. +#else
  1835. +#define RALINK_SYSCTL_BASE 0xb0000000
  1836. +#define RALINK_MSDC_BASE 0xb0130000
  1837. +#endif
  1838. +#define IRQ_SDC 22 /*FIXME*/
  1839. +
  1840. +#include <asm/dma.h>
  1841. +/* end of +++ */
  1842. +
  1843. +
  1844. +#include <asm/mach-ralink/ralink_regs.h>
  1845. +
  1846. +#if 0 /* --- by chhung */
  1847. +#include <mach/board.h>
  1848. +#include <mach/mt6575_devs.h>
  1849. +#include <mach/mt6575_typedefs.h>
  1850. +#include <mach/mt6575_clock_manager.h>
  1851. +#include <mach/mt6575_pm_ldo.h>
  1852. +//#include <mach/mt6575_pll.h>
  1853. +//#include <mach/mt6575_gpio.h>
  1854. +//#include <mach/mt6575_gpt_sw.h>
  1855. +#include <asm/tcm.h>
  1856. +// #include <mach/mt6575_gpt.h>
  1857. +#endif /* end of --- */
  1858. +
  1859. +#include "mt6575_sd.h"
  1860. +#include "dbg.h"
  1861. +
  1862. +/* +++ by chhung */
  1863. +#include "board.h"
  1864. +/* end of +++ */
  1865. +
  1866. +#if 0 /* --- by chhung */
  1867. +#define isb() __asm__ __volatile__ ("" : : : "memory")
  1868. +#define dsb() __asm__ __volatile__ ("mcr p15, 0, %0, c7, c10, 4" \
  1869. + : : "r" (0) : "memory")
  1870. +#define dmb() __asm__ __volatile__ ("" : : : "memory")
  1871. +#endif /* end of --- */
  1872. +
  1873. +#define DRV_NAME "mtk-sd"
  1874. +
  1875. +#define HOST_MAX_NUM (1) /* +/- by chhung */
  1876. +
  1877. +#if defined (CONFIG_SOC_MT7620)
  1878. +#define HOST_MAX_MCLK (48000000) /* +/- by chhung */
  1879. +#elif defined (CONFIG_SOC_MT7621)
  1880. +#define HOST_MAX_MCLK (50000000) /* +/- by chhung */
  1881. +#endif
  1882. +#define HOST_MIN_MCLK (260000)
  1883. +
  1884. +#define HOST_MAX_BLKSZ (2048)
  1885. +
  1886. +#define MSDC_OCR_AVAIL (MMC_VDD_28_29 | MMC_VDD_29_30 | MMC_VDD_30_31 | MMC_VDD_31_32 | MMC_VDD_32_33)
  1887. +
  1888. +#define GPIO_PULL_DOWN (0)
  1889. +#define GPIO_PULL_UP (1)
  1890. +
  1891. +#if 0 /* --- by chhung */
  1892. +#define MSDC_CLKSRC_REG (0xf100000C)
  1893. +#define PDN_REG (0xF1000010)
  1894. +#endif /* end of --- */
  1895. +
  1896. +#define DEFAULT_DEBOUNCE (8) /* 8 cycles */
  1897. +#define DEFAULT_DTOC (40) /* data timeout counter. 65536x40 sclk. */
  1898. +
  1899. +#define CMD_TIMEOUT (HZ/10) /* 100ms */
  1900. +#define DAT_TIMEOUT (HZ/2 * 5) /* 500ms x5 */
  1901. +
  1902. +#define MAX_DMA_CNT (64 * 1024 - 512) /* a single transaction for WIFI may be 50K*/
  1903. +
  1904. +#define MAX_GPD_NUM (1 + 1) /* one null gpd */
  1905. +#define MAX_BD_NUM (1024)
  1906. +#define MAX_BD_PER_GPD (MAX_BD_NUM)
  1907. +
  1908. +#define MAX_HW_SGMTS (MAX_BD_NUM)
  1909. +#define MAX_PHY_SGMTS (MAX_BD_NUM)
  1910. +#define MAX_SGMT_SZ (MAX_DMA_CNT)
  1911. +#define MAX_REQ_SZ (MAX_SGMT_SZ * 8)
  1912. +
  1913. +#ifdef MT6575_SD_DEBUG
  1914. +static struct msdc_regs *msdc_reg[HOST_MAX_NUM];
  1915. +#endif
  1916. +
  1917. +static int mtk_sw_poll;
  1918. +
  1919. +static int cd_active_low = 1;
  1920. +
  1921. +//=================================
  1922. +#define PERI_MSDC0_PDN (15)
  1923. +//#define PERI_MSDC1_PDN (16)
  1924. +//#define PERI_MSDC2_PDN (17)
  1925. +//#define PERI_MSDC3_PDN (18)
  1926. +
  1927. +struct msdc_host *msdc_6575_host[] = {NULL,NULL,NULL,NULL};
  1928. +#if 0 /* --- by chhung */
  1929. +/* gate means clock power down */
  1930. +static int g_clk_gate = 0;
  1931. +#define msdc_gate_clock(id) \
  1932. + do { \
  1933. + g_clk_gate &= ~(1 << ((id) + PERI_MSDC0_PDN)); \
  1934. + } while(0)
  1935. +/* not like power down register. 1 means clock on. */
  1936. +#define msdc_ungate_clock(id) \
  1937. + do { \
  1938. + g_clk_gate |= 1 << ((id) + PERI_MSDC0_PDN); \
  1939. + } while(0)
  1940. +
  1941. +// do we need sync object or not
  1942. +void msdc_clk_status(int * status)
  1943. +{
  1944. + *status = g_clk_gate;
  1945. +}
  1946. +#endif /* end of --- */
  1947. +
  1948. +/* +++ by chhung */
  1949. +struct msdc_hw msdc0_hw = {
  1950. + .clk_src = 0,
  1951. + .cmd_edge = MSDC_SMPL_FALLING,
  1952. + .data_edge = MSDC_SMPL_FALLING,
  1953. + .clk_drv = 4,
  1954. + .cmd_drv = 4,
  1955. + .dat_drv = 4,
  1956. + .data_pins = 4,
  1957. + .data_offset = 0,
  1958. + .flags = MSDC_SYS_SUSPEND | MSDC_CD_PIN_EN | MSDC_REMOVABLE | MSDC_HIGHSPEED,
  1959. +// .flags = MSDC_SYS_SUSPEND | MSDC_WP_PIN_EN | MSDC_CD_PIN_EN | MSDC_REMOVABLE,
  1960. +};
  1961. +
  1962. +static struct resource mtk_sd_resources[] = {
  1963. + [0] = {
  1964. + .start = RALINK_MSDC_BASE,
  1965. + .end = RALINK_MSDC_BASE+0x3fff,
  1966. + .flags = IORESOURCE_MEM,
  1967. + },
  1968. + [1] = {
  1969. + .start = IRQ_SDC, /*FIXME*/
  1970. + .end = IRQ_SDC, /*FIXME*/
  1971. + .flags = IORESOURCE_IRQ,
  1972. + },
  1973. +};
  1974. +
  1975. +static struct platform_device mtk_sd_device = {
  1976. + .name = "mtk-sd",
  1977. + .id = 0,
  1978. + .num_resources = ARRAY_SIZE(mtk_sd_resources),
  1979. + .resource = mtk_sd_resources,
  1980. +};
  1981. +/* end of +++ */
  1982. +
  1983. +static int msdc_rsp[] = {
  1984. + 0, /* RESP_NONE */
  1985. + 1, /* RESP_R1 */
  1986. + 2, /* RESP_R2 */
  1987. + 3, /* RESP_R3 */
  1988. + 4, /* RESP_R4 */
  1989. + 1, /* RESP_R5 */
  1990. + 1, /* RESP_R6 */
  1991. + 1, /* RESP_R7 */
  1992. + 7, /* RESP_R1b */
  1993. +};
  1994. +
  1995. +/* For Inhanced DMA */
  1996. +#define msdc_init_gpd_ex(gpd,extlen,cmd,arg,blknum) \
  1997. + do { \
  1998. + ((gpd_t*)gpd)->extlen = extlen; \
  1999. + ((gpd_t*)gpd)->cmd = cmd; \
  2000. + ((gpd_t*)gpd)->arg = arg; \
  2001. + ((gpd_t*)gpd)->blknum = blknum; \
  2002. + }while(0)
  2003. +
  2004. +#define msdc_init_bd(bd, blkpad, dwpad, dptr, dlen) \
  2005. + do { \
  2006. + BUG_ON(dlen > 0xFFFFUL); \
  2007. + ((bd_t*)bd)->blkpad = blkpad; \
  2008. + ((bd_t*)bd)->dwpad = dwpad; \
  2009. + ((bd_t*)bd)->ptr = (void*)dptr; \
  2010. + ((bd_t*)bd)->buflen = dlen; \
  2011. + }while(0)
  2012. +
  2013. +#define msdc_txfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16)
  2014. +#define msdc_rxfifocnt() ((sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) >> 0)
  2015. +#define msdc_fifo_write32(v) sdr_write32(MSDC_TXDATA, (v))
  2016. +#define msdc_fifo_write8(v) sdr_write8(MSDC_TXDATA, (v))
  2017. +#define msdc_fifo_read32() sdr_read32(MSDC_RXDATA)
  2018. +#define msdc_fifo_read8() sdr_read8(MSDC_RXDATA)
  2019. +
  2020. +
  2021. +#define msdc_dma_on() sdr_clr_bits(MSDC_CFG, MSDC_CFG_PIO)
  2022. +#define msdc_dma_off() sdr_set_bits(MSDC_CFG, MSDC_CFG_PIO)
  2023. +
  2024. +#define msdc_retry(expr,retry,cnt) \
  2025. + do { \
  2026. + int backup = cnt; \
  2027. + while (retry) { \
  2028. + if (!(expr)) break; \
  2029. + if (cnt-- == 0) { \
  2030. + retry--; mdelay(1); cnt = backup; \
  2031. + } \
  2032. + } \
  2033. + WARN_ON(retry == 0); \
  2034. + } while(0)
  2035. +
  2036. +#if 0 /* --- by chhung */
  2037. +#define msdc_reset() \
  2038. + do { \
  2039. + int retry = 3, cnt = 1000; \
  2040. + sdr_set_bits(MSDC_CFG, MSDC_CFG_RST); \
  2041. + dsb(); \
  2042. + msdc_retry(sdr_read32(MSDC_CFG) & MSDC_CFG_RST, retry, cnt); \
  2043. + } while(0)
  2044. +#else
  2045. +#define msdc_reset() \
  2046. + do { \
  2047. + int retry = 3, cnt = 1000; \
  2048. + sdr_set_bits(MSDC_CFG, MSDC_CFG_RST); \
  2049. + msdc_retry(sdr_read32(MSDC_CFG) & MSDC_CFG_RST, retry, cnt); \
  2050. + } while(0)
  2051. +#endif /* end of +/- */
  2052. +
  2053. +#define msdc_clr_int() \
  2054. + do { \
  2055. + volatile u32 val = sdr_read32(MSDC_INT); \
  2056. + sdr_write32(MSDC_INT, val); \
  2057. + } while(0)
  2058. +
  2059. +#define msdc_clr_fifo() \
  2060. + do { \
  2061. + int retry = 3, cnt = 1000; \
  2062. + sdr_set_bits(MSDC_FIFOCS, MSDC_FIFOCS_CLR); \
  2063. + msdc_retry(sdr_read32(MSDC_FIFOCS) & MSDC_FIFOCS_CLR, retry, cnt); \
  2064. + } while(0)
  2065. +
  2066. +#define msdc_irq_save(val) \
  2067. + do { \
  2068. + val = sdr_read32(MSDC_INTEN); \
  2069. + sdr_clr_bits(MSDC_INTEN, val); \
  2070. + } while(0)
  2071. +
  2072. +#define msdc_irq_restore(val) \
  2073. + do { \
  2074. + sdr_set_bits(MSDC_INTEN, val); \
  2075. + } while(0)
  2076. +
  2077. +/* clock source for host: global */
  2078. +#if defined (CONFIG_SOC_MT7620)
  2079. +static u32 hclks[] = {48000000}; /* +/- by chhung */
  2080. +#elif defined (CONFIG_SOC_MT7621)
  2081. +static u32 hclks[] = {50000000}; /* +/- by chhung */
  2082. +#endif
  2083. +
  2084. +//============================================
  2085. +// the power for msdc host controller: global
  2086. +// always keep the VMC on.
  2087. +//============================================
  2088. +#define msdc_vcore_on(host) \
  2089. + do { \
  2090. + INIT_MSG("[+]VMC ref. count<%d>", ++host->pwr_ref); \
  2091. + (void)hwPowerOn(MT65XX_POWER_LDO_VMC, VOL_3300, "SD"); \
  2092. + } while (0)
  2093. +#define msdc_vcore_off(host) \
  2094. + do { \
  2095. + INIT_MSG("[-]VMC ref. count<%d>", --host->pwr_ref); \
  2096. + (void)hwPowerDown(MT65XX_POWER_LDO_VMC, "SD"); \
  2097. + } while (0)
  2098. +
  2099. +//====================================
  2100. +// the vdd output for card: global
  2101. +// always keep the VMCH on.
  2102. +//====================================
  2103. +#define msdc_vdd_on(host) \
  2104. + do { \
  2105. + (void)hwPowerOn(MT65XX_POWER_LDO_VMCH, VOL_3300, "SD"); \
  2106. + } while (0)
  2107. +#define msdc_vdd_off(host) \
  2108. + do { \
  2109. + (void)hwPowerDown(MT65XX_POWER_LDO_VMCH, "SD"); \
  2110. + } while (0)
  2111. +
  2112. +#define sdc_is_busy() (sdr_read32(SDC_STS) & SDC_STS_SDCBUSY)
  2113. +#define sdc_is_cmd_busy() (sdr_read32(SDC_STS) & SDC_STS_CMDBUSY)
  2114. +
  2115. +#define sdc_send_cmd(cmd,arg) \
  2116. + do { \
  2117. + sdr_write32(SDC_ARG, (arg)); \
  2118. + sdr_write32(SDC_CMD, (cmd)); \
  2119. + } while(0)
  2120. +
  2121. +// can modify to read h/w register.
  2122. +//#define is_card_present(h) ((sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1);
  2123. +#define is_card_present(h) (((struct msdc_host*)(h))->card_inserted)
  2124. +
  2125. +/* +++ by chhung */
  2126. +#ifndef __ASSEMBLY__
  2127. +#define PHYSADDR(a) (((unsigned long)(a)) & 0x1fffffff)
  2128. +#else
  2129. +#define PHYSADDR(a) ((a) & 0x1fffffff)
  2130. +#endif
  2131. +/* end of +++ */
  2132. +static unsigned int msdc_do_command(struct msdc_host *host,
  2133. + struct mmc_command *cmd,
  2134. + int tune,
  2135. + unsigned long timeout);
  2136. +
  2137. +static int msdc_tune_cmdrsp(struct msdc_host*host,struct mmc_command *cmd);
  2138. +
  2139. +#ifdef MT6575_SD_DEBUG
  2140. +static void msdc_dump_card_status(struct msdc_host *host, u32 status)
  2141. +{
  2142. + static char *state[] = {
  2143. + "Idle", /* 0 */
  2144. + "Ready", /* 1 */
  2145. + "Ident", /* 2 */
  2146. + "Stby", /* 3 */
  2147. + "Tran", /* 4 */
  2148. + "Data", /* 5 */
  2149. + "Rcv", /* 6 */
  2150. + "Prg", /* 7 */
  2151. + "Dis", /* 8 */
  2152. + "Reserved", /* 9 */
  2153. + "Reserved", /* 10 */
  2154. + "Reserved", /* 11 */
  2155. + "Reserved", /* 12 */
  2156. + "Reserved", /* 13 */
  2157. + "Reserved", /* 14 */
  2158. + "I/O mode", /* 15 */
  2159. + };
  2160. + if (status & R1_OUT_OF_RANGE)
  2161. + N_MSG(RSP, "[CARD_STATUS] Out of Range");
  2162. + if (status & R1_ADDRESS_ERROR)
  2163. + N_MSG(RSP, "[CARD_STATUS] Address Error");
  2164. + if (status & R1_BLOCK_LEN_ERROR)
  2165. + N_MSG(RSP, "[CARD_STATUS] Block Len Error");
  2166. + if (status & R1_ERASE_SEQ_ERROR)
  2167. + N_MSG(RSP, "[CARD_STATUS] Erase Seq Error");
  2168. + if (status & R1_ERASE_PARAM)
  2169. + N_MSG(RSP, "[CARD_STATUS] Erase Param");
  2170. + if (status & R1_WP_VIOLATION)
  2171. + N_MSG(RSP, "[CARD_STATUS] WP Violation");
  2172. + if (status & R1_CARD_IS_LOCKED)
  2173. + N_MSG(RSP, "[CARD_STATUS] Card is Locked");
  2174. + if (status & R1_LOCK_UNLOCK_FAILED)
  2175. + N_MSG(RSP, "[CARD_STATUS] Lock/Unlock Failed");
  2176. + if (status & R1_COM_CRC_ERROR)
  2177. + N_MSG(RSP, "[CARD_STATUS] Command CRC Error");
  2178. + if (status & R1_ILLEGAL_COMMAND)
  2179. + N_MSG(RSP, "[CARD_STATUS] Illegal Command");
  2180. + if (status & R1_CARD_ECC_FAILED)
  2181. + N_MSG(RSP, "[CARD_STATUS] Card ECC Failed");
  2182. + if (status & R1_CC_ERROR)
  2183. + N_MSG(RSP, "[CARD_STATUS] CC Error");
  2184. + if (status & R1_ERROR)
  2185. + N_MSG(RSP, "[CARD_STATUS] Error");
  2186. + if (status & R1_UNDERRUN)
  2187. + N_MSG(RSP, "[CARD_STATUS] Underrun");
  2188. + if (status & R1_OVERRUN)
  2189. + N_MSG(RSP, "[CARD_STATUS] Overrun");
  2190. + if (status & R1_CID_CSD_OVERWRITE)
  2191. + N_MSG(RSP, "[CARD_STATUS] CID/CSD Overwrite");
  2192. + if (status & R1_WP_ERASE_SKIP)
  2193. + N_MSG(RSP, "[CARD_STATUS] WP Eraser Skip");
  2194. + if (status & R1_CARD_ECC_DISABLED)
  2195. + N_MSG(RSP, "[CARD_STATUS] Card ECC Disabled");
  2196. + if (status & R1_ERASE_RESET)
  2197. + N_MSG(RSP, "[CARD_STATUS] Erase Reset");
  2198. + if (status & R1_READY_FOR_DATA)
  2199. + N_MSG(RSP, "[CARD_STATUS] Ready for Data");
  2200. + if (status & R1_SWITCH_ERROR)
  2201. + N_MSG(RSP, "[CARD_STATUS] Switch error");
  2202. + if (status & R1_APP_CMD)
  2203. + N_MSG(RSP, "[CARD_STATUS] App Command");
  2204. +
  2205. + N_MSG(RSP, "[CARD_STATUS] '%s' State", state[R1_CURRENT_STATE(status)]);
  2206. +}
  2207. +
  2208. +static void msdc_dump_ocr_reg(struct msdc_host *host, u32 resp)
  2209. +{
  2210. + if (resp & (1 << 7))
  2211. + N_MSG(RSP, "[OCR] Low Voltage Range");
  2212. + if (resp & (1 << 15))
  2213. + N_MSG(RSP, "[OCR] 2.7-2.8 volt");
  2214. + if (resp & (1 << 16))
  2215. + N_MSG(RSP, "[OCR] 2.8-2.9 volt");
  2216. + if (resp & (1 << 17))
  2217. + N_MSG(RSP, "[OCR] 2.9-3.0 volt");
  2218. + if (resp & (1 << 18))
  2219. + N_MSG(RSP, "[OCR] 3.0-3.1 volt");
  2220. + if (resp & (1 << 19))
  2221. + N_MSG(RSP, "[OCR] 3.1-3.2 volt");
  2222. + if (resp & (1 << 20))
  2223. + N_MSG(RSP, "[OCR] 3.2-3.3 volt");
  2224. + if (resp & (1 << 21))
  2225. + N_MSG(RSP, "[OCR] 3.3-3.4 volt");
  2226. + if (resp & (1 << 22))
  2227. + N_MSG(RSP, "[OCR] 3.4-3.5 volt");
  2228. + if (resp & (1 << 23))
  2229. + N_MSG(RSP, "[OCR] 3.5-3.6 volt");
  2230. + if (resp & (1 << 24))
  2231. + N_MSG(RSP, "[OCR] Switching to 1.8V Accepted (S18A)");
  2232. + if (resp & (1 << 30))
  2233. + N_MSG(RSP, "[OCR] Card Capacity Status (CCS)");
  2234. + if (resp & (1 << 31))
  2235. + N_MSG(RSP, "[OCR] Card Power Up Status (Idle)");
  2236. + else
  2237. + N_MSG(RSP, "[OCR] Card Power Up Status (Busy)");
  2238. +}
  2239. +
  2240. +static void msdc_dump_rca_resp(struct msdc_host *host, u32 resp)
  2241. +{
  2242. + u32 status = (((resp >> 15) & 0x1) << 23) |
  2243. + (((resp >> 14) & 0x1) << 22) |
  2244. + (((resp >> 13) & 0x1) << 19) |
  2245. + (resp & 0x1fff);
  2246. +
  2247. + N_MSG(RSP, "[RCA] 0x%.4x", resp >> 16);
  2248. + msdc_dump_card_status(host, status);
  2249. +}
  2250. +
  2251. +static void msdc_dump_io_resp(struct msdc_host *host, u32 resp)
  2252. +{
  2253. + u32 flags = (resp >> 8) & 0xFF;
  2254. + char *state[] = {"DIS", "CMD", "TRN", "RFU"};
  2255. +
  2256. + if (flags & (1 << 7))
  2257. + N_MSG(RSP, "[IO] COM_CRC_ERR");
  2258. + if (flags & (1 << 6))
  2259. + N_MSG(RSP, "[IO] Illgal command");
  2260. + if (flags & (1 << 3))
  2261. + N_MSG(RSP, "[IO] Error");
  2262. + if (flags & (1 << 2))
  2263. + N_MSG(RSP, "[IO] RFU");
  2264. + if (flags & (1 << 1))
  2265. + N_MSG(RSP, "[IO] Function number error");
  2266. + if (flags & (1 << 0))
  2267. + N_MSG(RSP, "[IO] Out of range");
  2268. +
  2269. + N_MSG(RSP, "[IO] State: %s, Data:0x%x", state[(resp >> 12) & 0x3], resp & 0xFF);
  2270. +}
  2271. +#endif
  2272. +
  2273. +static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
  2274. +{
  2275. + u32 base = host->base;
  2276. + u32 timeout, clk_ns;
  2277. +
  2278. + host->timeout_ns = ns;
  2279. + host->timeout_clks = clks;
  2280. +
  2281. + clk_ns = 1000000000UL / host->sclk;
  2282. + timeout = ns / clk_ns + clks;
  2283. + timeout = timeout >> 16; /* in 65536 sclk cycle unit */
  2284. + timeout = timeout > 1 ? timeout - 1 : 0;
  2285. + timeout = timeout > 255 ? 255 : timeout;
  2286. +
  2287. + sdr_set_field(SDC_CFG, SDC_CFG_DTOC, timeout);
  2288. +
  2289. + N_MSG(OPS, "Set read data timeout: %dns %dclks -> %d x 65536 cycles",
  2290. + ns, clks, timeout + 1);
  2291. +}
  2292. +
  2293. +/* msdc_eirq_sdio() will be called when EIRQ(for WIFI) */
  2294. +static void msdc_eirq_sdio(void *data)
  2295. +{
  2296. + struct msdc_host *host = (struct msdc_host *)data;
  2297. +
  2298. + N_MSG(INT, "SDIO EINT");
  2299. +
  2300. + mmc_signal_sdio_irq(host->mmc);
  2301. +}
  2302. +
  2303. +/* msdc_eirq_cd will not be used! We not using EINT for card detection. */
  2304. +static void msdc_eirq_cd(void *data)
  2305. +{
  2306. + struct msdc_host *host = (struct msdc_host *)data;
  2307. +
  2308. + N_MSG(INT, "CD EINT");
  2309. +
  2310. +#if 0
  2311. + tasklet_hi_schedule(&host->card_tasklet);
  2312. +#else
  2313. + schedule_delayed_work(&host->card_delaywork, HZ);
  2314. +#endif
  2315. +}
  2316. +
  2317. +#if 0
  2318. +static void msdc_tasklet_card(unsigned long arg)
  2319. +{
  2320. + struct msdc_host *host = (struct msdc_host *)arg;
  2321. +#else
  2322. +static void msdc_tasklet_card(struct work_struct *work)
  2323. +{
  2324. + struct msdc_host *host = (struct msdc_host *)container_of(work,
  2325. + struct msdc_host, card_delaywork.work);
  2326. +#endif
  2327. + struct msdc_hw *hw = host->hw;
  2328. + u32 base = host->base;
  2329. + u32 inserted;
  2330. + u32 status = 0;
  2331. + //u32 change = 0;
  2332. +
  2333. + spin_lock(&host->lock);
  2334. +
  2335. + if (hw->get_cd_status) { // NULL
  2336. + inserted = hw->get_cd_status();
  2337. + } else {
  2338. + status = sdr_read32(MSDC_PS);
  2339. + if (cd_active_low)
  2340. + inserted = (status & MSDC_PS_CDSTS) ? 0 : 1;
  2341. + else
  2342. + inserted = (status & MSDC_PS_CDSTS) ? 1 : 0;
  2343. + }
  2344. +
  2345. +#if 0
  2346. + change = host->card_inserted ^ inserted;
  2347. + host->card_inserted = inserted;
  2348. +
  2349. + if (change && !host->suspend) {
  2350. + if (inserted) {
  2351. + host->mmc->f_max = HOST_MAX_MCLK; // work around
  2352. + }
  2353. + mmc_detect_change(host->mmc, msecs_to_jiffies(20));
  2354. + }
  2355. +#else /* Make sure: handle the last interrupt */
  2356. + host->card_inserted = inserted;
  2357. +
  2358. + if (!host->suspend) {
  2359. + host->mmc->f_max = HOST_MAX_MCLK;
  2360. + mmc_detect_change(host->mmc, msecs_to_jiffies(20));
  2361. + }
  2362. +
  2363. + IRQ_MSG("card found<%s>", inserted ? "inserted" : "removed");
  2364. +#endif
  2365. +
  2366. + spin_unlock(&host->lock);
  2367. +}
  2368. +
  2369. +#if 0 /* --- by chhung */
  2370. +/* For E2 only */
  2371. +static u8 clk_src_bit[4] = {
  2372. + 0, 3, 5, 7
  2373. +};
  2374. +
  2375. +static void msdc_select_clksrc(struct msdc_host* host, unsigned char clksrc)
  2376. +{
  2377. + u32 val;
  2378. + u32 base = host->base;
  2379. +
  2380. + BUG_ON(clksrc > 3);
  2381. + INIT_MSG("set clock source to <%d>", clksrc);
  2382. +
  2383. + val = sdr_read32(MSDC_CLKSRC_REG);
  2384. + if (sdr_read32(MSDC_ECO_VER) >= 4) {
  2385. + val &= ~(0x3 << clk_src_bit[host->id]);
  2386. + val |= clksrc << clk_src_bit[host->id];
  2387. + } else {
  2388. + val &= ~0x3; val |= clksrc;
  2389. + }
  2390. + sdr_write32(MSDC_CLKSRC_REG, val);
  2391. +
  2392. + host->hclk = hclks[clksrc];
  2393. + host->hw->clk_src = clksrc;
  2394. +}
  2395. +#endif /* end of --- */
  2396. +
  2397. +static void msdc_set_mclk(struct msdc_host *host, int ddr, unsigned int hz)
  2398. +{
  2399. + //struct msdc_hw *hw = host->hw;
  2400. + u32 base = host->base;
  2401. + u32 mode;
  2402. + u32 flags;
  2403. + u32 div;
  2404. + u32 sclk;
  2405. + u32 hclk = host->hclk;
  2406. + //u8 clksrc = hw->clk_src;
  2407. +
  2408. + if (!hz) { // set mmc system clock to 0 ?
  2409. + //ERR_MSG("set mclk to 0!!!");
  2410. + msdc_reset();
  2411. + return;
  2412. + }
  2413. +
  2414. + msdc_irq_save(flags);
  2415. +
  2416. +#if defined (CONFIG_MT7621_FPGA) || defined (CONFIG_MT7628_FPGA)
  2417. + mode = 0x0; /* use divisor */
  2418. + if (hz >= (hclk >> 1)) {
  2419. + div = 0; /* mean div = 1/2 */
  2420. + sclk = hclk >> 1; /* sclk = clk / 2 */
  2421. + } else {
  2422. + div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  2423. + sclk = (hclk >> 2) / div;
  2424. + }
  2425. +#else
  2426. + if (ddr) {
  2427. + mode = 0x2; /* ddr mode and use divisor */
  2428. + if (hz >= (hclk >> 2)) {
  2429. + div = 1; /* mean div = 1/4 */
  2430. + sclk = hclk >> 2; /* sclk = clk / 4 */
  2431. + } else {
  2432. + div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  2433. + sclk = (hclk >> 2) / div;
  2434. + }
  2435. + } else if (hz >= hclk) { /* bug fix */
  2436. + mode = 0x1; /* no divisor and divisor is ignored */
  2437. + div = 0;
  2438. + sclk = hclk;
  2439. + } else {
  2440. + mode = 0x0; /* use divisor */
  2441. + if (hz >= (hclk >> 1)) {
  2442. + div = 0; /* mean div = 1/2 */
  2443. + sclk = hclk >> 1; /* sclk = clk / 2 */
  2444. + } else {
  2445. + div = (hclk + ((hz << 2) - 1)) / (hz << 2);
  2446. + sclk = (hclk >> 2) / div;
  2447. + }
  2448. + }
  2449. +#endif
  2450. + /* set clock mode and divisor */
  2451. + sdr_set_field(MSDC_CFG, MSDC_CFG_CKMOD, mode);
  2452. + sdr_set_field(MSDC_CFG, MSDC_CFG_CKDIV, div);
  2453. +
  2454. + /* wait clock stable */
  2455. + while (!(sdr_read32(MSDC_CFG) & MSDC_CFG_CKSTB));
  2456. +
  2457. + host->sclk = sclk;
  2458. + host->mclk = hz;
  2459. + msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); // need?
  2460. +
  2461. + INIT_MSG("================");
  2462. + INIT_MSG("!!! Set<%dKHz> Source<%dKHz> -> sclk<%dKHz>", hz/1000, hclk/1000, sclk/1000);
  2463. + INIT_MSG("================");
  2464. +
  2465. + msdc_irq_restore(flags);
  2466. +}
  2467. +
  2468. +/* Fix me. when need to abort */
  2469. +static void msdc_abort_data(struct msdc_host *host)
  2470. +{
  2471. + u32 base = host->base;
  2472. + struct mmc_command *stop = host->mrq->stop;
  2473. +
  2474. + ERR_MSG("Need to Abort. dma<%d>", host->dma_xfer);
  2475. +
  2476. + msdc_reset();
  2477. + msdc_clr_fifo();
  2478. + msdc_clr_int();
  2479. +
  2480. + // need to check FIFO count 0 ?
  2481. +
  2482. + if (stop) { /* try to stop, but may not success */
  2483. + ERR_MSG("stop when abort CMD<%d>", stop->opcode);
  2484. + (void)msdc_do_command(host, stop, 0, CMD_TIMEOUT);
  2485. + }
  2486. +
  2487. + //if (host->mclk >= 25000000) {
  2488. + // msdc_set_mclk(host, 0, host->mclk >> 1);
  2489. + //}
  2490. +}
  2491. +
  2492. +#if 0 /* --- by chhung */
  2493. +static void msdc_pin_config(struct msdc_host *host, int mode)
  2494. +{
  2495. + struct msdc_hw *hw = host->hw;
  2496. + u32 base = host->base;
  2497. + int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
  2498. +
  2499. + /* Config WP pin */
  2500. + if (hw->flags & MSDC_WP_PIN_EN) {
  2501. + if (hw->config_gpio_pin) /* NULL */
  2502. + hw->config_gpio_pin(MSDC_WP_PIN, pull);
  2503. + }
  2504. +
  2505. + switch (mode) {
  2506. + case MSDC_PIN_PULL_UP:
  2507. + //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 1); /* Check & FIXME */
  2508. + //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
  2509. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 1);
  2510. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
  2511. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 1);
  2512. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
  2513. + break;
  2514. + case MSDC_PIN_PULL_DOWN:
  2515. + //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
  2516. + //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 1); /* Check & FIXME */
  2517. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
  2518. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 1);
  2519. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
  2520. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 1);
  2521. + break;
  2522. + case MSDC_PIN_PULL_NONE:
  2523. + default:
  2524. + //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPU, 0); /* Check & FIXME */
  2525. + //sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKPD, 0); /* Check & FIXME */
  2526. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPU, 0);
  2527. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDPD, 0);
  2528. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPU, 0);
  2529. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATPD, 0);
  2530. + break;
  2531. + }
  2532. +
  2533. + N_MSG(CFG, "Pins mode(%d), down(%d), up(%d)",
  2534. + mode, MSDC_PIN_PULL_DOWN, MSDC_PIN_PULL_UP);
  2535. +}
  2536. +
  2537. +void msdc_pin_reset(struct msdc_host *host, int mode)
  2538. +{
  2539. + struct msdc_hw *hw = (struct msdc_hw *)host->hw;
  2540. + u32 base = host->base;
  2541. + int pull = (mode == MSDC_PIN_PULL_UP) ? GPIO_PULL_UP : GPIO_PULL_DOWN;
  2542. +
  2543. + /* Config reset pin */
  2544. + if (hw->flags & MSDC_RST_PIN_EN) {
  2545. + if (hw->config_gpio_pin) /* NULL */
  2546. + hw->config_gpio_pin(MSDC_RST_PIN, pull);
  2547. +
  2548. + if (mode == MSDC_PIN_PULL_UP) {
  2549. + sdr_clr_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2550. + } else {
  2551. + sdr_set_bits(EMMC_IOCON, EMMC_IOCON_BOOTRST);
  2552. + }
  2553. + }
  2554. +}
  2555. +
  2556. +static void msdc_core_power(struct msdc_host *host, int on)
  2557. +{
  2558. + N_MSG(CFG, "Turn %s %s power (copower: %d -> %d)",
  2559. + on ? "on" : "off", "core", host->core_power, on);
  2560. +
  2561. + if (on && host->core_power == 0) {
  2562. + msdc_vcore_on(host);
  2563. + host->core_power = 1;
  2564. + msleep(1);
  2565. + } else if (!on && host->core_power == 1) {
  2566. + msdc_vcore_off(host);
  2567. + host->core_power = 0;
  2568. + msleep(1);
  2569. + }
  2570. +}
  2571. +
  2572. +static void msdc_host_power(struct msdc_host *host, int on)
  2573. +{
  2574. + N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "host");
  2575. +
  2576. + if (on) {
  2577. + //msdc_core_power(host, 1); // need do card detection.
  2578. + msdc_pin_reset(host, MSDC_PIN_PULL_UP);
  2579. + } else {
  2580. + msdc_pin_reset(host, MSDC_PIN_PULL_DOWN);
  2581. + //msdc_core_power(host, 0);
  2582. + }
  2583. +}
  2584. +
  2585. +static void msdc_card_power(struct msdc_host *host, int on)
  2586. +{
  2587. + N_MSG(CFG, "Turn %s %s power ", on ? "on" : "off", "card");
  2588. +
  2589. + if (on) {
  2590. + msdc_pin_config(host, MSDC_PIN_PULL_UP);
  2591. + if (host->hw->ext_power_on) {
  2592. + host->hw->ext_power_on();
  2593. + } else {
  2594. + //msdc_vdd_on(host); // need todo card detection.
  2595. + }
  2596. + msleep(1);
  2597. + } else {
  2598. + if (host->hw->ext_power_off) {
  2599. + host->hw->ext_power_off();
  2600. + } else {
  2601. + //msdc_vdd_off(host);
  2602. + }
  2603. + msdc_pin_config(host, MSDC_PIN_PULL_DOWN);
  2604. + msleep(1);
  2605. + }
  2606. +}
  2607. +
  2608. +static void msdc_set_power_mode(struct msdc_host *host, u8 mode)
  2609. +{
  2610. + N_MSG(CFG, "Set power mode(%d)", mode);
  2611. +
  2612. + if (host->power_mode == MMC_POWER_OFF && mode != MMC_POWER_OFF) {
  2613. + msdc_host_power(host, 1);
  2614. + msdc_card_power(host, 1);
  2615. + } else if (host->power_mode != MMC_POWER_OFF && mode == MMC_POWER_OFF) {
  2616. + msdc_card_power(host, 0);
  2617. + msdc_host_power(host, 0);
  2618. + }
  2619. + host->power_mode = mode;
  2620. +}
  2621. +#endif /* end of --- */
  2622. +
  2623. +#ifdef CONFIG_PM
  2624. +/*
  2625. + register as callback function of WIFI(combo_sdio_register_pm) .
  2626. + can called by msdc_drv_suspend/resume too.
  2627. +*/
  2628. +static void msdc_pm(pm_message_t state, void *data)
  2629. +{
  2630. + struct msdc_host *host = (struct msdc_host *)data;
  2631. + int evt = state.event;
  2632. +
  2633. + if (evt == PM_EVENT_USER_RESUME || evt == PM_EVENT_USER_SUSPEND) {
  2634. + INIT_MSG("USR_%s: suspend<%d> power<%d>",
  2635. + evt == PM_EVENT_USER_RESUME ? "EVENT_USER_RESUME" : "EVENT_USER_SUSPEND",
  2636. + host->suspend, host->power_mode);
  2637. + }
  2638. +
  2639. + if (evt == PM_EVENT_SUSPEND || evt == PM_EVENT_USER_SUSPEND) {
  2640. + if (host->suspend) /* already suspend */ /* default 0*/
  2641. + return;
  2642. +
  2643. + /* for memory card. already power off by mmc */
  2644. + if (evt == PM_EVENT_SUSPEND && host->power_mode == MMC_POWER_OFF)
  2645. + return;
  2646. +
  2647. + host->suspend = 1;
  2648. + host->pm_state = state; /* default PMSG_RESUME */
  2649. +
  2650. + INIT_MSG("%s Suspend", evt == PM_EVENT_SUSPEND ? "PM" : "USR");
  2651. + if(host->hw->flags & MSDC_SYS_SUSPEND) /* set for card */
  2652. + (void)mmc_suspend_host(host->mmc);
  2653. + else {
  2654. + // host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY; /* just for double confirm */ /* --- by chhung */
  2655. + mmc_remove_host(host->mmc);
  2656. + }
  2657. + } else if (evt == PM_EVENT_RESUME || evt == PM_EVENT_USER_RESUME) {
  2658. + if (!host->suspend){
  2659. + //ERR_MSG("warning: already resume");
  2660. + return;
  2661. + }
  2662. +
  2663. + /* No PM resume when USR suspend */
  2664. + if (evt == PM_EVENT_RESUME && host->pm_state.event == PM_EVENT_USER_SUSPEND) {
  2665. + ERR_MSG("PM Resume when in USR Suspend"); /* won't happen. */
  2666. + return;
  2667. + }
  2668. +
  2669. + host->suspend = 0;
  2670. + host->pm_state = state;
  2671. +
  2672. + INIT_MSG("%s Resume", evt == PM_EVENT_RESUME ? "PM" : "USR");
  2673. + if(host->hw->flags & MSDC_SYS_SUSPEND) { /* will not set for WIFI */
  2674. + (void)mmc_resume_host(host->mmc);
  2675. + }
  2676. + else {
  2677. + // host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY; /* --- by chhung */
  2678. + mmc_add_host(host->mmc);
  2679. + }
  2680. + }
  2681. +}
  2682. +#endif
  2683. +
  2684. +/*--------------------------------------------------------------------------*/
  2685. +/* mmc_host_ops members */
  2686. +/*--------------------------------------------------------------------------*/
  2687. +static unsigned int msdc_command_start(struct msdc_host *host,
  2688. + struct mmc_command *cmd,
  2689. + int tune, /* not used */
  2690. + unsigned long timeout)
  2691. +{
  2692. + u32 base = host->base;
  2693. + u32 opcode = cmd->opcode;
  2694. + u32 rawcmd;
  2695. + u32 wints = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO |
  2696. + MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO |
  2697. + MSDC_INT_ACMD19_DONE;
  2698. +
  2699. + u32 resp;
  2700. + unsigned long tmo;
  2701. +
  2702. + /* Protocol layer does not provide response type, but our hardware needs
  2703. + * to know exact type, not just size!
  2704. + */
  2705. + if (opcode == MMC_SEND_OP_COND || opcode == SD_APP_OP_COND)
  2706. + resp = RESP_R3;
  2707. + else if (opcode == MMC_SET_RELATIVE_ADDR || opcode == SD_SEND_RELATIVE_ADDR)
  2708. + resp = (mmc_cmd_type(cmd) == MMC_CMD_BCR) ? RESP_R6 : RESP_R1;
  2709. + else if (opcode == MMC_FAST_IO)
  2710. + resp = RESP_R4;
  2711. + else if (opcode == MMC_GO_IRQ_STATE)
  2712. + resp = RESP_R5;
  2713. + else if (opcode == MMC_SELECT_CARD)
  2714. + resp = (cmd->arg != 0) ? RESP_R1B : RESP_NONE;
  2715. + else if (opcode == SD_IO_RW_DIRECT || opcode == SD_IO_RW_EXTENDED)
  2716. + resp = RESP_R1; /* SDIO workaround. */
  2717. + else if (opcode == SD_SEND_IF_COND && (mmc_cmd_type(cmd) == MMC_CMD_BCR))
  2718. + resp = RESP_R1;
  2719. + else {
  2720. + switch (mmc_resp_type(cmd)) {
  2721. + case MMC_RSP_R1:
  2722. + resp = RESP_R1;
  2723. + break;
  2724. + case MMC_RSP_R1B:
  2725. + resp = RESP_R1B;
  2726. + break;
  2727. + case MMC_RSP_R2:
  2728. + resp = RESP_R2;
  2729. + break;
  2730. + case MMC_RSP_R3:
  2731. + resp = RESP_R3;
  2732. + break;
  2733. + case MMC_RSP_NONE:
  2734. + default:
  2735. + resp = RESP_NONE;
  2736. + break;
  2737. + }
  2738. + }
  2739. +
  2740. + cmd->error = 0;
  2741. + /* rawcmd :
  2742. + * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
  2743. + * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
  2744. + */
  2745. + rawcmd = opcode | msdc_rsp[resp] << 7 | host->blksz << 16;
  2746. +
  2747. + if (opcode == MMC_READ_MULTIPLE_BLOCK) {
  2748. + rawcmd |= (2 << 11);
  2749. + } else if (opcode == MMC_READ_SINGLE_BLOCK) {
  2750. + rawcmd |= (1 << 11);
  2751. + } else if (opcode == MMC_WRITE_MULTIPLE_BLOCK) {
  2752. + rawcmd |= ((2 << 11) | (1 << 13));
  2753. + } else if (opcode == MMC_WRITE_BLOCK) {
  2754. + rawcmd |= ((1 << 11) | (1 << 13));
  2755. + } else if (opcode == SD_IO_RW_EXTENDED) {
  2756. + if (cmd->data->flags & MMC_DATA_WRITE)
  2757. + rawcmd |= (1 << 13);
  2758. + if (cmd->data->blocks > 1)
  2759. + rawcmd |= (2 << 11);
  2760. + else
  2761. + rawcmd |= (1 << 11);
  2762. + } else if (opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int)-1) {
  2763. + rawcmd |= (1 << 14);
  2764. + } else if ((opcode == SD_APP_SEND_SCR) ||
  2765. + (opcode == SD_APP_SEND_NUM_WR_BLKS) ||
  2766. + (opcode == SD_SWITCH && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
  2767. + (opcode == SD_APP_SD_STATUS && (mmc_cmd_type(cmd) == MMC_CMD_ADTC)) ||
  2768. + (opcode == MMC_SEND_EXT_CSD && (mmc_cmd_type(cmd) == MMC_CMD_ADTC))) {
  2769. + rawcmd |= (1 << 11);
  2770. + } else if (opcode == MMC_STOP_TRANSMISSION) {
  2771. + rawcmd |= (1 << 14);
  2772. + rawcmd &= ~(0x0FFF << 16);
  2773. + }
  2774. +
  2775. + N_MSG(CMD, "CMD<%d><0x%.8x> Arg<0x%.8x>", opcode , rawcmd, cmd->arg);
  2776. +
  2777. + tmo = jiffies + timeout;
  2778. +
  2779. + if (opcode == MMC_SEND_STATUS) {
  2780. + for (;;) {
  2781. + if (!sdc_is_cmd_busy())
  2782. + break;
  2783. +
  2784. + if (time_after(jiffies, tmo)) {
  2785. + ERR_MSG("XXX cmd_busy timeout: before CMD<%d>", opcode);
  2786. + cmd->error = (unsigned int)-ETIMEDOUT;
  2787. + msdc_reset();
  2788. + goto end;
  2789. + }
  2790. + }
  2791. + }else {
  2792. + for (;;) {
  2793. + if (!sdc_is_busy())
  2794. + break;
  2795. + if (time_after(jiffies, tmo)) {
  2796. + ERR_MSG("XXX sdc_busy timeout: before CMD<%d>", opcode);
  2797. + cmd->error = (unsigned int)-ETIMEDOUT;
  2798. + msdc_reset();
  2799. + goto end;
  2800. + }
  2801. + }
  2802. + }
  2803. +
  2804. + //BUG_ON(in_interrupt());
  2805. + host->cmd = cmd;
  2806. + host->cmd_rsp = resp;
  2807. +
  2808. + init_completion(&host->cmd_done);
  2809. +
  2810. + sdr_set_bits(MSDC_INTEN, wints);
  2811. + sdc_send_cmd(rawcmd, cmd->arg);
  2812. +
  2813. +end:
  2814. + return cmd->error;
  2815. +}
  2816. +
  2817. +static unsigned int msdc_command_resp(struct msdc_host *host,
  2818. + struct mmc_command *cmd,
  2819. + int tune,
  2820. + unsigned long timeout)
  2821. +{
  2822. + u32 base = host->base;
  2823. + u32 opcode = cmd->opcode;
  2824. + //u32 rawcmd;
  2825. + u32 resp;
  2826. + u32 wints = MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO |
  2827. + MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO |
  2828. + MSDC_INT_ACMD19_DONE;
  2829. +
  2830. + resp = host->cmd_rsp;
  2831. +
  2832. + BUG_ON(in_interrupt());
  2833. + //init_completion(&host->cmd_done);
  2834. + //sdr_set_bits(MSDC_INTEN, wints);
  2835. +
  2836. + spin_unlock(&host->lock);
  2837. + if(!wait_for_completion_timeout(&host->cmd_done, 10*timeout)){
  2838. + ERR_MSG("XXX CMD<%d> wait_for_completion timeout ARG<0x%.8x>", opcode, cmd->arg);
  2839. + cmd->error = (unsigned int)-ETIMEDOUT;
  2840. + msdc_reset();
  2841. + }
  2842. + spin_lock(&host->lock);
  2843. +
  2844. + sdr_clr_bits(MSDC_INTEN, wints);
  2845. + host->cmd = NULL;
  2846. +
  2847. +//end:
  2848. +#ifdef MT6575_SD_DEBUG
  2849. + switch (resp) {
  2850. + case RESP_NONE:
  2851. + N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)", opcode, cmd->error, resp);
  2852. + break;
  2853. + case RESP_R2:
  2854. + N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= %.8x %.8x %.8x %.8x",
  2855. + opcode, cmd->error, resp, cmd->resp[0], cmd->resp[1],
  2856. + cmd->resp[2], cmd->resp[3]);
  2857. + break;
  2858. + default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  2859. + N_MSG(RSP, "CMD_RSP(%d): %d RSP(%d)= 0x%.8x",
  2860. + opcode, cmd->error, resp, cmd->resp[0]);
  2861. + if (cmd->error == 0) {
  2862. + switch (resp) {
  2863. + case RESP_R1:
  2864. + case RESP_R1B:
  2865. + msdc_dump_card_status(host, cmd->resp[0]);
  2866. + break;
  2867. + case RESP_R3:
  2868. + msdc_dump_ocr_reg(host, cmd->resp[0]);
  2869. + break;
  2870. + case RESP_R5:
  2871. + msdc_dump_io_resp(host, cmd->resp[0]);
  2872. + break;
  2873. + case RESP_R6:
  2874. + msdc_dump_rca_resp(host, cmd->resp[0]);
  2875. + break;
  2876. + }
  2877. + }
  2878. + break;
  2879. + }
  2880. +#endif
  2881. +
  2882. + /* do we need to save card's RCA when SD_SEND_RELATIVE_ADDR */
  2883. +
  2884. + if (!tune) {
  2885. + return cmd->error;
  2886. + }
  2887. +
  2888. + /* memory card CRC */
  2889. + if(host->hw->flags & MSDC_REMOVABLE && cmd->error == (unsigned int)(-EIO) ) {
  2890. + if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */
  2891. + msdc_abort_data(host);
  2892. + } else {
  2893. + /* do basic: reset*/
  2894. + msdc_reset();
  2895. + msdc_clr_fifo();
  2896. + msdc_clr_int();
  2897. + }
  2898. + cmd->error = msdc_tune_cmdrsp(host,cmd);
  2899. + }
  2900. +
  2901. + // check DAT0
  2902. + /* if (resp == RESP_R1B) {
  2903. + while ((sdr_read32(MSDC_PS) & 0x10000) != 0x10000);
  2904. + } */
  2905. + /* CMD12 Error Handle */
  2906. +
  2907. + return cmd->error;
  2908. +}
  2909. +
  2910. +static unsigned int msdc_do_command(struct msdc_host *host,
  2911. + struct mmc_command *cmd,
  2912. + int tune,
  2913. + unsigned long timeout)
  2914. +{
  2915. + if (msdc_command_start(host, cmd, tune, timeout))
  2916. + goto end;
  2917. +
  2918. + if (msdc_command_resp(host, cmd, tune, timeout))
  2919. + goto end;
  2920. +
  2921. +end:
  2922. +
  2923. + N_MSG(CMD, " return<%d> resp<0x%.8x>", cmd->error, cmd->resp[0]);
  2924. + return cmd->error;
  2925. +}
  2926. +
  2927. +/* The abort condition when PIO read/write
  2928. + tmo:
  2929. +*/
  2930. +static int msdc_pio_abort(struct msdc_host *host, struct mmc_data *data, unsigned long tmo)
  2931. +{
  2932. + int ret = 0;
  2933. + u32 base = host->base;
  2934. +
  2935. + if (atomic_read(&host->abort)) {
  2936. + ret = 1;
  2937. + }
  2938. +
  2939. + if (time_after(jiffies, tmo)) {
  2940. + data->error = (unsigned int)-ETIMEDOUT;
  2941. + ERR_MSG("XXX PIO Data Timeout: CMD<%d>", host->mrq->cmd->opcode);
  2942. + ret = 1;
  2943. + }
  2944. +
  2945. + if(ret) {
  2946. + msdc_reset();
  2947. + msdc_clr_fifo();
  2948. + msdc_clr_int();
  2949. + ERR_MSG("msdc pio find abort");
  2950. + }
  2951. + return ret;
  2952. +}
  2953. +
  2954. +/*
  2955. + Need to add a timeout, or WDT timeout, system reboot.
  2956. +*/
  2957. +// pio mode data read/write
  2958. +static int msdc_pio_read(struct msdc_host *host, struct mmc_data *data)
  2959. +{
  2960. + struct scatterlist *sg = data->sg;
  2961. + u32 base = host->base;
  2962. + u32 num = data->sg_len;
  2963. + u32 *ptr;
  2964. + u8 *u8ptr;
  2965. + u32 left = 0;
  2966. + u32 count, size = 0;
  2967. + u32 wints = MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ;
  2968. + unsigned long tmo = jiffies + DAT_TIMEOUT;
  2969. +
  2970. + sdr_set_bits(MSDC_INTEN, wints);
  2971. + while (num) {
  2972. + left = sg_dma_len(sg);
  2973. + ptr = sg_virt(sg);
  2974. + while (left) {
  2975. + if ((left >= MSDC_FIFO_THD) && (msdc_rxfifocnt() >= MSDC_FIFO_THD)) {
  2976. + count = MSDC_FIFO_THD >> 2;
  2977. + do {
  2978. + *ptr++ = msdc_fifo_read32();
  2979. + } while (--count);
  2980. + left -= MSDC_FIFO_THD;
  2981. + } else if ((left < MSDC_FIFO_THD) && msdc_rxfifocnt() >= left) {
  2982. + while (left > 3) {
  2983. + *ptr++ = msdc_fifo_read32();
  2984. + left -= 4;
  2985. + }
  2986. +
  2987. + u8ptr = (u8 *)ptr;
  2988. + while(left) {
  2989. + * u8ptr++ = msdc_fifo_read8();
  2990. + left--;
  2991. + }
  2992. + }
  2993. +
  2994. + if (msdc_pio_abort(host, data, tmo)) {
  2995. + goto end;
  2996. + }
  2997. + }
  2998. + size += sg_dma_len(sg);
  2999. + sg = sg_next(sg); num--;
  3000. + }
  3001. +end:
  3002. + data->bytes_xfered += size;
  3003. + N_MSG(FIO, " PIO Read<%d>bytes", size);
  3004. +
  3005. + sdr_clr_bits(MSDC_INTEN, wints);
  3006. + if(data->error) ERR_MSG("read pio data->error<%d> left<%d> size<%d>", data->error, left, size);
  3007. + return data->error;
  3008. +}
  3009. +
  3010. +/* please make sure won't using PIO when size >= 512
  3011. + which means, memory card block read/write won't using pio
  3012. + then don't need to handle the CMD12 when data error.
  3013. +*/
  3014. +static int msdc_pio_write(struct msdc_host* host, struct mmc_data *data)
  3015. +{
  3016. + u32 base = host->base;
  3017. + struct scatterlist *sg = data->sg;
  3018. + u32 num = data->sg_len;
  3019. + u32 *ptr;
  3020. + u8 *u8ptr;
  3021. + u32 left;
  3022. + u32 count, size = 0;
  3023. + u32 wints = MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ;
  3024. + unsigned long tmo = jiffies + DAT_TIMEOUT;
  3025. +
  3026. + sdr_set_bits(MSDC_INTEN, wints);
  3027. + while (num) {
  3028. + left = sg_dma_len(sg);
  3029. + ptr = sg_virt(sg);
  3030. +
  3031. + while (left) {
  3032. + if (left >= MSDC_FIFO_SZ && msdc_txfifocnt() == 0) {
  3033. + count = MSDC_FIFO_SZ >> 2;
  3034. + do {
  3035. + msdc_fifo_write32(*ptr); ptr++;
  3036. + } while (--count);
  3037. + left -= MSDC_FIFO_SZ;
  3038. + } else if (left < MSDC_FIFO_SZ && msdc_txfifocnt() == 0) {
  3039. + while (left > 3) {
  3040. + msdc_fifo_write32(*ptr); ptr++;
  3041. + left -= 4;
  3042. + }
  3043. +
  3044. + u8ptr = (u8*)ptr;
  3045. + while(left){
  3046. + msdc_fifo_write8(*u8ptr); u8ptr++;
  3047. + left--;
  3048. + }
  3049. + }
  3050. +
  3051. + if (msdc_pio_abort(host, data, tmo)) {
  3052. + goto end;
  3053. + }
  3054. + }
  3055. + size += sg_dma_len(sg);
  3056. + sg = sg_next(sg); num--;
  3057. + }
  3058. +end:
  3059. + data->bytes_xfered += size;
  3060. + N_MSG(FIO, " PIO Write<%d>bytes", size);
  3061. + if(data->error) ERR_MSG("write pio data->error<%d>", data->error);
  3062. +
  3063. + sdr_clr_bits(MSDC_INTEN, wints);
  3064. + return data->error;
  3065. +}
  3066. +
  3067. +#if 0 /* --- by chhung */
  3068. +// DMA resume / start / stop
  3069. +static void msdc_dma_resume(struct msdc_host *host)
  3070. +{
  3071. + u32 base = host->base;
  3072. +
  3073. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_RESUME, 1);
  3074. +
  3075. + N_MSG(DMA, "DMA resume");
  3076. +}
  3077. +#endif /* end of --- */
  3078. +
  3079. +static void msdc_dma_start(struct msdc_host *host)
  3080. +{
  3081. + u32 base = host->base;
  3082. + u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ;
  3083. +
  3084. + sdr_set_bits(MSDC_INTEN, wints);
  3085. + //dsb(); /* --- by chhung */
  3086. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
  3087. +
  3088. + N_MSG(DMA, "DMA start");
  3089. +}
  3090. +
  3091. +static void msdc_dma_stop(struct msdc_host *host)
  3092. +{
  3093. + u32 base = host->base;
  3094. + //u32 retries=500;
  3095. + u32 wints = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | MSDC_INTEN_DATCRCERR ;
  3096. +
  3097. + N_MSG(DMA, "DMA status: 0x%.8x",sdr_read32(MSDC_DMA_CFG));
  3098. + //while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);
  3099. +
  3100. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 1);
  3101. + while (sdr_read32(MSDC_DMA_CFG) & MSDC_DMA_CFG_STS);
  3102. +
  3103. + //dsb(); /* --- by chhung */
  3104. + sdr_clr_bits(MSDC_INTEN, wints); /* Not just xfer_comp */
  3105. +
  3106. + N_MSG(DMA, "DMA stop");
  3107. +}
  3108. +
  3109. +#if 0 /* --- by chhung */
  3110. +/* dump a gpd list */
  3111. +static void msdc_dma_dump(struct msdc_host *host, struct msdc_dma *dma)
  3112. +{
  3113. + gpd_t *gpd = dma->gpd;
  3114. + bd_t *bd = dma->bd;
  3115. + bd_t *ptr;
  3116. + int i = 0;
  3117. + int p_to_v;
  3118. +
  3119. + if (dma->mode != MSDC_MODE_DMA_DESC) {
  3120. + return;
  3121. + }
  3122. +
  3123. + ERR_MSG("try to dump gpd and bd");
  3124. +
  3125. + /* dump gpd */
  3126. + ERR_MSG(".gpd<0x%.8x> gpd_phy<0x%.8x>", (int)gpd, (int)dma->gpd_addr);
  3127. + ERR_MSG("...hwo <%d>", gpd->hwo );
  3128. + ERR_MSG("...bdp <%d>", gpd->bdp );
  3129. + ERR_MSG("...chksum<0x%.8x>", gpd->chksum );
  3130. + //ERR_MSG("...intr <0x%.8x>", gpd->intr );
  3131. + ERR_MSG("...next <0x%.8x>", (int)gpd->next );
  3132. + ERR_MSG("...ptr <0x%.8x>", (int)gpd->ptr );
  3133. + ERR_MSG("...buflen<0x%.8x>", gpd->buflen );
  3134. + //ERR_MSG("...extlen<0x%.8x>", gpd->extlen );
  3135. + //ERR_MSG("...arg <0x%.8x>", gpd->arg );
  3136. + //ERR_MSG("...blknum<0x%.8x>", gpd->blknum );
  3137. + //ERR_MSG("...cmd <0x%.8x>", gpd->cmd );
  3138. +
  3139. + /* dump bd */
  3140. + ERR_MSG(".bd<0x%.8x> bd_phy<0x%.8x> gpd_ptr<0x%.8x>", (int)bd, (int)dma->bd_addr, (int)gpd->ptr);
  3141. + ptr = bd;
  3142. + p_to_v = ((u32)bd - (u32)dma->bd_addr);
  3143. + while (1) {
  3144. + ERR_MSG(".bd[%d]", i); i++;
  3145. + ERR_MSG("...eol <%d>", ptr->eol );
  3146. + ERR_MSG("...chksum<0x%.8x>", ptr->chksum );
  3147. + //ERR_MSG("...blkpad<0x%.8x>", ptr->blkpad );
  3148. + //ERR_MSG("...dwpad <0x%.8x>", ptr->dwpad );
  3149. + ERR_MSG("...next <0x%.8x>", (int)ptr->next );
  3150. + ERR_MSG("...ptr <0x%.8x>", (int)ptr->ptr );
  3151. + ERR_MSG("...buflen<0x%.8x>", (int)ptr->buflen );
  3152. +
  3153. + if (ptr->eol == 1) {
  3154. + break;
  3155. + }
  3156. +
  3157. + /* find the next bd, virtual address of ptr->next */
  3158. + /* don't need to enable when use malloc */
  3159. + //BUG_ON( (ptr->next + p_to_v)!=(ptr+1) );
  3160. + //ERR_MSG(".next bd<0x%.8x><0x%.8x>", (ptr->next + p_to_v), (ptr+1));
  3161. + ptr++;
  3162. + }
  3163. +
  3164. + ERR_MSG("dump gpd and bd finished");
  3165. +}
  3166. +#endif /* end of --- */
  3167. +
  3168. +/* calc checksum */
  3169. +static u8 msdc_dma_calcs(u8 *buf, u32 len)
  3170. +{
  3171. + u32 i, sum = 0;
  3172. + for (i = 0; i < len; i++) {
  3173. + sum += buf[i];
  3174. + }
  3175. + return 0xFF - (u8)sum;
  3176. +}
  3177. +
  3178. +/* gpd bd setup + dma registers */
  3179. +static int msdc_dma_config(struct msdc_host *host, struct msdc_dma *dma)
  3180. +{
  3181. + u32 base = host->base;
  3182. + u32 sglen = dma->sglen;
  3183. + //u32 i, j, num, bdlen, arg, xfersz;
  3184. + u32 j, num, bdlen;
  3185. + u8 blkpad, dwpad, chksum;
  3186. + struct scatterlist *sg = dma->sg;
  3187. + gpd_t *gpd;
  3188. + bd_t *bd;
  3189. +
  3190. + switch (dma->mode) {
  3191. + case MSDC_MODE_DMA_BASIC:
  3192. + BUG_ON(dma->xfersz > 65535);
  3193. + BUG_ON(dma->sglen != 1);
  3194. + sdr_write32(MSDC_DMA_SA, PHYSADDR(sg_dma_address(sg)));
  3195. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_LASTBUF, 1);
  3196. +//#if defined (CONFIG_RALINK_MT7620)
  3197. + if (ralink_soc == MT762X_SOC_MT7620A)
  3198. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_XFERSZ, sg_dma_len(sg));
  3199. +//#elif defined (CONFIG_RALINK_MT7621) || defined (CONFIG_RALINK_MT7628)
  3200. + else
  3201. + sdr_write32((volatile u32*)(RALINK_MSDC_BASE+0xa8), sg_dma_len(sg));
  3202. +//#endif
  3203. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ, dma->burstsz);
  3204. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 0);
  3205. + break;
  3206. + case MSDC_MODE_DMA_DESC:
  3207. + blkpad = (dma->flags & DMA_FLAG_PAD_BLOCK) ? 1 : 0;
  3208. + dwpad = (dma->flags & DMA_FLAG_PAD_DWORD) ? 1 : 0;
  3209. + chksum = (dma->flags & DMA_FLAG_EN_CHKSUM) ? 1 : 0;
  3210. +
  3211. + /* calculate the required number of gpd */
  3212. + num = (sglen + MAX_BD_PER_GPD - 1) / MAX_BD_PER_GPD;
  3213. + BUG_ON(num !=1 );
  3214. +
  3215. + gpd = dma->gpd;
  3216. + bd = dma->bd;
  3217. + bdlen = sglen;
  3218. +
  3219. + /* modify gpd*/
  3220. + //gpd->intr = 0;
  3221. + gpd->hwo = 1; /* hw will clear it */
  3222. + gpd->bdp = 1;
  3223. + gpd->chksum = 0; /* need to clear first. */
  3224. + gpd->chksum = (chksum ? msdc_dma_calcs((u8 *)gpd, 16) : 0);
  3225. +
  3226. + /* modify bd*/
  3227. + for (j = 0; j < bdlen; j++) {
  3228. + msdc_init_bd(&bd[j], blkpad, dwpad, sg_dma_address(sg), sg_dma_len(sg));
  3229. + if(j == bdlen - 1) {
  3230. + bd[j].eol = 1; /* the last bd */
  3231. + } else {
  3232. + bd[j].eol = 0;
  3233. + }
  3234. + bd[j].chksum = 0; /* checksume need to clear first */
  3235. + bd[j].chksum = (chksum ? msdc_dma_calcs((u8 *)(&bd[j]), 16) : 0);
  3236. + sg++;
  3237. + }
  3238. +
  3239. + dma->used_gpd += 2;
  3240. + dma->used_bd += bdlen;
  3241. +
  3242. + sdr_set_field(MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, chksum);
  3243. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_BRUSTSZ, dma->burstsz);
  3244. + sdr_set_field(MSDC_DMA_CTRL, MSDC_DMA_CTRL_MODE, 1);
  3245. +
  3246. + sdr_write32(MSDC_DMA_SA, PHYSADDR((u32)dma->gpd_addr));
  3247. + break;
  3248. +
  3249. + default:
  3250. + break;
  3251. + }
  3252. +
  3253. + N_MSG(DMA, "DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
  3254. + N_MSG(DMA, "DMA_CFG = 0x%x", sdr_read32(MSDC_DMA_CFG));
  3255. + N_MSG(DMA, "DMA_SA = 0x%x", sdr_read32(MSDC_DMA_SA));
  3256. +
  3257. + return 0;
  3258. +}
  3259. +
  3260. +static void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
  3261. + struct scatterlist *sg, unsigned int sglen)
  3262. +{
  3263. + BUG_ON(sglen > MAX_BD_NUM); /* not support currently */
  3264. +
  3265. + dma->sg = sg;
  3266. + dma->flags = DMA_FLAG_EN_CHKSUM;
  3267. + //dma->flags = DMA_FLAG_NONE; /* CHECKME */
  3268. + dma->sglen = sglen;
  3269. + dma->xfersz = host->xfer_size;
  3270. + dma->burstsz = MSDC_BRUST_64B;
  3271. +
  3272. + if (sglen == 1 && sg_dma_len(sg) <= MAX_DMA_CNT)
  3273. + dma->mode = MSDC_MODE_DMA_BASIC;
  3274. + else
  3275. + dma->mode = MSDC_MODE_DMA_DESC;
  3276. +
  3277. + N_MSG(DMA, "DMA mode<%d> sglen<%d> xfersz<%d>", dma->mode, dma->sglen, dma->xfersz);
  3278. +
  3279. + msdc_dma_config(host, dma);
  3280. +
  3281. + /*if (dma->mode == MSDC_MODE_DMA_DESC) {
  3282. + //msdc_dma_dump(host, dma);
  3283. + } */
  3284. +}
  3285. +
  3286. +/* set block number before send command */
  3287. +static void msdc_set_blknum(struct msdc_host *host, u32 blknum)
  3288. +{
  3289. + u32 base = host->base;
  3290. +
  3291. + sdr_write32(SDC_BLK_NUM, blknum);
  3292. +}
  3293. +
  3294. +static int msdc_do_request(struct mmc_host*mmc, struct mmc_request*mrq)
  3295. +{
  3296. + struct msdc_host *host = mmc_priv(mmc);
  3297. + struct mmc_command *cmd;
  3298. + struct mmc_data *data;
  3299. + u32 base = host->base;
  3300. + //u32 intsts = 0;
  3301. + unsigned int left=0;
  3302. + int dma = 0, read = 1, dir = DMA_FROM_DEVICE, send_type=0;
  3303. +
  3304. + #define SND_DAT 0
  3305. + #define SND_CMD 1
  3306. +
  3307. + BUG_ON(mmc == NULL);
  3308. + BUG_ON(mrq == NULL);
  3309. +
  3310. + host->error = 0;
  3311. + atomic_set(&host->abort, 0);
  3312. +
  3313. + cmd = mrq->cmd;
  3314. + data = mrq->cmd->data;
  3315. +
  3316. +#if 0 /* --- by chhung */
  3317. + //if(host->id ==1){
  3318. + N_MSG(OPS, "enable clock!");
  3319. + msdc_ungate_clock(host->id);
  3320. + //}
  3321. +#endif /* end of --- */
  3322. +
  3323. + if (!data) {
  3324. + send_type=SND_CMD;
  3325. + if (msdc_do_command(host, cmd, 1, CMD_TIMEOUT) != 0) {
  3326. + goto done;
  3327. + }
  3328. + } else {
  3329. + BUG_ON(data->blksz > HOST_MAX_BLKSZ);
  3330. + send_type=SND_DAT;
  3331. +
  3332. + data->error = 0;
  3333. + read = data->flags & MMC_DATA_READ ? 1 : 0;
  3334. + host->data = data;
  3335. + host->xfer_size = data->blocks * data->blksz;
  3336. + host->blksz = data->blksz;
  3337. +
  3338. + /* deside the transfer mode */
  3339. + if (drv_mode[host->id] == MODE_PIO) {
  3340. + host->dma_xfer = dma = 0;
  3341. + } else if (drv_mode[host->id] == MODE_DMA) {
  3342. + host->dma_xfer = dma = 1;
  3343. + } else if (drv_mode[host->id] == MODE_SIZE_DEP) {
  3344. + host->dma_xfer = dma = ((host->xfer_size >= dma_size[host->id]) ? 1 : 0);
  3345. + }
  3346. +
  3347. + if (read) {
  3348. + if ((host->timeout_ns != data->timeout_ns) ||
  3349. + (host->timeout_clks != data->timeout_clks)) {
  3350. + msdc_set_timeout(host, data->timeout_ns, data->timeout_clks);
  3351. + }
  3352. + }
  3353. +
  3354. + msdc_set_blknum(host, data->blocks);
  3355. + //msdc_clr_fifo(); /* no need */
  3356. +
  3357. + if (dma) {
  3358. + msdc_dma_on(); /* enable DMA mode first!! */
  3359. + init_completion(&host->xfer_done);
  3360. +
  3361. + /* start the command first*/
  3362. + if (msdc_command_start(host, cmd, 1, CMD_TIMEOUT) != 0)
  3363. + goto done;
  3364. +
  3365. + dir = read ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  3366. + (void)dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
  3367. + msdc_dma_setup(host, &host->dma, data->sg, data->sg_len);
  3368. +
  3369. + /* then wait command done */
  3370. + if (msdc_command_resp(host, cmd, 1, CMD_TIMEOUT) != 0)
  3371. + goto done;
  3372. +
  3373. + /* for read, the data coming too fast, then CRC error
  3374. + start DMA no business with CRC. */
  3375. + //init_completion(&host->xfer_done);
  3376. + msdc_dma_start(host);
  3377. +
  3378. + spin_unlock(&host->lock);
  3379. + if(!wait_for_completion_timeout(&host->xfer_done, DAT_TIMEOUT)){
  3380. + ERR_MSG("XXX CMD<%d> wait xfer_done<%d> timeout!!", cmd->opcode, data->blocks * data->blksz);
  3381. + ERR_MSG(" DMA_SA = 0x%x", sdr_read32(MSDC_DMA_SA));
  3382. + ERR_MSG(" DMA_CA = 0x%x", sdr_read32(MSDC_DMA_CA));
  3383. + ERR_MSG(" DMA_CTRL = 0x%x", sdr_read32(MSDC_DMA_CTRL));
  3384. + ERR_MSG(" DMA_CFG = 0x%x", sdr_read32(MSDC_DMA_CFG));
  3385. + data->error = (unsigned int)-ETIMEDOUT;
  3386. +
  3387. + msdc_reset();
  3388. + msdc_clr_fifo();
  3389. + msdc_clr_int();
  3390. + }
  3391. + spin_lock(&host->lock);
  3392. + msdc_dma_stop(host);
  3393. + } else {
  3394. + /* Firstly: send command */
  3395. + if (msdc_do_command(host, cmd, 1, CMD_TIMEOUT) != 0) {
  3396. + goto done;
  3397. + }
  3398. +
  3399. + /* Secondly: pio data phase */
  3400. + if (read) {
  3401. + if (msdc_pio_read(host, data)){
  3402. + goto done;
  3403. + }
  3404. + } else {
  3405. + if (msdc_pio_write(host, data)) {
  3406. + goto done;
  3407. + }
  3408. + }
  3409. +
  3410. + /* For write case: make sure contents in fifo flushed to device */
  3411. + if (!read) {
  3412. + while (1) {
  3413. + left=msdc_txfifocnt();
  3414. + if (left == 0) {
  3415. + break;
  3416. + }
  3417. + if (msdc_pio_abort(host, data, jiffies + DAT_TIMEOUT)) {
  3418. + break;
  3419. + /* Fix me: what about if data error, when stop ? how to? */
  3420. + }
  3421. + }
  3422. + } else {
  3423. + /* Fix me: read case: need to check CRC error */
  3424. + }
  3425. +
  3426. + /* For write case: SDCBUSY and Xfer_Comp will assert when DAT0 not busy.
  3427. + For read case : SDCBUSY and Xfer_Comp will assert when last byte read out from FIFO.
  3428. + */
  3429. +
  3430. + /* try not to wait xfer_comp interrupt.
  3431. + the next command will check SDC_BUSY.
  3432. + SDC_BUSY means xfer_comp assert
  3433. + */
  3434. +
  3435. + } // PIO mode
  3436. +
  3437. + /* Last: stop transfer */
  3438. + if (data->stop){
  3439. + if (msdc_do_command(host, data->stop, 0, CMD_TIMEOUT) != 0) {
  3440. + goto done;
  3441. + }
  3442. + }
  3443. + }
  3444. +
  3445. +done:
  3446. + if (data != NULL) {
  3447. + host->data = NULL;
  3448. + host->dma_xfer = 0;
  3449. + if (dma != 0) {
  3450. + msdc_dma_off();
  3451. + host->dma.used_bd = 0;
  3452. + host->dma.used_gpd = 0;
  3453. + dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len, dir);
  3454. + }
  3455. + host->blksz = 0;
  3456. +
  3457. +#if 0 // don't stop twice!
  3458. + if(host->hw->flags & MSDC_REMOVABLE && data->error) {
  3459. + msdc_abort_data(host);
  3460. + /* reset in IRQ, stop command has issued. -> No need */
  3461. + }
  3462. +#endif
  3463. +
  3464. + N_MSG(OPS, "CMD<%d> data<%s %s> blksz<%d> block<%d> error<%d>",cmd->opcode, (dma? "dma":"pio"),
  3465. + (read ? "read ":"write") ,data->blksz, data->blocks, data->error);
  3466. + }
  3467. +
  3468. +#if 0 /* --- by chhung */
  3469. +#if 1
  3470. + //if(host->id==1) {
  3471. + if(send_type==SND_CMD) {
  3472. + if(cmd->opcode == MMC_SEND_STATUS) {
  3473. + if((cmd->resp[0] & CARD_READY_FOR_DATA) ||(CARD_CURRENT_STATE(cmd->resp[0]) != 7)){
  3474. + N_MSG(OPS,"disable clock, CMD13 IDLE");
  3475. + msdc_gate_clock(host->id);
  3476. + }
  3477. + } else {
  3478. + N_MSG(OPS,"disable clock, CMD<%d>", cmd->opcode);
  3479. + msdc_gate_clock(host->id);
  3480. + }
  3481. + } else {
  3482. + if(read) {
  3483. + N_MSG(OPS,"disable clock!!! Read CMD<%d>",cmd->opcode);
  3484. + msdc_gate_clock(host->id);
  3485. + }
  3486. + }
  3487. + //}
  3488. +#else
  3489. + msdc_gate_clock(host->id);
  3490. +#endif
  3491. +#endif /* end of --- */
  3492. +
  3493. + if (mrq->cmd->error) host->error = 0x001;
  3494. + if (mrq->data && mrq->data->error) host->error |= 0x010;
  3495. + if (mrq->stop && mrq->stop->error) host->error |= 0x100;
  3496. +
  3497. + //if (host->error) ERR_MSG("host->error<%d>", host->error);
  3498. +
  3499. + return host->error;
  3500. +}
  3501. +
  3502. +static int msdc_app_cmd(struct mmc_host *mmc, struct msdc_host *host)
  3503. +{
  3504. + struct mmc_command cmd;
  3505. + struct mmc_request mrq;
  3506. + u32 err;
  3507. +
  3508. + memset(&cmd, 0, sizeof(struct mmc_command));
  3509. + cmd.opcode = MMC_APP_CMD;
  3510. +#if 0 /* bug: we meet mmc->card is null when ACMD6 */
  3511. + cmd.arg = mmc->card->rca << 16;
  3512. +#else
  3513. + cmd.arg = host->app_cmd_arg;
  3514. +#endif
  3515. + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  3516. +
  3517. + memset(&mrq, 0, sizeof(struct mmc_request));
  3518. + mrq.cmd = &cmd; cmd.mrq = &mrq;
  3519. + cmd.data = NULL;
  3520. +
  3521. + err = msdc_do_command(host, &cmd, 0, CMD_TIMEOUT);
  3522. + return err;
  3523. +}
  3524. +
  3525. +static int msdc_tune_cmdrsp(struct msdc_host*host, struct mmc_command *cmd)
  3526. +{
  3527. + int result = -1;
  3528. + u32 base = host->base;
  3529. + u32 rsmpl, cur_rsmpl, orig_rsmpl;
  3530. + u32 rrdly, cur_rrdly = 0xffffffff, orig_rrdly;
  3531. + u32 skip = 1;
  3532. +
  3533. + /* ==== don't support 3.0 now ====
  3534. + 1: R_SMPL[1]
  3535. + 2: PAD_CMD_RESP_RXDLY[26:22]
  3536. + ==========================*/
  3537. +
  3538. + // save the previous tune result
  3539. + sdr_get_field(MSDC_IOCON, MSDC_IOCON_RSPL, orig_rsmpl);
  3540. + sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, orig_rrdly);
  3541. +
  3542. + rrdly = 0;
  3543. + do {
  3544. + for (rsmpl = 0; rsmpl < 2; rsmpl++) {
  3545. + /* Lv1: R_SMPL[1] */
  3546. + cur_rsmpl = (orig_rsmpl + rsmpl) % 2;
  3547. + if (skip == 1) {
  3548. + skip = 0;
  3549. + continue;
  3550. + }
  3551. + sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, cur_rsmpl);
  3552. +
  3553. + if (host->app_cmd) {
  3554. + result = msdc_app_cmd(host->mmc, host);
  3555. + if (result) {
  3556. + ERR_MSG("TUNE_CMD app_cmd<%d> failed: RESP_RXDLY<%d>,R_SMPL<%d>",
  3557. + host->mrq->cmd->opcode, cur_rrdly, cur_rsmpl);
  3558. + continue;
  3559. + }
  3560. + }
  3561. + result = msdc_do_command(host, cmd, 0, CMD_TIMEOUT); // not tune.
  3562. + ERR_MSG("TUNE_CMD<%d> %s PAD_CMD_RESP_RXDLY[26:22]<%d> R_SMPL[1]<%d>", cmd->opcode,
  3563. + (result == 0) ? "PASS" : "FAIL", cur_rrdly, cur_rsmpl);
  3564. +
  3565. + if (result == 0) {
  3566. + return 0;
  3567. + }
  3568. + if (result != (unsigned int)(-EIO)) {
  3569. + ERR_MSG("TUNE_CMD<%d> Error<%d> not -EIO", cmd->opcode, result);
  3570. + return result;
  3571. + }
  3572. +
  3573. + /* should be EIO */
  3574. + if (sdr_read32(SDC_CMD) & 0x1800) { /* check if has data phase */
  3575. + msdc_abort_data(host);
  3576. + }
  3577. + }
  3578. +
  3579. + /* Lv2: PAD_CMD_RESP_RXDLY[26:22] */
  3580. + cur_rrdly = (orig_rrdly + rrdly + 1) % 32;
  3581. + sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_CMDRRDLY, cur_rrdly);
  3582. + }while (++rrdly < 32);
  3583. +
  3584. + return result;
  3585. +}
  3586. +
  3587. +/* Support SD2.0 Only */
  3588. +static int msdc_tune_bread(struct mmc_host *mmc, struct mmc_request *mrq)
  3589. +{
  3590. + struct msdc_host *host = mmc_priv(mmc);
  3591. + u32 base = host->base;
  3592. + u32 ddr=0;
  3593. + u32 dcrc=0;
  3594. + u32 rxdly, cur_rxdly0, cur_rxdly1;
  3595. + u32 dsmpl, cur_dsmpl, orig_dsmpl;
  3596. + u32 cur_dat0, cur_dat1, cur_dat2, cur_dat3;
  3597. + u32 cur_dat4, cur_dat5, cur_dat6, cur_dat7;
  3598. + u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
  3599. + u32 orig_dat4, orig_dat5, orig_dat6, orig_dat7;
  3600. + int result = -1;
  3601. + u32 skip = 1;
  3602. +
  3603. + sdr_get_field(MSDC_IOCON, MSDC_IOCON_DSPL, orig_dsmpl);
  3604. +
  3605. + /* Tune Method 2. */
  3606. + sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  3607. +
  3608. + rxdly = 0;
  3609. + do {
  3610. + for (dsmpl = 0; dsmpl < 2; dsmpl++) {
  3611. + cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
  3612. + if (skip == 1) {
  3613. + skip = 0;
  3614. + continue;
  3615. + }
  3616. + sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
  3617. +
  3618. + if (host->app_cmd) {
  3619. + result = msdc_app_cmd(host->mmc, host);
  3620. + if (result) {
  3621. + ERR_MSG("TUNE_BREAD app_cmd<%d> failed", host->mrq->cmd->opcode);
  3622. + continue;
  3623. + }
  3624. + }
  3625. + result = msdc_do_request(mmc,mrq);
  3626. +
  3627. + sdr_get_field(SDC_DCRC_STS, SDC_DCRC_STS_POS|SDC_DCRC_STS_NEG, dcrc); /* RO */
  3628. + if (!ddr) dcrc &= ~SDC_DCRC_STS_NEG;
  3629. + ERR_MSG("TUNE_BREAD<%s> dcrc<0x%x> DATRDDLY0/1<0x%x><0x%x> dsmpl<0x%x>",
  3630. + (result == 0 && dcrc == 0) ? "PASS" : "FAIL", dcrc,
  3631. + sdr_read32(MSDC_DAT_RDDLY0), sdr_read32(MSDC_DAT_RDDLY1), cur_dsmpl);
  3632. +
  3633. + /* Fix me: result is 0, but dcrc is still exist */
  3634. + if (result == 0 && dcrc == 0) {
  3635. + goto done;
  3636. + } else {
  3637. + /* there is a case: command timeout, and data phase not processed */
  3638. + if (mrq->data->error != 0 && mrq->data->error != (unsigned int)(-EIO)) {
  3639. + ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>",
  3640. + result, mrq->cmd->error, mrq->data->error);
  3641. + goto done;
  3642. + }
  3643. + }
  3644. + }
  3645. +
  3646. + cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  3647. + cur_rxdly1 = sdr_read32(MSDC_DAT_RDDLY1);
  3648. +
  3649. + /* E1 ECO. YD: Reverse */
  3650. + if (sdr_read32(MSDC_ECO_VER) >= 4) {
  3651. + orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  3652. + orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  3653. + orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  3654. + orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  3655. + orig_dat4 = (cur_rxdly1 >> 24) & 0x1F;
  3656. + orig_dat5 = (cur_rxdly1 >> 16) & 0x1F;
  3657. + orig_dat6 = (cur_rxdly1 >> 8) & 0x1F;
  3658. + orig_dat7 = (cur_rxdly1 >> 0) & 0x1F;
  3659. + } else {
  3660. + orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
  3661. + orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
  3662. + orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
  3663. + orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
  3664. + orig_dat4 = (cur_rxdly1 >> 0) & 0x1F;
  3665. + orig_dat5 = (cur_rxdly1 >> 8) & 0x1F;
  3666. + orig_dat6 = (cur_rxdly1 >> 16) & 0x1F;
  3667. + orig_dat7 = (cur_rxdly1 >> 24) & 0x1F;
  3668. + }
  3669. +
  3670. + if (ddr) {
  3671. + cur_dat0 = (dcrc & (1 << 0) || dcrc & (1 << 8)) ? ((orig_dat0 + 1) % 32) : orig_dat0;
  3672. + cur_dat1 = (dcrc & (1 << 1) || dcrc & (1 << 9)) ? ((orig_dat1 + 1) % 32) : orig_dat1;
  3673. + cur_dat2 = (dcrc & (1 << 2) || dcrc & (1 << 10)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
  3674. + cur_dat3 = (dcrc & (1 << 3) || dcrc & (1 << 11)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
  3675. + } else {
  3676. + cur_dat0 = (dcrc & (1 << 0)) ? ((orig_dat0 + 1) % 32) : orig_dat0;
  3677. + cur_dat1 = (dcrc & (1 << 1)) ? ((orig_dat1 + 1) % 32) : orig_dat1;
  3678. + cur_dat2 = (dcrc & (1 << 2)) ? ((orig_dat2 + 1) % 32) : orig_dat2;
  3679. + cur_dat3 = (dcrc & (1 << 3)) ? ((orig_dat3 + 1) % 32) : orig_dat3;
  3680. + }
  3681. + cur_dat4 = (dcrc & (1 << 4)) ? ((orig_dat4 + 1) % 32) : orig_dat4;
  3682. + cur_dat5 = (dcrc & (1 << 5)) ? ((orig_dat5 + 1) % 32) : orig_dat5;
  3683. + cur_dat6 = (dcrc & (1 << 6)) ? ((orig_dat6 + 1) % 32) : orig_dat6;
  3684. + cur_dat7 = (dcrc & (1 << 7)) ? ((orig_dat7 + 1) % 32) : orig_dat7;
  3685. +
  3686. + cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);
  3687. + cur_rxdly1 = (cur_dat4 << 24) | (cur_dat5 << 16) | (cur_dat6 << 8) | (cur_dat7 << 0);
  3688. +
  3689. + sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  3690. + sdr_write32(MSDC_DAT_RDDLY1, cur_rxdly1);
  3691. +
  3692. + } while (++rxdly < 32);
  3693. +
  3694. +done:
  3695. + return result;
  3696. +}
  3697. +
  3698. +static int msdc_tune_bwrite(struct mmc_host *mmc,struct mmc_request *mrq)
  3699. +{
  3700. + struct msdc_host *host = mmc_priv(mmc);
  3701. + u32 base = host->base;
  3702. +
  3703. + u32 wrrdly, cur_wrrdly = 0xffffffff, orig_wrrdly;
  3704. + u32 dsmpl, cur_dsmpl, orig_dsmpl;
  3705. + u32 rxdly, cur_rxdly0;
  3706. + u32 orig_dat0, orig_dat1, orig_dat2, orig_dat3;
  3707. + u32 cur_dat0, cur_dat1, cur_dat2, cur_dat3;
  3708. + int result = -1;
  3709. + u32 skip = 1;
  3710. +
  3711. + // MSDC_IOCON_DDR50CKD need to check. [Fix me]
  3712. +
  3713. + sdr_get_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, orig_wrrdly);
  3714. + sdr_get_field(MSDC_IOCON, MSDC_IOCON_DSPL, orig_dsmpl );
  3715. +
  3716. + /* Tune Method 2. just DAT0 */
  3717. + sdr_set_field(MSDC_IOCON, MSDC_IOCON_DDLSEL, 1);
  3718. + cur_rxdly0 = sdr_read32(MSDC_DAT_RDDLY0);
  3719. +
  3720. + /* E1 ECO. YD: Reverse */
  3721. + if (sdr_read32(MSDC_ECO_VER) >= 4) {
  3722. + orig_dat0 = (cur_rxdly0 >> 24) & 0x1F;
  3723. + orig_dat1 = (cur_rxdly0 >> 16) & 0x1F;
  3724. + orig_dat2 = (cur_rxdly0 >> 8) & 0x1F;
  3725. + orig_dat3 = (cur_rxdly0 >> 0) & 0x1F;
  3726. + } else {
  3727. + orig_dat0 = (cur_rxdly0 >> 0) & 0x1F;
  3728. + orig_dat1 = (cur_rxdly0 >> 8) & 0x1F;
  3729. + orig_dat2 = (cur_rxdly0 >> 16) & 0x1F;
  3730. + orig_dat3 = (cur_rxdly0 >> 24) & 0x1F;
  3731. + }
  3732. +
  3733. + rxdly = 0;
  3734. + do {
  3735. + wrrdly = 0;
  3736. + do {
  3737. + for (dsmpl = 0; dsmpl < 2; dsmpl++) {
  3738. + cur_dsmpl = (orig_dsmpl + dsmpl) % 2;
  3739. + if (skip == 1) {
  3740. + skip = 0;
  3741. + continue;
  3742. + }
  3743. + sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, cur_dsmpl);
  3744. +
  3745. + if (host->app_cmd) {
  3746. + result = msdc_app_cmd(host->mmc, host);
  3747. + if (result) {
  3748. + ERR_MSG("TUNE_BWRITE app_cmd<%d> failed", host->mrq->cmd->opcode);
  3749. + continue;
  3750. + }
  3751. + }
  3752. + result = msdc_do_request(mmc,mrq);
  3753. +
  3754. + ERR_MSG("TUNE_BWRITE<%s> DSPL<%d> DATWRDLY<%d> MSDC_DAT_RDDLY0<0x%x>",
  3755. + result == 0 ? "PASS" : "FAIL",
  3756. + cur_dsmpl, cur_wrrdly, cur_rxdly0);
  3757. +
  3758. + if (result == 0) {
  3759. + goto done;
  3760. + }
  3761. + else {
  3762. + /* there is a case: command timeout, and data phase not processed */
  3763. + if (mrq->data->error != (unsigned int)(-EIO)) {
  3764. + ERR_MSG("TUNE_READ: result<0x%x> cmd_error<%d> data_error<%d>",
  3765. + result, mrq->cmd->error, mrq->data->error);
  3766. + goto done;
  3767. + }
  3768. + }
  3769. + }
  3770. + cur_wrrdly = (orig_wrrdly + wrrdly + 1) % 32;
  3771. + sdr_set_field(MSDC_PAD_TUNE, MSDC_PAD_TUNE_DATWRDLY, cur_wrrdly);
  3772. + } while (++wrrdly < 32);
  3773. +
  3774. + cur_dat0 = (orig_dat0 + rxdly) % 32; /* only adjust bit-1 for crc */
  3775. + cur_dat1 = orig_dat1;
  3776. + cur_dat2 = orig_dat2;
  3777. + cur_dat3 = orig_dat3;
  3778. +
  3779. + cur_rxdly0 = (cur_dat0 << 24) | (cur_dat1 << 16) | (cur_dat2 << 8) | (cur_dat3 << 0);
  3780. + sdr_write32(MSDC_DAT_RDDLY0, cur_rxdly0);
  3781. + } while (++rxdly < 32);
  3782. +
  3783. +done:
  3784. + return result;
  3785. +}
  3786. +
  3787. +static int msdc_get_card_status(struct mmc_host *mmc, struct msdc_host *host, u32 *status)
  3788. +{
  3789. + struct mmc_command cmd;
  3790. + struct mmc_request mrq;
  3791. + u32 err;
  3792. +
  3793. + memset(&cmd, 0, sizeof(struct mmc_command));
  3794. + cmd.opcode = MMC_SEND_STATUS;
  3795. + if (mmc->card) {
  3796. + cmd.arg = mmc->card->rca << 16;
  3797. + } else {
  3798. + ERR_MSG("cmd13 mmc card is null");
  3799. + cmd.arg = host->app_cmd_arg;
  3800. + }
  3801. + cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
  3802. +
  3803. + memset(&mrq, 0, sizeof(struct mmc_request));
  3804. + mrq.cmd = &cmd; cmd.mrq = &mrq;
  3805. + cmd.data = NULL;
  3806. +
  3807. + err = msdc_do_command(host, &cmd, 1, CMD_TIMEOUT);
  3808. +
  3809. + if (status) {
  3810. + *status = cmd.resp[0];
  3811. + }
  3812. +
  3813. + return err;
  3814. +}
  3815. +
  3816. +static int msdc_check_busy(struct mmc_host *mmc, struct msdc_host *host)
  3817. +{
  3818. + u32 err = 0;
  3819. + u32 status = 0;
  3820. +
  3821. + do {
  3822. + err = msdc_get_card_status(mmc, host, &status);
  3823. + if (err) return err;
  3824. + /* need cmd12? */
  3825. + ERR_MSG("cmd<13> resp<0x%x>", status);
  3826. + } while (R1_CURRENT_STATE(status) == 7);
  3827. +
  3828. + return err;
  3829. +}
  3830. +
  3831. +/* failed when msdc_do_request */
  3832. +static int msdc_tune_request(struct mmc_host *mmc, struct mmc_request *mrq)
  3833. +{
  3834. + struct msdc_host *host = mmc_priv(mmc);
  3835. + struct mmc_command *cmd;
  3836. + struct mmc_data *data;
  3837. + //u32 base = host->base;
  3838. + int ret=0, read;
  3839. +
  3840. + cmd = mrq->cmd;
  3841. + data = mrq->cmd->data;
  3842. +
  3843. + read = data->flags & MMC_DATA_READ ? 1 : 0;
  3844. +
  3845. + if (read) {
  3846. + if (data->error == (unsigned int)(-EIO)) {
  3847. + ret = msdc_tune_bread(mmc,mrq);
  3848. + }
  3849. + } else {
  3850. + ret = msdc_check_busy(mmc, host);
  3851. + if (ret){
  3852. + ERR_MSG("XXX cmd13 wait program done failed");
  3853. + return ret;
  3854. + }
  3855. + /* CRC and TO */
  3856. + /* Fix me: don't care card status? */
  3857. + ret = msdc_tune_bwrite(mmc,mrq);
  3858. + }
  3859. +
  3860. + return ret;
  3861. +}
  3862. +
  3863. +/* ops.request */
  3864. +static void msdc_ops_request(struct mmc_host *mmc,struct mmc_request *mrq)
  3865. +{
  3866. + struct msdc_host *host = mmc_priv(mmc);
  3867. +
  3868. + //=== for sdio profile ===
  3869. +#if 0 /* --- by chhung */
  3870. + u32 old_H32, old_L32, new_H32, new_L32;
  3871. + u32 ticks = 0, opcode = 0, sizes = 0, bRx = 0;
  3872. +#endif /* end of --- */
  3873. +
  3874. + if(host->mrq){
  3875. + ERR_MSG("XXX host->mrq<0x%.8x>", (int)host->mrq);
  3876. + BUG();
  3877. + }
  3878. +
  3879. + if (!is_card_present(host) || host->power_mode == MMC_POWER_OFF) {
  3880. + ERR_MSG("cmd<%d> card<%d> power<%d>", mrq->cmd->opcode, is_card_present(host), host->power_mode);
  3881. + mrq->cmd->error = (unsigned int)-ENOMEDIUM;
  3882. +
  3883. +#if 1
  3884. + mrq->done(mrq); // call done directly.
  3885. +#else
  3886. + mrq->cmd->retries = 0; // please don't retry.
  3887. + mmc_request_done(mmc, mrq);
  3888. +#endif
  3889. +
  3890. + return;
  3891. + }
  3892. +
  3893. + /* start to process */
  3894. + spin_lock(&host->lock);
  3895. +#if 0 /* --- by chhung */
  3896. + if (sdio_pro_enable) { //=== for sdio profile ===
  3897. + if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53) {
  3898. + GPT_GetCounter64(&old_L32, &old_H32);
  3899. + }
  3900. + }
  3901. +#endif /* end of --- */
  3902. +
  3903. + host->mrq = mrq;
  3904. +
  3905. + if (msdc_do_request(mmc,mrq)) {
  3906. + if(host->hw->flags & MSDC_REMOVABLE && ralink_soc == MT762X_SOC_MT7621AT && mrq->data && mrq->data->error) {
  3907. + msdc_tune_request(mmc,mrq);
  3908. + }
  3909. + }
  3910. +
  3911. + /* ==== when request done, check if app_cmd ==== */
  3912. + if (mrq->cmd->opcode == MMC_APP_CMD) {
  3913. + host->app_cmd = 1;
  3914. + host->app_cmd_arg = mrq->cmd->arg; /* save the RCA */
  3915. + } else {
  3916. + host->app_cmd = 0;
  3917. + //host->app_cmd_arg = 0;
  3918. + }
  3919. +
  3920. + host->mrq = NULL;
  3921. +
  3922. +#if 0 /* --- by chhung */
  3923. + //=== for sdio profile ===
  3924. + if (sdio_pro_enable) {
  3925. + if (mrq->cmd->opcode == 52 || mrq->cmd->opcode == 53) {
  3926. + GPT_GetCounter64(&new_L32, &new_H32);
  3927. + ticks = msdc_time_calc(old_L32, old_H32, new_L32, new_H32);
  3928. +
  3929. + opcode = mrq->cmd->opcode;
  3930. + if (mrq->cmd->data) {
  3931. + sizes = mrq->cmd->data->blocks * mrq->cmd->data->blksz;
  3932. + bRx = mrq->cmd->data->flags & MMC_DATA_READ ? 1 : 0 ;
  3933. + } else {
  3934. + bRx = mrq->cmd->arg & 0x80000000 ? 1 : 0;
  3935. + }
  3936. +
  3937. + if (!mrq->cmd->error) {
  3938. + msdc_performance(opcode, sizes, bRx, ticks);
  3939. + }
  3940. + }
  3941. + }
  3942. +#endif /* end of --- */
  3943. + spin_unlock(&host->lock);
  3944. +
  3945. + mmc_request_done(mmc, mrq);
  3946. +
  3947. + return;
  3948. +}
  3949. +
  3950. +/* called by ops.set_ios */
  3951. +static void msdc_set_buswidth(struct msdc_host *host, u32 width)
  3952. +{
  3953. + u32 base = host->base;
  3954. + u32 val = sdr_read32(SDC_CFG);
  3955. +
  3956. + val &= ~SDC_CFG_BUSWIDTH;
  3957. +
  3958. + switch (width) {
  3959. + default:
  3960. + case MMC_BUS_WIDTH_1:
  3961. + width = 1;
  3962. + val |= (MSDC_BUS_1BITS << 16);
  3963. + break;
  3964. + case MMC_BUS_WIDTH_4:
  3965. + val |= (MSDC_BUS_4BITS << 16);
  3966. + break;
  3967. + case MMC_BUS_WIDTH_8:
  3968. + val |= (MSDC_BUS_8BITS << 16);
  3969. + break;
  3970. + }
  3971. +
  3972. + sdr_write32(SDC_CFG, val);
  3973. +
  3974. + N_MSG(CFG, "Bus Width = %d", width);
  3975. +}
  3976. +
  3977. +/* ops.set_ios */
  3978. +static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  3979. +{
  3980. + struct msdc_host *host = mmc_priv(mmc);
  3981. + struct msdc_hw *hw=host->hw;
  3982. + u32 base = host->base;
  3983. + u32 ddr = 0;
  3984. +
  3985. +#ifdef MT6575_SD_DEBUG
  3986. + static char *vdd[] = {
  3987. + "1.50v", "1.55v", "1.60v", "1.65v", "1.70v", "1.80v", "1.90v",
  3988. + "2.00v", "2.10v", "2.20v", "2.30v", "2.40v", "2.50v", "2.60v",
  3989. + "2.70v", "2.80v", "2.90v", "3.00v", "3.10v", "3.20v", "3.30v",
  3990. + "3.40v", "3.50v", "3.60v"
  3991. + };
  3992. + static char *power_mode[] = {
  3993. + "OFF", "UP", "ON"
  3994. + };
  3995. + static char *bus_mode[] = {
  3996. + "UNKNOWN", "OPENDRAIN", "PUSHPULL"
  3997. + };
  3998. + static char *timing[] = {
  3999. + "LEGACY", "MMC_HS", "SD_HS"
  4000. + };
  4001. +
  4002. + printk("SET_IOS: CLK(%dkHz), BUS(%s), BW(%u), PWR(%s), VDD(%s), TIMING(%s)",
  4003. + ios->clock / 1000, bus_mode[ios->bus_mode],
  4004. + (ios->bus_width == MMC_BUS_WIDTH_4) ? 4 : 1,
  4005. + power_mode[ios->power_mode], vdd[ios->vdd], timing[ios->timing]);
  4006. +#endif
  4007. +
  4008. + msdc_set_buswidth(host, ios->bus_width);
  4009. +
  4010. + /* Power control ??? */
  4011. + switch (ios->power_mode) {
  4012. + case MMC_POWER_OFF:
  4013. + case MMC_POWER_UP:
  4014. + // msdc_set_power_mode(host, ios->power_mode); /* --- by chhung */
  4015. + break;
  4016. + case MMC_POWER_ON:
  4017. + host->power_mode = MMC_POWER_ON;
  4018. + break;
  4019. + default:
  4020. + break;
  4021. + }
  4022. +
  4023. + /* Clock control */
  4024. + if (host->mclk != ios->clock) {
  4025. + if(ios->clock > 25000000) {
  4026. + //if (!(host->hw->flags & MSDC_REMOVABLE)) {
  4027. + INIT_MSG("SD data latch edge<%d>", hw->data_edge);
  4028. + sdr_set_field(MSDC_IOCON, MSDC_IOCON_RSPL, hw->cmd_edge);
  4029. + sdr_set_field(MSDC_IOCON, MSDC_IOCON_DSPL, hw->data_edge);
  4030. + //} /* for tuning debug */
  4031. + } else { /* default value */
  4032. + sdr_write32(MSDC_IOCON, 0x00000000);
  4033. + // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
  4034. + sdr_write32(MSDC_DAT_RDDLY0, 0x10101010); // for MT7620 E2 and afterward
  4035. + sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
  4036. + // sdr_write32(MSDC_PAD_TUNE, 0x00000000);
  4037. + sdr_write32(MSDC_PAD_TUNE, 0x84101010); // for MT7620 E2 and afterward
  4038. + }
  4039. + msdc_set_mclk(host, ddr, ios->clock);
  4040. + }
  4041. +}
  4042. +
  4043. +/* ops.get_ro */
  4044. +static int msdc_ops_get_ro(struct mmc_host *mmc)
  4045. +{
  4046. + struct msdc_host *host = mmc_priv(mmc);
  4047. + u32 base = host->base;
  4048. + unsigned long flags;
  4049. + int ro = 0;
  4050. +
  4051. + if (host->hw->flags & MSDC_WP_PIN_EN) { /* set for card */
  4052. + spin_lock_irqsave(&host->lock, flags);
  4053. + ro = (sdr_read32(MSDC_PS) >> 31);
  4054. + spin_unlock_irqrestore(&host->lock, flags);
  4055. + }
  4056. + return ro;
  4057. +}
  4058. +
  4059. +/* ops.get_cd */
  4060. +static int msdc_ops_get_cd(struct mmc_host *mmc)
  4061. +{
  4062. + struct msdc_host *host = mmc_priv(mmc);
  4063. + u32 base = host->base;
  4064. + unsigned long flags;
  4065. + int present = 1;
  4066. +
  4067. + /* for sdio, MSDC_REMOVABLE not set, always return 1 */
  4068. + if (!(host->hw->flags & MSDC_REMOVABLE)) {
  4069. + /* For sdio, read H/W always get<1>, but may timeout some times */
  4070. +#if 1
  4071. + host->card_inserted = 1;
  4072. + return 1;
  4073. +#else
  4074. + host->card_inserted = (host->pm_state.event == PM_EVENT_USER_RESUME) ? 1 : 0;
  4075. + INIT_MSG("sdio ops_get_cd<%d>", host->card_inserted);
  4076. + return host->card_inserted;
  4077. +#endif
  4078. + }
  4079. +
  4080. + /* MSDC_CD_PIN_EN set for card */
  4081. + if (host->hw->flags & MSDC_CD_PIN_EN) {
  4082. + spin_lock_irqsave(&host->lock, flags);
  4083. +#if 0
  4084. + present = host->card_inserted; /* why not read from H/W: Fix me*/
  4085. +#else
  4086. + // CD
  4087. + if (cd_active_low)
  4088. + present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 0 : 1;
  4089. + else
  4090. + present = (sdr_read32(MSDC_PS) & MSDC_PS_CDSTS) ? 1 : 0;
  4091. + host->card_inserted = present;
  4092. +#endif
  4093. + spin_unlock_irqrestore(&host->lock, flags);
  4094. + } else {
  4095. + present = 0; /* TODO? Check DAT3 pins for card detection */
  4096. + }
  4097. +
  4098. + INIT_MSG("ops_get_cd return<%d>", present);
  4099. + return present;
  4100. +}
  4101. +
  4102. +/* ops.enable_sdio_irq */
  4103. +static void msdc_ops_enable_sdio_irq(struct mmc_host *mmc, int enable)
  4104. +{
  4105. + struct msdc_host *host = mmc_priv(mmc);
  4106. + struct msdc_hw *hw = host->hw;
  4107. + u32 base = host->base;
  4108. + u32 tmp;
  4109. +
  4110. + if (hw->flags & MSDC_EXT_SDIO_IRQ) { /* yes for sdio */
  4111. + if (enable) {
  4112. + hw->enable_sdio_eirq(); /* combo_sdio_enable_eirq */
  4113. + } else {
  4114. + hw->disable_sdio_eirq(); /* combo_sdio_disable_eirq */
  4115. + }
  4116. + } else {
  4117. + ERR_MSG("XXX "); /* so never enter here */
  4118. + tmp = sdr_read32(SDC_CFG);
  4119. + /* FIXME. Need to interrupt gap detection */
  4120. + if (enable) {
  4121. + tmp |= (SDC_CFG_SDIOIDE | SDC_CFG_SDIOINTWKUP);
  4122. + } else {
  4123. + tmp &= ~(SDC_CFG_SDIOIDE | SDC_CFG_SDIOINTWKUP);
  4124. + }
  4125. + sdr_write32(SDC_CFG, tmp);
  4126. + }
  4127. +}
  4128. +
  4129. +static struct mmc_host_ops mt_msdc_ops = {
  4130. + .request = msdc_ops_request,
  4131. + .set_ios = msdc_ops_set_ios,
  4132. + .get_ro = msdc_ops_get_ro,
  4133. + .get_cd = msdc_ops_get_cd,
  4134. + .enable_sdio_irq = msdc_ops_enable_sdio_irq,
  4135. +};
  4136. +
  4137. +/*--------------------------------------------------------------------------*/
  4138. +/* interrupt handler */
  4139. +/*--------------------------------------------------------------------------*/
  4140. +static irqreturn_t msdc_irq(int irq, void *dev_id)
  4141. +{
  4142. + struct msdc_host *host = (struct msdc_host *)dev_id;
  4143. + struct mmc_data *data = host->data;
  4144. + struct mmc_command *cmd = host->cmd;
  4145. + u32 base = host->base;
  4146. +
  4147. + u32 cmdsts = MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO | MSDC_INT_CMDRDY |
  4148. + MSDC_INT_ACMDCRCERR | MSDC_INT_ACMDTMO | MSDC_INT_ACMDRDY |
  4149. + MSDC_INT_ACMD19_DONE;
  4150. + u32 datsts = MSDC_INT_DATCRCERR |MSDC_INT_DATTMO;
  4151. +
  4152. + u32 intsts = sdr_read32(MSDC_INT);
  4153. + u32 inten = sdr_read32(MSDC_INTEN); inten &= intsts;
  4154. +
  4155. + sdr_write32(MSDC_INT, intsts); /* clear interrupts */
  4156. + /* MSG will cause fatal error */
  4157. +
  4158. + /* card change interrupt */
  4159. + if (intsts & MSDC_INT_CDSC){
  4160. + if (mtk_sw_poll)
  4161. + return IRQ_HANDLED;
  4162. + IRQ_MSG("MSDC_INT_CDSC irq<0x%.8x>", intsts);
  4163. +#if 0 /* ---/+++ by chhung: fix slot mechanical bounce issue */
  4164. + tasklet_hi_schedule(&host->card_tasklet);
  4165. +#else
  4166. + schedule_delayed_work(&host->card_delaywork, HZ);
  4167. +#endif
  4168. + /* tuning when plug card ? */
  4169. + }
  4170. +
  4171. + /* sdio interrupt */
  4172. + if (intsts & MSDC_INT_SDIOIRQ){
  4173. + IRQ_MSG("XXX MSDC_INT_SDIOIRQ"); /* seems not sdio irq */
  4174. + //mmc_signal_sdio_irq(host->mmc);
  4175. + }
  4176. +
  4177. + /* transfer complete interrupt */
  4178. + if (data != NULL) {
  4179. + if (inten & MSDC_INT_XFER_COMPL) {
  4180. + data->bytes_xfered = host->dma.xfersz;
  4181. + complete(&host->xfer_done);
  4182. + }
  4183. +
  4184. + if (intsts & datsts) {
  4185. + /* do basic reset, or stop command will sdc_busy */
  4186. + msdc_reset();
  4187. + msdc_clr_fifo();
  4188. + msdc_clr_int();
  4189. + atomic_set(&host->abort, 1); /* For PIO mode exit */
  4190. +
  4191. + if (intsts & MSDC_INT_DATTMO){
  4192. + IRQ_MSG("XXX CMD<%d> MSDC_INT_DATTMO", host->mrq->cmd->opcode);
  4193. + data->error = (unsigned int)-ETIMEDOUT;
  4194. + }
  4195. + else if (intsts & MSDC_INT_DATCRCERR){
  4196. + IRQ_MSG("XXX CMD<%d> MSDC_INT_DATCRCERR, SDC_DCRC_STS<0x%x>", host->mrq->cmd->opcode, sdr_read32(SDC_DCRC_STS));
  4197. + data->error = (unsigned int)-EIO;
  4198. + }
  4199. +
  4200. + //if(sdr_read32(MSDC_INTEN) & MSDC_INT_XFER_COMPL) {
  4201. + if (host->dma_xfer) {
  4202. + complete(&host->xfer_done); /* Read CRC come fast, XFER_COMPL not enabled */
  4203. + } /* PIO mode can't do complete, because not init */
  4204. + }
  4205. + }
  4206. +
  4207. + /* command interrupts */
  4208. + if ((cmd != NULL) && (intsts & cmdsts)) {
  4209. + if ((intsts & MSDC_INT_CMDRDY) || (intsts & MSDC_INT_ACMDRDY) ||
  4210. + (intsts & MSDC_INT_ACMD19_DONE)) {
  4211. + u32 *rsp = &cmd->resp[0];
  4212. +
  4213. + switch (host->cmd_rsp) {
  4214. + case RESP_NONE:
  4215. + break;
  4216. + case RESP_R2:
  4217. + *rsp++ = sdr_read32(SDC_RESP3); *rsp++ = sdr_read32(SDC_RESP2);
  4218. + *rsp++ = sdr_read32(SDC_RESP1); *rsp++ = sdr_read32(SDC_RESP0);
  4219. + break;
  4220. + default: /* Response types 1, 3, 4, 5, 6, 7(1b) */
  4221. + if ((intsts & MSDC_INT_ACMDRDY) || (intsts & MSDC_INT_ACMD19_DONE)) {
  4222. + *rsp = sdr_read32(SDC_ACMD_RESP);
  4223. + } else {
  4224. + *rsp = sdr_read32(SDC_RESP0);
  4225. + }
  4226. + break;
  4227. + }
  4228. + } else if ((intsts & MSDC_INT_RSPCRCERR) || (intsts & MSDC_INT_ACMDCRCERR)) {
  4229. + if(intsts & MSDC_INT_ACMDCRCERR){
  4230. + IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDCRCERR",cmd->opcode);
  4231. + }
  4232. + else {
  4233. + IRQ_MSG("XXX CMD<%d> MSDC_INT_RSPCRCERR",cmd->opcode);
  4234. + }
  4235. + cmd->error = (unsigned int)-EIO;
  4236. + } else if ((intsts & MSDC_INT_CMDTMO) || (intsts & MSDC_INT_ACMDTMO)) {
  4237. + if(intsts & MSDC_INT_ACMDTMO){
  4238. + IRQ_MSG("XXX CMD<%d> MSDC_INT_ACMDTMO",cmd->opcode);
  4239. + }
  4240. + else {
  4241. + IRQ_MSG("XXX CMD<%d> MSDC_INT_CMDTMO",cmd->opcode);
  4242. + }
  4243. + cmd->error = (unsigned int)-ETIMEDOUT;
  4244. + msdc_reset();
  4245. + msdc_clr_fifo();
  4246. + msdc_clr_int();
  4247. + }
  4248. + complete(&host->cmd_done);
  4249. + }
  4250. +
  4251. + /* mmc irq interrupts */
  4252. + if (intsts & MSDC_INT_MMCIRQ) {
  4253. + printk(KERN_INFO "msdc[%d] MMCIRQ: SDC_CSTS=0x%.8x\r\n", host->id, sdr_read32(SDC_CSTS));
  4254. + }
  4255. +
  4256. +#ifdef MT6575_SD_DEBUG
  4257. + {
  4258. + msdc_int_reg *int_reg = (msdc_int_reg*)&intsts;
  4259. + N_MSG(INT, "IRQ_EVT(0x%x): MMCIRQ(%d) CDSC(%d), ACRDY(%d), ACTMO(%d), ACCRE(%d) AC19DN(%d)",
  4260. + intsts,
  4261. + int_reg->mmcirq,
  4262. + int_reg->cdsc,
  4263. + int_reg->atocmdrdy,
  4264. + int_reg->atocmdtmo,
  4265. + int_reg->atocmdcrc,
  4266. + int_reg->atocmd19done);
  4267. + N_MSG(INT, "IRQ_EVT(0x%x): SDIO(%d) CMDRDY(%d), CMDTMO(%d), RSPCRC(%d), CSTA(%d)",
  4268. + intsts,
  4269. + int_reg->sdioirq,
  4270. + int_reg->cmdrdy,
  4271. + int_reg->cmdtmo,
  4272. + int_reg->rspcrc,
  4273. + int_reg->csta);
  4274. + N_MSG(INT, "IRQ_EVT(0x%x): XFCMP(%d) DXDONE(%d), DATTMO(%d), DATCRC(%d), DMAEMP(%d)",
  4275. + intsts,
  4276. + int_reg->xfercomp,
  4277. + int_reg->dxferdone,
  4278. + int_reg->dattmo,
  4279. + int_reg->datcrc,
  4280. + int_reg->dmaqempty);
  4281. +
  4282. + }
  4283. +#endif
  4284. +
  4285. + return IRQ_HANDLED;
  4286. +}
  4287. +
  4288. +/*--------------------------------------------------------------------------*/
  4289. +/* platform_driver members */
  4290. +/*--------------------------------------------------------------------------*/
  4291. +/* called by msdc_drv_probe/remove */
  4292. +static void msdc_enable_cd_irq(struct msdc_host *host, int enable)
  4293. +{
  4294. + struct msdc_hw *hw = host->hw;
  4295. + u32 base = host->base;
  4296. +
  4297. + /* for sdio, not set */
  4298. + if ((hw->flags & MSDC_CD_PIN_EN) == 0) {
  4299. + /* Pull down card detection pin since it is not avaiable */
  4300. + /*
  4301. + if (hw->config_gpio_pin)
  4302. + hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
  4303. + */
  4304. + sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  4305. + sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
  4306. + sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
  4307. + return;
  4308. + }
  4309. +
  4310. + N_MSG(CFG, "CD IRQ Eanable(%d)", enable);
  4311. +
  4312. + if (enable) {
  4313. + if (hw->enable_cd_eirq) { /* not set, never enter */
  4314. + hw->enable_cd_eirq();
  4315. + } else {
  4316. + /* card detection circuit relies on the core power so that the core power
  4317. + * shouldn't be turned off. Here adds a reference count to keep
  4318. + * the core power alive.
  4319. + */
  4320. + //msdc_vcore_on(host); //did in msdc_init_hw()
  4321. +
  4322. + if (hw->config_gpio_pin) /* NULL */
  4323. + hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_UP);
  4324. +
  4325. + sdr_set_field(MSDC_PS, MSDC_PS_CDDEBOUNCE, DEFAULT_DEBOUNCE);
  4326. + sdr_set_bits(MSDC_PS, MSDC_PS_CDEN);
  4327. + sdr_set_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
  4328. + sdr_set_bits(SDC_CFG, SDC_CFG_INSWKUP); /* not in document! Fix me */
  4329. + }
  4330. + } else {
  4331. + if (hw->disable_cd_eirq) {
  4332. + hw->disable_cd_eirq();
  4333. + } else {
  4334. + if (hw->config_gpio_pin) /* NULL */
  4335. + hw->config_gpio_pin(MSDC_CD_PIN, GPIO_PULL_DOWN);
  4336. +
  4337. + sdr_clr_bits(SDC_CFG, SDC_CFG_INSWKUP);
  4338. + sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  4339. + sdr_clr_bits(MSDC_INTEN, MSDC_INTEN_CDSC);
  4340. +
  4341. + /* Here decreases a reference count to core power since card
  4342. + * detection circuit is shutdown.
  4343. + */
  4344. + //msdc_vcore_off(host);
  4345. + }
  4346. + }
  4347. +}
  4348. +
  4349. +/* called by msdc_drv_probe */
  4350. +static void msdc_init_hw(struct msdc_host *host)
  4351. +{
  4352. + u32 base = host->base;
  4353. + struct msdc_hw *hw = host->hw;
  4354. +
  4355. +#ifdef MT6575_SD_DEBUG
  4356. + msdc_reg[host->id] = (struct msdc_regs *)host->base;
  4357. +#endif
  4358. +
  4359. + /* Power on */
  4360. +#if 0 /* --- by chhung */
  4361. + msdc_vcore_on(host);
  4362. + msdc_pin_reset(host, MSDC_PIN_PULL_UP);
  4363. + msdc_select_clksrc(host, hw->clk_src);
  4364. + enable_clock(PERI_MSDC0_PDN + host->id, "SD");
  4365. + msdc_vdd_on(host);
  4366. +#endif /* end of --- */
  4367. + /* Configure to MMC/SD mode */
  4368. + sdr_set_field(MSDC_CFG, MSDC_CFG_MODE, MSDC_SDMMC);
  4369. +
  4370. + /* Reset */
  4371. + msdc_reset();
  4372. + msdc_clr_fifo();
  4373. +
  4374. + /* Disable card detection */
  4375. + sdr_clr_bits(MSDC_PS, MSDC_PS_CDEN);
  4376. +
  4377. + /* Disable and clear all interrupts */
  4378. + sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
  4379. + sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
  4380. +
  4381. +#if 1
  4382. + /* reset tuning parameter */
  4383. + sdr_write32(MSDC_PAD_CTL0, 0x00090000);
  4384. + sdr_write32(MSDC_PAD_CTL1, 0x000A0000);
  4385. + sdr_write32(MSDC_PAD_CTL2, 0x000A0000);
  4386. + // sdr_write32(MSDC_PAD_TUNE, 0x00000000);
  4387. + sdr_write32(MSDC_PAD_TUNE, 0x84101010); // for MT7620 E2 and afterward
  4388. + // sdr_write32(MSDC_DAT_RDDLY0, 0x00000000);
  4389. + sdr_write32(MSDC_DAT_RDDLY0, 0x10101010); // for MT7620 E2 and afterward
  4390. + sdr_write32(MSDC_DAT_RDDLY1, 0x00000000);
  4391. + sdr_write32(MSDC_IOCON, 0x00000000);
  4392. +#if 0 // use MT7620 default value: 0x403c004f
  4393. + sdr_write32(MSDC_PATCH_BIT0, 0x003C000F); /* bit0 modified: Rx Data Clock Source: 1 -> 2.0*/
  4394. +#endif
  4395. +
  4396. + if (sdr_read32(MSDC_ECO_VER) >= 4) {
  4397. + if (host->id == 1) {
  4398. + sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_WRDAT_CRCS, 1);
  4399. + sdr_set_field(MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMD_RSP, 1);
  4400. +
  4401. + /* internal clock: latch read data */
  4402. + sdr_set_bits(MSDC_PATCH_BIT0, MSDC_PATCH_BIT_CKGEN_CK);
  4403. + }
  4404. + }
  4405. +#endif
  4406. +
  4407. + /* for safety, should clear SDC_CFG.SDIO_INT_DET_EN & set SDC_CFG.SDIO in
  4408. + pre-loader,uboot,kernel drivers. and SDC_CFG.SDIO_INT_DET_EN will be only
  4409. + set when kernel driver wants to use SDIO bus interrupt */
  4410. + /* Configure to enable SDIO mode. it's must otherwise sdio cmd5 failed */
  4411. + sdr_set_bits(SDC_CFG, SDC_CFG_SDIO);
  4412. +
  4413. + /* disable detect SDIO device interupt function */
  4414. + sdr_clr_bits(SDC_CFG, SDC_CFG_SDIOIDE);
  4415. +
  4416. + /* eneable SMT for glitch filter */
  4417. + sdr_set_bits(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKSMT);
  4418. + sdr_set_bits(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDSMT);
  4419. + sdr_set_bits(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATSMT);
  4420. +
  4421. +#if 1
  4422. + /* set clk, cmd, dat pad driving */
  4423. + sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, hw->clk_drv);
  4424. + sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, hw->clk_drv);
  4425. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, hw->cmd_drv);
  4426. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, hw->cmd_drv);
  4427. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, hw->dat_drv);
  4428. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, hw->dat_drv);
  4429. +#else
  4430. + sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVN, 0);
  4431. + sdr_set_field(MSDC_PAD_CTL0, MSDC_PAD_CTL0_CLKDRVP, 0);
  4432. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVN, 0);
  4433. + sdr_set_field(MSDC_PAD_CTL1, MSDC_PAD_CTL1_CMDDRVP, 0);
  4434. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVN, 0);
  4435. + sdr_set_field(MSDC_PAD_CTL2, MSDC_PAD_CTL2_DATDRVP, 0);
  4436. +#endif
  4437. +
  4438. + /* set sampling edge */
  4439. +
  4440. + /* write crc timeout detection */
  4441. + sdr_set_field(MSDC_PATCH_BIT0, 1 << 30, 1);
  4442. +
  4443. + /* Configure to default data timeout */
  4444. + sdr_set_field(SDC_CFG, SDC_CFG_DTOC, DEFAULT_DTOC);
  4445. +
  4446. + msdc_set_buswidth(host, MMC_BUS_WIDTH_1);
  4447. +
  4448. + N_MSG(FUC, "init hardware done!");
  4449. +}
  4450. +
  4451. +/* called by msdc_drv_remove */
  4452. +static void msdc_deinit_hw(struct msdc_host *host)
  4453. +{
  4454. + u32 base = host->base;
  4455. +
  4456. + /* Disable and clear all interrupts */
  4457. + sdr_clr_bits(MSDC_INTEN, sdr_read32(MSDC_INTEN));
  4458. + sdr_write32(MSDC_INT, sdr_read32(MSDC_INT));
  4459. +
  4460. + /* Disable card detection */
  4461. + msdc_enable_cd_irq(host, 0);
  4462. + // msdc_set_power_mode(host, MMC_POWER_OFF); /* make sure power down */ /* --- by chhung */
  4463. +}
  4464. +
  4465. +/* init gpd and bd list in msdc_drv_probe */
  4466. +static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
  4467. +{
  4468. + gpd_t *gpd = dma->gpd;
  4469. + bd_t *bd = dma->bd;
  4470. + bd_t *ptr, *prev;
  4471. +
  4472. + /* we just support one gpd */
  4473. + int bdlen = MAX_BD_PER_GPD;
  4474. +
  4475. + /* init the 2 gpd */
  4476. + memset(gpd, 0, sizeof(gpd_t) * 2);
  4477. + //gpd->next = (void *)virt_to_phys(gpd + 1); /* pointer to a null gpd, bug! kmalloc <-> virt_to_phys */
  4478. + //gpd->next = (dma->gpd_addr + 1); /* bug */
  4479. + gpd->next = (void *)((u32)dma->gpd_addr + sizeof(gpd_t));
  4480. +
  4481. + //gpd->intr = 0;
  4482. + gpd->bdp = 1; /* hwo, cs, bd pointer */
  4483. + //gpd->ptr = (void*)virt_to_phys(bd);
  4484. + gpd->ptr = (void *)dma->bd_addr; /* physical address */
  4485. +
  4486. + memset(bd, 0, sizeof(bd_t) * bdlen);
  4487. + ptr = bd + bdlen - 1;
  4488. + //ptr->eol = 1; /* 0 or 1 [Fix me]*/
  4489. + //ptr->next = 0;
  4490. +
  4491. + while (ptr != bd) {
  4492. + prev = ptr - 1;
  4493. + prev->next = (void *)(dma->bd_addr + sizeof(bd_t) *(ptr - bd));
  4494. + ptr = prev;
  4495. + }
  4496. +}
  4497. +
  4498. +static int msdc_drv_probe(struct platform_device *pdev)
  4499. +{
  4500. + struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4501. + __iomem void *base;
  4502. + struct mmc_host *mmc;
  4503. + struct resource *mem;
  4504. + struct msdc_host *host;
  4505. + struct msdc_hw *hw;
  4506. + int ret, irq;
  4507. +
  4508. + pdev->dev.platform_data = &msdc0_hw;
  4509. +
  4510. + if (of_property_read_bool(pdev->dev.of_node, "mtk,wp-en"))
  4511. + msdc0_hw.flags |= MSDC_WP_PIN_EN;
  4512. +
  4513. + /* Allocate MMC host for this device */
  4514. + mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
  4515. + if (!mmc) return -ENOMEM;
  4516. +
  4517. + hw = (struct msdc_hw*)pdev->dev.platform_data;
  4518. + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4519. + irq = platform_get_irq(pdev, 0);
  4520. +
  4521. + //BUG_ON((!hw) || (!mem) || (irq < 0)); /* --- by chhung */
  4522. +
  4523. + base = devm_ioremap_resource(&pdev->dev, res);
  4524. + if (IS_ERR(base))
  4525. + return PTR_ERR(base);
  4526. +
  4527. + /* Set host parameters to mmc */
  4528. + mmc->ops = &mt_msdc_ops;
  4529. + mmc->f_min = HOST_MIN_MCLK;
  4530. + mmc->f_max = HOST_MAX_MCLK;
  4531. + mmc->ocr_avail = MSDC_OCR_AVAIL;
  4532. +
  4533. + /* For sd card: MSDC_SYS_SUSPEND | MSDC_WP_PIN_EN | MSDC_CD_PIN_EN | MSDC_REMOVABLE | MSDC_HIGHSPEED,
  4534. + For sdio : MSDC_EXT_SDIO_IRQ | MSDC_HIGHSPEED */
  4535. + if (hw->flags & MSDC_HIGHSPEED) {
  4536. + mmc->caps = MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
  4537. + }
  4538. + if (hw->data_pins == 4) { /* current data_pins are all 4*/
  4539. + mmc->caps |= MMC_CAP_4_BIT_DATA;
  4540. + } else if (hw->data_pins == 8) {
  4541. + mmc->caps |= MMC_CAP_8_BIT_DATA;
  4542. + }
  4543. + if ((hw->flags & MSDC_SDIO_IRQ) || (hw->flags & MSDC_EXT_SDIO_IRQ))
  4544. + mmc->caps |= MMC_CAP_SDIO_IRQ; /* yes for sdio */
  4545. +
  4546. + cd_active_low = !of_property_read_bool(pdev->dev.of_node, "mediatek,cd-high");
  4547. + mtk_sw_poll = of_property_read_bool(pdev->dev.of_node, "mediatek,cd-poll");
  4548. +
  4549. + if (mtk_sw_poll)
  4550. + mmc->caps |= MMC_CAP_NEEDS_POLL;
  4551. +
  4552. + /* MMC core transfer sizes tunable parameters */
  4553. +#if LINUX_VERSION_CODE > KERNEL_VERSION(3,10,0)
  4554. + mmc->max_segs = MAX_HW_SGMTS;
  4555. +#else
  4556. + mmc->max_hw_segs = MAX_HW_SGMTS;
  4557. + mmc->max_phys_segs = MAX_PHY_SGMTS;
  4558. +#endif
  4559. + mmc->max_seg_size = MAX_SGMT_SZ;
  4560. + mmc->max_blk_size = HOST_MAX_BLKSZ;
  4561. + mmc->max_req_size = MAX_REQ_SZ;
  4562. + mmc->max_blk_count = mmc->max_req_size;
  4563. +
  4564. + host = mmc_priv(mmc);
  4565. + host->hw = hw;
  4566. + host->mmc = mmc;
  4567. + host->id = pdev->id;
  4568. + host->error = 0;
  4569. + host->irq = irq;
  4570. + host->base = (unsigned long) base;
  4571. + host->mclk = 0; /* mclk: the request clock of mmc sub-system */
  4572. + host->hclk = hclks[hw->clk_src]; /* hclk: clock of clock source to msdc controller */
  4573. + host->sclk = 0; /* sclk: the really clock after divition */
  4574. + host->pm_state = PMSG_RESUME;
  4575. + host->suspend = 0;
  4576. + host->core_clkon = 0;
  4577. + host->card_clkon = 0;
  4578. + host->core_power = 0;
  4579. + host->power_mode = MMC_POWER_OFF;
  4580. +// host->card_inserted = hw->flags & MSDC_REMOVABLE ? 0 : 1;
  4581. + host->timeout_ns = 0;
  4582. + host->timeout_clks = DEFAULT_DTOC * 65536;
  4583. +
  4584. + host->mrq = NULL;
  4585. + //init_MUTEX(&host->sem); /* we don't need to support multiple threads access */
  4586. +
  4587. + host->dma.used_gpd = 0;
  4588. + host->dma.used_bd = 0;
  4589. +
  4590. + /* using dma_alloc_coherent*/ /* todo: using 1, for all 4 slots */
  4591. + host->dma.gpd = dma_alloc_coherent(NULL, MAX_GPD_NUM * sizeof(gpd_t), &host->dma.gpd_addr, GFP_KERNEL);
  4592. + host->dma.bd = dma_alloc_coherent(NULL, MAX_BD_NUM * sizeof(bd_t), &host->dma.bd_addr, GFP_KERNEL);
  4593. + BUG_ON((!host->dma.gpd) || (!host->dma.bd));
  4594. + msdc_init_gpd_bd(host, &host->dma);
  4595. + /*for emmc*/
  4596. + msdc_6575_host[pdev->id] = host;
  4597. +
  4598. +#if 0
  4599. + tasklet_init(&host->card_tasklet, msdc_tasklet_card, (ulong)host);
  4600. +#else
  4601. + INIT_DELAYED_WORK(&host->card_delaywork, msdc_tasklet_card);
  4602. +#endif
  4603. + spin_lock_init(&host->lock);
  4604. + msdc_init_hw(host);
  4605. +
  4606. + if (ralink_soc == MT762X_SOC_MT7621AT)
  4607. + ret = request_irq((unsigned int)irq, msdc_irq, 0, dev_name(&pdev->dev), host);
  4608. + else
  4609. + ret = request_irq((unsigned int)irq, msdc_irq, IRQF_TRIGGER_LOW, dev_name(&pdev->dev), host);
  4610. +
  4611. + if (ret) goto release;
  4612. + // mt65xx_irq_unmask(irq); /* --- by chhung */
  4613. +
  4614. + if (hw->flags & MSDC_CD_PIN_EN) { /* not set for sdio */
  4615. + if (hw->request_cd_eirq) { /* not set for MT6575 */
  4616. + hw->request_cd_eirq(msdc_eirq_cd, (void*)host); /* msdc_eirq_cd will not be used! */
  4617. + }
  4618. + }
  4619. +
  4620. + if (hw->request_sdio_eirq) /* set to combo_sdio_request_eirq() for WIFI */
  4621. + hw->request_sdio_eirq(msdc_eirq_sdio, (void*)host); /* msdc_eirq_sdio() will be called when EIRQ */
  4622. +
  4623. + if (hw->register_pm) {/* yes for sdio */
  4624. +#ifdef CONFIG_PM
  4625. + hw->register_pm(msdc_pm, (void*)host); /* combo_sdio_register_pm() */
  4626. +#endif
  4627. + if(hw->flags & MSDC_SYS_SUSPEND) { /* will not set for WIFI */
  4628. + ERR_MSG("MSDC_SYS_SUSPEND and register_pm both set");
  4629. + }
  4630. + //mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY; /* pm not controlled by system but by client. */ /* --- by chhung */
  4631. + }
  4632. +
  4633. + platform_set_drvdata(pdev, mmc);
  4634. +
  4635. + ret = mmc_add_host(mmc);
  4636. + if (ret) goto free_irq;
  4637. +
  4638. + /* Config card detection pin and enable interrupts */
  4639. + if (hw->flags & MSDC_CD_PIN_EN) { /* set for card */
  4640. + msdc_enable_cd_irq(host, 1);
  4641. + } else {
  4642. + msdc_enable_cd_irq(host, 0);
  4643. + }
  4644. +
  4645. + return 0;
  4646. +
  4647. +free_irq:
  4648. + free_irq(irq, host);
  4649. +release:
  4650. + platform_set_drvdata(pdev, NULL);
  4651. + msdc_deinit_hw(host);
  4652. +
  4653. +#if 0
  4654. + tasklet_kill(&host->card_tasklet);
  4655. +#else
  4656. + cancel_delayed_work_sync(&host->card_delaywork);
  4657. +#endif
  4658. +
  4659. + if (mem)
  4660. + release_mem_region(mem->start, mem->end - mem->start + 1);
  4661. +
  4662. + mmc_free_host(mmc);
  4663. +
  4664. + return ret;
  4665. +}
  4666. +
  4667. +/* 4 device share one driver, using "drvdata" to show difference */
  4668. +static int msdc_drv_remove(struct platform_device *pdev)
  4669. +{
  4670. + struct mmc_host *mmc;
  4671. + struct msdc_host *host;
  4672. + struct resource *mem;
  4673. +
  4674. + mmc = platform_get_drvdata(pdev);
  4675. + BUG_ON(!mmc);
  4676. +
  4677. + host = mmc_priv(mmc);
  4678. + BUG_ON(!host);
  4679. +
  4680. + ERR_MSG("removed !!!");
  4681. +
  4682. + platform_set_drvdata(pdev, NULL);
  4683. + mmc_remove_host(host->mmc);
  4684. + msdc_deinit_hw(host);
  4685. +
  4686. +#if 0
  4687. + tasklet_kill(&host->card_tasklet);
  4688. +#else
  4689. + cancel_delayed_work_sync(&host->card_delaywork);
  4690. +#endif
  4691. + free_irq(host->irq, host);
  4692. +
  4693. + dma_free_coherent(NULL, MAX_GPD_NUM * sizeof(gpd_t), host->dma.gpd, host->dma.gpd_addr);
  4694. + dma_free_coherent(NULL, MAX_BD_NUM * sizeof(bd_t), host->dma.bd, host->dma.bd_addr);
  4695. +
  4696. + mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  4697. +
  4698. + if (mem)
  4699. + release_mem_region(mem->start, mem->end - mem->start + 1);
  4700. +
  4701. + mmc_free_host(host->mmc);
  4702. +
  4703. + return 0;
  4704. +}
  4705. +
  4706. +/* Fix me: Power Flow */
  4707. +#ifdef CONFIG_PM
  4708. +static int msdc_drv_suspend(struct platform_device *pdev, pm_message_t state)
  4709. +{
  4710. + int ret = 0;
  4711. + struct mmc_host *mmc = platform_get_drvdata(pdev);
  4712. + struct msdc_host *host = mmc_priv(mmc);
  4713. +
  4714. + if (mmc && state.event == PM_EVENT_SUSPEND && (host->hw->flags & MSDC_SYS_SUSPEND)) { /* will set for card */
  4715. + msdc_pm(state, (void*)host);
  4716. + }
  4717. +
  4718. + return ret;
  4719. +}
  4720. +
  4721. +static int msdc_drv_resume(struct platform_device *pdev)
  4722. +{
  4723. + int ret = 0;
  4724. + struct mmc_host *mmc = platform_get_drvdata(pdev);
  4725. + struct msdc_host *host = mmc_priv(mmc);
  4726. + struct pm_message state;
  4727. +
  4728. + state.event = PM_EVENT_RESUME;
  4729. + if (mmc && (host->hw->flags & MSDC_SYS_SUSPEND)) {/* will set for card */
  4730. + msdc_pm(state, (void*)host);
  4731. + }
  4732. +
  4733. + /* This mean WIFI not controller by PM */
  4734. +
  4735. + return ret;
  4736. +}
  4737. +#endif
  4738. +
  4739. +static const struct of_device_id mt7620_sdhci_match[] = {
  4740. + { .compatible = "ralink,mt7620-sdhci" },
  4741. + {},
  4742. +};
  4743. +MODULE_DEVICE_TABLE(of, rt288x_wdt_match);
  4744. +
  4745. +static struct platform_driver mt_msdc_driver = {
  4746. + .probe = msdc_drv_probe,
  4747. + .remove = msdc_drv_remove,
  4748. +#ifdef CONFIG_PM
  4749. + .suspend = msdc_drv_suspend,
  4750. + .resume = msdc_drv_resume,
  4751. +#endif
  4752. + .driver = {
  4753. + .name = DRV_NAME,
  4754. + .owner = THIS_MODULE,
  4755. + .of_match_table = mt7620_sdhci_match,
  4756. + },
  4757. +};
  4758. +
  4759. +/*--------------------------------------------------------------------------*/
  4760. +/* module init/exit */
  4761. +/*--------------------------------------------------------------------------*/
  4762. +static int __init mt_msdc_init(void)
  4763. +{
  4764. + int ret;
  4765. +/* +++ by chhung */
  4766. + u32 reg;
  4767. +
  4768. +#if defined (CONFIG_MTD_ANY_RALINK)
  4769. + extern int ra_check_flash_type(void);
  4770. + if(ra_check_flash_type() == 2) { /* NAND */
  4771. + printk("%s: !!!!! SDXC Module Initialize Fail !!!!!", __func__);
  4772. + return 0;
  4773. + }
  4774. +#endif
  4775. + printk("MTK MSDC device init.\n");
  4776. + mtk_sd_device.dev.platform_data = &msdc0_hw;
  4777. +if (ralink_soc == MT762X_SOC_MT7620A || ralink_soc == MT762X_SOC_MT7621AT) {
  4778. +//#if defined (CONFIG_RALINK_MT7620) || defined (CONFIG_RALINK_MT7621)
  4779. + reg = sdr_read32((volatile u32*)(RALINK_SYSCTL_BASE + 0x60)) & ~(0x3<<18);
  4780. +//#if defined (CONFIG_RALINK_MT7620)
  4781. + if (ralink_soc == MT762X_SOC_MT7620A)
  4782. + reg |= 0x1<<18;
  4783. +//#endif
  4784. +} else {
  4785. +//#elif defined (CONFIG_RALINK_MT7628)
  4786. + /* TODO: maybe omitted when RAether already toggle AGPIO_CFG */
  4787. + reg = sdr_read32((volatile u32*)(RALINK_SYSCTL_BASE + 0x3c));
  4788. + reg |= 0x1e << 16;
  4789. + sdr_write32((volatile u32*)(RALINK_SYSCTL_BASE + 0x3c), reg);
  4790. +
  4791. + reg = sdr_read32((volatile u32*)(RALINK_SYSCTL_BASE + 0x60)) & ~(0x3<<10);
  4792. +#if defined (CONFIG_MTK_MMC_EMMC_8BIT)
  4793. + reg |= 0x3<<26 | 0x3<<28 | 0x3<<30;
  4794. + msdc0_hw.data_pins = 8,
  4795. +#endif
  4796. +//#endif
  4797. +}
  4798. + sdr_write32((volatile u32*)(RALINK_SYSCTL_BASE + 0x60), reg);
  4799. + //platform_device_register(&mtk_sd_device);
  4800. +/* end of +++ */
  4801. +
  4802. + ret = platform_driver_register(&mt_msdc_driver);
  4803. + if (ret) {
  4804. + printk(KERN_ERR DRV_NAME ": Can't register driver");
  4805. + return ret;
  4806. + }
  4807. + printk(KERN_INFO DRV_NAME ": MediaTek MT6575 MSDC Driver\n");
  4808. +
  4809. +#if defined (MT6575_SD_DEBUG)
  4810. + msdc_debug_proc_init();
  4811. +#endif
  4812. + return 0;
  4813. +}
  4814. +
  4815. +static void __exit mt_msdc_exit(void)
  4816. +{
  4817. +// platform_device_unregister(&mtk_sd_device);
  4818. + platform_driver_unregister(&mt_msdc_driver);
  4819. +}
  4820. +
  4821. +module_init(mt_msdc_init);
  4822. +module_exit(mt_msdc_exit);
  4823. +MODULE_LICENSE("GPL");
  4824. +MODULE_DESCRIPTION("MediaTek MT6575 SD/MMC Card Driver");
  4825. +MODULE_AUTHOR("Infinity Chen <infinity.chen@mediatek.com>");
  4826. +
  4827. +EXPORT_SYMBOL(msdc_6575_host);