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