sdhci-msm.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868
  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 *
  1328. sdhci_msm_populate_pdata(struct device *dev, struct sdhci_host *host)
  1329. {
  1330. struct sdhci_msm_pltfm_data *pdata = NULL;
  1331. struct device_node *np = dev->of_node;
  1332. u32 bus_width = 0;
  1333. u32 cpu_dma_latency;
  1334. int len, i, mpm_int;
  1335. int clk_table_len;
  1336. u32 *clk_table = NULL;
  1337. enum of_gpio_flags flags = OF_GPIO_ACTIVE_LOW;
  1338. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  1339. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  1340. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  1341. int vendor_type = 0;
  1342. #endif
  1343. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  1344. if (!pdata) {
  1345. dev_err(dev, "failed to allocate memory for platform data\n");
  1346. goto out;
  1347. }
  1348. pdata->status_gpio = of_get_named_gpio_flags(np, "cd-gpios", 0, &flags);
  1349. if (gpio_is_valid(pdata->status_gpio) & !(flags & OF_GPIO_ACTIVE_LOW))
  1350. pdata->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  1351. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  1352. pdata->ls_gpio = of_get_named_gpio_flags(np, "ls-gpios", 0, 0);
  1353. #endif
  1354. of_property_read_u32(np, "qcom,bus-width", &bus_width);
  1355. if (bus_width == 8)
  1356. pdata->mmc_bus_width = MMC_CAP_8_BIT_DATA;
  1357. else if (bus_width == 4)
  1358. pdata->mmc_bus_width = MMC_CAP_4_BIT_DATA;
  1359. else {
  1360. dev_notice(dev, "invalid bus-width, default to 1-bit mode\n");
  1361. pdata->mmc_bus_width = 0;
  1362. }
  1363. if (!of_property_read_u32(np, "qcom,cpu-dma-latency-us",
  1364. &cpu_dma_latency))
  1365. pdata->cpu_dma_latency_us = cpu_dma_latency;
  1366. if (sdhci_msm_dt_get_array(dev, "qcom,clk-rates",
  1367. &clk_table, &clk_table_len, 0)) {
  1368. dev_err(dev, "failed parsing supported clock rates\n");
  1369. goto out;
  1370. }
  1371. if (!clk_table || !clk_table_len) {
  1372. dev_err(dev, "Invalid clock table\n");
  1373. goto out;
  1374. }
  1375. pdata->sup_clk_table = clk_table;
  1376. pdata->sup_clk_cnt = clk_table_len;
  1377. pdata->vreg_data = devm_kzalloc(dev, sizeof(struct
  1378. sdhci_msm_slot_reg_data),
  1379. GFP_KERNEL);
  1380. if (!pdata->vreg_data) {
  1381. dev_err(dev, "failed to allocate memory for vreg data\n");
  1382. goto out;
  1383. }
  1384. if (sdhci_msm_dt_parse_vreg_info(dev, &pdata->vreg_data->vdd_data,
  1385. "vdd")) {
  1386. dev_err(dev, "failed parsing vdd data\n");
  1387. goto out;
  1388. }
  1389. if (sdhci_msm_dt_parse_vreg_info(dev,
  1390. &pdata->vreg_data->vdd_io_data,
  1391. "vdd-io")) {
  1392. dev_err(dev, "failed parsing vdd-io data\n");
  1393. goto out;
  1394. }
  1395. if (sdhci_msm_dt_parse_gpio_info(dev, pdata)) {
  1396. dev_err(dev, "failed parsing gpio data\n");
  1397. goto out;
  1398. }
  1399. len = of_property_count_strings(np, "qcom,bus-speed-mode");
  1400. for (i = 0; i < len; i++) {
  1401. const char *name = NULL;
  1402. of_property_read_string_index(np,
  1403. "qcom,bus-speed-mode", i, &name);
  1404. if (!name)
  1405. continue;
  1406. if (!strncmp(name, "HS400_1p8v", sizeof("HS400_1p8v")))
  1407. pdata->caps2 |= MMC_CAP2_HS400_1_8V;
  1408. else if (!strncmp(name, "HS400_1p2v", sizeof("HS400_1p2v")))
  1409. pdata->caps2 |= MMC_CAP2_HS400_1_2V;
  1410. else if (!strncmp(name, "HS200_1p8v", sizeof("HS200_1p8v")))
  1411. pdata->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
  1412. else if (!strncmp(name, "HS200_1p2v", sizeof("HS200_1p2v")))
  1413. pdata->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
  1414. else if (!strncmp(name, "DDR_1p8v", sizeof("DDR_1p8v")))
  1415. pdata->caps |= MMC_CAP_1_8V_DDR
  1416. | MMC_CAP_UHS_DDR50;
  1417. else if (!strncmp(name, "DDR_1p2v", sizeof("DDR_1p2v")))
  1418. pdata->caps |= MMC_CAP_1_2V_DDR
  1419. | MMC_CAP_UHS_DDR50;
  1420. }
  1421. if (of_get_property(np, "qcom,nonremovable", NULL))
  1422. pdata->nonremovable = true;
  1423. if (!of_property_read_u32(np, "qcom,dat1-mpm-int",
  1424. &mpm_int))
  1425. pdata->mpm_sdiowakeup_int = mpm_int;
  1426. else
  1427. pdata->mpm_sdiowakeup_int = -1;
  1428. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  1429. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  1430. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  1431. pr_err("%s: before parsing vendor_type\n", __FUNCTION__);
  1432. if (of_get_property(np, "status-cb", &vendor_type)) {
  1433. pdata->is_status_cb = true;
  1434. pr_err("%s: vendor_type=%d \n", __FUNCTION__, vendor_type);
  1435. }
  1436. #endif
  1437. if (of_property_read_bool(np, "qcom,wakeup-on-idle"))
  1438. host->mmc->wakeup_on_idle = true;
  1439. return pdata;
  1440. out:
  1441. return NULL;
  1442. }
  1443. /* Returns required bandwidth in Bytes per Sec */
  1444. static unsigned int sdhci_get_bw_required(struct sdhci_host *host,
  1445. struct mmc_ios *ios)
  1446. {
  1447. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1448. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1449. unsigned int bw;
  1450. bw = msm_host->clk_rate;
  1451. /*
  1452. * For DDR mode, SDCC controller clock will be at
  1453. * the double rate than the actual clock that goes to card.
  1454. */
  1455. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1456. bw /= 2;
  1457. else if (ios->bus_width == MMC_BUS_WIDTH_1)
  1458. bw /= 8;
  1459. return bw;
  1460. }
  1461. static int sdhci_msm_bus_get_vote_for_bw(struct sdhci_msm_host *host,
  1462. unsigned int bw)
  1463. {
  1464. unsigned int *table = host->pdata->voting_data->bw_vecs;
  1465. unsigned int size = host->pdata->voting_data->bw_vecs_size;
  1466. int i;
  1467. if (host->msm_bus_vote.is_max_bw_needed && bw)
  1468. return host->msm_bus_vote.max_bw_vote;
  1469. for (i = 0; i < size; i++) {
  1470. if (bw <= table[i])
  1471. break;
  1472. }
  1473. if (i && (i == size))
  1474. i--;
  1475. return i;
  1476. }
  1477. /*
  1478. * This function must be called with host lock acquired.
  1479. * Caller of this function should also ensure that msm bus client
  1480. * handle is not null.
  1481. */
  1482. static inline int sdhci_msm_bus_set_vote(struct sdhci_msm_host *msm_host,
  1483. int vote,
  1484. unsigned long flags)
  1485. {
  1486. struct sdhci_host *host = platform_get_drvdata(msm_host->pdev);
  1487. int rc = 0;
  1488. if (vote != msm_host->msm_bus_vote.curr_vote) {
  1489. spin_unlock_irqrestore(&host->lock, flags);
  1490. rc = msm_bus_scale_client_update_request(
  1491. msm_host->msm_bus_vote.client_handle, vote);
  1492. spin_lock_irqsave(&host->lock, flags);
  1493. if (rc) {
  1494. pr_err("%s: msm_bus_scale_client_update_request() failed: bus_client_handle=0x%x, vote=%d, err=%d\n",
  1495. mmc_hostname(host->mmc),
  1496. msm_host->msm_bus_vote.client_handle, vote, rc);
  1497. goto out;
  1498. }
  1499. msm_host->msm_bus_vote.curr_vote = vote;
  1500. }
  1501. out:
  1502. return rc;
  1503. }
  1504. /*
  1505. * Internal work. Work to set 0 bandwidth for msm bus.
  1506. */
  1507. static void sdhci_msm_bus_work(struct work_struct *work)
  1508. {
  1509. struct sdhci_msm_host *msm_host;
  1510. struct sdhci_host *host;
  1511. unsigned long flags;
  1512. msm_host = container_of(work, struct sdhci_msm_host,
  1513. msm_bus_vote.vote_work.work);
  1514. host = platform_get_drvdata(msm_host->pdev);
  1515. if (!msm_host->msm_bus_vote.client_handle)
  1516. return;
  1517. spin_lock_irqsave(&host->lock, flags);
  1518. /* don't vote for 0 bandwidth if any request is in progress */
  1519. if (!host->mrq) {
  1520. sdhci_msm_bus_set_vote(msm_host,
  1521. msm_host->msm_bus_vote.min_bw_vote, flags);
  1522. } else
  1523. pr_warning("%s: %s: Transfer in progress. skipping bus voting to 0 bandwidth\n",
  1524. mmc_hostname(host->mmc), __func__);
  1525. spin_unlock_irqrestore(&host->lock, flags);
  1526. }
  1527. /*
  1528. * This function cancels any scheduled delayed work and sets the bus
  1529. * vote based on bw (bandwidth) argument.
  1530. */
  1531. static void sdhci_msm_bus_cancel_work_and_set_vote(struct sdhci_host *host,
  1532. unsigned int bw)
  1533. {
  1534. int vote;
  1535. unsigned long flags;
  1536. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1537. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1538. cancel_delayed_work_sync(&msm_host->msm_bus_vote.vote_work);
  1539. spin_lock_irqsave(&host->lock, flags);
  1540. vote = sdhci_msm_bus_get_vote_for_bw(msm_host, bw);
  1541. sdhci_msm_bus_set_vote(msm_host, vote, flags);
  1542. spin_unlock_irqrestore(&host->lock, flags);
  1543. }
  1544. #define MSM_MMC_BUS_VOTING_DELAY 200 /* msecs */
  1545. /* This function queues a work which will set the bandwidth requiement to 0 */
  1546. static void sdhci_msm_bus_queue_work(struct sdhci_host *host)
  1547. {
  1548. unsigned long flags;
  1549. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1550. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1551. spin_lock_irqsave(&host->lock, flags);
  1552. if (msm_host->msm_bus_vote.min_bw_vote !=
  1553. msm_host->msm_bus_vote.curr_vote)
  1554. queue_delayed_work(system_nrt_wq,
  1555. &msm_host->msm_bus_vote.vote_work,
  1556. msecs_to_jiffies(MSM_MMC_BUS_VOTING_DELAY));
  1557. spin_unlock_irqrestore(&host->lock, flags);
  1558. }
  1559. static int sdhci_msm_bus_register(struct sdhci_msm_host *host,
  1560. struct platform_device *pdev)
  1561. {
  1562. int rc = 0;
  1563. struct msm_bus_scale_pdata *bus_pdata;
  1564. struct sdhci_msm_bus_voting_data *data;
  1565. struct device *dev = &pdev->dev;
  1566. data = devm_kzalloc(dev,
  1567. sizeof(struct sdhci_msm_bus_voting_data), GFP_KERNEL);
  1568. if (!data) {
  1569. dev_err(&pdev->dev,
  1570. "%s: failed to allocate memory\n", __func__);
  1571. rc = -ENOMEM;
  1572. goto out;
  1573. }
  1574. data->bus_pdata = msm_bus_cl_get_pdata(pdev);
  1575. if (data->bus_pdata) {
  1576. rc = sdhci_msm_dt_get_array(dev, "qcom,bus-bw-vectors-bps",
  1577. &data->bw_vecs, &data->bw_vecs_size, 0);
  1578. if (rc) {
  1579. dev_err(&pdev->dev,
  1580. "%s: Failed to get bus-bw-vectors-bps\n",
  1581. __func__);
  1582. goto out;
  1583. }
  1584. host->pdata->voting_data = data;
  1585. }
  1586. if (host->pdata->voting_data &&
  1587. host->pdata->voting_data->bus_pdata &&
  1588. host->pdata->voting_data->bw_vecs &&
  1589. host->pdata->voting_data->bw_vecs_size) {
  1590. bus_pdata = host->pdata->voting_data->bus_pdata;
  1591. host->msm_bus_vote.client_handle =
  1592. msm_bus_scale_register_client(bus_pdata);
  1593. if (!host->msm_bus_vote.client_handle) {
  1594. dev_err(&pdev->dev, "msm_bus_scale_register_client()\n");
  1595. rc = -EFAULT;
  1596. goto out;
  1597. }
  1598. /* cache the vote index for minimum and maximum bandwidth */
  1599. host->msm_bus_vote.min_bw_vote =
  1600. sdhci_msm_bus_get_vote_for_bw(host, 0);
  1601. host->msm_bus_vote.max_bw_vote =
  1602. sdhci_msm_bus_get_vote_for_bw(host, UINT_MAX);
  1603. } else {
  1604. devm_kfree(dev, data);
  1605. }
  1606. out:
  1607. return rc;
  1608. }
  1609. static void sdhci_msm_bus_unregister(struct sdhci_msm_host *host)
  1610. {
  1611. if (host->msm_bus_vote.client_handle)
  1612. msm_bus_scale_unregister_client(
  1613. host->msm_bus_vote.client_handle);
  1614. }
  1615. static void sdhci_msm_bus_voting(struct sdhci_host *host, u32 enable)
  1616. {
  1617. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1618. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1619. struct mmc_ios *ios = &host->mmc->ios;
  1620. unsigned int bw;
  1621. if (!msm_host->msm_bus_vote.client_handle)
  1622. return;
  1623. bw = sdhci_get_bw_required(host, ios);
  1624. if (enable) {
  1625. sdhci_msm_bus_cancel_work_and_set_vote(host, bw);
  1626. } else {
  1627. /*
  1628. * If clock gating is enabled, then remove the vote
  1629. * immediately because clocks will be disabled only
  1630. * after SDHCI_MSM_MMC_CLK_GATE_DELAY and thus no
  1631. * additional delay is required to remove the bus vote.
  1632. */
  1633. if (host->mmc->clkgate_delay)
  1634. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  1635. else
  1636. sdhci_msm_bus_queue_work(host);
  1637. }
  1638. }
  1639. /* Regulator utility functions */
  1640. static int sdhci_msm_vreg_init_reg(struct device *dev,
  1641. struct sdhci_msm_reg_data *vreg)
  1642. {
  1643. int ret = 0;
  1644. /* check if regulator is already initialized? */
  1645. if (vreg->reg)
  1646. goto out;
  1647. /* Get the regulator handle */
  1648. vreg->reg = devm_regulator_get(dev, vreg->name);
  1649. if (IS_ERR(vreg->reg)) {
  1650. ret = PTR_ERR(vreg->reg);
  1651. pr_err("%s: devm_regulator_get(%s) failed. ret=%d\n",
  1652. __func__, vreg->name, ret);
  1653. goto out;
  1654. }
  1655. if (regulator_count_voltages(vreg->reg) > 0) {
  1656. vreg->set_voltage_sup = true;
  1657. /* sanity check */
  1658. if (!vreg->high_vol_level || !vreg->hpm_uA) {
  1659. pr_err("%s: %s invalid constraints specified\n",
  1660. __func__, vreg->name);
  1661. ret = -EINVAL;
  1662. }
  1663. }
  1664. out:
  1665. return ret;
  1666. }
  1667. static void sdhci_msm_vreg_deinit_reg(struct sdhci_msm_reg_data *vreg)
  1668. {
  1669. if (vreg->reg)
  1670. devm_regulator_put(vreg->reg);
  1671. }
  1672. static int sdhci_msm_vreg_set_optimum_mode(struct sdhci_msm_reg_data
  1673. *vreg, int uA_load)
  1674. {
  1675. int ret = 0;
  1676. /*
  1677. * regulators that do not support regulator_set_voltage also
  1678. * do not support regulator_set_optimum_mode
  1679. */
  1680. if (vreg->set_voltage_sup) {
  1681. ret = regulator_set_optimum_mode(vreg->reg, uA_load);
  1682. if (ret < 0)
  1683. pr_err("%s: regulator_set_optimum_mode(reg=%s,uA_load=%d) failed. ret=%d\n",
  1684. __func__, vreg->name, uA_load, ret);
  1685. else
  1686. /*
  1687. * regulator_set_optimum_mode() can return non zero
  1688. * value even for success case.
  1689. */
  1690. ret = 0;
  1691. }
  1692. return ret;
  1693. }
  1694. static int sdhci_msm_vreg_set_voltage(struct sdhci_msm_reg_data *vreg,
  1695. int min_uV, int max_uV)
  1696. {
  1697. int ret = 0;
  1698. if (vreg->set_voltage_sup) {
  1699. ret = regulator_set_voltage(vreg->reg, min_uV, max_uV);
  1700. if (ret) {
  1701. pr_err("%s: regulator_set_voltage(%s)failed. min_uV=%d,max_uV=%d,ret=%d\n",
  1702. __func__, vreg->name, min_uV, max_uV, ret);
  1703. }
  1704. }
  1705. return ret;
  1706. }
  1707. static int sdhci_msm_vreg_enable(struct sdhci_msm_reg_data *vreg)
  1708. {
  1709. int ret = 0;
  1710. if (!vreg->reg) {
  1711. pr_err("%s: %s Cannot find Regulator\n", __func__, vreg->name);
  1712. return ret;
  1713. }
  1714. #if defined(CONFIG_ARCH_MSM8974)\
  1715. || defined(CONFIG_MACH_FRESCOLTESKT)||defined(CONFIG_MACH_FRESCOLTEKTT)||defined(CONFIG_MACH_FRESCOLTELGT)
  1716. if (vreg->rpm_reg) {
  1717. ret = rpm_regulator_set_mode(vreg->rpm_reg,
  1718. RPM_REGULATOR_MODE_HPM);
  1719. if (ret) {
  1720. pr_err("Failed setting rpm-reg(%s) to HPM: %d",
  1721. vreg->name, ret);
  1722. return ret;
  1723. }
  1724. return ret;
  1725. }
  1726. #endif
  1727. /* Put regulator in HPM (high power mode) */
  1728. ret = sdhci_msm_vreg_set_optimum_mode(vreg, vreg->hpm_uA);
  1729. if (ret < 0)
  1730. return ret;
  1731. if (!vreg->is_enabled) {
  1732. /* Set voltage level */
  1733. ret = sdhci_msm_vreg_set_voltage(vreg, vreg->high_vol_level,
  1734. vreg->high_vol_level);
  1735. if (ret)
  1736. return ret;
  1737. }
  1738. ret = regulator_enable(vreg->reg);
  1739. if (ret) {
  1740. pr_err("%s: regulator_enable(%s) failed. ret=%d\n",
  1741. __func__, vreg->name, ret);
  1742. return ret;
  1743. }
  1744. vreg->is_enabled = true;
  1745. return ret;
  1746. }
  1747. static int sdhci_msm_vreg_disable(struct sdhci_msm_reg_data *vreg)
  1748. {
  1749. int ret = 0;
  1750. if (!vreg->reg) {
  1751. pr_err("%s: %s Cannot find Regulator\n", __func__, vreg->name);
  1752. return ret;
  1753. }
  1754. /* Never disable regulator marked as always_on */
  1755. if (vreg->is_enabled && !vreg->is_always_on) {
  1756. ret = regulator_disable(vreg->reg);
  1757. if (ret) {
  1758. pr_err("%s: regulator_disable(%s) failed. ret=%d\n",
  1759. __func__, vreg->name, ret);
  1760. goto out;
  1761. }
  1762. vreg->is_enabled = false;
  1763. ret = sdhci_msm_vreg_set_optimum_mode(vreg, 0);
  1764. if (ret < 0)
  1765. goto out;
  1766. /* Set min. voltage level to 0 */
  1767. ret = sdhci_msm_vreg_set_voltage(vreg, 0, vreg->high_vol_level);
  1768. if (ret)
  1769. goto out;
  1770. } else if (vreg->is_enabled && vreg->is_always_on) {
  1771. if (vreg->lpm_sup) {
  1772. /* Put always_on regulator in LPM (low power mode) */
  1773. ret = sdhci_msm_vreg_set_optimum_mode(vreg,
  1774. vreg->lpm_uA);
  1775. if (ret < 0)
  1776. goto out;
  1777. }
  1778. }
  1779. out:
  1780. return ret;
  1781. }
  1782. static int sdhci_msm_setup_vreg(struct sdhci_msm_pltfm_data *pdata,
  1783. bool enable, bool is_init)
  1784. {
  1785. int ret = 0, i;
  1786. struct sdhci_msm_slot_reg_data *curr_slot;
  1787. struct sdhci_msm_reg_data *vreg_table[2];
  1788. curr_slot = pdata->vreg_data;
  1789. if (!curr_slot) {
  1790. pr_debug("%s: vreg info unavailable,assuming the slot is powered by always on domain\n",
  1791. __func__);
  1792. goto out;
  1793. }
  1794. #if defined(CONFIG_SEC_K_PROJECT)
  1795. /*
  1796. * In SD Card Case using FPGA,
  1797. * Turn on : vdd_io on -> vdd on
  1798. * Turn off : vdd off -> delay 5msec -> vdd_io off
  1799. * vdd : L21, vdd_io : L13
  1800. */
  1801. if (pdata->status_gpio && enable)
  1802. {
  1803. vreg_table[0] = curr_slot->vdd_io_data;
  1804. vreg_table[1] = curr_slot->vdd_data;
  1805. } else {
  1806. vreg_table[0] = curr_slot->vdd_data;
  1807. vreg_table[1] = curr_slot->vdd_io_data;
  1808. }
  1809. #else
  1810. vreg_table[0] = curr_slot->vdd_data;
  1811. vreg_table[1] = curr_slot->vdd_io_data;
  1812. #endif
  1813. for (i = 0; i < ARRAY_SIZE(vreg_table); i++) {
  1814. if (vreg_table[i]) {
  1815. if (enable)
  1816. ret = sdhci_msm_vreg_enable(vreg_table[i]);
  1817. #if defined(CONFIG_SEC_K_PROJECT)
  1818. else {
  1819. ret = sdhci_msm_vreg_disable(vreg_table[i]);
  1820. if (pdata->status_gpio && i == 0)
  1821. mdelay(5);
  1822. }
  1823. #else
  1824. else
  1825. ret = sdhci_msm_vreg_disable(vreg_table[i]);
  1826. #endif
  1827. if (ret)
  1828. goto out;
  1829. }
  1830. }
  1831. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  1832. if(pdata->ls_gpio > 0 && gpio_is_valid(pdata->ls_gpio)) {
  1833. if(enable) {
  1834. gpio_set_value(pdata->ls_gpio, 1);
  1835. udelay(500);
  1836. } else {
  1837. gpio_set_value(pdata->ls_gpio, 0);
  1838. }
  1839. }
  1840. #endif
  1841. out:
  1842. return ret;
  1843. }
  1844. /*
  1845. * Reset vreg by ensuring it is off during probe. A call
  1846. * to enable vreg is needed to balance disable vreg
  1847. */
  1848. static int sdhci_msm_vreg_reset(struct sdhci_msm_pltfm_data *pdata)
  1849. {
  1850. int ret;
  1851. ret = sdhci_msm_setup_vreg(pdata, 1, true);
  1852. if (ret)
  1853. return ret;
  1854. ret = sdhci_msm_setup_vreg(pdata, 0, true);
  1855. return ret;
  1856. }
  1857. /* This init function should be called only once for each SDHC slot */
  1858. static int sdhci_msm_vreg_init(struct device *dev,
  1859. struct sdhci_msm_pltfm_data *pdata,
  1860. bool is_init)
  1861. {
  1862. int ret = 0;
  1863. struct sdhci_msm_slot_reg_data *curr_slot;
  1864. struct sdhci_msm_reg_data *curr_vdd_reg, *curr_vdd_io_reg;
  1865. curr_slot = pdata->vreg_data;
  1866. if (!curr_slot)
  1867. goto out;
  1868. curr_vdd_reg = curr_slot->vdd_data;
  1869. curr_vdd_io_reg = curr_slot->vdd_io_data;
  1870. if (!is_init)
  1871. /* Deregister all regulators from regulator framework */
  1872. goto vdd_io_reg_deinit;
  1873. /*
  1874. * Get the regulator handle from voltage regulator framework
  1875. * and then try to set the voltage level for the regulator
  1876. */
  1877. if (curr_vdd_reg) {
  1878. ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_reg);
  1879. if (ret)
  1880. goto out;
  1881. }
  1882. if (curr_vdd_io_reg) {
  1883. #if defined(CONFIG_ARCH_MSM8974)\
  1884. || defined(CONFIG_MACH_FRESCOLTESKT)||defined(CONFIG_MACH_FRESCOLTEKTT)||defined(CONFIG_MACH_FRESCOLTELGT)
  1885. if (pdata->nonremovable) {
  1886. /* Only for emmc */
  1887. curr_vdd_io_reg->rpm_reg = rpm_regulator_get(
  1888. dev,
  1889. curr_vdd_io_reg->name);
  1890. if (IS_ERR_OR_NULL(curr_vdd_io_reg->rpm_reg)) {
  1891. dev_err(dev, "rpm-reg get: failed: %ld\n",
  1892. PTR_ERR(curr_vdd_io_reg->rpm_reg));
  1893. curr_vdd_io_reg->rpm_reg = NULL;
  1894. } else {
  1895. dev_dbg(dev, "rpm-reg get: success\n");
  1896. }
  1897. } else {
  1898. curr_vdd_io_reg->rpm_reg = NULL;
  1899. ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
  1900. if (ret)
  1901. goto vdd_reg_deinit;
  1902. }
  1903. #else
  1904. ret = sdhci_msm_vreg_init_reg(dev, curr_vdd_io_reg);
  1905. if (ret)
  1906. goto vdd_reg_deinit;
  1907. #endif
  1908. }
  1909. ret = sdhci_msm_vreg_reset(pdata);
  1910. if (ret)
  1911. dev_err(dev, "vreg reset failed (%d)\n", ret);
  1912. goto out;
  1913. vdd_io_reg_deinit:
  1914. if (curr_vdd_io_reg)
  1915. sdhci_msm_vreg_deinit_reg(curr_vdd_io_reg);
  1916. vdd_reg_deinit:
  1917. if (curr_vdd_reg)
  1918. sdhci_msm_vreg_deinit_reg(curr_vdd_reg);
  1919. out:
  1920. return ret;
  1921. }
  1922. static int sdhci_msm_set_vdd_io_vol(struct sdhci_msm_pltfm_data *pdata,
  1923. enum vdd_io_level level,
  1924. unsigned int voltage_level)
  1925. {
  1926. int ret = 0;
  1927. int set_level;
  1928. struct sdhci_msm_reg_data *vdd_io_reg;
  1929. if (!pdata->vreg_data)
  1930. return ret;
  1931. vdd_io_reg = pdata->vreg_data->vdd_io_data;
  1932. if (vdd_io_reg && vdd_io_reg->is_enabled) {
  1933. switch (level) {
  1934. case VDD_IO_LOW:
  1935. set_level = vdd_io_reg->low_vol_level;
  1936. break;
  1937. case VDD_IO_HIGH:
  1938. set_level = vdd_io_reg->high_vol_level;
  1939. break;
  1940. case VDD_IO_SET_LEVEL:
  1941. set_level = voltage_level;
  1942. break;
  1943. default:
  1944. pr_err("%s: invalid argument level = %d",
  1945. __func__, level);
  1946. ret = -EINVAL;
  1947. return ret;
  1948. }
  1949. ret = sdhci_msm_vreg_set_voltage(vdd_io_reg, set_level,
  1950. set_level);
  1951. }
  1952. return ret;
  1953. }
  1954. /*
  1955. * Acquire spin-lock host->lock before calling this function
  1956. */
  1957. static void sdhci_msm_cfg_sdiowakeup_gpio_irq(struct sdhci_host *host,
  1958. bool enable)
  1959. {
  1960. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1961. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1962. if (enable && !msm_host->is_sdiowakeup_enabled)
  1963. enable_irq(msm_host->pdata->sdiowakeup_irq);
  1964. else if (!enable && msm_host->is_sdiowakeup_enabled)
  1965. disable_irq_nosync(msm_host->pdata->sdiowakeup_irq);
  1966. else
  1967. dev_warn(&msm_host->pdev->dev, "%s: wakeup to config: %d curr: %d\n",
  1968. __func__, enable, msm_host->is_sdiowakeup_enabled);
  1969. msm_host->is_sdiowakeup_enabled = enable;
  1970. }
  1971. static irqreturn_t sdhci_msm_sdiowakeup_irq(int irq, void *data)
  1972. {
  1973. struct sdhci_host *host = (struct sdhci_host *)data;
  1974. unsigned long flags;
  1975. pr_debug("%s: irq (%d) received\n", __func__, irq);
  1976. spin_lock_irqsave(&host->lock, flags);
  1977. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
  1978. spin_unlock_irqrestore(&host->lock, flags);
  1979. return IRQ_HANDLED;
  1980. }
  1981. static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
  1982. {
  1983. struct sdhci_host *host = (struct sdhci_host *)data;
  1984. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1985. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  1986. u8 irq_status = 0;
  1987. u8 irq_ack = 0;
  1988. int ret = 0;
  1989. int pwr_state = 0, io_level = 0;
  1990. unsigned long flags;
  1991. irq_status = readb_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
  1992. pr_debug("%s: Received IRQ(%d), status=0x%x\n",
  1993. mmc_hostname(msm_host->mmc), irq, irq_status);
  1994. /* Clear the interrupt */
  1995. writeb_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
  1996. /*
  1997. * SDHC has core_mem and hc_mem device memory and these memory
  1998. * addresses do not fall within 1KB region. Hence, any update to
  1999. * core_mem address space would require an mb() to ensure this gets
  2000. * completed before its next update to registers within hc_mem.
  2001. */
  2002. mb();
  2003. /* Handle BUS ON/OFF*/
  2004. if (irq_status & CORE_PWRCTL_BUS_ON) {
  2005. ret = sdhci_msm_setup_vreg(msm_host->pdata, true, false);
  2006. if (!ret) {
  2007. ret = sdhci_msm_setup_pins(msm_host->pdata, true);
  2008. ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
  2009. VDD_IO_HIGH, 0);
  2010. }
  2011. if (ret)
  2012. irq_ack |= CORE_PWRCTL_BUS_FAIL;
  2013. else
  2014. irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
  2015. pwr_state = REQ_BUS_ON;
  2016. io_level = REQ_IO_HIGH;
  2017. }
  2018. if (irq_status & CORE_PWRCTL_BUS_OFF) {
  2019. ret = sdhci_msm_setup_vreg(msm_host->pdata, false, false);
  2020. if (!ret) {
  2021. ret = sdhci_msm_setup_pins(msm_host->pdata, false);
  2022. ret |= sdhci_msm_set_vdd_io_vol(msm_host->pdata,
  2023. VDD_IO_LOW, 0);
  2024. }
  2025. if (ret)
  2026. irq_ack |= CORE_PWRCTL_BUS_FAIL;
  2027. else
  2028. irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
  2029. pwr_state = REQ_BUS_OFF;
  2030. io_level = REQ_IO_LOW;
  2031. }
  2032. /* Handle IO LOW/HIGH */
  2033. if (irq_status & CORE_PWRCTL_IO_LOW) {
  2034. /* Switch voltage Low */
  2035. ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0);
  2036. if (ret)
  2037. irq_ack |= CORE_PWRCTL_IO_FAIL;
  2038. else
  2039. irq_ack |= CORE_PWRCTL_IO_SUCCESS;
  2040. io_level = REQ_IO_LOW;
  2041. }
  2042. if (irq_status & CORE_PWRCTL_IO_HIGH) {
  2043. /* Switch voltage High */
  2044. ret = sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0);
  2045. if (ret)
  2046. irq_ack |= CORE_PWRCTL_IO_FAIL;
  2047. else
  2048. irq_ack |= CORE_PWRCTL_IO_SUCCESS;
  2049. io_level = REQ_IO_HIGH;
  2050. }
  2051. /* ACK status to the core */
  2052. writeb_relaxed(irq_ack, (msm_host->core_mem + CORE_PWRCTL_CTL));
  2053. /*
  2054. * SDHC has core_mem and hc_mem device memory and these memory
  2055. * addresses do not fall within 1KB region. Hence, any update to
  2056. * core_mem address space would require an mb() to ensure this gets
  2057. * completed before its next update to registers within hc_mem.
  2058. */
  2059. mb();
  2060. if (io_level & REQ_IO_HIGH)
  2061. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
  2062. ~CORE_IO_PAD_PWR_SWITCH),
  2063. host->ioaddr + CORE_VENDOR_SPEC);
  2064. else if (io_level & REQ_IO_LOW)
  2065. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) |
  2066. CORE_IO_PAD_PWR_SWITCH),
  2067. host->ioaddr + CORE_VENDOR_SPEC);
  2068. mb();
  2069. pr_debug("%s: Handled IRQ(%d), ret=%d, ack=0x%x\n",
  2070. mmc_hostname(msm_host->mmc), irq, ret, irq_ack);
  2071. spin_lock_irqsave(&host->lock, flags);
  2072. if (pwr_state)
  2073. msm_host->curr_pwr_state = pwr_state;
  2074. if (io_level)
  2075. msm_host->curr_io_level = io_level;
  2076. complete(&msm_host->pwr_irq_completion);
  2077. spin_unlock_irqrestore(&host->lock, flags);
  2078. return IRQ_HANDLED;
  2079. }
  2080. /* This function returns the max. current supported by VDD rail in mA */
  2081. static unsigned int sdhci_msm_get_vreg_vdd_max_current(struct sdhci_msm_host
  2082. *host)
  2083. {
  2084. struct sdhci_msm_slot_reg_data *curr_slot = host->pdata->vreg_data;
  2085. if (!curr_slot)
  2086. return 0;
  2087. if (curr_slot->vdd_data)
  2088. return curr_slot->vdd_data->hpm_uA / 1000;
  2089. else
  2090. return 0;
  2091. }
  2092. static ssize_t
  2093. show_polling(struct device *dev, struct device_attribute *attr, char *buf)
  2094. {
  2095. struct sdhci_host *host = dev_get_drvdata(dev);
  2096. int poll;
  2097. unsigned long flags;
  2098. spin_lock_irqsave(&host->lock, flags);
  2099. poll = !!(host->mmc->caps & MMC_CAP_NEEDS_POLL);
  2100. spin_unlock_irqrestore(&host->lock, flags);
  2101. return snprintf(buf, PAGE_SIZE, "%d\n", poll);
  2102. }
  2103. static ssize_t
  2104. store_polling(struct device *dev, struct device_attribute *attr,
  2105. const char *buf, size_t count)
  2106. {
  2107. struct sdhci_host *host = dev_get_drvdata(dev);
  2108. int value;
  2109. unsigned long flags;
  2110. if (!kstrtou32(buf, 0, &value)) {
  2111. spin_lock_irqsave(&host->lock, flags);
  2112. if (value) {
  2113. host->mmc->caps |= MMC_CAP_NEEDS_POLL;
  2114. mmc_detect_change(host->mmc, 0);
  2115. } else {
  2116. host->mmc->caps &= ~MMC_CAP_NEEDS_POLL;
  2117. }
  2118. spin_unlock_irqrestore(&host->lock, flags);
  2119. }
  2120. return count;
  2121. }
  2122. static ssize_t
  2123. show_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
  2124. char *buf)
  2125. {
  2126. struct sdhci_host *host = dev_get_drvdata(dev);
  2127. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2128. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2129. return snprintf(buf, PAGE_SIZE, "%u\n",
  2130. msm_host->msm_bus_vote.is_max_bw_needed);
  2131. }
  2132. static ssize_t
  2133. store_sdhci_max_bus_bw(struct device *dev, struct device_attribute *attr,
  2134. const char *buf, size_t count)
  2135. {
  2136. struct sdhci_host *host = dev_get_drvdata(dev);
  2137. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2138. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2139. uint32_t value;
  2140. unsigned long flags;
  2141. if (!kstrtou32(buf, 0, &value)) {
  2142. spin_lock_irqsave(&host->lock, flags);
  2143. msm_host->msm_bus_vote.is_max_bw_needed = !!value;
  2144. spin_unlock_irqrestore(&host->lock, flags);
  2145. }
  2146. return count;
  2147. }
  2148. static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
  2149. {
  2150. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2151. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2152. unsigned long flags;
  2153. bool done = false;
  2154. spin_lock_irqsave(&host->lock, flags);
  2155. pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
  2156. mmc_hostname(host->mmc), __func__, req_type,
  2157. msm_host->curr_pwr_state, msm_host->curr_io_level);
  2158. if ((req_type & msm_host->curr_pwr_state) ||
  2159. (req_type & msm_host->curr_io_level))
  2160. done = true;
  2161. spin_unlock_irqrestore(&host->lock, flags);
  2162. /*
  2163. * 8974pro after HW_GPIO_06 uses SDR50 Mode
  2164. * KLTE uses SDR50 on SDC3 port.
  2165. * Handle I/O voltage switch here if this request is for SDC3.
  2166. * SDC3 Don't use PWR_IRQ.
  2167. */
  2168. #if defined(CONFIG_SEC_K_PROJECT)
  2169. if (strcmp(host->hw_name, "msm_sdcc.3") == 0 && !done && system_rev >= 6) {
  2170. if (req_type == REQ_IO_HIGH) {
  2171. /* Switch voltage High */
  2172. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0)) {
  2173. pr_err("%s: %s: Voltage Switch to High is Fail\n", mmc_hostname(host->mmc), __func__);
  2174. return;
  2175. }
  2176. msm_host->curr_io_level = REQ_IO_HIGH;
  2177. done = true;
  2178. } else if (req_type == REQ_IO_LOW) {
  2179. /* Switch voltage Low */
  2180. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0)) {
  2181. pr_err("%s: %s: Voltage Switch to Low is Fail\n", mmc_hostname(host->mmc), __func__);
  2182. return;
  2183. }
  2184. msm_host->curr_io_level = REQ_IO_LOW;
  2185. done = true;
  2186. }
  2187. }
  2188. #elif defined(CONFIG_SEC_PATEK_PROJECT)
  2189. if (strcmp(host->hw_name, "msm_sdcc.3") == 0 && !done) {
  2190. if (req_type == REQ_IO_HIGH) {
  2191. /* Switch voltage High */
  2192. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_HIGH, 0)) {
  2193. pr_err("%s: %s: Voltage Switch to High is Fail\n", mmc_hostname(host->mmc), __func__);
  2194. return;
  2195. }
  2196. msm_host->curr_io_level = REQ_IO_HIGH;
  2197. done = true;
  2198. } else if (req_type == REQ_IO_LOW) {
  2199. /* Switch voltage Low */
  2200. if (sdhci_msm_set_vdd_io_vol(msm_host->pdata, VDD_IO_LOW, 0)) {
  2201. pr_err("%s: %s: Voltage Switch to Low is Fail\n", mmc_hostname(host->mmc), __func__);
  2202. return;
  2203. }
  2204. msm_host->curr_io_level = REQ_IO_LOW;
  2205. done = true;
  2206. }
  2207. }
  2208. #endif
  2209. /*
  2210. * This is needed here to hanlde a case where IRQ gets
  2211. * triggered even before this function is called so that
  2212. * x->done counter of completion gets reset. Otherwise,
  2213. * next call to wait_for_completion returns immediately
  2214. * without actually waiting for the IRQ to be handled.
  2215. */
  2216. if (done)
  2217. init_completion(&msm_host->pwr_irq_completion);
  2218. else
  2219. wait_for_completion(&msm_host->pwr_irq_completion);
  2220. pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
  2221. __func__, req_type);
  2222. }
  2223. static void sdhci_msm_toggle_cdr(struct sdhci_host *host, bool enable)
  2224. {
  2225. if (enable)
  2226. writel_relaxed((readl_relaxed(host->ioaddr +
  2227. CORE_DLL_CONFIG) | CORE_CDR_EN),
  2228. host->ioaddr + CORE_DLL_CONFIG);
  2229. else
  2230. writel_relaxed((readl_relaxed(host->ioaddr +
  2231. CORE_DLL_CONFIG) & ~CORE_CDR_EN),
  2232. host->ioaddr + CORE_DLL_CONFIG);
  2233. }
  2234. static unsigned int sdhci_msm_max_segs(void)
  2235. {
  2236. return SDHCI_MSM_MAX_SEGMENTS / 16;
  2237. }
  2238. static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
  2239. {
  2240. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2241. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2242. return msm_host->pdata->sup_clk_table[0];
  2243. }
  2244. static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
  2245. {
  2246. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2247. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2248. int max_clk_index = msm_host->pdata->sup_clk_cnt;
  2249. return msm_host->pdata->sup_clk_table[max_clk_index - 1];
  2250. }
  2251. static unsigned int sdhci_msm_get_sup_clk_rate(struct sdhci_host *host,
  2252. u32 req_clk)
  2253. {
  2254. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2255. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2256. unsigned int sel_clk = -1;
  2257. unsigned char cnt;
  2258. if (req_clk < sdhci_msm_get_min_clock(host)) {
  2259. sel_clk = sdhci_msm_get_min_clock(host);
  2260. return sel_clk;
  2261. }
  2262. for (cnt = 0; cnt < msm_host->pdata->sup_clk_cnt; cnt++) {
  2263. if (msm_host->pdata->sup_clk_table[cnt] > req_clk) {
  2264. break;
  2265. } else if (msm_host->pdata->sup_clk_table[cnt] == req_clk) {
  2266. sel_clk = msm_host->pdata->sup_clk_table[cnt];
  2267. break;
  2268. } else {
  2269. sel_clk = msm_host->pdata->sup_clk_table[cnt];
  2270. }
  2271. }
  2272. return sel_clk;
  2273. }
  2274. static int sdhci_msm_enable_controller_clock(struct sdhci_host *host)
  2275. {
  2276. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2277. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2278. int rc = 0;
  2279. if (atomic_read(&msm_host->controller_clock))
  2280. return 0;
  2281. sdhci_msm_bus_voting(host, 1);
  2282. if (!IS_ERR(msm_host->pclk)) {
  2283. rc = clk_prepare_enable(msm_host->pclk);
  2284. if (rc) {
  2285. pr_err("%s: %s: failed to enable the pclk with error %d\n",
  2286. mmc_hostname(host->mmc), __func__, rc);
  2287. goto remove_vote;
  2288. }
  2289. }
  2290. rc = clk_prepare_enable(msm_host->clk);
  2291. if (rc) {
  2292. pr_err("%s: %s: failed to enable the host->clk with error %d\n",
  2293. mmc_hostname(host->mmc), __func__, rc);
  2294. goto disable_pclk;
  2295. }
  2296. atomic_set(&msm_host->controller_clock, 1);
  2297. pr_debug("%s: %s: enabled controller clock\n",
  2298. mmc_hostname(host->mmc), __func__);
  2299. goto out;
  2300. disable_pclk:
  2301. if (!IS_ERR(msm_host->pclk))
  2302. clk_disable_unprepare(msm_host->pclk);
  2303. remove_vote:
  2304. if (msm_host->msm_bus_vote.client_handle)
  2305. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  2306. out:
  2307. return rc;
  2308. }
  2309. static int sdhci_msm_prepare_clocks(struct sdhci_host *host, bool enable)
  2310. {
  2311. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2312. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2313. int rc = 0;
  2314. if (enable && !atomic_read(&msm_host->clks_on)) {
  2315. pr_debug("%s: request to enable clocks\n",
  2316. mmc_hostname(host->mmc));
  2317. /*
  2318. * The bus-width or the clock rate might have changed
  2319. * after controller clocks are enabled, update bus vote
  2320. * in such case.
  2321. */
  2322. if (atomic_read(&msm_host->controller_clock))
  2323. sdhci_msm_bus_voting(host, 1);
  2324. rc = sdhci_msm_enable_controller_clock(host);
  2325. if (rc)
  2326. goto remove_vote;
  2327. if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
  2328. rc = clk_prepare_enable(msm_host->bus_clk);
  2329. if (rc) {
  2330. pr_err("%s: %s: failed to enable the bus-clock with error %d\n",
  2331. mmc_hostname(host->mmc), __func__, rc);
  2332. goto disable_controller_clk;
  2333. }
  2334. }
  2335. if (!IS_ERR(msm_host->ff_clk)) {
  2336. rc = clk_prepare_enable(msm_host->ff_clk);
  2337. if (rc) {
  2338. pr_err("%s: %s: failed to enable the ff_clk with error %d\n",
  2339. mmc_hostname(host->mmc), __func__, rc);
  2340. goto disable_bus_clk;
  2341. }
  2342. }
  2343. if (!IS_ERR(msm_host->sleep_clk)) {
  2344. rc = clk_prepare_enable(msm_host->sleep_clk);
  2345. if (rc) {
  2346. pr_err("%s: %s: failed to enable the sleep_clk with error %d\n",
  2347. mmc_hostname(host->mmc), __func__, rc);
  2348. goto disable_ff_clk;
  2349. }
  2350. }
  2351. mb();
  2352. } else if (!enable && atomic_read(&msm_host->clks_on)) {
  2353. pr_debug("%s: request to disable clocks\n",
  2354. mmc_hostname(host->mmc));
  2355. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  2356. mb();
  2357. if (!IS_ERR_OR_NULL(msm_host->sleep_clk))
  2358. clk_disable_unprepare(msm_host->sleep_clk);
  2359. if (!IS_ERR_OR_NULL(msm_host->ff_clk))
  2360. clk_disable_unprepare(msm_host->ff_clk);
  2361. clk_disable_unprepare(msm_host->clk);
  2362. if (!IS_ERR(msm_host->pclk))
  2363. clk_disable_unprepare(msm_host->pclk);
  2364. if (!IS_ERR_OR_NULL(msm_host->bus_clk))
  2365. clk_disable_unprepare(msm_host->bus_clk);
  2366. atomic_set(&msm_host->controller_clock, 0);
  2367. sdhci_msm_bus_voting(host, 0);
  2368. }
  2369. atomic_set(&msm_host->clks_on, enable);
  2370. goto out;
  2371. disable_ff_clk:
  2372. if (!IS_ERR_OR_NULL(msm_host->ff_clk))
  2373. clk_disable_unprepare(msm_host->ff_clk);
  2374. disable_bus_clk:
  2375. if (!IS_ERR_OR_NULL(msm_host->bus_clk))
  2376. clk_disable_unprepare(msm_host->bus_clk);
  2377. disable_controller_clk:
  2378. if (!IS_ERR_OR_NULL(msm_host->clk))
  2379. clk_disable_unprepare(msm_host->clk);
  2380. if (!IS_ERR_OR_NULL(msm_host->pclk))
  2381. clk_disable_unprepare(msm_host->pclk);
  2382. atomic_set(&msm_host->controller_clock, 0);
  2383. remove_vote:
  2384. if (msm_host->msm_bus_vote.client_handle)
  2385. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  2386. out:
  2387. return rc;
  2388. }
  2389. static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
  2390. {
  2391. int rc;
  2392. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2393. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2394. struct mmc_ios curr_ios = host->mmc->ios;
  2395. u32 sup_clock, ddr_clock;
  2396. bool curr_pwrsave;
  2397. if (!clock) {
  2398. /*
  2399. * disable pwrsave to ensure clock is not auto-gated until
  2400. * the rate is >400KHz (initialization complete).
  2401. */
  2402. writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
  2403. ~CORE_CLK_PWRSAVE, host->ioaddr + CORE_VENDOR_SPEC);
  2404. sdhci_msm_prepare_clocks(host, false);
  2405. host->clock = clock;
  2406. return;
  2407. }
  2408. rc = sdhci_msm_prepare_clocks(host, true);
  2409. if (rc)
  2410. return;
  2411. if (msm_host->id == 1) { /* power save mode : eMMC only */
  2412. curr_pwrsave = !!(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC) &
  2413. CORE_CLK_PWRSAVE);
  2414. if ((msm_host->clk_rate > 400000) &&
  2415. !curr_pwrsave && mmc_host_may_gate_card(host->mmc->card))
  2416. writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2417. | CORE_CLK_PWRSAVE,
  2418. host->ioaddr + CORE_VENDOR_SPEC);
  2419. /*
  2420. * Disable pwrsave for a newly added card if doesn't allow clock
  2421. * gating.
  2422. */
  2423. else if (curr_pwrsave && !mmc_host_may_gate_card(host->mmc->card))
  2424. writel_relaxed(readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2425. & ~CORE_CLK_PWRSAVE,
  2426. host->ioaddr + CORE_VENDOR_SPEC);
  2427. }
  2428. sup_clock = sdhci_msm_get_sup_clk_rate(host, clock);
  2429. if ((curr_ios.timing == MMC_TIMING_UHS_DDR50) ||
  2430. (curr_ios.timing == MMC_TIMING_MMC_HS400)) {
  2431. /*
  2432. * The SDHC requires internal clock frequency to be double the
  2433. * actual clock that will be set for DDR mode. The controller
  2434. * uses the faster clock(100/400MHz) for some of its parts and
  2435. * send the actual required clock (50/200MHz) to the card.
  2436. */
  2437. ddr_clock = clock * 2;
  2438. sup_clock = sdhci_msm_get_sup_clk_rate(host,
  2439. ddr_clock);
  2440. }
  2441. /*
  2442. * In general all timing modes are controlled via UHS mode select in
  2443. * Host Control2 register. eMMC specific HS200/HS400 doesn't have
  2444. * their respective modes defined here, hence we use these values.
  2445. *
  2446. * HS200 - SDR104 (Since they both are equivalent in functionality)
  2447. * HS400 - This involves multiple configurations
  2448. * Initially SDR104 - when tuning is required as HS200
  2449. * Then when switching to DDR @ 400MHz (HS400) we use
  2450. * the vendor specific HC_SELECT_IN to control the mode.
  2451. *
  2452. * In addition to controlling the modes we also need to select the
  2453. * correct input clock for DLL depending on the mode.
  2454. *
  2455. * HS400 - divided clock (free running MCLK/2)
  2456. * All other modes - default (free running MCLK)
  2457. */
  2458. if (curr_ios.timing == MMC_TIMING_MMC_HS400) {
  2459. /* Select the divided clock (free running MCLK/2) */
  2460. writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2461. & ~CORE_HC_MCLK_SEL_MASK)
  2462. | CORE_HC_MCLK_SEL_HS400),
  2463. host->ioaddr + CORE_VENDOR_SPEC);
  2464. /*
  2465. * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
  2466. * register
  2467. */
  2468. if (msm_host->tuning_done && !msm_host->calibration_done) {
  2469. /*
  2470. * Write 0x6 to HC_SELECT_IN and 1 to HC_SELECT_IN_EN
  2471. * field in VENDOR_SPEC_FUNC
  2472. */
  2473. writel_relaxed((readl_relaxed(host->ioaddr + \
  2474. CORE_VENDOR_SPEC)
  2475. | CORE_HC_SELECT_IN_HS400
  2476. | CORE_HC_SELECT_IN_EN),
  2477. host->ioaddr + CORE_VENDOR_SPEC);
  2478. }
  2479. } else {
  2480. /* Select the default clock (free running MCLK) */
  2481. writel_relaxed(((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2482. & ~CORE_HC_MCLK_SEL_MASK)
  2483. | CORE_HC_MCLK_SEL_DFLT),
  2484. host->ioaddr + CORE_VENDOR_SPEC);
  2485. /*
  2486. * Disable HC_SELECT_IN to be able to use the UHS mode select
  2487. * configuration from Host Control2 register for all other
  2488. * modes.
  2489. *
  2490. * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
  2491. * in VENDOR_SPEC_FUNC
  2492. */
  2493. writel_relaxed((readl_relaxed(host->ioaddr + CORE_VENDOR_SPEC)
  2494. & ~CORE_HC_SELECT_IN_EN
  2495. & ~CORE_HC_SELECT_IN_MASK),
  2496. host->ioaddr + CORE_VENDOR_SPEC);
  2497. }
  2498. mb();
  2499. if (sup_clock != msm_host->clk_rate) {
  2500. pr_debug("%s: %s: setting clk rate to %u\n",
  2501. mmc_hostname(host->mmc), __func__, sup_clock);
  2502. rc = clk_set_rate(msm_host->clk, sup_clock);
  2503. if (rc) {
  2504. pr_err("%s: %s: Failed to set rate %u for host-clk : %d\n",
  2505. mmc_hostname(host->mmc), __func__,
  2506. sup_clock, rc);
  2507. return;
  2508. }
  2509. msm_host->clk_rate = sup_clock;
  2510. host->clock = clock;
  2511. /*
  2512. * Update the bus vote in case of frequency change due to
  2513. * clock scaling.
  2514. */
  2515. sdhci_msm_bus_voting(host, 1);
  2516. }
  2517. }
  2518. static int sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
  2519. unsigned int uhs)
  2520. {
  2521. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2522. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2523. u16 ctrl_2;
  2524. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  2525. /* Select Bus Speed Mode for host */
  2526. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  2527. if (uhs == MMC_TIMING_MMC_HS400)
  2528. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  2529. else if (uhs == MMC_TIMING_MMC_HS200)
  2530. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  2531. else if (uhs == MMC_TIMING_UHS_SDR12)
  2532. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  2533. else if (uhs == MMC_TIMING_UHS_SDR25)
  2534. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  2535. else if (uhs == MMC_TIMING_UHS_SDR50)
  2536. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  2537. else if (uhs == MMC_TIMING_UHS_SDR104)
  2538. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  2539. else if (uhs == MMC_TIMING_UHS_DDR50)
  2540. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  2541. /*
  2542. * When clock frquency is less than 100MHz, the feedback clock must be
  2543. * provided and DLL must not be used so that tuning can be skipped. To
  2544. * provide feedback clock, the mode selection can be any value less
  2545. * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
  2546. */
  2547. if (host->clock <= CORE_FREQ_100MHZ) {
  2548. if ((uhs == MMC_TIMING_MMC_HS400) ||
  2549. (uhs == MMC_TIMING_MMC_HS200) ||
  2550. (uhs == MMC_TIMING_UHS_SDR104))
  2551. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  2552. /*
  2553. * Make sure DLL is disabled when not required
  2554. *
  2555. * Write 1 to DLL_RST bit of DLL_CONFIG register
  2556. */
  2557. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  2558. | CORE_DLL_RST),
  2559. host->ioaddr + CORE_DLL_CONFIG);
  2560. /* Write 1 to DLL_PDN bit of DLL_CONFIG register */
  2561. writel_relaxed((readl_relaxed(host->ioaddr + CORE_DLL_CONFIG)
  2562. | CORE_DLL_PDN),
  2563. host->ioaddr + CORE_DLL_CONFIG);
  2564. mb();
  2565. /*
  2566. * The DLL needs to be restored and CDCLP533 recalibrated
  2567. * when the clock frequency is set back to 400MHz.
  2568. */
  2569. msm_host->calibration_done = false;
  2570. }
  2571. pr_debug("%s: %s-clock:%u uhs mode:%u ctrl_2:0x%x\n",
  2572. mmc_hostname(host->mmc), __func__, host->clock, uhs, ctrl_2);
  2573. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  2574. return 0;
  2575. }
  2576. /*
  2577. * sdhci_msm_disable_data_xfer - disable undergoing AHB bus data transfer
  2578. *
  2579. * Write 0 to bit 0 in MCI_DATA_CTL (offset 0x2C) - clearing TxActive bit by
  2580. * access to legacy registers. It will stop current burst and prevent start of
  2581. * the next on.
  2582. *
  2583. * Polling CORE_AHB_DATA_DELAY_US timeout, by reading bit 13:12 until they are 0
  2584. * in CORE_SDCC_DEBUG_REG (offset 0x124) will validate that AHB burst was
  2585. * completed and a new one didn't start.
  2586. *
  2587. * Waiting for 4us while AHB finishes descriptors fetch.
  2588. */
  2589. static void sdhci_msm_disable_data_xfer(struct sdhci_host *host)
  2590. {
  2591. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2592. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2593. u32 value;
  2594. int ret;
  2595. u32 version;
  2596. version = readl_relaxed(msm_host->core_mem + CORE_MCI_VERSION);
  2597. /* Core version 3.1.0 doesn't need this workaround */
  2598. if (version == CORE_VERSION_310)
  2599. return;
  2600. value = readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CTRL);
  2601. value &= ~(u32)CORE_MCI_DPSM_ENABLE;
  2602. writel_relaxed(value, msm_host->core_mem + CORE_MCI_DATA_CTRL);
  2603. /* Enable the test bus for device slot */
  2604. writel_relaxed(CORE_TESTBUS_ENA | CORE_TESTBUS_SEL2,
  2605. msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2606. ret = readl_poll_timeout_noirq(msm_host->core_mem
  2607. + CORE_SDCC_DEBUG_REG, value,
  2608. !(value & CORE_DEBUG_REG_AHB_HTRANS),
  2609. CORE_AHB_DATA_DELAY_US, 1);
  2610. if (ret) {
  2611. pr_err("%s: %s: can't stop ongoing AHB bus access by ADMA\n",
  2612. mmc_hostname(host->mmc), __func__);
  2613. BUG();
  2614. }
  2615. /* Disable the test bus for device slot */
  2616. value = readl_relaxed(msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2617. value &= ~CORE_TESTBUS_ENA;
  2618. writel_relaxed(value, msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2619. udelay(CORE_AHB_DESC_DELAY_US);
  2620. }
  2621. void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
  2622. {
  2623. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2624. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2625. int i, j, index = 0;
  2626. u32 debug_reg[20] = {0};
  2627. pr_info("----------- VENDOR REGISTER DUMP -----------\n");
  2628. pr_info("Data cnt: 0x%08x | Fifo cnt: 0x%08x | Int sts: 0x%08x\n",
  2629. readl_relaxed(msm_host->core_mem + CORE_MCI_DATA_CNT),
  2630. readl_relaxed(msm_host->core_mem + CORE_MCI_FIFO_CNT),
  2631. readl_relaxed(msm_host->core_mem + CORE_MCI_STATUS));
  2632. pr_info("DLL cfg: 0x%08x | DLL sts: 0x%08x\n",
  2633. readl_relaxed(host->ioaddr + CORE_DLL_CONFIG),
  2634. readl_relaxed(host->ioaddr + CORE_DLL_STATUS));
  2635. /* Enable test bus */
  2636. for (j = 0; j < 3; j++) {
  2637. for (i = 8; i < 16; i++) {
  2638. if (j == 2 && i > 9)
  2639. break;
  2640. writel_relaxed((j << CORE_TESTBUS_SEL2_BIT) |
  2641. (i & CORE_TESTBUS_MASK),
  2642. msm_host->core_mem + CORE_TESTBUS_CONFIG);
  2643. debug_reg[index++] = readl_relaxed(msm_host->core_mem +
  2644. CORE_SDCC_DEBUG_REG);
  2645. }
  2646. }
  2647. for (i = 0; i < 20; i = i + 4)
  2648. pr_info(" Test bus[%d to %d]: 0x%08x 0x%08x 0x%08x 0x%08x\n",
  2649. i, i + 3, debug_reg[i], debug_reg[i+1],
  2650. debug_reg[i+2], debug_reg[i+3]);
  2651. /* Disable test bus */
  2652. writel_relaxed(~CORE_TESTBUS_ENA, msm_host->core_mem +
  2653. CORE_TESTBUS_CONFIG);
  2654. }
  2655. static struct sdhci_ops sdhci_msm_ops = {
  2656. .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
  2657. .check_power_status = sdhci_msm_check_power_status,
  2658. .execute_tuning = sdhci_msm_execute_tuning,
  2659. .toggle_cdr = sdhci_msm_toggle_cdr,
  2660. .get_max_segments = sdhci_msm_max_segs,
  2661. .set_clock = sdhci_msm_set_clock,
  2662. .get_min_clock = sdhci_msm_get_min_clock,
  2663. .get_max_clock = sdhci_msm_get_max_clock,
  2664. .disable_data_xfer = sdhci_msm_disable_data_xfer,
  2665. .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
  2666. .enable_controller_clock = sdhci_msm_enable_controller_clock,
  2667. };
  2668. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  2669. static void mmc_enable_ls_gpio(struct sdhci_host *host, unsigned int gpio_no)
  2670. {
  2671. int status;
  2672. if (!gpio_is_valid(gpio_no))
  2673. return;
  2674. status = gpio_request_one(gpio_no, GPIOF_DIR_OUT, "LS_EN");
  2675. if (status)
  2676. pr_err("%s: %s: gpio_request(%d) failed\n",
  2677. mmc_hostname(host->mmc), __func__, gpio_no);
  2678. }
  2679. #endif
  2680. static int sdhci_msm_cfg_mpm_pin_wakeup(struct sdhci_host *host, unsigned mode)
  2681. {
  2682. int ret = 0;
  2683. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2684. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  2685. unsigned int pin = msm_host->pdata->mpm_sdiowakeup_int;
  2686. if (!pin)
  2687. return 0;
  2688. switch (mode) {
  2689. case SDC_DAT1_DISABLE:
  2690. ret = msm_mpm_enable_pin(pin, 0);
  2691. break;
  2692. case SDC_DAT1_ENABLE:
  2693. ret = msm_mpm_set_pin_type(pin, IRQ_TYPE_LEVEL_LOW);
  2694. if (!ret)
  2695. ret = msm_mpm_enable_pin(pin, 1);
  2696. break;
  2697. case SDC_DAT1_ENWAKE:
  2698. ret = msm_mpm_set_pin_wake(pin, 1);
  2699. break;
  2700. case SDC_DAT1_DISWAKE:
  2701. ret = msm_mpm_set_pin_wake(pin, 0);
  2702. break;
  2703. default:
  2704. ret = -EINVAL;
  2705. break;
  2706. }
  2707. return ret;
  2708. }
  2709. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  2710. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  2711. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  2712. static void sdhci_msm_status_notify(int card_present, void *dev_id)
  2713. {
  2714. struct sdhci_host *host = (struct sdhci_host *)dev_id;
  2715. unsigned long flags;
  2716. if (host) {
  2717. host->mmc->rescan_disable=0;
  2718. pr_err("%s: %s: rescan_disable : %d\n",mmc_hostname(host->mmc),
  2719. __func__, host->mmc->rescan_disable);
  2720. spin_lock_irqsave(&host->lock, flags);
  2721. if (card_present) {
  2722. pr_err("%s: card inserted.\n", mmc_hostname(host->mmc));
  2723. host->flags &= ~SDHCI_DEVICE_DEAD;
  2724. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  2725. } else {
  2726. pr_err("%s: card removed.\n", mmc_hostname(host->mmc));
  2727. host->flags |= SDHCI_DEVICE_DEAD;
  2728. host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  2729. }
  2730. tasklet_schedule(&host->card_tasklet);
  2731. spin_unlock_irqrestore(&host->lock, flags);
  2732. }
  2733. }
  2734. #endif
  2735. /* SYSFS about SD Card Detection */
  2736. extern struct class *sec_class;
  2737. static struct device *t_flash_detect_dev;
  2738. static ssize_t t_flash_detect_show(struct device *dev,
  2739. struct device_attribute *attr, char *buf)
  2740. {
  2741. struct sdhci_msm_host *msm_host = dev_get_drvdata(dev);
  2742. #if defined(CONFIG_MACH_SERRANO) || defined(CONFIG_SEC_ATLANTIC_PROJECT)
  2743. if (msm_host->mmc->card) {
  2744. printk(KERN_DEBUG "External sd: card inserted.\n");
  2745. return sprintf(buf, "Insert\n");
  2746. } else {
  2747. printk(KERN_DEBUG "External sd: card removed.\n");
  2748. return sprintf(buf, "Remove\n");
  2749. }
  2750. #else
  2751. unsigned int detect;
  2752. if (gpio_is_valid(msm_host->pdata->status_gpio))
  2753. detect = gpio_get_value(msm_host->pdata->status_gpio);
  2754. else {
  2755. pr_info("%s : External SD detect pin Error\n", __func__);
  2756. return sprintf(buf, "Error\n");
  2757. }
  2758. pr_info("%s : detect = %d.\n", __func__, detect);
  2759. if (!detect) {
  2760. printk(KERN_DEBUG "External sd: card inserted.\n");
  2761. return sprintf(buf, "Insert\n");
  2762. } else {
  2763. printk(KERN_DEBUG "External sd: card removed.\n");
  2764. return sprintf(buf, "Remove\n");
  2765. }
  2766. #endif
  2767. }
  2768. static DEVICE_ATTR(status, 0444, t_flash_detect_show, NULL);
  2769. static int __devinit sdhci_msm_probe(struct platform_device *pdev)
  2770. {
  2771. struct sdhci_host *host;
  2772. struct sdhci_pltfm_host *pltfm_host;
  2773. struct sdhci_msm_host *msm_host;
  2774. struct resource *core_memres = NULL;
  2775. int ret = 0, dead = 0;
  2776. u32 vdd_max_current;
  2777. u16 host_version;
  2778. u32 pwr, irq_status, irq_ctl;
  2779. unsigned long flags;
  2780. pr_debug("%s: Enter %s\n", dev_name(&pdev->dev), __func__);
  2781. msm_host = devm_kzalloc(&pdev->dev, sizeof(struct sdhci_msm_host),
  2782. GFP_KERNEL);
  2783. if (!msm_host) {
  2784. ret = -ENOMEM;
  2785. goto out;
  2786. }
  2787. msm_host->sdhci_msm_pdata.ops = &sdhci_msm_ops;
  2788. host = sdhci_pltfm_init(pdev, &msm_host->sdhci_msm_pdata);
  2789. if (IS_ERR(host)) {
  2790. ret = PTR_ERR(host);
  2791. goto out;
  2792. }
  2793. pltfm_host = sdhci_priv(host);
  2794. pltfm_host->priv = msm_host;
  2795. msm_host->mmc = host->mmc;
  2796. msm_host->pdev = pdev;
  2797. /* Extract platform data */
  2798. if (pdev->dev.of_node) {
  2799. ret = of_alias_get_id(pdev->dev.of_node, "sdhc");
  2800. if (ret < 0) {
  2801. dev_err(&pdev->dev, "Failed to get slot index %d\n",
  2802. ret);
  2803. goto pltfm_free;
  2804. }
  2805. msm_host->id = ret;
  2806. if (disable_slots & (1 << (ret - 1))) {
  2807. dev_info(&pdev->dev, "%s: Slot %d disabled\n", __func__,
  2808. ret);
  2809. ret = -ENODEV;
  2810. goto pltfm_free;
  2811. }
  2812. msm_host->pdata = sdhci_msm_populate_pdata(&pdev->dev, host);
  2813. if (!msm_host->pdata) {
  2814. dev_err(&pdev->dev, "DT parsing error\n");
  2815. goto pltfm_free;
  2816. }
  2817. } else {
  2818. dev_err(&pdev->dev, "No device tree node\n");
  2819. goto pltfm_free;
  2820. }
  2821. /* Setup Clocks */
  2822. /* Setup SDCC bus voter clock. */
  2823. msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
  2824. if (!IS_ERR_OR_NULL(msm_host->bus_clk)) {
  2825. /* Vote for max. clk rate for max. performance */
  2826. ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
  2827. if (ret)
  2828. goto pltfm_free;
  2829. ret = clk_prepare_enable(msm_host->bus_clk);
  2830. if (ret)
  2831. goto pltfm_free;
  2832. }
  2833. /* Setup main peripheral bus clock */
  2834. msm_host->pclk = devm_clk_get(&pdev->dev, "iface_clk");
  2835. if (!IS_ERR(msm_host->pclk)) {
  2836. ret = clk_prepare_enable(msm_host->pclk);
  2837. if (ret)
  2838. goto bus_clk_disable;
  2839. }
  2840. atomic_set(&msm_host->controller_clock, 1);
  2841. /* Setup SDC MMC clock */
  2842. msm_host->clk = devm_clk_get(&pdev->dev, "core_clk");
  2843. if (IS_ERR(msm_host->clk)) {
  2844. ret = PTR_ERR(msm_host->clk);
  2845. goto pclk_disable;
  2846. }
  2847. /* Set to the minimum supported clock frequency */
  2848. ret = clk_set_rate(msm_host->clk, sdhci_msm_get_min_clock(host));
  2849. if (ret) {
  2850. dev_err(&pdev->dev, "MClk rate set failed (%d)\n", ret);
  2851. goto pclk_disable;
  2852. }
  2853. ret = clk_prepare_enable(msm_host->clk);
  2854. if (ret)
  2855. goto pclk_disable;
  2856. msm_host->clk_rate = sdhci_msm_get_min_clock(host);
  2857. atomic_set(&msm_host->clks_on, 1);
  2858. /* Setup CDC calibration fixed feedback clock */
  2859. msm_host->ff_clk = devm_clk_get(&pdev->dev, "cal_clk");
  2860. if (!IS_ERR(msm_host->ff_clk)) {
  2861. ret = clk_prepare_enable(msm_host->ff_clk);
  2862. if (ret)
  2863. goto clk_disable;
  2864. }
  2865. /* Setup CDC calibration sleep clock */
  2866. msm_host->sleep_clk = devm_clk_get(&pdev->dev, "sleep_clk");
  2867. if (!IS_ERR(msm_host->sleep_clk)) {
  2868. ret = clk_prepare_enable(msm_host->sleep_clk);
  2869. if (ret)
  2870. goto ff_clk_disable;
  2871. }
  2872. msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
  2873. ret = sdhci_msm_bus_register(msm_host, pdev);
  2874. if (ret)
  2875. goto sleep_clk_disable;
  2876. if (msm_host->msm_bus_vote.client_handle)
  2877. INIT_DELAYED_WORK(&msm_host->msm_bus_vote.vote_work,
  2878. sdhci_msm_bus_work);
  2879. sdhci_msm_bus_voting(host, 1);
  2880. /* Setup regulators */
  2881. ret = sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, true);
  2882. if (ret) {
  2883. dev_err(&pdev->dev, "Regulator setup failed (%d)\n", ret);
  2884. goto bus_unregister;
  2885. }
  2886. /* Reset the core and Enable SDHC mode */
  2887. core_memres = platform_get_resource_byname(pdev,
  2888. IORESOURCE_MEM, "core_mem");
  2889. msm_host->core_mem = devm_ioremap(&pdev->dev, core_memres->start,
  2890. resource_size(core_memres));
  2891. if (!msm_host->core_mem) {
  2892. dev_err(&pdev->dev, "Failed to remap registers\n");
  2893. ret = -ENOMEM;
  2894. goto vreg_deinit;
  2895. }
  2896. /* Unset HC_MODE_EN bit in HC_MODE register */
  2897. writel_relaxed(0, (msm_host->core_mem + CORE_HC_MODE));
  2898. /* Set SW_RST bit in POWER register (Offset 0x0) */
  2899. writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_POWER) |
  2900. CORE_SW_RST, msm_host->core_mem + CORE_POWER);
  2901. /*
  2902. * SW reset can take upto 10HCLK + 15MCLK cycles.
  2903. * Calculating based on min clk rates (hclk = 27MHz,
  2904. * mclk = 400KHz) it comes to ~40us. Let's poll for
  2905. * max. 1ms for reset completion.
  2906. */
  2907. ret = readl_poll_timeout(msm_host->core_mem + CORE_POWER,
  2908. pwr, !(pwr & CORE_SW_RST), 10, 1000);
  2909. if (ret) {
  2910. dev_err(&pdev->dev, "reset failed (%d)\n", ret);
  2911. goto vreg_deinit;
  2912. }
  2913. /* Set HC_MODE_EN bit in HC_MODE register */
  2914. writel_relaxed(HC_MODE_EN, (msm_host->core_mem + CORE_HC_MODE));
  2915. /* Set FF_CLK_SW_RST_DIS bit in HC_MODE register */
  2916. writel_relaxed(readl_relaxed(msm_host->core_mem + CORE_HC_MODE) |
  2917. FF_CLK_SW_RST_DIS, msm_host->core_mem + CORE_HC_MODE);
  2918. /*
  2919. * CORE_SW_RST above may trigger power irq if previous status of PWRCTL
  2920. * was either BUS_ON or IO_HIGH_V. So before we enable the power irq
  2921. * interrupt in GIC (by registering the interrupt handler), we need to
  2922. * ensure that any pending power irq interrupt status is acknowledged
  2923. * otherwise power irq interrupt handler would be fired prematurely.
  2924. */
  2925. irq_status = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_STATUS);
  2926. writel_relaxed(irq_status, (msm_host->core_mem + CORE_PWRCTL_CLEAR));
  2927. irq_ctl = readl_relaxed(msm_host->core_mem + CORE_PWRCTL_CTL);
  2928. if (irq_status & (CORE_PWRCTL_BUS_ON | CORE_PWRCTL_BUS_OFF))
  2929. irq_ctl |= CORE_PWRCTL_BUS_SUCCESS;
  2930. if (irq_status & (CORE_PWRCTL_IO_HIGH | CORE_PWRCTL_IO_LOW))
  2931. irq_ctl |= CORE_PWRCTL_IO_SUCCESS;
  2932. writel_relaxed(irq_ctl, (msm_host->core_mem + CORE_PWRCTL_CTL));
  2933. /*
  2934. * Ensure that above writes are propogated before interrupt enablement
  2935. * in GIC.
  2936. */
  2937. mb();
  2938. /*
  2939. * Following are the deviations from SDHC spec v3.0 -
  2940. * 1. Card detection is handled using separate GPIO.
  2941. * 2. Bus power control is handled by interacting with PMIC.
  2942. */
  2943. host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
  2944. host->quirks |= SDHCI_QUIRK_SINGLE_POWER_WRITE;
  2945. host->quirks |= SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN;
  2946. host->quirks2 |= SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK;
  2947. host->quirks2 |= SDHCI_QUIRK2_USE_MAX_DISCARD_SIZE;
  2948. host->quirks2 |= SDHCI_QUIRK2_IGNORE_DATATOUT_FOR_R1BCMD;
  2949. host->quirks2 |= SDHCI_QUIRK2_BROKEN_PRESET_VALUE;
  2950. host->quirks2 |= SDHCI_QUIRK2_USE_RESERVED_MAX_TIMEOUT;
  2951. if (host->quirks2 & SDHCI_QUIRK2_ALWAYS_USE_BASE_CLOCK)
  2952. host->quirks2 |= SDHCI_QUIRK2_DIVIDE_TOUT_BY_4;
  2953. host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
  2954. dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
  2955. host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
  2956. SDHCI_VENDOR_VER_SHIFT));
  2957. if (((host_version & SDHCI_VENDOR_VER_MASK) >>
  2958. SDHCI_VENDOR_VER_SHIFT) == SDHCI_VER_100) {
  2959. /*
  2960. * Add 40us delay in interrupt handler when
  2961. * operating at initialization frequency(400KHz).
  2962. */
  2963. host->quirks2 |= SDHCI_QUIRK2_SLOW_INT_CLR;
  2964. /*
  2965. * Set Software Reset for DAT line in Software
  2966. * Reset Register (Bit 2).
  2967. */
  2968. host->quirks2 |= SDHCI_QUIRK2_RDWR_TX_ACTIVE_EOT;
  2969. }
  2970. host->quirks2 |= SDHCI_QUIRK2_IGN_DATA_END_BIT_ERROR;
  2971. /* Setup PWRCTL irq */
  2972. msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
  2973. if (msm_host->pwr_irq < 0) {
  2974. dev_err(&pdev->dev, "Failed to get pwr_irq by name (%d)\n",
  2975. msm_host->pwr_irq);
  2976. goto vreg_deinit;
  2977. }
  2978. ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
  2979. sdhci_msm_pwr_irq, IRQF_ONESHOT,
  2980. dev_name(&pdev->dev), host);
  2981. if (ret) {
  2982. dev_err(&pdev->dev, "Request threaded irq(%d) failed (%d)\n",
  2983. msm_host->pwr_irq, ret);
  2984. goto vreg_deinit;
  2985. }
  2986. /* Enable pwr irq interrupts */
  2987. writel_relaxed(INT_MASK, (msm_host->core_mem + CORE_PWRCTL_MASK));
  2988. /* Set clock gating delay to be used when CONFIG_MMC_CLKGATE is set */
  2989. msm_host->mmc->clkgate_delay = SDHCI_MSM_MMC_CLK_GATE_DELAY;
  2990. /* Set host capabilities */
  2991. msm_host->mmc->caps |= msm_host->pdata->mmc_bus_width;
  2992. msm_host->mmc->caps |= msm_host->pdata->caps;
  2993. vdd_max_current = sdhci_msm_get_vreg_vdd_max_current(msm_host);
  2994. if (vdd_max_current >= 800)
  2995. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_800;
  2996. else if (vdd_max_current >= 600)
  2997. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_600;
  2998. else if (vdd_max_current >= 400)
  2999. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_400;
  3000. else
  3001. msm_host->mmc->caps |= MMC_CAP_MAX_CURRENT_200;
  3002. if (vdd_max_current > 150)
  3003. msm_host->mmc->caps |= MMC_CAP_SET_XPC_180 |
  3004. MMC_CAP_SET_XPC_300|
  3005. MMC_CAP_SET_XPC_330;
  3006. msm_host->mmc->caps2 |= msm_host->pdata->caps2;
  3007. msm_host->mmc->caps2 |= (MMC_CAP2_BOOTPART_NOACC |
  3008. MMC_CAP2_DETECT_ON_ERR);
  3009. /* msm_host->mmc->caps2 |= MMC_CAP2_SANITIZE; */
  3010. msm_host->mmc->caps2 |= MMC_CAP2_CACHE_CTRL;
  3011. msm_host->mmc->caps2 |= MMC_CAP2_POWEROFF_NOTIFY;
  3012. /* msm_host->mmc->caps2 &= ~MMC_CAP2_CLK_SCALE; */ /* Disable CLK_SCALE at L UPG */
  3013. /* msm_host->mmc->caps2 |= MMC_CAP2_CORE_RUNTIME_PM; */
  3014. msm_host->mmc->caps2 |= MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE;
  3015. msm_host->mmc->caps2 |= MMC_CAP2_CORE_PM;
  3016. msm_host->mmc->pm_caps |= MMC_PM_KEEP_POWER | MMC_PM_WAKE_SDIO_IRQ;
  3017. if (msm_host->pdata->nonremovable)
  3018. msm_host->mmc->caps |= MMC_CAP_NONREMOVABLE;
  3019. host->cpu_dma_latency_us = msm_host->pdata->cpu_dma_latency_us;
  3020. init_completion(&msm_host->pwr_irq_completion);
  3021. #if defined(CONFIG_MACH_KS01SKT) || defined(CONFIG_MACH_KS01KTT) || defined(CONFIG_MACH_KS01LGT) || defined(CONFIG_MACH_JACTIVESKT)
  3022. if (msm_host->pdata->ls_gpio || gpio_is_valid(msm_host->pdata->ls_gpio))
  3023. mmc_enable_ls_gpio(host, msm_host->pdata->ls_gpio);
  3024. #endif
  3025. if (gpio_is_valid(msm_host->pdata->status_gpio)) {
  3026. ret = mmc_cd_gpio_request(msm_host->mmc,
  3027. msm_host->pdata->status_gpio);
  3028. if (ret) {
  3029. dev_err(&pdev->dev, "%s: Failed to request card detection IRQ %d\n",
  3030. __func__, ret);
  3031. goto vreg_deinit;
  3032. }
  3033. }
  3034. #if defined(CONFIG_BCM4335) || defined(CONFIG_BCM4335_MODULE) || \
  3035. defined(CONFIG_BCM4339) || defined(CONFIG_BCM4339_MODULE) || \
  3036. defined(CONFIG_BCM4354) || defined(CONFIG_BCM4354_MODULE)
  3037. pr_err("%s: id %d\n", mmc_hostname(msm_host->mmc), msm_host->id);
  3038. if (msm_host->pdata->is_status_cb) {
  3039. msm_host->pdata->register_status_notify = brcm_wifi_status_register;
  3040. msm_host->pdata->register_status_notify(sdhci_msm_status_notify, host, host->mmc);
  3041. host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY | MMC_PM_KEEP_POWER;
  3042. host->mmc->pm_caps |= MMC_PM_IGNORE_PM_NOTIFY | MMC_PM_KEEP_POWER;
  3043. #if defined(CONFIG_DEFERRED_INITCALLS)
  3044. host->mmc->rescan_disable=0;
  3045. #else
  3046. host->mmc->rescan_disable=1;
  3047. #endif
  3048. pr_err("%s: id %d, register_status_notify=%x, host=%x, rescan_disable=%d\n",
  3049. mmc_hostname(msm_host->mmc), msm_host->id,
  3050. (unsigned int)msm_host->pdata->register_status_notify,
  3051. (unsigned int)host, host->mmc->rescan_disable);
  3052. }
  3053. #endif
  3054. /* SYSFS about SD Card Detection by soonil.lim */
  3055. #if defined(CONFIG_MACH_SERRANO)
  3056. if (t_flash_detect_dev == NULL && !strcmp(host->hw_name, "msm_sdcc.3")) {
  3057. #elif defined(CONFIG_SEC_ATLANTIC_PROJECT) || defined(CONFIG_MACH_JSGLTE_CHN_CMCC)
  3058. if (t_flash_detect_dev == NULL && !strcmp(host->hw_name, "msm_sdcc.2")) {
  3059. #else
  3060. if (t_flash_detect_dev == NULL && gpio_is_valid(msm_host->pdata->status_gpio)) {
  3061. #endif
  3062. printk(KERN_DEBUG "%s : Change sysfs Card Detect\n", __func__);
  3063. t_flash_detect_dev = device_create(sec_class,
  3064. NULL, 0, NULL, "sdcard");
  3065. if (IS_ERR(t_flash_detect_dev))
  3066. pr_err("%s : Failed to create device!\n", __func__);
  3067. if (device_create_file(t_flash_detect_dev,
  3068. &dev_attr_status) < 0)
  3069. pr_err("%s : Failed to create device file(%s)!\n",
  3070. __func__, dev_attr_status.attr.name);
  3071. dev_set_drvdata(t_flash_detect_dev, msm_host);
  3072. }
  3073. if (dma_supported(mmc_dev(host->mmc), DMA_BIT_MASK(32))) {
  3074. host->dma_mask = DMA_BIT_MASK(32);
  3075. mmc_dev(host->mmc)->dma_mask = &host->dma_mask;
  3076. } else {
  3077. dev_err(&pdev->dev, "%s: Failed to set dma mask\n", __func__);
  3078. }
  3079. msm_host->pdata->sdiowakeup_irq = platform_get_irq_byname(pdev,
  3080. "sdiowakeup_irq");
  3081. if (msm_host->pdata->sdiowakeup_irq >= 0) {
  3082. msm_host->is_sdiowakeup_enabled = true;
  3083. ret = request_irq(msm_host->pdata->sdiowakeup_irq,
  3084. sdhci_msm_sdiowakeup_irq,
  3085. IRQF_SHARED | IRQF_TRIGGER_LOW,
  3086. "sdhci-msm sdiowakeup", host);
  3087. if (ret) {
  3088. dev_err(&pdev->dev, "%s: request sdiowakeup IRQ %d: failed: %d\n",
  3089. __func__, msm_host->pdata->sdiowakeup_irq, ret);
  3090. msm_host->pdata->sdiowakeup_irq = -1;
  3091. msm_host->is_sdiowakeup_enabled = false;
  3092. goto free_cd_gpio;
  3093. } else {
  3094. spin_lock_irqsave(&host->lock, flags);
  3095. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
  3096. spin_unlock_irqrestore(&host->lock, flags);
  3097. }
  3098. }
  3099. ret = sdhci_add_host(host);
  3100. if (ret) {
  3101. dev_err(&pdev->dev, "Add host failed (%d)\n", ret);
  3102. goto free_cd_gpio;
  3103. }
  3104. msm_host->msm_bus_vote.max_bus_bw.show = show_sdhci_max_bus_bw;
  3105. msm_host->msm_bus_vote.max_bus_bw.store = store_sdhci_max_bus_bw;
  3106. sysfs_attr_init(&msm_host->msm_bus_vote.max_bus_bw.attr);
  3107. msm_host->msm_bus_vote.max_bus_bw.attr.name = "max_bus_bw";
  3108. msm_host->msm_bus_vote.max_bus_bw.attr.mode = S_IRUGO | S_IWUSR;
  3109. ret = device_create_file(&pdev->dev,
  3110. &msm_host->msm_bus_vote.max_bus_bw);
  3111. if (ret)
  3112. goto remove_host;
  3113. if (!gpio_is_valid(msm_host->pdata->status_gpio)) {
  3114. msm_host->polling.show = show_polling;
  3115. msm_host->polling.store = store_polling;
  3116. sysfs_attr_init(&msm_host->polling.attr);
  3117. msm_host->polling.attr.name = "polling";
  3118. msm_host->polling.attr.mode = S_IRUGO | S_IWUSR;
  3119. ret = device_create_file(&pdev->dev, &msm_host->polling);
  3120. if (ret)
  3121. goto remove_max_bus_bw_file;
  3122. }
  3123. ret = pm_runtime_set_active(&pdev->dev);
  3124. if (ret)
  3125. pr_err("%s: %s: pm_runtime_set_active failed: err: %d\n",
  3126. mmc_hostname(host->mmc), __func__, ret);
  3127. else if (mmc_use_core_runtime_pm(host->mmc))
  3128. pm_runtime_enable(&pdev->dev);
  3129. if (msm_host->pdata->mpm_sdiowakeup_int != -1) {
  3130. ret = sdhci_msm_cfg_mpm_pin_wakeup(host, SDC_DAT1_ENABLE);
  3131. if (ret) {
  3132. pr_err("%s: enabling wakeup: failed: ret: %d\n",
  3133. mmc_hostname(host->mmc), ret);
  3134. ret = 0;
  3135. msm_host->pdata->mpm_sdiowakeup_int = -1;
  3136. }
  3137. }
  3138. device_enable_async_suspend(&pdev->dev);
  3139. /* Successful initialization */
  3140. goto out;
  3141. remove_max_bus_bw_file:
  3142. device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
  3143. remove_host:
  3144. dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
  3145. sdhci_remove_host(host, dead);
  3146. free_cd_gpio:
  3147. if (gpio_is_valid(msm_host->pdata->status_gpio))
  3148. mmc_cd_gpio_free(msm_host->mmc);
  3149. if (sdhci_is_valid_gpio_wakeup_int(msm_host))
  3150. free_irq(msm_host->pdata->sdiowakeup_irq, host);
  3151. vreg_deinit:
  3152. sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
  3153. bus_unregister:
  3154. if (msm_host->msm_bus_vote.client_handle)
  3155. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  3156. sdhci_msm_bus_unregister(msm_host);
  3157. sleep_clk_disable:
  3158. if (!IS_ERR(msm_host->sleep_clk))
  3159. clk_disable_unprepare(msm_host->sleep_clk);
  3160. ff_clk_disable:
  3161. if (!IS_ERR(msm_host->ff_clk))
  3162. clk_disable_unprepare(msm_host->ff_clk);
  3163. clk_disable:
  3164. if (!IS_ERR(msm_host->clk))
  3165. clk_disable_unprepare(msm_host->clk);
  3166. pclk_disable:
  3167. if (!IS_ERR(msm_host->pclk))
  3168. clk_disable_unprepare(msm_host->pclk);
  3169. bus_clk_disable:
  3170. if (!IS_ERR_OR_NULL(msm_host->bus_clk))
  3171. clk_disable_unprepare(msm_host->bus_clk);
  3172. pltfm_free:
  3173. sdhci_pltfm_free(pdev);
  3174. out:
  3175. pr_debug("%s: Exit %s\n", dev_name(&pdev->dev), __func__);
  3176. return ret;
  3177. }
  3178. static int __devexit sdhci_msm_remove(struct platform_device *pdev)
  3179. {
  3180. struct sdhci_host *host = platform_get_drvdata(pdev);
  3181. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3182. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3183. struct sdhci_msm_pltfm_data *pdata = msm_host->pdata;
  3184. int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
  3185. 0xffffffff);
  3186. pr_debug("%s: %s\n", dev_name(&pdev->dev), __func__);
  3187. if (!gpio_is_valid(msm_host->pdata->status_gpio))
  3188. device_remove_file(&pdev->dev, &msm_host->polling);
  3189. device_remove_file(&pdev->dev, &msm_host->msm_bus_vote.max_bus_bw);
  3190. sdhci_remove_host(host, dead);
  3191. pm_runtime_disable(&pdev->dev);
  3192. sdhci_pltfm_free(pdev);
  3193. if (sdhci_is_valid_mpm_wakeup_int(msm_host))
  3194. sdhci_msm_cfg_mpm_pin_wakeup(host, SDC_DAT1_DISABLE);
  3195. if (sdhci_is_valid_gpio_wakeup_int(msm_host))
  3196. free_irq(msm_host->pdata->sdiowakeup_irq, host);
  3197. if (gpio_is_valid(msm_host->pdata->status_gpio))
  3198. mmc_cd_gpio_free(msm_host->mmc);
  3199. sdhci_msm_vreg_init(&pdev->dev, msm_host->pdata, false);
  3200. if (pdata->pin_data)
  3201. sdhci_msm_setup_pins(pdata, false);
  3202. if (msm_host->msm_bus_vote.client_handle) {
  3203. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  3204. sdhci_msm_bus_unregister(msm_host);
  3205. }
  3206. return 0;
  3207. }
  3208. static int sdhci_msm_cfg_sdio_wakeup(struct sdhci_host *host, bool enable)
  3209. {
  3210. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3211. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3212. unsigned long flags;
  3213. int ret = 0;
  3214. if (!(host->mmc->card && mmc_card_sdio(host->mmc->card) &&
  3215. (sdhci_is_valid_mpm_wakeup_int(msm_host) ||
  3216. sdhci_is_valid_gpio_wakeup_int(msm_host)) &&
  3217. mmc_card_wake_sdio_irq(host->mmc))) {
  3218. return 1;
  3219. }
  3220. spin_lock_irqsave(&host->lock, flags);
  3221. if (enable) {
  3222. /* configure DAT1 gpio if applicable */
  3223. if (sdhci_is_valid_gpio_wakeup_int(msm_host)) {
  3224. ret = enable_irq_wake(msm_host->pdata->sdiowakeup_irq);
  3225. if (!ret)
  3226. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, true);
  3227. goto out;
  3228. } else {
  3229. ret = sdhci_msm_cfg_mpm_pin_wakeup(host,
  3230. SDC_DAT1_ENWAKE);
  3231. if (ret)
  3232. goto out;
  3233. ret = enable_irq_wake(host->irq);
  3234. if (ret)
  3235. sdhci_msm_cfg_mpm_pin_wakeup(host,
  3236. SDC_DAT1_DISWAKE);
  3237. }
  3238. } else {
  3239. if (sdhci_is_valid_gpio_wakeup_int(msm_host)) {
  3240. ret = disable_irq_wake(msm_host->pdata->sdiowakeup_irq);
  3241. sdhci_msm_cfg_sdiowakeup_gpio_irq(host, false);
  3242. } else {
  3243. ret = sdhci_msm_cfg_mpm_pin_wakeup(host,
  3244. SDC_DAT1_DISWAKE);
  3245. if (ret)
  3246. goto out;
  3247. ret = disable_irq_wake(host->irq);
  3248. }
  3249. }
  3250. out:
  3251. if (ret)
  3252. pr_err("%s: %s: %sable wakeup: failed: %d gpio: %d mpm: %d\n",
  3253. mmc_hostname(host->mmc), __func__, enable ? "en" : "dis",
  3254. ret, msm_host->pdata->sdiowakeup_irq,
  3255. msm_host->pdata->mpm_sdiowakeup_int);
  3256. spin_unlock_irqrestore(&host->lock, flags);
  3257. return ret;
  3258. }
  3259. static int sdhci_msm_runtime_suspend(struct device *dev)
  3260. {
  3261. struct sdhci_host *host = dev_get_drvdata(dev);
  3262. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3263. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3264. int ret;
  3265. ret = sdhci_msm_cfg_sdio_wakeup(host, true);
  3266. /* pwr_irq is not monitored by mpm on suspend, hence disable it */
  3267. if (!ret)
  3268. goto skip_disable_host_irq;
  3269. disable_irq(host->irq);
  3270. skip_disable_host_irq:
  3271. disable_irq(msm_host->pwr_irq);
  3272. /*
  3273. * Remove the vote immediately only if clocks are off in which
  3274. * case we might have queued work to remove vote but it may not
  3275. * be completed before runtime suspend or system suspend.
  3276. */
  3277. if (!atomic_read(&msm_host->clks_on)) {
  3278. if (msm_host->msm_bus_vote.client_handle)
  3279. sdhci_msm_bus_cancel_work_and_set_vote(host, 0);
  3280. }
  3281. if (msm_host->id == 3)
  3282. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3283. return 0;
  3284. }
  3285. static int sdhci_msm_runtime_resume(struct device *dev)
  3286. {
  3287. struct sdhci_host *host = dev_get_drvdata(dev);
  3288. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3289. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3290. int ret;
  3291. ret = sdhci_msm_cfg_sdio_wakeup(host, false);
  3292. if (!ret)
  3293. goto skip_enable_host_irq;
  3294. enable_irq(host->irq);
  3295. if (msm_host->id == 3)
  3296. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3297. skip_enable_host_irq:
  3298. enable_irq(msm_host->pwr_irq);
  3299. return 0;
  3300. }
  3301. #ifdef CONFIG_PM_SLEEP
  3302. static int sdhci_msm_suspend(struct device *dev)
  3303. {
  3304. struct sdhci_host *host = dev_get_drvdata(dev);
  3305. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3306. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3307. int ret = 0;
  3308. if (gpio_is_valid(msm_host->pdata->status_gpio))
  3309. mmc_cd_gpio_free(msm_host->mmc);
  3310. if (pm_runtime_suspended(dev)) {
  3311. pr_debug("%s: %s: already runtime suspended\n",
  3312. mmc_hostname(host->mmc), __func__);
  3313. goto out;
  3314. }
  3315. if (msm_host->id == 3)
  3316. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3317. return sdhci_msm_runtime_suspend(dev);
  3318. out:
  3319. return ret;
  3320. }
  3321. static int sdhci_msm_resume(struct device *dev)
  3322. {
  3323. struct sdhci_host *host = dev_get_drvdata(dev);
  3324. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3325. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3326. int ret = 0;
  3327. if (gpio_is_valid(msm_host->pdata->status_gpio)) {
  3328. ret = mmc_cd_gpio_request(msm_host->mmc,
  3329. msm_host->pdata->status_gpio);
  3330. if (ret)
  3331. pr_err("%s: %s: Failed to request card detection IRQ %d\n",
  3332. mmc_hostname(host->mmc), __func__, ret);
  3333. }
  3334. if (pm_runtime_suspended(dev)) {
  3335. pr_debug("%s: %s: runtime suspended, defer system resume\n",
  3336. mmc_hostname(host->mmc), __func__);
  3337. goto out;
  3338. }
  3339. if (msm_host->id == 3)
  3340. pr_info("%s: %s", mmc_hostname(host->mmc), __func__);
  3341. return sdhci_msm_runtime_resume(dev);
  3342. out:
  3343. return ret;
  3344. }
  3345. static int sdhci_msm_suspend_noirq(struct device *dev)
  3346. {
  3347. struct sdhci_host *host = dev_get_drvdata(dev);
  3348. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  3349. struct sdhci_msm_host *msm_host = pltfm_host->priv;
  3350. int ret = 0;
  3351. /*
  3352. * ksdioirqd may get scheduled after sdhc suspend, hence retry
  3353. * suspend in case the clocks are ON
  3354. */
  3355. if (atomic_read(&msm_host->clks_on)) {
  3356. pr_warn("%s: %s: clock ON after suspend, aborting suspend\n",
  3357. mmc_hostname(host->mmc), __func__);
  3358. ret = -EAGAIN;
  3359. }
  3360. return ret;
  3361. }
  3362. #endif
  3363. #ifdef CONFIG_PM
  3364. static const struct dev_pm_ops sdhci_msm_pmops = {
  3365. SET_SYSTEM_SLEEP_PM_OPS(sdhci_msm_suspend, sdhci_msm_resume)
  3366. SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend, sdhci_msm_runtime_resume,
  3367. NULL)
  3368. .suspend_noirq = sdhci_msm_suspend_noirq,
  3369. };
  3370. #define SDHCI_MSM_PMOPS (&sdhci_msm_pmops)
  3371. #else
  3372. #define SDHCI_PM_OPS NULL
  3373. #endif
  3374. static const struct of_device_id sdhci_msm_dt_match[] = {
  3375. {.compatible = "qcom,sdhci-msm"},
  3376. { /* sentinel */ }
  3377. };
  3378. MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
  3379. static struct platform_driver sdhci_msm_driver = {
  3380. .probe = sdhci_msm_probe,
  3381. .remove = __devexit_p(sdhci_msm_remove),
  3382. .driver = {
  3383. .name = "sdhci_msm",
  3384. .owner = THIS_MODULE,
  3385. .of_match_table = sdhci_msm_dt_match,
  3386. .pm = SDHCI_MSM_PMOPS,
  3387. },
  3388. };
  3389. module_platform_driver(sdhci_msm_driver);
  3390. MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
  3391. MODULE_LICENSE("GPL v2");