blob: 63dfb793c6bdeac041da58edd5450357ac5d9cbb [file] [log] [blame]
developer507fc9b2020-05-02 11:35:18 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019 MediaTek, Inc.
4 * Authors: Chunfeng Yun <chunfeng.yun@mediatek.com>
5 */
6
7#include <clk.h>
Tom Riniabb9a042024-05-18 20:20:43 -06008#include <common.h>
developer507fc9b2020-05-02 11:35:18 +02009#include <dm.h>
Sean Anderson429ce522020-10-04 21:39:53 -040010#include <dm/device_compat.h>
developer507fc9b2020-05-02 11:35:18 +020011#include <dm/devres.h>
12#include <generic-phy.h>
13#include <malloc.h>
Sean Anderson429ce522020-10-04 21:39:53 -040014#include <power/regulator.h>
developer507fc9b2020-05-02 11:35:18 +020015#include <usb.h>
Sean Anderson429ce522020-10-04 21:39:53 -040016#include <usb/xhci.h>
developer005bc332023-02-17 17:04:10 +080017#include <linux/bitfield.h>
developer507fc9b2020-05-02 11:35:18 +020018#include <linux/compat.h>
developer005bc332023-02-17 17:04:10 +080019#include <linux/errno.h>
developer507fc9b2020-05-02 11:35:18 +020020#include <linux/iopoll.h>
developer507fc9b2020-05-02 11:35:18 +020021
22/* IPPC (IP Port Control) registers */
23#define IPPC_IP_PW_CTRL0 0x00
24#define CTRL0_IP_SW_RST BIT(0)
25
26#define IPPC_IP_PW_CTRL1 0x04
27#define CTRL1_IP_HOST_PDN BIT(0)
28
29#define IPPC_IP_PW_STS1 0x10
30#define STS1_IP_SLEEP_STS BIT(30)
31#define STS1_U3_MAC_RST BIT(16)
32#define STS1_XHCI_RST BIT(11)
33#define STS1_SYS125_RST BIT(10)
34#define STS1_REF_RST BIT(8)
35#define STS1_SYSPLL_STABLE BIT(0)
36
37#define IPPC_IP_XHCI_CAP 0x24
38#define CAP_U3_PORT_NUM(p) ((p) & 0xff)
39#define CAP_U2_PORT_NUM(p) (((p) >> 8) & 0xff)
40
41#define IPPC_U3_CTRL_0P 0x30
42#define CTRL_U3_PORT_HOST_SEL BIT(2)
43#define CTRL_U3_PORT_PDN BIT(1)
44#define CTRL_U3_PORT_DIS BIT(0)
45
46#define IPPC_U2_CTRL_0P 0x50
47#define CTRL_U2_PORT_HOST_SEL BIT(2)
48#define CTRL_U2_PORT_PDN BIT(1)
49#define CTRL_U2_PORT_DIS BIT(0)
50
51#define IPPC_U3_CTRL(p) (IPPC_U3_CTRL_0P + ((p) * 0x08))
52#define IPPC_U2_CTRL(p) (IPPC_U2_CTRL_0P + ((p) * 0x08))
53
developer005bc332023-02-17 17:04:10 +080054/* xHCI CSR */
55#define LS_EOF_CFG 0x930
56#define LSEOF_OFFSET 0x89
57
58#define FS_EOF_CFG 0x934
59#define FSEOF_OFFSET 0x2e
60
61#define SS_GEN1_EOF_CFG 0x93c
62#define SSG1EOF_OFFSET 0x78
63
64#define HFCNTR_CFG 0x944
65#define ITP_DELTA_CLK_MASK GENMASK(5, 1)
66#define FRMCNT_LEV1_RANG_MASK GENMASK(19, 8)
67
68#define SS_GEN2_EOF_CFG 0x990
69#define SSG2EOF_OFFSET 0x3c
70
71#define XSEOF_OFFSET_MASK GENMASK(11, 0)
72
developer507fc9b2020-05-02 11:35:18 +020073struct mtk_xhci {
74 struct xhci_ctrl ctrl; /* Needs to come first in this struct! */
75 struct xhci_hccr *hcd;
76 void __iomem *ippc;
77 struct udevice *dev;
78 struct udevice *vusb33_supply;
79 struct udevice *vbus_supply;
80 struct clk_bulk clks;
81 struct phy_bulk phys;
82 int num_u2ports;
83 int num_u3ports;
developer37b83282020-12-23 09:52:20 +080084 u32 u3p_dis_msk;
85 u32 u2p_dis_msk;
developer507fc9b2020-05-02 11:35:18 +020086};
87
developer005bc332023-02-17 17:04:10 +080088/*
89 * workaround for mt8195:
90 * MT8195 has 4 controllers, the controller1~3's default SOF/ITP interval
91 * is calculated from the frame counter clock 24M, but in fact, the clock
92 * is 48M.
93 */
94static void xhci_mtk_set_frame_interval(struct mtk_xhci *mtk)
95{
96 void __iomem *mac = (void __iomem *)mtk->hcd;
97
98 if (!ofnode_device_is_compatible(dev_ofnode(mtk->dev), "mediatek,mt8195-xhci"))
99 return;
100
101 clrsetbits_le32(mac + HFCNTR_CFG,
102 ITP_DELTA_CLK_MASK | FRMCNT_LEV1_RANG_MASK,
103 FIELD_PREP(ITP_DELTA_CLK_MASK, 0xa) |
104 FIELD_PREP(FRMCNT_LEV1_RANG_MASK, 0x12b));
105
106 clrsetbits_le32(mac + LS_EOF_CFG, XSEOF_OFFSET_MASK, LSEOF_OFFSET);
107 clrsetbits_le32(mac + FS_EOF_CFG, XSEOF_OFFSET_MASK, FSEOF_OFFSET);
108 clrsetbits_le32(mac + SS_GEN1_EOF_CFG, XSEOF_OFFSET_MASK, SSG1EOF_OFFSET);
109 clrsetbits_le32(mac + SS_GEN2_EOF_CFG, XSEOF_OFFSET_MASK, SSG2EOF_OFFSET);
110}
111
developer507fc9b2020-05-02 11:35:18 +0200112static int xhci_mtk_host_enable(struct mtk_xhci *mtk)
113{
developer37b83282020-12-23 09:52:20 +0800114 int u3_ports_disabed = 0;
developer507fc9b2020-05-02 11:35:18 +0200115 u32 value;
116 u32 check_val;
117 int ret;
118 int i;
119
120 /* power on host ip */
121 clrbits_le32(mtk->ippc + IPPC_IP_PW_CTRL1, CTRL1_IP_HOST_PDN);
122
developer37b83282020-12-23 09:52:20 +0800123 /* power on and enable u3 ports except skipped ones */
developer507fc9b2020-05-02 11:35:18 +0200124 for (i = 0; i < mtk->num_u3ports; i++) {
developer37b83282020-12-23 09:52:20 +0800125 if (BIT(i) & mtk->u3p_dis_msk) {
126 u3_ports_disabed++;
127 continue;
128 }
129
developer507fc9b2020-05-02 11:35:18 +0200130 clrsetbits_le32(mtk->ippc + IPPC_U3_CTRL(i),
131 CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS,
132 CTRL_U3_PORT_HOST_SEL);
133 }
134
developer37b83282020-12-23 09:52:20 +0800135 /* power on and enable u2 ports except skipped ones */
developer507fc9b2020-05-02 11:35:18 +0200136 for (i = 0; i < mtk->num_u2ports; i++) {
developer37b83282020-12-23 09:52:20 +0800137 if (BIT(i) & mtk->u2p_dis_msk)
138 continue;
139
developer507fc9b2020-05-02 11:35:18 +0200140 clrsetbits_le32(mtk->ippc + IPPC_U2_CTRL(i),
141 CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS,
142 CTRL_U2_PORT_HOST_SEL);
143 }
144
145 /*
146 * wait for clocks to be stable, and clock domains reset to
147 * be inactive after power on and enable ports
148 */
149 check_val = STS1_SYSPLL_STABLE | STS1_REF_RST |
150 STS1_SYS125_RST | STS1_XHCI_RST;
151
developer37b83282020-12-23 09:52:20 +0800152 if (mtk->num_u3ports > u3_ports_disabed)
developer507fc9b2020-05-02 11:35:18 +0200153 check_val |= STS1_U3_MAC_RST;
154
155 ret = readl_poll_timeout(mtk->ippc + IPPC_IP_PW_STS1, value,
156 (check_val == (value & check_val)), 20000);
157 if (ret)
158 dev_err(mtk->dev, "clocks are not stable 0x%x!\n", value);
159
160 return ret;
161}
162
163static int xhci_mtk_host_disable(struct mtk_xhci *mtk)
164{
165 int i;
166
167 /* power down all u3 ports */
168 for (i = 0; i < mtk->num_u3ports; i++)
developer683cde32022-05-27 09:52:09 +0800169 setbits_le32(mtk->ippc + IPPC_U3_CTRL(i),
170 CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS);
developer507fc9b2020-05-02 11:35:18 +0200171
172 /* power down all u2 ports */
173 for (i = 0; i < mtk->num_u2ports; i++)
developer683cde32022-05-27 09:52:09 +0800174 setbits_le32(mtk->ippc + IPPC_U2_CTRL(i),
175 CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS);
developer507fc9b2020-05-02 11:35:18 +0200176
177 /* power down host ip */
178 setbits_le32(mtk->ippc + IPPC_IP_PW_CTRL1, CTRL1_IP_HOST_PDN);
179
180 return 0;
181}
182
183static int xhci_mtk_ssusb_init(struct mtk_xhci *mtk)
184{
185 u32 value;
186
187 /* reset whole ip */
188 setbits_le32(mtk->ippc + IPPC_IP_PW_CTRL0, CTRL0_IP_SW_RST);
189 udelay(1);
190 clrbits_le32(mtk->ippc + IPPC_IP_PW_CTRL0, CTRL0_IP_SW_RST);
191
192 value = readl(mtk->ippc + IPPC_IP_XHCI_CAP);
193 mtk->num_u3ports = CAP_U3_PORT_NUM(value);
194 mtk->num_u2ports = CAP_U2_PORT_NUM(value);
195 dev_info(mtk->dev, "u2p:%d, u3p:%d\n",
196 mtk->num_u2ports, mtk->num_u3ports);
197
198 return xhci_mtk_host_enable(mtk);
199}
200
201static int xhci_mtk_ofdata_get(struct mtk_xhci *mtk)
202{
203 struct udevice *dev = mtk->dev;
204 int ret = 0;
205
206 mtk->hcd = devfdt_remap_addr_name(dev, "mac");
207 if (!mtk->hcd) {
208 dev_err(dev, "failed to get xHCI base address\n");
209 return -ENXIO;
210 }
211
212 mtk->ippc = devfdt_remap_addr_name(dev, "ippc");
213 if (!mtk->ippc) {
214 dev_err(dev, "failed to get IPPC base address\n");
215 return -ENXIO;
216 }
217
218 dev_info(dev, "hcd: 0x%p, ippc: 0x%p\n", mtk->hcd, mtk->ippc);
219
220 ret = clk_get_bulk(dev, &mtk->clks);
221 if (ret) {
222 dev_err(dev, "failed to get clocks %d!\n", ret);
223 return ret;
224 }
225
226 ret = device_get_supply_regulator(dev, "vusb33-supply",
227 &mtk->vusb33_supply);
228 if (ret)
229 debug("can't get vusb33 regulator %d!\n", ret);
230
231 ret = device_get_supply_regulator(dev, "vbus-supply",
232 &mtk->vbus_supply);
233 if (ret)
234 debug("can't get vbus regulator %d!\n", ret);
235
developer37b83282020-12-23 09:52:20 +0800236 /* optional properties to disable ports, ignore the error */
237 dev_read_u32(dev, "mediatek,u3p-dis-msk", &mtk->u3p_dis_msk);
238 dev_read_u32(dev, "mediatek,u2p-dis-msk", &mtk->u2p_dis_msk);
239 dev_info(dev, "ports disabled mask: u3p-0x%x, u2p-0x%x\n",
240 mtk->u3p_dis_msk, mtk->u2p_dis_msk);
241
developer507fc9b2020-05-02 11:35:18 +0200242 return 0;
243}
244
245static int xhci_mtk_ldos_enable(struct mtk_xhci *mtk)
246{
247 int ret;
248
249 ret = regulator_set_enable(mtk->vusb33_supply, true);
250 if (ret < 0 && ret != -ENOSYS) {
251 dev_err(mtk->dev, "failed to enable vusb33 %d!\n", ret);
252 return ret;
253 }
254
255 ret = regulator_set_enable(mtk->vbus_supply, true);
256 if (ret < 0 && ret != -ENOSYS) {
257 dev_err(mtk->dev, "failed to enable vbus %d!\n", ret);
258 regulator_set_enable(mtk->vusb33_supply, false);
259 return ret;
260 }
261
262 return 0;
263}
264
265static void xhci_mtk_ldos_disable(struct mtk_xhci *mtk)
266{
267 regulator_set_enable(mtk->vbus_supply, false);
268 regulator_set_enable(mtk->vusb33_supply, false);
269}
270
271static int xhci_mtk_phy_setup(struct mtk_xhci *mtk)
272{
273 struct udevice *dev = mtk->dev;
274 struct phy_bulk *phys = &mtk->phys;
275 int ret;
276
277 ret = generic_phy_get_bulk(dev, phys);
278 if (ret)
279 return ret;
280
281 ret = generic_phy_init_bulk(phys);
282 if (ret)
283 return ret;
284
285 ret = generic_phy_power_on_bulk(phys);
286 if (ret)
287 generic_phy_exit_bulk(phys);
288
289 return ret;
290}
291
292static void xhci_mtk_phy_shutdown(struct mtk_xhci *mtk)
293{
294 generic_phy_power_off_bulk(&mtk->phys);
295 generic_phy_exit_bulk(&mtk->phys);
296}
297
298static int xhci_mtk_probe(struct udevice *dev)
299{
300 struct mtk_xhci *mtk = dev_get_priv(dev);
301 struct xhci_hcor *hcor;
302 int ret;
303
304 mtk->dev = dev;
305 ret = xhci_mtk_ofdata_get(mtk);
306 if (ret)
307 return ret;
308
309 ret = xhci_mtk_ldos_enable(mtk);
310 if (ret)
311 goto ldos_err;
312
313 ret = clk_enable_bulk(&mtk->clks);
314 if (ret)
315 goto clks_err;
316
317 ret = xhci_mtk_phy_setup(mtk);
318 if (ret)
319 goto phys_err;
320
321 ret = xhci_mtk_ssusb_init(mtk);
322 if (ret)
323 goto ssusb_init_err;
324
developer005bc332023-02-17 17:04:10 +0800325 xhci_mtk_set_frame_interval(mtk);
326
developer80390532020-09-08 18:59:57 +0200327 mtk->ctrl.quirks = XHCI_MTK_HOST;
developer507fc9b2020-05-02 11:35:18 +0200328 hcor = (struct xhci_hcor *)((uintptr_t)mtk->hcd +
329 HC_LENGTH(xhci_readl(&mtk->hcd->cr_capbase)));
330
331 return xhci_register(dev, mtk->hcd, hcor);
332
333ssusb_init_err:
334 xhci_mtk_phy_shutdown(mtk);
335phys_err:
336 clk_disable_bulk(&mtk->clks);
337clks_err:
338 xhci_mtk_ldos_disable(mtk);
339ldos_err:
340 return ret;
341}
342
343static int xhci_mtk_remove(struct udevice *dev)
344{
345 struct mtk_xhci *mtk = dev_get_priv(dev);
346
347 xhci_deregister(dev);
348 xhci_mtk_host_disable(mtk);
349 xhci_mtk_ldos_disable(mtk);
350 clk_disable_bulk(&mtk->clks);
351
352 return 0;
353}
354
355static const struct udevice_id xhci_mtk_ids[] = {
356 { .compatible = "mediatek,mtk-xhci" },
developer005bc332023-02-17 17:04:10 +0800357 { .compatible = "mediatek,mt8195-xhci" },
developer507fc9b2020-05-02 11:35:18 +0200358 { }
359};
360
361U_BOOT_DRIVER(usb_xhci) = {
362 .name = "xhci-mtk",
363 .id = UCLASS_USB,
364 .of_match = xhci_mtk_ids,
365 .probe = xhci_mtk_probe,
366 .remove = xhci_mtk_remove,
367 .ops = &xhci_usb_ops,
368 .bind = dm_scan_fdt_dev,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700369 .priv_auto = sizeof(struct mtk_xhci),
developer507fc9b2020-05-02 11:35:18 +0200370 .flags = DM_FLAG_ALLOC_PRIV_DMA,
371};