sdhci-msm.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/mmc/host/sdhci-msm.c - Qualcomm SDHCI Platform driver
  4. *
  5. * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
  6. */
  7. #include <linux/module.h>
  8. #include <linux/delay.h>
  9. #include <linux/mmc/mmc.h>
  10. #include <linux/pm_runtime.h>
  11. #include <linux/pm_opp.h>
  12. #include <linux/slab.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/regulator/consumer.h>
  15. #include <linux/interconnect.h>
  16. #include <linux/of.h>
  17. #include <linux/pinctrl/consumer.h>
  18. #include <linux/reset.h>
  19. #include <soc/qcom/ice.h>
  20. #include "sdhci-cqhci.h"
  21. #include "sdhci-pltfm.h"
  22. #include "cqhci.h"
  23. #define CORE_MCI_VERSION 0x50
  24. #define CORE_VERSION_MAJOR_SHIFT 28
  25. #define CORE_VERSION_MAJOR_MASK (0xf << CORE_VERSION_MAJOR_SHIFT)
  26. #define CORE_VERSION_MINOR_MASK 0xff
  27. #define CORE_MCI_GENERICS 0x70
  28. #define SWITCHABLE_SIGNALING_VOLTAGE BIT(29)
  29. #define HC_MODE_EN 0x1
  30. #define CORE_POWER 0x0
  31. #define CORE_SW_RST BIT(7)
  32. #define FF_CLK_SW_RST_DIS BIT(13)
  33. #define CORE_PWRCTL_BUS_OFF BIT(0)
  34. #define CORE_PWRCTL_BUS_ON BIT(1)
  35. #define CORE_PWRCTL_IO_LOW BIT(2)
  36. #define CORE_PWRCTL_IO_HIGH BIT(3)
  37. #define CORE_PWRCTL_BUS_SUCCESS BIT(0)
  38. #define CORE_PWRCTL_BUS_FAIL BIT(1)
  39. #define CORE_PWRCTL_IO_SUCCESS BIT(2)
  40. #define CORE_PWRCTL_IO_FAIL BIT(3)
  41. #define REQ_BUS_OFF BIT(0)
  42. #define REQ_BUS_ON BIT(1)
  43. #define REQ_IO_LOW BIT(2)
  44. #define REQ_IO_HIGH BIT(3)
  45. #define INT_MASK 0xf
  46. #define MAX_PHASES 16
  47. #define CORE_DLL_LOCK BIT(7)
  48. #define CORE_DDR_DLL_LOCK BIT(11)
  49. #define CORE_DLL_EN BIT(16)
  50. #define CORE_CDR_EN BIT(17)
  51. #define CORE_CK_OUT_EN BIT(18)
  52. #define CORE_CDR_EXT_EN BIT(19)
  53. #define CORE_DLL_PDN BIT(29)
  54. #define CORE_DLL_RST BIT(30)
  55. #define CORE_CMD_DAT_TRACK_SEL BIT(0)
  56. #define CORE_DDR_CAL_EN BIT(0)
  57. #define CORE_FLL_CYCLE_CNT BIT(18)
  58. #define CORE_DLL_CLOCK_DISABLE BIT(21)
  59. #define DLL_USR_CTL_POR_VAL 0x10800
  60. #define ENABLE_DLL_LOCK_STATUS BIT(26)
  61. #define FINE_TUNE_MODE_EN BIT(27)
  62. #define BIAS_OK_SIGNAL BIT(29)
  63. #define DLL_CONFIG_3_LOW_FREQ_VAL 0x08
  64. #define DLL_CONFIG_3_HIGH_FREQ_VAL 0x10
  65. #define CORE_VENDOR_SPEC_POR_VAL 0xa9c
  66. #define CORE_CLK_PWRSAVE BIT(1)
  67. #define CORE_HC_MCLK_SEL_DFLT (2 << 8)
  68. #define CORE_HC_MCLK_SEL_HS400 (3 << 8)
  69. #define CORE_HC_MCLK_SEL_MASK (3 << 8)
  70. #define CORE_IO_PAD_PWR_SWITCH_EN BIT(15)
  71. #define CORE_IO_PAD_PWR_SWITCH BIT(16)
  72. #define CORE_HC_SELECT_IN_EN BIT(18)
  73. #define CORE_HC_SELECT_IN_HS400 (6 << 19)
  74. #define CORE_HC_SELECT_IN_MASK (7 << 19)
  75. #define CORE_3_0V_SUPPORT BIT(25)
  76. #define CORE_1_8V_SUPPORT BIT(26)
  77. #define CORE_VOLT_SUPPORT (CORE_3_0V_SUPPORT | CORE_1_8V_SUPPORT)
  78. #define CORE_CSR_CDC_CTLR_CFG0 0x130
  79. #define CORE_SW_TRIG_FULL_CALIB BIT(16)
  80. #define CORE_HW_AUTOCAL_ENA BIT(17)
  81. #define CORE_CSR_CDC_CTLR_CFG1 0x134
  82. #define CORE_CSR_CDC_CAL_TIMER_CFG0 0x138
  83. #define CORE_TIMER_ENA BIT(16)
  84. #define CORE_CSR_CDC_CAL_TIMER_CFG1 0x13C
  85. #define CORE_CSR_CDC_REFCOUNT_CFG 0x140
  86. #define CORE_CSR_CDC_COARSE_CAL_CFG 0x144
  87. #define CORE_CDC_OFFSET_CFG 0x14C
  88. #define CORE_CSR_CDC_DELAY_CFG 0x150
  89. #define CORE_CDC_SLAVE_DDA_CFG 0x160
  90. #define CORE_CSR_CDC_STATUS0 0x164
  91. #define CORE_CALIBRATION_DONE BIT(0)
  92. #define CORE_CDC_ERROR_CODE_MASK 0x7000000
  93. #define CORE_CSR_CDC_GEN_CFG 0x178
  94. #define CORE_CDC_SWITCH_BYPASS_OFF BIT(0)
  95. #define CORE_CDC_SWITCH_RC_EN BIT(1)
  96. #define CORE_CDC_T4_DLY_SEL BIT(0)
  97. #define CORE_CMDIN_RCLK_EN BIT(1)
  98. #define CORE_START_CDC_TRAFFIC BIT(6)
  99. #define CORE_PWRSAVE_DLL BIT(3)
  100. #define DDR_CONFIG_POR_VAL 0x80040873
  101. #define INVALID_TUNING_PHASE -1
  102. #define SDHCI_MSM_MIN_CLOCK 400000
  103. #define CORE_FREQ_100MHZ (100 * 1000 * 1000)
  104. #define CDR_SELEXT_SHIFT 20
  105. #define CDR_SELEXT_MASK (0xf << CDR_SELEXT_SHIFT)
  106. #define CMUX_SHIFT_PHASE_SHIFT 24
  107. #define CMUX_SHIFT_PHASE_MASK (7 << CMUX_SHIFT_PHASE_SHIFT)
  108. #define MSM_MMC_AUTOSUSPEND_DELAY_MS 50
  109. /* Timeout value to avoid infinite waiting for pwr_irq */
  110. #define MSM_PWR_IRQ_TIMEOUT_MS 5000
  111. /* Max load for eMMC Vdd supply */
  112. #define MMC_VMMC_MAX_LOAD_UA 570000
  113. /* Max load for eMMC Vdd-io supply */
  114. #define MMC_VQMMC_MAX_LOAD_UA 325000
  115. /* Max load for SD Vdd supply */
  116. #define SD_VMMC_MAX_LOAD_UA 800000
  117. /* Max load for SD Vdd-io supply */
  118. #define SD_VQMMC_MAX_LOAD_UA 22000
  119. #define msm_host_readl(msm_host, host, offset) \
  120. msm_host->var_ops->msm_readl_relaxed(host, offset)
  121. #define msm_host_writel(msm_host, val, host, offset) \
  122. msm_host->var_ops->msm_writel_relaxed(val, host, offset)
  123. /* CQHCI vendor specific registers */
  124. #define CQHCI_VENDOR_CFG1 0xA00
  125. #define CQHCI_VENDOR_DIS_RST_ON_CQ_EN (0x3 << 13)
  126. struct sdhci_msm_offset {
  127. u32 core_hc_mode;
  128. u32 core_mci_data_cnt;
  129. u32 core_mci_status;
  130. u32 core_mci_fifo_cnt;
  131. u32 core_mci_version;
  132. u32 core_generics;
  133. u32 core_testbus_config;
  134. u32 core_testbus_sel2_bit;
  135. u32 core_testbus_ena;
  136. u32 core_testbus_sel2;
  137. u32 core_pwrctl_status;
  138. u32 core_pwrctl_mask;
  139. u32 core_pwrctl_clear;
  140. u32 core_pwrctl_ctl;
  141. u32 core_sdcc_debug_reg;
  142. u32 core_dll_config;
  143. u32 core_dll_status;
  144. u32 core_vendor_spec;
  145. u32 core_vendor_spec_adma_err_addr0;
  146. u32 core_vendor_spec_adma_err_addr1;
  147. u32 core_vendor_spec_func2;
  148. u32 core_vendor_spec_capabilities0;
  149. u32 core_ddr_200_cfg;
  150. u32 core_vendor_spec3;
  151. u32 core_dll_config_2;
  152. u32 core_dll_config_3;
  153. u32 core_ddr_config_old; /* Applicable to sdcc minor ver < 0x49 */
  154. u32 core_ddr_config;
  155. u32 core_dll_usr_ctl; /* Present on SDCC5.1 onwards */
  156. };
  157. static const struct sdhci_msm_offset sdhci_msm_v5_offset = {
  158. .core_mci_data_cnt = 0x35c,
  159. .core_mci_status = 0x324,
  160. .core_mci_fifo_cnt = 0x308,
  161. .core_mci_version = 0x318,
  162. .core_generics = 0x320,
  163. .core_testbus_config = 0x32c,
  164. .core_testbus_sel2_bit = 3,
  165. .core_testbus_ena = (1 << 31),
  166. .core_testbus_sel2 = (1 << 3),
  167. .core_pwrctl_status = 0x240,
  168. .core_pwrctl_mask = 0x244,
  169. .core_pwrctl_clear = 0x248,
  170. .core_pwrctl_ctl = 0x24c,
  171. .core_sdcc_debug_reg = 0x358,
  172. .core_dll_config = 0x200,
  173. .core_dll_status = 0x208,
  174. .core_vendor_spec = 0x20c,
  175. .core_vendor_spec_adma_err_addr0 = 0x214,
  176. .core_vendor_spec_adma_err_addr1 = 0x218,
  177. .core_vendor_spec_func2 = 0x210,
  178. .core_vendor_spec_capabilities0 = 0x21c,
  179. .core_ddr_200_cfg = 0x224,
  180. .core_vendor_spec3 = 0x250,
  181. .core_dll_config_2 = 0x254,
  182. .core_dll_config_3 = 0x258,
  183. .core_ddr_config = 0x25c,
  184. .core_dll_usr_ctl = 0x388,
  185. };
  186. static const struct sdhci_msm_offset sdhci_msm_mci_offset = {
  187. .core_hc_mode = 0x78,
  188. .core_mci_data_cnt = 0x30,
  189. .core_mci_status = 0x34,
  190. .core_mci_fifo_cnt = 0x44,
  191. .core_mci_version = 0x050,
  192. .core_generics = 0x70,
  193. .core_testbus_config = 0x0cc,
  194. .core_testbus_sel2_bit = 4,
  195. .core_testbus_ena = (1 << 3),
  196. .core_testbus_sel2 = (1 << 4),
  197. .core_pwrctl_status = 0xdc,
  198. .core_pwrctl_mask = 0xe0,
  199. .core_pwrctl_clear = 0xe4,
  200. .core_pwrctl_ctl = 0xe8,
  201. .core_sdcc_debug_reg = 0x124,
  202. .core_dll_config = 0x100,
  203. .core_dll_status = 0x108,
  204. .core_vendor_spec = 0x10c,
  205. .core_vendor_spec_adma_err_addr0 = 0x114,
  206. .core_vendor_spec_adma_err_addr1 = 0x118,
  207. .core_vendor_spec_func2 = 0x110,
  208. .core_vendor_spec_capabilities0 = 0x11c,
  209. .core_ddr_200_cfg = 0x184,
  210. .core_vendor_spec3 = 0x1b0,
  211. .core_dll_config_2 = 0x1b4,
  212. .core_ddr_config_old = 0x1b8,
  213. .core_ddr_config = 0x1bc,
  214. };
  215. struct sdhci_msm_variant_ops {
  216. u32 (*msm_readl_relaxed)(struct sdhci_host *host, u32 offset);
  217. void (*msm_writel_relaxed)(u32 val, struct sdhci_host *host,
  218. u32 offset);
  219. };
  220. /*
  221. * From V5, register spaces have changed. Wrap this info in a structure
  222. * and choose the data_structure based on version info mentioned in DT.
  223. */
  224. struct sdhci_msm_variant_info {
  225. bool mci_removed;
  226. bool restore_dll_config;
  227. const struct sdhci_msm_variant_ops *var_ops;
  228. const struct sdhci_msm_offset *offset;
  229. };
  230. struct sdhci_msm_host {
  231. struct platform_device *pdev;
  232. void __iomem *core_mem; /* MSM SDCC mapped address */
  233. int pwr_irq; /* power irq */
  234. struct clk *bus_clk; /* SDHC bus voter clock */
  235. struct clk *xo_clk; /* TCXO clk needed for FLL feature of cm_dll*/
  236. /* core, iface, cal and sleep clocks */
  237. struct clk_bulk_data bulk_clks[4];
  238. #ifdef CONFIG_MMC_CRYPTO
  239. struct qcom_ice *ice;
  240. #endif
  241. unsigned long clk_rate;
  242. struct mmc_host *mmc;
  243. bool use_14lpp_dll_reset;
  244. bool tuning_done;
  245. bool calibration_done;
  246. u8 saved_tuning_phase;
  247. bool use_cdclp533;
  248. u32 curr_pwr_state;
  249. u32 curr_io_level;
  250. wait_queue_head_t pwr_irq_wait;
  251. bool pwr_irq_flag;
  252. u32 caps_0;
  253. bool mci_removed;
  254. bool restore_dll_config;
  255. const struct sdhci_msm_variant_ops *var_ops;
  256. const struct sdhci_msm_offset *offset;
  257. bool use_cdr;
  258. u32 transfer_mode;
  259. bool updated_ddr_cfg;
  260. bool uses_tassadar_dll;
  261. u32 dll_config;
  262. u32 ddr_config;
  263. bool vqmmc_enabled;
  264. };
  265. static const struct sdhci_msm_offset *sdhci_priv_msm_offset(struct sdhci_host *host)
  266. {
  267. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  268. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  269. return msm_host->offset;
  270. }
  271. /*
  272. * APIs to read/write to vendor specific registers which were there in the
  273. * core_mem region before MCI was removed.
  274. */
  275. static u32 sdhci_msm_mci_variant_readl_relaxed(struct sdhci_host *host,
  276. u32 offset)
  277. {
  278. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  279. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  280. return readl_relaxed(msm_host->core_mem + offset);
  281. }
  282. static u32 sdhci_msm_v5_variant_readl_relaxed(struct sdhci_host *host,
  283. u32 offset)
  284. {
  285. return readl_relaxed(host->ioaddr + offset);
  286. }
  287. static void sdhci_msm_mci_variant_writel_relaxed(u32 val,
  288. struct sdhci_host *host, u32 offset)
  289. {
  290. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  291. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  292. writel_relaxed(val, msm_host->core_mem + offset);
  293. }
  294. static void sdhci_msm_v5_variant_writel_relaxed(u32 val,
  295. struct sdhci_host *host, u32 offset)
  296. {
  297. writel_relaxed(val, host->ioaddr + offset);
  298. }
  299. static unsigned int msm_get_clock_mult_for_bus_mode(struct sdhci_host *host)
  300. {
  301. struct mmc_ios ios = host->mmc->ios;
  302. /*
  303. * The SDHC requires internal clock frequency to be double the
  304. * actual clock that will be set for DDR mode. The controller
  305. * uses the faster clock(100/400MHz) for some of its parts and
  306. * send the actual required clock (50/200MHz) to the card.
  307. */
  308. if (ios.timing == MMC_TIMING_UHS_DDR50 ||
  309. ios.timing == MMC_TIMING_MMC_DDR52 ||
  310. ios.timing == MMC_TIMING_MMC_HS400 ||
  311. host->flags & SDHCI_HS400_TUNING)
  312. return 2;
  313. return 1;
  314. }
  315. static void msm_set_clock_rate_for_bus_mode(struct sdhci_host *host,
  316. unsigned int clock)
  317. {
  318. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  319. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  320. struct mmc_ios curr_ios = host->mmc->ios;
  321. struct clk *core_clk = msm_host->bulk_clks[0].clk;
  322. unsigned long achieved_rate;
  323. unsigned int desired_rate;
  324. unsigned int mult;
  325. int rc;
  326. mult = msm_get_clock_mult_for_bus_mode(host);
  327. desired_rate = clock * mult;
  328. rc = dev_pm_opp_set_rate(mmc_dev(host->mmc), desired_rate);
  329. if (rc) {
  330. pr_err("%s: Failed to set clock at rate %u at timing %d\n",
  331. mmc_hostname(host->mmc), desired_rate, curr_ios.timing);
  332. return;
  333. }
  334. /*
  335. * Qualcomm clock drivers by default round clock _up_ if they can't
  336. * make the requested rate. This is not good for SD. Yell if we
  337. * encounter it.
  338. */
  339. achieved_rate = clk_get_rate(core_clk);
  340. if (achieved_rate > desired_rate)
  341. pr_warn("%s: Card appears overclocked; req %u Hz, actual %lu Hz\n",
  342. mmc_hostname(host->mmc), desired_rate, achieved_rate);
  343. host->mmc->actual_clock = achieved_rate / mult;
  344. /* Stash the rate we requested to use in sdhci_msm_runtime_resume() */
  345. msm_host->clk_rate = desired_rate;
  346. pr_debug("%s: Setting clock at rate %lu at timing %d\n",
  347. mmc_hostname(host->mmc), achieved_rate, curr_ios.timing);
  348. }
  349. /* Platform specific tuning */
  350. static inline int msm_dll_poll_ck_out_en(struct sdhci_host *host, u8 poll)
  351. {
  352. u32 wait_cnt = 50;
  353. u8 ck_out_en;
  354. struct mmc_host *mmc = host->mmc;
  355. const struct sdhci_msm_offset *msm_offset =
  356. sdhci_priv_msm_offset(host);
  357. /* Poll for CK_OUT_EN bit. max. poll time = 50us */
  358. ck_out_en = !!(readl_relaxed(host->ioaddr +
  359. msm_offset->core_dll_config) & CORE_CK_OUT_EN);
  360. while (ck_out_en != poll) {
  361. if (--wait_cnt == 0) {
  362. dev_err(mmc_dev(mmc), "%s: CK_OUT_EN bit is not %d\n",
  363. mmc_hostname(mmc), poll);
  364. return -ETIMEDOUT;
  365. }
  366. udelay(1);
  367. ck_out_en = !!(readl_relaxed(host->ioaddr +
  368. msm_offset->core_dll_config) & CORE_CK_OUT_EN);
  369. }
  370. return 0;
  371. }
  372. static int msm_config_cm_dll_phase(struct sdhci_host *host, u8 phase)
  373. {
  374. int rc;
  375. static const u8 grey_coded_phase_table[] = {
  376. 0x0, 0x1, 0x3, 0x2, 0x6, 0x7, 0x5, 0x4,
  377. 0xc, 0xd, 0xf, 0xe, 0xa, 0xb, 0x9, 0x8
  378. };
  379. unsigned long flags;
  380. u32 config;
  381. struct mmc_host *mmc = host->mmc;
  382. const struct sdhci_msm_offset *msm_offset =
  383. sdhci_priv_msm_offset(host);
  384. if (phase > 0xf)
  385. return -EINVAL;
  386. spin_lock_irqsave(&host->lock, flags);
  387. config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
  388. config &= ~(CORE_CDR_EN | CORE_CK_OUT_EN);
  389. config |= (CORE_CDR_EXT_EN | CORE_DLL_EN);
  390. writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
  391. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '0' */
  392. rc = msm_dll_poll_ck_out_en(host, 0);
  393. if (rc)
  394. goto err_out;
  395. /*
  396. * Write the selected DLL clock output phase (0 ... 15)
  397. * to CDR_SELEXT bit field of DLL_CONFIG register.
  398. */
  399. config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
  400. config &= ~CDR_SELEXT_MASK;
  401. config |= grey_coded_phase_table[phase] << CDR_SELEXT_SHIFT;
  402. writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
  403. config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
  404. config |= CORE_CK_OUT_EN;
  405. writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
  406. /* Wait until CK_OUT_EN bit of DLL_CONFIG register becomes '1' */
  407. rc = msm_dll_poll_ck_out_en(host, 1);
  408. if (rc)
  409. goto err_out;
  410. config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
  411. config |= CORE_CDR_EN;
  412. config &= ~CORE_CDR_EXT_EN;
  413. writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
  414. goto out;
  415. err_out:
  416. dev_err(mmc_dev(mmc), "%s: Failed to set DLL phase: %d\n",
  417. mmc_hostname(mmc), phase);
  418. out:
  419. spin_unlock_irqrestore(&host->lock, flags);
  420. return rc;
  421. }
  422. /*
  423. * Find out the greatest range of consecuitive selected
  424. * DLL clock output phases that can be used as sampling
  425. * setting for SD3.0 UHS-I card read operation (in SDR104
  426. * timing mode) or for eMMC4.5 card read operation (in
  427. * HS400/HS200 timing mode).
  428. * Select the 3/4 of the range and configure the DLL with the
  429. * selected DLL clock output phase.
  430. */
  431. static int msm_find_most_appropriate_phase(struct sdhci_host *host,
  432. u8 *phase_table, u8 total_phases)
  433. {
  434. int ret;
  435. u8 ranges[MAX_PHASES][MAX_PHASES] = { {0}, {0} };
  436. u8 phases_per_row[MAX_PHASES] = { 0 };
  437. int row_index = 0, col_index = 0, selected_row_index = 0, curr_max = 0;
  438. int i, cnt, phase_0_raw_index = 0, phase_15_raw_index = 0;
  439. bool phase_0_found = false, phase_15_found = false;
  440. struct mmc_host *mmc = host->mmc;
  441. if (!total_phases || (total_phases > MAX_PHASES)) {
  442. dev_err(mmc_dev(mmc), "%s: Invalid argument: total_phases=%d\n",
  443. mmc_hostname(mmc), total_phases);
  444. return -EINVAL;
  445. }
  446. for (cnt = 0; cnt < total_phases; cnt++) {
  447. ranges[row_index][col_index] = phase_table[cnt];
  448. phases_per_row[row_index] += 1;
  449. col_index++;
  450. if ((cnt + 1) == total_phases) {
  451. continue;
  452. /* check if next phase in phase_table is consecutive or not */
  453. } else if ((phase_table[cnt] + 1) != phase_table[cnt + 1]) {
  454. row_index++;
  455. col_index = 0;
  456. }
  457. }
  458. if (row_index >= MAX_PHASES)
  459. return -EINVAL;
  460. /* Check if phase-0 is present in first valid window? */
  461. if (!ranges[0][0]) {
  462. phase_0_found = true;
  463. phase_0_raw_index = 0;
  464. /* Check if cycle exist between 2 valid windows */
  465. for (cnt = 1; cnt <= row_index; cnt++) {
  466. if (phases_per_row[cnt]) {
  467. for (i = 0; i < phases_per_row[cnt]; i++) {
  468. if (ranges[cnt][i] == 15) {
  469. phase_15_found = true;
  470. phase_15_raw_index = cnt;
  471. break;
  472. }
  473. }
  474. }
  475. }
  476. }
  477. /* If 2 valid windows form cycle then merge them as single window */
  478. if (phase_0_found && phase_15_found) {
  479. /* number of phases in raw where phase 0 is present */
  480. u8 phases_0 = phases_per_row[phase_0_raw_index];
  481. /* number of phases in raw where phase 15 is present */
  482. u8 phases_15 = phases_per_row[phase_15_raw_index];
  483. if (phases_0 + phases_15 >= MAX_PHASES)
  484. /*
  485. * If there are more than 1 phase windows then total
  486. * number of phases in both the windows should not be
  487. * more than or equal to MAX_PHASES.
  488. */
  489. return -EINVAL;
  490. /* Merge 2 cyclic windows */
  491. i = phases_15;
  492. for (cnt = 0; cnt < phases_0; cnt++) {
  493. ranges[phase_15_raw_index][i] =
  494. ranges[phase_0_raw_index][cnt];
  495. if (++i >= MAX_PHASES)
  496. break;
  497. }
  498. phases_per_row[phase_0_raw_index] = 0;
  499. phases_per_row[phase_15_raw_index] = phases_15 + phases_0;
  500. }
  501. for (cnt = 0; cnt <= row_index; cnt++) {
  502. if (phases_per_row[cnt] > curr_max) {
  503. curr_max = phases_per_row[cnt];
  504. selected_row_index = cnt;
  505. }
  506. }
  507. i = (curr_max * 3) / 4;
  508. if (i)
  509. i--;
  510. ret = ranges[selected_row_index][i];
  511. if (ret >= MAX_PHASES) {
  512. ret = -EINVAL;
  513. dev_err(mmc_dev(mmc), "%s: Invalid phase selected=%d\n",
  514. mmc_hostname(mmc), ret);
  515. }
  516. return ret;
  517. }
  518. static inline void msm_cm_dll_set_freq(struct sdhci_host *host)
  519. {
  520. u32 mclk_freq = 0, config;
  521. const struct sdhci_msm_offset *msm_offset =
  522. sdhci_priv_msm_offset(host);
  523. /* Program the MCLK value to MCLK_FREQ bit field */
  524. if (host->clock <= 112000000)
  525. mclk_freq = 0;
  526. else if (host->clock <= 125000000)
  527. mclk_freq = 1;
  528. else if (host->clock <= 137000000)
  529. mclk_freq = 2;
  530. else if (host->clock <= 150000000)
  531. mclk_freq = 3;
  532. else if (host->clock <= 162000000)
  533. mclk_freq = 4;
  534. else if (host->clock <= 175000000)
  535. mclk_freq = 5;
  536. else if (host->clock <= 187000000)
  537. mclk_freq = 6;
  538. else if (host->clock <= 200000000)
  539. mclk_freq = 7;
  540. config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
  541. config &= ~CMUX_SHIFT_PHASE_MASK;
  542. config |= mclk_freq << CMUX_SHIFT_PHASE_SHIFT;
  543. writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
  544. }
  545. /* Initialize the DLL (Programmable Delay Line) */
  546. static int msm_init_cm_dll(struct sdhci_host *host)
  547. {
  548. struct mmc_host *mmc = host->mmc;
  549. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  550. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  551. int wait_cnt = 50;
  552. unsigned long flags, xo_clk = 0;
  553. u32 config;
  554. const struct sdhci_msm_offset *msm_offset =
  555. msm_host->offset;
  556. if (msm_host->use_14lpp_dll_reset && !IS_ERR_OR_NULL(msm_host->xo_clk))
  557. xo_clk = clk_get_rate(msm_host->xo_clk);
  558. spin_lock_irqsave(&host->lock, flags);
  559. /*
  560. * Make sure that clock is always enabled when DLL
  561. * tuning is in progress. Keeping PWRSAVE ON may
  562. * turn off the clock.
  563. */
  564. config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
  565. config &= ~CORE_CLK_PWRSAVE;
  566. writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
  567. if (msm_host->dll_config)
  568. writel_relaxed(msm_host->dll_config,
  569. host->ioaddr + msm_offset->core_dll_config);
  570. if (msm_host->use_14lpp_dll_reset) {
  571. config = readl_relaxed(host->ioaddr +
  572. msm_offset->core_dll_config);
  573. config &= ~CORE_CK_OUT_EN;
  574. writel_relaxed(config, host->ioaddr +
  575. msm_offset->core_dll_config);
  576. config = readl_relaxed(host->ioaddr +
  577. msm_offset->core_dll_config_2);
  578. config |= CORE_DLL_CLOCK_DISABLE;
  579. writel_relaxed(config, host->ioaddr +
  580. msm_offset->core_dll_config_2);
  581. }
  582. config = readl_relaxed(host->ioaddr +
  583. msm_offset->core_dll_config);
  584. config |= CORE_DLL_RST;
  585. writel_relaxed(config, host->ioaddr +
  586. msm_offset->core_dll_config);
  587. config = readl_relaxed(host->ioaddr +
  588. msm_offset->core_dll_config);
  589. config |= CORE_DLL_PDN;
  590. writel_relaxed(config, host->ioaddr +
  591. msm_offset->core_dll_config);
  592. if (!msm_host->dll_config)
  593. msm_cm_dll_set_freq(host);
  594. if (msm_host->use_14lpp_dll_reset &&
  595. !IS_ERR_OR_NULL(msm_host->xo_clk)) {
  596. u32 mclk_freq = 0;
  597. config = readl_relaxed(host->ioaddr +
  598. msm_offset->core_dll_config_2);
  599. config &= CORE_FLL_CYCLE_CNT;
  600. if (config)
  601. mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 8),
  602. xo_clk);
  603. else
  604. mclk_freq = DIV_ROUND_CLOSEST_ULL((host->clock * 4),
  605. xo_clk);
  606. config = readl_relaxed(host->ioaddr +
  607. msm_offset->core_dll_config_2);
  608. config &= ~(0xFF << 10);
  609. config |= mclk_freq << 10;
  610. writel_relaxed(config, host->ioaddr +
  611. msm_offset->core_dll_config_2);
  612. /* wait for 5us before enabling DLL clock */
  613. udelay(5);
  614. }
  615. config = readl_relaxed(host->ioaddr +
  616. msm_offset->core_dll_config);
  617. config &= ~CORE_DLL_RST;
  618. writel_relaxed(config, host->ioaddr +
  619. msm_offset->core_dll_config);
  620. config = readl_relaxed(host->ioaddr +
  621. msm_offset->core_dll_config);
  622. config &= ~CORE_DLL_PDN;
  623. writel_relaxed(config, host->ioaddr +
  624. msm_offset->core_dll_config);
  625. if (msm_host->use_14lpp_dll_reset) {
  626. if (!msm_host->dll_config)
  627. msm_cm_dll_set_freq(host);
  628. config = readl_relaxed(host->ioaddr +
  629. msm_offset->core_dll_config_2);
  630. config &= ~CORE_DLL_CLOCK_DISABLE;
  631. writel_relaxed(config, host->ioaddr +
  632. msm_offset->core_dll_config_2);
  633. }
  634. /*
  635. * Configure DLL user control register to enable DLL status.
  636. * This setting is applicable to SDCC v5.1 onwards only.
  637. */
  638. if (msm_host->uses_tassadar_dll) {
  639. config = DLL_USR_CTL_POR_VAL | FINE_TUNE_MODE_EN |
  640. ENABLE_DLL_LOCK_STATUS | BIAS_OK_SIGNAL;
  641. writel_relaxed(config, host->ioaddr +
  642. msm_offset->core_dll_usr_ctl);
  643. config = readl_relaxed(host->ioaddr +
  644. msm_offset->core_dll_config_3);
  645. config &= ~0xFF;
  646. if (msm_host->clk_rate < 150000000)
  647. config |= DLL_CONFIG_3_LOW_FREQ_VAL;
  648. else
  649. config |= DLL_CONFIG_3_HIGH_FREQ_VAL;
  650. writel_relaxed(config, host->ioaddr +
  651. msm_offset->core_dll_config_3);
  652. }
  653. config = readl_relaxed(host->ioaddr +
  654. msm_offset->core_dll_config);
  655. config |= CORE_DLL_EN;
  656. writel_relaxed(config, host->ioaddr +
  657. msm_offset->core_dll_config);
  658. config = readl_relaxed(host->ioaddr +
  659. msm_offset->core_dll_config);
  660. config |= CORE_CK_OUT_EN;
  661. writel_relaxed(config, host->ioaddr +
  662. msm_offset->core_dll_config);
  663. /* Wait until DLL_LOCK bit of DLL_STATUS register becomes '1' */
  664. while (!(readl_relaxed(host->ioaddr + msm_offset->core_dll_status) &
  665. CORE_DLL_LOCK)) {
  666. /* max. wait for 50us sec for LOCK bit to be set */
  667. if (--wait_cnt == 0) {
  668. dev_err(mmc_dev(mmc), "%s: DLL failed to LOCK\n",
  669. mmc_hostname(mmc));
  670. spin_unlock_irqrestore(&host->lock, flags);
  671. return -ETIMEDOUT;
  672. }
  673. udelay(1);
  674. }
  675. spin_unlock_irqrestore(&host->lock, flags);
  676. return 0;
  677. }
  678. static void msm_hc_select_default(struct sdhci_host *host)
  679. {
  680. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  681. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  682. u32 config;
  683. const struct sdhci_msm_offset *msm_offset =
  684. msm_host->offset;
  685. if (!msm_host->use_cdclp533) {
  686. config = readl_relaxed(host->ioaddr +
  687. msm_offset->core_vendor_spec3);
  688. config &= ~CORE_PWRSAVE_DLL;
  689. writel_relaxed(config, host->ioaddr +
  690. msm_offset->core_vendor_spec3);
  691. }
  692. config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
  693. config &= ~CORE_HC_MCLK_SEL_MASK;
  694. config |= CORE_HC_MCLK_SEL_DFLT;
  695. writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
  696. /*
  697. * Disable HC_SELECT_IN to be able to use the UHS mode select
  698. * configuration from Host Control2 register for all other
  699. * modes.
  700. * Write 0 to HC_SELECT_IN and HC_SELECT_IN_EN field
  701. * in VENDOR_SPEC_FUNC
  702. */
  703. config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
  704. config &= ~CORE_HC_SELECT_IN_EN;
  705. config &= ~CORE_HC_SELECT_IN_MASK;
  706. writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
  707. /*
  708. * Make sure above writes impacting free running MCLK are completed
  709. * before changing the clk_rate at GCC.
  710. */
  711. wmb();
  712. }
  713. static void msm_hc_select_hs400(struct sdhci_host *host)
  714. {
  715. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  716. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  717. struct mmc_ios ios = host->mmc->ios;
  718. u32 config, dll_lock;
  719. int rc;
  720. const struct sdhci_msm_offset *msm_offset =
  721. msm_host->offset;
  722. /* Select the divided clock (free running MCLK/2) */
  723. config = readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec);
  724. config &= ~CORE_HC_MCLK_SEL_MASK;
  725. config |= CORE_HC_MCLK_SEL_HS400;
  726. writel_relaxed(config, host->ioaddr + msm_offset->core_vendor_spec);
  727. /*
  728. * Select HS400 mode using the HC_SELECT_IN from VENDOR SPEC
  729. * register
  730. */
  731. if ((msm_host->tuning_done || ios.enhanced_strobe) &&
  732. !msm_host->calibration_done) {
  733. config = readl_relaxed(host->ioaddr +
  734. msm_offset->core_vendor_spec);
  735. config |= CORE_HC_SELECT_IN_HS400;
  736. config |= CORE_HC_SELECT_IN_EN;
  737. writel_relaxed(config, host->ioaddr +
  738. msm_offset->core_vendor_spec);
  739. }
  740. if (!msm_host->clk_rate && !msm_host->use_cdclp533) {
  741. /*
  742. * Poll on DLL_LOCK or DDR_DLL_LOCK bits in
  743. * core_dll_status to be set. This should get set
  744. * within 15 us at 200 MHz.
  745. */
  746. rc = readl_relaxed_poll_timeout(host->ioaddr +
  747. msm_offset->core_dll_status,
  748. dll_lock,
  749. (dll_lock &
  750. (CORE_DLL_LOCK |
  751. CORE_DDR_DLL_LOCK)), 10,
  752. 1000);
  753. if (rc == -ETIMEDOUT)
  754. pr_err("%s: Unable to get DLL_LOCK/DDR_DLL_LOCK, dll_status: 0x%08x\n",
  755. mmc_hostname(host->mmc), dll_lock);
  756. }
  757. /*
  758. * Make sure above writes impacting free running MCLK are completed
  759. * before changing the clk_rate at GCC.
  760. */
  761. wmb();
  762. }
  763. /*
  764. * sdhci_msm_hc_select_mode :- In general all timing modes are
  765. * controlled via UHS mode select in Host Control2 register.
  766. * eMMC specific HS200/HS400 doesn't have their respective modes
  767. * defined here, hence we use these values.
  768. *
  769. * HS200 - SDR104 (Since they both are equivalent in functionality)
  770. * HS400 - This involves multiple configurations
  771. * Initially SDR104 - when tuning is required as HS200
  772. * Then when switching to DDR @ 400MHz (HS400) we use
  773. * the vendor specific HC_SELECT_IN to control the mode.
  774. *
  775. * In addition to controlling the modes we also need to select the
  776. * correct input clock for DLL depending on the mode.
  777. *
  778. * HS400 - divided clock (free running MCLK/2)
  779. * All other modes - default (free running MCLK)
  780. */
  781. static void sdhci_msm_hc_select_mode(struct sdhci_host *host)
  782. {
  783. struct mmc_ios ios = host->mmc->ios;
  784. if (ios.timing == MMC_TIMING_MMC_HS400 ||
  785. host->flags & SDHCI_HS400_TUNING)
  786. msm_hc_select_hs400(host);
  787. else
  788. msm_hc_select_default(host);
  789. }
  790. static int sdhci_msm_cdclp533_calibration(struct sdhci_host *host)
  791. {
  792. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  793. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  794. u32 config, calib_done;
  795. int ret;
  796. const struct sdhci_msm_offset *msm_offset =
  797. msm_host->offset;
  798. pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
  799. /*
  800. * Retuning in HS400 (DDR mode) will fail, just reset the
  801. * tuning block and restore the saved tuning phase.
  802. */
  803. ret = msm_init_cm_dll(host);
  804. if (ret)
  805. goto out;
  806. /* Set the selected phase in delay line hw block */
  807. ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
  808. if (ret)
  809. goto out;
  810. config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config);
  811. config |= CORE_CMD_DAT_TRACK_SEL;
  812. writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config);
  813. config = readl_relaxed(host->ioaddr + msm_offset->core_ddr_200_cfg);
  814. config &= ~CORE_CDC_T4_DLY_SEL;
  815. writel_relaxed(config, host->ioaddr + msm_offset->core_ddr_200_cfg);
  816. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  817. config &= ~CORE_CDC_SWITCH_BYPASS_OFF;
  818. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  819. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  820. config |= CORE_CDC_SWITCH_RC_EN;
  821. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_GEN_CFG);
  822. config = readl_relaxed(host->ioaddr + msm_offset->core_ddr_200_cfg);
  823. config &= ~CORE_START_CDC_TRAFFIC;
  824. writel_relaxed(config, host->ioaddr + msm_offset->core_ddr_200_cfg);
  825. /* Perform CDC Register Initialization Sequence */
  826. writel_relaxed(0x11800EC, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  827. writel_relaxed(0x3011111, host->ioaddr + CORE_CSR_CDC_CTLR_CFG1);
  828. writel_relaxed(0x1201000, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  829. writel_relaxed(0x4, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG1);
  830. writel_relaxed(0xCB732020, host->ioaddr + CORE_CSR_CDC_REFCOUNT_CFG);
  831. writel_relaxed(0xB19, host->ioaddr + CORE_CSR_CDC_COARSE_CAL_CFG);
  832. writel_relaxed(0x4E2, host->ioaddr + CORE_CSR_CDC_DELAY_CFG);
  833. writel_relaxed(0x0, host->ioaddr + CORE_CDC_OFFSET_CFG);
  834. writel_relaxed(0x16334, host->ioaddr + CORE_CDC_SLAVE_DDA_CFG);
  835. /* CDC HW Calibration */
  836. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  837. config |= CORE_SW_TRIG_FULL_CALIB;
  838. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  839. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  840. config &= ~CORE_SW_TRIG_FULL_CALIB;
  841. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  842. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  843. config |= CORE_HW_AUTOCAL_ENA;
  844. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CTLR_CFG0);
  845. config = readl_relaxed(host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  846. config |= CORE_TIMER_ENA;
  847. writel_relaxed(config, host->ioaddr + CORE_CSR_CDC_CAL_TIMER_CFG0);
  848. ret = readl_relaxed_poll_timeout(host->ioaddr + CORE_CSR_CDC_STATUS0,
  849. calib_done,
  850. (calib_done & CORE_CALIBRATION_DONE),
  851. 1, 50);
  852. if (ret == -ETIMEDOUT) {
  853. pr_err("%s: %s: CDC calibration was not completed\n",
  854. mmc_hostname(host->mmc), __func__);
  855. goto out;
  856. }
  857. ret = readl_relaxed(host->ioaddr + CORE_CSR_CDC_STATUS0)
  858. & CORE_CDC_ERROR_CODE_MASK;
  859. if (ret) {
  860. pr_err("%s: %s: CDC error code %d\n",
  861. mmc_hostname(host->mmc), __func__, ret);
  862. ret = -EINVAL;
  863. goto out;
  864. }
  865. config = readl_relaxed(host->ioaddr + msm_offset->core_ddr_200_cfg);
  866. config |= CORE_START_CDC_TRAFFIC;
  867. writel_relaxed(config, host->ioaddr + msm_offset->core_ddr_200_cfg);
  868. out:
  869. pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
  870. __func__, ret);
  871. return ret;
  872. }
  873. static int sdhci_msm_cm_dll_sdc4_calibration(struct sdhci_host *host)
  874. {
  875. struct mmc_host *mmc = host->mmc;
  876. u32 dll_status, config, ddr_cfg_offset;
  877. int ret;
  878. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  879. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  880. const struct sdhci_msm_offset *msm_offset =
  881. sdhci_priv_msm_offset(host);
  882. pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
  883. /*
  884. * Currently the core_ddr_config register defaults to desired
  885. * configuration on reset. Currently reprogramming the power on
  886. * reset (POR) value in case it might have been modified by
  887. * bootloaders. In the future, if this changes, then the desired
  888. * values will need to be programmed appropriately.
  889. */
  890. if (msm_host->updated_ddr_cfg)
  891. ddr_cfg_offset = msm_offset->core_ddr_config;
  892. else
  893. ddr_cfg_offset = msm_offset->core_ddr_config_old;
  894. writel_relaxed(msm_host->ddr_config, host->ioaddr + ddr_cfg_offset);
  895. if (mmc->ios.enhanced_strobe) {
  896. config = readl_relaxed(host->ioaddr +
  897. msm_offset->core_ddr_200_cfg);
  898. config |= CORE_CMDIN_RCLK_EN;
  899. writel_relaxed(config, host->ioaddr +
  900. msm_offset->core_ddr_200_cfg);
  901. }
  902. config = readl_relaxed(host->ioaddr + msm_offset->core_dll_config_2);
  903. config |= CORE_DDR_CAL_EN;
  904. writel_relaxed(config, host->ioaddr + msm_offset->core_dll_config_2);
  905. ret = readl_relaxed_poll_timeout(host->ioaddr +
  906. msm_offset->core_dll_status,
  907. dll_status,
  908. (dll_status & CORE_DDR_DLL_LOCK),
  909. 10, 1000);
  910. if (ret == -ETIMEDOUT) {
  911. pr_err("%s: %s: CM_DLL_SDC4 calibration was not completed\n",
  912. mmc_hostname(host->mmc), __func__);
  913. goto out;
  914. }
  915. /*
  916. * Set CORE_PWRSAVE_DLL bit in CORE_VENDOR_SPEC3.
  917. * When MCLK is gated OFF, it is not gated for less than 0.5us
  918. * and MCLK must be switched on for at-least 1us before DATA
  919. * starts coming. Controllers with 14lpp and later tech DLL cannot
  920. * guarantee above requirement. So PWRSAVE_DLL should not be
  921. * turned on for host controllers using this DLL.
  922. */
  923. if (!msm_host->use_14lpp_dll_reset) {
  924. config = readl_relaxed(host->ioaddr +
  925. msm_offset->core_vendor_spec3);
  926. config |= CORE_PWRSAVE_DLL;
  927. writel_relaxed(config, host->ioaddr +
  928. msm_offset->core_vendor_spec3);
  929. }
  930. /*
  931. * Drain writebuffer to ensure above DLL calibration
  932. * and PWRSAVE DLL is enabled.
  933. */
  934. wmb();
  935. out:
  936. pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
  937. __func__, ret);
  938. return ret;
  939. }
  940. static int sdhci_msm_hs400_dll_calibration(struct sdhci_host *host)
  941. {
  942. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  943. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  944. struct mmc_host *mmc = host->mmc;
  945. int ret;
  946. u32 config;
  947. const struct sdhci_msm_offset *msm_offset =
  948. msm_host->offset;
  949. pr_debug("%s: %s: Enter\n", mmc_hostname(host->mmc), __func__);
  950. /*
  951. * Retuning in HS400 (DDR mode) will fail, just reset the
  952. * tuning block and restore the saved tuning phase.
  953. */
  954. ret = msm_init_cm_dll(host);
  955. if (ret)
  956. goto out;
  957. if (!mmc->ios.enhanced_strobe) {
  958. /* Set the selected phase in delay line hw block */
  959. ret = msm_config_cm_dll_phase(host,
  960. msm_host->saved_tuning_phase);
  961. if (ret)
  962. goto out;
  963. config = readl_relaxed(host->ioaddr +
  964. msm_offset->core_dll_config);
  965. config |= CORE_CMD_DAT_TRACK_SEL;
  966. writel_relaxed(config, host->ioaddr +
  967. msm_offset->core_dll_config);
  968. }
  969. if (msm_host->use_cdclp533)
  970. ret = sdhci_msm_cdclp533_calibration(host);
  971. else
  972. ret = sdhci_msm_cm_dll_sdc4_calibration(host);
  973. out:
  974. pr_debug("%s: %s: Exit, ret %d\n", mmc_hostname(host->mmc),
  975. __func__, ret);
  976. return ret;
  977. }
  978. static bool sdhci_msm_is_tuning_needed(struct sdhci_host *host)
  979. {
  980. struct mmc_ios *ios = &host->mmc->ios;
  981. /*
  982. * Tuning is required for SDR104, HS200 and HS400 cards and
  983. * if clock frequency is greater than 100MHz in these modes.
  984. */
  985. if (host->clock <= CORE_FREQ_100MHZ ||
  986. !(ios->timing == MMC_TIMING_MMC_HS400 ||
  987. ios->timing == MMC_TIMING_MMC_HS200 ||
  988. ios->timing == MMC_TIMING_UHS_SDR104) ||
  989. ios->enhanced_strobe)
  990. return false;
  991. return true;
  992. }
  993. static int sdhci_msm_restore_sdr_dll_config(struct sdhci_host *host)
  994. {
  995. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  996. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  997. int ret;
  998. /*
  999. * SDR DLL comes into picture only for timing modes which needs
  1000. * tuning.
  1001. */
  1002. if (!sdhci_msm_is_tuning_needed(host))
  1003. return 0;
  1004. /* Reset the tuning block */
  1005. ret = msm_init_cm_dll(host);
  1006. if (ret)
  1007. return ret;
  1008. /* Restore the tuning block */
  1009. ret = msm_config_cm_dll_phase(host, msm_host->saved_tuning_phase);
  1010. return ret;
  1011. }
  1012. static void sdhci_msm_set_cdr(struct sdhci_host *host, bool enable)
  1013. {
  1014. const struct sdhci_msm_offset *msm_offset = sdhci_priv_msm_offset(host);
  1015. u32 config, oldconfig = readl_relaxed(host->ioaddr +
  1016. msm_offset->core_dll_config);
  1017. config = oldconfig;
  1018. if (enable) {
  1019. config |= CORE_CDR_EN;
  1020. config &= ~CORE_CDR_EXT_EN;
  1021. } else {
  1022. config &= ~CORE_CDR_EN;
  1023. config |= CORE_CDR_EXT_EN;
  1024. }
  1025. if (config != oldconfig) {
  1026. writel_relaxed(config, host->ioaddr +
  1027. msm_offset->core_dll_config);
  1028. }
  1029. }
  1030. static int sdhci_msm_execute_tuning(struct mmc_host *mmc, u32 opcode)
  1031. {
  1032. struct sdhci_host *host = mmc_priv(mmc);
  1033. int tuning_seq_cnt = 10;
  1034. u8 phase, tuned_phases[16], tuned_phase_cnt = 0;
  1035. int rc;
  1036. struct mmc_ios ios = host->mmc->ios;
  1037. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1038. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1039. if (!sdhci_msm_is_tuning_needed(host)) {
  1040. msm_host->use_cdr = false;
  1041. sdhci_msm_set_cdr(host, false);
  1042. return 0;
  1043. }
  1044. /* Clock-Data-Recovery used to dynamically adjust RX sampling point */
  1045. msm_host->use_cdr = true;
  1046. /*
  1047. * Clear tuning_done flag before tuning to ensure proper
  1048. * HS400 settings.
  1049. */
  1050. msm_host->tuning_done = 0;
  1051. /*
  1052. * For HS400 tuning in HS200 timing requires:
  1053. * - select MCLK/2 in VENDOR_SPEC
  1054. * - program MCLK to 400MHz (or nearest supported) in GCC
  1055. */
  1056. if (host->flags & SDHCI_HS400_TUNING) {
  1057. sdhci_msm_hc_select_mode(host);
  1058. msm_set_clock_rate_for_bus_mode(host, ios.clock);
  1059. host->flags &= ~SDHCI_HS400_TUNING;
  1060. }
  1061. retry:
  1062. /* First of all reset the tuning block */
  1063. rc = msm_init_cm_dll(host);
  1064. if (rc)
  1065. return rc;
  1066. phase = 0;
  1067. do {
  1068. /* Set the phase in delay line hw block */
  1069. rc = msm_config_cm_dll_phase(host, phase);
  1070. if (rc)
  1071. return rc;
  1072. rc = mmc_send_tuning(mmc, opcode, NULL);
  1073. if (!rc) {
  1074. /* Tuning is successful at this tuning point */
  1075. tuned_phases[tuned_phase_cnt++] = phase;
  1076. dev_dbg(mmc_dev(mmc), "%s: Found good phase = %d\n",
  1077. mmc_hostname(mmc), phase);
  1078. }
  1079. } while (++phase < ARRAY_SIZE(tuned_phases));
  1080. if (tuned_phase_cnt) {
  1081. if (tuned_phase_cnt == ARRAY_SIZE(tuned_phases)) {
  1082. /*
  1083. * All phases valid is _almost_ as bad as no phases
  1084. * valid. Probably all phases are not really reliable
  1085. * but we didn't detect where the unreliable place is.
  1086. * That means we'll essentially be guessing and hoping
  1087. * we get a good phase. Better to try a few times.
  1088. */
  1089. dev_dbg(mmc_dev(mmc), "%s: All phases valid; try again\n",
  1090. mmc_hostname(mmc));
  1091. if (--tuning_seq_cnt) {
  1092. tuned_phase_cnt = 0;
  1093. goto retry;
  1094. }
  1095. }
  1096. rc = msm_find_most_appropriate_phase(host, tuned_phases,
  1097. tuned_phase_cnt);
  1098. if (rc < 0)
  1099. return rc;
  1100. else
  1101. phase = rc;
  1102. /*
  1103. * Finally set the selected phase in delay
  1104. * line hw block.
  1105. */
  1106. rc = msm_config_cm_dll_phase(host, phase);
  1107. if (rc)
  1108. return rc;
  1109. msm_host->saved_tuning_phase = phase;
  1110. dev_dbg(mmc_dev(mmc), "%s: Setting the tuning phase to %d\n",
  1111. mmc_hostname(mmc), phase);
  1112. } else {
  1113. if (--tuning_seq_cnt)
  1114. goto retry;
  1115. /* Tuning failed */
  1116. dev_dbg(mmc_dev(mmc), "%s: No tuning point found\n",
  1117. mmc_hostname(mmc));
  1118. rc = -EIO;
  1119. }
  1120. if (!rc)
  1121. msm_host->tuning_done = true;
  1122. return rc;
  1123. }
  1124. /*
  1125. * sdhci_msm_hs400 - Calibrate the DLL for HS400 bus speed mode operation.
  1126. * This needs to be done for both tuning and enhanced_strobe mode.
  1127. * DLL operation is only needed for clock > 100MHz. For clock <= 100MHz
  1128. * fixed feedback clock is used.
  1129. */
  1130. static void sdhci_msm_hs400(struct sdhci_host *host, struct mmc_ios *ios)
  1131. {
  1132. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1133. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1134. int ret;
  1135. if (host->clock > CORE_FREQ_100MHZ &&
  1136. (msm_host->tuning_done || ios->enhanced_strobe) &&
  1137. !msm_host->calibration_done) {
  1138. ret = sdhci_msm_hs400_dll_calibration(host);
  1139. if (!ret)
  1140. msm_host->calibration_done = true;
  1141. else
  1142. pr_err("%s: Failed to calibrate DLL for hs400 mode (%d)\n",
  1143. mmc_hostname(host->mmc), ret);
  1144. }
  1145. }
  1146. static void sdhci_msm_set_uhs_signaling(struct sdhci_host *host,
  1147. unsigned int uhs)
  1148. {
  1149. struct mmc_host *mmc = host->mmc;
  1150. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1151. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1152. u16 ctrl_2;
  1153. u32 config;
  1154. const struct sdhci_msm_offset *msm_offset =
  1155. msm_host->offset;
  1156. ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1157. /* Select Bus Speed Mode for host */
  1158. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1159. switch (uhs) {
  1160. case MMC_TIMING_UHS_SDR12:
  1161. ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
  1162. break;
  1163. case MMC_TIMING_UHS_SDR25:
  1164. ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
  1165. break;
  1166. case MMC_TIMING_UHS_SDR50:
  1167. ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
  1168. break;
  1169. case MMC_TIMING_MMC_HS400:
  1170. case MMC_TIMING_MMC_HS200:
  1171. case MMC_TIMING_UHS_SDR104:
  1172. ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
  1173. break;
  1174. case MMC_TIMING_UHS_DDR50:
  1175. case MMC_TIMING_MMC_DDR52:
  1176. ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
  1177. break;
  1178. }
  1179. /*
  1180. * When clock frequency is less than 100MHz, the feedback clock must be
  1181. * provided and DLL must not be used so that tuning can be skipped. To
  1182. * provide feedback clock, the mode selection can be any value less
  1183. * than 3'b011 in bits [2:0] of HOST CONTROL2 register.
  1184. */
  1185. if (host->clock <= CORE_FREQ_100MHZ) {
  1186. if (uhs == MMC_TIMING_MMC_HS400 ||
  1187. uhs == MMC_TIMING_MMC_HS200 ||
  1188. uhs == MMC_TIMING_UHS_SDR104)
  1189. ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
  1190. /*
  1191. * DLL is not required for clock <= 100MHz
  1192. * Thus, make sure DLL it is disabled when not required
  1193. */
  1194. config = readl_relaxed(host->ioaddr +
  1195. msm_offset->core_dll_config);
  1196. config |= CORE_DLL_RST;
  1197. writel_relaxed(config, host->ioaddr +
  1198. msm_offset->core_dll_config);
  1199. config = readl_relaxed(host->ioaddr +
  1200. msm_offset->core_dll_config);
  1201. config |= CORE_DLL_PDN;
  1202. writel_relaxed(config, host->ioaddr +
  1203. msm_offset->core_dll_config);
  1204. /*
  1205. * The DLL needs to be restored and CDCLP533 recalibrated
  1206. * when the clock frequency is set back to 400MHz.
  1207. */
  1208. msm_host->calibration_done = false;
  1209. }
  1210. dev_dbg(mmc_dev(mmc), "%s: clock=%u uhs=%u ctrl_2=0x%x\n",
  1211. mmc_hostname(host->mmc), host->clock, uhs, ctrl_2);
  1212. sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
  1213. if (mmc->ios.timing == MMC_TIMING_MMC_HS400)
  1214. sdhci_msm_hs400(host, &mmc->ios);
  1215. }
  1216. static int sdhci_msm_set_pincfg(struct sdhci_msm_host *msm_host, bool level)
  1217. {
  1218. struct platform_device *pdev = msm_host->pdev;
  1219. int ret;
  1220. if (level)
  1221. ret = pinctrl_pm_select_default_state(&pdev->dev);
  1222. else
  1223. ret = pinctrl_pm_select_sleep_state(&pdev->dev);
  1224. return ret;
  1225. }
  1226. static void msm_config_vmmc_regulator(struct mmc_host *mmc, bool hpm)
  1227. {
  1228. int load;
  1229. if (!hpm)
  1230. load = 0;
  1231. else if (!mmc->card)
  1232. load = max(MMC_VMMC_MAX_LOAD_UA, SD_VMMC_MAX_LOAD_UA);
  1233. else if (mmc_card_mmc(mmc->card))
  1234. load = MMC_VMMC_MAX_LOAD_UA;
  1235. else if (mmc_card_sd(mmc->card))
  1236. load = SD_VMMC_MAX_LOAD_UA;
  1237. else
  1238. return;
  1239. regulator_set_load(mmc->supply.vmmc, load);
  1240. }
  1241. static void msm_config_vqmmc_regulator(struct mmc_host *mmc, bool hpm)
  1242. {
  1243. int load;
  1244. if (!hpm)
  1245. load = 0;
  1246. else if (!mmc->card)
  1247. load = max(MMC_VQMMC_MAX_LOAD_UA, SD_VQMMC_MAX_LOAD_UA);
  1248. else if (mmc_card_sd(mmc->card))
  1249. load = SD_VQMMC_MAX_LOAD_UA;
  1250. else
  1251. return;
  1252. regulator_set_load(mmc->supply.vqmmc, load);
  1253. }
  1254. static int sdhci_msm_set_vmmc(struct sdhci_msm_host *msm_host,
  1255. struct mmc_host *mmc, bool hpm)
  1256. {
  1257. if (IS_ERR(mmc->supply.vmmc))
  1258. return 0;
  1259. msm_config_vmmc_regulator(mmc, hpm);
  1260. return mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, mmc->ios.vdd);
  1261. }
  1262. static int msm_toggle_vqmmc(struct sdhci_msm_host *msm_host,
  1263. struct mmc_host *mmc, bool level)
  1264. {
  1265. int ret;
  1266. struct mmc_ios ios;
  1267. if (msm_host->vqmmc_enabled == level)
  1268. return 0;
  1269. msm_config_vqmmc_regulator(mmc, level);
  1270. if (level) {
  1271. /* Set the IO voltage regulator to default voltage level */
  1272. if (msm_host->caps_0 & CORE_3_0V_SUPPORT)
  1273. ios.signal_voltage = MMC_SIGNAL_VOLTAGE_330;
  1274. else if (msm_host->caps_0 & CORE_1_8V_SUPPORT)
  1275. ios.signal_voltage = MMC_SIGNAL_VOLTAGE_180;
  1276. if (msm_host->caps_0 & CORE_VOLT_SUPPORT) {
  1277. ret = mmc_regulator_set_vqmmc(mmc, &ios);
  1278. if (ret < 0) {
  1279. dev_err(mmc_dev(mmc), "%s: vqmmc set volgate failed: %d\n",
  1280. mmc_hostname(mmc), ret);
  1281. goto out;
  1282. }
  1283. }
  1284. ret = regulator_enable(mmc->supply.vqmmc);
  1285. } else {
  1286. ret = regulator_disable(mmc->supply.vqmmc);
  1287. }
  1288. if (ret)
  1289. dev_err(mmc_dev(mmc), "%s: vqmm %sable failed: %d\n",
  1290. mmc_hostname(mmc), level ? "en":"dis", ret);
  1291. else
  1292. msm_host->vqmmc_enabled = level;
  1293. out:
  1294. return ret;
  1295. }
  1296. static int msm_config_vqmmc_mode(struct sdhci_msm_host *msm_host,
  1297. struct mmc_host *mmc, bool hpm)
  1298. {
  1299. int load, ret;
  1300. load = hpm ? MMC_VQMMC_MAX_LOAD_UA : 0;
  1301. ret = regulator_set_load(mmc->supply.vqmmc, load);
  1302. if (ret)
  1303. dev_err(mmc_dev(mmc), "%s: vqmmc set load failed: %d\n",
  1304. mmc_hostname(mmc), ret);
  1305. return ret;
  1306. }
  1307. static int sdhci_msm_set_vqmmc(struct sdhci_msm_host *msm_host,
  1308. struct mmc_host *mmc, bool level)
  1309. {
  1310. int ret;
  1311. bool always_on;
  1312. if (IS_ERR(mmc->supply.vqmmc) ||
  1313. (mmc->ios.power_mode == MMC_POWER_UNDEFINED))
  1314. return 0;
  1315. /*
  1316. * For eMMC don't turn off Vqmmc, Instead just configure it in LPM
  1317. * and HPM modes by setting the corresponding load.
  1318. *
  1319. * Till eMMC is initialized (i.e. always_on == 0), just turn on/off
  1320. * Vqmmc. Vqmmc gets turned off only if init fails and mmc_power_off
  1321. * gets invoked. Once eMMC is initialized (i.e. always_on == 1),
  1322. * Vqmmc should remain ON, So just set the load instead of turning it
  1323. * off/on.
  1324. */
  1325. always_on = !mmc_card_is_removable(mmc) &&
  1326. mmc->card && mmc_card_mmc(mmc->card);
  1327. if (always_on)
  1328. ret = msm_config_vqmmc_mode(msm_host, mmc, level);
  1329. else
  1330. ret = msm_toggle_vqmmc(msm_host, mmc, level);
  1331. return ret;
  1332. }
  1333. static inline void sdhci_msm_init_pwr_irq_wait(struct sdhci_msm_host *msm_host)
  1334. {
  1335. init_waitqueue_head(&msm_host->pwr_irq_wait);
  1336. }
  1337. static inline void sdhci_msm_complete_pwr_irq_wait(
  1338. struct sdhci_msm_host *msm_host)
  1339. {
  1340. wake_up(&msm_host->pwr_irq_wait);
  1341. }
  1342. /*
  1343. * sdhci_msm_check_power_status API should be called when registers writes
  1344. * which can toggle sdhci IO bus ON/OFF or change IO lines HIGH/LOW happens.
  1345. * To what state the register writes will change the IO lines should be passed
  1346. * as the argument req_type. This API will check whether the IO line's state
  1347. * is already the expected state and will wait for power irq only if
  1348. * power irq is expected to be triggered based on the current IO line state
  1349. * and expected IO line state.
  1350. */
  1351. static void sdhci_msm_check_power_status(struct sdhci_host *host, u32 req_type)
  1352. {
  1353. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1354. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1355. struct mmc_host *mmc = host->mmc;
  1356. bool done = false;
  1357. u32 val = SWITCHABLE_SIGNALING_VOLTAGE;
  1358. const struct sdhci_msm_offset *msm_offset =
  1359. msm_host->offset;
  1360. pr_debug("%s: %s: request %d curr_pwr_state %x curr_io_level %x\n",
  1361. mmc_hostname(host->mmc), __func__, req_type,
  1362. msm_host->curr_pwr_state, msm_host->curr_io_level);
  1363. /*
  1364. * The power interrupt will not be generated for signal voltage
  1365. * switches if SWITCHABLE_SIGNALING_VOLTAGE in MCI_GENERICS is not set.
  1366. * Since sdhci-msm-v5, this bit has been removed and SW must consider
  1367. * it as always set.
  1368. */
  1369. if (!msm_host->mci_removed)
  1370. val = msm_host_readl(msm_host, host,
  1371. msm_offset->core_generics);
  1372. if ((req_type & REQ_IO_HIGH || req_type & REQ_IO_LOW) &&
  1373. !(val & SWITCHABLE_SIGNALING_VOLTAGE)) {
  1374. return;
  1375. }
  1376. /*
  1377. * The IRQ for request type IO High/LOW will be generated when -
  1378. * there is a state change in 1.8V enable bit (bit 3) of
  1379. * SDHCI_HOST_CONTROL2 register. The reset state of that bit is 0
  1380. * which indicates 3.3V IO voltage. So, when MMC core layer tries
  1381. * to set it to 3.3V before card detection happens, the
  1382. * IRQ doesn't get triggered as there is no state change in this bit.
  1383. * The driver already handles this case by changing the IO voltage
  1384. * level to high as part of controller power up sequence. Hence, check
  1385. * for host->pwr to handle a case where IO voltage high request is
  1386. * issued even before controller power up.
  1387. */
  1388. if ((req_type & REQ_IO_HIGH) && !host->pwr) {
  1389. pr_debug("%s: do not wait for power IRQ that never comes, req_type: %d\n",
  1390. mmc_hostname(host->mmc), req_type);
  1391. return;
  1392. }
  1393. if ((req_type & msm_host->curr_pwr_state) ||
  1394. (req_type & msm_host->curr_io_level))
  1395. done = true;
  1396. /*
  1397. * This is needed here to handle cases where register writes will
  1398. * not change the current bus state or io level of the controller.
  1399. * In this case, no power irq will be triggerred and we should
  1400. * not wait.
  1401. */
  1402. if (!done) {
  1403. if (!wait_event_timeout(msm_host->pwr_irq_wait,
  1404. msm_host->pwr_irq_flag,
  1405. msecs_to_jiffies(MSM_PWR_IRQ_TIMEOUT_MS)))
  1406. dev_warn(&msm_host->pdev->dev,
  1407. "%s: pwr_irq for req: (%d) timed out\n",
  1408. mmc_hostname(host->mmc), req_type);
  1409. }
  1410. if ((req_type & REQ_BUS_ON) && mmc->card && !mmc->ops->get_cd(mmc)) {
  1411. sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
  1412. host->pwr = 0;
  1413. }
  1414. pr_debug("%s: %s: request %d done\n", mmc_hostname(host->mmc),
  1415. __func__, req_type);
  1416. }
  1417. static void sdhci_msm_dump_pwr_ctrl_regs(struct sdhci_host *host)
  1418. {
  1419. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1420. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1421. const struct sdhci_msm_offset *msm_offset =
  1422. msm_host->offset;
  1423. pr_err("%s: PWRCTL_STATUS: 0x%08x | PWRCTL_MASK: 0x%08x | PWRCTL_CTL: 0x%08x\n",
  1424. mmc_hostname(host->mmc),
  1425. msm_host_readl(msm_host, host, msm_offset->core_pwrctl_status),
  1426. msm_host_readl(msm_host, host, msm_offset->core_pwrctl_mask),
  1427. msm_host_readl(msm_host, host, msm_offset->core_pwrctl_ctl));
  1428. }
  1429. static void sdhci_msm_handle_pwr_irq(struct sdhci_host *host, int irq)
  1430. {
  1431. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1432. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1433. struct mmc_host *mmc = host->mmc;
  1434. u32 irq_status, irq_ack = 0;
  1435. int retry = 10, ret;
  1436. u32 pwr_state = 0, io_level = 0;
  1437. u32 config;
  1438. const struct sdhci_msm_offset *msm_offset = msm_host->offset;
  1439. irq_status = msm_host_readl(msm_host, host,
  1440. msm_offset->core_pwrctl_status);
  1441. irq_status &= INT_MASK;
  1442. msm_host_writel(msm_host, irq_status, host,
  1443. msm_offset->core_pwrctl_clear);
  1444. /*
  1445. * There is a rare HW scenario where the first clear pulse could be
  1446. * lost when actual reset and clear/read of status register is
  1447. * happening at a time. Hence, retry for at least 10 times to make
  1448. * sure status register is cleared. Otherwise, this will result in
  1449. * a spurious power IRQ resulting in system instability.
  1450. */
  1451. while (irq_status & msm_host_readl(msm_host, host,
  1452. msm_offset->core_pwrctl_status)) {
  1453. if (retry == 0) {
  1454. pr_err("%s: Timedout clearing (0x%x) pwrctl status register\n",
  1455. mmc_hostname(host->mmc), irq_status);
  1456. sdhci_msm_dump_pwr_ctrl_regs(host);
  1457. WARN_ON(1);
  1458. break;
  1459. }
  1460. msm_host_writel(msm_host, irq_status, host,
  1461. msm_offset->core_pwrctl_clear);
  1462. retry--;
  1463. udelay(10);
  1464. }
  1465. if ((irq_status & CORE_PWRCTL_BUS_ON) && mmc->card &&
  1466. !mmc->ops->get_cd(mmc)) {
  1467. msm_host_writel(msm_host, CORE_PWRCTL_BUS_FAIL, host,
  1468. msm_offset->core_pwrctl_ctl);
  1469. return;
  1470. }
  1471. /* Handle BUS ON/OFF*/
  1472. if (irq_status & CORE_PWRCTL_BUS_ON) {
  1473. pwr_state = REQ_BUS_ON;
  1474. io_level = REQ_IO_HIGH;
  1475. }
  1476. if (irq_status & CORE_PWRCTL_BUS_OFF) {
  1477. pwr_state = REQ_BUS_OFF;
  1478. io_level = REQ_IO_LOW;
  1479. }
  1480. if (pwr_state) {
  1481. ret = sdhci_msm_set_vmmc(msm_host, mmc,
  1482. pwr_state & REQ_BUS_ON);
  1483. if (!ret)
  1484. ret = sdhci_msm_set_vqmmc(msm_host, mmc,
  1485. pwr_state & REQ_BUS_ON);
  1486. if (!ret)
  1487. ret = sdhci_msm_set_pincfg(msm_host,
  1488. pwr_state & REQ_BUS_ON);
  1489. if (!ret)
  1490. irq_ack |= CORE_PWRCTL_BUS_SUCCESS;
  1491. else
  1492. irq_ack |= CORE_PWRCTL_BUS_FAIL;
  1493. }
  1494. /* Handle IO LOW/HIGH */
  1495. if (irq_status & CORE_PWRCTL_IO_LOW)
  1496. io_level = REQ_IO_LOW;
  1497. if (irq_status & CORE_PWRCTL_IO_HIGH)
  1498. io_level = REQ_IO_HIGH;
  1499. if (io_level)
  1500. irq_ack |= CORE_PWRCTL_IO_SUCCESS;
  1501. if (io_level && !IS_ERR(mmc->supply.vqmmc) && !pwr_state) {
  1502. ret = mmc_regulator_set_vqmmc(mmc, &mmc->ios);
  1503. if (ret < 0) {
  1504. dev_err(mmc_dev(mmc), "%s: IO_level setting failed(%d). signal_voltage: %d, vdd: %d irq_status: 0x%08x\n",
  1505. mmc_hostname(mmc), ret,
  1506. mmc->ios.signal_voltage, mmc->ios.vdd,
  1507. irq_status);
  1508. irq_ack |= CORE_PWRCTL_IO_FAIL;
  1509. }
  1510. }
  1511. /*
  1512. * The driver has to acknowledge the interrupt, switch voltages and
  1513. * report back if it succeded or not to this register. The voltage
  1514. * switches are handled by the sdhci core, so just report success.
  1515. */
  1516. msm_host_writel(msm_host, irq_ack, host,
  1517. msm_offset->core_pwrctl_ctl);
  1518. /*
  1519. * If we don't have info regarding the voltage levels supported by
  1520. * regulators, don't change the IO PAD PWR SWITCH.
  1521. */
  1522. if (msm_host->caps_0 & CORE_VOLT_SUPPORT) {
  1523. u32 new_config;
  1524. /*
  1525. * We should unset IO PAD PWR switch only if the register write
  1526. * can set IO lines high and the regulator also switches to 3 V.
  1527. * Else, we should keep the IO PAD PWR switch set.
  1528. * This is applicable to certain targets where eMMC vccq supply
  1529. * is only 1.8V. In such targets, even during REQ_IO_HIGH, the
  1530. * IO PAD PWR switch must be kept set to reflect actual
  1531. * regulator voltage. This way, during initialization of
  1532. * controllers with only 1.8V, we will set the IO PAD bit
  1533. * without waiting for a REQ_IO_LOW.
  1534. */
  1535. config = readl_relaxed(host->ioaddr +
  1536. msm_offset->core_vendor_spec);
  1537. new_config = config;
  1538. if ((io_level & REQ_IO_HIGH) &&
  1539. (msm_host->caps_0 & CORE_3_0V_SUPPORT))
  1540. new_config &= ~CORE_IO_PAD_PWR_SWITCH;
  1541. else if ((io_level & REQ_IO_LOW) ||
  1542. (msm_host->caps_0 & CORE_1_8V_SUPPORT))
  1543. new_config |= CORE_IO_PAD_PWR_SWITCH;
  1544. if (config ^ new_config)
  1545. writel_relaxed(new_config, host->ioaddr +
  1546. msm_offset->core_vendor_spec);
  1547. }
  1548. if (pwr_state)
  1549. msm_host->curr_pwr_state = pwr_state;
  1550. if (io_level)
  1551. msm_host->curr_io_level = io_level;
  1552. dev_dbg(mmc_dev(mmc), "%s: %s: Handled IRQ(%d), irq_status=0x%x, ack=0x%x\n",
  1553. mmc_hostname(msm_host->mmc), __func__, irq, irq_status,
  1554. irq_ack);
  1555. }
  1556. static irqreturn_t sdhci_msm_pwr_irq(int irq, void *data)
  1557. {
  1558. struct sdhci_host *host = (struct sdhci_host *)data;
  1559. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1560. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1561. sdhci_msm_handle_pwr_irq(host, irq);
  1562. msm_host->pwr_irq_flag = 1;
  1563. sdhci_msm_complete_pwr_irq_wait(msm_host);
  1564. return IRQ_HANDLED;
  1565. }
  1566. static unsigned int sdhci_msm_get_max_clock(struct sdhci_host *host)
  1567. {
  1568. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1569. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1570. struct clk *core_clk = msm_host->bulk_clks[0].clk;
  1571. return clk_round_rate(core_clk, ULONG_MAX);
  1572. }
  1573. static unsigned int sdhci_msm_get_min_clock(struct sdhci_host *host)
  1574. {
  1575. return SDHCI_MSM_MIN_CLOCK;
  1576. }
  1577. /*
  1578. * __sdhci_msm_set_clock - sdhci_msm clock control.
  1579. *
  1580. * Description:
  1581. * MSM controller does not use internal divider and
  1582. * instead directly control the GCC clock as per
  1583. * HW recommendation.
  1584. **/
  1585. static void __sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
  1586. {
  1587. u16 clk;
  1588. sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
  1589. if (clock == 0)
  1590. return;
  1591. /*
  1592. * MSM controller do not use clock divider.
  1593. * Thus read SDHCI_CLOCK_CONTROL and only enable
  1594. * clock with no divider value programmed.
  1595. */
  1596. clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
  1597. sdhci_enable_clk(host, clk);
  1598. }
  1599. /* sdhci_msm_set_clock - Called with (host->lock) spinlock held. */
  1600. static void sdhci_msm_set_clock(struct sdhci_host *host, unsigned int clock)
  1601. {
  1602. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1603. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1604. if (!clock) {
  1605. host->mmc->actual_clock = msm_host->clk_rate = 0;
  1606. goto out;
  1607. }
  1608. sdhci_msm_hc_select_mode(host);
  1609. msm_set_clock_rate_for_bus_mode(host, clock);
  1610. out:
  1611. __sdhci_msm_set_clock(host, clock);
  1612. }
  1613. /*****************************************************************************\
  1614. * *
  1615. * Inline Crypto Engine (ICE) support *
  1616. * *
  1617. \*****************************************************************************/
  1618. #ifdef CONFIG_MMC_CRYPTO
  1619. static int sdhci_msm_ice_init(struct sdhci_msm_host *msm_host,
  1620. struct cqhci_host *cq_host)
  1621. {
  1622. struct mmc_host *mmc = msm_host->mmc;
  1623. struct device *dev = mmc_dev(mmc);
  1624. struct qcom_ice *ice;
  1625. if (!(cqhci_readl(cq_host, CQHCI_CAP) & CQHCI_CAP_CS))
  1626. return 0;
  1627. ice = devm_of_qcom_ice_get(dev);
  1628. if (ice == ERR_PTR(-EOPNOTSUPP)) {
  1629. dev_warn(dev, "Disabling inline encryption support\n");
  1630. ice = NULL;
  1631. }
  1632. if (IS_ERR_OR_NULL(ice))
  1633. return PTR_ERR_OR_ZERO(ice);
  1634. msm_host->ice = ice;
  1635. mmc->caps2 |= MMC_CAP2_CRYPTO;
  1636. return 0;
  1637. }
  1638. static void sdhci_msm_ice_enable(struct sdhci_msm_host *msm_host)
  1639. {
  1640. if (msm_host->mmc->caps2 & MMC_CAP2_CRYPTO)
  1641. qcom_ice_enable(msm_host->ice);
  1642. }
  1643. static __maybe_unused int sdhci_msm_ice_resume(struct sdhci_msm_host *msm_host)
  1644. {
  1645. if (msm_host->mmc->caps2 & MMC_CAP2_CRYPTO)
  1646. return qcom_ice_resume(msm_host->ice);
  1647. return 0;
  1648. }
  1649. static __maybe_unused int sdhci_msm_ice_suspend(struct sdhci_msm_host *msm_host)
  1650. {
  1651. if (msm_host->mmc->caps2 & MMC_CAP2_CRYPTO)
  1652. return qcom_ice_suspend(msm_host->ice);
  1653. return 0;
  1654. }
  1655. /*
  1656. * Program a key into a QC ICE keyslot, or evict a keyslot. QC ICE requires
  1657. * vendor-specific SCM calls for this; it doesn't support the standard way.
  1658. */
  1659. static int sdhci_msm_program_key(struct cqhci_host *cq_host,
  1660. const union cqhci_crypto_cfg_entry *cfg,
  1661. int slot)
  1662. {
  1663. struct sdhci_host *host = mmc_priv(cq_host->mmc);
  1664. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1665. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1666. union cqhci_crypto_cap_entry cap;
  1667. if (!(cfg->config_enable & CQHCI_CRYPTO_CONFIGURATION_ENABLE))
  1668. return qcom_ice_evict_key(msm_host->ice, slot);
  1669. /* Only AES-256-XTS has been tested so far. */
  1670. cap = cq_host->crypto_cap_array[cfg->crypto_cap_idx];
  1671. if (cap.algorithm_id != CQHCI_CRYPTO_ALG_AES_XTS ||
  1672. cap.key_size != CQHCI_CRYPTO_KEY_SIZE_256)
  1673. return -EINVAL;
  1674. return qcom_ice_program_key(msm_host->ice,
  1675. QCOM_ICE_CRYPTO_ALG_AES_XTS,
  1676. QCOM_ICE_CRYPTO_KEY_SIZE_256,
  1677. cfg->crypto_key,
  1678. cfg->data_unit_size, slot);
  1679. }
  1680. #else /* CONFIG_MMC_CRYPTO */
  1681. static inline int sdhci_msm_ice_init(struct sdhci_msm_host *msm_host,
  1682. struct cqhci_host *cq_host)
  1683. {
  1684. return 0;
  1685. }
  1686. static inline void sdhci_msm_ice_enable(struct sdhci_msm_host *msm_host)
  1687. {
  1688. }
  1689. static inline __maybe_unused int
  1690. sdhci_msm_ice_resume(struct sdhci_msm_host *msm_host)
  1691. {
  1692. return 0;
  1693. }
  1694. static inline __maybe_unused int
  1695. sdhci_msm_ice_suspend(struct sdhci_msm_host *msm_host)
  1696. {
  1697. return 0;
  1698. }
  1699. #endif /* !CONFIG_MMC_CRYPTO */
  1700. /*****************************************************************************\
  1701. * *
  1702. * MSM Command Queue Engine (CQE) *
  1703. * *
  1704. \*****************************************************************************/
  1705. static u32 sdhci_msm_cqe_irq(struct sdhci_host *host, u32 intmask)
  1706. {
  1707. int cmd_error = 0;
  1708. int data_error = 0;
  1709. if (!sdhci_cqe_irq(host, intmask, &cmd_error, &data_error))
  1710. return intmask;
  1711. cqhci_irq(host->mmc, intmask, cmd_error, data_error);
  1712. return 0;
  1713. }
  1714. static void sdhci_msm_cqe_enable(struct mmc_host *mmc)
  1715. {
  1716. struct sdhci_host *host = mmc_priv(mmc);
  1717. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1718. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1719. sdhci_cqe_enable(mmc);
  1720. sdhci_msm_ice_enable(msm_host);
  1721. }
  1722. static void sdhci_msm_cqe_disable(struct mmc_host *mmc, bool recovery)
  1723. {
  1724. struct sdhci_host *host = mmc_priv(mmc);
  1725. unsigned long flags;
  1726. u32 ctrl;
  1727. /*
  1728. * When CQE is halted, the legacy SDHCI path operates only
  1729. * on 16-byte descriptors in 64bit mode.
  1730. */
  1731. if (host->flags & SDHCI_USE_64_BIT_DMA)
  1732. host->desc_sz = 16;
  1733. spin_lock_irqsave(&host->lock, flags);
  1734. /*
  1735. * During CQE command transfers, command complete bit gets latched.
  1736. * So s/w should clear command complete interrupt status when CQE is
  1737. * either halted or disabled. Otherwise unexpected SDCHI legacy
  1738. * interrupt gets triggered when CQE is halted/disabled.
  1739. */
  1740. ctrl = sdhci_readl(host, SDHCI_INT_ENABLE);
  1741. ctrl |= SDHCI_INT_RESPONSE;
  1742. sdhci_writel(host, ctrl, SDHCI_INT_ENABLE);
  1743. sdhci_writel(host, SDHCI_INT_RESPONSE, SDHCI_INT_STATUS);
  1744. spin_unlock_irqrestore(&host->lock, flags);
  1745. sdhci_cqe_disable(mmc, recovery);
  1746. }
  1747. static void sdhci_msm_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
  1748. {
  1749. u32 count, start = 15;
  1750. __sdhci_set_timeout(host, cmd);
  1751. count = sdhci_readb(host, SDHCI_TIMEOUT_CONTROL);
  1752. /*
  1753. * Update software timeout value if its value is less than hardware data
  1754. * timeout value. Qcom SoC hardware data timeout value was calculated
  1755. * using 4 * MCLK * 2^(count + 13). where MCLK = 1 / host->clock.
  1756. */
  1757. if (cmd && cmd->data && host->clock > 400000 &&
  1758. host->clock <= 50000000 &&
  1759. ((1 << (count + start)) > (10 * host->clock)))
  1760. host->data_timeout = 22LL * NSEC_PER_SEC;
  1761. }
  1762. static const struct cqhci_host_ops sdhci_msm_cqhci_ops = {
  1763. .enable = sdhci_msm_cqe_enable,
  1764. .disable = sdhci_msm_cqe_disable,
  1765. #ifdef CONFIG_MMC_CRYPTO
  1766. .program_key = sdhci_msm_program_key,
  1767. #endif
  1768. };
  1769. static int sdhci_msm_cqe_add_host(struct sdhci_host *host,
  1770. struct platform_device *pdev)
  1771. {
  1772. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1773. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1774. struct cqhci_host *cq_host;
  1775. bool dma64;
  1776. u32 cqcfg;
  1777. int ret;
  1778. /*
  1779. * When CQE is halted, SDHC operates only on 16byte ADMA descriptors.
  1780. * So ensure ADMA table is allocated for 16byte descriptors.
  1781. */
  1782. if (host->caps & SDHCI_CAN_64BIT)
  1783. host->alloc_desc_sz = 16;
  1784. ret = sdhci_setup_host(host);
  1785. if (ret)
  1786. return ret;
  1787. cq_host = cqhci_pltfm_init(pdev);
  1788. if (IS_ERR(cq_host)) {
  1789. ret = PTR_ERR(cq_host);
  1790. dev_err(&pdev->dev, "cqhci-pltfm init: failed: %d\n", ret);
  1791. goto cleanup;
  1792. }
  1793. msm_host->mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD;
  1794. cq_host->ops = &sdhci_msm_cqhci_ops;
  1795. dma64 = host->flags & SDHCI_USE_64_BIT_DMA;
  1796. ret = sdhci_msm_ice_init(msm_host, cq_host);
  1797. if (ret)
  1798. goto cleanup;
  1799. ret = cqhci_init(cq_host, host->mmc, dma64);
  1800. if (ret) {
  1801. dev_err(&pdev->dev, "%s: CQE init: failed (%d)\n",
  1802. mmc_hostname(host->mmc), ret);
  1803. goto cleanup;
  1804. }
  1805. /* Disable cqe reset due to cqe enable signal */
  1806. cqcfg = cqhci_readl(cq_host, CQHCI_VENDOR_CFG1);
  1807. cqcfg |= CQHCI_VENDOR_DIS_RST_ON_CQ_EN;
  1808. cqhci_writel(cq_host, cqcfg, CQHCI_VENDOR_CFG1);
  1809. /*
  1810. * SDHC expects 12byte ADMA descriptors till CQE is enabled.
  1811. * So limit desc_sz to 12 so that the data commands that are sent
  1812. * during card initialization (before CQE gets enabled) would
  1813. * get executed without any issues.
  1814. */
  1815. if (host->flags & SDHCI_USE_64_BIT_DMA)
  1816. host->desc_sz = 12;
  1817. ret = __sdhci_add_host(host);
  1818. if (ret)
  1819. goto cleanup;
  1820. dev_info(&pdev->dev, "%s: CQE init: success\n",
  1821. mmc_hostname(host->mmc));
  1822. return ret;
  1823. cleanup:
  1824. sdhci_cleanup_host(host);
  1825. return ret;
  1826. }
  1827. /*
  1828. * Platform specific register write functions. This is so that, if any
  1829. * register write needs to be followed up by platform specific actions,
  1830. * they can be added here. These functions can go to sleep when writes
  1831. * to certain registers are done.
  1832. * These functions are relying on sdhci_set_ios not using spinlock.
  1833. */
  1834. static int __sdhci_msm_check_write(struct sdhci_host *host, u16 val, int reg)
  1835. {
  1836. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1837. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1838. u32 req_type = 0;
  1839. switch (reg) {
  1840. case SDHCI_HOST_CONTROL2:
  1841. req_type = (val & SDHCI_CTRL_VDD_180) ? REQ_IO_LOW :
  1842. REQ_IO_HIGH;
  1843. break;
  1844. case SDHCI_SOFTWARE_RESET:
  1845. if (host->pwr && (val & SDHCI_RESET_ALL))
  1846. req_type = REQ_BUS_OFF;
  1847. break;
  1848. case SDHCI_POWER_CONTROL:
  1849. req_type = !val ? REQ_BUS_OFF : REQ_BUS_ON;
  1850. break;
  1851. case SDHCI_TRANSFER_MODE:
  1852. msm_host->transfer_mode = val;
  1853. break;
  1854. case SDHCI_COMMAND:
  1855. if (!msm_host->use_cdr)
  1856. break;
  1857. if ((msm_host->transfer_mode & SDHCI_TRNS_READ) &&
  1858. !mmc_op_tuning(SDHCI_GET_CMD(val)))
  1859. sdhci_msm_set_cdr(host, true);
  1860. else
  1861. sdhci_msm_set_cdr(host, false);
  1862. break;
  1863. }
  1864. if (req_type) {
  1865. msm_host->pwr_irq_flag = 0;
  1866. /*
  1867. * Since this register write may trigger a power irq, ensure
  1868. * all previous register writes are complete by this point.
  1869. */
  1870. mb();
  1871. }
  1872. return req_type;
  1873. }
  1874. /* This function may sleep*/
  1875. static void sdhci_msm_writew(struct sdhci_host *host, u16 val, int reg)
  1876. {
  1877. u32 req_type = 0;
  1878. req_type = __sdhci_msm_check_write(host, val, reg);
  1879. writew_relaxed(val, host->ioaddr + reg);
  1880. if (req_type)
  1881. sdhci_msm_check_power_status(host, req_type);
  1882. }
  1883. /* This function may sleep*/
  1884. static void sdhci_msm_writeb(struct sdhci_host *host, u8 val, int reg)
  1885. {
  1886. u32 req_type = 0;
  1887. req_type = __sdhci_msm_check_write(host, val, reg);
  1888. writeb_relaxed(val, host->ioaddr + reg);
  1889. if (req_type)
  1890. sdhci_msm_check_power_status(host, req_type);
  1891. }
  1892. static void sdhci_msm_set_regulator_caps(struct sdhci_msm_host *msm_host)
  1893. {
  1894. struct mmc_host *mmc = msm_host->mmc;
  1895. struct regulator *supply = mmc->supply.vqmmc;
  1896. u32 caps = 0, config;
  1897. struct sdhci_host *host = mmc_priv(mmc);
  1898. const struct sdhci_msm_offset *msm_offset = msm_host->offset;
  1899. if (!IS_ERR(mmc->supply.vqmmc)) {
  1900. if (regulator_is_supported_voltage(supply, 1700000, 1950000))
  1901. caps |= CORE_1_8V_SUPPORT;
  1902. if (regulator_is_supported_voltage(supply, 2700000, 3600000))
  1903. caps |= CORE_3_0V_SUPPORT;
  1904. if (!caps)
  1905. pr_warn("%s: 1.8/3V not supported for vqmmc\n",
  1906. mmc_hostname(mmc));
  1907. }
  1908. if (caps) {
  1909. /*
  1910. * Set the PAD_PWR_SWITCH_EN bit so that the PAD_PWR_SWITCH
  1911. * bit can be used as required later on.
  1912. */
  1913. u32 io_level = msm_host->curr_io_level;
  1914. config = readl_relaxed(host->ioaddr +
  1915. msm_offset->core_vendor_spec);
  1916. config |= CORE_IO_PAD_PWR_SWITCH_EN;
  1917. if ((io_level & REQ_IO_HIGH) && (caps & CORE_3_0V_SUPPORT))
  1918. config &= ~CORE_IO_PAD_PWR_SWITCH;
  1919. else if ((io_level & REQ_IO_LOW) || (caps & CORE_1_8V_SUPPORT))
  1920. config |= CORE_IO_PAD_PWR_SWITCH;
  1921. writel_relaxed(config,
  1922. host->ioaddr + msm_offset->core_vendor_spec);
  1923. }
  1924. msm_host->caps_0 |= caps;
  1925. pr_debug("%s: supported caps: 0x%08x\n", mmc_hostname(mmc), caps);
  1926. }
  1927. static int sdhci_msm_register_vreg(struct sdhci_msm_host *msm_host)
  1928. {
  1929. int ret;
  1930. ret = mmc_regulator_get_supply(msm_host->mmc);
  1931. if (ret)
  1932. return ret;
  1933. sdhci_msm_set_regulator_caps(msm_host);
  1934. return 0;
  1935. }
  1936. static int sdhci_msm_start_signal_voltage_switch(struct mmc_host *mmc,
  1937. struct mmc_ios *ios)
  1938. {
  1939. struct sdhci_host *host = mmc_priv(mmc);
  1940. u16 ctrl, status;
  1941. /*
  1942. * Signal Voltage Switching is only applicable for Host Controllers
  1943. * v3.00 and above.
  1944. */
  1945. if (host->version < SDHCI_SPEC_300)
  1946. return 0;
  1947. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1948. switch (ios->signal_voltage) {
  1949. case MMC_SIGNAL_VOLTAGE_330:
  1950. if (!(host->flags & SDHCI_SIGNALING_330))
  1951. return -EINVAL;
  1952. /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
  1953. ctrl &= ~SDHCI_CTRL_VDD_180;
  1954. break;
  1955. case MMC_SIGNAL_VOLTAGE_180:
  1956. if (!(host->flags & SDHCI_SIGNALING_180))
  1957. return -EINVAL;
  1958. /* Enable 1.8V Signal Enable in the Host Control2 register */
  1959. ctrl |= SDHCI_CTRL_VDD_180;
  1960. break;
  1961. default:
  1962. return -EINVAL;
  1963. }
  1964. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  1965. /* Wait for 5ms */
  1966. usleep_range(5000, 5500);
  1967. /* regulator output should be stable within 5 ms */
  1968. status = ctrl & SDHCI_CTRL_VDD_180;
  1969. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  1970. if ((ctrl & SDHCI_CTRL_VDD_180) == status)
  1971. return 0;
  1972. dev_warn(mmc_dev(mmc), "%s: Regulator output did not became stable\n",
  1973. mmc_hostname(mmc));
  1974. return -EAGAIN;
  1975. }
  1976. #define DRIVER_NAME "sdhci_msm"
  1977. #define SDHCI_MSM_DUMP(f, x...) \
  1978. pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
  1979. static void sdhci_msm_dump_vendor_regs(struct sdhci_host *host)
  1980. {
  1981. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  1982. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  1983. const struct sdhci_msm_offset *msm_offset = msm_host->offset;
  1984. SDHCI_MSM_DUMP("----------- VENDOR REGISTER DUMP -----------\n");
  1985. SDHCI_MSM_DUMP(
  1986. "DLL sts: 0x%08x | DLL cfg: 0x%08x | DLL cfg2: 0x%08x\n",
  1987. readl_relaxed(host->ioaddr + msm_offset->core_dll_status),
  1988. readl_relaxed(host->ioaddr + msm_offset->core_dll_config),
  1989. readl_relaxed(host->ioaddr + msm_offset->core_dll_config_2));
  1990. SDHCI_MSM_DUMP(
  1991. "DLL cfg3: 0x%08x | DLL usr ctl: 0x%08x | DDR cfg: 0x%08x\n",
  1992. readl_relaxed(host->ioaddr + msm_offset->core_dll_config_3),
  1993. readl_relaxed(host->ioaddr + msm_offset->core_dll_usr_ctl),
  1994. readl_relaxed(host->ioaddr + msm_offset->core_ddr_config));
  1995. SDHCI_MSM_DUMP(
  1996. "Vndr func: 0x%08x | Vndr func2 : 0x%08x Vndr func3: 0x%08x\n",
  1997. readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec),
  1998. readl_relaxed(host->ioaddr +
  1999. msm_offset->core_vendor_spec_func2),
  2000. readl_relaxed(host->ioaddr + msm_offset->core_vendor_spec3));
  2001. }
  2002. static const struct sdhci_msm_variant_ops mci_var_ops = {
  2003. .msm_readl_relaxed = sdhci_msm_mci_variant_readl_relaxed,
  2004. .msm_writel_relaxed = sdhci_msm_mci_variant_writel_relaxed,
  2005. };
  2006. static const struct sdhci_msm_variant_ops v5_var_ops = {
  2007. .msm_readl_relaxed = sdhci_msm_v5_variant_readl_relaxed,
  2008. .msm_writel_relaxed = sdhci_msm_v5_variant_writel_relaxed,
  2009. };
  2010. static const struct sdhci_msm_variant_info sdhci_msm_mci_var = {
  2011. .var_ops = &mci_var_ops,
  2012. .offset = &sdhci_msm_mci_offset,
  2013. };
  2014. static const struct sdhci_msm_variant_info sdhci_msm_v5_var = {
  2015. .mci_removed = true,
  2016. .var_ops = &v5_var_ops,
  2017. .offset = &sdhci_msm_v5_offset,
  2018. };
  2019. static const struct sdhci_msm_variant_info sdm845_sdhci_var = {
  2020. .mci_removed = true,
  2021. .restore_dll_config = true,
  2022. .var_ops = &v5_var_ops,
  2023. .offset = &sdhci_msm_v5_offset,
  2024. };
  2025. static const struct of_device_id sdhci_msm_dt_match[] = {
  2026. /*
  2027. * Do not add new variants to the driver which are compatible with
  2028. * generic ones, unless they need customization.
  2029. */
  2030. {.compatible = "qcom,sdhci-msm-v4", .data = &sdhci_msm_mci_var},
  2031. {.compatible = "qcom,sdhci-msm-v5", .data = &sdhci_msm_v5_var},
  2032. {.compatible = "qcom,sdm670-sdhci", .data = &sdm845_sdhci_var},
  2033. {.compatible = "qcom,sdm845-sdhci", .data = &sdm845_sdhci_var},
  2034. {.compatible = "qcom,sc7180-sdhci", .data = &sdm845_sdhci_var},
  2035. {},
  2036. };
  2037. MODULE_DEVICE_TABLE(of, sdhci_msm_dt_match);
  2038. static const struct sdhci_ops sdhci_msm_ops = {
  2039. .reset = sdhci_and_cqhci_reset,
  2040. .set_clock = sdhci_msm_set_clock,
  2041. .get_min_clock = sdhci_msm_get_min_clock,
  2042. .get_max_clock = sdhci_msm_get_max_clock,
  2043. .set_bus_width = sdhci_set_bus_width,
  2044. .set_uhs_signaling = sdhci_msm_set_uhs_signaling,
  2045. .write_w = sdhci_msm_writew,
  2046. .write_b = sdhci_msm_writeb,
  2047. .irq = sdhci_msm_cqe_irq,
  2048. .dump_vendor_regs = sdhci_msm_dump_vendor_regs,
  2049. .set_power = sdhci_set_power_noreg,
  2050. .set_timeout = sdhci_msm_set_timeout,
  2051. };
  2052. static const struct sdhci_pltfm_data sdhci_msm_pdata = {
  2053. .quirks = SDHCI_QUIRK_BROKEN_CARD_DETECTION |
  2054. SDHCI_QUIRK_SINGLE_POWER_WRITE |
  2055. SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN |
  2056. SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12,
  2057. .quirks2 = SDHCI_QUIRK2_PRESET_VALUE_BROKEN,
  2058. .ops = &sdhci_msm_ops,
  2059. };
  2060. static inline void sdhci_msm_get_of_property(struct platform_device *pdev,
  2061. struct sdhci_host *host)
  2062. {
  2063. struct device_node *node = pdev->dev.of_node;
  2064. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2065. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  2066. if (of_property_read_u32(node, "qcom,ddr-config",
  2067. &msm_host->ddr_config))
  2068. msm_host->ddr_config = DDR_CONFIG_POR_VAL;
  2069. of_property_read_u32(node, "qcom,dll-config", &msm_host->dll_config);
  2070. if (of_device_is_compatible(node, "qcom,msm8916-sdhci"))
  2071. host->quirks2 |= SDHCI_QUIRK2_BROKEN_64_BIT_DMA;
  2072. }
  2073. static int sdhci_msm_gcc_reset(struct device *dev, struct sdhci_host *host)
  2074. {
  2075. struct reset_control *reset;
  2076. int ret = 0;
  2077. reset = reset_control_get_optional_exclusive(dev, NULL);
  2078. if (IS_ERR(reset))
  2079. return dev_err_probe(dev, PTR_ERR(reset),
  2080. "unable to acquire core_reset\n");
  2081. if (!reset)
  2082. return ret;
  2083. ret = reset_control_assert(reset);
  2084. if (ret) {
  2085. reset_control_put(reset);
  2086. return dev_err_probe(dev, ret, "core_reset assert failed\n");
  2087. }
  2088. /*
  2089. * The hardware requirement for delay between assert/deassert
  2090. * is at least 3-4 sleep clock (32.7KHz) cycles, which comes to
  2091. * ~125us (4/32768). To be on the safe side add 200us delay.
  2092. */
  2093. usleep_range(200, 210);
  2094. ret = reset_control_deassert(reset);
  2095. if (ret) {
  2096. reset_control_put(reset);
  2097. return dev_err_probe(dev, ret, "core_reset deassert failed\n");
  2098. }
  2099. usleep_range(200, 210);
  2100. reset_control_put(reset);
  2101. return ret;
  2102. }
  2103. static int sdhci_msm_probe(struct platform_device *pdev)
  2104. {
  2105. struct sdhci_host *host;
  2106. struct sdhci_pltfm_host *pltfm_host;
  2107. struct sdhci_msm_host *msm_host;
  2108. struct clk *clk;
  2109. int ret;
  2110. u16 host_version, core_minor;
  2111. u32 core_version, config;
  2112. u8 core_major;
  2113. const struct sdhci_msm_offset *msm_offset;
  2114. const struct sdhci_msm_variant_info *var_info;
  2115. struct device_node *node = pdev->dev.of_node;
  2116. host = sdhci_pltfm_init(pdev, &sdhci_msm_pdata, sizeof(*msm_host));
  2117. if (IS_ERR(host))
  2118. return PTR_ERR(host);
  2119. host->sdma_boundary = 0;
  2120. pltfm_host = sdhci_priv(host);
  2121. msm_host = sdhci_pltfm_priv(pltfm_host);
  2122. msm_host->mmc = host->mmc;
  2123. msm_host->pdev = pdev;
  2124. ret = mmc_of_parse(host->mmc);
  2125. if (ret)
  2126. goto pltfm_free;
  2127. /*
  2128. * Based on the compatible string, load the required msm host info from
  2129. * the data associated with the version info.
  2130. */
  2131. var_info = of_device_get_match_data(&pdev->dev);
  2132. msm_host->mci_removed = var_info->mci_removed;
  2133. msm_host->restore_dll_config = var_info->restore_dll_config;
  2134. msm_host->var_ops = var_info->var_ops;
  2135. msm_host->offset = var_info->offset;
  2136. msm_offset = msm_host->offset;
  2137. sdhci_get_of_property(pdev);
  2138. sdhci_msm_get_of_property(pdev, host);
  2139. msm_host->saved_tuning_phase = INVALID_TUNING_PHASE;
  2140. ret = sdhci_msm_gcc_reset(&pdev->dev, host);
  2141. if (ret)
  2142. goto pltfm_free;
  2143. /* Setup SDCC bus voter clock. */
  2144. msm_host->bus_clk = devm_clk_get(&pdev->dev, "bus");
  2145. if (!IS_ERR(msm_host->bus_clk)) {
  2146. /* Vote for max. clk rate for max. performance */
  2147. ret = clk_set_rate(msm_host->bus_clk, INT_MAX);
  2148. if (ret)
  2149. goto pltfm_free;
  2150. ret = clk_prepare_enable(msm_host->bus_clk);
  2151. if (ret)
  2152. goto pltfm_free;
  2153. }
  2154. /* Setup main peripheral bus clock */
  2155. clk = devm_clk_get(&pdev->dev, "iface");
  2156. if (IS_ERR(clk)) {
  2157. ret = PTR_ERR(clk);
  2158. dev_err(&pdev->dev, "Peripheral clk setup failed (%d)\n", ret);
  2159. goto bus_clk_disable;
  2160. }
  2161. msm_host->bulk_clks[1].clk = clk;
  2162. /* Setup SDC MMC clock */
  2163. clk = devm_clk_get(&pdev->dev, "core");
  2164. if (IS_ERR(clk)) {
  2165. ret = PTR_ERR(clk);
  2166. dev_err(&pdev->dev, "SDC MMC clk setup failed (%d)\n", ret);
  2167. goto bus_clk_disable;
  2168. }
  2169. msm_host->bulk_clks[0].clk = clk;
  2170. /* Check for optional interconnect paths */
  2171. ret = dev_pm_opp_of_find_icc_paths(&pdev->dev, NULL);
  2172. if (ret)
  2173. goto bus_clk_disable;
  2174. ret = devm_pm_opp_set_clkname(&pdev->dev, "core");
  2175. if (ret)
  2176. goto bus_clk_disable;
  2177. /* OPP table is optional */
  2178. ret = devm_pm_opp_of_add_table(&pdev->dev);
  2179. if (ret && ret != -ENODEV) {
  2180. dev_err(&pdev->dev, "Invalid OPP table in Device tree\n");
  2181. goto bus_clk_disable;
  2182. }
  2183. /* Vote for maximum clock rate for maximum performance */
  2184. ret = dev_pm_opp_set_rate(&pdev->dev, INT_MAX);
  2185. if (ret)
  2186. dev_warn(&pdev->dev, "core clock boost failed\n");
  2187. clk = devm_clk_get(&pdev->dev, "cal");
  2188. if (IS_ERR(clk))
  2189. clk = NULL;
  2190. msm_host->bulk_clks[2].clk = clk;
  2191. clk = devm_clk_get(&pdev->dev, "sleep");
  2192. if (IS_ERR(clk))
  2193. clk = NULL;
  2194. msm_host->bulk_clks[3].clk = clk;
  2195. ret = clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks),
  2196. msm_host->bulk_clks);
  2197. if (ret)
  2198. goto bus_clk_disable;
  2199. /*
  2200. * xo clock is needed for FLL feature of cm_dll.
  2201. * In case if xo clock is not mentioned in DT, warn and proceed.
  2202. */
  2203. msm_host->xo_clk = devm_clk_get(&pdev->dev, "xo");
  2204. if (IS_ERR(msm_host->xo_clk)) {
  2205. ret = PTR_ERR(msm_host->xo_clk);
  2206. dev_warn(&pdev->dev, "TCXO clk not present (%d)\n", ret);
  2207. }
  2208. if (!msm_host->mci_removed) {
  2209. msm_host->core_mem = devm_platform_ioremap_resource(pdev, 1);
  2210. if (IS_ERR(msm_host->core_mem)) {
  2211. ret = PTR_ERR(msm_host->core_mem);
  2212. goto clk_disable;
  2213. }
  2214. }
  2215. /* Reset the vendor spec register to power on reset state */
  2216. writel_relaxed(CORE_VENDOR_SPEC_POR_VAL,
  2217. host->ioaddr + msm_offset->core_vendor_spec);
  2218. if (!msm_host->mci_removed) {
  2219. /* Set HC_MODE_EN bit in HC_MODE register */
  2220. msm_host_writel(msm_host, HC_MODE_EN, host,
  2221. msm_offset->core_hc_mode);
  2222. config = msm_host_readl(msm_host, host,
  2223. msm_offset->core_hc_mode);
  2224. config |= FF_CLK_SW_RST_DIS;
  2225. msm_host_writel(msm_host, config, host,
  2226. msm_offset->core_hc_mode);
  2227. }
  2228. host_version = readw_relaxed((host->ioaddr + SDHCI_HOST_VERSION));
  2229. dev_dbg(&pdev->dev, "Host Version: 0x%x Vendor Version 0x%x\n",
  2230. host_version, ((host_version & SDHCI_VENDOR_VER_MASK) >>
  2231. SDHCI_VENDOR_VER_SHIFT));
  2232. core_version = msm_host_readl(msm_host, host,
  2233. msm_offset->core_mci_version);
  2234. core_major = (core_version & CORE_VERSION_MAJOR_MASK) >>
  2235. CORE_VERSION_MAJOR_SHIFT;
  2236. core_minor = core_version & CORE_VERSION_MINOR_MASK;
  2237. dev_dbg(&pdev->dev, "MCI Version: 0x%08x, major: 0x%04x, minor: 0x%02x\n",
  2238. core_version, core_major, core_minor);
  2239. if (core_major == 1 && core_minor >= 0x42)
  2240. msm_host->use_14lpp_dll_reset = true;
  2241. /*
  2242. * SDCC 5 controller with major version 1, minor version 0x34 and later
  2243. * with HS 400 mode support will use CM DLL instead of CDC LP 533 DLL.
  2244. */
  2245. if (core_major == 1 && core_minor < 0x34)
  2246. msm_host->use_cdclp533 = true;
  2247. /*
  2248. * Support for some capabilities is not advertised by newer
  2249. * controller versions and must be explicitly enabled.
  2250. */
  2251. if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
  2252. config = readl_relaxed(host->ioaddr + SDHCI_CAPABILITIES);
  2253. config |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
  2254. writel_relaxed(config, host->ioaddr +
  2255. msm_offset->core_vendor_spec_capabilities0);
  2256. }
  2257. if (core_major == 1 && core_minor >= 0x49)
  2258. msm_host->updated_ddr_cfg = true;
  2259. if (core_major == 1 && core_minor >= 0x71)
  2260. msm_host->uses_tassadar_dll = true;
  2261. ret = sdhci_msm_register_vreg(msm_host);
  2262. if (ret)
  2263. goto clk_disable;
  2264. /*
  2265. * Power on reset state may trigger power irq if previous status of
  2266. * PWRCTL was either BUS_ON or IO_HIGH_V. So before enabling pwr irq
  2267. * interrupt in GIC, any pending power irq interrupt should be
  2268. * acknowledged. Otherwise power irq interrupt handler would be
  2269. * fired prematurely.
  2270. */
  2271. sdhci_msm_handle_pwr_irq(host, 0);
  2272. /*
  2273. * Ensure that above writes are propogated before interrupt enablement
  2274. * in GIC.
  2275. */
  2276. mb();
  2277. /* Setup IRQ for handling power/voltage tasks with PMIC */
  2278. msm_host->pwr_irq = platform_get_irq_byname(pdev, "pwr_irq");
  2279. if (msm_host->pwr_irq < 0) {
  2280. ret = msm_host->pwr_irq;
  2281. goto clk_disable;
  2282. }
  2283. sdhci_msm_init_pwr_irq_wait(msm_host);
  2284. /* Enable pwr irq interrupts */
  2285. msm_host_writel(msm_host, INT_MASK, host,
  2286. msm_offset->core_pwrctl_mask);
  2287. ret = devm_request_threaded_irq(&pdev->dev, msm_host->pwr_irq, NULL,
  2288. sdhci_msm_pwr_irq, IRQF_ONESHOT,
  2289. dev_name(&pdev->dev), host);
  2290. if (ret) {
  2291. dev_err(&pdev->dev, "Request IRQ failed (%d)\n", ret);
  2292. goto clk_disable;
  2293. }
  2294. msm_host->mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_NEED_RSP_BUSY;
  2295. /* Set the timeout value to max possible */
  2296. host->max_timeout_count = 0xF;
  2297. pm_runtime_get_noresume(&pdev->dev);
  2298. pm_runtime_set_active(&pdev->dev);
  2299. pm_runtime_enable(&pdev->dev);
  2300. pm_runtime_set_autosuspend_delay(&pdev->dev,
  2301. MSM_MMC_AUTOSUSPEND_DELAY_MS);
  2302. pm_runtime_use_autosuspend(&pdev->dev);
  2303. host->mmc_host_ops.start_signal_voltage_switch =
  2304. sdhci_msm_start_signal_voltage_switch;
  2305. host->mmc_host_ops.execute_tuning = sdhci_msm_execute_tuning;
  2306. if (of_property_read_bool(node, "supports-cqe"))
  2307. ret = sdhci_msm_cqe_add_host(host, pdev);
  2308. else
  2309. ret = sdhci_add_host(host);
  2310. if (ret)
  2311. goto pm_runtime_disable;
  2312. pm_runtime_mark_last_busy(&pdev->dev);
  2313. pm_runtime_put_autosuspend(&pdev->dev);
  2314. return 0;
  2315. pm_runtime_disable:
  2316. pm_runtime_disable(&pdev->dev);
  2317. pm_runtime_set_suspended(&pdev->dev);
  2318. pm_runtime_put_noidle(&pdev->dev);
  2319. clk_disable:
  2320. clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
  2321. msm_host->bulk_clks);
  2322. bus_clk_disable:
  2323. if (!IS_ERR(msm_host->bus_clk))
  2324. clk_disable_unprepare(msm_host->bus_clk);
  2325. pltfm_free:
  2326. sdhci_pltfm_free(pdev);
  2327. return ret;
  2328. }
  2329. static void sdhci_msm_remove(struct platform_device *pdev)
  2330. {
  2331. struct sdhci_host *host = platform_get_drvdata(pdev);
  2332. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2333. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  2334. int dead = (readl_relaxed(host->ioaddr + SDHCI_INT_STATUS) ==
  2335. 0xffffffff);
  2336. sdhci_remove_host(host, dead);
  2337. pm_runtime_get_sync(&pdev->dev);
  2338. pm_runtime_disable(&pdev->dev);
  2339. pm_runtime_put_noidle(&pdev->dev);
  2340. clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
  2341. msm_host->bulk_clks);
  2342. if (!IS_ERR(msm_host->bus_clk))
  2343. clk_disable_unprepare(msm_host->bus_clk);
  2344. sdhci_pltfm_free(pdev);
  2345. }
  2346. static __maybe_unused int sdhci_msm_runtime_suspend(struct device *dev)
  2347. {
  2348. struct sdhci_host *host = dev_get_drvdata(dev);
  2349. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2350. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  2351. unsigned long flags;
  2352. spin_lock_irqsave(&host->lock, flags);
  2353. host->runtime_suspended = true;
  2354. spin_unlock_irqrestore(&host->lock, flags);
  2355. /* Drop the performance vote */
  2356. dev_pm_opp_set_rate(dev, 0);
  2357. clk_bulk_disable_unprepare(ARRAY_SIZE(msm_host->bulk_clks),
  2358. msm_host->bulk_clks);
  2359. return sdhci_msm_ice_suspend(msm_host);
  2360. }
  2361. static __maybe_unused int sdhci_msm_runtime_resume(struct device *dev)
  2362. {
  2363. struct sdhci_host *host = dev_get_drvdata(dev);
  2364. struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
  2365. struct sdhci_msm_host *msm_host = sdhci_pltfm_priv(pltfm_host);
  2366. unsigned long flags;
  2367. int ret;
  2368. ret = clk_bulk_prepare_enable(ARRAY_SIZE(msm_host->bulk_clks),
  2369. msm_host->bulk_clks);
  2370. if (ret)
  2371. return ret;
  2372. /*
  2373. * Whenever core-clock is gated dynamically, it's needed to
  2374. * restore the SDR DLL settings when the clock is ungated.
  2375. */
  2376. if (msm_host->restore_dll_config && msm_host->clk_rate) {
  2377. ret = sdhci_msm_restore_sdr_dll_config(host);
  2378. if (ret)
  2379. return ret;
  2380. }
  2381. dev_pm_opp_set_rate(dev, msm_host->clk_rate);
  2382. ret = sdhci_msm_ice_resume(msm_host);
  2383. if (ret)
  2384. return ret;
  2385. spin_lock_irqsave(&host->lock, flags);
  2386. host->runtime_suspended = false;
  2387. spin_unlock_irqrestore(&host->lock, flags);
  2388. return ret;
  2389. }
  2390. static const struct dev_pm_ops sdhci_msm_pm_ops = {
  2391. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  2392. pm_runtime_force_resume)
  2393. SET_RUNTIME_PM_OPS(sdhci_msm_runtime_suspend,
  2394. sdhci_msm_runtime_resume,
  2395. NULL)
  2396. };
  2397. static struct platform_driver sdhci_msm_driver = {
  2398. .probe = sdhci_msm_probe,
  2399. .remove_new = sdhci_msm_remove,
  2400. .driver = {
  2401. .name = "sdhci_msm",
  2402. .of_match_table = sdhci_msm_dt_match,
  2403. .pm = &sdhci_msm_pm_ops,
  2404. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  2405. },
  2406. };
  2407. module_platform_driver(sdhci_msm_driver);
  2408. MODULE_DESCRIPTION("Qualcomm Secure Digital Host Controller Interface driver");
  2409. MODULE_LICENSE("GPL v2");