ufshcd-dwc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <[email protected]>
  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()
  30. * This function programs the clk divider value. This value is needed to
  31. * provide 1 microsecond tick to unipro layer.
  32. * @hba: Private Structure pointer
  33. * @divider_val: clock divider value to be programmed
  34. *
  35. */
  36. static void ufshcd_dwc_program_clk_div(struct ufs_hba *hba, u32 divider_val)
  37. {
  38. ufshcd_writel(hba, divider_val, DWC_UFS_REG_HCLKDIV);
  39. }
  40. /**
  41. * ufshcd_dwc_link_is_up()
  42. * Check if link is up
  43. * @hba: private structure pointer
  44. *
  45. * Returns 0 on success, non-zero value on failure
  46. */
  47. static int ufshcd_dwc_link_is_up(struct ufs_hba *hba)
  48. {
  49. int dme_result = 0;
  50. ufshcd_dme_get(hba, UIC_ARG_MIB(VS_POWERSTATE), &dme_result);
  51. if (dme_result == UFSHCD_LINK_IS_UP) {
  52. ufshcd_set_link_active(hba);
  53. return 0;
  54. }
  55. return 1;
  56. }
  57. /**
  58. * ufshcd_dwc_connection_setup()
  59. * This function configures both the local side (host) and the peer side
  60. * (device) unipro attributes to establish the connection to application/
  61. * cport.
  62. * This function is not required if the hardware is properly configured to
  63. * have this connection setup on reset. But invoking this function does no
  64. * harm and should be fine even working with any ufs device.
  65. *
  66. * @hba: pointer to drivers private data
  67. *
  68. * Returns 0 on success non-zero value on failure
  69. */
  70. static int ufshcd_dwc_connection_setup(struct ufs_hba *hba)
  71. {
  72. static const struct ufshcd_dme_attr_val setup_attrs[] = {
  73. { UIC_ARG_MIB(T_CONNECTIONSTATE), 0, DME_LOCAL },
  74. { UIC_ARG_MIB(N_DEVICEID), 0, DME_LOCAL },
  75. { UIC_ARG_MIB(N_DEVICEID_VALID), 0, DME_LOCAL },
  76. { UIC_ARG_MIB(T_PEERDEVICEID), 1, DME_LOCAL },
  77. { UIC_ARG_MIB(T_PEERCPORTID), 0, DME_LOCAL },
  78. { UIC_ARG_MIB(T_TRAFFICCLASS), 0, DME_LOCAL },
  79. { UIC_ARG_MIB(T_CPORTFLAGS), 0x6, DME_LOCAL },
  80. { UIC_ARG_MIB(T_CPORTMODE), 1, DME_LOCAL },
  81. { UIC_ARG_MIB(T_CONNECTIONSTATE), 1, DME_LOCAL },
  82. { UIC_ARG_MIB(T_CONNECTIONSTATE), 0, DME_PEER },
  83. { UIC_ARG_MIB(N_DEVICEID), 1, DME_PEER },
  84. { UIC_ARG_MIB(N_DEVICEID_VALID), 1, DME_PEER },
  85. { UIC_ARG_MIB(T_PEERDEVICEID), 1, DME_PEER },
  86. { UIC_ARG_MIB(T_PEERCPORTID), 0, DME_PEER },
  87. { UIC_ARG_MIB(T_TRAFFICCLASS), 0, DME_PEER },
  88. { UIC_ARG_MIB(T_CPORTFLAGS), 0x6, DME_PEER },
  89. { UIC_ARG_MIB(T_CPORTMODE), 1, DME_PEER },
  90. { UIC_ARG_MIB(T_CONNECTIONSTATE), 1, DME_PEER }
  91. };
  92. return ufshcd_dwc_dme_set_attrs(hba, setup_attrs, ARRAY_SIZE(setup_attrs));
  93. }
  94. /**
  95. * ufshcd_dwc_link_startup_notify()
  96. * UFS Host DWC specific link startup sequence
  97. * @hba: private structure pointer
  98. * @status: Callback notify status
  99. *
  100. * Returns 0 on success, non-zero value on failure
  101. */
  102. int ufshcd_dwc_link_startup_notify(struct ufs_hba *hba,
  103. enum ufs_notify_change_status status)
  104. {
  105. int err = 0;
  106. if (status == PRE_CHANGE) {
  107. ufshcd_dwc_program_clk_div(hba, DWC_UFS_REG_HCLKDIV_DIV_125);
  108. err = ufshcd_vops_phy_initialization(hba);
  109. if (err) {
  110. dev_err(hba->dev, "Phy setup failed (%d)\n", err);
  111. goto out;
  112. }
  113. } else { /* POST_CHANGE */
  114. err = ufshcd_dwc_link_is_up(hba);
  115. if (err) {
  116. dev_err(hba->dev, "Link is not up\n");
  117. goto out;
  118. }
  119. err = ufshcd_dwc_connection_setup(hba);
  120. if (err)
  121. dev_err(hba->dev, "Connection setup failed (%d)\n",
  122. err);
  123. }
  124. out:
  125. return err;
  126. }
  127. EXPORT_SYMBOL(ufshcd_dwc_link_startup_notify);
  128. MODULE_AUTHOR("Joao Pinto <[email protected]>");
  129. MODULE_DESCRIPTION("UFS Host driver for Synopsys Designware Core");
  130. MODULE_LICENSE("Dual BSD/GPL");