ufshcd-dwc.c 4.2 KB

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