sdhci-msm.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864
  1. /*
  2. * drivers/mmc/host/sdhci-msm.c - Qualcomm MSM SDHCI Platform
  3. * driver source file
  4. *
  5. * Copyright (c) 2012-2014, The Linux Foundation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 and
  9. * only version 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/mmc/host.h>
  19. #include <linux/mmc/card.h>
  20. #include <linux/mmc/sdio_func.h>
  21. #include <linux/gfp.h>
  22. #include <linux/of.h>
  23. #include <linux/of_gpio.h>
  24. #include <linux/regulator/consumer.h>
  25. #include <linux/types.h>
  26. #include <linux/input.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/wait.h>
  29. #include <linux/io.h>
  30. #include <linux/delay.h>
  31. #include <linux/scatterlist.h>
  32. #include <linux/slab.h>
  33. #include <linux/irq.h>
  34. #include <linux/mmc/mmc.h>
  35. #include <linux/pm.h>
  36. #include <linux/pm_runtime.h>
  37. #include <linux/mmc/cd-gpio.h>
  38. #include <linux/dma-mapping.h>
  39. #if defined(CONFIG_ARCH_MSM8974)\
  40. || defined(CONFIG_MACH_FRESCOLTESKT)||defined(CONFIG_MACH_FRESCOLTEKTT)||defined(CONFIG_MACH_FRESCOLTELGT)
  41. #include <linux/regulator/consumer.h>
  42. #include <mach/rpm-regulator.h>
  43. #include <mach/rpm-regulator-smd.h>
  44. #endif
  45. #include <mach/gpio.h>
  46. #include <mach/msm_bus.h>
  47. #include <mach/mpm.h>
  48. #include <linux/iopoll.h>
  49. #include "sdhci-pltfm.h"
  50. enum sdc_mpm_pin_state {
  51. SDC_DAT1_DISABLE,
  52. SDC_DAT1_ENABLE,
  53. SDC_DAT1_ENWAKE,
  54. SDC_DAT1_DISWAKE,
  55. };
  56. #define SDHCI_VER_100 0x2B
  57. #define CORE_HC_MODE 0x78
  58. #define HC_MODE_EN 0x1
  59. #define FF_CLK_SW_RST_DIS (1 << 13)
  60. #define CORE_POWER 0x0
  61. #define CORE_SW_RST (1 << 7)
  62. #define CORE_PWRCTL_STATUS 0xDC
  63. #define CORE_PWRCTL_MASK 0xE0
  64. #define CORE_PWRCTL_CLEAR 0xE4
  65. #define CORE_PWRCTL_CTL 0xE8
  66. #define CORE_PWRCTL_BUS_OFF 0x01
  67. #define CORE_PWRCTL_BUS_ON (1 << 1)
  68. #define CORE_PWRCTL_IO_LOW (1 << 2)
  69. #define CORE_PWRCTL_IO_HIGH (1 << 3)
  70. #define CORE_PWRCTL_BUS_SUCCESS 0x01
  71. #define CORE_PWRCTL_BUS_FAIL (1 << 1)
  72. #define CORE_PWRCTL_IO_SUCCESS (1 << 2)
  73. #define CORE_PWRCTL_IO_FAIL (1 << 3)
  74. #define INT_MASK 0xF
  75. #define MAX_PHASES 16
  76. #define CORE_DLL_CONFIG 0x100
  77. #define CORE_CMD_DAT_TRACK_SEL (1 << 0)
  78. #define CORE_DLL_LOCK (1 << 7)
  79. #define CORE_DLL_EN (1 << 16)
  80. #define CORE_CDR_EN (1 << 17)
  81. #define CORE_CK_OUT_EN (1 << 18)
  82. #define CORE_CDR_EXT_EN (1 << 19)
  83. #define CORE_DLL_PDN (1 << 29)
  84. #define CORE_DLL_RST (1 << 30)
  85. #define CORE_DLL_CONFIG 0x100
  86. #define CORE_DLL_TEST_CTL 0x104
  87. #define CORE_DLL_STATUS 0x108
  88. #define CORE_VENDOR_SPEC 0x10C
  89. #define CORE_CLK_PWRSAVE (1 << 1)
  90. #define CORE_HC_MCLK_SEL_DFLT (2 << 8)
  91. #define CORE_HC_MCLK_SEL_HS400 (3 << 8)
  92. #define CORE_HC_MCLK_SEL_MASK (3 << 8)
  93. #define CORE_IO_PAD_PWR_SWITCH (1 << 16)
  94. #define CORE_HC_SELECT_IN_EN (1 << 18)
  95. #define CORE_HC_SELECT_IN_HS400 (6 << 19)
  96. #define CORE_HC_SELECT_IN_MASK (7 << 19)
  97. #define CORE_VENDOR_SPEC_ADMA_ERR_ADDR0 0x114
  98. #define CORE_VENDOR_SPEC_ADMA_ERR_ADDR1 0x118
  99. #define CORE_CSR_CDC_CTLR_CFG0 0x130
  100. #define CORE_SW_TRIG_FULL_CALIB (1 << 16)
  101. #define CORE_HW_AUTOCAL_ENA (1 << 17)
  102. #define CORE_CSR_CDC_CTLR_CFG1 0x134
  103. #define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
  104. #define CORE_TIMER_ENA (1 << 16)
  105. #define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
  106. #define CORE_CSR_CDC_REFCOUNT_CFG 0x140
  107. #define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
  108. #define CORE_CDC_OFFSET_CFG 0x14C
  109. #define CORE_CSR_CDC_DELAY_CFG 0x150
  110. #define CORE_CDC_SLAVE_DDA_CFG 0x160
  111. #define CORE_CSR_CDC_STATUS0 0x164
  112. #define CORE_CALIBRATION_DONE (1 << 0)
  113. #define CORE_CDC_ERROR_CODE_MASK 0x7000000
  114. #define CORE_CSR_CDC_GEN_CFG 0x178
  115. #define CORE_CDC_SWITCH_BYPASS_OFF (1 << 0)
  116. #define CORE_CDC_SWITCH_RC_EN (1 << 1)
  117. #define CORE_DDR_200_CFG 0x184
  118. #define CORE_CDC_T4_DLY_SEL (1 << 0)
  119. #define CORE_START_CDC_TRAFFIC (1 << 6)
  120. #define CORE_MCI_DATA_CTRL 0x2C
  121. #define CORE_MCI_DPSM_ENABLE (1 << 0)
  122. #define CORE_MCI_DATA_CNT 0x30
  123. #define CORE_MCI_STATUS 0x34
  124. #define CORE_MCI_FIFO_CNT 0x44
  125. #define CORE_TESTBUS_CONFIG 0x0CC
  126. #define CORE_TESTBUS_ENA (1 << 3)
  127. #define CORE_TESTBUS_SEL2 (1 << 4)
  128. #define CORE_TESTBUS_SEL2_STATE_MACHINE (2 << 4)
  129. #define CORE_SEL_TESTBUS1 (1 << 0)
  130. #define CORE_TESTBUS_MASK 0xF
  131. #define CORE_TESTBUS_SEL2_BIT 4
  132. #define CORE_MCI_VERSION 0x050
  133. #define CORE_VERSION_310 0x10000011
  134. /*
  135. * Waiting until end of potential AHB access for data:
  136. * 16 AHB cycles (160ns for 100MHz and 320ns for 50MHz) +
  137. * delay on AHB (2us) = maximum 2.32us
  138. * Taking x10 times margin
  139. */
  140. #define CORE_AHB_DATA_DELAY_US 23
  141. /* Waiting until end of potential AHB access for descriptor:
  142. * Single (1 AHB cycle) + delay on AHB bus = max 2us
  143. * INCR4 (4 AHB cycles) + delay on AHB bus = max 2us
  144. * Single (1 AHB cycle) + delay on AHB bus = max 2us
  145. * Total 8 us delay with margin
  146. */
  147. #define CORE_AHB_DESC_DELAY_US 8
  148. #define CORE_SDCC_DEBUG_REG 0x124
  149. #define CORE_DEBUG_REG_AHB_HTRANS (3 << 12)
  150. /* 8KB descriptors */
  151. #define SDHCI_MSM_MAX_SEGMENTS (1 << 13)
  152. #define SDHCI_MSM_MMC_CLK_GATE_DELAY 200 /* msecs */
  153. #define CORE_FREQ_100MHZ (100 * 1000 * 1000)
  154. #define INVALID_TUNING_PHASE -1
  155. #define sdhci_is_valid_mpm_wakeup_int(_h) ((_h)->pdata->mpm_sdiowakeup_int >= 0)
  156. #define sdhci_is_valid_gpio_wakeup_int(_h) ((_h)->pdata->sdiowakeup_irq >= 0)
  157. #define NUM_TUNING_PHASES 16
  158. #define MAX_DRV_TYPES_SUPPORTED_HS200 3
  159. static const u32 tuning_block_64[] = {
  160. 0x00FF0FFF, 0xCCC3CCFF, 0xFFCC3CC3, 0xEFFEFFFE,
  161. 0xDDFFDFFF, 0xFBFFFBFF, 0xFF7FFFBF, 0xEFBDF777,
  162. 0xF0FFF0FF, 0x3CCCFC0F, 0xCFCC33CC, 0xEEFFEFFF,
  163. 0xFDFFFDFF, 0xFFBFFFDF, 0xFFF7FFBB, 0xDE7B7FF7
  164. };
  165. static const u32 tuning_block_128[] = {
  166. 0xFF00FFFF, 0x0000FFFF, 0xCCCCFFFF, 0xCCCC33CC,
  167. 0xCC3333CC, 0xFFFFCCCC, 0xFFFFEEFF, 0xFFEEEEFF,
  168. 0xFFDDFFFF, 0xDDDDFFFF, 0xBBFFFFFF, 0xBBFFFFFF,
  169. 0xFFFFFFBB, 0xFFFFFF77, 0x77FF7777, 0xFFEEDDBB,
  170. 0x00FFFFFF, 0x00FFFFFF, 0xCCFFFF00, 0xCC33CCCC,
  171. 0x3333CCCC, 0xFFCCCCCC, 0xFFEEFFFF, 0xEEEEFFFF,
  172. 0xDDFFFFFF, 0xDDFFFFFF, 0xFFFFFFDD, 0xFFFFFFBB,
  173. 0xFFFFBBBB, 0xFFFF77FF, 0xFF7777FF, 0xEEDDBB77
  174. };
  175. extern unsigned int system_rev;
  176. static int disable_slots;
  177. /* root can write, others read */
  178. module_param(disable_slots, int, S_IRUGO|S_IWUSR);
  179. /* This structure keeps information per regulator */
  180. struct sdhci_msm_reg_data {
  181. /* voltage regulator handle */
  182. struct regulator *reg;
  183. #if defined(CONFIG_ARCH_MSM8974)\
  184. || defined(CONFIG_MACH_FRESCOLTESKT)||defined(CONFIG_MACH_FRESCOLTEKTT)||defined(CONFIG_MACH_FRESCOLTELGT)
  185. /* rpm regulator */
  186. struct rpm_regulator *rpm_reg;
  187. #endif
  188. /* regulator name */
  189. const char *name;
  190. /* voltage level to be set */
  191. u32 low_vol_level;
  192. u32 high_vol_level;
  193. /* Load values for low power and high power mode */
  194. u32 lpm_uA;
  195. u32 hpm_uA;
  196. /* is this regulator enabled? */
  197. bool is_enabled;
  198. /* is this regulator needs to be always on? */
  199. bool is_always_on;
  200. /* is low power mode setting required for this regulator? */
  201. bool lpm_sup;
  202. bool set_voltage_sup;
  203. };
  204. /*
  205. * This structure keeps information for all the
  206. * regulators required for a SDCC slot.
  207. */
  208. struct sdhci_msm_slot_reg_data {
  209. /* keeps VDD/VCC regulator info */
  210. struct sdhci_msm_reg_data *vdd_data;
  211. /* keeps VDD IO regulator info */
  212. struct sdhci_msm_reg_data *vdd_io_data;
  213. };
  214. struct sdhci_msm_gpio {
  215. u32 no;
  216. const char *name;
  217. bool is_enabled;
  218. };
  219. struct sdhci_msm_gpio_data {
  220. struct sdhci_msm_gpio *gpio;
  221. u8 size;
  222. };
  223. struct sdhci_msm_pad_pull {
  224. enum msm_tlmm_pull_tgt no;
  225. u32 val;
  226. };
  227. struct sdhci_msm_pad_pull_data {
  228. struct sdhci_msm_pad_pull *on;
  229. struct sdhci_msm_pad_pull *off;
  230. u8 size;
  231. };
  232. struct sdhci_msm_pad_drv {
  233. enum msm_tlmm_hdrive_tgt no;
  234. u32 val;
  235. };
  236. struct sdhci_msm_pad_drv_data {
  237. struct sdhci_msm_pad_drv *on;
  238. struct sdhci_msm_pad_drv *off;
  239. u8 size;
  240. };
  241. struct sdhci_msm_pad_data {
  242. struct sdhci_msm_pad_pull_data *pull;
  243. struct sdhci_msm_pad_drv_data *drv;
  244. };
  245. struct sdhci_msm_pin_data {
  246. /*
  247. * = 1 if controller pins are using gpios
  248. * = 0 if controller has dedicated MSM pads
  249. */
  250. u8 is_gpio;
  251. bool cfg_sts;
  252. struct sdhci_msm_gpio_data *gpio_data;
  253. struct sdhci_msm_pad_data *pad_data;
  254. };
  255. struct sdhci_msm_bus_voting_data {
  256. struct msm_bus_scale_pdata *bus_pdata;
  257. unsigned int *bw_vecs;
  258. unsigned int bw_vecs_size;
  259. };
  260. struct sdhci_msm_pltfm_data {
  261. /* Supported UHS-I Modes */
  262. u32 caps;
  263. /* More capabilities */
  264. u32 caps2;
  265. unsigned long mmc_bus_width;
  266. struct sdhci_msm_slot_reg_data *vreg_data;
  267. bool nonremovable;
  268. struct sdhci_msm_pin_data *pin_data;
  269. u32 cpu_dma_latency_us;
  270. int status_gpio; /* card detection GPIO that is configured as IRQ */
  271. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  272. int ls_gpio; /* level shifter gpio */
  273. #endif
  274. struct sdhci_msm_bus_voting_data *voting_data;
  275. u32 *sup_clk_table;
  276. unsigned char sup_clk_cnt;
  277. int mpm_sdiowakeup_int;
  278. int sdiowakeup_irq;
  279. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  280. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  281. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  282. bool is_status_cb;
  283. int (*register_status_notify)(void (*callback)(int card_present, void *dev_id),
  284. void *dev_id, void *mmc_host);
  285. #endif /* CONFIG_BCM4335 || CONFIG_BCM4335_MODULE || CONFIG_BCM4339 || CONFIG_BCCM4339_MODULE */
  286. };
  287. struct sdhci_msm_bus_vote {
  288. uint32_t client_handle;
  289. uint32_t curr_vote;
  290. int min_bw_vote;
  291. int max_bw_vote;
  292. bool is_max_bw_needed;
  293. struct delayed_work vote_work;
  294. struct device_attribute max_bus_bw;
  295. };
  296. struct sdhci_msm_host {
  297. struct platform_device *pdev;
  298. int id;
  299. void __iomem *core_mem; /* MSM SDCC mapped address */
  300. int pwr_irq; /* power irq */
  301. struct clk *clk; /* main SD/MMC bus clock */
  302. struct clk *pclk; /* SDHC peripheral bus clock */
  303. struct clk *bus_clk; /* SDHC bus voter clock */
  304. struct clk *ff_clk; /* CDC calibration fixed feedback clock */
  305. struct clk *sleep_clk; /* CDC calibration sleep clock */
  306. atomic_t clks_on; /* Set if clocks are enabled */
  307. struct sdhci_msm_pltfm_data *pdata;
  308. struct mmc_host *mmc;
  309. struct sdhci_pltfm_data sdhci_msm_pdata;
  310. u32 curr_pwr_state;
  311. u32 curr_io_level;
  312. struct completion pwr_irq_completion;
  313. struct sdhci_msm_bus_vote msm_bus_vote;
  314. struct device_attribute polling;
  315. u32 clk_rate; /* Keeps track of current clock rate that is set */
  316. bool tuning_done;
  317. bool calibration_done;
  318. u8 saved_tuning_phase;
  319. bool is_sdiowakeup_enabled;
  320. atomic_t controller_clock;
  321. };
  322. enum vdd_io_level {
  323. /* set vdd_io_data->low_vol_level */
  324. VDD_IO_LOW,
  325. /* set vdd_io_data->high_vol_level */
  326. VDD_IO_HIGH,
  327. /*
  328. * set whatever there in voltage_level (third argument) of
  329. * sdhci_msm_set_vdd_io_vol() function.
  330. */
  331. VDD_IO_SET_LEVEL,
  332. };
  333. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  334. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  335. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  336. extern int brcm_wifi_status_register(
  337. void (*callback)(int card_present, void *dev_id), void *dev_id, void *mmc_host);
  338. #endif
  339. /* MSM platform specific tuning */
  340. static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host,
  341. u8 poll)
  342. {
  343. int rc = 0;
  344. u32 wait_cnt = 50;
  345. u8 ck_out_en = 0;
  346. struct mmc_host *mmc = host->mmc;
  347. /* poll for CK_OUT_EN bit. max. poll time = 50us */
  348. ck_out_en = !!(readl_relaxed(host->ioaddr + CORE_DLL_CONFIG) &
  349. CORE_CK_OUT_EN);
  350. while (ck_out_en != poll) {
  351. if (--wait_cnt == 0) {
  352. pr_err("%s: %s: CK_OUT_EN bit is not %d\n",
  353. mmc_hostname(mmc), __func__, poll);
  354. rc = -ETIMEDOUT;
  355. goto out;
  356. }
  357. udelay(1);
  358. ck_out_en = !!(readl_relaxed(host->ioaddr +
  359. CORE_DLL_CONFIG) & CORE_CK_OUT_EN);
  360. }
  361. out:
  362. return rc;
  363. }
  364. static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
  365. {
  366. int rc = 0;
  367. u8 grey_coded_phase_table[] = {0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
  368. 0xC, 0xD, 0xF, 0xE, 0xA, 0xB, 0x9,
  369. 0x8};
  370. unsigned long flags;
  371. u32 config;
  372. struct mmc_host *mmc = host->mmc;
  373. pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
  374. spin_lock_irqsave(&host->lock, flags);
  375. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  376. config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
  377. config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
  378. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  379. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
  380. rc = msm_dll_poll_ck_out_en(host, 0);
  381. if (rc)
  382. goto err_out;
  383. /*
  384. * Write the selected DLL clock output phase (0 ... 15)
  385. * to CDR_SELEXT bit field of DLL_CONFIG register.
  386. */
  387. writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  388. & ~(0xF << 20))
  389. | (grey_coded_phase_table[phase] << 20)),
  390. host->ioaddr + CORE_DLL_CONFIG);
  391. /* Set CK_OUT_EN bit of DLL_CONFIG register to 1. */
  392. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  393. | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  394. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
  395. rc = msm_dll_poll_ck_out_en(host, 1);
  396. if (rc)
  397. goto err_out;
  398. config = readl_relaxed(host->ioaddr + CORE_DLL_CONFIG);
  399. config |= CORE_CDR_EN;
  400. config &= ~CORE_CDR_EXT_EN;
  401. writel_relaxed(config, host->ioaddr + CORE_DLL_CONFIG);
  402. goto out;
  403. err_out:
  404. pr_err("%s: %s: Failed to set DLL phase: %d\n",
  405. mmc_hostname(mmc), __func__, phase);
  406. out:
  407. spin_unlock_irqrestore(&host->lock, flags);
  408. pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
  409. return rc;
  410. }
  411. /*
  412. * Find out the greatest range of consecuitive selected
  413. * DLL clock output phases that can be used as sampling
  414. * setting for SD3.0 UHS-I card read operation (in SDR104
  415. * timing mode) or for eMMC4.5 card read operation (in
  416. * HS400/HS200 timing mode).
  417. * Select the 3/4 of the range and configure the DLL with the
  418. * selected DLL clock output phase.
  419. */
  420. static int msm_find_most_appropriate_phase(struct sdhci_host *host,
  421. u8 *phase_table, u8 total_phases)
  422. {
  423. int ret;
  424. u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
  425. u8 phases_per_row[MAX_PHASES] = {0};
  426. int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
  427. int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
  428. bool phase_0_found = false, phase_15_found = false;
  429. struct mmc_host *mmc = host->mmc;
  430. pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
  431. if (!total_phases || (total_phases > MAX_PHASES)) {
  432. pr_err("%s: %s: invalid argument: total_phases=%d\n",
  433. mmc_hostname(mmc), __func__, total_phases);
  434. return -EINVAL;
  435. }
  436. for (cnt = 0; cnt < total_phases; cnt++) {
  437. ranges[row_index][col_index] = phase_table[cnt];
  438. phases_per_row[row_index] += 1;
  439. col_index++;
  440. if ((cnt + 1) == total_phases) {
  441. continue;
  442. /* check if next phase in phase_table is consecutive or not */
  443. } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
  444. row_index++;
  445. col_index = 0;
  446. }
  447. }
  448. if (row_index >= MAX_PHASES)
  449. return -EINVAL;
  450. /* Check if phase-0 is present in first valid window? */
  451. if (!ranges[0][0]) {
  452. phase_0_found = true;
  453. phase_0_raw_index = 0;
  454. /* Check if cycle exist between 2 valid windows */
  455. for (cnt = 1; cnt <= row_index; cnt++) {
  456. if (phases_per_row[cnt]) {
  457. for (i = 0; i < phases_per_row[cnt]; i++) {
  458. if (ranges[cnt][i] == 15) {
  459. phase_15_found = true;
  460. phase_15_raw_index = cnt;
  461. break;
  462. }
  463. }
  464. }
  465. }
  466. }
  467. /* If 2 valid windows form cycle then merge them as single window */
  468. if (phase_0_found && phase_15_found) {
  469. /* number of phases in raw where phase 0 is present */
  470. u8 phases_0 = phases_per_row[phase_0_raw_index];
  471. /* number of phases in raw where phase 15 is present */
  472. u8 phases_15 = phases_per_row[phase_15_raw_index];
  473. if (phases_0 + phases_15 >= MAX_PHASES)
  474. /*
  475. * If there are more than 1 phase windows then total
  476. * number of phases in both the windows should not be
  477. * more than or equal to MAX_PHASES.
  478. */
  479. return -EINVAL;
  480. /* Merge 2 cyclic windows */
  481. i = phases_15;
  482. for (cnt = 0; cnt < phases_0; cnt++) {
  483. ranges[phase_15_raw_index][i] =
  484. ranges[phase_0_raw_index][cnt];
  485. if (++i >= MAX_PHASES)
  486. break;
  487. }
  488. phases_per_row[phase_0_raw_index] = 0;
  489. phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
  490. }
  491. for (cnt = 0; cnt <= row_index; cnt++) {
  492. if (phases_per_row[cnt] > curr_max) {
  493. curr_max = phases_per_row[cnt];
  494. selected_row_index = cnt;
  495. }
  496. }
  497. i = ((curr_max * 3) / 4);
  498. if (i)
  499. i--;
  500. ret = (int)ranges[selected_row_index][i];
  501. if (ret >= MAX_PHASES) {
  502. ret = -EINVAL;
  503. pr_err("%s: %s: invalid phase selected=%d\n",
  504. mmc_hostname(mmc), __func__, ret);
  505. }
  506. pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
  507. return ret;
  508. }
  509. static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
  510. {
  511. u32 mclk_freq = 0;
  512. /* Program the MCLK value to MCLK_FREQ bit field */
  513. if (host->clock <= 112000000)
  514. mclk_freq = 0;
  515. else if (host->clock <= 125000000)
  516. mclk_freq = 1;
  517. else if (host->clock <= 137000000)
  518. mclk_freq = 2;
  519. else if (host->clock <= 150000000)
  520. mclk_freq = 3;
  521. else if (host->clock <= 162000000)
  522. mclk_freq = 4;
  523. else if (host->clock <= 175000000)
  524. mclk_freq = 5;
  525. else if (host->clock <= 187000000)
  526. mclk_freq = 6;
  527. else if (host->clock <= 200000000)
  528. mclk_freq = 7;
  529. writel_relaxed(((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  530. & ~(7 << 24)) | (mclk_freq << 24)),
  531. host->ioaddr + CORE_DLL_CONFIG);
  532. }
  533. /* Initialize the DLL (Programmable Delay Line ) */
  534. static int msm_init_cm_dll(struct sdhci_host *host)
  535. {
  536. struct mmc_host *mmc = host->mmc;
  537. int rc = 0;
  538. unsigned long flags;
  539. u32 wait_cnt;
  540. bool prev_pwrsave, curr_pwrsave;
  541. pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
  542. spin_lock_irqsave(&host->lock, flags);
  543. prev_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
  544. CORE_CLK_PWRSAVE);
  545. curr_pwrsave = prev_pwrsave;
  546. /*
  547. * Make sure that clock is always enabled when DLL
  548. * tuning is in progress. Keeping PWRSAVE ON may
  549. * turn off the clock. So let's disable the PWRSAVE
  550. * here and re-enable it once tuning is completed.
  551. */
  552. if (prev_pwrsave) {
  553. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  554. & ~CORE_CLK_PWRSAVE),
  555. host->ioaddr + CORE_VENDOR_SPEC);
  556. curr_pwrsave = false;
  557. }
  558. /* Write 1 to DLL_RST bit of DLL_CONFIG register */
  559. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  560. | CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  561. /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
  562. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  563. | CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  564. msm_cm_dll_set_freq(host);
  565. /* Write 0 to DLL_RST bit of DLL_CONFIG register */
  566. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  567. & ~CORE_DLL_RST), host->ioaddr + CORE_DLL_CONFIG);
  568. /* Write 0 to DLL_PDN bit of DLL_CONFIG register */
  569. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  570. & ~CORE_DLL_PDN), host->ioaddr + CORE_DLL_CONFIG);
  571. /* Set DLL_EN bit to 1. */
  572. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  573. | CORE_DLL_EN), host->ioaddr + CORE_DLL_CONFIG);
  574. /* Set CK_OUT_EN bit to 1. */
  575. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  576. | CORE_CK_OUT_EN), host->ioaddr + CORE_DLL_CONFIG);
  577. wait_cnt = 50;
  578. /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
  579. while (!(readl_relaxed(host->ioaddr + CORE_DLL_STATUS) &
  580. CORE_DLL_LOCK)) {
  581. /* max. wait for 50us sec for LOCK bit to be set */
  582. if (--wait_cnt == 0) {
  583. pr_err("%s: %s: DLL failed to LOCK\n",
  584. mmc_hostname(mmc), __func__);
  585. rc = -ETIMEDOUT;
  586. goto out;
  587. }
  588. /* wait for 1us before polling again */
  589. udelay(1);
  590. }
  591. out:
  592. /* Restore the correct PWRSAVE state */
  593. if (prev_pwrsave ^ curr_pwrsave) {
  594. u32 reg = readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC);
  595. if (prev_pwrsave)
  596. reg |= CORE_CLK_PWRSAVE;
  597. else
  598. reg &= ~CORE_CLK_PWRSAVE;
  599. writel_relaxed(reg, host->ioaddr + CORE_VENDOR_SPEC);
  600. }
  601. spin_unlock_irqrestore(&host->lock, flags);
  602. pr_debug("%s: Exit %s\n", mmc_hostname(mmc), __func__);
  603. return rc;
  604. }
  605. static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
  606. {
  607. u32 wait_cnt;
  608. int ret = 0;
  609. int cdc_err = 0;
  610. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  611. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  612. pr_debug("%s: Enter %s\n", mmc_hostname(host->mmc), __func__);
  613. /*
  614. * Retuning in HS400 (DDR mode) will fail, just reset the
  615. * tuning block and restore the saved tuning phase.
  616. */
  617. ret = msm_init_cm_dll(host);
  618. if (ret)
  619. goto out;
  620. /* Set the selected phase in delay line hw block */
  621. ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
  622. if (ret)
  623. goto out;
  624. /* Write 1 to CMD_DAT_TRACK_SEL field in DLL_CONFIG */
  625. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  626. | CORE_CMD_DAT_TRACK_SEL),
  627. host->ioaddr + CORE_DLL_CONFIG);
  628. /* Write 0 to CDC_T4_DLY_SEL field in VENDOR_SPEC_DDR200_CFG */
  629. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
  630. & ~CORE_CDC_T4_DLY_SEL),
  631. host->ioaddr + CORE_DDR_200_CFG);
  632. /* Write 0 to CDC_SWITCH_BYPASS_OFF field in CORE_CSR_CDC_GEN_CFG */
  633. writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
  634. & ~CORE_CDC_SWITCH_BYPASS_OFF),
  635. host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  636. /* Write 1 to CDC_SWITCH_RC_EN field in CORE_CSR_CDC_GEN_CFG */
  637. writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG)
  638. | CORE_CDC_SWITCH_RC_EN),
  639. host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  640. /* Write 0 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
  641. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
  642. & ~CORE_START_CDC_TRAFFIC),
  643. host->ioaddr + CORE_DDR_200_CFG);
  644. /*
  645. * Perform CDC Register Initialization Sequence
  646. *
  647. * CORE_CSR_CDC_CTLR_CFG0 0x11800EC
  648. * CORE_CSR_CDC_CTLR_CFG1 0x3011111
  649. * CORE_CSR_CDC_CAL_TIMER_CFG0 0x1201000
  650. * CORE_CSR_CDC_CAL_TIMER_CFG1 0x4
  651. * CORE_CSR_CDC_REFCOUNT_CFG 0xCB732020
  652. * CORE_CSR_CDC_COARSE_CAL_CFG 0xB19
  653. * CORE_CSR_CDC_DELAY_CFG 0x3AC
  654. * CORE_CDC_OFFSET_CFG 0x0
  655. * CORE_CDC_SLAVE_DDA_CFG 0x16334
  656. */
  657. writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  658. writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
  659. writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  660. writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
  661. writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
  662. writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
  663. writel_relaxed(0x3AC, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
  664. writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
  665. writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
  666. /* CDC HW Calibration */
  667. /* Write 1 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
  668. writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
  669. | CORE_SW_TRIG_FULL_CALIB),
  670. host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  671. /* Write 0 to SW_TRIG_FULL_CALIB field in CORE_CSR_CDC_CTLR_CFG0 */
  672. writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
  673. & ~CORE_SW_TRIG_FULL_CALIB),
  674. host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  675. /* Write 1 to HW_AUTOCAL_ENA field in CORE_CSR_CDC_CTLR_CFG0 */
  676. writel_relaxed((readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0)
  677. | CORE_HW_AUTOCAL_ENA),
  678. host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  679. /* Write 1 to TIMER_ENA field in CORE_CSR_CDC_CAL_TIMER_CFG0 */
  680. writel_relaxed((readl_relaxed(host->ioaddr +
  681. CORE_CSR_CDC_CAL_TIMER_CFG0) | CORE_TIMER_ENA),
  682. host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  683. mb();
  684. /* Poll on CALIBRATION_DONE field in CORE_CSR_CDC_STATUS0 to be 1 */
  685. wait_cnt = 50;
  686. while (!(readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
  687. & CORE_CALIBRATION_DONE)) {
  688. /* max. wait for 50us sec for CALIBRATION_DONE bit to be set */
  689. if (--wait_cnt == 0) {
  690. pr_err("%s: %s: CDC Calibration was not completed\n",
  691. mmc_hostname(host->mmc), __func__);
  692. ret = -ETIMEDOUT;
  693. goto out;
  694. }
  695. /* wait for 1us before polling again */
  696. udelay(1);
  697. }
  698. /* Verify CDC_ERROR_CODE field in CORE_CSR_CDC_STATUS0 is 0 */
  699. cdc_err = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
  700. & CORE_CDC_ERROR_CODE_MASK;
  701. if (cdc_err) {
  702. pr_err("%s: %s: CDC Error Code %d\n",
  703. mmc_hostname(host->mmc), __func__, cdc_err);
  704. ret = -EINVAL;
  705. goto out;
  706. }
  707. /* Write 1 to START_CDC_TRAFFIC field in CORE_DDR200_CFG */
  708. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DDR_200_CFG)
  709. | CORE_START_CDC_TRAFFIC),
  710. host->ioaddr + CORE_DDR_200_CFG);
  711. out:
  712. pr_debug("%s: Exit %s, ret:%d\n", mmc_hostname(host->mmc),
  713. __func__, ret);
  714. return ret;
  715. }
  716. static void sdhci_msm_set_mmc_drv_type(struct sdhci_host *host, u32 opcode,
  717. u8 drv_type)
  718. {
  719. struct mmc_command cmd = {0};
  720. struct mmc_request mrq = {NULL};
  721. struct mmc_host *mmc = host->mmc;
  722. u8 val = ((drv_type << 4) | 2);
  723. cmd.opcode = MMC_SWITCH;
  724. cmd.arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
  725. (EXT_CSD_HS_TIMING << 16) |
  726. (val << 8) |
  727. EXT_CSD_CMD_SET_NORMAL;
  728. cmd.flags = MMC_CMD_AC | MMC_RSP_R1B;
  729. /* 1 sec */
  730. cmd.cmd_timeout_ms = 1000 * 1000;
  731. memset(cmd.resp, 0, sizeof(cmd.resp));
  732. cmd.retries = 3;
  733. mrq.cmd = &cmd;
  734. cmd.data = NULL;
  735. mmc_wait_for_req(mmc, &mrq);
  736. pr_debug("%s: %s: set card drive type to %d\n",
  737. mmc_hostname(mmc), __func__,
  738. drv_type);
  739. }
  740. int sdhci_msm_execute_tuning(struct sdhci_host *host, u32 opcode)
  741. {
  742. unsigned long flags;
  743. int tuning_seq_cnt = 3;
  744. u8 phase, *data_buf, tuned_phases[NUM_TUNING_PHASES], tuned_phase_cnt;
  745. const u32 *tuning_block_pattern = tuning_block_64;
  746. int size = sizeof(tuning_block_64); /* Tuning pattern size in bytes */
  747. int rc;
  748. struct mmc_host *mmc = host->mmc;
  749. struct mmc_ios ios = host->mmc->ios;
  750. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  751. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  752. u8 drv_type = 0;
  753. bool drv_type_changed = false;
  754. struct mmc_card *card = host->mmc->card;
  755. int sts_retry;
  756. /*
  757. * Tuning is required for SDR104, HS200 and HS400 cards and
  758. * if clock frequency is greater than 100MHz in these modes.
  759. */
  760. if (host->clock <= CORE_FREQ_100MHZ ||
  761. !((ios.timing == MMC_TIMING_MMC_HS400) ||
  762. (ios.timing == MMC_TIMING_MMC_HS200) ||
  763. (ios.timing == MMC_TIMING_UHS_SDR104)))
  764. return 0;
  765. pr_debug("%s: Enter %s\n", mmc_hostname(mmc), __func__);
  766. /* CDCLP533 HW calibration is only required for HS400 mode*/
  767. if (msm_host->tuning_done && !msm_host->calibration_done &&
  768. (mmc->ios.timing == MMC_TIMING_MMC_HS400)) {
  769. rc = sdhci_msm_cdclp533_calibration(host);
  770. spin_lock_irqsave(&host->lock, flags);
  771. if (!rc)
  772. msm_host->calibration_done = true;
  773. spin_unlock_irqrestore(&host->lock, flags);
  774. goto out;
  775. }
  776. spin_lock_irqsave(&host->lock, flags);
  777. if (((opcode == MMC_SEND_TUNING_BLOCK_HS400) ||
  778. (opcode == MMC_SEND_TUNING_BLOCK_HS200)) &&
  779. (mmc->ios.bus_width == MMC_BUS_WIDTH_8)) {
  780. tuning_block_pattern = tuning_block_128;
  781. size = sizeof(tuning_block_128);
  782. }
  783. spin_unlock_irqrestore(&host->lock, flags);
  784. data_buf = kmalloc(size, GFP_KERNEL);
  785. if (!data_buf) {
  786. rc = -ENOMEM;
  787. goto out;
  788. }
  789. retry:
  790. tuned_phase_cnt = 0;
  791. /* first of all reset the tuning block */
  792. rc = msm_init_cm_dll(host);
  793. if (rc)
  794. goto kfree;
  795. phase = 0;
  796. do {
  797. struct mmc_command cmd = {0};
  798. struct mmc_data data = {0};
  799. struct mmc_request mrq = {
  800. .cmd = &cmd,
  801. .data = &data
  802. };
  803. struct scatterlist sg;
  804. struct mmc_command sts_cmd = {0};
  805. /* set the phase in delay line hw block */
  806. rc = msm_config_cm_dll_phase(host, phase);
  807. if (rc)
  808. goto kfree;
  809. cmd.opcode = opcode;
  810. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  811. data.blksz = size;
  812. data.blocks = 1;
  813. data.flags = MMC_DATA_READ;
  814. data.timeout_ns = 1000 * 1000 * 1000; /* 1 sec */
  815. data.sg = &sg;
  816. data.sg_len = 1;
  817. sg_init_one(&sg, data_buf, size);
  818. memset(data_buf, 0, size);
  819. mmc_wait_for_req(mmc, &mrq);
  820. if (card && (cmd.error || data.error)) {
  821. sts_cmd.opcode = MMC_SEND_STATUS;
  822. sts_cmd.arg = card->rca << 16;
  823. sts_cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  824. sts_retry = 5;
  825. while (sts_retry) {
  826. mmc_wait_for_cmd(mmc, &sts_cmd, 0);
  827. if (sts_cmd.error ||
  828. (R1_CURRENT_STATE(sts_cmd.resp[0])
  829. != R1_STATE_TRAN)) {
  830. sts_retry--;
  831. /*
  832. * wait for at least 146 MCLK cycles for
  833. * the card to move to TRANS state. As
  834. * the MCLK would be min 200MHz for
  835. * tuning, we need max 0.73us delay. To
  836. * be on safer side 1ms delay is given.
  837. */
  838. usleep_range(1000, 1200);
  839. pr_debug("%s: phase %d sts cmd err %d resp 0x%x\n",
  840. mmc_hostname(mmc), phase,
  841. sts_cmd.error, sts_cmd.resp[0]);
  842. continue;
  843. }
  844. break;
  845. };
  846. }
  847. if (!cmd.error && !data.error &&
  848. !memcmp(data_buf, tuning_block_pattern, size)) {
  849. /* tuning is successful at this tuning point */
  850. tuned_phases[tuned_phase_cnt++] = phase;
  851. pr_debug("%s: %s: found *** good *** phase = %d\n",
  852. mmc_hostname(mmc), __func__, phase);
  853. } else {
  854. pr_debug("%s: %s: found ## bad ## phase = %d\n",
  855. mmc_hostname(mmc), __func__, phase);
  856. if (cmd.error)
  857. pr_err("%s: %s: cmd error (%d) \n",
  858. mmc_hostname(mmc), __func__, cmd.error);
  859. else if (data.error)
  860. pr_err("%s: %s: data error (%d) \n",
  861. mmc_hostname(mmc), __func__, data.error);
  862. else
  863. pr_err("%s: %s: data buf != tuning_block_pattern \n",
  864. mmc_hostname(mmc), __func__);
  865. }
  866. } while (++phase < 16);
  867. if ((tuned_phase_cnt == NUM_TUNING_PHASES) &&
  868. card && mmc_card_mmc(card)) {
  869. /*
  870. * If all phases pass then its a problem. So change the card's
  871. * drive type to a different value, if supported and repeat
  872. * tuning until at least one phase fails. Then set the original
  873. * drive type back.
  874. *
  875. * If all the phases still pass after trying all possible
  876. * drive type, then one of those 16 phases will be picked.
  877. * This is no different from what was going on before the
  878. * modification to change drive type and retune.
  879. */
  880. pr_debug("%s: tuned phases count: %d\n", mmc_hostname(mmc),
  881. tuned_phase_cnt);
  882. /* set drive type to other value. default setting is 0x0 */
  883. while (++drv_type <= MAX_DRV_TYPES_SUPPORTED_HS200) {
  884. if (card->ext_csd.raw_drive_strength &
  885. (1 << drv_type)) {
  886. sdhci_msm_set_mmc_drv_type(host, opcode,
  887. drv_type);
  888. if (!drv_type_changed)
  889. drv_type_changed = true;
  890. goto retry;
  891. }
  892. }
  893. }
  894. /* reset drive type to default (50 ohm) if changed */
  895. if (drv_type_changed)
  896. sdhci_msm_set_mmc_drv_type(host, opcode, 0);
  897. if (tuned_phase_cnt) {
  898. rc = msm_find_most_appropriate_phase(host, tuned_phases,
  899. tuned_phase_cnt);
  900. if (rc < 0)
  901. goto kfree;
  902. else
  903. phase = (u8)rc;
  904. /*
  905. * Finally set the selected phase in delay
  906. * line hw block.
  907. */
  908. rc = msm_config_cm_dll_phase(host, phase);
  909. if (rc)
  910. goto kfree;
  911. msm_host->saved_tuning_phase = phase;
  912. pr_debug("%s: %s: finally setting the tuning phase to %d\n",
  913. mmc_hostname(mmc), __func__, phase);
  914. } else {
  915. if (--tuning_seq_cnt)
  916. goto retry;
  917. /* tuning failed */
  918. pr_err("%s: %s: no tuning point found\n",
  919. mmc_hostname(mmc), __func__);
  920. rc = -EIO;
  921. }
  922. kfree:
  923. kfree(data_buf);
  924. out:
  925. spin_lock_irqsave(&host->lock, flags);
  926. if (!rc)
  927. msm_host->tuning_done = true;
  928. spin_unlock_irqrestore(&host->lock, flags);
  929. pr_debug("%s: Exit %s, err(%d)\n", mmc_hostname(mmc), __func__, rc);
  930. return rc;
  931. }
  932. static int sdhci_msm_setup_gpio(struct sdhci_msm_pltfm_data *pdata, bool enable)
  933. {
  934. struct sdhci_msm_gpio_data *curr;
  935. int i, ret = 0;
  936. curr = pdata->pin_data->gpio_data;
  937. for (i = 0; i < curr->size; i++) {
  938. if (!gpio_is_valid(curr->gpio[i].no)) {
  939. ret = -EINVAL;
  940. pr_err("%s: Invalid gpio = %d\n", __func__,
  941. curr->gpio[i].no);
  942. goto free_gpios;
  943. }
  944. if (enable) {
  945. ret = gpio_request(curr->gpio[i].no,
  946. curr->gpio[i].name);
  947. if (ret) {
  948. pr_err("%s: gpio_request(%d, %s) failed %d\n",
  949. __func__, curr->gpio[i].no,
  950. curr->gpio[i].name, ret);
  951. goto free_gpios;
  952. }
  953. curr->gpio[i].is_enabled = true;
  954. } else {
  955. gpio_free(curr->gpio[i].no);
  956. curr->gpio[i].is_enabled = false;
  957. }
  958. }
  959. return ret;
  960. free_gpios:
  961. for (i--; i >= 0; i--) {
  962. gpio_free(curr->gpio[i].no);
  963. curr->gpio[i].is_enabled = false;
  964. }
  965. return ret;
  966. }
  967. static int sdhci_msm_setup_pad(struct sdhci_msm_pltfm_data *pdata, bool enable)
  968. {
  969. struct sdhci_msm_pad_data *curr;
  970. int i;
  971. curr = pdata->pin_data->pad_data;
  972. for (i = 0; i < curr->drv->size; i++) {
  973. if (enable)
  974. msm_tlmm_set_hdrive(curr->drv->on[i].no,
  975. curr->drv->on[i].val);
  976. else
  977. msm_tlmm_set_hdrive(curr->drv->off[i].no,
  978. curr->drv->off[i].val);
  979. }
  980. for (i = 0; i < curr->pull->size; i++) {
  981. if (enable)
  982. msm_tlmm_set_pull(curr->pull->on[i].no,
  983. curr->pull->on[i].val);
  984. else
  985. msm_tlmm_set_pull(curr->pull->off[i].no,
  986. curr->pull->off[i].val);
  987. }
  988. return 0;
  989. }
  990. static int sdhci_msm_setup_pins(struct sdhci_msm_pltfm_data *pdata, bool enable)
  991. {
  992. int ret = 0;
  993. if (!pdata->pin_data || (pdata->pin_data->cfg_sts == enable))
  994. return 0;
  995. if (pdata->pin_data->is_gpio)
  996. ret = sdhci_msm_setup_gpio(pdata, enable);
  997. else
  998. ret = sdhci_msm_setup_pad(pdata, enable);
  999. if (!ret)
  1000. pdata->pin_data->cfg_sts = enable;
  1001. return ret;
  1002. }
  1003. static int sdhci_msm_dt_get_array(struct device *dev, const char *prop_name,
  1004. u32 **out, int *len, u32 size)
  1005. {
  1006. int ret = 0;
  1007. struct device_node *np = dev->of_node;
  1008. size_t sz;
  1009. u32 *arr = NULL;
  1010. if (!of_get_property(np, prop_name, len)) {
  1011. ret = -EINVAL;
  1012. goto out;
  1013. }
  1014. sz = *len = *len / sizeof(*arr);
  1015. if (sz <= 0 || (size > 0 && (sz > size))) {
  1016. dev_err(dev, "%s invalid size\n", prop_name);
  1017. ret = -EINVAL;
  1018. goto out;
  1019. }
  1020. arr = devm_kzalloc(dev, sz * sizeof(*arr), GFP_KERNEL);
  1021. if (!arr) {
  1022. dev_err(dev, "%s failed allocating memory\n", prop_name);
  1023. ret = -ENOMEM;
  1024. goto out;
  1025. }
  1026. ret = of_property_read_u32_array(np, prop_name, arr, sz);
  1027. if (ret < 0) {
  1028. dev_err(dev, "%s failed reading array %d\n", prop_name, ret);
  1029. goto out;
  1030. }
  1031. *out = arr;
  1032. out:
  1033. if (ret)
  1034. *len = 0;
  1035. return ret;
  1036. }
  1037. #define MAX_PROP_SIZE 32
  1038. static int sdhci_msm_dt_parse_vreg_info(struct device *dev,
  1039. struct sdhci_msm_reg_data **vreg_data, const char *vreg_name)
  1040. {
  1041. int len, ret = 0;
  1042. const __be32 *prop;
  1043. char prop_name[MAX_PROP_SIZE];
  1044. struct sdhci_msm_reg_data *vreg;
  1045. struct device_node *np = dev->of_node;
  1046. snprintf(prop_name, MAX_PROP_SIZE, "%s-supply", vreg_name);
  1047. #if !defined(CONFIG_SEC_PATEK_PROJECT)
  1048. if (!of_parse_phandle(np, prop_name, 0)) {
  1049. dev_info(dev, "No vreg data found for %s\n", vreg_name);
  1050. return ret;
  1051. }
  1052. #endif
  1053. vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL);
  1054. if (!vreg) {
  1055. dev_err(dev, "No memory for vreg: %s\n", vreg_name);
  1056. ret = -ENOMEM;
  1057. return ret;
  1058. }
  1059. #if defined(CONFIG_SEC_PATEK_PROJECT)
  1060. if (!strcmp(dev_name(dev), "msm_sdcc.3")) {
  1061. if (!strcmp(vreg_name, "vdd")) {
  1062. vreg->reg = regulator_get(dev, "max77826_ldo10");
  1063. dev_info(dev, "max77826_ldo10 is for %s\n", vreg_name);
  1064. }
  1065. else if (!strcmp(vreg_name, "vdd-io")) {
  1066. vreg->reg = regulator_get(dev, "max77826_ldo4");
  1067. dev_info(dev, "max77826_ldo4 is for %s\n", vreg_name);
  1068. }
  1069. if (IS_ERR(vreg->reg))
  1070. vreg->reg = NULL;
  1071. if (!vreg->reg)
  1072. dev_err(dev, "Settng Regulator for %s is failed\n", vreg_name);
  1073. else if (!strcmp(vreg_name, "vdd-io")) {
  1074. vreg->set_voltage_sup = true;
  1075. regulator_set_voltage(vreg->reg, 2950000, 2950000);
  1076. }
  1077. }
  1078. #endif
  1079. vreg->name = vreg_name;
  1080. snprintf(prop_name, MAX_PROP_SIZE,
  1081. "qcom,%s-always-on", vreg_name);
  1082. if (of_get_property(np, prop_name, NULL))
  1083. vreg->is_always_on = true;
  1084. snprintf(prop_name, MAX_PROP_SIZE,
  1085. "qcom,%s-lpm-sup", vreg_name);
  1086. if (of_get_property(np, prop_name, NULL))
  1087. vreg->lpm_sup = true;
  1088. snprintf(prop_name, MAX_PROP_SIZE,
  1089. "qcom,%s-voltage-level", vreg_name);
  1090. prop = of_get_property(np, prop_name, &len);
  1091. if (!prop || (len != (2 * sizeof(__be32)))) {
  1092. dev_warn(dev, "%s %s property\n",
  1093. prop ? "invalid format" : "no", prop_name);
  1094. } else {
  1095. vreg->low_vol_level = be32_to_cpup(&prop[0]);
  1096. vreg->high_vol_level = be32_to_cpup(&prop[1]);
  1097. }
  1098. snprintf(prop_name, MAX_PROP_SIZE,
  1099. "qcom,%s-current-level", vreg_name);
  1100. prop = of_get_property(np, prop_name, &len);
  1101. if (!prop || (len != (2 * sizeof(__be32)))) {
  1102. dev_warn(dev, "%s %s property\n",
  1103. prop ? "invalid format" : "no", prop_name);
  1104. } else {
  1105. vreg->lpm_uA = be32_to_cpup(&prop[0]);
  1106. vreg->hpm_uA = be32_to_cpup(&prop[1]);
  1107. }
  1108. *vreg_data = vreg;
  1109. dev_dbg(dev, "%s: %s %s vol=[%d %d]uV, curr=[%d %d]uA\n",
  1110. vreg->name, vreg->is_always_on ? "always_on," : "",
  1111. vreg->lpm_sup ? "lpm_sup," : "", vreg->low_vol_level,
  1112. vreg->high_vol_level, vreg->lpm_uA, vreg->hpm_uA);
  1113. return ret;
  1114. }
  1115. /* GPIO/Pad data extraction */
  1116. static int sdhci_msm_dt_get_pad_pull_info(struct device *dev, int id,
  1117. struct sdhci_msm_pad_pull_data **pad_pull_data)
  1118. {
  1119. int ret = 0, base = 0, len, i;
  1120. u32 *tmp;
  1121. struct sdhci_msm_pad_pull_data *pull_data;
  1122. struct sdhci_msm_pad_pull *pull;
  1123. switch (id) {
  1124. case 1:
  1125. base = TLMM_PULL_SDC1_CLK;
  1126. break;
  1127. case 2:
  1128. base = TLMM_PULL_SDC2_CLK;
  1129. break;
  1130. case 3:
  1131. base = TLMM_PULL_SDC3_CLK;
  1132. break;
  1133. case 4:
  1134. base = TLMM_PULL_SDC4_CLK;
  1135. break;
  1136. default:
  1137. dev_err(dev, "%s: Invalid slot id\n", __func__);
  1138. ret = -EINVAL;
  1139. goto out;
  1140. }
  1141. pull_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_pull_data),
  1142. GFP_KERNEL);
  1143. if (!pull_data) {
  1144. dev_err(dev, "No memory for msm_mmc_pad_pull_data\n");
  1145. ret = -ENOMEM;
  1146. goto out;
  1147. }
  1148. pull_data->size = 4; /* array size for clk, cmd, data and rclk */
  1149. /* Allocate on, off configs for clk, cmd, data and rclk */
  1150. pull = devm_kzalloc(dev, 2 * pull_data->size *\
  1151. sizeof(struct sdhci_msm_pad_pull), GFP_KERNEL);
  1152. if (!pull) {
  1153. dev_err(dev, "No memory for msm_mmc_pad_pull\n");
  1154. ret = -ENOMEM;
  1155. goto out;
  1156. }
  1157. pull_data->on = pull;
  1158. pull_data->off = pull + pull_data->size;
  1159. ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-on",
  1160. &tmp, &len, pull_data->size);
  1161. if (ret)
  1162. goto out;
  1163. for (i = 0; i < len; i++) {
  1164. pull_data->on[i].no = base + i;
  1165. pull_data->on[i].val = tmp[i];
  1166. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  1167. i, pull_data->on[i].val);
  1168. }
  1169. ret = sdhci_msm_dt_get_array(dev, "qcom,pad-pull-off",
  1170. &tmp, &len, pull_data->size);
  1171. if (ret)
  1172. goto out;
  1173. for (i = 0; i < len; i++) {
  1174. pull_data->off[i].no = base + i;
  1175. pull_data->off[i].val = tmp[i];
  1176. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  1177. i, pull_data->off[i].val);
  1178. }
  1179. *pad_pull_data = pull_data;
  1180. out:
  1181. return ret;
  1182. }
  1183. static int sdhci_msm_dt_get_pad_drv_info(struct device *dev, int id,
  1184. struct sdhci_msm_pad_drv_data **pad_drv_data)
  1185. {
  1186. int ret = 0, base = 0, len, i;
  1187. u32 *tmp;
  1188. struct sdhci_msm_pad_drv_data *drv_data;
  1189. struct sdhci_msm_pad_drv *drv;
  1190. switch (id) {
  1191. case 1:
  1192. base = TLMM_HDRV_SDC1_CLK;
  1193. break;
  1194. case 2:
  1195. base = TLMM_HDRV_SDC2_CLK;
  1196. break;
  1197. case 3:
  1198. base = TLMM_HDRV_SDC3_CLK;
  1199. break;
  1200. case 4:
  1201. base = TLMM_HDRV_SDC4_CLK;
  1202. break;
  1203. default:
  1204. dev_err(dev, "%s: Invalid slot id\n", __func__);
  1205. ret = -EINVAL;
  1206. goto out;
  1207. }
  1208. drv_data = devm_kzalloc(dev, sizeof(struct sdhci_msm_pad_drv_data),
  1209. GFP_KERNEL);
  1210. if (!drv_data) {
  1211. dev_err(dev, "No memory for msm_mmc_pad_drv_data\n");
  1212. ret = -ENOMEM;
  1213. goto out;
  1214. }
  1215. drv_data->size = 3; /* array size for clk, cmd, data */
  1216. /* Allocate on, off configs for clk, cmd, data */
  1217. drv = devm_kzalloc(dev, 2 * drv_data->size *\
  1218. sizeof(struct sdhci_msm_pad_drv), GFP_KERNEL);
  1219. if (!drv) {
  1220. dev_err(dev, "No memory msm_mmc_pad_drv\n");
  1221. ret = -ENOMEM;
  1222. goto out;
  1223. }
  1224. drv_data->on = drv;
  1225. drv_data->off = drv + drv_data->size;
  1226. ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-on",
  1227. &tmp, &len, drv_data->size);
  1228. if (ret)
  1229. goto out;
  1230. for (i = 0; i < len; i++) {
  1231. drv_data->on[i].no = base + i;
  1232. drv_data->on[i].val = tmp[i];
  1233. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  1234. i, drv_data->on[i].val);
  1235. }
  1236. ret = sdhci_msm_dt_get_array(dev, "qcom,pad-drv-off",
  1237. &tmp, &len, drv_data->size);
  1238. if (ret)
  1239. goto out;
  1240. for (i = 0; i < len; i++) {
  1241. drv_data->off[i].no = base + i;
  1242. drv_data->off[i].val = tmp[i];
  1243. dev_dbg(dev, "%s: val[%d]=0x%x\n", __func__,
  1244. i, drv_data->off[i].val);
  1245. }
  1246. *pad_drv_data = drv_data;
  1247. out:
  1248. return ret;
  1249. }
  1250. #define GPIO_NAME_MAX_LEN 32
  1251. static int sdhci_msm_dt_parse_gpio_info(struct device *dev,
  1252. struct sdhci_msm_pltfm_data *pdata)
  1253. {
  1254. int ret = 0, id = 0, cnt, i;
  1255. struct sdhci_msm_pin_data *pin_data;
  1256. struct device_node *np = dev->of_node;
  1257. pin_data = devm_kzalloc(dev, sizeof(*pin_data), GFP_KERNEL);
  1258. if (!pin_data) {
  1259. dev_err(dev, "No memory for pin_data\n");
  1260. ret = -ENOMEM;
  1261. goto out;
  1262. }
  1263. cnt = of_gpio_count(np);
  1264. if (cnt > 0) {
  1265. pin_data->is_gpio = true;
  1266. pin_data->gpio_data = devm_kzalloc(dev,
  1267. sizeof(struct sdhci_msm_gpio_data), GFP_KERNEL);
  1268. if (!pin_data->gpio_data) {
  1269. dev_err(dev, "No memory for gpio_data\n");
  1270. ret = -ENOMEM;
  1271. goto out;
  1272. }
  1273. pin_data->gpio_data->size = cnt;
  1274. pin_data->gpio_data->gpio = devm_kzalloc(dev, cnt *
  1275. sizeof(struct sdhci_msm_gpio), GFP_KERNEL);
  1276. if (!pin_data->gpio_data->gpio) {
  1277. dev_err(dev, "No memory for gpio\n");
  1278. ret = -ENOMEM;
  1279. goto out;
  1280. }
  1281. for (i = 0; i < cnt; i++) {
  1282. const char *name = NULL;
  1283. char result[GPIO_NAME_MAX_LEN];
  1284. pin_data->gpio_data->gpio[i].no = of_get_gpio(np, i);
  1285. of_property_read_string_index(np,
  1286. "qcom,gpio-names", i, &name);
  1287. snprintf(result, GPIO_NAME_MAX_LEN, "%s-%s",
  1288. dev_name(dev), name ? name : "?");
  1289. pin_data->gpio_data->gpio[i].name = result;
  1290. dev_dbg(dev, "%s: gpio[%s] = %d\n", __func__,
  1291. pin_data->gpio_data->gpio[i].name,
  1292. pin_data->gpio_data->gpio[i].no);
  1293. }
  1294. } else {
  1295. pin_data->pad_data =
  1296. devm_kzalloc(dev,
  1297. sizeof(struct sdhci_msm_pad_data),
  1298. GFP_KERNEL);
  1299. if (!pin_data->pad_data) {
  1300. dev_err(dev,
  1301. "No memory for pin_data->pad_data\n");
  1302. ret = -ENOMEM;
  1303. goto out;
  1304. }
  1305. ret = of_alias_get_id(np, "sdhc");
  1306. if (ret < 0) {
  1307. dev_err(dev, "Failed to get slot index %d\n", ret);
  1308. goto out;
  1309. }
  1310. id = ret;
  1311. ret = sdhci_msm_dt_get_pad_pull_info(
  1312. dev, id, &pin_data->pad_data->pull);
  1313. if (ret)
  1314. goto out;
  1315. ret = sdhci_msm_dt_get_pad_drv_info(
  1316. dev, id, &pin_data->pad_data->drv);
  1317. if (ret)
  1318. goto out;
  1319. }
  1320. pdata->pin_data = pin_data;
  1321. out:
  1322. if (ret)
  1323. dev_err(dev, "%s failed with err %d\n", __func__, ret);
  1324. return ret;
  1325. }
  1326. /* Parse platform data */
  1327. static struct sdhci_msm_pltfm_data *sdhci_msm_populate_pdata(struct device *dev)
  1328. {
  1329. struct sdhci_msm_pltfm_data *pdata = NULL;
  1330. struct device_node *np = dev->of_node;
  1331. u32 bus_width = 0;
  1332. u32 cpu_dma_latency;
  1333. int len, i, mpm_int;
  1334. int clk_table_len;
  1335. u32 *clk_table = NULL;
  1336. enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
  1337. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  1338. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  1339. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  1340. int vendor_type = 0;
  1341. #endif
  1342. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1343. if (!pdata) {
  1344. dev_err(dev, "failed to allocate memory for platform data\n");
  1345. goto out;
  1346. }
  1347. pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
  1348. if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
  1349. pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  1350. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  1351. pdata->ls_gpio = of_get_named_gpio_flags(np, "ls-gpios", 0, 0);
  1352. #endif
  1353. of_property_read_u32(np, "qcom,bus-width", &bus_width);
  1354. if (bus_width == 8)
  1355. pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
  1356. else if (bus_width == 4)
  1357. pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
  1358. else {
  1359. dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
  1360. pdata->mmc_bus_width = 0;
  1361. }
  1362. if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
  1363. &cpu_dma_latency))
  1364. pdata->cpu_dma_latency_us = cpu_dma_latency;
  1365. if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
  1366. &clk_table, &clk_table_len, 0)) {
  1367. dev_err(dev, "failed parsing supported clock rates\n");
  1368. goto out;
  1369. }
  1370. if (!clk_table || !clk_table_len) {
  1371. dev_err(dev, "Invalid clock table\n");
  1372. goto out;
  1373. }
  1374. pdata->sup_clk_table = clk_table;
  1375. pdata->sup_clk_cnt = clk_table_len;
  1376. pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
  1377. sdhci_msm_slot_reg_data),
  1378. GFP_KERNEL);
  1379. if (!pdata->vreg_data) {
  1380. dev_err(dev, "failed to allocate memory for vreg data\n");
  1381. goto out;
  1382. }
  1383. if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
  1384. "vdd")) {
  1385. dev_err(dev, "failed parsing vdd data\n");
  1386. goto out;
  1387. }
  1388. if (sdhci_msm_dt_parse_vreg_info(dev,
  1389. &pdata->vreg_data->vdd_io_data,
  1390. "vdd-io")) {
  1391. dev_err(dev, "failed parsing vdd-io data\n");
  1392. goto out;
  1393. }
  1394. if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
  1395. dev_err(dev, "failed parsing gpio data\n");
  1396. goto out;
  1397. }
  1398. len = of_property_count_strings(np, "qcom,bus-speed-mode");
  1399. for (i = 0; i < len; i++) {
  1400. const char *name = NULL;
  1401. of_property_read_string_index(np,
  1402. "qcom,bus-speed-mode", i, &name);
  1403. if (!name)
  1404. continue;
  1405. if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
  1406. pdata->caps2 |= MMC_CAP2_HS400_1_8V;
  1407. else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
  1408. pdata->caps2 |= MMC_CAP2_HS400_1_2V;
  1409. else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
  1410. pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
  1411. else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
  1412. pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
  1413. else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
  1414. pdata->caps |= MMC_CAP_1_8V_DDR
  1415. | MMC_CAP_UHS_DDR50;
  1416. else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
  1417. pdata->caps |= MMC_CAP_1_2V_DDR
  1418. | MMC_CAP_UHS_DDR50;
  1419. }
  1420. if (of_get_property(np, "qcom,nonremovable", NULL))
  1421. pdata->nonremovable = true;
  1422. if (!of_property_read_u32(np, "qcom,dat1-mpm-int",
  1423. &mpm_int))
  1424. pdata->mpm_sdiowakeup_int = mpm_int;
  1425. else
  1426. pdata->mpm_sdiowakeup_int = -1;
  1427. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  1428. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  1429. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  1430. pr_err("%s: before parsing vendor_type\n", __FUNCTION__);
  1431. if (of_get_property(np, "status-cb", &vendor_type)) {
  1432. pdata->is_status_cb = true;
  1433. pr_err("%s: vendor_type=%d \n", __FUNCTION__, vendor_type);
  1434. }
  1435. #endif
  1436. return pdata;
  1437. out:
  1438. return NULL;
  1439. }
  1440. /* Returns required bandwidth in Bytes per Sec */
  1441. static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
  1442. struct mmc_ios *ios)
  1443. {
  1444. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1445. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1446. unsigned int bw;
  1447. bw = msm_host->clk_rate;
  1448. /*
  1449. * For DDR mode, SDCC controller clock will be at
  1450. * the double rate than the actual clock that goes to card.
  1451. */
  1452. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1453. bw /= 2;
  1454. else if (ios->bus_width == MMC_BUS_WIDTH_1)
  1455. bw /= 8;
  1456. return bw;
  1457. }
  1458. static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
  1459. unsigned int bw)
  1460. {
  1461. unsigned int *table = host->pdata->voting_data->bw_vecs;
  1462. unsigned int size = host->pdata->voting_data->bw_vecs_size;
  1463. int i;
  1464. if (host->msm_bus_vote.is_max_bw_needed && bw)
  1465. return host->msm_bus_vote.max_bw_vote;
  1466. for (i = 0; i < size; i++) {
  1467. if (bw <= table[i])
  1468. break;
  1469. }
  1470. if (i && (i == size))
  1471. i--;
  1472. return i;
  1473. }
  1474. /*
  1475. * This function must be called with host lock acquired.
  1476. * Caller of this function should also ensure that msm bus client
  1477. * handle is not null.
  1478. */
  1479. static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
  1480. int vote,
  1481. unsigned long flags)
  1482. {
  1483. struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
  1484. int rc = 0;
  1485. if (vote != msm_host->msm_bus_vote.curr_vote) {
  1486. spin_unlock_irqrestore(&host->lock, flags);
  1487. rc = msm_bus_scale_client_update_request(
  1488. msm_host->msm_bus_vote.client_handle, vote);
  1489. spin_lock_irqsave(&host->lock, flags);
  1490. if (rc) {
  1491. pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
  1492. mmc_hostname(host->mmc),
  1493. msm_host->msm_bus_vote.client_handle, vote, rc);
  1494. goto out;
  1495. }
  1496. msm_host->msm_bus_vote.curr_vote = vote;
  1497. }
  1498. out:
  1499. return rc;
  1500. }
  1501. /*
  1502. * Internal work. Work to set 0 bandwidth for msm bus.
  1503. */
  1504. static void sdhci_msm_bus_work(struct work_struct *work)
  1505. {
  1506. struct sdhci_msm_host *msm_host;
  1507. struct sdhci_host *host;
  1508. unsigned long flags;
  1509. msm_host = container_of(work, struct sdhci_msm_host,
  1510. msm_bus_vote.vote_work.work);
  1511. host = platform_get_drvdata(msm_host->pdev);
  1512. if (!msm_host->msm_bus_vote.client_handle)
  1513. return;
  1514. spin_lock_irqsave(&host->lock, flags);
  1515. /* don't vote for 0 bandwidth if any request is in progress */
  1516. if (!host->mrq) {
  1517. sdhci_msm_bus_set_vote(msm_host,
  1518. msm_host->msm_bus_vote.min_bw_vote, flags);
  1519. } else
  1520. pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
  1521. mmc_hostname(host->mmc), __func__);
  1522. spin_unlock_irqrestore(&host->lock, flags);
  1523. }
  1524. /*
  1525. * This function cancels any scheduled delayed work and sets the bus
  1526. * vote based on bw (bandwidth) argument.
  1527. */
  1528. static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
  1529. unsigned int bw)
  1530. {
  1531. int vote;
  1532. unsigned long flags;
  1533. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1534. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1535. cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
  1536. spin_lock_irqsave(&host->lock, flags);
  1537. vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
  1538. sdhci_msm_bus_set_vote(msm_host, vote, flags);
  1539. spin_unlock_irqrestore(&host->lock, flags);
  1540. }
  1541. #define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
  1542. /* This function queues a work which will set the bandwidth requiement to 0 */
  1543. static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
  1544. {
  1545. unsigned long flags;
  1546. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1547. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1548. spin_lock_irqsave(&host->lock, flags);
  1549. if (msm_host->msm_bus_vote.min_bw_vote !=
  1550. msm_host->msm_bus_vote.curr_vote)
  1551. queue_delayed_work(system_nrt_wq,
  1552. &msm_host->msm_bus_vote.vote_work,
  1553. msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
  1554. spin_unlock_irqrestore(&host->lock, flags);
  1555. }
  1556. static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
  1557. struct platform_device *pdev)
  1558. {
  1559. int rc = 0;
  1560. struct msm_bus_scale_pdata *bus_pdata;
  1561. struct sdhci_msm_bus_voting_data *data;
  1562. struct device *dev = &pdev->dev;
  1563. data = devm_kzalloc(dev,
  1564. sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
  1565. if (!data) {
  1566. dev_err(&pdev->dev,
  1567. "%s: failed to allocate memory\n", __func__);
  1568. rc = -ENOMEM;
  1569. goto out;
  1570. }
  1571. data->bus_pdata = msm_bus_cl_get_pdata(pdev);
  1572. if (data->bus_pdata) {
  1573. rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
  1574. &data->bw_vecs, &data->bw_vecs_size, 0);
  1575. if (rc) {
  1576. dev_err(&pdev->dev,
  1577. "%s: Failed to get bus-bw-vectors-bps\n",
  1578. __func__);
  1579. goto out;
  1580. }
  1581. host->pdata->voting_data = data;
  1582. }
  1583. if (host->pdata->voting_data &&
  1584. host->pdata->voting_data->bus_pdata &&
  1585. host->pdata->voting_data->bw_vecs &&
  1586. host->pdata->voting_data->bw_vecs_size) {
  1587. bus_pdata = host->pdata->voting_data->bus_pdata;
  1588. host->msm_bus_vote.client_handle =
  1589. msm_bus_scale_register_client(bus_pdata);
  1590. if (!host->msm_bus_vote.client_handle) {
  1591. dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
  1592. rc = -EFAULT;
  1593. goto out;
  1594. }
  1595. /* cache the vote index for minimum and maximum bandwidth */
  1596. host->msm_bus_vote.min_bw_vote =
  1597. sdhci_msm_bus_get_vote_for_bw(host, 0);
  1598. host->msm_bus_vote.max_bw_vote =
  1599. sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
  1600. } else {
  1601. devm_kfree(dev, data);
  1602. }
  1603. out:
  1604. return rc;
  1605. }
  1606. static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
  1607. {
  1608. if (host->msm_bus_vote.client_handle)
  1609. msm_bus_scale_unregister_client(
  1610. host->msm_bus_vote.client_handle);
  1611. }
  1612. static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
  1613. {
  1614. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1615. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1616. struct mmc_ios *ios = &host->mmc->ios;
  1617. unsigned int bw;
  1618. if (!msm_host->msm_bus_vote.client_handle)
  1619. return;
  1620. bw = sdhci_get_bw_required(host, ios);
  1621. if (enable) {
  1622. sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
  1623. } else {
  1624. /*
  1625. * If clock gating is enabled, then remove the vote
  1626. * immediately because clocks will be disabled only
  1627. * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
  1628. * additional delay is required to remove the bus vote.
  1629. */
  1630. if (host->mmc->clkgate_delay)
  1631. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  1632. else
  1633. sdhci_msm_bus_queue_work(host);
  1634. }
  1635. }
  1636. /* Regulator utility functions */
  1637. static int sdhci_msm_vreg_init_reg(struct device *dev,
  1638. struct sdhci_msm_reg_data *vreg)
  1639. {
  1640. int ret = 0;
  1641. /* check if regulator is already initialized? */
  1642. if (vreg->reg)
  1643. goto out;
  1644. /* Get the regulator handle */
  1645. vreg->reg = devm_regulator_get(dev, vreg->name);
  1646. if (IS_ERR(vreg->reg)) {
  1647. ret = PTR_ERR(vreg->reg);
  1648. pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
  1649. __func__, vreg->name, ret);
  1650. goto out;
  1651. }
  1652. if (regulator_count_voltages(vreg->reg) > 0) {
  1653. vreg->set_voltage_sup = true;
  1654. /* sanity check */
  1655. if (!vreg->high_vol_level || !vreg->hpm_uA) {
  1656. pr_err("%s: %s invalid constraints specified\n",
  1657. __func__, vreg->name);
  1658. ret = -EINVAL;
  1659. }
  1660. }
  1661. out:
  1662. return ret;
  1663. }
  1664. static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
  1665. {
  1666. if (vreg->reg)
  1667. devm_regulator_put(vreg->reg);
  1668. }
  1669. static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
  1670. *vreg, int uA_load)
  1671. {
  1672. int ret = 0;
  1673. /*
  1674. * regulators that do not support regulator_set_voltage also
  1675. * do not support regulator_set_optimum_mode
  1676. */
  1677. if (vreg->set_voltage_sup) {
  1678. ret = regulator_set_optimum_mode(vreg->reg, uA_load);
  1679. if (ret < 0)
  1680. pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
  1681. __func__, vreg->name, uA_load, ret);
  1682. else
  1683. /*
  1684. * regulator_set_optimum_mode() can return non zero
  1685. * value even for success case.
  1686. */
  1687. ret = 0;
  1688. }
  1689. return ret;
  1690. }
  1691. static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
  1692. int min_uV, int max_uV)
  1693. {
  1694. int ret = 0;
  1695. if (vreg->set_voltage_sup) {
  1696. ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
  1697. if (ret) {
  1698. pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
  1699. __func__, vreg->name, min_uV, max_uV, ret);
  1700. }
  1701. }
  1702. return ret;
  1703. }
  1704. static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
  1705. {
  1706. int ret = 0;
  1707. if (!vreg->reg) {
  1708. pr_err("%s: %s Cannot find Regulator\n", __func__, vreg->name);
  1709. return ret;
  1710. }
  1711. #if defined(CONFIG_ARCH_MSM8974)\
  1712. || defined(CONFIG_MACH_FRESCOLTESKT)||defined(CONFIG_MACH_FRESCOLTEKTT)||defined(CONFIG_MACH_FRESCOLTELGT)
  1713. if (vreg->rpm_reg) {
  1714. ret = rpm_regulator_set_mode(vreg->rpm_reg,
  1715. RPM_REGULATOR_MODE_HPM);
  1716. if (ret) {
  1717. pr_err("Failed setting rpm-reg(%s) to HPM: %d",
  1718. vreg->name, ret);
  1719. return ret;
  1720. }
  1721. return ret;
  1722. }
  1723. #endif
  1724. /* Put regulator in HPM (high power mode) */
  1725. ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
  1726. if (ret < 0)
  1727. return ret;
  1728. if (!vreg->is_enabled) {
  1729. /* Set voltage level */
  1730. ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
  1731. vreg->high_vol_level);
  1732. if (ret)
  1733. return ret;
  1734. }
  1735. ret = regulator_enable(vreg->reg);
  1736. if (ret) {
  1737. pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
  1738. __func__, vreg->name, ret);
  1739. return ret;
  1740. }
  1741. vreg->is_enabled = true;
  1742. return ret;
  1743. }
  1744. static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
  1745. {
  1746. int ret = 0;
  1747. if (!vreg->reg) {
  1748. pr_err("%s: %s Cannot find Regulator\n", __func__, vreg->name);
  1749. return ret;
  1750. }
  1751. /* Never disable regulator marked as always_on */
  1752. if (vreg->is_enabled && !vreg->is_always_on) {
  1753. ret = regulator_disable(vreg->reg);
  1754. if (ret) {
  1755. pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
  1756. __func__, vreg->name, ret);
  1757. goto out;
  1758. }
  1759. vreg->is_enabled = false;
  1760. ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
  1761. if (ret < 0)
  1762. goto out;
  1763. /* Set min. voltage level to 0 */
  1764. ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
  1765. if (ret)
  1766. goto out;
  1767. } else if (vreg->is_enabled && vreg->is_always_on) {
  1768. if (vreg->lpm_sup) {
  1769. /* Put always_on regulator in LPM (low power mode) */
  1770. ret = sdhci_msm_vreg_set_optimum_mode(vreg,
  1771. vreg->lpm_uA);
  1772. if (ret < 0)
  1773. goto out;
  1774. }
  1775. }
  1776. out:
  1777. return ret;
  1778. }
  1779. static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
  1780. bool enable, bool is_init)
  1781. {
  1782. int ret = 0, i;
  1783. struct sdhci_msm_slot_reg_data *curr_slot;
  1784. struct sdhci_msm_reg_data *vreg_table[2];
  1785. curr_slot = pdata->vreg_data;
  1786. if (!curr_slot) {
  1787. pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
  1788. __func__);
  1789. goto out;
  1790. }
  1791. #if defined(CONFIG_SEC_K_PROJECT)
  1792. /*
  1793. * In SD Card Case using FPGA,
  1794. * Turn on : vdd_io on -> vdd on
  1795. * Turn off : vdd off -> delay 5msec -> vdd_io off
  1796. * vdd : L21, vdd_io : L13
  1797. */
  1798. if (pdata->status_gpio && enable)
  1799. {
  1800. vreg_table[0] = curr_slot->vdd_io_data;
  1801. vreg_table[1] = curr_slot->vdd_data;
  1802. } else {
  1803. vreg_table[0] = curr_slot->vdd_data;
  1804. vreg_table[1] = curr_slot->vdd_io_data;
  1805. }
  1806. #else
  1807. vreg_table[0] = curr_slot->vdd_data;
  1808. vreg_table[1] = curr_slot->vdd_io_data;
  1809. #endif
  1810. for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
  1811. if (vreg_table[i]) {
  1812. if (enable)
  1813. ret = sdhci_msm_vreg_enable(vreg_table[i]);
  1814. #if defined(CONFIG_SEC_K_PROJECT)
  1815. else {
  1816. ret = sdhci_msm_vreg_disable(vreg_table[i]);
  1817. if (pdata->status_gpio && i == 0)
  1818. mdelay(5);
  1819. }
  1820. #else
  1821. else
  1822. ret = sdhci_msm_vreg_disable(vreg_table[i]);
  1823. #endif
  1824. if (ret)
  1825. goto out;
  1826. }
  1827. }
  1828. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  1829. if(pdata->ls_gpio > 0 && gpio_is_valid(pdata->ls_gpio)) {
  1830. if(enable) {
  1831. gpio_set_value(pdata->ls_gpio, 1);
  1832. udelay(500);
  1833. } else {
  1834. gpio_set_value(pdata->ls_gpio, 0);
  1835. }
  1836. }
  1837. #endif
  1838. out:
  1839. return ret;
  1840. }
  1841. /*
  1842. * Reset vreg by ensuring it is off during probe. A call
  1843. * to enable vreg is needed to balance disable vreg
  1844. */
  1845. static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
  1846. {
  1847. int ret;
  1848. ret = sdhci_msm_setup_vreg(pdata, 1, true);
  1849. if (ret)
  1850. return ret;
  1851. ret = sdhci_msm_setup_vreg(pdata, 0, true);
  1852. return ret;
  1853. }
  1854. /* This init function should be called only once for each SDHC slot */
  1855. static int sdhci_msm_vreg_init(struct device *dev,
  1856. struct sdhci_msm_pltfm_data *pdata,
  1857. bool is_init)
  1858. {
  1859. int ret = 0;
  1860. struct sdhci_msm_slot_reg_data *curr_slot;
  1861. struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
  1862. curr_slot = pdata->vreg_data;
  1863. if (!curr_slot)
  1864. goto out;
  1865. curr_vdd_reg = curr_slot->vdd_data;
  1866. curr_vdd_io_reg = curr_slot->vdd_io_data;
  1867. if (!is_init)
  1868. /* Deregister all regulators from regulator framework */
  1869. goto vdd_io_reg_deinit;
  1870. /*
  1871. * Get the regulator handle from voltage regulator framework
  1872. * and then try to set the voltage level for the regulator
  1873. */
  1874. if (curr_vdd_reg) {
  1875. ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
  1876. if (ret)
  1877. goto out;
  1878. }
  1879. if (curr_vdd_io_reg) {
  1880. #if defined(CONFIG_ARCH_MSM8974)\
  1881. || defined(CONFIG_MACH_FRESCOLTESKT)||defined(CONFIG_MACH_FRESCOLTEKTT)||defined(CONFIG_MACH_FRESCOLTELGT)
  1882. if (pdata->nonremovable) {
  1883. /* Only for emmc */
  1884. curr_vdd_io_reg->rpm_reg = rpm_regulator_get(
  1885. dev,
  1886. curr_vdd_io_reg->name);
  1887. if (IS_ERR_OR_NULL(curr_vdd_io_reg->rpm_reg)) {
  1888. dev_err(dev, "rpm-reg get: failed: %ld\n",
  1889. PTR_ERR(curr_vdd_io_reg->rpm_reg));
  1890. curr_vdd_io_reg->rpm_reg = NULL;
  1891. } else {
  1892. dev_dbg(dev, "rpm-reg get: success\n");
  1893. }
  1894. } else {
  1895. curr_vdd_io_reg->rpm_reg = NULL;
  1896. ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
  1897. if (ret)
  1898. goto vdd_reg_deinit;
  1899. }
  1900. #else
  1901. ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
  1902. if (ret)
  1903. goto vdd_reg_deinit;
  1904. #endif
  1905. }
  1906. ret = sdhci_msm_vreg_reset(pdata);
  1907. if (ret)
  1908. dev_err(dev, "vreg reset failed (%d)\n", ret);
  1909. goto out;
  1910. vdd_io_reg_deinit:
  1911. if (curr_vdd_io_reg)
  1912. sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
  1913. vdd_reg_deinit:
  1914. if (curr_vdd_reg)
  1915. sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
  1916. out:
  1917. return ret;
  1918. }
  1919. static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
  1920. enum vdd_io_level level,
  1921. unsigned int voltage_level)
  1922. {
  1923. int ret = 0;
  1924. int set_level;
  1925. struct sdhci_msm_reg_data *vdd_io_reg;
  1926. if (!pdata->vreg_data)
  1927. return ret;
  1928. vdd_io_reg = pdata->vreg_data->vdd_io_data;
  1929. if (vdd_io_reg && vdd_io_reg->is_enabled) {
  1930. switch (level) {
  1931. case VDD_IO_LOW:
  1932. set_level = vdd_io_reg->low_vol_level;
  1933. break;
  1934. case VDD_IO_HIGH:
  1935. set_level = vdd_io_reg->high_vol_level;
  1936. break;
  1937. case VDD_IO_SET_LEVEL:
  1938. set_level = voltage_level;
  1939. break;
  1940. default:
  1941. pr_err("%s: invalid argument level = %d",
  1942. __func__, level);
  1943. ret = -EINVAL;
  1944. return ret;
  1945. }
  1946. ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
  1947. set_level);
  1948. }
  1949. return ret;
  1950. }
  1951. /*
  1952. * Acquire spin-lock host->lock before calling this function
  1953. */
  1954. static void sdhci_msm_cfg_sdiowakeup_gpio_irq(struct sdhci_host *host,
  1955. bool enable)
  1956. {
  1957. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1958. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1959. if (enable && !msm_host->is_sdiowakeup_enabled)
  1960. enable_irq(msm_host->pdata->sdiowakeup_irq);
  1961. else if (!enable && msm_host->is_sdiowakeup_enabled)
  1962. disable_irq_nosync(msm_host->pdata->sdiowakeup_irq);
  1963. else
  1964. dev_warn(&msm_host->pdev->dev, "%s: wakeup to config: %d curr: %d\n",
  1965. __func__, enable, msm_host->is_sdiowakeup_enabled);
  1966. msm_host->is_sdiowakeup_enabled = enable;
  1967. }
  1968. static irqreturn_t sdhci_msm_sdiowakeup_irq(int irq, void *data)
  1969. {
  1970. struct sdhci_host *host = (struct sdhci_host *)data;
  1971. unsigned long flags;
  1972. pr_debug("%s: irq (%d) received\n", __func__, irq);
  1973. spin_lock_irqsave(&host->lock, flags);
  1974. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
  1975. spin_unlock_irqrestore(&host->lock, flags);
  1976. return IRQ_HANDLED;
  1977. }
  1978. static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
  1979. {
  1980. struct sdhci_host *host = (struct sdhci_host *)data;
  1981. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1982. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1983. u8 irq_status = 0;
  1984. u8 irq_ack = 0;
  1985. int ret = 0;
  1986. int pwr_state = 0, io_level = 0;
  1987. unsigned long flags;
  1988. irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
  1989. pr_debug("%s: Received IRQ(%d), status=0x%x\n",
  1990. mmc_hostname(msm_host->mmc), irq, irq_status);
  1991. /* Clear the interrupt */
  1992. writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
  1993. /*
  1994. * SDHC has core_mem and hc_mem device memory and these memory
  1995. * addresses do not fall within 1KB region. Hence, any update to
  1996. * core_mem address space would require an mb() to ensure this gets
  1997. * completed before its next update to registers within hc_mem.
  1998. */
  1999. mb();
  2000. /* Handle BUS ON/OFF*/
  2001. if (irq_status & CORE_PWRCTL_BUS_ON) {
  2002. ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
  2003. if (!ret) {
  2004. ret = sdhci_msm_setup_pins(msm_host->pdata, true);
  2005. ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
  2006. VDD_IO_HIGH, 0);
  2007. }
  2008. if (ret)
  2009. irq_ack |= CORE_PWRCTL_BUS_FAIL;
  2010. else
  2011. irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
  2012. pwr_state = REQ_BUS_ON;
  2013. io_level = REQ_IO_HIGH;
  2014. }
  2015. if (irq_status & CORE_PWRCTL_BUS_OFF) {
  2016. ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
  2017. if (!ret) {
  2018. ret = sdhci_msm_setup_pins(msm_host->pdata, false);
  2019. ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
  2020. VDD_IO_LOW, 0);
  2021. }
  2022. if (ret)
  2023. irq_ack |= CORE_PWRCTL_BUS_FAIL;
  2024. else
  2025. irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
  2026. pwr_state = REQ_BUS_OFF;
  2027. io_level = REQ_IO_LOW;
  2028. }
  2029. /* Handle IO LOW/HIGH */
  2030. if (irq_status & CORE_PWRCTL_IO_LOW) {
  2031. /* Switch voltage Low */
  2032. ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
  2033. if (ret)
  2034. irq_ack |= CORE_PWRCTL_IO_FAIL;
  2035. else
  2036. irq_ack |= CORE_PWRCTL_IO_SUCCESS;
  2037. io_level = REQ_IO_LOW;
  2038. }
  2039. if (irq_status & CORE_PWRCTL_IO_HIGH) {
  2040. /* Switch voltage High */
  2041. ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
  2042. if (ret)
  2043. irq_ack |= CORE_PWRCTL_IO_FAIL;
  2044. else
  2045. irq_ack |= CORE_PWRCTL_IO_SUCCESS;
  2046. io_level = REQ_IO_HIGH;
  2047. }
  2048. /* ACK status to the core */
  2049. writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
  2050. /*
  2051. * SDHC has core_mem and hc_mem device memory and these memory
  2052. * addresses do not fall within 1KB region. Hence, any update to
  2053. * core_mem address space would require an mb() to ensure this gets
  2054. * completed before its next update to registers within hc_mem.
  2055. */
  2056. mb();
  2057. if (io_level & REQ_IO_HIGH)
  2058. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
  2059. ~CORE_IO_PAD_PWR_SWITCH),
  2060. host->ioaddr + CORE_VENDOR_SPEC);
  2061. else if (io_level & REQ_IO_LOW)
  2062. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
  2063. CORE_IO_PAD_PWR_SWITCH),
  2064. host->ioaddr + CORE_VENDOR_SPEC);
  2065. mb();
  2066. pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
  2067. mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
  2068. spin_lock_irqsave(&host->lock, flags);
  2069. if (pwr_state)
  2070. msm_host->curr_pwr_state = pwr_state;
  2071. if (io_level)
  2072. msm_host->curr_io_level = io_level;
  2073. complete(&msm_host->pwr_irq_completion);
  2074. spin_unlock_irqrestore(&host->lock, flags);
  2075. return IRQ_HANDLED;
  2076. }
  2077. /* This function returns the max. current supported by VDD rail in mA */
  2078. static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
  2079. *host)
  2080. {
  2081. struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
  2082. if (!curr_slot)
  2083. return 0;
  2084. if (curr_slot->vdd_data)
  2085. return curr_slot->vdd_data->hpm_uA / 1000;
  2086. else
  2087. return 0;
  2088. }
  2089. static ssize_t
  2090. show_polling(struct device *dev, struct device_attribute *attr, char *buf)
  2091. {
  2092. struct sdhci_host *host = dev_get_drvdata(dev);
  2093. int poll;
  2094. unsigned long flags;
  2095. spin_lock_irqsave(&host->lock, flags);
  2096. poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
  2097. spin_unlock_irqrestore(&host->lock, flags);
  2098. return snprintf(buf, PAGE_SIZE, "%d\n", poll);
  2099. }
  2100. static ssize_t
  2101. store_polling(struct device *dev, struct device_attribute *attr,
  2102. const char *buf, size_t count)
  2103. {
  2104. struct sdhci_host *host = dev_get_drvdata(dev);
  2105. int value;
  2106. unsigned long flags;
  2107. if (!kstrtou32(buf, 0, &value)) {
  2108. spin_lock_irqsave(&host->lock, flags);
  2109. if (value) {
  2110. host->mmc->caps |= MMC_CAP_NEEDS_POLL;
  2111. mmc_detect_change(host->mmc, 0);
  2112. } else {
  2113. host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
  2114. }
  2115. spin_unlock_irqrestore(&host->lock, flags);
  2116. }
  2117. return count;
  2118. }
  2119. static ssize_t
  2120. show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
  2121. char *buf)
  2122. {
  2123. struct sdhci_host *host = dev_get_drvdata(dev);
  2124. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2125. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2126. return snprintf(buf, PAGE_SIZE, "%u\n",
  2127. msm_host->msm_bus_vote.is_max_bw_needed);
  2128. }
  2129. static ssize_t
  2130. store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
  2131. const char *buf, size_t count)
  2132. {
  2133. struct sdhci_host *host = dev_get_drvdata(dev);
  2134. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2135. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2136. uint32_t value;
  2137. unsigned long flags;
  2138. if (!kstrtou32(buf, 0, &value)) {
  2139. spin_lock_irqsave(&host->lock, flags);
  2140. msm_host->msm_bus_vote.is_max_bw_needed = !!value;
  2141. spin_unlock_irqrestore(&host->lock, flags);
  2142. }
  2143. return count;
  2144. }
  2145. static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
  2146. {
  2147. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2148. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2149. unsigned long flags;
  2150. bool done = false;
  2151. spin_lock_irqsave(&host->lock, flags);
  2152. pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
  2153. mmc_hostname(host->mmc), __func__, req_type,
  2154. msm_host->curr_pwr_state, msm_host->curr_io_level);
  2155. if ((req_type & msm_host->curr_pwr_state) ||
  2156. (req_type & msm_host->curr_io_level))
  2157. done = true;
  2158. spin_unlock_irqrestore(&host->lock, flags);
  2159. /*
  2160. * 8974pro after HW_GPIO_06 uses SDR50 Mode
  2161. * KLTE uses SDR50 on SDC3 port.
  2162. * Handle I/O voltage switch here if this request is for SDC3.
  2163. * SDC3 Don't use PWR_IRQ.
  2164. */
  2165. #if defined(CONFIG_SEC_K_PROJECT)
  2166. if (strcmp(host->hw_name, "msm_sdcc.3") == 0 && !done && system_rev >= 6) {
  2167. if (req_type == REQ_IO_HIGH) {
  2168. /* Switch voltage High */
  2169. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0)) {
  2170. pr_err("%s: %s: Voltage Switch to High is Fail\n", mmc_hostname(host->mmc), __func__);
  2171. return;
  2172. }
  2173. msm_host->curr_io_level = REQ_IO_HIGH;
  2174. done = true;
  2175. } else if (req_type == REQ_IO_LOW) {
  2176. /* Switch voltage Low */
  2177. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0)) {
  2178. pr_err("%s: %s: Voltage Switch to Low is Fail\n", mmc_hostname(host->mmc), __func__);
  2179. return;
  2180. }
  2181. msm_host->curr_io_level = REQ_IO_LOW;
  2182. done = true;
  2183. }
  2184. }
  2185. #elif defined(CONFIG_SEC_PATEK_PROJECT)
  2186. if (strcmp(host->hw_name, "msm_sdcc.3") == 0 && !done) {
  2187. if (req_type == REQ_IO_HIGH) {
  2188. /* Switch voltage High */
  2189. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0)) {
  2190. pr_err("%s: %s: Voltage Switch to High is Fail\n", mmc_hostname(host->mmc), __func__);
  2191. return;
  2192. }
  2193. msm_host->curr_io_level = REQ_IO_HIGH;
  2194. done = true;
  2195. } else if (req_type == REQ_IO_LOW) {
  2196. /* Switch voltage Low */
  2197. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0)) {
  2198. pr_err("%s: %s: Voltage Switch to Low is Fail\n", mmc_hostname(host->mmc), __func__);
  2199. return;
  2200. }
  2201. msm_host->curr_io_level = REQ_IO_LOW;
  2202. done = true;
  2203. }
  2204. }
  2205. #endif
  2206. /*
  2207. * This is needed here to hanlde a case where IRQ gets
  2208. * triggered even before this function is called so that
  2209. * x->done counter of completion gets reset. Otherwise,
  2210. * next call to wait_for_completion returns immediately
  2211. * without actually waiting for the IRQ to be handled.
  2212. */
  2213. if (done)
  2214. init_completion(&msm_host->pwr_irq_completion);
  2215. else
  2216. wait_for_completion(&msm_host->pwr_irq_completion);
  2217. pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
  2218. __func__, req_type);
  2219. }
  2220. static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
  2221. {
  2222. if (enable)
  2223. writel_relaxed((readl_relaxed(host->ioaddr +
  2224. CORE_DLL_CONFIG) | CORE_CDR_EN),
  2225. host->ioaddr + CORE_DLL_CONFIG);
  2226. else
  2227. writel_relaxed((readl_relaxed(host->ioaddr +
  2228. CORE_DLL_CONFIG) & ~CORE_CDR_EN),
  2229. host->ioaddr + CORE_DLL_CONFIG);
  2230. }
  2231. static unsigned int sdhci_msm_max_segs(void)
  2232. {
  2233. return SDHCI_MSM_MAX_SEGMENTS / 16;
  2234. }
  2235. static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
  2236. {
  2237. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2238. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2239. return msm_host->pdata->sup_clk_table[0];
  2240. }
  2241. static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
  2242. {
  2243. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2244. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2245. int max_clk_index = msm_host->pdata->sup_clk_cnt;
  2246. return msm_host->pdata->sup_clk_table[max_clk_index - 1];
  2247. }
  2248. static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
  2249. u32 req_clk)
  2250. {
  2251. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2252. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2253. unsigned int sel_clk = -1;
  2254. unsigned char cnt;
  2255. if (req_clk < sdhci_msm_get_min_clock(host)) {
  2256. sel_clk = sdhci_msm_get_min_clock(host);
  2257. return sel_clk;
  2258. }
  2259. for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
  2260. if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
  2261. break;
  2262. } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
  2263. sel_clk = msm_host->pdata->sup_clk_table[cnt];
  2264. break;
  2265. } else {
  2266. sel_clk = msm_host->pdata->sup_clk_table[cnt];
  2267. }
  2268. }
  2269. return sel_clk;
  2270. }
  2271. static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
  2272. {
  2273. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2274. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2275. int rc = 0;
  2276. if (atomic_read(&msm_host->controller_clock))
  2277. return 0;
  2278. sdhci_msm_bus_voting(host, 1);
  2279. if (!IS_ERR(msm_host->pclk)) {
  2280. rc = clk_prepare_enable(msm_host->pclk);
  2281. if (rc) {
  2282. pr_err("%s: %s: failed to enable the pclk with error %d\n",
  2283. mmc_hostname(host->mmc), __func__, rc);
  2284. goto remove_vote;
  2285. }
  2286. }
  2287. rc = clk_prepare_enable(msm_host->clk);
  2288. if (rc) {
  2289. pr_err("%s: %s: failed to enable the host->clk with error %d\n",
  2290. mmc_hostname(host->mmc), __func__, rc);
  2291. goto disable_pclk;
  2292. }
  2293. atomic_set(&msm_host->controller_clock, 1);
  2294. pr_debug("%s: %s: enabled controller clock\n",
  2295. mmc_hostname(host->mmc), __func__);
  2296. goto out;
  2297. disable_pclk:
  2298. if (!IS_ERR(msm_host->pclk))
  2299. clk_disable_unprepare(msm_host->pclk);
  2300. remove_vote:
  2301. if (msm_host->msm_bus_vote.client_handle)
  2302. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  2303. out:
  2304. return rc;
  2305. }
  2306. static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
  2307. {
  2308. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2309. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2310. int rc = 0;
  2311. if (enable && !atomic_read(&msm_host->clks_on)) {
  2312. pr_debug("%s: request to enable clocks\n",
  2313. mmc_hostname(host->mmc));
  2314. /*
  2315. * The bus-width or the clock rate might have changed
  2316. * after controller clocks are enabled, update bus vote
  2317. * in such case.
  2318. */
  2319. if (atomic_read(&msm_host->controller_clock))
  2320. sdhci_msm_bus_voting(host, 1);
  2321. rc = sdhci_msm_enable_controller_clock(host);
  2322. if (rc)
  2323. goto remove_vote;
  2324. if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
  2325. rc = clk_prepare_enable(msm_host->bus_clk);
  2326. if (rc) {
  2327. pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
  2328. mmc_hostname(host->mmc), __func__, rc);
  2329. goto disable_controller_clk;
  2330. }
  2331. }
  2332. if (!IS_ERR(msm_host->ff_clk)) {
  2333. rc = clk_prepare_enable(msm_host->ff_clk);
  2334. if (rc) {
  2335. pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
  2336. mmc_hostname(host->mmc), __func__, rc);
  2337. goto disable_bus_clk;
  2338. }
  2339. }
  2340. if (!IS_ERR(msm_host->sleep_clk)) {
  2341. rc = clk_prepare_enable(msm_host->sleep_clk);
  2342. if (rc) {
  2343. pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
  2344. mmc_hostname(host->mmc), __func__, rc);
  2345. goto disable_ff_clk;
  2346. }
  2347. }
  2348. mb();
  2349. } else if (!enable && atomic_read(&msm_host->clks_on)) {
  2350. pr_debug("%s: request to disable clocks\n",
  2351. mmc_hostname(host->mmc));
  2352. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  2353. mb();
  2354. if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
  2355. clk_disable_unprepare(msm_host->sleep_clk);
  2356. if (!IS_ERR_OR_NULL(msm_host->ff_clk))
  2357. clk_disable_unprepare(msm_host->ff_clk);
  2358. clk_disable_unprepare(msm_host->clk);
  2359. if (!IS_ERR(msm_host->pclk))
  2360. clk_disable_unprepare(msm_host->pclk);
  2361. if (!IS_ERR_OR_NULL(msm_host->bus_clk))
  2362. clk_disable_unprepare(msm_host->bus_clk);
  2363. atomic_set(&msm_host->controller_clock, 0);
  2364. sdhci_msm_bus_voting(host, 0);
  2365. }
  2366. atomic_set(&msm_host->clks_on, enable);
  2367. goto out;
  2368. disable_ff_clk:
  2369. if (!IS_ERR_OR_NULL(msm_host->ff_clk))
  2370. clk_disable_unprepare(msm_host->ff_clk);
  2371. disable_bus_clk:
  2372. if (!IS_ERR_OR_NULL(msm_host->bus_clk))
  2373. clk_disable_unprepare(msm_host->bus_clk);
  2374. disable_controller_clk:
  2375. if (!IS_ERR_OR_NULL(msm_host->clk))
  2376. clk_disable_unprepare(msm_host->clk);
  2377. if (!IS_ERR_OR_NULL(msm_host->pclk))
  2378. clk_disable_unprepare(msm_host->pclk);
  2379. atomic_set(&msm_host->controller_clock, 0);
  2380. remove_vote:
  2381. if (msm_host->msm_bus_vote.client_handle)
  2382. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  2383. out:
  2384. return rc;
  2385. }
  2386. static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
  2387. {
  2388. int rc;
  2389. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2390. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2391. struct mmc_ios curr_ios = host->mmc->ios;
  2392. u32 sup_clock, ddr_clock;
  2393. bool curr_pwrsave;
  2394. if (!clock) {
  2395. /*
  2396. * disable pwrsave to ensure clock is not auto-gated until
  2397. * the rate is >400KHz (initialization complete).
  2398. */
  2399. writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
  2400. ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
  2401. sdhci_msm_prepare_clocks(host, false);
  2402. host->clock = clock;
  2403. return;
  2404. }
  2405. rc = sdhci_msm_prepare_clocks(host, true);
  2406. if (rc)
  2407. return;
  2408. if (msm_host->id == 1) { /* power save mode : eMMC only */
  2409. curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
  2410. CORE_CLK_PWRSAVE);
  2411. if ((msm_host->clk_rate > 400000) &&
  2412. !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
  2413. writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2414. | CORE_CLK_PWRSAVE,
  2415. host->ioaddr + CORE_VENDOR_SPEC);
  2416. /*
  2417. * Disable pwrsave for a newly added card if doesn't allow clock
  2418. * gating.
  2419. */
  2420. else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
  2421. writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2422. & ~CORE_CLK_PWRSAVE,
  2423. host->ioaddr + CORE_VENDOR_SPEC);
  2424. }
  2425. sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
  2426. if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
  2427. (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
  2428. /*
  2429. * The SDHC requires internal clock frequency to be double the
  2430. * actual clock that will be set for DDR mode. The controller
  2431. * uses the faster clock(100/400MHz) for some of its parts and
  2432. * send the actual required clock (50/200MHz) to the card.
  2433. */
  2434. ddr_clock = clock * 2;
  2435. sup_clock = sdhci_msm_get_sup_clk_rate(host,
  2436. ddr_clock);
  2437. }
  2438. /*
  2439. * In general all timing modes are controlled via UHS mode select in
  2440. * Host Control2 register. eMMC specific HS200/HS400 doesn't have
  2441. * their respective modes defined here, hence we use these values.
  2442. *
  2443. * HS200 - SDR104 (Since they both are equivalent in functionality)
  2444. * HS400 - This involves multiple configurations
  2445. * Initially SDR104 - when tuning is required as HS200
  2446. * Then when switching to DDR @ 400MHz (HS400) we use
  2447. * the vendor specific HC_SELECT_IN to control the mode.
  2448. *
  2449. * In addition to controlling the modes we also need to select the
  2450. * correct input clock for DLL depending on the mode.
  2451. *
  2452. * HS400 - divided clock (free running MCLK/2)
  2453. * All other modes - default (free running MCLK)
  2454. */
  2455. if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
  2456. /* Select the divided clock (free running MCLK/2) */
  2457. writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2458. & ~CORE_HC_MCLK_SEL_MASK)
  2459. | CORE_HC_MCLK_SEL_HS400),
  2460. host->ioaddr + CORE_VENDOR_SPEC);
  2461. /*
  2462. * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
  2463. * register
  2464. */
  2465. if (msm_host->tuning_done && !msm_host->calibration_done) {
  2466. /*
  2467. * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
  2468. * field in VENDOR_SPEC_FUNC
  2469. */
  2470. writel_relaxed((readl_relaxed(host->ioaddr + \
  2471. CORE_VENDOR_SPEC)
  2472. | CORE_HC_SELECT_IN_HS400
  2473. | CORE_HC_SELECT_IN_EN),
  2474. host->ioaddr + CORE_VENDOR_SPEC);
  2475. }
  2476. } else {
  2477. /* Select the default clock (free running MCLK) */
  2478. writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2479. & ~CORE_HC_MCLK_SEL_MASK)
  2480. | CORE_HC_MCLK_SEL_DFLT),
  2481. host->ioaddr + CORE_VENDOR_SPEC);
  2482. /*
  2483. * Disable HC_SELECT_IN to be able to use the UHS mode select
  2484. * configuration from Host Control2 register for all other
  2485. * modes.
  2486. *
  2487. * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
  2488. * in VENDOR_SPEC_FUNC
  2489. */
  2490. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2491. & ~CORE_HC_SELECT_IN_EN
  2492. & ~CORE_HC_SELECT_IN_MASK),
  2493. host->ioaddr + CORE_VENDOR_SPEC);
  2494. }
  2495. mb();
  2496. if (sup_clock != msm_host->clk_rate) {
  2497. pr_debug("%s: %s: setting clk rate to %u\n",
  2498. mmc_hostname(host->mmc), __func__, sup_clock);
  2499. rc = clk_set_rate(msm_host->clk, sup_clock);
  2500. if (rc) {
  2501. pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
  2502. mmc_hostname(host->mmc), __func__,
  2503. sup_clock, rc);
  2504. return;
  2505. }
  2506. msm_host->clk_rate = sup_clock;
  2507. host->clock = clock;
  2508. /*
  2509. * Update the bus vote in case of frequency change due to
  2510. * clock scaling.
  2511. */
  2512. sdhci_msm_bus_voting(host, 1);
  2513. }
  2514. }
  2515. static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
  2516. unsigned int uhs)
  2517. {
  2518. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2519. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2520. u16 ctrl_2;
  2521. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2522. /* Select Bus Speed Mode for host */
  2523. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  2524. if (uhs == MMC_TIMING_MMC_HS400)
  2525. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  2526. else if (uhs == MMC_TIMING_MMC_HS200)
  2527. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  2528. else if (uhs == MMC_TIMING_UHS_SDR12)
  2529. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  2530. else if (uhs == MMC_TIMING_UHS_SDR25)
  2531. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  2532. else if (uhs == MMC_TIMING_UHS_SDR50)
  2533. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  2534. else if (uhs == MMC_TIMING_UHS_SDR104)
  2535. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  2536. else if (uhs == MMC_TIMING_UHS_DDR50)
  2537. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  2538. /*
  2539. * When clock frquency is less than 100MHz, the feedback clock must be
  2540. * provided and DLL must not be used so that tuning can be skipped. To
  2541. * provide feedback clock, the mode selection can be any value less
  2542. * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
  2543. */
  2544. if (host->clock <= CORE_FREQ_100MHZ) {
  2545. if ((uhs == MMC_TIMING_MMC_HS400) ||
  2546. (uhs == MMC_TIMING_MMC_HS200) ||
  2547. (uhs == MMC_TIMING_UHS_SDR104))
  2548. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  2549. /*
  2550. * Make sure DLL is disabled when not required
  2551. *
  2552. * Write 1 to DLL_RST bit of DLL_CONFIG register
  2553. */
  2554. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  2555. | CORE_DLL_RST),
  2556. host->ioaddr + CORE_DLL_CONFIG);
  2557. /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
  2558. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  2559. | CORE_DLL_PDN),
  2560. host->ioaddr + CORE_DLL_CONFIG);
  2561. mb();
  2562. /*
  2563. * The DLL needs to be restored and CDCLP533 recalibrated
  2564. * when the clock frequency is set back to 400MHz.
  2565. */
  2566. msm_host->calibration_done = false;
  2567. }
  2568. pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
  2569. mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
  2570. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  2571. return 0;
  2572. }
  2573. /*
  2574. * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
  2575. *
  2576. * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
  2577. * access to legacy registers. It will stop current burst and prevent start of
  2578. * the next on.
  2579. *
  2580. * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
  2581. * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
  2582. * completed and a new one didn't start.
  2583. *
  2584. * Waiting for 4us while AHB finishes descriptors fetch.
  2585. */
  2586. static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
  2587. {
  2588. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2589. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2590. u32 value;
  2591. int ret;
  2592. u32 version;
  2593. version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
  2594. /* Core version 3.1.0 doesn't need this workaround */
  2595. if (version == CORE_VERSION_310)
  2596. return;
  2597. value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
  2598. value &= ~(u32)CORE_MCI_DPSM_ENABLE;
  2599. writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
  2600. /* Enable the test bus for device slot */
  2601. writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
  2602. msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2603. ret = readl_poll_timeout_noirq(msm_host->core_mem
  2604. + CORE_SDCC_DEBUG_REG, value,
  2605. !(value & CORE_DEBUG_REG_AHB_HTRANS),
  2606. CORE_AHB_DATA_DELAY_US, 1);
  2607. if (ret) {
  2608. pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
  2609. mmc_hostname(host->mmc), __func__);
  2610. BUG();
  2611. }
  2612. /* Disable the test bus for device slot */
  2613. value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2614. value &= ~CORE_TESTBUS_ENA;
  2615. writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2616. udelay(CORE_AHB_DESC_DELAY_US);
  2617. }
  2618. void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
  2619. {
  2620. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2621. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2622. int i, j, index = 0;
  2623. u32 debug_reg[20] = {0};
  2624. pr_info("----------- VENDOR REGISTER DUMP -----------\n");
  2625. pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
  2626. readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
  2627. readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
  2628. readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
  2629. pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x\n",
  2630. readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
  2631. readl_relaxed(host->ioaddr + CORE_DLL_STATUS));
  2632. /* Enable test bus */
  2633. for (j = 0; j < 3; j++) {
  2634. for (i = 8; i < 16; i++) {
  2635. if (j == 2 && i > 9)
  2636. break;
  2637. writel_relaxed((j << CORE_TESTBUS_SEL2_BIT) |
  2638. (i & CORE_TESTBUS_MASK),
  2639. msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2640. debug_reg[index++] = readl_relaxed(msm_host->core_mem +
  2641. CORE_SDCC_DEBUG_REG);
  2642. }
  2643. }
  2644. for (i = 0; i < 20; i = i + 4)
  2645. pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  2646. i, i + 3, debug_reg[i], debug_reg[i+1],
  2647. debug_reg[i+2], debug_reg[i+3]);
  2648. /* Disable test bus */
  2649. writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
  2650. CORE_TESTBUS_CONFIG);
  2651. }
  2652. static struct sdhci_ops sdhci_msm_ops = {
  2653. .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
  2654. .check_power_status = sdhci_msm_check_power_status,
  2655. .execute_tuning = sdhci_msm_execute_tuning,
  2656. .toggle_cdr = sdhci_msm_toggle_cdr,
  2657. .get_max_segments = sdhci_msm_max_segs,
  2658. .set_clock = sdhci_msm_set_clock,
  2659. .get_min_clock = sdhci_msm_get_min_clock,
  2660. .get_max_clock = sdhci_msm_get_max_clock,
  2661. .disable_data_xfer = sdhci_msm_disable_data_xfer,
  2662. .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
  2663. .enable_controller_clock = sdhci_msm_enable_controller_clock,
  2664. };
  2665. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  2666. static void mmc_enable_ls_gpio(struct sdhci_host *host, unsigned int gpio_no)
  2667. {
  2668. int status;
  2669. if (!gpio_is_valid(gpio_no))
  2670. return;
  2671. status = gpio_request_one(gpio_no, GPIOF_DIR_OUT, "LS_EN");
  2672. if (status)
  2673. pr_err("%s: %s: gpio_request(%d) failed\n",
  2674. mmc_hostname(host->mmc), __func__, gpio_no);
  2675. }
  2676. #endif
  2677. static int sdhci_msm_cfg_mpm_pin_wakeup(struct sdhci_host *host, unsigned mode)
  2678. {
  2679. int ret = 0;
  2680. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2681. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2682. unsigned int pin = msm_host->pdata->mpm_sdiowakeup_int;
  2683. if (!pin)
  2684. return 0;
  2685. switch (mode) {
  2686. case SDC_DAT1_DISABLE:
  2687. ret = msm_mpm_enable_pin(pin, 0);
  2688. break;
  2689. case SDC_DAT1_ENABLE:
  2690. ret = msm_mpm_set_pin_type(pin, IRQ_TYPE_LEVEL_LOW);
  2691. if (!ret)
  2692. ret = msm_mpm_enable_pin(pin, 1);
  2693. break;
  2694. case SDC_DAT1_ENWAKE:
  2695. ret = msm_mpm_set_pin_wake(pin, 1);
  2696. break;
  2697. case SDC_DAT1_DISWAKE:
  2698. ret = msm_mpm_set_pin_wake(pin, 0);
  2699. break;
  2700. default:
  2701. ret = -EINVAL;
  2702. break;
  2703. }
  2704. return ret;
  2705. }
  2706. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  2707. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  2708. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  2709. static void sdhci_msm_status_notify(int card_present, void *dev_id)
  2710. {
  2711. struct sdhci_host *host = (struct sdhci_host *)dev_id;
  2712. unsigned long flags;
  2713. if (host) {
  2714. host->mmc->rescan_disable=0;
  2715. pr_err("%s: %s: rescan_disable : %d\n",mmc_hostname(host->mmc),
  2716. __func__, host->mmc->rescan_disable);
  2717. spin_lock_irqsave(&host->lock, flags);
  2718. if (card_present) {
  2719. pr_err("%s: card inserted.\n", mmc_hostname(host->mmc));
  2720. host->flags &= ~SDHCI_DEVICE_DEAD;
  2721. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  2722. } else {
  2723. pr_err("%s: card removed.\n", mmc_hostname(host->mmc));
  2724. host->flags |= SDHCI_DEVICE_DEAD;
  2725. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  2726. }
  2727. tasklet_schedule(&host->card_tasklet);
  2728. spin_unlock_irqrestore(&host->lock, flags);
  2729. }
  2730. }
  2731. #endif
  2732. /* SYSFS about SD Card Detection */
  2733. extern struct class *sec_class;
  2734. static struct device *t_flash_detect_dev;
  2735. static ssize_t t_flash_detect_show(struct device *dev,
  2736. struct device_attribute *attr, char *buf)
  2737. {
  2738. struct sdhci_msm_host *msm_host = dev_get_drvdata(dev);
  2739. #if defined(CONFIG_MACH_SERRANO) || defined(CONFIG_SEC_ATLANTIC_PROJECT)
  2740. if (msm_host->mmc->card) {
  2741. printk(KERN_DEBUG "External sd: card inserted.\n");
  2742. return sprintf(buf, "Insert\n");
  2743. } else {
  2744. printk(KERN_DEBUG "External sd: card removed.\n");
  2745. return sprintf(buf, "Remove\n");
  2746. }
  2747. #else
  2748. unsigned int detect;
  2749. if (gpio_is_valid(msm_host->pdata->status_gpio))
  2750. detect = gpio_get_value(msm_host->pdata->status_gpio);
  2751. else {
  2752. pr_info("%s : External SD detect pin Error\n", __func__);
  2753. return sprintf(buf, "Error\n");
  2754. }
  2755. pr_info("%s : detect = %d.\n", __func__, detect);
  2756. if (!detect) {
  2757. printk(KERN_DEBUG "External sd: card inserted.\n");
  2758. return sprintf(buf, "Insert\n");
  2759. } else {
  2760. printk(KERN_DEBUG "External sd: card removed.\n");
  2761. return sprintf(buf, "Remove\n");
  2762. }
  2763. #endif
  2764. }
  2765. static DEVICE_ATTR(status, 0444, t_flash_detect_show, NULL);
  2766. static int __devinit sdhci_msm_probe(struct platform_device *pdev)
  2767. {
  2768. struct sdhci_host *host;
  2769. struct sdhci_pltfm_host *pltfm_host;
  2770. struct sdhci_msm_host *msm_host;
  2771. struct resource *core_memres = NULL;
  2772. int ret = 0, dead = 0;
  2773. u32 vdd_max_current;
  2774. u16 host_version;
  2775. u32 pwr, irq_status, irq_ctl;
  2776. unsigned long flags;
  2777. pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
  2778. msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
  2779. GFP_KERNEL);
  2780. if (!msm_host) {
  2781. ret = -ENOMEM;
  2782. goto out;
  2783. }
  2784. msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
  2785. host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
  2786. if (IS_ERR(host)) {
  2787. ret = PTR_ERR(host);
  2788. goto out;
  2789. }
  2790. pltfm_host = sdhci_priv(host);
  2791. pltfm_host->priv = msm_host;
  2792. msm_host->mmc = host->mmc;
  2793. msm_host->pdev = pdev;
  2794. /* Extract platform data */
  2795. if (pdev->dev.of_node) {
  2796. ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
  2797. if (ret < 0) {
  2798. dev_err(&pdev->dev, "Failed to get slot index %d\n",
  2799. ret);
  2800. goto pltfm_free;
  2801. }
  2802. msm_host->id = ret;
  2803. if (disable_slots & (1 << (ret - 1))) {
  2804. dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
  2805. ret);
  2806. ret = -ENODEV;
  2807. goto pltfm_free;
  2808. }
  2809. msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev);
  2810. if (!msm_host->pdata) {
  2811. dev_err(&pdev->dev, "DT parsing error\n");
  2812. goto pltfm_free;
  2813. }
  2814. } else {
  2815. dev_err(&pdev->dev, "No device tree node\n");
  2816. goto pltfm_free;
  2817. }
  2818. /* Setup Clocks */
  2819. /* Setup SDCC bus voter clock. */
  2820. msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
  2821. if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
  2822. /* Vote for max. clk rate for max. performance */
  2823. ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
  2824. if (ret)
  2825. goto pltfm_free;
  2826. ret = clk_prepare_enable(msm_host->bus_clk);
  2827. if (ret)
  2828. goto pltfm_free;
  2829. }
  2830. /* Setup main peripheral bus clock */
  2831. msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
  2832. if (!IS_ERR(msm_host->pclk)) {
  2833. ret = clk_prepare_enable(msm_host->pclk);
  2834. if (ret)
  2835. goto bus_clk_disable;
  2836. }
  2837. atomic_set(&msm_host->controller_clock, 1);
  2838. /* Setup SDC MMC clock */
  2839. msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
  2840. if (IS_ERR(msm_host->clk)) {
  2841. ret = PTR_ERR(msm_host->clk);
  2842. goto pclk_disable;
  2843. }
  2844. /* Set to the minimum supported clock frequency */
  2845. ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
  2846. if (ret) {
  2847. dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
  2848. goto pclk_disable;
  2849. }
  2850. ret = clk_prepare_enable(msm_host->clk);
  2851. if (ret)
  2852. goto pclk_disable;
  2853. msm_host->clk_rate = sdhci_msm_get_min_clock(host);
  2854. atomic_set(&msm_host->clks_on, 1);
  2855. /* Setup CDC calibration fixed feedback clock */
  2856. msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
  2857. if (!IS_ERR(msm_host->ff_clk)) {
  2858. ret = clk_prepare_enable(msm_host->ff_clk);
  2859. if (ret)
  2860. goto clk_disable;
  2861. }
  2862. /* Setup CDC calibration sleep clock */
  2863. msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
  2864. if (!IS_ERR(msm_host->sleep_clk)) {
  2865. ret = clk_prepare_enable(msm_host->sleep_clk);
  2866. if (ret)
  2867. goto ff_clk_disable;
  2868. }
  2869. msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
  2870. ret = sdhci_msm_bus_register(msm_host, pdev);
  2871. if (ret)
  2872. goto sleep_clk_disable;
  2873. if (msm_host->msm_bus_vote.client_handle)
  2874. INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
  2875. sdhci_msm_bus_work);
  2876. sdhci_msm_bus_voting(host, 1);
  2877. /* Setup regulators */
  2878. ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
  2879. if (ret) {
  2880. dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
  2881. goto bus_unregister;
  2882. }
  2883. /* Reset the core and Enable SDHC mode */
  2884. core_memres = platform_get_resource_byname(pdev,
  2885. IORESOURCE_MEM, "core_mem");
  2886. msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
  2887. resource_size(core_memres));
  2888. if (!msm_host->core_mem) {
  2889. dev_err(&pdev->dev, "Failed to remap registers\n");
  2890. ret = -ENOMEM;
  2891. goto vreg_deinit;
  2892. }
  2893. /* Unset HC_MODE_EN bit in HC_MODE register */
  2894. writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
  2895. /* Set SW_RST bit in POWER register (Offset 0x0) */
  2896. writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
  2897. CORE_SW_RST, msm_host->core_mem + CORE_POWER);
  2898. /*
  2899. * SW reset can take upto 10HCLK + 15MCLK cycles.
  2900. * Calculating based on min clk rates (hclk = 27MHz,
  2901. * mclk = 400KHz) it comes to ~40us. Let's poll for
  2902. * max. 1ms for reset completion.
  2903. */
  2904. ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
  2905. pwr, !(pwr & CORE_SW_RST), 10, 1000);
  2906. if (ret) {
  2907. dev_err(&pdev->dev, "reset failed (%d)\n", ret);
  2908. goto vreg_deinit;
  2909. }
  2910. /* Set HC_MODE_EN bit in HC_MODE register */
  2911. writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
  2912. /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
  2913. writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
  2914. FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
  2915. /*
  2916. * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
  2917. * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
  2918. * interrupt in GIC (by registering the interrupt handler), we need to
  2919. * ensure that any pending power irq interrupt status is acknowledged
  2920. * otherwise power irq interrupt handler would be fired prematurely.
  2921. */
  2922. irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
  2923. writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
  2924. irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
  2925. if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
  2926. irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
  2927. if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
  2928. irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
  2929. writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
  2930. /*
  2931. * Ensure that above writes are propogated before interrupt enablement
  2932. * in GIC.
  2933. */
  2934. mb();
  2935. /*
  2936. * Following are the deviations from SDHC spec v3.0 -
  2937. * 1. Card detection is handled using separate GPIO.
  2938. * 2. Bus power control is handled by interacting with PMIC.
  2939. */
  2940. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  2941. host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
  2942. host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
  2943. host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
  2944. host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
  2945. host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
  2946. host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
  2947. host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
  2948. if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
  2949. host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
  2950. host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
  2951. dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
  2952. host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
  2953. SDHCI_VENDOR_VER_SHIFT));
  2954. if (((host_version & SDHCI_VENDOR_VER_MASK) >>
  2955. SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
  2956. /*
  2957. * Add 40us delay in interrupt handler when
  2958. * operating at initialization frequency(400KHz).
  2959. */
  2960. host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
  2961. /*
  2962. * Set Software Reset for DAT line in Software
  2963. * Reset Register (Bit 2).
  2964. */
  2965. host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
  2966. }
  2967. host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
  2968. /* Setup PWRCTL irq */
  2969. msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
  2970. if (msm_host->pwr_irq < 0) {
  2971. dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
  2972. msm_host->pwr_irq);
  2973. goto vreg_deinit;
  2974. }
  2975. ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
  2976. sdhci_msm_pwr_irq, IRQF_ONESHOT,
  2977. dev_name(&pdev->dev), host);
  2978. if (ret) {
  2979. dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
  2980. msm_host->pwr_irq, ret);
  2981. goto vreg_deinit;
  2982. }
  2983. /* Enable pwr irq interrupts */
  2984. writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
  2985. /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
  2986. msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
  2987. /* Set host capabilities */
  2988. msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
  2989. msm_host->mmc->caps |= msm_host->pdata->caps;
  2990. vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
  2991. if (vdd_max_current >= 800)
  2992. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
  2993. else if (vdd_max_current >= 600)
  2994. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
  2995. else if (vdd_max_current >= 400)
  2996. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
  2997. else
  2998. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
  2999. if (vdd_max_current > 150)
  3000. msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
  3001. MMC_CAP_SET_XPC_300|
  3002. MMC_CAP_SET_XPC_330;
  3003. msm_host->mmc->caps2 |= msm_host->pdata->caps2;
  3004. msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
  3005. MMC_CAP2_DETECT_ON_ERR);
  3006. /* msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE; */
  3007. msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
  3008. msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
  3009. /* msm_host->mmc->caps2 &= ~MMC_CAP2_CLK_SCALE; */ /* Disable CLK_SCALE at L UPG */
  3010. /* msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM; */
  3011. msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
  3012. msm_host->mmc->caps2 |= MMC_CAP2_CORE_PM;
  3013. msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  3014. if (msm_host->pdata->nonremovable)
  3015. msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  3016. host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
  3017. init_completion(&msm_host->pwr_irq_completion);
  3018. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  3019. if (msm_host->pdata->ls_gpio || gpio_is_valid(msm_host->pdata->ls_gpio))
  3020. mmc_enable_ls_gpio(host, msm_host->pdata->ls_gpio);
  3021. #endif
  3022. if (gpio_is_valid(msm_host->pdata->status_gpio)) {
  3023. ret = mmc_cd_gpio_request(msm_host->mmc,
  3024. msm_host->pdata->status_gpio);
  3025. if (ret) {
  3026. dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
  3027. __func__, ret);
  3028. goto vreg_deinit;
  3029. }
  3030. }
  3031. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  3032. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  3033. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  3034. pr_err("%s: id %d\n", mmc_hostname(msm_host->mmc), msm_host->id);
  3035. if (msm_host->pdata->is_status_cb) {
  3036. msm_host->pdata->register_status_notify = brcm_wifi_status_register;
  3037. msm_host->pdata->register_status_notify(sdhci_msm_status_notify, host, host->mmc);
  3038. host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY | MMC_PM_KEEP_POWER;
  3039. host->mmc->pm_caps |= MMC_PM_IGNORE_PM_NOTIFY | MMC_PM_KEEP_POWER;
  3040. #if defined(CONFIG_DEFERRED_INITCALLS)
  3041. host->mmc->rescan_disable=0;
  3042. #else
  3043. host->mmc->rescan_disable=1;
  3044. #endif
  3045. pr_err("%s: id %d, register_status_notify=%x, host=%x, rescan_disable=%d\n",
  3046. mmc_hostname(msm_host->mmc), msm_host->id,
  3047. (unsigned int)msm_host->pdata->register_status_notify,
  3048. (unsigned int)host, host->mmc->rescan_disable);
  3049. }
  3050. #endif
  3051. /* SYSFS about SD Card Detection by soonil.lim */
  3052. #if defined(CONFIG_MACH_SERRANO)
  3053. if (t_flash_detect_dev == NULL && !strcmp(host->hw_name, "msm_sdcc.3")) {
  3054. #elif defined(CONFIG_SEC_ATLANTIC_PROJECT) || defined(CONFIG_MACH_JSGLTE_CHN_CMCC)
  3055. if (t_flash_detect_dev == NULL && !strcmp(host->hw_name, "msm_sdcc.2")) {
  3056. #else
  3057. if (t_flash_detect_dev == NULL && gpio_is_valid(msm_host->pdata->status_gpio)) {
  3058. #endif
  3059. printk(KERN_DEBUG "%s : Change sysfs Card Detect\n", __func__);
  3060. t_flash_detect_dev = device_create(sec_class,
  3061. NULL, 0, NULL, "sdcard");
  3062. if (IS_ERR(t_flash_detect_dev))
  3063. pr_err("%s : Failed to create device!\n", __func__);
  3064. if (device_create_file(t_flash_detect_dev,
  3065. &dev_attr_status) < 0)
  3066. pr_err("%s : Failed to create device file(%s)!\n",
  3067. __func__, dev_attr_status.attr.name);
  3068. dev_set_drvdata(t_flash_detect_dev, msm_host);
  3069. }
  3070. if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
  3071. host->dma_mask = DMA_BIT_MASK(32);
  3072. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  3073. } else {
  3074. dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
  3075. }
  3076. msm_host->pdata->sdiowakeup_irq = platform_get_irq_byname(pdev,
  3077. "sdiowakeup_irq");
  3078. if (msm_host->pdata->sdiowakeup_irq >= 0) {
  3079. msm_host->is_sdiowakeup_enabled = true;
  3080. ret = request_irq(msm_host->pdata->sdiowakeup_irq,
  3081. sdhci_msm_sdiowakeup_irq,
  3082. IRQF_SHARED | IRQF_TRIGGER_LOW,
  3083. "sdhci-msm sdiowakeup", host);
  3084. if (ret) {
  3085. dev_err(&pdev->dev, "%s: request sdiowakeup IRQ %d: failed: %d\n",
  3086. __func__, msm_host->pdata->sdiowakeup_irq, ret);
  3087. msm_host->pdata->sdiowakeup_irq = -1;
  3088. msm_host->is_sdiowakeup_enabled = false;
  3089. goto free_cd_gpio;
  3090. } else {
  3091. spin_lock_irqsave(&host->lock, flags);
  3092. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
  3093. spin_unlock_irqrestore(&host->lock, flags);
  3094. }
  3095. }
  3096. ret = sdhci_add_host(host);
  3097. if (ret) {
  3098. dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
  3099. goto free_cd_gpio;
  3100. }
  3101. msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
  3102. msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
  3103. sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
  3104. msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
  3105. msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
  3106. ret = device_create_file(&pdev->dev,
  3107. &msm_host->msm_bus_vote.max_bus_bw);
  3108. if (ret)
  3109. goto remove_host;
  3110. if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
  3111. msm_host->polling.show = show_polling;
  3112. msm_host->polling.store = store_polling;
  3113. sysfs_attr_init(&msm_host->polling.attr);
  3114. msm_host->polling.attr.name = "polling";
  3115. msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
  3116. ret = device_create_file(&pdev->dev, &msm_host->polling);
  3117. if (ret)
  3118. goto remove_max_bus_bw_file;
  3119. }
  3120. ret = pm_runtime_set_active(&pdev->dev);
  3121. if (ret)
  3122. pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
  3123. mmc_hostname(host->mmc), __func__, ret);
  3124. else if (mmc_use_core_runtime_pm(host->mmc))
  3125. pm_runtime_enable(&pdev->dev);
  3126. if (msm_host->pdata->mpm_sdiowakeup_int != -1) {
  3127. ret = sdhci_msm_cfg_mpm_pin_wakeup(host, SDC_DAT1_ENABLE);
  3128. if (ret) {
  3129. pr_err("%s: enabling wakeup: failed: ret: %d\n",
  3130. mmc_hostname(host->mmc), ret);
  3131. ret = 0;
  3132. msm_host->pdata->mpm_sdiowakeup_int = -1;
  3133. }
  3134. }
  3135. device_enable_async_suspend(&pdev->dev);
  3136. /* Successful initialization */
  3137. goto out;
  3138. remove_max_bus_bw_file:
  3139. device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
  3140. remove_host:
  3141. dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  3142. sdhci_remove_host(host, dead);
  3143. free_cd_gpio:
  3144. if (gpio_is_valid(msm_host->pdata->status_gpio))
  3145. mmc_cd_gpio_free(msm_host->mmc);
  3146. if (sdhci_is_valid_gpio_wakeup_int(msm_host))
  3147. free_irq(msm_host->pdata->sdiowakeup_irq, host);
  3148. vreg_deinit:
  3149. sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
  3150. bus_unregister:
  3151. if (msm_host->msm_bus_vote.client_handle)
  3152. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  3153. sdhci_msm_bus_unregister(msm_host);
  3154. sleep_clk_disable:
  3155. if (!IS_ERR(msm_host->sleep_clk))
  3156. clk_disable_unprepare(msm_host->sleep_clk);
  3157. ff_clk_disable:
  3158. if (!IS_ERR(msm_host->ff_clk))
  3159. clk_disable_unprepare(msm_host->ff_clk);
  3160. clk_disable:
  3161. if (!IS_ERR(msm_host->clk))
  3162. clk_disable_unprepare(msm_host->clk);
  3163. pclk_disable:
  3164. if (!IS_ERR(msm_host->pclk))
  3165. clk_disable_unprepare(msm_host->pclk);
  3166. bus_clk_disable:
  3167. if (!IS_ERR_OR_NULL(msm_host->bus_clk))
  3168. clk_disable_unprepare(msm_host->bus_clk);
  3169. pltfm_free:
  3170. sdhci_pltfm_free(pdev);
  3171. out:
  3172. pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
  3173. return ret;
  3174. }
  3175. static int __devexit sdhci_msm_remove(struct platform_device *pdev)
  3176. {
  3177. struct sdhci_host *host = platform_get_drvdata(pdev);
  3178. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3179. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3180. struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
  3181. int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
  3182. 0xffffffff);
  3183. pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
  3184. if (!gpio_is_valid(msm_host->pdata->status_gpio))
  3185. device_remove_file(&pdev->dev, &msm_host->polling);
  3186. device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
  3187. sdhci_remove_host(host, dead);
  3188. pm_runtime_disable(&pdev->dev);
  3189. sdhci_pltfm_free(pdev);
  3190. if (sdhci_is_valid_mpm_wakeup_int(msm_host))
  3191. sdhci_msm_cfg_mpm_pin_wakeup(host, SDC_DAT1_DISABLE);
  3192. if (sdhci_is_valid_gpio_wakeup_int(msm_host))
  3193. free_irq(msm_host->pdata->sdiowakeup_irq, host);
  3194. if (gpio_is_valid(msm_host->pdata->status_gpio))
  3195. mmc_cd_gpio_free(msm_host->mmc);
  3196. sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
  3197. if (pdata->pin_data)
  3198. sdhci_msm_setup_pins(pdata, false);
  3199. if (msm_host->msm_bus_vote.client_handle) {
  3200. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  3201. sdhci_msm_bus_unregister(msm_host);
  3202. }
  3203. return 0;
  3204. }
  3205. static int sdhci_msm_cfg_sdio_wakeup(struct sdhci_host *host, bool enable)
  3206. {
  3207. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3208. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3209. unsigned long flags;
  3210. int ret = 0;
  3211. if (!(host->mmc->card && mmc_card_sdio(host->mmc->card) &&
  3212. (sdhci_is_valid_mpm_wakeup_int(msm_host) ||
  3213. sdhci_is_valid_gpio_wakeup_int(msm_host)) &&
  3214. mmc_card_wake_sdio_irq(host->mmc))) {
  3215. return 1;
  3216. }
  3217. spin_lock_irqsave(&host->lock, flags);
  3218. if (enable) {
  3219. /* configure DAT1 gpio if applicable */
  3220. if (sdhci_is_valid_gpio_wakeup_int(msm_host)) {
  3221. ret = enable_irq_wake(msm_host->pdata->sdiowakeup_irq);
  3222. if (!ret)
  3223. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, true);
  3224. goto out;
  3225. } else {
  3226. ret = sdhci_msm_cfg_mpm_pin_wakeup(host,
  3227. SDC_DAT1_ENWAKE);
  3228. if (ret)
  3229. goto out;
  3230. ret = enable_irq_wake(host->irq);
  3231. if (ret)
  3232. sdhci_msm_cfg_mpm_pin_wakeup(host,
  3233. SDC_DAT1_DISWAKE);
  3234. }
  3235. } else {
  3236. if (sdhci_is_valid_gpio_wakeup_int(msm_host)) {
  3237. ret = disable_irq_wake(msm_host->pdata->sdiowakeup_irq);
  3238. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
  3239. } else {
  3240. ret = sdhci_msm_cfg_mpm_pin_wakeup(host,
  3241. SDC_DAT1_DISWAKE);
  3242. if (ret)
  3243. goto out;
  3244. ret = disable_irq_wake(host->irq);
  3245. }
  3246. }
  3247. out:
  3248. if (ret)
  3249. pr_err("%s: %s: %sable wakeup: failed: %d gpio: %d mpm: %d\n",
  3250. mmc_hostname(host->mmc), __func__, enable ? "en" : "dis",
  3251. ret, msm_host->pdata->sdiowakeup_irq,
  3252. msm_host->pdata->mpm_sdiowakeup_int);
  3253. spin_unlock_irqrestore(&host->lock, flags);
  3254. return ret;
  3255. }
  3256. static int sdhci_msm_runtime_suspend(struct device *dev)
  3257. {
  3258. struct sdhci_host *host = dev_get_drvdata(dev);
  3259. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3260. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3261. int ret;
  3262. ret = sdhci_msm_cfg_sdio_wakeup(host, true);
  3263. /* pwr_irq is not monitored by mpm on suspend, hence disable it */
  3264. if (!ret)
  3265. goto skip_disable_host_irq;
  3266. disable_irq(host->irq);
  3267. skip_disable_host_irq:
  3268. disable_irq(msm_host->pwr_irq);
  3269. /*
  3270. * Remove the vote immediately only if clocks are off in which
  3271. * case we might have queued work to remove vote but it may not
  3272. * be completed before runtime suspend or system suspend.
  3273. */
  3274. if (!atomic_read(&msm_host->clks_on)) {
  3275. if (msm_host->msm_bus_vote.client_handle)
  3276. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  3277. }
  3278. if (msm_host->id == 3)
  3279. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3280. return 0;
  3281. }
  3282. static int sdhci_msm_runtime_resume(struct device *dev)
  3283. {
  3284. struct sdhci_host *host = dev_get_drvdata(dev);
  3285. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3286. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3287. int ret;
  3288. ret = sdhci_msm_cfg_sdio_wakeup(host, false);
  3289. if (!ret)
  3290. goto skip_enable_host_irq;
  3291. enable_irq(host->irq);
  3292. if (msm_host->id == 3)
  3293. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3294. skip_enable_host_irq:
  3295. enable_irq(msm_host->pwr_irq);
  3296. return 0;
  3297. }
  3298. #ifdef CONFIG_PM_SLEEP
  3299. static int sdhci_msm_suspend(struct device *dev)
  3300. {
  3301. struct sdhci_host *host = dev_get_drvdata(dev);
  3302. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3303. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3304. int ret = 0;
  3305. if (gpio_is_valid(msm_host->pdata->status_gpio))
  3306. mmc_cd_gpio_free(msm_host->mmc);
  3307. if (pm_runtime_suspended(dev)) {
  3308. pr_debug("%s: %s: already runtime suspended\n",
  3309. mmc_hostname(host->mmc), __func__);
  3310. goto out;
  3311. }
  3312. if (msm_host->id == 3)
  3313. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3314. return sdhci_msm_runtime_suspend(dev);
  3315. out:
  3316. return ret;
  3317. }
  3318. static int sdhci_msm_resume(struct device *dev)
  3319. {
  3320. struct sdhci_host *host = dev_get_drvdata(dev);
  3321. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3322. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3323. int ret = 0;
  3324. if (gpio_is_valid(msm_host->pdata->status_gpio)) {
  3325. ret = mmc_cd_gpio_request(msm_host->mmc,
  3326. msm_host->pdata->status_gpio);
  3327. if (ret)
  3328. pr_err("%s: %s: Failed to request card detection IRQ %d\n",
  3329. mmc_hostname(host->mmc), __func__, ret);
  3330. }
  3331. if (pm_runtime_suspended(dev)) {
  3332. pr_debug("%s: %s: runtime suspended, defer system resume\n",
  3333. mmc_hostname(host->mmc), __func__);
  3334. goto out;
  3335. }
  3336. if (msm_host->id == 3)
  3337. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3338. return sdhci_msm_runtime_resume(dev);
  3339. out:
  3340. return ret;
  3341. }
  3342. static int sdhci_msm_suspend_noirq(struct device *dev)
  3343. {
  3344. struct sdhci_host *host = dev_get_drvdata(dev);
  3345. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3346. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3347. int ret = 0;
  3348. /*
  3349. * ksdioirqd may get scheduled after sdhc suspend, hence retry
  3350. * suspend in case the clocks are ON
  3351. */
  3352. if (atomic_read(&msm_host->clks_on)) {
  3353. pr_warn("%s: %s: clock ON after suspend, aborting suspend\n",
  3354. mmc_hostname(host->mmc), __func__);
  3355. ret = -EAGAIN;
  3356. }
  3357. return ret;
  3358. }
  3359. #endif
  3360. #ifdef CONFIG_PM
  3361. static const struct dev_pm_ops sdhci_msm_pmops = {
  3362. SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
  3363. SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
  3364. NULL)
  3365. .suspend_noirq = sdhci_msm_suspend_noirq,
  3366. };
  3367. #define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
  3368. #else
  3369. #define SDHCI_PM_OPS NULL
  3370. #endif
  3371. static const struct of_device_id sdhci_msm_dt_match[] = {
  3372. {.compatible = "qcom,sdhci-msm"},
  3373. { /* sentinel */ }
  3374. };
  3375. MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
  3376. static struct platform_driver sdhci_msm_driver = {
  3377. .probe = sdhci_msm_probe,
  3378. .remove = __devexit_p(sdhci_msm_remove),
  3379. .driver = {
  3380. .name = "sdhci_msm",
  3381. .owner = THIS_MODULE,
  3382. .of_match_table = sdhci_msm_dt_match,
  3383. .pm = SDHCI_MSM_PMOPS,
  3384. },
  3385. };
  3386. module_platform_driver(sdhci_msm_driver);
  3387. MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
  3388. MODULE_LICENSE("GPL v2");