blob: 4dd9508a60b521dcb1e239db1d5a2590a9a988ec [file] [log] [blame]
Chunfeng Yunb3f4e722016-10-19 10:28:25 +08001/*
2 * mtu3_dr.c - dual role switch and host glue layer
3 *
4 * Copyright (C) 2016 MediaTek Inc.
5 *
6 * Author: Chunfeng Yun <chunfeng.yun@mediatek.com>
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 */
18
19#include <linux/clk.h>
20#include <linux/iopoll.h>
21#include <linux/irq.h>
22#include <linux/kernel.h>
23#include <linux/mfd/syscon.h>
24#include <linux/of_device.h>
25#include <linux/regmap.h>
26
27#include "mtu3.h"
28#include "mtu3_dr.h"
29
30#define PERI_WK_CTRL1 0x404
31#define UWK_CTL1_IS_C(x) (((x) & 0xf) << 26)
32#define UWK_CTL1_IS_E BIT(25)
33#define UWK_CTL1_IDDIG_C(x) (((x) & 0xf) << 11) /* cycle debounce */
34#define UWK_CTL1_IDDIG_E BIT(10) /* enable debounce */
35#define UWK_CTL1_IDDIG_P BIT(9) /* polarity */
36#define UWK_CTL1_IS_P BIT(6) /* polarity for ip sleep */
37
38/*
39 * ip-sleep wakeup mode:
40 * all clocks can be turn off, but power domain should be kept on
41 */
42static void ssusb_wakeup_ip_sleep_en(struct ssusb_mtk *ssusb)
43{
44 u32 tmp;
45 struct regmap *pericfg = ssusb->pericfg;
46
47 regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
48 tmp &= ~UWK_CTL1_IS_P;
49 tmp &= ~(UWK_CTL1_IS_C(0xf));
50 tmp |= UWK_CTL1_IS_C(0x8);
51 regmap_write(pericfg, PERI_WK_CTRL1, tmp);
52 regmap_write(pericfg, PERI_WK_CTRL1, tmp | UWK_CTL1_IS_E);
53
54 regmap_read(pericfg, PERI_WK_CTRL1, &tmp);
55 dev_dbg(ssusb->dev, "%s(): WK_CTRL1[P6,E25,C26:29]=%#x\n",
56 __func__, tmp);
57}
58
59static void ssusb_wakeup_ip_sleep_dis(struct ssusb_mtk *ssusb)
60{
61 u32 tmp;
62
63 regmap_read(ssusb->pericfg, PERI_WK_CTRL1, &tmp);
64 tmp &= ~UWK_CTL1_IS_E;
65 regmap_write(ssusb->pericfg, PERI_WK_CTRL1, tmp);
66}
67
68int ssusb_wakeup_of_property_parse(struct ssusb_mtk *ssusb,
69 struct device_node *dn)
70{
71 struct device *dev = ssusb->dev;
72
73 /*
74 * Wakeup function is optional, so it is not an error if this property
75 * does not exist, and in such case, no need to get relative
76 * properties anymore.
77 */
78 ssusb->wakeup_en = of_property_read_bool(dn, "mediatek,enable-wakeup");
79 if (!ssusb->wakeup_en)
80 return 0;
81
82 ssusb->wk_deb_p0 = devm_clk_get(dev, "wakeup_deb_p0");
83 if (IS_ERR(ssusb->wk_deb_p0)) {
84 dev_err(dev, "fail to get wakeup_deb_p0\n");
85 return PTR_ERR(ssusb->wk_deb_p0);
86 }
87
88 if (of_property_read_bool(dn, "wakeup_deb_p1")) {
89 ssusb->wk_deb_p1 = devm_clk_get(dev, "wakeup_deb_p1");
90 if (IS_ERR(ssusb->wk_deb_p1)) {
91 dev_err(dev, "fail to get wakeup_deb_p1\n");
92 return PTR_ERR(ssusb->wk_deb_p1);
93 }
94 }
95
96 ssusb->pericfg = syscon_regmap_lookup_by_phandle(dn,
97 "mediatek,syscon-wakeup");
98 if (IS_ERR(ssusb->pericfg)) {
99 dev_err(dev, "fail to get pericfg regs\n");
100 return PTR_ERR(ssusb->pericfg);
101 }
102
103 return 0;
104}
105
106static int ssusb_wakeup_clks_enable(struct ssusb_mtk *ssusb)
107{
108 int ret;
109
110 ret = clk_prepare_enable(ssusb->wk_deb_p0);
111 if (ret) {
112 dev_err(ssusb->dev, "failed to enable wk_deb_p0\n");
113 goto usb_p0_err;
114 }
115
116 ret = clk_prepare_enable(ssusb->wk_deb_p1);
117 if (ret) {
118 dev_err(ssusb->dev, "failed to enable wk_deb_p1\n");
119 goto usb_p1_err;
120 }
121
122 return 0;
123
124usb_p1_err:
125 clk_disable_unprepare(ssusb->wk_deb_p0);
126usb_p0_err:
127 return -EINVAL;
128}
129
130static void ssusb_wakeup_clks_disable(struct ssusb_mtk *ssusb)
131{
132 clk_disable_unprepare(ssusb->wk_deb_p1);
133 clk_disable_unprepare(ssusb->wk_deb_p0);
134}
135
136static void host_ports_num_get(struct ssusb_mtk *ssusb)
137{
138 u32 xhci_cap;
139
140 xhci_cap = mtu3_readl(ssusb->ippc_base, U3D_SSUSB_IP_XHCI_CAP);
141 ssusb->u2_ports = SSUSB_IP_XHCI_U2_PORT_NUM(xhci_cap);
142 ssusb->u3_ports = SSUSB_IP_XHCI_U3_PORT_NUM(xhci_cap);
143
144 dev_dbg(ssusb->dev, "host - u2_ports:%d, u3_ports:%d\n",
145 ssusb->u2_ports, ssusb->u3_ports);
146}
147
148/* only configure ports will be used later */
149int ssusb_host_enable(struct ssusb_mtk *ssusb)
150{
151 void __iomem *ibase = ssusb->ippc_base;
152 int num_u3p = ssusb->u3_ports;
153 int num_u2p = ssusb->u2_ports;
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800154 int u3_ports_disabed;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800155 u32 check_clk;
156 u32 value;
157 int i;
158
159 /* power on host ip */
160 mtu3_clrbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
161
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800162 /* power on and enable u3 ports except skipped ones */
163 u3_ports_disabed = 0;
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800164 for (i = 0; i < num_u3p; i++) {
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800165 if ((0x1 << i) & ssusb->u3p_dis_msk) {
166 u3_ports_disabed++;
167 continue;
168 }
169
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800170 value = mtu3_readl(ibase, SSUSB_U3_CTRL(i));
171 value &= ~(SSUSB_U3_PORT_PDN | SSUSB_U3_PORT_DIS);
172 value |= SSUSB_U3_PORT_HOST_SEL;
173 mtu3_writel(ibase, SSUSB_U3_CTRL(i), value);
174 }
175
176 /* power on and enable all u2 ports */
177 for (i = 0; i < num_u2p; i++) {
178 value = mtu3_readl(ibase, SSUSB_U2_CTRL(i));
179 value &= ~(SSUSB_U2_PORT_PDN | SSUSB_U2_PORT_DIS);
180 value |= SSUSB_U2_PORT_HOST_SEL;
181 mtu3_writel(ibase, SSUSB_U2_CTRL(i), value);
182 }
183
184 check_clk = SSUSB_XHCI_RST_B_STS;
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800185 if (num_u3p > u3_ports_disabed)
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800186 check_clk = SSUSB_U3_MAC_RST_B_STS;
187
188 return ssusb_check_clocks(ssusb, check_clk);
189}
190
191int ssusb_host_disable(struct ssusb_mtk *ssusb, bool suspend)
192{
193 void __iomem *ibase = ssusb->ippc_base;
194 int num_u3p = ssusb->u3_ports;
195 int num_u2p = ssusb->u2_ports;
196 u32 value;
197 int ret;
198 int i;
199
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800200 /* power down and disable u3 ports except skipped ones */
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800201 for (i = 0; i < num_u3p; i++) {
Chunfeng Yun076f1a82017-10-13 17:10:38 +0800202 if ((0x1 << i) & ssusb->u3p_dis_msk)
203 continue;
204
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800205 value = mtu3_readl(ibase, SSUSB_U3_CTRL(i));
206 value |= SSUSB_U3_PORT_PDN;
207 value |= suspend ? 0 : SSUSB_U3_PORT_DIS;
208 mtu3_writel(ibase, SSUSB_U3_CTRL(i), value);
209 }
210
211 /* power down and disable all u2 ports */
212 for (i = 0; i < num_u2p; i++) {
213 value = mtu3_readl(ibase, SSUSB_U2_CTRL(i));
214 value |= SSUSB_U2_PORT_PDN;
215 value |= suspend ? 0 : SSUSB_U2_PORT_DIS;
216 mtu3_writel(ibase, SSUSB_U2_CTRL(i), value);
217 }
218
219 /* power down host ip */
220 mtu3_setbits(ibase, U3D_SSUSB_IP_PW_CTRL1, SSUSB_IP_HOST_PDN);
221
222 if (!suspend)
223 return 0;
224
225 /* wait for host ip to sleep */
226 ret = readl_poll_timeout(ibase + U3D_SSUSB_IP_PW_STS1, value,
227 (value & SSUSB_IP_SLEEP_STS), 100, 100000);
228 if (ret)
229 dev_err(ssusb->dev, "ip sleep failed!!!\n");
230
231 return ret;
232}
233
234static void ssusb_host_setup(struct ssusb_mtk *ssusb)
235{
236 host_ports_num_get(ssusb);
237
238 /*
239 * power on host and power on/enable all ports
240 * if support OTG, gadget driver will switch port0 to device mode
241 */
242 ssusb_host_enable(ssusb);
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800243
244 /* if port0 supports dual-role, works as host mode by default */
245 ssusb_set_vbus(&ssusb->otg_switch, 1);
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800246}
247
248static void ssusb_host_cleanup(struct ssusb_mtk *ssusb)
249{
Chunfeng Yund0ed0622016-10-19 10:28:26 +0800250 if (ssusb->is_host)
251 ssusb_set_vbus(&ssusb->otg_switch, 0);
252
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800253 ssusb_host_disable(ssusb, false);
254}
255
256/*
257 * If host supports multiple ports, the VBUSes(5V) of ports except port0
258 * which supports OTG are better to be enabled by default in DTS.
259 * Because the host driver will keep link with devices attached when system
260 * enters suspend mode, so no need to control VBUSes after initialization.
261 */
262int ssusb_host_init(struct ssusb_mtk *ssusb, struct device_node *parent_dn)
263{
264 struct device *parent_dev = ssusb->dev;
265 int ret;
266
267 ssusb_host_setup(ssusb);
268
269 ret = of_platform_populate(parent_dn, NULL, NULL, parent_dev);
270 if (ret) {
Rob Herringd9241ff2017-07-18 16:43:35 -0500271 dev_dbg(parent_dev, "failed to create child devices at %pOF\n",
272 parent_dn);
Chunfeng Yunb3f4e722016-10-19 10:28:25 +0800273 return ret;
274 }
275
276 dev_info(parent_dev, "xHCI platform device register success...\n");
277
278 return 0;
279}
280
281void ssusb_host_exit(struct ssusb_mtk *ssusb)
282{
283 of_platform_depopulate(ssusb->dev);
284 ssusb_host_cleanup(ssusb);
285}
286
287int ssusb_wakeup_enable(struct ssusb_mtk *ssusb)
288{
289 int ret = 0;
290
291 if (ssusb->wakeup_en) {
292 ret = ssusb_wakeup_clks_enable(ssusb);
293 ssusb_wakeup_ip_sleep_en(ssusb);
294 }
295 return ret;
296}
297
298void ssusb_wakeup_disable(struct ssusb_mtk *ssusb)
299{
300 if (ssusb->wakeup_en) {
301 ssusb_wakeup_ip_sleep_dis(ssusb);
302 ssusb_wakeup_clks_disable(ssusb);
303 }
304}