cdns-pltfrm.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Platform UFS Host driver for Cadence controller
  4. *
  5. * Copyright (C) 2018 Cadence Design Systems, Inc.
  6. *
  7. * Authors:
  8. * Jan Kotas <jank@cadence.com>
  9. *
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/of.h>
  16. #include <linux/time.h>
  17. #include "ufshcd-pltfrm.h"
  18. #define CDNS_UFS_REG_HCLKDIV 0xFC
  19. #define CDNS_UFS_REG_PHY_XCFGD1 0x113C
  20. #define CDNS_UFS_MAX_L4_ATTRS 12
  21. struct cdns_ufs_host {
  22. /**
  23. * cdns_ufs_dme_attr_val - for storing L4 attributes
  24. */
  25. u32 cdns_ufs_dme_attr_val[CDNS_UFS_MAX_L4_ATTRS];
  26. };
  27. /**
  28. * cdns_ufs_get_l4_attr - get L4 attributes on local side
  29. * @hba: per adapter instance
  30. *
  31. */
  32. static void cdns_ufs_get_l4_attr(struct ufs_hba *hba)
  33. {
  34. struct cdns_ufs_host *host = ufshcd_get_variant(hba);
  35. ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERDEVICEID),
  36. &host->cdns_ufs_dme_attr_val[0]);
  37. ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERCPORTID),
  38. &host->cdns_ufs_dme_attr_val[1]);
  39. ufshcd_dme_get(hba, UIC_ARG_MIB(T_TRAFFICCLASS),
  40. &host->cdns_ufs_dme_attr_val[2]);
  41. ufshcd_dme_get(hba, UIC_ARG_MIB(T_PROTOCOLID),
  42. &host->cdns_ufs_dme_attr_val[3]);
  43. ufshcd_dme_get(hba, UIC_ARG_MIB(T_CPORTFLAGS),
  44. &host->cdns_ufs_dme_attr_val[4]);
  45. ufshcd_dme_get(hba, UIC_ARG_MIB(T_TXTOKENVALUE),
  46. &host->cdns_ufs_dme_attr_val[5]);
  47. ufshcd_dme_get(hba, UIC_ARG_MIB(T_RXTOKENVALUE),
  48. &host->cdns_ufs_dme_attr_val[6]);
  49. ufshcd_dme_get(hba, UIC_ARG_MIB(T_LOCALBUFFERSPACE),
  50. &host->cdns_ufs_dme_attr_val[7]);
  51. ufshcd_dme_get(hba, UIC_ARG_MIB(T_PEERBUFFERSPACE),
  52. &host->cdns_ufs_dme_attr_val[8]);
  53. ufshcd_dme_get(hba, UIC_ARG_MIB(T_CREDITSTOSEND),
  54. &host->cdns_ufs_dme_attr_val[9]);
  55. ufshcd_dme_get(hba, UIC_ARG_MIB(T_CPORTMODE),
  56. &host->cdns_ufs_dme_attr_val[10]);
  57. ufshcd_dme_get(hba, UIC_ARG_MIB(T_CONNECTIONSTATE),
  58. &host->cdns_ufs_dme_attr_val[11]);
  59. }
  60. /**
  61. * cdns_ufs_set_l4_attr - set L4 attributes on local side
  62. * @hba: per adapter instance
  63. *
  64. */
  65. static void cdns_ufs_set_l4_attr(struct ufs_hba *hba)
  66. {
  67. struct cdns_ufs_host *host = ufshcd_get_variant(hba);
  68. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE), 0);
  69. ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERDEVICEID),
  70. host->cdns_ufs_dme_attr_val[0]);
  71. ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERCPORTID),
  72. host->cdns_ufs_dme_attr_val[1]);
  73. ufshcd_dme_set(hba, UIC_ARG_MIB(T_TRAFFICCLASS),
  74. host->cdns_ufs_dme_attr_val[2]);
  75. ufshcd_dme_set(hba, UIC_ARG_MIB(T_PROTOCOLID),
  76. host->cdns_ufs_dme_attr_val[3]);
  77. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CPORTFLAGS),
  78. host->cdns_ufs_dme_attr_val[4]);
  79. ufshcd_dme_set(hba, UIC_ARG_MIB(T_TXTOKENVALUE),
  80. host->cdns_ufs_dme_attr_val[5]);
  81. ufshcd_dme_set(hba, UIC_ARG_MIB(T_RXTOKENVALUE),
  82. host->cdns_ufs_dme_attr_val[6]);
  83. ufshcd_dme_set(hba, UIC_ARG_MIB(T_LOCALBUFFERSPACE),
  84. host->cdns_ufs_dme_attr_val[7]);
  85. ufshcd_dme_set(hba, UIC_ARG_MIB(T_PEERBUFFERSPACE),
  86. host->cdns_ufs_dme_attr_val[8]);
  87. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CREDITSTOSEND),
  88. host->cdns_ufs_dme_attr_val[9]);
  89. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CPORTMODE),
  90. host->cdns_ufs_dme_attr_val[10]);
  91. ufshcd_dme_set(hba, UIC_ARG_MIB(T_CONNECTIONSTATE),
  92. host->cdns_ufs_dme_attr_val[11]);
  93. }
  94. /**
  95. * cdns_ufs_set_hclkdiv() - set HCLKDIV register value based on the core_clk.
  96. * @hba: host controller instance
  97. *
  98. * Return: zero for success and non-zero for failure.
  99. */
  100. static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
  101. {
  102. struct ufs_clk_info *clki;
  103. struct list_head *head = &hba->clk_list_head;
  104. unsigned long core_clk_rate = 0;
  105. u32 core_clk_div = 0;
  106. if (list_empty(head))
  107. return 0;
  108. list_for_each_entry(clki, head, list) {
  109. if (IS_ERR_OR_NULL(clki->clk))
  110. continue;
  111. if (!strcmp(clki->name, "core_clk"))
  112. core_clk_rate = clk_get_rate(clki->clk);
  113. }
  114. if (!core_clk_rate) {
  115. dev_err(hba->dev, "%s: unable to find core_clk rate\n",
  116. __func__);
  117. return -EINVAL;
  118. }
  119. core_clk_div = core_clk_rate / USEC_PER_SEC;
  120. ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
  121. /**
  122. * Make sure the register was updated,
  123. * UniPro layer will not work with an incorrect value.
  124. */
  125. ufshcd_readl(hba, CDNS_UFS_REG_HCLKDIV);
  126. return 0;
  127. }
  128. /**
  129. * cdns_ufs_hce_enable_notify() - set HCLKDIV register
  130. * @hba: host controller instance
  131. * @status: notify stage (pre, post change)
  132. *
  133. * Return: zero for success and non-zero for failure.
  134. */
  135. static int cdns_ufs_hce_enable_notify(struct ufs_hba *hba,
  136. enum ufs_notify_change_status status)
  137. {
  138. if (status != PRE_CHANGE)
  139. return 0;
  140. return cdns_ufs_set_hclkdiv(hba);
  141. }
  142. /**
  143. * cdns_ufs_hibern8_notify() - save and restore L4 attributes.
  144. * @hba: host controller instance
  145. * @cmd: UIC Command
  146. * @status: notify stage (pre, post change)
  147. */
  148. static void cdns_ufs_hibern8_notify(struct ufs_hba *hba, enum uic_cmd_dme cmd,
  149. enum ufs_notify_change_status status)
  150. {
  151. if (status == PRE_CHANGE && cmd == UIC_CMD_DME_HIBER_ENTER)
  152. cdns_ufs_get_l4_attr(hba);
  153. if (status == POST_CHANGE && cmd == UIC_CMD_DME_HIBER_EXIT)
  154. cdns_ufs_set_l4_attr(hba);
  155. }
  156. /**
  157. * cdns_ufs_link_startup_notify() - handle link startup.
  158. * @hba: host controller instance
  159. * @status: notify stage (pre, post change)
  160. *
  161. * Return: zero for success and non-zero for failure.
  162. */
  163. static int cdns_ufs_link_startup_notify(struct ufs_hba *hba,
  164. enum ufs_notify_change_status status)
  165. {
  166. if (status != PRE_CHANGE)
  167. return 0;
  168. /*
  169. * Some UFS devices have issues if LCC is enabled.
  170. * So we are setting PA_Local_TX_LCC_Enable to 0
  171. * before link startup which will make sure that both host
  172. * and device TX LCC are disabled once link startup is
  173. * completed.
  174. */
  175. ufshcd_disable_host_tx_lcc(hba);
  176. /*
  177. * Disabling Autohibern8 feature in cadence UFS
  178. * to mask unexpected interrupt trigger.
  179. */
  180. hba->ahit = 0;
  181. return 0;
  182. }
  183. /**
  184. * cdns_ufs_init - performs additional ufs initialization
  185. * @hba: host controller instance
  186. *
  187. * Return: status of initialization.
  188. */
  189. static int cdns_ufs_init(struct ufs_hba *hba)
  190. {
  191. int status = 0;
  192. struct cdns_ufs_host *host;
  193. struct device *dev = hba->dev;
  194. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  195. if (!host)
  196. return -ENOMEM;
  197. ufshcd_set_variant(hba, host);
  198. status = ufshcd_vops_phy_initialization(hba);
  199. return status;
  200. }
  201. /**
  202. * cdns_ufs_m31_16nm_phy_initialization - performs m31 phy initialization
  203. * @hba: host controller instance
  204. *
  205. * Return: 0 (success).
  206. */
  207. static int cdns_ufs_m31_16nm_phy_initialization(struct ufs_hba *hba)
  208. {
  209. u32 data;
  210. /* Increase RX_Advanced_Min_ActivateTime_Capability */
  211. data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
  212. data |= BIT(24);
  213. ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
  214. return 0;
  215. }
  216. static const struct ufs_hba_variant_ops cdns_ufs_pltfm_hba_vops = {
  217. .name = "cdns-ufs-pltfm",
  218. .init = cdns_ufs_init,
  219. .hce_enable_notify = cdns_ufs_hce_enable_notify,
  220. .link_startup_notify = cdns_ufs_link_startup_notify,
  221. .hibern8_notify = cdns_ufs_hibern8_notify,
  222. };
  223. static const struct ufs_hba_variant_ops cdns_ufs_m31_16nm_pltfm_hba_vops = {
  224. .name = "cdns-ufs-pltfm",
  225. .init = cdns_ufs_init,
  226. .hce_enable_notify = cdns_ufs_hce_enable_notify,
  227. .link_startup_notify = cdns_ufs_link_startup_notify,
  228. .phy_initialization = cdns_ufs_m31_16nm_phy_initialization,
  229. .hibern8_notify = cdns_ufs_hibern8_notify,
  230. };
  231. static const struct of_device_id cdns_ufs_of_match[] = {
  232. {
  233. .compatible = "cdns,ufshc",
  234. .data = &cdns_ufs_pltfm_hba_vops,
  235. },
  236. {
  237. .compatible = "cdns,ufshc-m31-16nm",
  238. .data = &cdns_ufs_m31_16nm_pltfm_hba_vops,
  239. },
  240. { },
  241. };
  242. MODULE_DEVICE_TABLE(of, cdns_ufs_of_match);
  243. /**
  244. * cdns_ufs_pltfrm_probe - probe routine of the driver
  245. * @pdev: pointer to platform device handle
  246. *
  247. * Return: zero for success and non-zero for failure.
  248. */
  249. static int cdns_ufs_pltfrm_probe(struct platform_device *pdev)
  250. {
  251. int err;
  252. const struct of_device_id *of_id;
  253. struct ufs_hba_variant_ops *vops;
  254. struct device *dev = &pdev->dev;
  255. of_id = of_match_node(cdns_ufs_of_match, dev->of_node);
  256. vops = (struct ufs_hba_variant_ops *)of_id->data;
  257. /* Perform generic probe */
  258. err = ufshcd_pltfrm_init(pdev, vops);
  259. if (err)
  260. dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
  261. return err;
  262. }
  263. /**
  264. * cdns_ufs_pltfrm_remove - removes the ufs driver
  265. * @pdev: pointer to platform device handle
  266. *
  267. * Return: 0 (success).
  268. */
  269. static void cdns_ufs_pltfrm_remove(struct platform_device *pdev)
  270. {
  271. ufshcd_pltfrm_remove(pdev);
  272. }
  273. static const struct dev_pm_ops cdns_ufs_dev_pm_ops = {
  274. SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
  275. SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
  276. .prepare = ufshcd_suspend_prepare,
  277. .complete = ufshcd_resume_complete,
  278. };
  279. static struct platform_driver cdns_ufs_pltfrm_driver = {
  280. .probe = cdns_ufs_pltfrm_probe,
  281. .remove = cdns_ufs_pltfrm_remove,
  282. .driver = {
  283. .name = "cdns-ufshcd",
  284. .pm = &cdns_ufs_dev_pm_ops,
  285. .of_match_table = cdns_ufs_of_match,
  286. },
  287. };
  288. module_platform_driver(cdns_ufs_pltfrm_driver);
  289. MODULE_AUTHOR("Jan Kotas <jank@cadence.com>");
  290. MODULE_DESCRIPTION("Cadence UFS host controller platform driver");
  291. MODULE_LICENSE("GPL v2");