ufshcd-dwc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * UFS Host driver for Synopsys Designware Core
  4. *
  5. * Copyright (C) 2015-2016 Synopsys, Inc. (www.synopsys.com)
  6. *
  7. * Authors: Joao Pinto <jpinto@synopsys.com>
  8. */
  9. #include <linux/module.h>
  10. #include <ufs/ufshcd.h>
  11. #include <ufs/unipro.h>
  12. #include "ufshcd-dwc.h"
  13. #include "ufshci-dwc.h"
  14. int ufshcd_dwc_dme_set_attrs(struct ufs_hba *hba,
  15. const struct ufshcd_dme_attr_val *v, int n)
  16. {
  17. int ret = 0;
  18. int attr_node = 0;
  19. for (attr_node = 0; attr_node < n; attr_node++) {
  20. ret = ufshcd_dme_set_attr(hba, v[attr_node].attr_sel,
  21. ATTR_SET_NOR, v[attr_node].mib_val, v[attr_node].peer);
  22. if (ret)
  23. return ret;
  24. }
  25. return 0;
  26. }
  27. EXPORT_SYMBOL(ufshcd_dwc_dme_set_attrs);
  28. /**
  29. * ufshcd_dwc_program_clk_div() - program clock divider.
  30. * @hba: Private Structure pointer
  31. * @divider_val: clock divider value to be programmed
  32. *
  33. */
  34. static void ufshcd_dwc_program_clk_div(struct ufs_hba *hba, u32 divider_val)
  35. {
  36. ufshcd_writel(hba, divider_val, DWC_UFS_REG_HCLKDIV);
  37. }
  38. /**
  39. * ufshcd_dwc_link_is_up() - check if link is up.
  40. * @hba: private structure pointer
  41. *
  42. * Return: 0 on success, non-zero value on failure.
  43. */
  44. static int ufshcd_dwc_link_is_up(struct ufs_hba *hba)
  45. {
  46. int dme_result = 0;
  47. ufshcd_dme_get(hba, UIC_ARG_MIB(VS_POWERSTATE), &dme_result);
  48. if (dme_result == UFSHCD_LINK_IS_UP) {
  49. ufshcd_set_link_active(hba);
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. /**
  55. * ufshcd_dwc_connection_setup() - configure unipro attributes.
  56. * @hba: pointer to drivers private data
  57. *
  58. * This function configures both the local side (host) and the peer side
  59. * (device) unipro attributes to establish the connection to application/
  60. * cport.
  61. * This function is not required if the hardware is properly configured to
  62. * have this connection setup on reset. But invoking this function does no
  63. * harm and should be fine even working with any ufs device.
  64. *
  65. * Return: 0 on success non-zero value on failure.
  66. */
  67. static int ufshcd_dwc_connection_setup(struct ufs_hba *hba)
  68. {
  69. static const struct ufshcd_dme_attr_val setup_attrs[] = {
  70. { UIC_ARG_MIB(T_CONNECTIONSTATE), 0, DME_LOCAL },
  71. { UIC_ARG_MIB(N_DEVICEID), 0, DME_LOCAL },
  72. { UIC_ARG_MIB(N_DEVICEID_VALID), 0, DME_LOCAL },
  73. { UIC_ARG_MIB(T_PEERDEVICEID), 1, DME_LOCAL },
  74. { UIC_ARG_MIB(T_PEERCPORTID), 0, DME_LOCAL },
  75. { UIC_ARG_MIB(T_TRAFFICCLASS), 0, DME_LOCAL },
  76. { UIC_ARG_MIB(T_CPORTFLAGS), 0x6, DME_LOCAL },
  77. { UIC_ARG_MIB(T_CPORTMODE), 1, DME_LOCAL },
  78. { UIC_ARG_MIB(T_CONNECTIONSTATE), 1, DME_LOCAL },
  79. { UIC_ARG_MIB(T_CONNECTIONSTATE), 0, DME_PEER },
  80. { UIC_ARG_MIB(N_DEVICEID), 1, DME_PEER },
  81. { UIC_ARG_MIB(N_DEVICEID_VALID), 1, DME_PEER },
  82. { UIC_ARG_MIB(T_PEERDEVICEID), 1, DME_PEER },
  83. { UIC_ARG_MIB(T_PEERCPORTID), 0, DME_PEER },
  84. { UIC_ARG_MIB(T_TRAFFICCLASS), 0, DME_PEER },
  85. { UIC_ARG_MIB(T_CPORTFLAGS), 0x6, DME_PEER },
  86. { UIC_ARG_MIB(T_CPORTMODE), 1, DME_PEER },
  87. { UIC_ARG_MIB(T_CONNECTIONSTATE), 1, DME_PEER }
  88. };
  89. return ufshcd_dwc_dme_set_attrs(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
  90. }
  91. /**
  92. * ufshcd_dwc_link_startup_notify() - program clock divider.
  93. * @hba: private structure pointer
  94. * @status: Callback notify status
  95. *
  96. * Return: 0 on success, non-zero value on failure.
  97. */
  98. int ufshcd_dwc_link_startup_notify(struct ufs_hba *hba,
  99. enum ufs_notify_change_status status)
  100. {
  101. int err = 0;
  102. if (status == PRE_CHANGE) {
  103. ufshcd_dwc_program_clk_div(hba, DWC_UFS_REG_HCLKDIV_DIV_125);
  104. err = ufshcd_vops_phy_initialization(hba);
  105. if (err) {
  106. dev_err(hba->dev, "Phy setup failed (%d)\n", err);
  107. goto out;
  108. }
  109. } else { /* POST_CHANGE */
  110. err = ufshcd_dwc_link_is_up(hba);
  111. if (err) {
  112. dev_err(hba->dev, "Link is not up\n");
  113. goto out;
  114. }
  115. err = ufshcd_dwc_connection_setup(hba);
  116. if (err)
  117. dev_err(hba->dev, "Connection setup failed (%d)\n",
  118. err);
  119. }
  120. out:
  121. return err;
  122. }
  123. EXPORT_SYMBOL(ufshcd_dwc_link_startup_notify);
  124. MODULE_AUTHOR("Joao Pinto <Joao.Pinto@synopsys.com>");
  125. MODULE_DESCRIPTION("UFS Host driver for Synopsys Designware Core");
  126. MODULE_LICENSE("Dual BSD/GPL");