cdns-pltfrm.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <[email protected]>
  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()
  96. * Sets HCLKDIV register value based on the core_clk
  97. * @hba: host controller instance
  98. *
  99. * Return zero for success and non-zero for failure
  100. */
  101. static int cdns_ufs_set_hclkdiv(struct ufs_hba *hba)
  102. {
  103. struct ufs_clk_info *clki;
  104. struct list_head *head = &hba->clk_list_head;
  105. unsigned long core_clk_rate = 0;
  106. u32 core_clk_div = 0;
  107. if (list_empty(head))
  108. return 0;
  109. list_for_each_entry(clki, head, list) {
  110. if (IS_ERR_OR_NULL(clki->clk))
  111. continue;
  112. if (!strcmp(clki->name, "core_clk"))
  113. core_clk_rate = clk_get_rate(clki->clk);
  114. }
  115. if (!core_clk_rate) {
  116. dev_err(hba->dev, "%s: unable to find core_clk rate\n",
  117. __func__);
  118. return -EINVAL;
  119. }
  120. core_clk_div = core_clk_rate / USEC_PER_SEC;
  121. ufshcd_writel(hba, core_clk_div, CDNS_UFS_REG_HCLKDIV);
  122. /**
  123. * Make sure the register was updated,
  124. * UniPro layer will not work with an incorrect value.
  125. */
  126. mb();
  127. return 0;
  128. }
  129. /**
  130. * cdns_ufs_hce_enable_notify()
  131. * Called before and after HCE enable bit is set.
  132. * @hba: host controller instance
  133. * @status: notify stage (pre, post change)
  134. *
  135. * Return zero for success and non-zero for failure
  136. */
  137. static int cdns_ufs_hce_enable_notify(struct ufs_hba *hba,
  138. enum ufs_notify_change_status status)
  139. {
  140. if (status != PRE_CHANGE)
  141. return 0;
  142. return cdns_ufs_set_hclkdiv(hba);
  143. }
  144. /**
  145. * cdns_ufs_hibern8_notify()
  146. * Called around hibern8 enter/exit.
  147. * @hba: host controller instance
  148. * @cmd: UIC Command
  149. * @status: notify stage (pre, post change)
  150. *
  151. */
  152. static void cdns_ufs_hibern8_notify(struct ufs_hba *hba, enum uic_cmd_dme cmd,
  153. enum ufs_notify_change_status status)
  154. {
  155. if (status == PRE_CHANGE && cmd == UIC_CMD_DME_HIBER_ENTER)
  156. cdns_ufs_get_l4_attr(hba);
  157. if (status == POST_CHANGE && cmd == UIC_CMD_DME_HIBER_EXIT)
  158. cdns_ufs_set_l4_attr(hba);
  159. }
  160. /**
  161. * cdns_ufs_link_startup_notify()
  162. * Called before and after Link startup is carried out.
  163. * @hba: host controller instance
  164. * @status: notify stage (pre, post change)
  165. *
  166. * Return zero for success and non-zero for failure
  167. */
  168. static int cdns_ufs_link_startup_notify(struct ufs_hba *hba,
  169. enum ufs_notify_change_status status)
  170. {
  171. if (status != PRE_CHANGE)
  172. return 0;
  173. /*
  174. * Some UFS devices have issues if LCC is enabled.
  175. * So we are setting PA_Local_TX_LCC_Enable to 0
  176. * before link startup which will make sure that both host
  177. * and device TX LCC are disabled once link startup is
  178. * completed.
  179. */
  180. ufshcd_disable_host_tx_lcc(hba);
  181. /*
  182. * Disabling Autohibern8 feature in cadence UFS
  183. * to mask unexpected interrupt trigger.
  184. */
  185. hba->ahit = 0;
  186. return 0;
  187. }
  188. /**
  189. * cdns_ufs_init - performs additional ufs initialization
  190. * @hba: host controller instance
  191. *
  192. * Returns status of initialization
  193. */
  194. static int cdns_ufs_init(struct ufs_hba *hba)
  195. {
  196. int status = 0;
  197. struct cdns_ufs_host *host;
  198. struct device *dev = hba->dev;
  199. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  200. if (!host)
  201. return -ENOMEM;
  202. ufshcd_set_variant(hba, host);
  203. status = ufshcd_vops_phy_initialization(hba);
  204. return status;
  205. }
  206. /**
  207. * cdns_ufs_m31_16nm_phy_initialization - performs m31 phy initialization
  208. * @hba: host controller instance
  209. *
  210. * Always returns 0
  211. */
  212. static int cdns_ufs_m31_16nm_phy_initialization(struct ufs_hba *hba)
  213. {
  214. u32 data;
  215. /* Increase RX_Advanced_Min_ActivateTime_Capability */
  216. data = ufshcd_readl(hba, CDNS_UFS_REG_PHY_XCFGD1);
  217. data |= BIT(24);
  218. ufshcd_writel(hba, data, CDNS_UFS_REG_PHY_XCFGD1);
  219. return 0;
  220. }
  221. static const struct ufs_hba_variant_ops cdns_ufs_pltfm_hba_vops = {
  222. .name = "cdns-ufs-pltfm",
  223. .init = cdns_ufs_init,
  224. .hce_enable_notify = cdns_ufs_hce_enable_notify,
  225. .link_startup_notify = cdns_ufs_link_startup_notify,
  226. .hibern8_notify = cdns_ufs_hibern8_notify,
  227. };
  228. static const struct ufs_hba_variant_ops cdns_ufs_m31_16nm_pltfm_hba_vops = {
  229. .name = "cdns-ufs-pltfm",
  230. .init = cdns_ufs_init,
  231. .hce_enable_notify = cdns_ufs_hce_enable_notify,
  232. .link_startup_notify = cdns_ufs_link_startup_notify,
  233. .phy_initialization = cdns_ufs_m31_16nm_phy_initialization,
  234. .hibern8_notify = cdns_ufs_hibern8_notify,
  235. };
  236. static const struct of_device_id cdns_ufs_of_match[] = {
  237. {
  238. .compatible = "cdns,ufshc",
  239. .data = &cdns_ufs_pltfm_hba_vops,
  240. },
  241. {
  242. .compatible = "cdns,ufshc-m31-16nm",
  243. .data = &cdns_ufs_m31_16nm_pltfm_hba_vops,
  244. },
  245. { },
  246. };
  247. MODULE_DEVICE_TABLE(of, cdns_ufs_of_match);
  248. /**
  249. * cdns_ufs_pltfrm_probe - probe routine of the driver
  250. * @pdev: pointer to platform device handle
  251. *
  252. * Return zero for success and non-zero for failure
  253. */
  254. static int cdns_ufs_pltfrm_probe(struct platform_device *pdev)
  255. {
  256. int err;
  257. const struct of_device_id *of_id;
  258. struct ufs_hba_variant_ops *vops;
  259. struct device *dev = &pdev->dev;
  260. of_id = of_match_node(cdns_ufs_of_match, dev->of_node);
  261. vops = (struct ufs_hba_variant_ops *)of_id->data;
  262. /* Perform generic probe */
  263. err = ufshcd_pltfrm_init(pdev, vops);
  264. if (err)
  265. dev_err(dev, "ufshcd_pltfrm_init() failed %d\n", err);
  266. return err;
  267. }
  268. /**
  269. * cdns_ufs_pltfrm_remove - removes the ufs driver
  270. * @pdev: pointer to platform device handle
  271. *
  272. * Always returns 0
  273. */
  274. static int cdns_ufs_pltfrm_remove(struct platform_device *pdev)
  275. {
  276. struct ufs_hba *hba = platform_get_drvdata(pdev);
  277. ufshcd_remove(hba);
  278. return 0;
  279. }
  280. static const struct dev_pm_ops cdns_ufs_dev_pm_ops = {
  281. SET_SYSTEM_SLEEP_PM_OPS(ufshcd_system_suspend, ufshcd_system_resume)
  282. SET_RUNTIME_PM_OPS(ufshcd_runtime_suspend, ufshcd_runtime_resume, NULL)
  283. .prepare = ufshcd_suspend_prepare,
  284. .complete = ufshcd_resume_complete,
  285. };
  286. static struct platform_driver cdns_ufs_pltfrm_driver = {
  287. .probe = cdns_ufs_pltfrm_probe,
  288. .remove = cdns_ufs_pltfrm_remove,
  289. .shutdown = ufshcd_pltfrm_shutdown,
  290. .driver = {
  291. .name = "cdns-ufshcd",
  292. .pm = &cdns_ufs_dev_pm_ops,
  293. .of_match_table = cdns_ufs_of_match,
  294. },
  295. };
  296. module_platform_driver(cdns_ufs_pltfrm_driver);
  297. MODULE_AUTHOR("Jan Kotas <[email protected]>");
  298. MODULE_DESCRIPTION("Cadence UFS host controller platform driver");
  299. MODULE_LICENSE("GPL v2");