ufs-qcom.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888
  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/delay.h>
  8. #include <linux/devfreq.h>
  9. #include <linux/gpio/consumer.h>
  10. #include <linux/interconnect.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/reset-controller.h>
  16. #include <linux/time.h>
  17. #include <soc/qcom/ice.h>
  18. #include <ufs/ufshcd.h>
  19. #include <ufs/ufshci.h>
  20. #include <ufs/ufs_quirks.h>
  21. #include <ufs/unipro.h>
  22. #include "ufshcd-pltfrm.h"
  23. #include "ufs-qcom.h"
  24. #define MCQ_QCFGPTR_MASK GENMASK(7, 0)
  25. #define MCQ_QCFGPTR_UNIT 0x200
  26. #define MCQ_SQATTR_OFFSET(c) \
  27. ((((c) >> 16) & MCQ_QCFGPTR_MASK) * MCQ_QCFGPTR_UNIT)
  28. #define MCQ_QCFG_SIZE 0x40
  29. enum {
  30. TSTBUS_UAWM,
  31. TSTBUS_UARM,
  32. TSTBUS_TXUC,
  33. TSTBUS_RXUC,
  34. TSTBUS_DFC,
  35. TSTBUS_TRLUT,
  36. TSTBUS_TMRLUT,
  37. TSTBUS_OCSC,
  38. TSTBUS_UTP_HCI,
  39. TSTBUS_COMBINED,
  40. TSTBUS_WRAPPER,
  41. TSTBUS_UNIPRO,
  42. TSTBUS_MAX,
  43. };
  44. #define QCOM_UFS_MAX_GEAR 5
  45. #define QCOM_UFS_MAX_LANE 2
  46. enum {
  47. MODE_MIN,
  48. MODE_PWM,
  49. MODE_HS_RA,
  50. MODE_HS_RB,
  51. MODE_MAX,
  52. };
  53. static const struct __ufs_qcom_bw_table {
  54. u32 mem_bw;
  55. u32 cfg_bw;
  56. } ufs_qcom_bw_table[MODE_MAX + 1][QCOM_UFS_MAX_GEAR + 1][QCOM_UFS_MAX_LANE + 1] = {
  57. [MODE_MIN][0][0] = { 0, 0 }, /* Bandwidth values in KB/s */
  58. [MODE_PWM][UFS_PWM_G1][UFS_LANE_1] = { 922, 1000 },
  59. [MODE_PWM][UFS_PWM_G2][UFS_LANE_1] = { 1844, 1000 },
  60. [MODE_PWM][UFS_PWM_G3][UFS_LANE_1] = { 3688, 1000 },
  61. [MODE_PWM][UFS_PWM_G4][UFS_LANE_1] = { 7376, 1000 },
  62. [MODE_PWM][UFS_PWM_G5][UFS_LANE_1] = { 14752, 1000 },
  63. [MODE_PWM][UFS_PWM_G1][UFS_LANE_2] = { 1844, 1000 },
  64. [MODE_PWM][UFS_PWM_G2][UFS_LANE_2] = { 3688, 1000 },
  65. [MODE_PWM][UFS_PWM_G3][UFS_LANE_2] = { 7376, 1000 },
  66. [MODE_PWM][UFS_PWM_G4][UFS_LANE_2] = { 14752, 1000 },
  67. [MODE_PWM][UFS_PWM_G5][UFS_LANE_2] = { 29504, 1000 },
  68. [MODE_HS_RA][UFS_HS_G1][UFS_LANE_1] = { 127796, 1000 },
  69. [MODE_HS_RA][UFS_HS_G2][UFS_LANE_1] = { 255591, 1000 },
  70. [MODE_HS_RA][UFS_HS_G3][UFS_LANE_1] = { 1492582, 102400 },
  71. [MODE_HS_RA][UFS_HS_G4][UFS_LANE_1] = { 2915200, 204800 },
  72. [MODE_HS_RA][UFS_HS_G5][UFS_LANE_1] = { 5836800, 409600 },
  73. [MODE_HS_RA][UFS_HS_G1][UFS_LANE_2] = { 255591, 1000 },
  74. [MODE_HS_RA][UFS_HS_G2][UFS_LANE_2] = { 511181, 1000 },
  75. [MODE_HS_RA][UFS_HS_G3][UFS_LANE_2] = { 1492582, 204800 },
  76. [MODE_HS_RA][UFS_HS_G4][UFS_LANE_2] = { 2915200, 409600 },
  77. [MODE_HS_RA][UFS_HS_G5][UFS_LANE_2] = { 5836800, 819200 },
  78. [MODE_HS_RB][UFS_HS_G1][UFS_LANE_1] = { 149422, 1000 },
  79. [MODE_HS_RB][UFS_HS_G2][UFS_LANE_1] = { 298189, 1000 },
  80. [MODE_HS_RB][UFS_HS_G3][UFS_LANE_1] = { 1492582, 102400 },
  81. [MODE_HS_RB][UFS_HS_G4][UFS_LANE_1] = { 2915200, 204800 },
  82. [MODE_HS_RB][UFS_HS_G5][UFS_LANE_1] = { 5836800, 409600 },
  83. [MODE_HS_RB][UFS_HS_G1][UFS_LANE_2] = { 298189, 1000 },
  84. [MODE_HS_RB][UFS_HS_G2][UFS_LANE_2] = { 596378, 1000 },
  85. [MODE_HS_RB][UFS_HS_G3][UFS_LANE_2] = { 1492582, 204800 },
  86. [MODE_HS_RB][UFS_HS_G4][UFS_LANE_2] = { 2915200, 409600 },
  87. [MODE_HS_RB][UFS_HS_G5][UFS_LANE_2] = { 5836800, 819200 },
  88. [MODE_MAX][0][0] = { 7643136, 819200 },
  89. };
  90. static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host);
  91. static int ufs_qcom_set_core_clk_ctrl(struct ufs_hba *hba, bool is_scale_up);
  92. static struct ufs_qcom_host *rcdev_to_ufs_host(struct reset_controller_dev *rcd)
  93. {
  94. return container_of(rcd, struct ufs_qcom_host, rcdev);
  95. }
  96. #ifdef CONFIG_SCSI_UFS_CRYPTO
  97. static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host)
  98. {
  99. if (host->hba->caps & UFSHCD_CAP_CRYPTO)
  100. qcom_ice_enable(host->ice);
  101. }
  102. static int ufs_qcom_ice_init(struct ufs_qcom_host *host)
  103. {
  104. struct ufs_hba *hba = host->hba;
  105. struct device *dev = hba->dev;
  106. struct qcom_ice *ice;
  107. ice = devm_of_qcom_ice_get(dev);
  108. if (ice == ERR_PTR(-EOPNOTSUPP)) {
  109. dev_warn(dev, "Disabling inline encryption support\n");
  110. ice = NULL;
  111. }
  112. if (IS_ERR_OR_NULL(ice))
  113. return PTR_ERR_OR_ZERO(ice);
  114. host->ice = ice;
  115. hba->caps |= UFSHCD_CAP_CRYPTO;
  116. return 0;
  117. }
  118. static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host)
  119. {
  120. if (host->hba->caps & UFSHCD_CAP_CRYPTO)
  121. return qcom_ice_resume(host->ice);
  122. return 0;
  123. }
  124. static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host)
  125. {
  126. if (host->hba->caps & UFSHCD_CAP_CRYPTO)
  127. return qcom_ice_suspend(host->ice);
  128. return 0;
  129. }
  130. static int ufs_qcom_ice_program_key(struct ufs_hba *hba,
  131. const union ufs_crypto_cfg_entry *cfg,
  132. int slot)
  133. {
  134. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  135. union ufs_crypto_cap_entry cap;
  136. if (!(cfg->config_enable & UFS_CRYPTO_CONFIGURATION_ENABLE))
  137. return qcom_ice_evict_key(host->ice, slot);
  138. /* Only AES-256-XTS has been tested so far. */
  139. cap = hba->crypto_cap_array[cfg->crypto_cap_idx];
  140. if (cap.algorithm_id != UFS_CRYPTO_ALG_AES_XTS ||
  141. cap.key_size != UFS_CRYPTO_KEY_SIZE_256)
  142. return -EOPNOTSUPP;
  143. return qcom_ice_program_key(host->ice,
  144. QCOM_ICE_CRYPTO_ALG_AES_XTS,
  145. QCOM_ICE_CRYPTO_KEY_SIZE_256,
  146. cfg->crypto_key,
  147. cfg->data_unit_size, slot);
  148. }
  149. #else
  150. #define ufs_qcom_ice_program_key NULL
  151. static inline void ufs_qcom_ice_enable(struct ufs_qcom_host *host)
  152. {
  153. }
  154. static int ufs_qcom_ice_init(struct ufs_qcom_host *host)
  155. {
  156. return 0;
  157. }
  158. static inline int ufs_qcom_ice_resume(struct ufs_qcom_host *host)
  159. {
  160. return 0;
  161. }
  162. static inline int ufs_qcom_ice_suspend(struct ufs_qcom_host *host)
  163. {
  164. return 0;
  165. }
  166. #endif
  167. static void ufs_qcom_disable_lane_clks(struct ufs_qcom_host *host)
  168. {
  169. if (!host->is_lane_clks_enabled)
  170. return;
  171. clk_bulk_disable_unprepare(host->num_clks, host->clks);
  172. host->is_lane_clks_enabled = false;
  173. }
  174. static int ufs_qcom_enable_lane_clks(struct ufs_qcom_host *host)
  175. {
  176. int err;
  177. err = clk_bulk_prepare_enable(host->num_clks, host->clks);
  178. if (err)
  179. return err;
  180. host->is_lane_clks_enabled = true;
  181. return 0;
  182. }
  183. static int ufs_qcom_init_lane_clks(struct ufs_qcom_host *host)
  184. {
  185. int err;
  186. struct device *dev = host->hba->dev;
  187. if (has_acpi_companion(dev))
  188. return 0;
  189. err = devm_clk_bulk_get_all(dev, &host->clks);
  190. if (err <= 0)
  191. return err;
  192. host->num_clks = err;
  193. return 0;
  194. }
  195. static int ufs_qcom_check_hibern8(struct ufs_hba *hba)
  196. {
  197. int err;
  198. u32 tx_fsm_val;
  199. unsigned long timeout = jiffies + msecs_to_jiffies(HBRN8_POLL_TOUT_MS);
  200. do {
  201. err = ufshcd_dme_get(hba,
  202. UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
  203. UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
  204. &tx_fsm_val);
  205. if (err || tx_fsm_val == TX_FSM_HIBERN8)
  206. break;
  207. /* sleep for max. 200us */
  208. usleep_range(100, 200);
  209. } while (time_before(jiffies, timeout));
  210. /*
  211. * we might have scheduled out for long during polling so
  212. * check the state again.
  213. */
  214. if (time_after(jiffies, timeout))
  215. err = ufshcd_dme_get(hba,
  216. UIC_ARG_MIB_SEL(MPHY_TX_FSM_STATE,
  217. UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
  218. &tx_fsm_val);
  219. if (err) {
  220. dev_err(hba->dev, "%s: unable to get TX_FSM_STATE, err %d\n",
  221. __func__, err);
  222. } else if (tx_fsm_val != TX_FSM_HIBERN8) {
  223. err = tx_fsm_val;
  224. dev_err(hba->dev, "%s: invalid TX_FSM_STATE = %d\n",
  225. __func__, err);
  226. }
  227. return err;
  228. }
  229. static void ufs_qcom_select_unipro_mode(struct ufs_qcom_host *host)
  230. {
  231. ufshcd_rmwl(host->hba, QUNIPRO_SEL, QUNIPRO_SEL, REG_UFS_CFG1);
  232. if (host->hw_ver.major >= 0x05)
  233. ufshcd_rmwl(host->hba, QUNIPRO_G4_SEL, 0, REG_UFS_CFG0);
  234. }
  235. /*
  236. * ufs_qcom_host_reset - reset host controller and PHY
  237. */
  238. static int ufs_qcom_host_reset(struct ufs_hba *hba)
  239. {
  240. int ret;
  241. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  242. bool reenable_intr;
  243. if (!host->core_reset)
  244. return 0;
  245. reenable_intr = hba->is_irq_enabled;
  246. ufshcd_disable_irq(hba);
  247. ret = reset_control_assert(host->core_reset);
  248. if (ret) {
  249. dev_err(hba->dev, "%s: core_reset assert failed, err = %d\n",
  250. __func__, ret);
  251. return ret;
  252. }
  253. /*
  254. * The hardware requirement for delay between assert/deassert
  255. * is at least 3-4 sleep clock (32.7KHz) cycles, which comes to
  256. * ~125us (4/32768). To be on the safe side add 200us delay.
  257. */
  258. usleep_range(200, 210);
  259. ret = reset_control_deassert(host->core_reset);
  260. if (ret) {
  261. dev_err(hba->dev, "%s: core_reset deassert failed, err = %d\n",
  262. __func__, ret);
  263. return ret;
  264. }
  265. usleep_range(1000, 1100);
  266. if (reenable_intr)
  267. ufshcd_enable_irq(hba);
  268. return 0;
  269. }
  270. static u32 ufs_qcom_get_hs_gear(struct ufs_hba *hba)
  271. {
  272. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  273. if (host->hw_ver.major >= 0x4)
  274. return UFS_QCOM_MAX_GEAR(ufshcd_readl(hba, REG_UFS_PARAM0));
  275. /* Default is HS-G3 */
  276. return UFS_HS_G3;
  277. }
  278. static int ufs_qcom_power_up_sequence(struct ufs_hba *hba)
  279. {
  280. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  281. struct ufs_host_params *host_params = &host->host_params;
  282. struct phy *phy = host->generic_phy;
  283. enum phy_mode mode;
  284. int ret;
  285. /*
  286. * HW ver 5 can only support up to HS-G5 Rate-A due to HW limitations.
  287. * If the HS-G5 PHY gear is used, update host_params->hs_rate to Rate-A,
  288. * so that the subsequent power mode change shall stick to Rate-A.
  289. */
  290. if (host->hw_ver.major == 0x5) {
  291. if (host->phy_gear == UFS_HS_G5)
  292. host_params->hs_rate = PA_HS_MODE_A;
  293. else
  294. host_params->hs_rate = PA_HS_MODE_B;
  295. }
  296. mode = host_params->hs_rate == PA_HS_MODE_B ? PHY_MODE_UFS_HS_B : PHY_MODE_UFS_HS_A;
  297. /* Reset UFS Host Controller and PHY */
  298. ret = ufs_qcom_host_reset(hba);
  299. if (ret)
  300. return ret;
  301. if (phy->power_count)
  302. phy_power_off(phy);
  303. /* phy initialization - calibrate the phy */
  304. ret = phy_init(phy);
  305. if (ret) {
  306. dev_err(hba->dev, "%s: phy init failed, ret = %d\n",
  307. __func__, ret);
  308. return ret;
  309. }
  310. ret = phy_set_mode_ext(phy, mode, host->phy_gear);
  311. if (ret)
  312. goto out_disable_phy;
  313. /* power on phy - start serdes and phy's power and clocks */
  314. ret = phy_power_on(phy);
  315. if (ret) {
  316. dev_err(hba->dev, "%s: phy power on failed, ret = %d\n",
  317. __func__, ret);
  318. goto out_disable_phy;
  319. }
  320. ufs_qcom_select_unipro_mode(host);
  321. return 0;
  322. out_disable_phy:
  323. phy_exit(phy);
  324. return ret;
  325. }
  326. /*
  327. * The UTP controller has a number of internal clock gating cells (CGCs).
  328. * Internal hardware sub-modules within the UTP controller control the CGCs.
  329. * Hardware CGCs disable the clock to inactivate UTP sub-modules not involved
  330. * in a specific operation, UTP controller CGCs are by default disabled and
  331. * this function enables them (after every UFS link startup) to save some power
  332. * leakage.
  333. */
  334. static void ufs_qcom_enable_hw_clk_gating(struct ufs_hba *hba)
  335. {
  336. ufshcd_rmwl(hba, REG_UFS_CFG2_CGC_EN_ALL, REG_UFS_CFG2_CGC_EN_ALL,
  337. REG_UFS_CFG2);
  338. /* Ensure that HW clock gating is enabled before next operations */
  339. ufshcd_readl(hba, REG_UFS_CFG2);
  340. }
  341. static int ufs_qcom_hce_enable_notify(struct ufs_hba *hba,
  342. enum ufs_notify_change_status status)
  343. {
  344. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  345. int err;
  346. switch (status) {
  347. case PRE_CHANGE:
  348. err = ufs_qcom_power_up_sequence(hba);
  349. if (err)
  350. return err;
  351. /*
  352. * The PHY PLL output is the source of tx/rx lane symbol
  353. * clocks, hence, enable the lane clocks only after PHY
  354. * is initialized.
  355. */
  356. err = ufs_qcom_enable_lane_clks(host);
  357. break;
  358. case POST_CHANGE:
  359. /* check if UFS PHY moved from DISABLED to HIBERN8 */
  360. err = ufs_qcom_check_hibern8(hba);
  361. ufs_qcom_enable_hw_clk_gating(hba);
  362. ufs_qcom_ice_enable(host);
  363. break;
  364. default:
  365. dev_err(hba->dev, "%s: invalid status %d\n", __func__, status);
  366. err = -EINVAL;
  367. break;
  368. }
  369. return err;
  370. }
  371. /**
  372. * ufs_qcom_cfg_timers - Configure ufs qcom cfg timers
  373. *
  374. * @hba: host controller instance
  375. * @gear: Current operating gear
  376. * @hs: current power mode
  377. * @rate: current operating rate (A or B)
  378. * @update_link_startup_timer: indicate if link_start ongoing
  379. * @is_pre_scale_up: flag to check if pre scale up condition.
  380. * Return: zero for success and non-zero in case of a failure.
  381. */
  382. static int ufs_qcom_cfg_timers(struct ufs_hba *hba, u32 gear,
  383. u32 hs, u32 rate, bool update_link_startup_timer,
  384. bool is_pre_scale_up)
  385. {
  386. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  387. struct ufs_clk_info *clki;
  388. unsigned long core_clk_rate = 0;
  389. u32 core_clk_cycles_per_us;
  390. /*
  391. * UTP controller uses SYS1CLK_1US_REG register for Interrupt
  392. * Aggregation logic.
  393. * It is mandatory to write SYS1CLK_1US_REG register on UFS host
  394. * controller V4.0.0 onwards.
  395. */
  396. if (host->hw_ver.major < 4 && !ufshcd_is_intr_aggr_allowed(hba))
  397. return 0;
  398. if (gear == 0) {
  399. dev_err(hba->dev, "%s: invalid gear = %d\n", __func__, gear);
  400. return -EINVAL;
  401. }
  402. list_for_each_entry(clki, &hba->clk_list_head, list) {
  403. if (!strcmp(clki->name, "core_clk")) {
  404. if (is_pre_scale_up)
  405. core_clk_rate = clki->max_freq;
  406. else
  407. core_clk_rate = clk_get_rate(clki->clk);
  408. break;
  409. }
  410. }
  411. /* If frequency is smaller than 1MHz, set to 1MHz */
  412. if (core_clk_rate < DEFAULT_CLK_RATE_HZ)
  413. core_clk_rate = DEFAULT_CLK_RATE_HZ;
  414. core_clk_cycles_per_us = core_clk_rate / USEC_PER_SEC;
  415. if (ufshcd_readl(hba, REG_UFS_SYS1CLK_1US) != core_clk_cycles_per_us) {
  416. ufshcd_writel(hba, core_clk_cycles_per_us, REG_UFS_SYS1CLK_1US);
  417. /*
  418. * make sure above write gets applied before we return from
  419. * this function.
  420. */
  421. ufshcd_readl(hba, REG_UFS_SYS1CLK_1US);
  422. }
  423. return 0;
  424. }
  425. static int ufs_qcom_link_startup_notify(struct ufs_hba *hba,
  426. enum ufs_notify_change_status status)
  427. {
  428. int err = 0;
  429. switch (status) {
  430. case PRE_CHANGE:
  431. if (ufs_qcom_cfg_timers(hba, UFS_PWM_G1, SLOWAUTO_MODE,
  432. 0, true, false)) {
  433. dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
  434. __func__);
  435. return -EINVAL;
  436. }
  437. err = ufs_qcom_set_core_clk_ctrl(hba, true);
  438. if (err)
  439. dev_err(hba->dev, "cfg core clk ctrl failed\n");
  440. /*
  441. * Some UFS devices (and may be host) have issues if LCC is
  442. * enabled. So we are setting PA_Local_TX_LCC_Enable to 0
  443. * before link startup which will make sure that both host
  444. * and device TX LCC are disabled once link startup is
  445. * completed.
  446. */
  447. err = ufshcd_disable_host_tx_lcc(hba);
  448. break;
  449. default:
  450. break;
  451. }
  452. return err;
  453. }
  454. static void ufs_qcom_device_reset_ctrl(struct ufs_hba *hba, bool asserted)
  455. {
  456. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  457. /* reset gpio is optional */
  458. if (!host->device_reset)
  459. return;
  460. gpiod_set_value_cansleep(host->device_reset, asserted);
  461. }
  462. static int ufs_qcom_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op,
  463. enum ufs_notify_change_status status)
  464. {
  465. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  466. struct phy *phy = host->generic_phy;
  467. if (status == PRE_CHANGE)
  468. return 0;
  469. if (ufs_qcom_is_link_off(hba)) {
  470. /*
  471. * Disable the tx/rx lane symbol clocks before PHY is
  472. * powered down as the PLL source should be disabled
  473. * after downstream clocks are disabled.
  474. */
  475. ufs_qcom_disable_lane_clks(host);
  476. phy_power_off(phy);
  477. /* reset the connected UFS device during power down */
  478. ufs_qcom_device_reset_ctrl(hba, true);
  479. } else if (!ufs_qcom_is_link_active(hba)) {
  480. ufs_qcom_disable_lane_clks(host);
  481. }
  482. return ufs_qcom_ice_suspend(host);
  483. }
  484. static int ufs_qcom_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
  485. {
  486. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  487. struct phy *phy = host->generic_phy;
  488. int err;
  489. if (ufs_qcom_is_link_off(hba)) {
  490. err = phy_power_on(phy);
  491. if (err) {
  492. dev_err(hba->dev, "%s: failed PHY power on: %d\n",
  493. __func__, err);
  494. return err;
  495. }
  496. err = ufs_qcom_enable_lane_clks(host);
  497. if (err)
  498. return err;
  499. } else if (!ufs_qcom_is_link_active(hba)) {
  500. err = ufs_qcom_enable_lane_clks(host);
  501. if (err)
  502. return err;
  503. }
  504. return ufs_qcom_ice_resume(host);
  505. }
  506. static void ufs_qcom_dev_ref_clk_ctrl(struct ufs_qcom_host *host, bool enable)
  507. {
  508. if (host->dev_ref_clk_ctrl_mmio &&
  509. (enable ^ host->is_dev_ref_clk_enabled)) {
  510. u32 temp = readl_relaxed(host->dev_ref_clk_ctrl_mmio);
  511. if (enable)
  512. temp |= host->dev_ref_clk_en_mask;
  513. else
  514. temp &= ~host->dev_ref_clk_en_mask;
  515. /*
  516. * If we are here to disable this clock it might be immediately
  517. * after entering into hibern8 in which case we need to make
  518. * sure that device ref_clk is active for specific time after
  519. * hibern8 enter.
  520. */
  521. if (!enable) {
  522. unsigned long gating_wait;
  523. gating_wait = host->hba->dev_info.clk_gating_wait_us;
  524. if (!gating_wait) {
  525. udelay(1);
  526. } else {
  527. /*
  528. * bRefClkGatingWaitTime defines the minimum
  529. * time for which the reference clock is
  530. * required by device during transition from
  531. * HS-MODE to LS-MODE or HIBERN8 state. Give it
  532. * more delay to be on the safe side.
  533. */
  534. gating_wait += 10;
  535. usleep_range(gating_wait, gating_wait + 10);
  536. }
  537. }
  538. writel_relaxed(temp, host->dev_ref_clk_ctrl_mmio);
  539. /*
  540. * Make sure the write to ref_clk reaches the destination and
  541. * not stored in a Write Buffer (WB).
  542. */
  543. readl(host->dev_ref_clk_ctrl_mmio);
  544. /*
  545. * If we call hibern8 exit after this, we need to make sure that
  546. * device ref_clk is stable for at least 1us before the hibern8
  547. * exit command.
  548. */
  549. if (enable)
  550. udelay(1);
  551. host->is_dev_ref_clk_enabled = enable;
  552. }
  553. }
  554. static int ufs_qcom_icc_set_bw(struct ufs_qcom_host *host, u32 mem_bw, u32 cfg_bw)
  555. {
  556. struct device *dev = host->hba->dev;
  557. int ret;
  558. ret = icc_set_bw(host->icc_ddr, 0, mem_bw);
  559. if (ret < 0) {
  560. dev_err(dev, "failed to set bandwidth request: %d\n", ret);
  561. return ret;
  562. }
  563. ret = icc_set_bw(host->icc_cpu, 0, cfg_bw);
  564. if (ret < 0) {
  565. dev_err(dev, "failed to set bandwidth request: %d\n", ret);
  566. return ret;
  567. }
  568. return 0;
  569. }
  570. static struct __ufs_qcom_bw_table ufs_qcom_get_bw_table(struct ufs_qcom_host *host)
  571. {
  572. struct ufs_pa_layer_attr *p = &host->dev_req_params;
  573. int gear = max_t(u32, p->gear_rx, p->gear_tx);
  574. int lane = max_t(u32, p->lane_rx, p->lane_tx);
  575. if (WARN_ONCE(gear > QCOM_UFS_MAX_GEAR,
  576. "ICC scaling for UFS Gear (%d) not supported. Using Gear (%d) bandwidth\n",
  577. gear, QCOM_UFS_MAX_GEAR))
  578. gear = QCOM_UFS_MAX_GEAR;
  579. if (WARN_ONCE(lane > QCOM_UFS_MAX_LANE,
  580. "ICC scaling for UFS Lane (%d) not supported. Using Lane (%d) bandwidth\n",
  581. lane, QCOM_UFS_MAX_LANE))
  582. lane = QCOM_UFS_MAX_LANE;
  583. if (ufshcd_is_hs_mode(p)) {
  584. if (p->hs_rate == PA_HS_MODE_B)
  585. return ufs_qcom_bw_table[MODE_HS_RB][gear][lane];
  586. else
  587. return ufs_qcom_bw_table[MODE_HS_RA][gear][lane];
  588. } else {
  589. return ufs_qcom_bw_table[MODE_PWM][gear][lane];
  590. }
  591. }
  592. static int ufs_qcom_icc_update_bw(struct ufs_qcom_host *host)
  593. {
  594. struct __ufs_qcom_bw_table bw_table;
  595. bw_table = ufs_qcom_get_bw_table(host);
  596. return ufs_qcom_icc_set_bw(host, bw_table.mem_bw, bw_table.cfg_bw);
  597. }
  598. static int ufs_qcom_pwr_change_notify(struct ufs_hba *hba,
  599. enum ufs_notify_change_status status,
  600. struct ufs_pa_layer_attr *dev_max_params,
  601. struct ufs_pa_layer_attr *dev_req_params)
  602. {
  603. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  604. struct ufs_host_params *host_params = &host->host_params;
  605. int ret = 0;
  606. if (!dev_req_params) {
  607. pr_err("%s: incoming dev_req_params is NULL\n", __func__);
  608. return -EINVAL;
  609. }
  610. switch (status) {
  611. case PRE_CHANGE:
  612. ret = ufshcd_negotiate_pwr_params(host_params, dev_max_params, dev_req_params);
  613. if (ret) {
  614. dev_err(hba->dev, "%s: failed to determine capabilities\n",
  615. __func__);
  616. return ret;
  617. }
  618. /*
  619. * During UFS driver probe, always update the PHY gear to match the negotiated
  620. * gear, so that, if quirk UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH is enabled,
  621. * the second init can program the optimal PHY settings. This allows one to start
  622. * the first init with either the minimum or the maximum support gear.
  623. */
  624. if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
  625. /*
  626. * Skip REINIT if the negotiated gear matches with the
  627. * initial phy_gear. Otherwise, update the phy_gear to
  628. * program the optimal gear setting during REINIT.
  629. */
  630. if (host->phy_gear == dev_req_params->gear_tx)
  631. hba->quirks &= ~UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH;
  632. else
  633. host->phy_gear = dev_req_params->gear_tx;
  634. }
  635. /* enable the device ref clock before changing to HS mode */
  636. if (!ufshcd_is_hs_mode(&hba->pwr_info) &&
  637. ufshcd_is_hs_mode(dev_req_params))
  638. ufs_qcom_dev_ref_clk_ctrl(host, true);
  639. if (host->hw_ver.major >= 0x4) {
  640. ufshcd_dme_configure_adapt(hba,
  641. dev_req_params->gear_tx,
  642. PA_INITIAL_ADAPT);
  643. }
  644. break;
  645. case POST_CHANGE:
  646. if (ufs_qcom_cfg_timers(hba, dev_req_params->gear_rx,
  647. dev_req_params->pwr_rx,
  648. dev_req_params->hs_rate, false, false)) {
  649. dev_err(hba->dev, "%s: ufs_qcom_cfg_timers() failed\n",
  650. __func__);
  651. /*
  652. * we return error code at the end of the routine,
  653. * but continue to configure UFS_PHY_TX_LANE_ENABLE
  654. * and bus voting as usual
  655. */
  656. ret = -EINVAL;
  657. }
  658. /* cache the power mode parameters to use internally */
  659. memcpy(&host->dev_req_params,
  660. dev_req_params, sizeof(*dev_req_params));
  661. ufs_qcom_icc_update_bw(host);
  662. /* disable the device ref clock if entered PWM mode */
  663. if (ufshcd_is_hs_mode(&hba->pwr_info) &&
  664. !ufshcd_is_hs_mode(dev_req_params))
  665. ufs_qcom_dev_ref_clk_ctrl(host, false);
  666. break;
  667. default:
  668. ret = -EINVAL;
  669. break;
  670. }
  671. return ret;
  672. }
  673. static int ufs_qcom_quirk_host_pa_saveconfigtime(struct ufs_hba *hba)
  674. {
  675. int err;
  676. u32 pa_vs_config_reg1;
  677. err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
  678. &pa_vs_config_reg1);
  679. if (err)
  680. return err;
  681. /* Allow extension of MSB bits of PA_SaveConfigTime attribute */
  682. return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CONFIG_REG1),
  683. (pa_vs_config_reg1 | (1 << 12)));
  684. }
  685. static int ufs_qcom_apply_dev_quirks(struct ufs_hba *hba)
  686. {
  687. int err = 0;
  688. if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME)
  689. err = ufs_qcom_quirk_host_pa_saveconfigtime(hba);
  690. if (hba->dev_info.wmanufacturerid == UFS_VENDOR_WDC)
  691. hba->dev_quirks |= UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE;
  692. return err;
  693. }
  694. static u32 ufs_qcom_get_ufs_hci_version(struct ufs_hba *hba)
  695. {
  696. return ufshci_version(2, 0);
  697. }
  698. /**
  699. * ufs_qcom_advertise_quirks - advertise the known QCOM UFS controller quirks
  700. * @hba: host controller instance
  701. *
  702. * QCOM UFS host controller might have some non standard behaviours (quirks)
  703. * than what is specified by UFSHCI specification. Advertise all such
  704. * quirks to standard UFS host controller driver so standard takes them into
  705. * account.
  706. */
  707. static void ufs_qcom_advertise_quirks(struct ufs_hba *hba)
  708. {
  709. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  710. if (host->hw_ver.major == 0x2)
  711. hba->quirks |= UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION;
  712. if (host->hw_ver.major > 0x3)
  713. hba->quirks |= UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH;
  714. if (of_device_is_compatible(hba->dev->of_node, "qcom,sm8550-ufshc"))
  715. hba->quirks |= UFSHCD_QUIRK_BROKEN_LSDBS_CAP;
  716. }
  717. static void ufs_qcom_set_phy_gear(struct ufs_qcom_host *host)
  718. {
  719. struct ufs_host_params *host_params = &host->host_params;
  720. u32 val, dev_major;
  721. /*
  722. * Default to powering up the PHY to the max gear possible, which is
  723. * backwards compatible with lower gears but not optimal from
  724. * a power usage point of view. After device negotiation, if the
  725. * gear is lower a reinit will be performed to program the PHY
  726. * to the ideal gear for this combo of controller and device.
  727. */
  728. host->phy_gear = host_params->hs_tx_gear;
  729. if (host->hw_ver.major < 0x4) {
  730. /*
  731. * These controllers only have one PHY init sequence,
  732. * let's power up the PHY using that (the minimum supported
  733. * gear, UFS_HS_G2).
  734. */
  735. host->phy_gear = UFS_HS_G2;
  736. } else if (host->hw_ver.major >= 0x5) {
  737. val = ufshcd_readl(host->hba, REG_UFS_DEBUG_SPARE_CFG);
  738. dev_major = FIELD_GET(UFS_DEV_VER_MAJOR_MASK, val);
  739. /*
  740. * Since the UFS device version is populated, let's remove the
  741. * REINIT quirk as the negotiated gear won't change during boot.
  742. * So there is no need to do reinit.
  743. */
  744. if (dev_major != 0x0)
  745. host->hba->quirks &= ~UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH;
  746. /*
  747. * For UFS 3.1 device and older, power up the PHY using HS-G4
  748. * PHY gear to save power.
  749. */
  750. if (dev_major > 0x0 && dev_major < 0x4)
  751. host->phy_gear = UFS_HS_G4;
  752. }
  753. }
  754. static void ufs_qcom_set_host_params(struct ufs_hba *hba)
  755. {
  756. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  757. struct ufs_host_params *host_params = &host->host_params;
  758. ufshcd_init_host_params(host_params);
  759. /* This driver only supports symmetic gear setting i.e., hs_tx_gear == hs_rx_gear */
  760. host_params->hs_tx_gear = host_params->hs_rx_gear = ufs_qcom_get_hs_gear(hba);
  761. }
  762. static void ufs_qcom_set_caps(struct ufs_hba *hba)
  763. {
  764. hba->caps |= UFSHCD_CAP_CLK_GATING | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;
  765. hba->caps |= UFSHCD_CAP_CLK_SCALING | UFSHCD_CAP_WB_WITH_CLK_SCALING;
  766. hba->caps |= UFSHCD_CAP_AUTO_BKOPS_SUSPEND;
  767. hba->caps |= UFSHCD_CAP_WB_EN;
  768. hba->caps |= UFSHCD_CAP_AGGR_POWER_COLLAPSE;
  769. hba->caps |= UFSHCD_CAP_RPM_AUTOSUSPEND;
  770. }
  771. /**
  772. * ufs_qcom_setup_clocks - enables/disable clocks
  773. * @hba: host controller instance
  774. * @on: If true, enable clocks else disable them.
  775. * @status: PRE_CHANGE or POST_CHANGE notify
  776. *
  777. * Return: 0 on success, non-zero on failure.
  778. */
  779. static int ufs_qcom_setup_clocks(struct ufs_hba *hba, bool on,
  780. enum ufs_notify_change_status status)
  781. {
  782. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  783. /*
  784. * In case ufs_qcom_init() is not yet done, simply ignore.
  785. * This ufs_qcom_setup_clocks() shall be called from
  786. * ufs_qcom_init() after init is done.
  787. */
  788. if (!host)
  789. return 0;
  790. switch (status) {
  791. case PRE_CHANGE:
  792. if (on) {
  793. ufs_qcom_icc_update_bw(host);
  794. } else {
  795. if (!ufs_qcom_is_link_active(hba)) {
  796. /* disable device ref_clk */
  797. ufs_qcom_dev_ref_clk_ctrl(host, false);
  798. }
  799. }
  800. break;
  801. case POST_CHANGE:
  802. if (on) {
  803. /* enable the device ref clock for HS mode*/
  804. if (ufshcd_is_hs_mode(&hba->pwr_info))
  805. ufs_qcom_dev_ref_clk_ctrl(host, true);
  806. } else {
  807. ufs_qcom_icc_set_bw(host, ufs_qcom_bw_table[MODE_MIN][0][0].mem_bw,
  808. ufs_qcom_bw_table[MODE_MIN][0][0].cfg_bw);
  809. }
  810. break;
  811. }
  812. return 0;
  813. }
  814. static int
  815. ufs_qcom_reset_assert(struct reset_controller_dev *rcdev, unsigned long id)
  816. {
  817. struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
  818. ufs_qcom_assert_reset(host->hba);
  819. /* provide 1ms delay to let the reset pulse propagate. */
  820. usleep_range(1000, 1100);
  821. return 0;
  822. }
  823. static int
  824. ufs_qcom_reset_deassert(struct reset_controller_dev *rcdev, unsigned long id)
  825. {
  826. struct ufs_qcom_host *host = rcdev_to_ufs_host(rcdev);
  827. ufs_qcom_deassert_reset(host->hba);
  828. /*
  829. * after reset deassertion, phy will need all ref clocks,
  830. * voltage, current to settle down before starting serdes.
  831. */
  832. usleep_range(1000, 1100);
  833. return 0;
  834. }
  835. static const struct reset_control_ops ufs_qcom_reset_ops = {
  836. .assert = ufs_qcom_reset_assert,
  837. .deassert = ufs_qcom_reset_deassert,
  838. };
  839. static int ufs_qcom_icc_init(struct ufs_qcom_host *host)
  840. {
  841. struct device *dev = host->hba->dev;
  842. int ret;
  843. host->icc_ddr = devm_of_icc_get(dev, "ufs-ddr");
  844. if (IS_ERR(host->icc_ddr))
  845. return dev_err_probe(dev, PTR_ERR(host->icc_ddr),
  846. "failed to acquire interconnect path\n");
  847. host->icc_cpu = devm_of_icc_get(dev, "cpu-ufs");
  848. if (IS_ERR(host->icc_cpu))
  849. return dev_err_probe(dev, PTR_ERR(host->icc_cpu),
  850. "failed to acquire interconnect path\n");
  851. /*
  852. * Set Maximum bandwidth vote before initializing the UFS controller and
  853. * device. Ideally, a minimal interconnect vote would suffice for the
  854. * initialization, but a max vote would allow faster initialization.
  855. */
  856. ret = ufs_qcom_icc_set_bw(host, ufs_qcom_bw_table[MODE_MAX][0][0].mem_bw,
  857. ufs_qcom_bw_table[MODE_MAX][0][0].cfg_bw);
  858. if (ret < 0)
  859. return dev_err_probe(dev, ret, "failed to set bandwidth request\n");
  860. return 0;
  861. }
  862. /**
  863. * ufs_qcom_init - bind phy with controller
  864. * @hba: host controller instance
  865. *
  866. * Binds PHY with controller and powers up PHY enabling clocks
  867. * and regulators.
  868. *
  869. * Return: -EPROBE_DEFER if binding fails, returns negative error
  870. * on phy power up failure and returns zero on success.
  871. */
  872. static int ufs_qcom_init(struct ufs_hba *hba)
  873. {
  874. int err;
  875. struct device *dev = hba->dev;
  876. struct ufs_qcom_host *host;
  877. struct ufs_clk_info *clki;
  878. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  879. if (!host)
  880. return -ENOMEM;
  881. /* Make a two way bind between the qcom host and the hba */
  882. host->hba = hba;
  883. ufshcd_set_variant(hba, host);
  884. /* Setup the optional reset control of HCI */
  885. host->core_reset = devm_reset_control_get_optional(hba->dev, "rst");
  886. if (IS_ERR(host->core_reset)) {
  887. err = dev_err_probe(dev, PTR_ERR(host->core_reset),
  888. "Failed to get reset control\n");
  889. goto out_variant_clear;
  890. }
  891. /* Fire up the reset controller. Failure here is non-fatal. */
  892. host->rcdev.of_node = dev->of_node;
  893. host->rcdev.ops = &ufs_qcom_reset_ops;
  894. host->rcdev.owner = dev->driver->owner;
  895. host->rcdev.nr_resets = 1;
  896. err = devm_reset_controller_register(dev, &host->rcdev);
  897. if (err)
  898. dev_warn(dev, "Failed to register reset controller\n");
  899. if (!has_acpi_companion(dev)) {
  900. host->generic_phy = devm_phy_get(dev, "ufsphy");
  901. if (IS_ERR(host->generic_phy)) {
  902. err = dev_err_probe(dev, PTR_ERR(host->generic_phy), "Failed to get PHY\n");
  903. goto out_variant_clear;
  904. }
  905. }
  906. err = ufs_qcom_icc_init(host);
  907. if (err)
  908. goto out_variant_clear;
  909. host->device_reset = devm_gpiod_get_optional(dev, "reset",
  910. GPIOD_OUT_HIGH);
  911. if (IS_ERR(host->device_reset)) {
  912. err = dev_err_probe(dev, PTR_ERR(host->device_reset),
  913. "Failed to acquire device reset gpio\n");
  914. goto out_variant_clear;
  915. }
  916. ufs_qcom_get_controller_revision(hba, &host->hw_ver.major,
  917. &host->hw_ver.minor, &host->hw_ver.step);
  918. host->dev_ref_clk_ctrl_mmio = hba->mmio_base + REG_UFS_CFG1;
  919. host->dev_ref_clk_en_mask = BIT(26);
  920. list_for_each_entry(clki, &hba->clk_list_head, list) {
  921. if (!strcmp(clki->name, "core_clk_unipro"))
  922. clki->keep_link_active = true;
  923. }
  924. err = ufs_qcom_init_lane_clks(host);
  925. if (err)
  926. goto out_variant_clear;
  927. ufs_qcom_set_caps(hba);
  928. ufs_qcom_advertise_quirks(hba);
  929. ufs_qcom_set_host_params(hba);
  930. ufs_qcom_set_phy_gear(host);
  931. err = ufs_qcom_ice_init(host);
  932. if (err)
  933. goto out_variant_clear;
  934. ufs_qcom_setup_clocks(hba, true, POST_CHANGE);
  935. ufs_qcom_get_default_testbus_cfg(host);
  936. err = ufs_qcom_testbus_config(host);
  937. if (err)
  938. /* Failure is non-fatal */
  939. dev_warn(dev, "%s: failed to configure the testbus %d\n",
  940. __func__, err);
  941. return 0;
  942. out_variant_clear:
  943. ufshcd_set_variant(hba, NULL);
  944. return err;
  945. }
  946. static void ufs_qcom_exit(struct ufs_hba *hba)
  947. {
  948. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  949. ufs_qcom_disable_lane_clks(host);
  950. phy_power_off(host->generic_phy);
  951. phy_exit(host->generic_phy);
  952. }
  953. /**
  954. * ufs_qcom_set_clk_40ns_cycles - Configure 40ns clk cycles
  955. *
  956. * @hba: host controller instance
  957. * @cycles_in_1us: No of cycles in 1us to be configured
  958. *
  959. * Returns error if dme get/set configuration for 40ns fails
  960. * and returns zero on success.
  961. */
  962. static int ufs_qcom_set_clk_40ns_cycles(struct ufs_hba *hba,
  963. u32 cycles_in_1us)
  964. {
  965. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  966. u32 cycles_in_40ns;
  967. u32 reg;
  968. int err;
  969. /*
  970. * UFS host controller V4.0.0 onwards needs to program
  971. * PA_VS_CORE_CLK_40NS_CYCLES attribute per programmed
  972. * frequency of unipro core clk of UFS host controller.
  973. */
  974. if (host->hw_ver.major < 4)
  975. return 0;
  976. /*
  977. * Generic formulae for cycles_in_40ns = (freq_unipro/25) is not
  978. * applicable for all frequencies. For ex: ceil(37.5 MHz/25) will
  979. * be 2 and ceil(403 MHZ/25) will be 17 whereas Hardware
  980. * specification expect to be 16. Hence use exact hardware spec
  981. * mandated value for cycles_in_40ns instead of calculating using
  982. * generic formulae.
  983. */
  984. switch (cycles_in_1us) {
  985. case UNIPRO_CORE_CLK_FREQ_403_MHZ:
  986. cycles_in_40ns = 16;
  987. break;
  988. case UNIPRO_CORE_CLK_FREQ_300_MHZ:
  989. cycles_in_40ns = 12;
  990. break;
  991. case UNIPRO_CORE_CLK_FREQ_201_5_MHZ:
  992. cycles_in_40ns = 8;
  993. break;
  994. case UNIPRO_CORE_CLK_FREQ_150_MHZ:
  995. cycles_in_40ns = 6;
  996. break;
  997. case UNIPRO_CORE_CLK_FREQ_100_MHZ:
  998. cycles_in_40ns = 4;
  999. break;
  1000. case UNIPRO_CORE_CLK_FREQ_75_MHZ:
  1001. cycles_in_40ns = 3;
  1002. break;
  1003. case UNIPRO_CORE_CLK_FREQ_37_5_MHZ:
  1004. cycles_in_40ns = 2;
  1005. break;
  1006. default:
  1007. dev_err(hba->dev, "UNIPRO clk freq %u MHz not supported\n",
  1008. cycles_in_1us);
  1009. return -EINVAL;
  1010. }
  1011. err = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_VS_CORE_CLK_40NS_CYCLES), &reg);
  1012. if (err)
  1013. return err;
  1014. reg &= ~PA_VS_CORE_CLK_40NS_CYCLES_MASK;
  1015. reg |= cycles_in_40ns;
  1016. return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_VS_CORE_CLK_40NS_CYCLES), reg);
  1017. }
  1018. static int ufs_qcom_set_core_clk_ctrl(struct ufs_hba *hba, bool is_scale_up)
  1019. {
  1020. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1021. struct list_head *head = &hba->clk_list_head;
  1022. struct ufs_clk_info *clki;
  1023. u32 cycles_in_1us = 0;
  1024. u32 core_clk_ctrl_reg;
  1025. int err;
  1026. list_for_each_entry(clki, head, list) {
  1027. if (!IS_ERR_OR_NULL(clki->clk) &&
  1028. !strcmp(clki->name, "core_clk_unipro")) {
  1029. if (!clki->max_freq)
  1030. cycles_in_1us = 150; /* default for backwards compatibility */
  1031. else if (is_scale_up)
  1032. cycles_in_1us = ceil(clki->max_freq, (1000 * 1000));
  1033. else
  1034. cycles_in_1us = ceil(clk_get_rate(clki->clk), (1000 * 1000));
  1035. break;
  1036. }
  1037. }
  1038. err = ufshcd_dme_get(hba,
  1039. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1040. &core_clk_ctrl_reg);
  1041. if (err)
  1042. return err;
  1043. /* Bit mask is different for UFS host controller V4.0.0 onwards */
  1044. if (host->hw_ver.major >= 4) {
  1045. if (!FIELD_FIT(CLK_1US_CYCLES_MASK_V4, cycles_in_1us))
  1046. return -ERANGE;
  1047. core_clk_ctrl_reg &= ~CLK_1US_CYCLES_MASK_V4;
  1048. core_clk_ctrl_reg |= FIELD_PREP(CLK_1US_CYCLES_MASK_V4, cycles_in_1us);
  1049. } else {
  1050. if (!FIELD_FIT(CLK_1US_CYCLES_MASK, cycles_in_1us))
  1051. return -ERANGE;
  1052. core_clk_ctrl_reg &= ~CLK_1US_CYCLES_MASK;
  1053. core_clk_ctrl_reg |= FIELD_PREP(CLK_1US_CYCLES_MASK, cycles_in_1us);
  1054. }
  1055. /* Clear CORE_CLK_DIV_EN */
  1056. core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
  1057. err = ufshcd_dme_set(hba,
  1058. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1059. core_clk_ctrl_reg);
  1060. if (err)
  1061. return err;
  1062. /* Configure unipro core clk 40ns attribute */
  1063. return ufs_qcom_set_clk_40ns_cycles(hba, cycles_in_1us);
  1064. }
  1065. static int ufs_qcom_clk_scale_up_pre_change(struct ufs_hba *hba)
  1066. {
  1067. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1068. struct ufs_pa_layer_attr *attr = &host->dev_req_params;
  1069. int ret;
  1070. ret = ufs_qcom_cfg_timers(hba, attr->gear_rx, attr->pwr_rx,
  1071. attr->hs_rate, false, true);
  1072. if (ret) {
  1073. dev_err(hba->dev, "%s ufs cfg timer failed\n", __func__);
  1074. return ret;
  1075. }
  1076. /* set unipro core clock attributes and clear clock divider */
  1077. return ufs_qcom_set_core_clk_ctrl(hba, true);
  1078. }
  1079. static int ufs_qcom_clk_scale_up_post_change(struct ufs_hba *hba)
  1080. {
  1081. return 0;
  1082. }
  1083. static int ufs_qcom_clk_scale_down_pre_change(struct ufs_hba *hba)
  1084. {
  1085. int err;
  1086. u32 core_clk_ctrl_reg;
  1087. err = ufshcd_dme_get(hba,
  1088. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1089. &core_clk_ctrl_reg);
  1090. /* make sure CORE_CLK_DIV_EN is cleared */
  1091. if (!err &&
  1092. (core_clk_ctrl_reg & DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT)) {
  1093. core_clk_ctrl_reg &= ~DME_VS_CORE_CLK_CTRL_CORE_CLK_DIV_EN_BIT;
  1094. err = ufshcd_dme_set(hba,
  1095. UIC_ARG_MIB(DME_VS_CORE_CLK_CTRL),
  1096. core_clk_ctrl_reg);
  1097. }
  1098. return err;
  1099. }
  1100. static int ufs_qcom_clk_scale_down_post_change(struct ufs_hba *hba)
  1101. {
  1102. /* set unipro core clock attributes and clear clock divider */
  1103. return ufs_qcom_set_core_clk_ctrl(hba, false);
  1104. }
  1105. static int ufs_qcom_clk_scale_notify(struct ufs_hba *hba,
  1106. bool scale_up, enum ufs_notify_change_status status)
  1107. {
  1108. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1109. int err;
  1110. /* check the host controller state before sending hibern8 cmd */
  1111. if (!ufshcd_is_hba_active(hba))
  1112. return 0;
  1113. if (status == PRE_CHANGE) {
  1114. err = ufshcd_uic_hibern8_enter(hba);
  1115. if (err)
  1116. return err;
  1117. if (scale_up)
  1118. err = ufs_qcom_clk_scale_up_pre_change(hba);
  1119. else
  1120. err = ufs_qcom_clk_scale_down_pre_change(hba);
  1121. if (err) {
  1122. ufshcd_uic_hibern8_exit(hba);
  1123. return err;
  1124. }
  1125. } else {
  1126. if (scale_up)
  1127. err = ufs_qcom_clk_scale_up_post_change(hba);
  1128. else
  1129. err = ufs_qcom_clk_scale_down_post_change(hba);
  1130. if (err) {
  1131. ufshcd_uic_hibern8_exit(hba);
  1132. return err;
  1133. }
  1134. ufs_qcom_icc_update_bw(host);
  1135. ufshcd_uic_hibern8_exit(hba);
  1136. }
  1137. return 0;
  1138. }
  1139. static void ufs_qcom_enable_test_bus(struct ufs_qcom_host *host)
  1140. {
  1141. ufshcd_rmwl(host->hba, UFS_REG_TEST_BUS_EN,
  1142. UFS_REG_TEST_BUS_EN, REG_UFS_CFG1);
  1143. ufshcd_rmwl(host->hba, TEST_BUS_EN, TEST_BUS_EN, REG_UFS_CFG1);
  1144. }
  1145. static void ufs_qcom_get_default_testbus_cfg(struct ufs_qcom_host *host)
  1146. {
  1147. /* provide a legal default configuration */
  1148. host->testbus.select_major = TSTBUS_UNIPRO;
  1149. host->testbus.select_minor = 37;
  1150. }
  1151. static bool ufs_qcom_testbus_cfg_is_ok(struct ufs_qcom_host *host)
  1152. {
  1153. if (host->testbus.select_major >= TSTBUS_MAX) {
  1154. dev_err(host->hba->dev,
  1155. "%s: UFS_CFG1[TEST_BUS_SEL} may not equal 0x%05X\n",
  1156. __func__, host->testbus.select_major);
  1157. return false;
  1158. }
  1159. return true;
  1160. }
  1161. int ufs_qcom_testbus_config(struct ufs_qcom_host *host)
  1162. {
  1163. int reg;
  1164. int offset;
  1165. u32 mask = TEST_BUS_SUB_SEL_MASK;
  1166. if (!host)
  1167. return -EINVAL;
  1168. if (!ufs_qcom_testbus_cfg_is_ok(host))
  1169. return -EPERM;
  1170. switch (host->testbus.select_major) {
  1171. case TSTBUS_UAWM:
  1172. reg = UFS_TEST_BUS_CTRL_0;
  1173. offset = 24;
  1174. break;
  1175. case TSTBUS_UARM:
  1176. reg = UFS_TEST_BUS_CTRL_0;
  1177. offset = 16;
  1178. break;
  1179. case TSTBUS_TXUC:
  1180. reg = UFS_TEST_BUS_CTRL_0;
  1181. offset = 8;
  1182. break;
  1183. case TSTBUS_RXUC:
  1184. reg = UFS_TEST_BUS_CTRL_0;
  1185. offset = 0;
  1186. break;
  1187. case TSTBUS_DFC:
  1188. reg = UFS_TEST_BUS_CTRL_1;
  1189. offset = 24;
  1190. break;
  1191. case TSTBUS_TRLUT:
  1192. reg = UFS_TEST_BUS_CTRL_1;
  1193. offset = 16;
  1194. break;
  1195. case TSTBUS_TMRLUT:
  1196. reg = UFS_TEST_BUS_CTRL_1;
  1197. offset = 8;
  1198. break;
  1199. case TSTBUS_OCSC:
  1200. reg = UFS_TEST_BUS_CTRL_1;
  1201. offset = 0;
  1202. break;
  1203. case TSTBUS_WRAPPER:
  1204. reg = UFS_TEST_BUS_CTRL_2;
  1205. offset = 16;
  1206. break;
  1207. case TSTBUS_COMBINED:
  1208. reg = UFS_TEST_BUS_CTRL_2;
  1209. offset = 8;
  1210. break;
  1211. case TSTBUS_UTP_HCI:
  1212. reg = UFS_TEST_BUS_CTRL_2;
  1213. offset = 0;
  1214. break;
  1215. case TSTBUS_UNIPRO:
  1216. reg = UFS_UNIPRO_CFG;
  1217. offset = 20;
  1218. mask = 0xFFF;
  1219. break;
  1220. /*
  1221. * No need for a default case, since
  1222. * ufs_qcom_testbus_cfg_is_ok() checks that the configuration
  1223. * is legal
  1224. */
  1225. }
  1226. mask <<= offset;
  1227. ufshcd_rmwl(host->hba, TEST_BUS_SEL,
  1228. (u32)host->testbus.select_major << 19,
  1229. REG_UFS_CFG1);
  1230. ufshcd_rmwl(host->hba, mask,
  1231. (u32)host->testbus.select_minor << offset,
  1232. reg);
  1233. ufs_qcom_enable_test_bus(host);
  1234. return 0;
  1235. }
  1236. static void ufs_qcom_dump_dbg_regs(struct ufs_hba *hba)
  1237. {
  1238. u32 reg;
  1239. struct ufs_qcom_host *host;
  1240. host = ufshcd_get_variant(hba);
  1241. ufshcd_dump_regs(hba, REG_UFS_SYS1CLK_1US, 16 * 4,
  1242. "HCI Vendor Specific Registers ");
  1243. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_REG_OCSC);
  1244. ufshcd_dump_regs(hba, reg, 44 * 4, "UFS_UFS_DBG_RD_REG_OCSC ");
  1245. reg = ufshcd_readl(hba, REG_UFS_CFG1);
  1246. reg |= UTP_DBG_RAMS_EN;
  1247. ufshcd_writel(hba, reg, REG_UFS_CFG1);
  1248. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_EDTL_RAM);
  1249. ufshcd_dump_regs(hba, reg, 32 * 4, "UFS_UFS_DBG_RD_EDTL_RAM ");
  1250. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_DESC_RAM);
  1251. ufshcd_dump_regs(hba, reg, 128 * 4, "UFS_UFS_DBG_RD_DESC_RAM ");
  1252. reg = ufs_qcom_get_debug_reg_offset(host, UFS_UFS_DBG_RD_PRDT_RAM);
  1253. ufshcd_dump_regs(hba, reg, 64 * 4, "UFS_UFS_DBG_RD_PRDT_RAM ");
  1254. /* clear bit 17 - UTP_DBG_RAMS_EN */
  1255. ufshcd_rmwl(hba, UTP_DBG_RAMS_EN, 0, REG_UFS_CFG1);
  1256. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UAWM);
  1257. ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UAWM ");
  1258. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_UARM);
  1259. ufshcd_dump_regs(hba, reg, 4 * 4, "UFS_DBG_RD_REG_UARM ");
  1260. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TXUC);
  1261. ufshcd_dump_regs(hba, reg, 48 * 4, "UFS_DBG_RD_REG_TXUC ");
  1262. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_RXUC);
  1263. ufshcd_dump_regs(hba, reg, 27 * 4, "UFS_DBG_RD_REG_RXUC ");
  1264. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_DFC);
  1265. ufshcd_dump_regs(hba, reg, 19 * 4, "UFS_DBG_RD_REG_DFC ");
  1266. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TRLUT);
  1267. ufshcd_dump_regs(hba, reg, 34 * 4, "UFS_DBG_RD_REG_TRLUT ");
  1268. reg = ufs_qcom_get_debug_reg_offset(host, UFS_DBG_RD_REG_TMRLUT);
  1269. ufshcd_dump_regs(hba, reg, 9 * 4, "UFS_DBG_RD_REG_TMRLUT ");
  1270. }
  1271. /**
  1272. * ufs_qcom_device_reset() - toggle the (optional) device reset line
  1273. * @hba: per-adapter instance
  1274. *
  1275. * Toggles the (optional) reset line to reset the attached device.
  1276. */
  1277. static int ufs_qcom_device_reset(struct ufs_hba *hba)
  1278. {
  1279. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1280. /* reset gpio is optional */
  1281. if (!host->device_reset)
  1282. return -EOPNOTSUPP;
  1283. /*
  1284. * The UFS device shall detect reset pulses of 1us, sleep for 10us to
  1285. * be on the safe side.
  1286. */
  1287. ufs_qcom_device_reset_ctrl(hba, true);
  1288. usleep_range(10, 15);
  1289. ufs_qcom_device_reset_ctrl(hba, false);
  1290. usleep_range(10, 15);
  1291. return 0;
  1292. }
  1293. #if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
  1294. static void ufs_qcom_config_scaling_param(struct ufs_hba *hba,
  1295. struct devfreq_dev_profile *p,
  1296. struct devfreq_simple_ondemand_data *d)
  1297. {
  1298. p->polling_ms = 60;
  1299. p->timer = DEVFREQ_TIMER_DELAYED;
  1300. d->upthreshold = 70;
  1301. d->downdifferential = 5;
  1302. hba->clk_scaling.suspend_on_no_request = true;
  1303. }
  1304. #else
  1305. static void ufs_qcom_config_scaling_param(struct ufs_hba *hba,
  1306. struct devfreq_dev_profile *p,
  1307. struct devfreq_simple_ondemand_data *data)
  1308. {
  1309. }
  1310. #endif
  1311. /* Resources */
  1312. static const struct ufshcd_res_info ufs_res_info[RES_MAX] = {
  1313. {.name = "ufs_mem",},
  1314. {.name = "mcq",},
  1315. /* Submission Queue DAO */
  1316. {.name = "mcq_sqd",},
  1317. /* Submission Queue Interrupt Status */
  1318. {.name = "mcq_sqis",},
  1319. /* Completion Queue DAO */
  1320. {.name = "mcq_cqd",},
  1321. /* Completion Queue Interrupt Status */
  1322. {.name = "mcq_cqis",},
  1323. /* MCQ vendor specific */
  1324. {.name = "mcq_vs",},
  1325. };
  1326. static int ufs_qcom_mcq_config_resource(struct ufs_hba *hba)
  1327. {
  1328. struct platform_device *pdev = to_platform_device(hba->dev);
  1329. struct ufshcd_res_info *res;
  1330. struct resource *res_mem, *res_mcq;
  1331. int i, ret;
  1332. memcpy(hba->res, ufs_res_info, sizeof(ufs_res_info));
  1333. for (i = 0; i < RES_MAX; i++) {
  1334. res = &hba->res[i];
  1335. res->resource = platform_get_resource_byname(pdev,
  1336. IORESOURCE_MEM,
  1337. res->name);
  1338. if (!res->resource) {
  1339. dev_info(hba->dev, "Resource %s not provided\n", res->name);
  1340. if (i == RES_UFS)
  1341. return -ENODEV;
  1342. continue;
  1343. } else if (i == RES_UFS) {
  1344. res_mem = res->resource;
  1345. res->base = hba->mmio_base;
  1346. continue;
  1347. }
  1348. res->base = devm_ioremap_resource(hba->dev, res->resource);
  1349. if (IS_ERR(res->base)) {
  1350. dev_err(hba->dev, "Failed to map res %s, err=%d\n",
  1351. res->name, (int)PTR_ERR(res->base));
  1352. ret = PTR_ERR(res->base);
  1353. res->base = NULL;
  1354. return ret;
  1355. }
  1356. }
  1357. /* MCQ resource provided in DT */
  1358. res = &hba->res[RES_MCQ];
  1359. /* Bail if MCQ resource is provided */
  1360. if (res->base)
  1361. goto out;
  1362. /* Explicitly allocate MCQ resource from ufs_mem */
  1363. res_mcq = devm_kzalloc(hba->dev, sizeof(*res_mcq), GFP_KERNEL);
  1364. if (!res_mcq)
  1365. return -ENOMEM;
  1366. res_mcq->start = res_mem->start +
  1367. MCQ_SQATTR_OFFSET(hba->mcq_capabilities);
  1368. res_mcq->end = res_mcq->start + hba->nr_hw_queues * MCQ_QCFG_SIZE - 1;
  1369. res_mcq->flags = res_mem->flags;
  1370. res_mcq->name = "mcq";
  1371. ret = insert_resource(&iomem_resource, res_mcq);
  1372. if (ret) {
  1373. dev_err(hba->dev, "Failed to insert MCQ resource, err=%d\n",
  1374. ret);
  1375. return ret;
  1376. }
  1377. res->base = devm_ioremap_resource(hba->dev, res_mcq);
  1378. if (IS_ERR(res->base)) {
  1379. dev_err(hba->dev, "MCQ registers mapping failed, err=%d\n",
  1380. (int)PTR_ERR(res->base));
  1381. ret = PTR_ERR(res->base);
  1382. goto ioremap_err;
  1383. }
  1384. out:
  1385. hba->mcq_base = res->base;
  1386. return 0;
  1387. ioremap_err:
  1388. res->base = NULL;
  1389. remove_resource(res_mcq);
  1390. return ret;
  1391. }
  1392. static int ufs_qcom_op_runtime_config(struct ufs_hba *hba)
  1393. {
  1394. struct ufshcd_res_info *mem_res, *sqdao_res;
  1395. struct ufshcd_mcq_opr_info_t *opr;
  1396. int i;
  1397. mem_res = &hba->res[RES_UFS];
  1398. sqdao_res = &hba->res[RES_MCQ_SQD];
  1399. if (!mem_res->base || !sqdao_res->base)
  1400. return -EINVAL;
  1401. for (i = 0; i < OPR_MAX; i++) {
  1402. opr = &hba->mcq_opr[i];
  1403. opr->offset = sqdao_res->resource->start -
  1404. mem_res->resource->start + 0x40 * i;
  1405. opr->stride = 0x100;
  1406. opr->base = sqdao_res->base + 0x40 * i;
  1407. }
  1408. return 0;
  1409. }
  1410. static int ufs_qcom_get_hba_mac(struct ufs_hba *hba)
  1411. {
  1412. /* Qualcomm HC supports up to 64 */
  1413. return MAX_SUPP_MAC;
  1414. }
  1415. static int ufs_qcom_get_outstanding_cqs(struct ufs_hba *hba,
  1416. unsigned long *ocqs)
  1417. {
  1418. struct ufshcd_res_info *mcq_vs_res = &hba->res[RES_MCQ_VS];
  1419. if (!mcq_vs_res->base)
  1420. return -EINVAL;
  1421. *ocqs = readl(mcq_vs_res->base + UFS_MEM_CQIS_VS);
  1422. return 0;
  1423. }
  1424. static void ufs_qcom_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
  1425. {
  1426. struct device *dev = msi_desc_to_dev(desc);
  1427. struct ufs_hba *hba = dev_get_drvdata(dev);
  1428. ufshcd_mcq_config_esi(hba, msg);
  1429. }
  1430. static irqreturn_t ufs_qcom_mcq_esi_handler(int irq, void *data)
  1431. {
  1432. struct msi_desc *desc = data;
  1433. struct device *dev = msi_desc_to_dev(desc);
  1434. struct ufs_hba *hba = dev_get_drvdata(dev);
  1435. u32 id = desc->msi_index;
  1436. struct ufs_hw_queue *hwq = &hba->uhq[id];
  1437. ufshcd_mcq_write_cqis(hba, 0x1, id);
  1438. ufshcd_mcq_poll_cqe_lock(hba, hwq);
  1439. return IRQ_HANDLED;
  1440. }
  1441. static int ufs_qcom_config_esi(struct ufs_hba *hba)
  1442. {
  1443. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1444. struct msi_desc *desc;
  1445. struct msi_desc *failed_desc = NULL;
  1446. int nr_irqs, ret;
  1447. if (host->esi_enabled)
  1448. return 0;
  1449. /*
  1450. * 1. We only handle CQs as of now.
  1451. * 2. Poll queues do not need ESI.
  1452. */
  1453. nr_irqs = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL];
  1454. ret = platform_device_msi_init_and_alloc_irqs(hba->dev, nr_irqs,
  1455. ufs_qcom_write_msi_msg);
  1456. if (ret) {
  1457. dev_err(hba->dev, "Failed to request Platform MSI %d\n", ret);
  1458. return ret;
  1459. }
  1460. msi_lock_descs(hba->dev);
  1461. msi_for_each_desc(desc, hba->dev, MSI_DESC_ALL) {
  1462. ret = devm_request_irq(hba->dev, desc->irq,
  1463. ufs_qcom_mcq_esi_handler,
  1464. IRQF_SHARED, "qcom-mcq-esi", desc);
  1465. if (ret) {
  1466. dev_err(hba->dev, "%s: Fail to request IRQ for %d, err = %d\n",
  1467. __func__, desc->irq, ret);
  1468. failed_desc = desc;
  1469. break;
  1470. }
  1471. }
  1472. msi_unlock_descs(hba->dev);
  1473. if (ret) {
  1474. /* Rewind */
  1475. msi_lock_descs(hba->dev);
  1476. msi_for_each_desc(desc, hba->dev, MSI_DESC_ALL) {
  1477. if (desc == failed_desc)
  1478. break;
  1479. devm_free_irq(hba->dev, desc->irq, hba);
  1480. }
  1481. msi_unlock_descs(hba->dev);
  1482. platform_device_msi_free_irqs_all(hba->dev);
  1483. } else {
  1484. if (host->hw_ver.major == 6 && host->hw_ver.minor == 0 &&
  1485. host->hw_ver.step == 0)
  1486. ufshcd_rmwl(hba, ESI_VEC_MASK,
  1487. FIELD_PREP(ESI_VEC_MASK, MAX_ESI_VEC - 1),
  1488. REG_UFS_CFG3);
  1489. ufshcd_mcq_enable_esi(hba);
  1490. host->esi_enabled = true;
  1491. }
  1492. return ret;
  1493. }
  1494. /*
  1495. * struct ufs_hba_qcom_vops - UFS QCOM specific variant operations
  1496. *
  1497. * The variant operations configure the necessary controller and PHY
  1498. * handshake during initialization.
  1499. */
  1500. static const struct ufs_hba_variant_ops ufs_hba_qcom_vops = {
  1501. .name = "qcom",
  1502. .init = ufs_qcom_init,
  1503. .exit = ufs_qcom_exit,
  1504. .get_ufs_hci_version = ufs_qcom_get_ufs_hci_version,
  1505. .clk_scale_notify = ufs_qcom_clk_scale_notify,
  1506. .setup_clocks = ufs_qcom_setup_clocks,
  1507. .hce_enable_notify = ufs_qcom_hce_enable_notify,
  1508. .link_startup_notify = ufs_qcom_link_startup_notify,
  1509. .pwr_change_notify = ufs_qcom_pwr_change_notify,
  1510. .apply_dev_quirks = ufs_qcom_apply_dev_quirks,
  1511. .suspend = ufs_qcom_suspend,
  1512. .resume = ufs_qcom_resume,
  1513. .dbg_register_dump = ufs_qcom_dump_dbg_regs,
  1514. .device_reset = ufs_qcom_device_reset,
  1515. .config_scaling_param = ufs_qcom_config_scaling_param,
  1516. .program_key = ufs_qcom_ice_program_key,
  1517. .mcq_config_resource = ufs_qcom_mcq_config_resource,
  1518. .get_hba_mac = ufs_qcom_get_hba_mac,
  1519. .op_runtime_config = ufs_qcom_op_runtime_config,
  1520. .get_outstanding_cqs = ufs_qcom_get_outstanding_cqs,
  1521. .config_esi = ufs_qcom_config_esi,
  1522. };
  1523. /**
  1524. * ufs_qcom_probe - probe routine of the driver
  1525. * @pdev: pointer to Platform device handle
  1526. *
  1527. * Return: zero for success and non-zero for failure.
  1528. */
  1529. static int ufs_qcom_probe(struct platform_device *pdev)
  1530. {
  1531. int err;
  1532. struct device *dev = &pdev->dev;
  1533. /* Perform generic probe */
  1534. err = ufshcd_pltfrm_init(pdev, &ufs_hba_qcom_vops);
  1535. if (err)
  1536. return dev_err_probe(dev, err, "ufshcd_pltfrm_init() failed\n");
  1537. return 0;
  1538. }
  1539. /**
  1540. * ufs_qcom_remove - set driver_data of the device to NULL
  1541. * @pdev: pointer to platform device handle
  1542. *
  1543. * Always returns 0
  1544. */
  1545. static void ufs_qcom_remove(struct platform_device *pdev)
  1546. {
  1547. struct ufs_hba *hba = platform_get_drvdata(pdev);
  1548. struct ufs_qcom_host *host = ufshcd_get_variant(hba);
  1549. ufshcd_pltfrm_remove(pdev);
  1550. if (host->esi_enabled)
  1551. platform_device_msi_free_irqs_all(hba->dev);
  1552. }
  1553. static const struct of_device_id ufs_qcom_of_match[] __maybe_unused = {
  1554. { .compatible = "qcom,ufshc" },
  1555. { .compatible = "qcom,sm8550-ufshc" },
  1556. {},
  1557. };
  1558. MODULE_DEVICE_TABLE(of, ufs_qcom_of_match);
  1559. #ifdef CONFIG_ACPI
  1560. static const struct acpi_device_id ufs_qcom_acpi_match[] = {
  1561. { "QCOM24A5" },
  1562. { },
  1563. };
  1564. MODULE_DEVICE_TABLE(acpi, ufs_qcom_acpi_match);
  1565. #endif
  1566. static const struct dev_pm_ops ufs_qcom_pm_ops = {
  1567. SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
  1568. .prepare = ufshcd_suspend_prepare,
  1569. .complete = ufshcd_resume_complete,
  1570. #ifdef CONFIG_PM_SLEEP
  1571. .suspend = ufshcd_system_suspend,
  1572. .resume = ufshcd_system_resume,
  1573. .freeze = ufshcd_system_freeze,
  1574. .restore = ufshcd_system_restore,
  1575. .thaw = ufshcd_system_thaw,
  1576. #endif
  1577. };
  1578. static struct platform_driver ufs_qcom_pltform = {
  1579. .probe = ufs_qcom_probe,
  1580. .remove_new = ufs_qcom_remove,
  1581. .driver = {
  1582. .name = "ufshcd-qcom",
  1583. .pm = &ufs_qcom_pm_ops,
  1584. .of_match_table = of_match_ptr(ufs_qcom_of_match),
  1585. .acpi_match_table = ACPI_PTR(ufs_qcom_acpi_match),
  1586. },
  1587. };
  1588. module_platform_driver(ufs_qcom_pltform);
  1589. MODULE_DESCRIPTION("Qualcomm UFS host controller driver");
  1590. MODULE_LICENSE("GPL v2");