ufs-qcom.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2013-2016, Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/acpi.h>
  6. #include <linux/clk.h>
  7. #include <linux/cleanup.h>
  8. #include <linux/delay.h>
  9. #include <linux/devfreq.h>
  10. #include <linux/gpio/consumer.h>
  11. #include <linux/interconnect.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_domain.h>
  17. #include <linux/reset-controller.h>
  18. #include <linux/time.h>
  19. #include <linux/unaligned.h>
  20. #include <linux/units.h>
  21. #include <soc/qcom/ice.h>
  22. #include <ufs/ufshcd.h>
  23. #include <ufs/ufshci.h>
  24. #include <ufs/ufs_quirks.h>
  25. #include <ufs/unipro.h>
  26. #include "ufshcd-pltfrm.h"
  27. #include "ufs-qcom.h"
  28. #define MCQ_QCFGPTR_MASK GENMASK(7, 0)
  29. #define MCQ_QCFGPTR_UNIT 0x200
  30. #define MCQ_SQATTR_OFFSET(c) \
  31. ((((c) >> 16) & MCQ_QCFGPTR_MASK) * MCQ_QCFGPTR_UNIT)
  32. #define MCQ_QCFG_SIZE 0x40
  33. /* De-emphasis for gear-5 */
  34. #define DEEMPHASIS_3_5_dB 0x04
  35. #define NO_DEEMPHASIS 0x0
  36. #define UFS_ICE_SYNC_RST_SEL BIT(3)
  37. #define UFS_ICE_SYNC_RST_SW BIT(4)
  38. enum {
  39. TSTBUS_UAWM,
  40. TSTBUS_UARM,
  41. TSTBUS_TXUC,
  42. TSTBUS_RXUC,
  43. TSTBUS_DFC,
  44. TSTBUS_TRLUT,
  45. TSTBUS_TMRLUT,
  46. TSTBUS_OCSC,
  47. TSTBUS_UTP_HCI,
  48. TSTBUS_COMBINED,
  49. TSTBUS_WRAPPER,
  50. TSTBUS_UNIPRO,
  51. TSTBUS_MAX,
  52. };
  53. #define QCOM_UFS_MAX_GEAR 5
  54. #define QCOM_UFS_MAX_LANE 2
  55. enum {
  56. MODE_MIN,
  57. MODE_PWM,
  58. MODE_HS_RA,
  59. MODE_HS_RB,
  60. MODE_MAX,
  61. };
  62. static const struct __ufs_qcom_bw_table {
  63. u32 mem_bw;
  64. u32 cfg_bw;
  65. } ufs_qcom_bw_table[MODE_MAX + 1][QCOM_UFS_MAX_GEAR + 1][QCOM_UFS_MAX_LANE + 1] = {
  66. [MODE_MIN][0][0] = { 0, 0 }, /* Bandwidth values in KB/s */
  67. [MODE_PWM][UFS_PWM_G1][UFS_LANE_1] = { 922, 1000 },
  68. [MODE_PWM][UFS_PWM_G2][UFS_LANE_1] = { 1844, 1000 },
  69. [MODE_PWM][UFS_PWM_G3][UFS_LANE_1] = { 3688, 1000 },
  70. [MODE_PWM][UFS_PWM_G4][UFS_LANE_1] = { 7376, 1000 },
  71. [MODE_PWM][UFS_PWM_G5][UFS_LANE_1] = { 14752, 1000 },
  72. [MODE_PWM][UFS_PWM_G1][UFS_LANE_2] = { 1844, 1000 },
  73. [MODE_PWM][UFS_PWM_G2][UFS_LANE_2] = { 3688, 1000 },
  74. [MODE_PWM][UFS_PWM_G3][UFS_LANE_2] = { 7376, 1000 },
  75. [MODE_PWM][UFS_PWM_G4][UFS_LANE_2] = { 14752, 1000 },
  76. [MODE_PWM][UFS_PWM_G5][UFS_LANE_2] = { 29504, 1000 },
  77. [MODE_HS_RA][UFS_HS_G1][UFS_LANE_1] = { 127796, 1000 },
  78. [MODE_HS_RA][UFS_HS_G2][UFS_LANE_1] = { 255591, 1000 },
  79. [MODE_HS_RA][UFS_HS_G3][UFS_LANE_1] = { 1492582, 102400 },
  80. [MODE_HS_RA][UFS_HS_G4][UFS_LANE_1] = { 2915200, 204800 },
  81. [MODE_HS_RA][UFS_HS_G5][UFS_LANE_1] = { 5836800, 409600 },
  82. [MODE_HS_RA][UFS_HS_G1][UFS_LANE_2] = { 255591, 1000 },
  83. [MODE_HS_RA][UFS_HS_G2][UFS_LANE_2] = { 511181, 1000 },
  84. [MODE_HS_RA][UFS_HS_G3][UFS_LANE_2] = { 1492582, 204800 },
  85. [MODE_HS_RA][UFS_HS_G4][UFS_LANE_2] = { 2915200, 409600 },
  86. [MODE_HS_RA][UFS_HS_G5][UFS_LANE_2] = { 5836800, 819200 },
  87. [MODE_HS_RB][UFS_HS_G1][UFS_LANE_1] = { 149422, 1000 },
  88. [MODE_HS_RB][UFS_HS_G2][UFS_LANE_1] = { 298189, 1000 },
  89. [MODE_HS_RB][UFS_HS_G3][UFS_LANE_1] = { 1492582, 102400 },
  90. [MODE_HS_RB][UFS_HS_G4][UFS_LANE_1] = { 2915200, 204800 },
  91. [MODE_HS_RB][UFS_HS_G5][UFS_LANE_1] = { 5836800, 409600 },
  92. [MODE_HS_RB][UFS_HS_G1][UFS_LANE_2] = { 298189, 1000 },
  93. [MODE_HS_RB][UFS_HS_G2][UFS_LANE_2] = { 596378, 1000 },
  94. [MODE_HS_RB][UFS_HS_G3][UFS_LANE_2] = { 1492582, 204800 },
  95. [MODE_HS_RB][UFS_HS_G4][UFS_LANE_2] = { 2915200, 409600 },
  96. [MODE_HS_RB][UFS_HS_G5][UFS_LANE_2] = { 5836800, 819200 },
  97. [MODE_MAX][0][0] = { 7643136, 819200 },
  98. };
  99. static const struct {
  100. int nminor;
  101. char *prefix;
  102. } testbus_info[TSTBUS_MAX] = {
  103. [TSTBUS_UAWM] = {32, "TSTBUS_UAWM"},
  104. [TSTBUS_UARM] = {32, "TSTBUS_UARM"},
  105. [TSTBUS_TXUC] = {32, "TSTBUS_TXUC"},
  106. [TSTBUS_RXUC] = {32, "TSTBUS_RXUC"},
  107. [TSTBUS_DFC] = {32, "TSTBUS_DFC"},
  108. [TSTBUS_TRLUT] = {32, "TSTBUS_TRLUT"},
  109. [TSTBUS_TMRLUT] = {32, "TSTBUS_TMRLUT"},
  110. [TSTBUS_OCSC] = {32, "TSTBUS_OCSC"},
  111. [TSTBUS_UTP_HCI] = {32, "TSTBUS_UTP_HCI"},
  112. [TSTBUS_COMBINED] = {32, "TSTBUS_COMBINED"},
  113. [TSTBUS_WRAPPER] = {32, "TSTBUS_WRAPPER"},
  114. [TSTBUS_UNIPRO] = {256, "TSTBUS_UNIPRO"},
  115. };
  116. static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host);
  117. static unsigned long ufs_qcom_opp_freq_to_clk_freq(struct ufs_hba *hba,
  118. unsigned long freq, char *name);
  119. static int ufs_qcom_set_core_clk_ctrl(struct ufs_hba *hba, bool is_scale_up, unsigned long freq);
  120. static struct ufs_qcom_host *rcdev_to_ufs_host(struct reset_controller_dev *rcd)
  121. {
  122. return container_of(rcd, struct ufs_qcom_host, rcdev);
  123. }
  124. #ifdef CONFIG_SCSI_UFS_CRYPTO
  125. /**
  126. * ufs_qcom_config_ice_allocator() - ICE core allocator configuration
  127. *
  128. * @host: pointer to qcom specific variant structure.
  129. */
  130. static void ufs_qcom_config_ice_allocator(struct ufs_qcom_host *host)
  131. {
  132. struct ufs_hba *hba = host->hba;
  133. static const uint8_t val[4] = { NUM_RX_R1W0, NUM_TX_R0W1, NUM_RX_R1W1, NUM_TX_R1W1 };
  134. u32 config;
  135. if (!(host->caps & UFS_QCOM_CAP_ICE_CONFIG) ||
  136. !(host->hba->caps & UFSHCD_CAP_CRYPTO))
  137. return;
  138. config = get_unaligned_le32(val);
  139. ufshcd_writel(hba, ICE_ALLOCATOR_TYPE, REG_UFS_MEM_ICE_CONFIG);
  140. ufshcd_writel(hba, config, REG_UFS_MEM_ICE_NUM_CORE);
  141. }
  142. static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host)
  143. {
  144. if (host->hba->caps & UFSHCD_CAP_CRYPTO)
  145. qcom_ice_enable(host->ice);
  146. }
  147. static const struct blk_crypto_ll_ops ufs_qcom_crypto_ops; /* forward decl */
  148. static int ufs_qcom_ice_init(struct ufs_qcom_host *host)
  149. {
  150. struct ufs_hba *hba = host->hba;
  151. struct blk_crypto_profile *profile = &hba->crypto_profile;
  152. struct device *dev = hba->dev;
  153. struct qcom_ice *ice;
  154. union ufs_crypto_capabilities caps;
  155. union ufs_crypto_cap_entry cap;
  156. int err;
  157. int i;
  158. ice = devm_of_qcom_ice_get(dev);
  159. if (ice == ERR_PTR(-EOPNOTSUPP)) {
  160. dev_warn(dev, "Disabling inline encryption support\n");
  161. ice = NULL;
  162. }
  163. if (IS_ERR_OR_NULL(ice))
  164. return PTR_ERR_OR_ZERO(ice);
  165. host->ice = ice;
  166. /* Initialize the blk_crypto_profile */
  167. caps.reg_val = cpu_to_le32(ufshcd_readl(hba, REG_UFS_CCAP));
  168. /* The number of keyslots supported is (CFGC+1) */
  169. err = devm_blk_crypto_profile_init(dev, profile, caps.config_count + 1);
  170. if (err)
  171. return err;
  172. profile->ll_ops = ufs_qcom_crypto_ops;
  173. profile->max_dun_bytes_supported = 8;
  174. profile->key_types_supported = qcom_ice_get_supported_key_type(ice);
  175. profile->dev = dev;
  176. /*
  177. * Currently this driver only supports AES-256-XTS. All known versions
  178. * of ICE support it, but to be safe make sure it is really declared in
  179. * the crypto capability registers. The crypto capability registers
  180. * also give the supported data unit size(s).
  181. */
  182. for (i = 0; i < caps.num_crypto_cap; i++) {
  183. cap.reg_val = cpu_to_le32(ufshcd_readl(hba,
  184. REG_UFS_CRYPTOCAP +
  185. i * sizeof(__le32)));
  186. if (cap.algorithm_id == UFS_CRYPTO_ALG_AES_XTS &&
  187. cap.key_size == UFS_CRYPTO_KEY_SIZE_256)
  188. profile->modes_supported[BLK_ENCRYPTION_MODE_AES_256_XTS] |=
  189. cap.sdus_mask * 512;
  190. }
  191. hba->caps |= UFSHCD_CAP_CRYPTO;
  192. hba->quirks |= UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE;
  193. return 0;
  194. }
  195. static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host)
  196. {
  197. if (host->hba->caps & UFSHCD_CAP_CRYPTO)
  198. return qcom_ice_resume(host->ice);
  199. return 0;
  200. }
  201. static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host)
  202. {
  203. if (host->hba->caps & UFSHCD_CAP_CRYPTO)
  204. return qcom_ice_suspend(host->ice);
  205. return 0;
  206. }
  207. static int ufs_qcom_ice_keyslot_program(struct blk_crypto_profile *profile,
  208. const struct blk_crypto_key *key,
  209. unsigned int slot)
  210. {
  211. struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
  212. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  213. int err;
  214. ufshcd_hold(hba);
  215. err = qcom_ice_program_key(host->ice, slot, key);
  216. ufshcd_release(hba);
  217. return err;
  218. }
  219. static int ufs_qcom_ice_keyslot_evict(struct blk_crypto_profile *profile,
  220. const struct blk_crypto_key *key,
  221. unsigned int slot)
  222. {
  223. struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
  224. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  225. int err;
  226. ufshcd_hold(hba);
  227. err = qcom_ice_evict_key(host->ice, slot);
  228. ufshcd_release(hba);
  229. return err;
  230. }
  231. static int ufs_qcom_ice_derive_sw_secret(struct blk_crypto_profile *profile,
  232. const u8 *eph_key, size_t eph_key_size,
  233. u8 sw_secret[BLK_CRYPTO_SW_SECRET_SIZE])
  234. {
  235. struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
  236. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  237. return qcom_ice_derive_sw_secret(host->ice, eph_key, eph_key_size,
  238. sw_secret);
  239. }
  240. static int ufs_qcom_ice_import_key(struct blk_crypto_profile *profile,
  241. const u8 *raw_key, size_t raw_key_size,
  242. u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
  243. {
  244. struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
  245. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  246. return qcom_ice_import_key(host->ice, raw_key, raw_key_size, lt_key);
  247. }
  248. static int ufs_qcom_ice_generate_key(struct blk_crypto_profile *profile,
  249. u8 lt_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
  250. {
  251. struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
  252. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  253. return qcom_ice_generate_key(host->ice, lt_key);
  254. }
  255. static int ufs_qcom_ice_prepare_key(struct blk_crypto_profile *profile,
  256. const u8 *lt_key, size_t lt_key_size,
  257. u8 eph_key[BLK_CRYPTO_MAX_HW_WRAPPED_KEY_SIZE])
  258. {
  259. struct ufs_hba *hba = ufs_hba_from_crypto_profile(profile);
  260. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  261. return qcom_ice_prepare_key(host->ice, lt_key, lt_key_size, eph_key);
  262. }
  263. static const struct blk_crypto_ll_ops ufs_qcom_crypto_ops = {
  264. .keyslot_program = ufs_qcom_ice_keyslot_program,
  265. .keyslot_evict = ufs_qcom_ice_keyslot_evict,
  266. .derive_sw_secret = ufs_qcom_ice_derive_sw_secret,
  267. .import_key = ufs_qcom_ice_import_key,
  268. .generate_key = ufs_qcom_ice_generate_key,
  269. .prepare_key = ufs_qcom_ice_prepare_key,
  270. };
  271. #else
  272. static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host)
  273. {
  274. }
  275. static int ufs_qcom_ice_init(struct ufs_qcom_host *host)
  276. {
  277. return 0;
  278. }
  279. static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host)
  280. {
  281. return 0;
  282. }
  283. static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host)
  284. {
  285. return 0;
  286. }
  287. static void ufs_qcom_config_ice_allocator(struct ufs_qcom_host *host)
  288. {
  289. }
  290. #endif
  291. static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host)
  292. {
  293. if (!host->is_lane_clks_enabled)
  294. return;
  295. clk_bulk_disable_unprepare(host->num_clks, host->clks);
  296. host->is_lane_clks_enabled = false;
  297. }
  298. static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host)
  299. {
  300. int err;
  301. err = clk_bulk_prepare_enable(host->num_clks, host->clks);
  302. if (err)
  303. return err;
  304. host->is_lane_clks_enabled = true;
  305. return 0;
  306. }
  307. static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host)
  308. {
  309. int err;
  310. struct device *dev = host->hba->dev;
  311. if (has_acpi_companion(dev))
  312. return 0;
  313. err = devm_clk_bulk_get_all(dev, &host->clks);
  314. if (err <= 0)
  315. return err;
  316. host->num_clks = err;
  317. return 0;
  318. }
  319. static int ufs_qcom_check_hibern8(struct ufs_hba *hba)
  320. {
  321. int err;
  322. u32 tx_fsm_val;
  323. unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);
  324. do {
  325. err = ufshcd_dme_get(hba,
  326. UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
  327. UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
  328. &tx_fsm_val);
  329. if (err || tx_fsm_val == TX_FSM_HIBERN8)
  330. break;
  331. /* sleep for max. 200us */
  332. usleep_range(100, 200);
  333. } while (time_before(jiffies, timeout));
  334. /*
  335. * we might have scheduled out for long during polling so
  336. * check the state again.
  337. */
  338. if (time_after(jiffies, timeout))
  339. err = ufshcd_dme_get(hba,
  340. UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
  341. UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
  342. &tx_fsm_val);
  343. if (err) {
  344. dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
  345. __func__, err);
  346. } else if (tx_fsm_val != TX_FSM_HIBERN8) {
  347. err = tx_fsm_val;
  348. dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n",
  349. __func__, err);
  350. }
  351. return err;
  352. }
  353. static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host)
  354. {
  355. ufshcd_rmwl(host->hba, QUNIPRO_SEL, QUNIPRO_SEL, REG_UFS_CFG1);
  356. if (host->hw_ver.major >= 0x05)
  357. ufshcd_rmwl(host->hba, QUNIPRO_G4_SEL, 0, REG_UFS_CFG0);
  358. }
  359. /*
  360. * ufs_qcom_host_reset - reset host controller and PHY
  361. */
  362. static int ufs_qcom_host_reset(struct ufs_hba *hba)
  363. {
  364. int ret;
  365. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  366. bool reenable_intr;
  367. if (!host->core_reset)
  368. return 0;
  369. reenable_intr = hba->is_irq_enabled;
  370. ufshcd_disable_irq(hba);
  371. ret = reset_control_assert(host->core_reset);
  372. if (ret) {
  373. dev_err(hba->dev, "%s: core_reset assert failed, err = %d\n",
  374. __func__, ret);
  375. return ret;
  376. }
  377. /*
  378. * The hardware requirement for delay between assert/deassert
  379. * is at least 3-4 sleep clock (32.7KHz) cycles, which comes to
  380. * ~125us (4/32768). To be on the safe side add 200us delay.
  381. */
  382. usleep_range(200, 210);
  383. ret = reset_control_deassert(host->core_reset);
  384. if (ret) {
  385. dev_err(hba->dev, "%s: core_reset deassert failed, err = %d\n",
  386. __func__, ret);
  387. return ret;
  388. }
  389. usleep_range(1000, 1100);
  390. if (reenable_intr)
  391. ufshcd_enable_irq(hba);
  392. return 0;
  393. }
  394. static u32 ufs_qcom_get_hs_gear(struct ufs_hba *hba)
  395. {
  396. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  397. if (host->hw_ver.major >= 0x4)
  398. return UFS_QCOM_MAX_GEAR(ufshcd_readl(hba, REG_UFS_PARAM0));
  399. /* Default is HS-G3 */
  400. return UFS_HS_G3;
  401. }
  402. static int ufs_qcom_power_up_sequence(struct ufs_hba *hba)
  403. {
  404. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  405. struct ufs_host_params *host_params = &host->host_params;
  406. struct phy *phy = host->generic_phy;
  407. enum phy_mode mode;
  408. int ret;
  409. /*
  410. * HW ver 5 can only support up to HS-G5 Rate-A due to HW limitations.
  411. * If the HS-G5 PHY gear is used, update host_params->hs_rate to Rate-A,
  412. * so that the subsequent power mode change shall stick to Rate-A.
  413. */
  414. if (host->hw_ver.major == 0x5 && host->phy_gear == UFS_HS_G5)
  415. host_params->hs_rate = PA_HS_MODE_A;
  416. mode = host_params->hs_rate == PA_HS_MODE_B ? PHY_MODE_UFS_HS_B : PHY_MODE_UFS_HS_A;
  417. /* Reset UFS Host Controller and PHY */
  418. ret = ufs_qcom_host_reset(hba);
  419. if (ret)
  420. return ret;
  421. if (phy->power_count)
  422. phy_power_off(phy);
  423. /* phy initialization - calibrate the phy */
  424. ret = phy_init(phy);
  425. if (ret) {
  426. dev_err(hba->dev, "%s: phy init failed, ret = %d\n",
  427. __func__, ret);
  428. return ret;
  429. }
  430. ret = phy_set_mode_ext(phy, mode, host->phy_gear);
  431. if (ret)
  432. goto out_disable_phy;
  433. /* power on phy - start serdes and phy's power and clocks */
  434. ret = phy_power_on(phy);
  435. if (ret) {
  436. dev_err(hba->dev, "%s: phy power on failed, ret = %d\n",
  437. __func__, ret);
  438. goto out_disable_phy;
  439. }
  440. ret = phy_calibrate(phy);
  441. if (ret) {
  442. dev_err(hba->dev, "Failed to calibrate PHY: %d\n", ret);
  443. goto out_disable_phy;
  444. }
  445. ufs_qcom_select_unipro_mode(host);
  446. return 0;
  447. out_disable_phy:
  448. phy_exit(phy);
  449. return ret;
  450. }
  451. /*
  452. * The UTP controller has a number of internal clock gating cells (CGCs).
  453. * Internal hardware sub-modules within the UTP controller control the CGCs.
  454. * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
  455. * in a specific operation, UTP controller CGCs are by default disabled and
  456. * this function enables them (after every UFS link startup) to save some power
  457. * leakage.
  458. */
  459. static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba)
  460. {
  461. int err;
  462. /* Enable UTP internal clock gating */
  463. ufshcd_rmwl(hba, REG_UFS_CFG2_CGC_EN_ALL, REG_UFS_CFG2_CGC_EN_ALL,
  464. REG_UFS_CFG2);
  465. /* Ensure that HW clock gating is enabled before next operations */
  466. ufshcd_readl(hba, REG_UFS_CFG2);
  467. /* Enable Unipro internal clock gating */
  468. err = ufshcd_dme_rmw(hba, DL_VS_CLK_CFG_MASK,
  469. DL_VS_CLK_CFG_MASK, DL_VS_CLK_CFG);
  470. if (err)
  471. goto out;
  472. err = ufshcd_dme_rmw(hba, PA_VS_CLK_CFG_REG_MASK,
  473. PA_VS_CLK_CFG_REG_MASK, PA_VS_CLK_CFG_REG);
  474. if (err)
  475. goto out;
  476. err = ufshcd_dme_rmw(hba, DME_VS_CORE_CLK_CTRL_DME_HW_CGC_EN,
  477. DME_VS_CORE_CLK_CTRL_DME_HW_CGC_EN,
  478. DME_VS_CORE_CLK_CTRL);
  479. out:
  480. if (err)
  481. dev_err(hba->dev, "hw clk gating enabled failed\n");
  482. }
  483. static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba,
  484. enum ufs_notify_change_status status)
  485. {
  486. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  487. int err;
  488. switch (status) {
  489. case PRE_CHANGE:
  490. err = ufs_qcom_power_up_sequence(hba);
  491. if (err)
  492. return err;
  493. /*
  494. * The PHY PLL output is the source of tx/rx lane symbol
  495. * clocks, hence, enable the lane clocks only after PHY
  496. * is initialized.
  497. */
  498. err = ufs_qcom_enable_lane_clks(host);
  499. break;
  500. case POST_CHANGE:
  501. /* check if UFS PHY moved from DISABLED to HIBERN8 */
  502. err = ufs_qcom_check_hibern8(hba);
  503. ufs_qcom_enable_hw_clk_gating(hba);
  504. ufs_qcom_ice_enable(host);
  505. ufs_qcom_config_ice_allocator(host);
  506. break;
  507. default:
  508. dev_err(hba->dev, "%s: invalid status %d\n", __func__, status);
  509. err = -EINVAL;
  510. break;
  511. }
  512. return err;
  513. }
  514. static int ufs_qcom_fw_managed_hce_enable_notify(struct ufs_hba *hba,
  515. enum ufs_notify_change_status status)
  516. {
  517. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  518. switch (status) {
  519. case PRE_CHANGE:
  520. ufs_qcom_select_unipro_mode(host);
  521. break;
  522. case POST_CHANGE:
  523. ufs_qcom_enable_hw_clk_gating(hba);
  524. ufs_qcom_ice_enable(host);
  525. break;
  526. default:
  527. dev_err(hba->dev, "Invalid status %d\n", status);
  528. return -EINVAL;
  529. }
  530. return 0;
  531. }
  532. /**
  533. * ufs_qcom_cfg_timers - Configure ufs qcom cfg timers
  534. *
  535. * @hba: host controller instance
  536. * @is_pre_scale_up: flag to check if pre scale up condition.
  537. * @freq: target opp freq
  538. * Return: zero for success and non-zero in case of a failure.
  539. */
  540. static int ufs_qcom_cfg_timers(struct ufs_hba *hba, bool is_pre_scale_up, unsigned long freq)
  541. {
  542. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  543. struct ufs_clk_info *clki;
  544. unsigned long clk_freq = 0;
  545. u32 core_clk_cycles_per_us;
  546. /*
  547. * UTP controller uses SYS1CLK_1US_REG register for Interrupt
  548. * Aggregation logic.
  549. * It is mandatory to write SYS1CLK_1US_REG register on UFS host
  550. * controller V4.0.0 onwards.
  551. */
  552. if (host->hw_ver.major < 4 && !ufshcd_is_intr_aggr_allowed(hba))
  553. return 0;
  554. if (hba->use_pm_opp && freq != ULONG_MAX) {
  555. clk_freq = ufs_qcom_opp_freq_to_clk_freq(hba, freq, "core_clk");
  556. if (clk_freq)
  557. goto cfg_timers;
  558. }
  559. list_for_each_entry(clki, &hba->clk_list_head, list) {
  560. if (!strcmp(clki->name, "core_clk")) {
  561. if (freq == ULONG_MAX) {
  562. clk_freq = clki->max_freq;
  563. break;
  564. }
  565. if (is_pre_scale_up)
  566. clk_freq = clki->max_freq;
  567. else
  568. clk_freq = clk_get_rate(clki->clk);
  569. break;
  570. }
  571. }
  572. cfg_timers:
  573. /* If frequency is smaller than 1MHz, set to 1MHz */
  574. if (clk_freq < DEFAULT_CLK_RATE_HZ)
  575. clk_freq = DEFAULT_CLK_RATE_HZ;
  576. core_clk_cycles_per_us = clk_freq / USEC_PER_SEC;
  577. if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) {
  578. ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US);
  579. /*
  580. * make sure above write gets applied before we return from
  581. * this function.
  582. */
  583. ufshcd_readl(hba, REG_UFS_SYS1CLK_1US);
  584. }
  585. return 0;
  586. }
  587. static int ufs_qcom_link_startup_notify(struct ufs_hba *hba,
  588. enum ufs_notify_change_status status)
  589. {
  590. int err = 0;
  591. switch (status) {
  592. case PRE_CHANGE:
  593. if (ufs_qcom_cfg_timers(hba, false, ULONG_MAX)) {
  594. dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
  595. __func__);
  596. return -EINVAL;
  597. }
  598. err = ufs_qcom_set_core_clk_ctrl(hba, true, ULONG_MAX);
  599. if (err)
  600. dev_err(hba->dev, "cfg core clk ctrl failed\n");
  601. /*
  602. * Some UFS devices (and may be host) have issues if LCC is
  603. * enabled. So we are setting PA_Local_TX_LCC_Enable to 0
  604. * before link startup which will make sure that both host
  605. * and device TX LCC are disabled once link startup is
  606. * completed.
  607. */
  608. err = ufshcd_disable_host_tx_lcc(hba);
  609. break;
  610. default:
  611. break;
  612. }
  613. return err;
  614. }
  615. static void ufs_qcom_device_reset_ctrl(struct ufs_hba *hba, bool asserted)
  616. {
  617. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  618. /* reset gpio is optional */
  619. if (!host->device_reset)
  620. return;
  621. gpiod_set_value_cansleep(host->device_reset, asserted);
  622. }
  623. static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
  624. enum ufs_notify_change_status status)
  625. {
  626. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  627. if (status == PRE_CHANGE)
  628. return 0;
  629. if (!ufs_qcom_is_link_active(hba))
  630. ufs_qcom_disable_lane_clks(host);
  631. /* reset the connected UFS device during power down */
  632. if (ufs_qcom_is_link_off(hba) && host->device_reset) {
  633. ufs_qcom_device_reset_ctrl(hba, true);
  634. /*
  635. * After sending the SSU command, asserting the rst_n
  636. * line causes the device firmware to wake up and
  637. * execute its reset routine.
  638. *
  639. * During this process, the device may draw current
  640. * beyond the permissible limit for low-power mode (LPM).
  641. * A 10ms delay, based on experimental observations,
  642. * allows the UFS device to complete its hardware reset
  643. * before transitioning the power rail to LPM.
  644. */
  645. usleep_range(10000, 11000);
  646. }
  647. return ufs_qcom_ice_suspend(host);
  648. }
  649. static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  650. {
  651. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  652. int err;
  653. u32 reg_val;
  654. err = ufs_qcom_enable_lane_clks(host);
  655. if (err)
  656. return err;
  657. if ((!ufs_qcom_is_link_active(hba)) &&
  658. host->hw_ver.major == 5 &&
  659. host->hw_ver.minor == 0 &&
  660. host->hw_ver.step == 0) {
  661. ufshcd_writel(hba, UFS_ICE_SYNC_RST_SEL | UFS_ICE_SYNC_RST_SW, UFS_MEM_ICE_CFG);
  662. reg_val = ufshcd_readl(hba, UFS_MEM_ICE_CFG);
  663. reg_val &= ~(UFS_ICE_SYNC_RST_SEL | UFS_ICE_SYNC_RST_SW);
  664. /*
  665. * HW documentation doesn't recommend any delay between the
  666. * reset set and clear. But we are enforcing an arbitrary delay
  667. * to give flops enough time to settle in.
  668. */
  669. usleep_range(50, 100);
  670. ufshcd_writel(hba, reg_val, UFS_MEM_ICE_CFG);
  671. ufshcd_readl(hba, UFS_MEM_ICE_CFG);
  672. }
  673. return ufs_qcom_ice_resume(host);
  674. }
  675. static int ufs_qcom_fw_managed_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
  676. enum ufs_notify_change_status status)
  677. {
  678. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  679. if (status == PRE_CHANGE)
  680. return 0;
  681. pm_runtime_put_sync(hba->dev);
  682. return ufs_qcom_ice_suspend(host);
  683. }
  684. static int ufs_qcom_fw_managed_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  685. {
  686. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  687. int err;
  688. err = pm_runtime_resume_and_get(hba->dev);
  689. if (err) {
  690. dev_err(hba->dev, "PM runtime resume failed: %d\n", err);
  691. return err;
  692. }
  693. return ufs_qcom_ice_resume(host);
  694. }
  695. static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable)
  696. {
  697. if (host->dev_ref_clk_ctrl_mmio &&
  698. (enable ^ host->is_dev_ref_clk_enabled)) {
  699. u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio);
  700. if (enable)
  701. temp |= host->dev_ref_clk_en_mask;
  702. else
  703. temp &= ~host->dev_ref_clk_en_mask;
  704. /*
  705. * If we are here to disable this clock it might be immediately
  706. * after entering into hibern8 in which case we need to make
  707. * sure that device ref_clk is active for specific time after
  708. * hibern8 enter.
  709. */
  710. if (!enable) {
  711. unsigned long gating_wait;
  712. gating_wait = host->hba->dev_info.clk_gating_wait_us;
  713. if (!gating_wait) {
  714. udelay(1);
  715. } else {
  716. /*
  717. * bRefClkGatingWaitTime defines the minimum
  718. * time for which the reference clock is
  719. * required by device during transition from
  720. * HS-MODE to LS-MODE or HIBERN8 state. Give it
  721. * more delay to be on the safe side.
  722. */
  723. gating_wait += 10;
  724. usleep_range(gating_wait, gating_wait + 10);
  725. }
  726. }
  727. writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio);
  728. /*
  729. * Make sure the write to ref_clk reaches the destination and
  730. * not stored in a Write Buffer (WB).
  731. */
  732. readl(host->dev_ref_clk_ctrl_mmio);
  733. /*
  734. * If we call hibern8 exit after this, we need to make sure that
  735. * device ref_clk is stable for at least 1us before the hibern8
  736. * exit command.
  737. */
  738. if (enable)
  739. udelay(1);
  740. host->is_dev_ref_clk_enabled = enable;
  741. }
  742. }
  743. static int ufs_qcom_icc_set_bw(struct ufs_qcom_host *host, u32 mem_bw, u32 cfg_bw)
  744. {
  745. struct device *dev = host->hba->dev;
  746. int ret;
  747. ret = icc_set_bw(host->icc_ddr, 0, mem_bw);
  748. if (ret < 0) {
  749. dev_err(dev, "failed to set bandwidth request: %d\n", ret);
  750. return ret;
  751. }
  752. ret = icc_set_bw(host->icc_cpu, 0, cfg_bw);
  753. if (ret < 0) {
  754. dev_err(dev, "failed to set bandwidth request: %d\n", ret);
  755. return ret;
  756. }
  757. return 0;
  758. }
  759. static struct __ufs_qcom_bw_table ufs_qcom_get_bw_table(struct ufs_qcom_host *host)
  760. {
  761. struct ufs_pa_layer_attr *p = &host->dev_req_params;
  762. int gear = max_t(u32, p->gear_rx, p->gear_tx);
  763. int lane = max_t(u32, p->lane_rx, p->lane_tx);
  764. if (WARN_ONCE(gear > QCOM_UFS_MAX_GEAR,
  765. "ICC scaling for UFS Gear (%d) not supported. Using Gear (%d) bandwidth\n",
  766. gear, QCOM_UFS_MAX_GEAR))
  767. gear = QCOM_UFS_MAX_GEAR;
  768. if (WARN_ONCE(lane > QCOM_UFS_MAX_LANE,
  769. "ICC scaling for UFS Lane (%d) not supported. Using Lane (%d) bandwidth\n",
  770. lane, QCOM_UFS_MAX_LANE))
  771. lane = QCOM_UFS_MAX_LANE;
  772. if (ufshcd_is_hs_mode(p)) {
  773. if (p->hs_rate == PA_HS_MODE_B)
  774. return ufs_qcom_bw_table[MODE_HS_RB][gear][lane];
  775. else
  776. return ufs_qcom_bw_table[MODE_HS_RA][gear][lane];
  777. } else {
  778. return ufs_qcom_bw_table[MODE_PWM][gear][lane];
  779. }
  780. }
  781. static int ufs_qcom_icc_update_bw(struct ufs_qcom_host *host)
  782. {
  783. struct __ufs_qcom_bw_table bw_table;
  784. bw_table = ufs_qcom_get_bw_table(host);
  785. return ufs_qcom_icc_set_bw(host, bw_table.mem_bw, bw_table.cfg_bw);
  786. }
  787. static void ufs_qcom_set_tx_hs_equalizer(struct ufs_hba *hba, u32 gear, u32 tx_lanes)
  788. {
  789. u32 equalizer_val;
  790. int ret, i;
  791. /* Determine the equalizer value based on the gear */
  792. equalizer_val = (gear == 5) ? DEEMPHASIS_3_5_dB : NO_DEEMPHASIS;
  793. for (i = 0; i < tx_lanes; i++) {
  794. ret = ufshcd_dme_set(hba, UIC_ARG_MIB_SEL(TX_HS_EQUALIZER, i),
  795. equalizer_val);
  796. if (ret)
  797. dev_err(hba->dev, "%s: failed equalizer lane %d\n",
  798. __func__, i);
  799. }
  800. }
  801. static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba,
  802. enum ufs_notify_change_status status,
  803. const struct ufs_pa_layer_attr *dev_max_params,
  804. struct ufs_pa_layer_attr *dev_req_params)
  805. {
  806. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  807. struct ufs_host_params *host_params = &host->host_params;
  808. int ret = 0;
  809. if (!dev_req_params) {
  810. pr_err("%s: incoming dev_req_params is NULL\n", __func__);
  811. return -EINVAL;
  812. }
  813. switch (status) {
  814. case PRE_CHANGE:
  815. ret = ufshcd_negotiate_pwr_params(host_params, dev_max_params, dev_req_params);
  816. if (ret) {
  817. dev_err(hba->dev, "%s: failed to determine capabilities\n",
  818. __func__);
  819. return ret;
  820. }
  821. /*
  822. * During UFS driver probe, always update the PHY gear to match the negotiated
  823. * gear, so that, if quirk UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH is enabled,
  824. * the second init can program the optimal PHY settings. This allows one to start
  825. * the first init with either the minimum or the maximum support gear.
  826. */
  827. if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
  828. /*
  829. * Skip REINIT if the negotiated gear matches with the
  830. * initial phy_gear. Otherwise, update the phy_gear to
  831. * program the optimal gear setting during REINIT.
  832. */
  833. if (host->phy_gear == dev_req_params->gear_tx)
  834. hba->quirks &= ~UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH;
  835. else
  836. host->phy_gear = dev_req_params->gear_tx;
  837. }
  838. /* enable the device ref clock before changing to HS mode */
  839. if (!ufshcd_is_hs_mode(&hba->pwr_info) &&
  840. ufshcd_is_hs_mode(dev_req_params))
  841. ufs_qcom_dev_ref_clk_ctrl(host, true);
  842. if (host->hw_ver.major >= 0x4) {
  843. ufshcd_dme_configure_adapt(hba,
  844. dev_req_params->gear_tx,
  845. PA_INITIAL_ADAPT);
  846. }
  847. if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TX_DEEMPHASIS_TUNING)
  848. ufs_qcom_set_tx_hs_equalizer(hba,
  849. dev_req_params->gear_tx, dev_req_params->lane_tx);
  850. break;
  851. case POST_CHANGE:
  852. /* cache the power mode parameters to use internally */
  853. memcpy(&host->dev_req_params,
  854. dev_req_params, sizeof(*dev_req_params));
  855. ufs_qcom_icc_update_bw(host);
  856. /* disable the device ref clock if entered PWM mode */
  857. if (ufshcd_is_hs_mode(&hba->pwr_info) &&
  858. !ufshcd_is_hs_mode(dev_req_params))
  859. ufs_qcom_dev_ref_clk_ctrl(host, false);
  860. break;
  861. default:
  862. ret = -EINVAL;
  863. break;
  864. }
  865. return ret;
  866. }
  867. static int ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba *hba)
  868. {
  869. int err;
  870. u32 pa_vs_config_reg1;
  871. err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
  872. &pa_vs_config_reg1);
  873. if (err)
  874. return err;
  875. /* Allow extension of MSB bits of PA_SaveConfigTime attribute */
  876. return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
  877. (pa_vs_config_reg1 | (1 << 12)));
  878. }
  879. static void ufs_qcom_override_pa_tx_hsg1_sync_len(struct ufs_hba *hba)
  880. {
  881. int err;
  882. err = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TX_HSG1_SYNC_LENGTH),
  883. PA_TX_HSG1_SYNC_LENGTH_VAL);
  884. if (err)
  885. dev_err(hba->dev, "Failed (%d) set PA_TX_HSG1_SYNC_LENGTH\n", err);
  886. }
  887. static int ufs_qcom_apply_dev_quirks(struct ufs_hba *hba)
  888. {
  889. int err = 0;
  890. if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
  891. err = ufs_qcom_quirk_host_pa_saveconfigtime(hba);
  892. if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TX_HSG1_SYNC_LENGTH)
  893. ufs_qcom_override_pa_tx_hsg1_sync_len(hba);
  894. return err;
  895. }
  896. /* UFS device-specific quirks */
  897. static struct ufs_dev_quirk ufs_qcom_dev_fixups[] = {
  898. { .wmanufacturerid = UFS_VENDOR_SKHYNIX,
  899. .model = UFS_ANY_MODEL,
  900. .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM },
  901. { .wmanufacturerid = UFS_VENDOR_WDC,
  902. .model = UFS_ANY_MODEL,
  903. .quirk = UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE },
  904. { .wmanufacturerid = UFS_VENDOR_SAMSUNG,
  905. .model = UFS_ANY_MODEL,
  906. .quirk = UFS_DEVICE_QUIRK_PA_TX_HSG1_SYNC_LENGTH |
  907. UFS_DEVICE_QUIRK_PA_TX_DEEMPHASIS_TUNING },
  908. {}
  909. };
  910. static void ufs_qcom_fixup_dev_quirks(struct ufs_hba *hba)
  911. {
  912. ufshcd_fixup_dev_quirks(hba, ufs_qcom_dev_fixups);
  913. }
  914. static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba)
  915. {
  916. return ufshci_version(2, 0);
  917. }
  918. /**
  919. * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
  920. * @hba: host controller instance
  921. *
  922. * QCOM UFS host controller might have some non standard behaviours (quirks)
  923. * than what is specified by UFSHCI specification. Advertise all such
  924. * quirks to standard UFS host controller driver so standard takes them into
  925. * account.
  926. */
  927. static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
  928. {
  929. const struct ufs_qcom_drvdata *drvdata = of_device_get_match_data(hba->dev);
  930. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  931. if (host->hw_ver.major == 0x2)
  932. hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
  933. if (host->hw_ver.major > 0x3)
  934. hba->quirks |= UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH;
  935. if (drvdata && drvdata->quirks)
  936. hba->quirks |= drvdata->quirks;
  937. }
  938. static void ufs_qcom_set_phy_gear(struct ufs_qcom_host *host)
  939. {
  940. struct ufs_host_params *host_params = &host->host_params;
  941. u32 val, dev_major;
  942. /*
  943. * Default to powering up the PHY to the max gear possible, which is
  944. * backwards compatible with lower gears but not optimal from
  945. * a power usage point of view. After device negotiation, if the
  946. * gear is lower a reinit will be performed to program the PHY
  947. * to the ideal gear for this combo of controller and device.
  948. */
  949. host->phy_gear = host_params->hs_tx_gear;
  950. if (host->hw_ver.major < 0x4) {
  951. /*
  952. * These controllers only have one PHY init sequence,
  953. * let's power up the PHY using that (the minimum supported
  954. * gear, UFS_HS_G2).
  955. */
  956. host->phy_gear = UFS_HS_G2;
  957. } else if (host->hw_ver.major >= 0x5) {
  958. val = ufshcd_readl(host->hba, REG_UFS_DEBUG_SPARE_CFG);
  959. dev_major = FIELD_GET(UFS_DEV_VER_MAJOR_MASK, val);
  960. /*
  961. * Since the UFS device version is populated, let's remove the
  962. * REINIT quirk as the negotiated gear won't change during boot.
  963. * So there is no need to do reinit.
  964. */
  965. if (dev_major != 0x0)
  966. host->hba->quirks &= ~UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH;
  967. /*
  968. * For UFS 3.1 device and older, power up the PHY using HS-G4
  969. * PHY gear to save power.
  970. */
  971. if (dev_major > 0x0 && dev_major < 0x4)
  972. host->phy_gear = UFS_HS_G4;
  973. }
  974. }
  975. static void ufs_qcom_parse_gear_limits(struct ufs_hba *hba)
  976. {
  977. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  978. struct ufs_host_params *host_params = &host->host_params;
  979. u32 hs_gear_old = host_params->hs_tx_gear;
  980. ufshcd_parse_gear_limits(hba, host_params);
  981. if (host_params->hs_tx_gear != hs_gear_old) {
  982. host->phy_gear = host_params->hs_tx_gear;
  983. }
  984. }
  985. static void ufs_qcom_set_host_params(struct ufs_hba *hba)
  986. {
  987. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  988. struct ufs_host_params *host_params = &host->host_params;
  989. ufshcd_init_host_params(host_params);
  990. /* This driver only supports symmetic gear setting i.e., hs_tx_gear == hs_rx_gear */
  991. host_params->hs_tx_gear = host_params->hs_rx_gear = ufs_qcom_get_hs_gear(hba);
  992. }
  993. static void ufs_qcom_set_host_caps(struct ufs_hba *hba)
  994. {
  995. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  996. if (host->hw_ver.major >= 0x5)
  997. host->caps |= UFS_QCOM_CAP_ICE_CONFIG;
  998. }
  999. static void ufs_qcom_set_caps(struct ufs_hba *hba)
  1000. {
  1001. hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
  1002. hba->caps |= UFSHCD_CAP_CLK_SCALING | UFSHCD_CAP_WB_WITH_CLK_SCALING;
  1003. hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
  1004. hba->caps |= UFSHCD_CAP_WB_EN;
  1005. hba->caps |= UFSHCD_CAP_AGGR_POWER_COLLAPSE;
  1006. hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
  1007. ufs_qcom_set_host_caps(hba);
  1008. }
  1009. /**
  1010. * ufs_qcom_setup_clocks - enables/disable clocks
  1011. * @hba: host controller instance
  1012. * @on: If true, enable clocks else disable them.
  1013. * @status: PRE_CHANGE or POST_CHANGE notify
  1014. *
  1015. * There are certain clocks which comes from the PHY so it needs
  1016. * to be managed together along with controller clocks which also
  1017. * provides a better power saving. Hence keep phy_power_off/on calls
  1018. * in ufs_qcom_setup_clocks, so that PHY's regulators & clks can be
  1019. * turned on/off along with UFS's clocks.
  1020. *
  1021. * Return: 0 on success, non-zero on failure.
  1022. */
  1023. static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
  1024. enum ufs_notify_change_status status)
  1025. {
  1026. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1027. struct phy *phy;
  1028. int err;
  1029. /*
  1030. * In case ufs_qcom_init() is not yet done, simply ignore.
  1031. * This ufs_qcom_setup_clocks() shall be called from
  1032. * ufs_qcom_init() after init is done.
  1033. */
  1034. if (!host)
  1035. return 0;
  1036. phy = host->generic_phy;
  1037. switch (status) {
  1038. case PRE_CHANGE:
  1039. if (on) {
  1040. ufs_qcom_icc_update_bw(host);
  1041. if (ufs_qcom_is_link_hibern8(hba)) {
  1042. err = ufs_qcom_enable_lane_clks(host);
  1043. if (err) {
  1044. dev_err(hba->dev, "enable lane clks failed, ret=%d\n", err);
  1045. return err;
  1046. }
  1047. }
  1048. } else {
  1049. if (!ufs_qcom_is_link_active(hba)) {
  1050. /* disable device ref_clk */
  1051. ufs_qcom_dev_ref_clk_ctrl(host, false);
  1052. }
  1053. err = phy_power_off(phy);
  1054. if (err) {
  1055. dev_err(hba->dev, "phy power off failed, ret=%d\n", err);
  1056. return err;
  1057. }
  1058. }
  1059. break;
  1060. case POST_CHANGE:
  1061. if (on) {
  1062. err = phy_power_on(phy);
  1063. if (err) {
  1064. dev_err(hba->dev, "phy power on failed, ret = %d\n", err);
  1065. return err;
  1066. }
  1067. /* enable the device ref clock for HS mode*/
  1068. if (ufshcd_is_hs_mode(&hba->pwr_info))
  1069. ufs_qcom_dev_ref_clk_ctrl(host, true);
  1070. } else {
  1071. if (ufs_qcom_is_link_hibern8(hba))
  1072. ufs_qcom_disable_lane_clks(host);
  1073. ufs_qcom_icc_set_bw(host, ufs_qcom_bw_table[MODE_MIN][0][0].mem_bw,
  1074. ufs_qcom_bw_table[MODE_MIN][0][0].cfg_bw);
  1075. }
  1076. break;
  1077. }
  1078. return 0;
  1079. }
  1080. static int
  1081. ufs_qcom_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
  1082. {
  1083. struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
  1084. ufs_qcom_assert_reset(host->hba);
  1085. /* provide 1ms delay to let the reset pulse propagate. */
  1086. usleep_range(1000, 1100);
  1087. return 0;
  1088. }
  1089. static int
  1090. ufs_qcom_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
  1091. {
  1092. struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
  1093. ufs_qcom_deassert_reset(host->hba);
  1094. /*
  1095. * after reset deassertion, phy will need all ref clocks,
  1096. * voltage, current to settle down before starting serdes.
  1097. */
  1098. usleep_range(1000, 1100);
  1099. return 0;
  1100. }
  1101. static const struct reset_control_ops ufs_qcom_reset_ops = {
  1102. .assert = ufs_qcom_reset_assert,
  1103. .deassert = ufs_qcom_reset_deassert,
  1104. };
  1105. static int ufs_qcom_icc_init(struct ufs_qcom_host *host)
  1106. {
  1107. struct device *dev = host->hba->dev;
  1108. int ret;
  1109. host->icc_ddr = devm_of_icc_get(dev, "ufs-ddr");
  1110. if (IS_ERR(host->icc_ddr))
  1111. return dev_err_probe(dev, PTR_ERR(host->icc_ddr),
  1112. "failed to acquire interconnect path\n");
  1113. host->icc_cpu = devm_of_icc_get(dev, "cpu-ufs");
  1114. if (IS_ERR(host->icc_cpu))
  1115. return dev_err_probe(dev, PTR_ERR(host->icc_cpu),
  1116. "failed to acquire interconnect path\n");
  1117. /*
  1118. * Set Maximum bandwidth vote before initializing the UFS controller and
  1119. * device. Ideally, a minimal interconnect vote would suffice for the
  1120. * initialization, but a max vote would allow faster initialization.
  1121. */
  1122. ret = ufs_qcom_icc_set_bw(host, ufs_qcom_bw_table[MODE_MAX][0][0].mem_bw,
  1123. ufs_qcom_bw_table[MODE_MAX][0][0].cfg_bw);
  1124. if (ret < 0)
  1125. return dev_err_probe(dev, ret, "failed to set bandwidth request\n");
  1126. return 0;
  1127. }
  1128. /**
  1129. * ufs_qcom_init - bind phy with controller
  1130. * @hba: host controller instance
  1131. *
  1132. * Binds PHY with controller and powers up PHY enabling clocks
  1133. * and regulators.
  1134. *
  1135. * Return: -EPROBE_DEFER if binding fails, returns negative error
  1136. * on phy power up failure and returns zero on success.
  1137. */
  1138. static int ufs_qcom_init(struct ufs_hba *hba)
  1139. {
  1140. int err;
  1141. struct device *dev = hba->dev;
  1142. struct ufs_qcom_host *host;
  1143. struct ufs_clk_info *clki;
  1144. const struct ufs_qcom_drvdata *drvdata = of_device_get_match_data(hba->dev);
  1145. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  1146. if (!host)
  1147. return -ENOMEM;
  1148. /* Make a two way bind between the qcom host and the hba */
  1149. host->hba = hba;
  1150. ufshcd_set_variant(hba, host);
  1151. /* Setup the optional reset control of HCI */
  1152. host->core_reset = devm_reset_control_get_optional(hba->dev, "rst");
  1153. if (IS_ERR(host->core_reset)) {
  1154. err = dev_err_probe(dev, PTR_ERR(host->core_reset),
  1155. "Failed to get reset control\n");
  1156. goto out_variant_clear;
  1157. }
  1158. /* Fire up the reset controller. Failure here is non-fatal. */
  1159. host->rcdev.of_node = dev->of_node;
  1160. host->rcdev.ops = &ufs_qcom_reset_ops;
  1161. host->rcdev.owner = dev->driver->owner;
  1162. host->rcdev.nr_resets = 1;
  1163. err = devm_reset_controller_register(dev, &host->rcdev);
  1164. if (err)
  1165. dev_warn(dev, "Failed to register reset controller\n");
  1166. if (!has_acpi_companion(dev)) {
  1167. host->generic_phy = devm_phy_get(dev, "ufsphy");
  1168. if (IS_ERR(host->generic_phy)) {
  1169. err = dev_err_probe(dev, PTR_ERR(host->generic_phy), "Failed to get PHY\n");
  1170. goto out_variant_clear;
  1171. }
  1172. }
  1173. err = ufs_qcom_icc_init(host);
  1174. if (err)
  1175. goto out_variant_clear;
  1176. host->device_reset = devm_gpiod_get_optional(dev, "reset",
  1177. GPIOD_OUT_HIGH);
  1178. if (IS_ERR(host->device_reset)) {
  1179. err = dev_err_probe(dev, PTR_ERR(host->device_reset),
  1180. "Failed to acquire device reset gpio\n");
  1181. goto out_variant_clear;
  1182. }
  1183. ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
  1184. &host->hw_ver.minor, &host->hw_ver.step);
  1185. host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1;
  1186. host->dev_ref_clk_en_mask = BIT(26);
  1187. list_for_each_entry(clki, &hba->clk_list_head, list) {
  1188. if (!strcmp(clki->name, "core_clk_unipro"))
  1189. clki->keep_link_active = true;
  1190. }
  1191. err = ufs_qcom_init_lane_clks(host);
  1192. if (err)
  1193. goto out_variant_clear;
  1194. ufs_qcom_set_caps(hba);
  1195. ufs_qcom_advertise_quirks(hba);
  1196. ufs_qcom_set_host_params(hba);
  1197. ufs_qcom_set_phy_gear(host);
  1198. ufs_qcom_parse_gear_limits(hba);
  1199. err = ufs_qcom_ice_init(host);
  1200. if (err)
  1201. goto out_variant_clear;
  1202. ufs_qcom_setup_clocks(hba, true, POST_CHANGE);
  1203. ufs_qcom_get_default_testbus_cfg(host);
  1204. err = ufs_qcom_testbus_config(host);
  1205. if (err)
  1206. /* Failure is non-fatal */
  1207. dev_warn(dev, "%s: failed to configure the testbus %d\n",
  1208. __func__, err);
  1209. if (drvdata && drvdata->no_phy_retention)
  1210. hba->spm_lvl = UFS_PM_LVL_5;
  1211. return 0;
  1212. out_variant_clear:
  1213. ufshcd_set_variant(hba, NULL);
  1214. return err;
  1215. }
  1216. static void ufs_qcom_exit(struct ufs_hba *hba)
  1217. {
  1218. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1219. ufs_qcom_disable_lane_clks(host);
  1220. phy_power_off(host->generic_phy);
  1221. phy_exit(host->generic_phy);
  1222. }
  1223. static int ufs_qcom_fw_managed_init(struct ufs_hba *hba)
  1224. {
  1225. struct device *dev = hba->dev;
  1226. struct ufs_qcom_host *host;
  1227. int err;
  1228. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  1229. if (!host)
  1230. return -ENOMEM;
  1231. host->hba = hba;
  1232. ufshcd_set_variant(hba, host);
  1233. ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
  1234. &host->hw_ver.minor, &host->hw_ver.step);
  1235. err = ufs_qcom_ice_init(host);
  1236. if (err)
  1237. goto out_variant_clear;
  1238. ufs_qcom_get_default_testbus_cfg(host);
  1239. err = ufs_qcom_testbus_config(host);
  1240. if (err)
  1241. /* Failure is non-fatal */
  1242. dev_warn(dev, "Failed to configure the testbus %d\n", err);
  1243. hba->caps |= UFSHCD_CAP_WB_EN;
  1244. ufs_qcom_advertise_quirks(hba);
  1245. host->hba->quirks &= ~UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH;
  1246. hba->spm_lvl = hba->rpm_lvl = hba->pm_lvl_min = UFS_PM_LVL_5;
  1247. ufs_qcom_set_host_params(hba);
  1248. ufs_qcom_parse_gear_limits(hba);
  1249. return 0;
  1250. out_variant_clear:
  1251. ufshcd_set_variant(hba, NULL);
  1252. return err;
  1253. }
  1254. static void ufs_qcom_fw_managed_exit(struct ufs_hba *hba)
  1255. {
  1256. pm_runtime_put_sync(hba->dev);
  1257. }
  1258. /**
  1259. * ufs_qcom_set_clk_40ns_cycles - Configure 40ns clk cycles
  1260. *
  1261. * @hba: host controller instance
  1262. * @cycles_in_1us: No of cycles in 1us to be configured
  1263. *
  1264. * Returns error if dme get/set configuration for 40ns fails
  1265. * and returns zero on success.
  1266. */
  1267. static int ufs_qcom_set_clk_40ns_cycles(struct ufs_hba *hba,
  1268. u32 cycles_in_1us)
  1269. {
  1270. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1271. u32 cycles_in_40ns;
  1272. u32 reg;
  1273. int err;
  1274. /*
  1275. * UFS host controller V4.0.0 onwards needs to program
  1276. * PA_VS_CORE_CLK_40NS_CYCLES attribute per programmed
  1277. * frequency of unipro core clk of UFS host controller.
  1278. */
  1279. if (host->hw_ver.major < 4)
  1280. return 0;
  1281. /*
  1282. * Generic formulae for cycles_in_40ns = (freq_unipro/25) is not
  1283. * applicable for all frequencies. For ex: ceil(37.5 MHz/25) will
  1284. * be 2 and ceil(403 MHZ/25) will be 17 whereas Hardware
  1285. * specification expect to be 16. Hence use exact hardware spec
  1286. * mandated value for cycles_in_40ns instead of calculating using
  1287. * generic formulae.
  1288. */
  1289. switch (cycles_in_1us) {
  1290. case UNIPRO_CORE_CLK_FREQ_403_MHZ:
  1291. cycles_in_40ns = 16;
  1292. break;
  1293. case UNIPRO_CORE_CLK_FREQ_300_MHZ:
  1294. cycles_in_40ns = 12;
  1295. break;
  1296. case UNIPRO_CORE_CLK_FREQ_201_5_MHZ:
  1297. cycles_in_40ns = 8;
  1298. break;
  1299. case UNIPRO_CORE_CLK_FREQ_150_MHZ:
  1300. cycles_in_40ns = 6;
  1301. break;
  1302. case UNIPRO_CORE_CLK_FREQ_100_MHZ:
  1303. cycles_in_40ns = 4;
  1304. break;
  1305. case UNIPRO_CORE_CLK_FREQ_75_MHZ:
  1306. cycles_in_40ns = 3;
  1307. break;
  1308. case UNIPRO_CORE_CLK_FREQ_37_5_MHZ:
  1309. cycles_in_40ns = 2;
  1310. break;
  1311. default:
  1312. dev_err(hba->dev, "UNIPRO clk freq %u MHz not supported\n",
  1313. cycles_in_1us);
  1314. return -EINVAL;
  1315. }
  1316. err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CORE_CLK_40NS_CYCLES), &reg);
  1317. if (err)
  1318. return err;
  1319. reg &= ~PA_VS_CORE_CLK_40NS_CYCLES_MASK;
  1320. reg |= cycles_in_40ns;
  1321. return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CORE_CLK_40NS_CYCLES), reg);
  1322. }
  1323. static int ufs_qcom_set_core_clk_ctrl(struct ufs_hba *hba, bool is_scale_up, unsigned long freq)
  1324. {
  1325. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1326. struct list_head *head = &hba->clk_list_head;
  1327. struct ufs_clk_info *clki;
  1328. u32 cycles_in_1us = 0;
  1329. u32 core_clk_ctrl_reg;
  1330. unsigned long clk_freq;
  1331. int err;
  1332. if (hba->use_pm_opp && freq != ULONG_MAX) {
  1333. clk_freq = ufs_qcom_opp_freq_to_clk_freq(hba, freq, "core_clk_unipro");
  1334. if (clk_freq) {
  1335. cycles_in_1us = ceil(clk_freq, HZ_PER_MHZ);
  1336. goto set_core_clk_ctrl;
  1337. }
  1338. }
  1339. list_for_each_entry(clki, head, list) {
  1340. if (!IS_ERR_OR_NULL(clki->clk) &&
  1341. !strcmp(clki->name, "core_clk_unipro")) {
  1342. if (!clki->max_freq) {
  1343. cycles_in_1us = 150; /* default for backwards compatibility */
  1344. break;
  1345. }
  1346. if (freq == ULONG_MAX) {
  1347. cycles_in_1us = ceil(clki->max_freq, HZ_PER_MHZ);
  1348. break;
  1349. }
  1350. if (is_scale_up)
  1351. cycles_in_1us = ceil(clki->max_freq, HZ_PER_MHZ);
  1352. else
  1353. cycles_in_1us = ceil(clk_get_rate(clki->clk), HZ_PER_MHZ);
  1354. break;
  1355. }
  1356. }
  1357. set_core_clk_ctrl:
  1358. err = ufshcd_dme_get(hba,
  1359. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1360. &core_clk_ctrl_reg);
  1361. if (err)
  1362. return err;
  1363. /* Bit mask is different for UFS host controller V4.0.0 onwards */
  1364. if (host->hw_ver.major >= 4) {
  1365. if (!FIELD_FIT(CLK_1US_CYCLES_MASK_V4, cycles_in_1us))
  1366. return -ERANGE;
  1367. core_clk_ctrl_reg &= ~CLK_1US_CYCLES_MASK_V4;
  1368. core_clk_ctrl_reg |= FIELD_PREP(CLK_1US_CYCLES_MASK_V4, cycles_in_1us);
  1369. } else {
  1370. if (!FIELD_FIT(CLK_1US_CYCLES_MASK, cycles_in_1us))
  1371. return -ERANGE;
  1372. core_clk_ctrl_reg &= ~CLK_1US_CYCLES_MASK;
  1373. core_clk_ctrl_reg |= FIELD_PREP(CLK_1US_CYCLES_MASK, cycles_in_1us);
  1374. }
  1375. /* Clear CORE_CLK_DIV_EN */
  1376. core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
  1377. err = ufshcd_dme_set(hba,
  1378. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1379. core_clk_ctrl_reg);
  1380. if (err)
  1381. return err;
  1382. /* Configure unipro core clk 40ns attribute */
  1383. return ufs_qcom_set_clk_40ns_cycles(hba, cycles_in_1us);
  1384. }
  1385. static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba, unsigned long freq)
  1386. {
  1387. int ret;
  1388. ret = ufs_qcom_cfg_timers(hba, true, freq);
  1389. if (ret) {
  1390. dev_err(hba->dev, "%s ufs cfg timer failed\n", __func__);
  1391. return ret;
  1392. }
  1393. /* set unipro core clock attributes and clear clock divider */
  1394. return ufs_qcom_set_core_clk_ctrl(hba, true, freq);
  1395. }
  1396. static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba)
  1397. {
  1398. return 0;
  1399. }
  1400. static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba)
  1401. {
  1402. int err;
  1403. u32 core_clk_ctrl_reg;
  1404. err = ufshcd_dme_get(hba,
  1405. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1406. &core_clk_ctrl_reg);
  1407. /* make sure CORE_CLK_DIV_EN is cleared */
  1408. if (!err &&
  1409. (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) {
  1410. core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
  1411. err = ufshcd_dme_set(hba,
  1412. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1413. core_clk_ctrl_reg);
  1414. }
  1415. return err;
  1416. }
  1417. static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba, unsigned long freq)
  1418. {
  1419. int ret;
  1420. ret = ufs_qcom_cfg_timers(hba, false, freq);
  1421. if (ret) {
  1422. dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n", __func__);
  1423. return ret;
  1424. }
  1425. /* set unipro core clock attributes and clear clock divider */
  1426. return ufs_qcom_set_core_clk_ctrl(hba, false, freq);
  1427. }
  1428. static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba, bool scale_up,
  1429. unsigned long target_freq,
  1430. enum ufs_notify_change_status status)
  1431. {
  1432. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1433. int err;
  1434. /* check the host controller state before sending hibern8 cmd */
  1435. if (!ufshcd_is_hba_active(hba))
  1436. return 0;
  1437. if (status == PRE_CHANGE) {
  1438. err = ufshcd_uic_hibern8_enter(hba);
  1439. if (err)
  1440. return err;
  1441. if (scale_up)
  1442. err = ufs_qcom_clk_scale_up_pre_change(hba, target_freq);
  1443. else
  1444. err = ufs_qcom_clk_scale_down_pre_change(hba);
  1445. if (err) {
  1446. ufshcd_uic_hibern8_exit(hba);
  1447. return err;
  1448. }
  1449. } else {
  1450. if (scale_up)
  1451. err = ufs_qcom_clk_scale_up_post_change(hba);
  1452. else
  1453. err = ufs_qcom_clk_scale_down_post_change(hba, target_freq);
  1454. if (err) {
  1455. ufshcd_uic_hibern8_exit(hba);
  1456. return err;
  1457. }
  1458. ufs_qcom_icc_update_bw(host);
  1459. ufshcd_uic_hibern8_exit(hba);
  1460. }
  1461. return 0;
  1462. }
  1463. static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host)
  1464. {
  1465. ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN,
  1466. UFS_REG_TEST_BUS_EN, REG_UFS_CFG1);
  1467. ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1);
  1468. }
  1469. static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host)
  1470. {
  1471. /* provide a legal default configuration */
  1472. host->testbus.select_major = TSTBUS_UNIPRO;
  1473. host->testbus.select_minor = 37;
  1474. }
  1475. static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host)
  1476. {
  1477. if (host->testbus.select_major >= TSTBUS_MAX) {
  1478. dev_err(host->hba->dev,
  1479. "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n",
  1480. __func__, host->testbus.select_major);
  1481. return false;
  1482. }
  1483. return true;
  1484. }
  1485. int ufs_qcom_testbus_config(struct ufs_qcom_host *host)
  1486. {
  1487. int reg;
  1488. int offset;
  1489. u32 mask = TEST_BUS_SUB_SEL_MASK;
  1490. if (!host)
  1491. return -EINVAL;
  1492. if (!ufs_qcom_testbus_cfg_is_ok(host))
  1493. return -EPERM;
  1494. switch (host->testbus.select_major) {
  1495. case TSTBUS_UAWM:
  1496. reg = UFS_TEST_BUS_CTRL_0;
  1497. offset = 24;
  1498. break;
  1499. case TSTBUS_UARM:
  1500. reg = UFS_TEST_BUS_CTRL_0;
  1501. offset = 16;
  1502. break;
  1503. case TSTBUS_TXUC:
  1504. reg = UFS_TEST_BUS_CTRL_0;
  1505. offset = 8;
  1506. break;
  1507. case TSTBUS_RXUC:
  1508. reg = UFS_TEST_BUS_CTRL_0;
  1509. offset = 0;
  1510. break;
  1511. case TSTBUS_DFC:
  1512. reg = UFS_TEST_BUS_CTRL_1;
  1513. offset = 24;
  1514. break;
  1515. case TSTBUS_TRLUT:
  1516. reg = UFS_TEST_BUS_CTRL_1;
  1517. offset = 16;
  1518. break;
  1519. case TSTBUS_TMRLUT:
  1520. reg = UFS_TEST_BUS_CTRL_1;
  1521. offset = 8;
  1522. break;
  1523. case TSTBUS_OCSC:
  1524. reg = UFS_TEST_BUS_CTRL_1;
  1525. offset = 0;
  1526. break;
  1527. case TSTBUS_WRAPPER:
  1528. reg = UFS_TEST_BUS_CTRL_2;
  1529. offset = 16;
  1530. break;
  1531. case TSTBUS_COMBINED:
  1532. reg = UFS_TEST_BUS_CTRL_2;
  1533. offset = 8;
  1534. break;
  1535. case TSTBUS_UTP_HCI:
  1536. reg = UFS_TEST_BUS_CTRL_2;
  1537. offset = 0;
  1538. break;
  1539. case TSTBUS_UNIPRO:
  1540. reg = UFS_UNIPRO_CFG;
  1541. offset = 20;
  1542. mask = 0xFFF;
  1543. break;
  1544. /*
  1545. * No need for a default case, since
  1546. * ufs_qcom_testbus_cfg_is_ok() checks that the configuration
  1547. * is legal
  1548. */
  1549. }
  1550. mask <<= offset;
  1551. ufshcd_rmwl(host->hba, TEST_BUS_SEL,
  1552. (u32)host->testbus.select_major << 19,
  1553. REG_UFS_CFG1);
  1554. ufshcd_rmwl(host->hba, mask,
  1555. (u32)host->testbus.select_minor << offset,
  1556. reg);
  1557. ufs_qcom_enable_test_bus(host);
  1558. return 0;
  1559. }
  1560. static void ufs_qcom_dump_testbus(struct ufs_hba *hba)
  1561. {
  1562. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1563. int i, j, nminor = 0, testbus_len = 0;
  1564. char *prefix;
  1565. u32 *testbus __free(kfree) = kmalloc_array(256, sizeof(u32), GFP_KERNEL);
  1566. if (!testbus)
  1567. return;
  1568. for (j = 0; j < TSTBUS_MAX; j++) {
  1569. nminor = testbus_info[j].nminor;
  1570. prefix = testbus_info[j].prefix;
  1571. host->testbus.select_major = j;
  1572. testbus_len = nminor * sizeof(u32);
  1573. for (i = 0; i < nminor; i++) {
  1574. host->testbus.select_minor = i;
  1575. ufs_qcom_testbus_config(host);
  1576. testbus[i] = ufshcd_readl(hba, UFS_TEST_BUS);
  1577. }
  1578. print_hex_dump(KERN_ERR, prefix, DUMP_PREFIX_OFFSET,
  1579. 16, 4, testbus, testbus_len, false);
  1580. }
  1581. }
  1582. static int ufs_qcom_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
  1583. const char *prefix, void __iomem *base)
  1584. {
  1585. size_t pos;
  1586. if (offset % 4 != 0 || len % 4 != 0)
  1587. return -EINVAL;
  1588. u32 *regs __free(kfree) = kzalloc(len, GFP_ATOMIC);
  1589. if (!regs)
  1590. return -ENOMEM;
  1591. for (pos = 0; pos < len; pos += 4)
  1592. regs[pos / 4] = readl(base + offset + pos);
  1593. print_hex_dump(KERN_ERR, prefix,
  1594. len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,
  1595. 16, 4, regs, len, false);
  1596. return 0;
  1597. }
  1598. static void ufs_qcom_dump_mcq_hci_regs(struct ufs_hba *hba)
  1599. {
  1600. struct ufshcd_mcq_opr_info_t *opr = &hba->mcq_opr[0];
  1601. void __iomem *mcq_vs_base = hba->mcq_base + UFS_MEM_VS_BASE;
  1602. struct dump_info {
  1603. void __iomem *base;
  1604. size_t offset;
  1605. size_t len;
  1606. const char *prefix;
  1607. };
  1608. struct dump_info mcq_dumps[] = {
  1609. {hba->mcq_base, 0x0, 256 * 4, "MCQ HCI-0 "},
  1610. {hba->mcq_base, 0x400, 256 * 4, "MCQ HCI-1 "},
  1611. {mcq_vs_base, 0x0, 5 * 4, "MCQ VS-0 "},
  1612. {opr->base, 0x0, 256 * 4, "MCQ SQD-0 "},
  1613. {opr->base, 0x400, 256 * 4, "MCQ SQD-1 "},
  1614. {opr->base, 0x800, 256 * 4, "MCQ SQD-2 "},
  1615. {opr->base, 0xc00, 256 * 4, "MCQ SQD-3 "},
  1616. {opr->base, 0x1000, 256 * 4, "MCQ SQD-4 "},
  1617. {opr->base, 0x1400, 256 * 4, "MCQ SQD-5 "},
  1618. {opr->base, 0x1800, 256 * 4, "MCQ SQD-6 "},
  1619. {opr->base, 0x1c00, 256 * 4, "MCQ SQD-7 "},
  1620. };
  1621. for (int i = 0; i < ARRAY_SIZE(mcq_dumps); i++) {
  1622. ufs_qcom_dump_regs(hba, mcq_dumps[i].offset, mcq_dumps[i].len,
  1623. mcq_dumps[i].prefix, mcq_dumps[i].base);
  1624. cond_resched();
  1625. }
  1626. }
  1627. static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba)
  1628. {
  1629. u32 reg;
  1630. struct ufs_qcom_host *host;
  1631. host = ufshcd_get_variant(hba);
  1632. dev_err(hba->dev, "HW_H8_ENTER_CNT=%d\n", ufshcd_readl(hba, REG_UFS_HW_H8_ENTER_CNT));
  1633. dev_err(hba->dev, "HW_H8_EXIT_CNT=%d\n", ufshcd_readl(hba, REG_UFS_HW_H8_EXIT_CNT));
  1634. dev_err(hba->dev, "SW_H8_ENTER_CNT=%d\n", ufshcd_readl(hba, REG_UFS_SW_H8_ENTER_CNT));
  1635. dev_err(hba->dev, "SW_H8_EXIT_CNT=%d\n", ufshcd_readl(hba, REG_UFS_SW_H8_EXIT_CNT));
  1636. dev_err(hba->dev, "SW_AFTER_HW_H8_ENTER_CNT=%d\n",
  1637. ufshcd_readl(hba, REG_UFS_SW_AFTER_HW_H8_ENTER_CNT));
  1638. ufshcd_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16 * 4,
  1639. "HCI Vendor Specific Registers ");
  1640. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC);
  1641. ufshcd_dump_regs(hba, reg, 44 * 4, "UFS_UFS_DBG_RD_REG_OCSC ");
  1642. reg = ufshcd_readl(hba, REG_UFS_CFG1);
  1643. reg |= UTP_DBG_RAMS_EN;
  1644. ufshcd_writel(hba, reg, REG_UFS_CFG1);
  1645. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM);
  1646. ufshcd_dump_regs(hba, reg, 32 * 4, "UFS_UFS_DBG_RD_EDTL_RAM ");
  1647. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM);
  1648. ufshcd_dump_regs(hba, reg, 128 * 4, "UFS_UFS_DBG_RD_DESC_RAM ");
  1649. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM);
  1650. ufshcd_dump_regs(hba, reg, 64 * 4, "UFS_UFS_DBG_RD_PRDT_RAM ");
  1651. /* clear bit 17 - UTP_DBG_RAMS_EN */
  1652. ufshcd_rmwl(hba, UTP_DBG_RAMS_EN, 0, REG_UFS_CFG1);
  1653. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM);
  1654. ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UAWM ");
  1655. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM);
  1656. ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UARM ");
  1657. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC);
  1658. ufshcd_dump_regs(hba, reg, 48 * 4, "UFS_DBG_RD_REG_TXUC ");
  1659. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC);
  1660. ufshcd_dump_regs(hba, reg, 27 * 4, "UFS_DBG_RD_REG_RXUC ");
  1661. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC);
  1662. ufshcd_dump_regs(hba, reg, 19 * 4, "UFS_DBG_RD_REG_DFC ");
  1663. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT);
  1664. ufshcd_dump_regs(hba, reg, 34 * 4, "UFS_DBG_RD_REG_TRLUT ");
  1665. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT);
  1666. ufshcd_dump_regs(hba, reg, 9 * 4, "UFS_DBG_RD_REG_TMRLUT ");
  1667. if (hba->mcq_enabled) {
  1668. reg = ufs_qcom_get_debug_reg_offset(host, UFS_RD_REG_MCQ);
  1669. ufshcd_dump_regs(hba, reg, 64 * 4, "HCI MCQ Debug Registers ");
  1670. }
  1671. /* ensure below dumps occur only in task context due to blocking calls. */
  1672. if (in_task()) {
  1673. /* Dump MCQ Host Vendor Specific Registers */
  1674. if (hba->mcq_enabled)
  1675. ufs_qcom_dump_mcq_hci_regs(hba);
  1676. /* voluntarily yield the CPU as we are dumping too much data */
  1677. ufshcd_dump_regs(hba, UFS_TEST_BUS, 4, "UFS_TEST_BUS ");
  1678. cond_resched();
  1679. ufs_qcom_dump_testbus(hba);
  1680. }
  1681. }
  1682. /**
  1683. * ufs_qcom_device_reset() - toggle the (optional) device reset line
  1684. * @hba: per-adapter instance
  1685. *
  1686. * Toggles the (optional) reset line to reset the attached device.
  1687. */
  1688. static int ufs_qcom_device_reset(struct ufs_hba *hba)
  1689. {
  1690. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1691. /* reset gpio is optional */
  1692. if (!host->device_reset)
  1693. return -EOPNOTSUPP;
  1694. /*
  1695. * The UFS device shall detect reset pulses of 1us, sleep for 10us to
  1696. * be on the safe side.
  1697. */
  1698. ufs_qcom_device_reset_ctrl(hba, true);
  1699. usleep_range(10, 15);
  1700. ufs_qcom_device_reset_ctrl(hba, false);
  1701. usleep_range(10, 15);
  1702. return 0;
  1703. }
  1704. /**
  1705. * ufs_qcom_fw_managed_device_reset - Reset UFS device under FW-managed design
  1706. * @hba: pointer to UFS host bus adapter
  1707. *
  1708. * In the firmware-managed reset model, the power domain is powered on by genpd
  1709. * before the UFS controller driver probes. For subsequent resets (such as
  1710. * suspend/resume or recovery), the UFS driver must explicitly invoke PM runtime
  1711. *
  1712. * Return: 0 on success or a negative error code on failure.
  1713. */
  1714. static int ufs_qcom_fw_managed_device_reset(struct ufs_hba *hba)
  1715. {
  1716. static bool is_boot = true;
  1717. int err;
  1718. /* Skip reset on cold boot; perform it on subsequent calls */
  1719. if (is_boot) {
  1720. is_boot = false;
  1721. return 0;
  1722. }
  1723. pm_runtime_put_sync(hba->dev);
  1724. err = pm_runtime_resume_and_get(hba->dev);
  1725. if (err < 0) {
  1726. dev_err(hba->dev, "PM runtime resume failed: %d\n", err);
  1727. return err;
  1728. }
  1729. return 0;
  1730. }
  1731. static void ufs_qcom_config_scaling_param(struct ufs_hba *hba,
  1732. struct devfreq_dev_profile *p,
  1733. struct devfreq_simple_ondemand_data *d)
  1734. {
  1735. p->polling_ms = 60;
  1736. p->timer = DEVFREQ_TIMER_DELAYED;
  1737. d->upthreshold = 70;
  1738. d->downdifferential = 5;
  1739. hba->clk_scaling.suspend_on_no_request = true;
  1740. }
  1741. static int ufs_qcom_mcq_config_resource(struct ufs_hba *hba)
  1742. {
  1743. struct platform_device *pdev = to_platform_device(hba->dev);
  1744. struct resource *res;
  1745. /* Map the MCQ configuration region */
  1746. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mcq");
  1747. if (!res) {
  1748. dev_err(hba->dev, "MCQ resource not found in device tree\n");
  1749. return -ENODEV;
  1750. }
  1751. hba->mcq_base = devm_ioremap_resource(hba->dev, res);
  1752. if (IS_ERR(hba->mcq_base)) {
  1753. dev_err(hba->dev, "Failed to map MCQ region: %ld\n",
  1754. PTR_ERR(hba->mcq_base));
  1755. return PTR_ERR(hba->mcq_base);
  1756. }
  1757. return 0;
  1758. }
  1759. static int ufs_qcom_op_runtime_config(struct ufs_hba *hba)
  1760. {
  1761. struct ufshcd_mcq_opr_info_t *opr;
  1762. int i;
  1763. u32 doorbell_offsets[OPR_MAX];
  1764. /*
  1765. * Configure doorbell address offsets in MCQ configuration registers.
  1766. * These values are offsets relative to mmio_base (UFS_HCI_BASE).
  1767. *
  1768. * Memory Layout:
  1769. * - mmio_base = UFS_HCI_BASE
  1770. * - mcq_base = MCQ_CONFIG_BASE = mmio_base + (UFS_QCOM_MCQCAP_QCFGPTR * 0x200)
  1771. * - Doorbell registers are at: mmio_base + (UFS_QCOM_MCQCAP_QCFGPTR * 0x200) +
  1772. * - UFS_QCOM_MCQ_SQD_OFFSET
  1773. * - Which is also: mcq_base + UFS_QCOM_MCQ_SQD_OFFSET
  1774. */
  1775. doorbell_offsets[OPR_SQD] = UFS_QCOM_SQD_ADDR_OFFSET;
  1776. doorbell_offsets[OPR_SQIS] = UFS_QCOM_SQIS_ADDR_OFFSET;
  1777. doorbell_offsets[OPR_CQD] = UFS_QCOM_CQD_ADDR_OFFSET;
  1778. doorbell_offsets[OPR_CQIS] = UFS_QCOM_CQIS_ADDR_OFFSET;
  1779. /*
  1780. * Configure MCQ operation registers.
  1781. *
  1782. * The doorbell registers are physically located within the MCQ region:
  1783. * - doorbell_physical_addr = mmio_base + doorbell_offset
  1784. * - doorbell_physical_addr = mcq_base + (doorbell_offset - MCQ_CONFIG_OFFSET)
  1785. */
  1786. for (i = 0; i < OPR_MAX; i++) {
  1787. opr = &hba->mcq_opr[i];
  1788. opr->offset = doorbell_offsets[i]; /* Offset relative to mmio_base */
  1789. opr->stride = UFS_QCOM_MCQ_STRIDE; /* 256 bytes between queues */
  1790. /*
  1791. * Calculate the actual doorbell base address within MCQ region:
  1792. * base = mcq_base + (doorbell_offset - MCQ_CONFIG_OFFSET)
  1793. */
  1794. opr->base = hba->mcq_base + (opr->offset - UFS_QCOM_MCQ_CONFIG_OFFSET);
  1795. }
  1796. return 0;
  1797. }
  1798. static int ufs_qcom_get_hba_mac(struct ufs_hba *hba)
  1799. {
  1800. /* Qualcomm HC supports up to 64 */
  1801. return MAX_SUPP_MAC;
  1802. }
  1803. static int ufs_qcom_get_outstanding_cqs(struct ufs_hba *hba,
  1804. unsigned long *ocqs)
  1805. {
  1806. /* Read from MCQ vendor-specific register in MCQ region */
  1807. *ocqs = readl(hba->mcq_base + UFS_MEM_CQIS_VS);
  1808. return 0;
  1809. }
  1810. static void ufs_qcom_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  1811. {
  1812. struct device *dev = msi_desc_to_dev(desc);
  1813. struct ufs_hba *hba = dev_get_drvdata(dev);
  1814. ufshcd_mcq_config_esi(hba, msg);
  1815. }
  1816. struct ufs_qcom_irq {
  1817. unsigned int irq;
  1818. unsigned int idx;
  1819. struct ufs_hba *hba;
  1820. };
  1821. static irqreturn_t ufs_qcom_mcq_esi_handler(int irq, void *data)
  1822. {
  1823. struct ufs_qcom_irq *qi = data;
  1824. struct ufs_hba *hba = qi->hba;
  1825. struct ufs_hw_queue *hwq = &hba->uhq[qi->idx];
  1826. ufshcd_mcq_write_cqis(hba, 0x1, qi->idx);
  1827. ufshcd_mcq_poll_cqe_lock(hba, hwq);
  1828. return IRQ_HANDLED;
  1829. }
  1830. static int ufs_qcom_config_esi(struct ufs_hba *hba)
  1831. {
  1832. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1833. int nr_irqs, ret;
  1834. if (host->esi_enabled)
  1835. return 0;
  1836. /*
  1837. * 1. We only handle CQs as of now.
  1838. * 2. Poll queues do not need ESI.
  1839. */
  1840. nr_irqs = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL];
  1841. ret = platform_device_msi_init_and_alloc_irqs(hba->dev, nr_irqs,
  1842. ufs_qcom_write_msi_msg);
  1843. if (ret) {
  1844. dev_warn(hba->dev, "Platform MSI not supported or failed, continuing without ESI\n");
  1845. return ret; /* Continue without ESI */
  1846. }
  1847. struct ufs_qcom_irq *qi = devm_kcalloc(hba->dev, nr_irqs, sizeof(*qi), GFP_KERNEL);
  1848. if (!qi) {
  1849. platform_device_msi_free_irqs_all(hba->dev);
  1850. return -ENOMEM;
  1851. }
  1852. for (int idx = 0; idx < nr_irqs; idx++) {
  1853. qi[idx].irq = msi_get_virq(hba->dev, idx);
  1854. qi[idx].idx = idx;
  1855. qi[idx].hba = hba;
  1856. ret = devm_request_irq(hba->dev, qi[idx].irq, ufs_qcom_mcq_esi_handler,
  1857. IRQF_SHARED, "qcom-mcq-esi", qi + idx);
  1858. if (ret) {
  1859. dev_err(hba->dev, "%s: Failed to request IRQ for %d, err = %d\n",
  1860. __func__, qi[idx].irq, ret);
  1861. /* Free previously allocated IRQs */
  1862. for (int j = 0; j < idx; j++)
  1863. devm_free_irq(hba->dev, qi[j].irq, qi + j);
  1864. platform_device_msi_free_irqs_all(hba->dev);
  1865. devm_kfree(hba->dev, qi);
  1866. return ret;
  1867. }
  1868. }
  1869. if (host->hw_ver.major >= 6) {
  1870. ufshcd_rmwl(hba, ESI_VEC_MASK, FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1),
  1871. REG_UFS_CFG3);
  1872. }
  1873. ufshcd_mcq_enable_esi(hba);
  1874. host->esi_enabled = true;
  1875. return 0;
  1876. }
  1877. static unsigned long ufs_qcom_opp_freq_to_clk_freq(struct ufs_hba *hba,
  1878. unsigned long freq, char *name)
  1879. {
  1880. struct ufs_clk_info *clki;
  1881. struct dev_pm_opp *opp;
  1882. unsigned long clk_freq;
  1883. int idx = 0;
  1884. bool found = false;
  1885. opp = dev_pm_opp_find_freq_exact_indexed(hba->dev, freq, 0, true);
  1886. if (IS_ERR(opp)) {
  1887. dev_err(hba->dev, "Failed to find OPP for exact frequency %lu\n", freq);
  1888. return 0;
  1889. }
  1890. list_for_each_entry(clki, &hba->clk_list_head, list) {
  1891. if (!strcmp(clki->name, name)) {
  1892. found = true;
  1893. break;
  1894. }
  1895. idx++;
  1896. }
  1897. if (!found) {
  1898. dev_err(hba->dev, "Failed to find clock '%s' in clk list\n", name);
  1899. dev_pm_opp_put(opp);
  1900. return 0;
  1901. }
  1902. clk_freq = dev_pm_opp_get_freq_indexed(opp, idx);
  1903. dev_pm_opp_put(opp);
  1904. return clk_freq;
  1905. }
  1906. static u32 ufs_qcom_freq_to_gear_speed(struct ufs_hba *hba, unsigned long freq)
  1907. {
  1908. u32 gear = UFS_HS_DONT_CHANGE;
  1909. unsigned long unipro_freq;
  1910. if (!hba->use_pm_opp)
  1911. return gear;
  1912. unipro_freq = ufs_qcom_opp_freq_to_clk_freq(hba, freq, "core_clk_unipro");
  1913. switch (unipro_freq) {
  1914. case 403000000:
  1915. gear = UFS_HS_G5;
  1916. break;
  1917. case 300000000:
  1918. gear = UFS_HS_G4;
  1919. break;
  1920. case 201500000:
  1921. gear = UFS_HS_G3;
  1922. break;
  1923. case 150000000:
  1924. case 100000000:
  1925. gear = UFS_HS_G2;
  1926. break;
  1927. case 75000000:
  1928. case 37500000:
  1929. gear = UFS_HS_G1;
  1930. break;
  1931. default:
  1932. dev_err(hba->dev, "%s: Unsupported clock freq : %lu\n", __func__, freq);
  1933. return UFS_HS_DONT_CHANGE;
  1934. }
  1935. return min_t(u32, gear, hba->max_pwr_info.info.gear_rx);
  1936. }
  1937. /*
  1938. * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
  1939. *
  1940. * The variant operations configure the necessary controller and PHY
  1941. * handshake during initialization.
  1942. */
  1943. static const struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
  1944. .name = "qcom",
  1945. .init = ufs_qcom_init,
  1946. .exit = ufs_qcom_exit,
  1947. .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version,
  1948. .clk_scale_notify = ufs_qcom_clk_scale_notify,
  1949. .setup_clocks = ufs_qcom_setup_clocks,
  1950. .hce_enable_notify = ufs_qcom_hce_enable_notify,
  1951. .link_startup_notify = ufs_qcom_link_startup_notify,
  1952. .pwr_change_notify = ufs_qcom_pwr_change_notify,
  1953. .apply_dev_quirks = ufs_qcom_apply_dev_quirks,
  1954. .fixup_dev_quirks = ufs_qcom_fixup_dev_quirks,
  1955. .suspend = ufs_qcom_suspend,
  1956. .resume = ufs_qcom_resume,
  1957. .dbg_register_dump = ufs_qcom_dump_dbg_regs,
  1958. .device_reset = ufs_qcom_device_reset,
  1959. .config_scaling_param = ufs_qcom_config_scaling_param,
  1960. .mcq_config_resource = ufs_qcom_mcq_config_resource,
  1961. .get_hba_mac = ufs_qcom_get_hba_mac,
  1962. .op_runtime_config = ufs_qcom_op_runtime_config,
  1963. .get_outstanding_cqs = ufs_qcom_get_outstanding_cqs,
  1964. .config_esi = ufs_qcom_config_esi,
  1965. .freq_to_gear_speed = ufs_qcom_freq_to_gear_speed,
  1966. };
  1967. static const struct ufs_hba_variant_ops ufs_hba_qcom_sa8255p_vops = {
  1968. .name = "qcom-sa8255p",
  1969. .init = ufs_qcom_fw_managed_init,
  1970. .exit = ufs_qcom_fw_managed_exit,
  1971. .hce_enable_notify = ufs_qcom_fw_managed_hce_enable_notify,
  1972. .pwr_change_notify = ufs_qcom_pwr_change_notify,
  1973. .apply_dev_quirks = ufs_qcom_apply_dev_quirks,
  1974. .fixup_dev_quirks = ufs_qcom_fixup_dev_quirks,
  1975. .suspend = ufs_qcom_fw_managed_suspend,
  1976. .resume = ufs_qcom_fw_managed_resume,
  1977. .dbg_register_dump = ufs_qcom_dump_dbg_regs,
  1978. .device_reset = ufs_qcom_fw_managed_device_reset,
  1979. };
  1980. /**
  1981. * ufs_qcom_probe - probe routine of the driver
  1982. * @pdev: pointer to Platform device handle
  1983. *
  1984. * Return: zero for success and non-zero for failure.
  1985. */
  1986. static int ufs_qcom_probe(struct platform_device *pdev)
  1987. {
  1988. int err;
  1989. struct device *dev = &pdev->dev;
  1990. const struct ufs_hba_variant_ops *vops;
  1991. const struct ufs_qcom_drvdata *drvdata = device_get_match_data(dev);
  1992. if (drvdata && drvdata->vops)
  1993. vops = drvdata->vops;
  1994. else
  1995. vops = &ufs_hba_qcom_vops;
  1996. /* Perform generic probe */
  1997. err = ufshcd_pltfrm_init(pdev, vops);
  1998. if (err)
  1999. return dev_err_probe(dev, err, "ufshcd_pltfrm_init() failed\n");
  2000. return 0;
  2001. }
  2002. /**
  2003. * ufs_qcom_remove - set driver_data of the device to NULL
  2004. * @pdev: pointer to platform device handle
  2005. *
  2006. * Always returns 0
  2007. */
  2008. static void ufs_qcom_remove(struct platform_device *pdev)
  2009. {
  2010. struct ufs_hba *hba = platform_get_drvdata(pdev);
  2011. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  2012. ufshcd_pltfrm_remove(pdev);
  2013. if (host->esi_enabled)
  2014. platform_device_msi_free_irqs_all(hba->dev);
  2015. }
  2016. static const struct ufs_qcom_drvdata ufs_qcom_sm8550_drvdata = {
  2017. .quirks = UFSHCD_QUIRK_BROKEN_LSDBS_CAP,
  2018. .no_phy_retention = true,
  2019. };
  2020. static const struct ufs_qcom_drvdata ufs_qcom_sa8255p_drvdata = {
  2021. .vops = &ufs_hba_qcom_sa8255p_vops
  2022. };
  2023. static const struct of_device_id ufs_qcom_of_match[] __maybe_unused = {
  2024. { .compatible = "qcom,ufshc" },
  2025. { .compatible = "qcom,sm8550-ufshc", .data = &ufs_qcom_sm8550_drvdata },
  2026. { .compatible = "qcom,sm8650-ufshc", .data = &ufs_qcom_sm8550_drvdata },
  2027. { .compatible = "qcom,sa8255p-ufshc", .data = &ufs_qcom_sa8255p_drvdata },
  2028. {},
  2029. };
  2030. MODULE_DEVICE_TABLE(of, ufs_qcom_of_match);
  2031. #ifdef CONFIG_ACPI
  2032. static const struct acpi_device_id ufs_qcom_acpi_match[] = {
  2033. { "QCOM24A5" },
  2034. { },
  2035. };
  2036. MODULE_DEVICE_TABLE(acpi, ufs_qcom_acpi_match);
  2037. #endif
  2038. static const struct dev_pm_ops ufs_qcom_pm_ops = {
  2039. SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
  2040. .prepare = ufshcd_suspend_prepare,
  2041. .complete = ufshcd_resume_complete,
  2042. #ifdef CONFIG_PM_SLEEP
  2043. .suspend = ufshcd_system_suspend,
  2044. .resume = ufshcd_system_resume,
  2045. .freeze = ufshcd_system_freeze,
  2046. .restore = ufshcd_system_restore,
  2047. .thaw = ufshcd_system_thaw,
  2048. #endif
  2049. };
  2050. static struct platform_driver ufs_qcom_pltform = {
  2051. .probe = ufs_qcom_probe,
  2052. .remove = ufs_qcom_remove,
  2053. .driver = {
  2054. .name = "ufshcd-qcom",
  2055. .pm = &ufs_qcom_pm_ops,
  2056. .of_match_table = of_match_ptr(ufs_qcom_of_match),
  2057. .acpi_match_table = ACPI_PTR(ufs_qcom_acpi_match),
  2058. },
  2059. };
  2060. module_platform_driver(ufs_qcom_pltform);
  2061. MODULE_DESCRIPTION("Qualcomm UFS host controller driver");
  2062. MODULE_LICENSE("GPL v2");