blob: 7e288f0575b6c8064e6c60a466519c0a1b10de86 [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>
developer507fc9b2020-05-02 11:35:18 +02008#include <dm.h>
Sean Anderson429ce522020-10-04 21:39:53 -04009#include <dm/device_compat.h>
developer507fc9b2020-05-02 11:35:18 +020010#include <dm/devres.h>
11#include <generic-phy.h>
12#include <malloc.h>
Sean Anderson429ce522020-10-04 21:39:53 -040013#include <power/regulator.h>
developer507fc9b2020-05-02 11:35:18 +020014#include <usb.h>
Sean Anderson429ce522020-10-04 21:39:53 -040015#include <usb/xhci.h>
developer005bc332023-02-17 17:04:10 +080016#include <linux/bitfield.h>
developer507fc9b2020-05-02 11:35:18 +020017#include <linux/compat.h>
developer005bc332023-02-17 17:04:10 +080018#include <linux/errno.h>
developer507fc9b2020-05-02 11:35:18 +020019#include <linux/iopoll.h>
developer507fc9b2020-05-02 11:35:18 +020020
21/* IPPC (IP Port Control) registers */
22#define IPPC_IP_PW_CTRL0 0x00
23#define CTRL0_IP_SW_RST BIT(0)
24
25#define IPPC_IP_PW_CTRL1 0x04
26#define CTRL1_IP_HOST_PDN BIT(0)
27
28#define IPPC_IP_PW_STS1 0x10
29#define STS1_IP_SLEEP_STS BIT(30)
30#define STS1_U3_MAC_RST BIT(16)
31#define STS1_XHCI_RST BIT(11)
32#define STS1_SYS125_RST BIT(10)
33#define STS1_REF_RST BIT(8)
34#define STS1_SYSPLL_STABLE BIT(0)
35
36#define IPPC_IP_XHCI_CAP 0x24
37#define CAP_U3_PORT_NUM(p) ((p) & 0xff)
38#define CAP_U2_PORT_NUM(p) (((p) >> 8) & 0xff)
39
40#define IPPC_U3_CTRL_0P 0x30
41#define CTRL_U3_PORT_HOST_SEL BIT(2)
42#define CTRL_U3_PORT_PDN BIT(1)
43#define CTRL_U3_PORT_DIS BIT(0)
44
45#define IPPC_U2_CTRL_0P 0x50
46#define CTRL_U2_PORT_HOST_SEL BIT(2)
47#define CTRL_U2_PORT_PDN BIT(1)
48#define CTRL_U2_PORT_DIS BIT(0)
49
50#define IPPC_U3_CTRL(p) (IPPC_U3_CTRL_0P + ((p) * 0x08))
51#define IPPC_U2_CTRL(p) (IPPC_U2_CTRL_0P + ((p) * 0x08))
52
developer005bc332023-02-17 17:04:10 +080053/* xHCI CSR */
54#define LS_EOF_CFG 0x930
55#define LSEOF_OFFSET 0x89
56
57#define FS_EOF_CFG 0x934
58#define FSEOF_OFFSET 0x2e
59
60#define SS_GEN1_EOF_CFG 0x93c
61#define SSG1EOF_OFFSET 0x78
62
63#define HFCNTR_CFG 0x944
64#define ITP_DELTA_CLK_MASK GENMASK(5, 1)
65#define FRMCNT_LEV1_RANG_MASK GENMASK(19, 8)
66
67#define SS_GEN2_EOF_CFG 0x990
68#define SSG2EOF_OFFSET 0x3c
69
70#define XSEOF_OFFSET_MASK GENMASK(11, 0)
71
developer507fc9b2020-05-02 11:35:18 +020072struct mtk_xhci {
73 struct xhci_ctrl ctrl; /* Needs to come first in this struct! */
74 struct xhci_hccr *hcd;
75 void __iomem *ippc;
76 struct udevice *dev;
77 struct udevice *vusb33_supply;
78 struct udevice *vbus_supply;
79 struct clk_bulk clks;
80 struct phy_bulk phys;
81 int num_u2ports;
82 int num_u3ports;
developer37b83282020-12-23 09:52:20 +080083 u32 u3p_dis_msk;
84 u32 u2p_dis_msk;
developer507fc9b2020-05-02 11:35:18 +020085};
86
developer005bc332023-02-17 17:04:10 +080087/*
88 * workaround for mt8195:
89 * MT8195 has 4 controllers, the controller1~3's default SOF/ITP interval
90 * is calculated from the frame counter clock 24M, but in fact, the clock
91 * is 48M.
92 */
93static void xhci_mtk_set_frame_interval(struct mtk_xhci *mtk)
94{
95 void __iomem *mac = (void __iomem *)mtk->hcd;
96
97 if (!ofnode_device_is_compatible(dev_ofnode(mtk->dev), "mediatek,mt8195-xhci"))
98 return;
99
100 clrsetbits_le32(mac + HFCNTR_CFG,
101 ITP_DELTA_CLK_MASK | FRMCNT_LEV1_RANG_MASK,
102 FIELD_PREP(ITP_DELTA_CLK_MASK, 0xa) |
103 FIELD_PREP(FRMCNT_LEV1_RANG_MASK, 0x12b));
104
105 clrsetbits_le32(mac + LS_EOF_CFG, XSEOF_OFFSET_MASK, LSEOF_OFFSET);
106 clrsetbits_le32(mac + FS_EOF_CFG, XSEOF_OFFSET_MASK, FSEOF_OFFSET);
107 clrsetbits_le32(mac + SS_GEN1_EOF_CFG, XSEOF_OFFSET_MASK, SSG1EOF_OFFSET);
108 clrsetbits_le32(mac + SS_GEN2_EOF_CFG, XSEOF_OFFSET_MASK, SSG2EOF_OFFSET);
109}
110
developer507fc9b2020-05-02 11:35:18 +0200111static int xhci_mtk_host_enable(struct mtk_xhci *mtk)
112{
developer37b83282020-12-23 09:52:20 +0800113 int u3_ports_disabed = 0;
developer507fc9b2020-05-02 11:35:18 +0200114 u32 value;
115 u32 check_val;
116 int ret;
117 int i;
118
119 /* power on host ip */
120 clrbits_le32(mtk->ippc + IPPC_IP_PW_CTRL1, CTRL1_IP_HOST_PDN);
121
developer37b83282020-12-23 09:52:20 +0800122 /* power on and enable u3 ports except skipped ones */
developer507fc9b2020-05-02 11:35:18 +0200123 for (i = 0; i < mtk->num_u3ports; i++) {
developer37b83282020-12-23 09:52:20 +0800124 if (BIT(i) & mtk->u3p_dis_msk) {
125 u3_ports_disabed++;
126 continue;
127 }
128
developer507fc9b2020-05-02 11:35:18 +0200129 clrsetbits_le32(mtk->ippc + IPPC_U3_CTRL(i),
130 CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS,
131 CTRL_U3_PORT_HOST_SEL);
132 }
133
developer37b83282020-12-23 09:52:20 +0800134 /* power on and enable u2 ports except skipped ones */
developer507fc9b2020-05-02 11:35:18 +0200135 for (i = 0; i < mtk->num_u2ports; i++) {
developer37b83282020-12-23 09:52:20 +0800136 if (BIT(i) & mtk->u2p_dis_msk)
137 continue;
138
developer507fc9b2020-05-02 11:35:18 +0200139 clrsetbits_le32(mtk->ippc + IPPC_U2_CTRL(i),
140 CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS,
141 CTRL_U2_PORT_HOST_SEL);
142 }
143
144 /*
145 * wait for clocks to be stable, and clock domains reset to
146 * be inactive after power on and enable ports
147 */
148 check_val = STS1_SYSPLL_STABLE | STS1_REF_RST |
149 STS1_SYS125_RST | STS1_XHCI_RST;
150
developer37b83282020-12-23 09:52:20 +0800151 if (mtk->num_u3ports > u3_ports_disabed)
developer507fc9b2020-05-02 11:35:18 +0200152 check_val |= STS1_U3_MAC_RST;
153
154 ret = readl_poll_timeout(mtk->ippc + IPPC_IP_PW_STS1, value,
155 (check_val == (value & check_val)), 20000);
156 if (ret)
157 dev_err(mtk->dev, "clocks are not stable 0x%x!\n", value);
158
159 return ret;
160}
161
162static int xhci_mtk_host_disable(struct mtk_xhci *mtk)
163{
164 int i;
165
166 /* power down all u3 ports */
167 for (i = 0; i < mtk->num_u3ports; i++)
developer683cde32022-05-27 09:52:09 +0800168 setbits_le32(mtk->ippc + IPPC_U3_CTRL(i),
169 CTRL_U3_PORT_PDN | CTRL_U3_PORT_DIS);
developer507fc9b2020-05-02 11:35:18 +0200170
171 /* power down all u2 ports */
172 for (i = 0; i < mtk->num_u2ports; i++)
developer683cde32022-05-27 09:52:09 +0800173 setbits_le32(mtk->ippc + IPPC_U2_CTRL(i),
174 CTRL_U2_PORT_PDN | CTRL_U2_PORT_DIS);
developer507fc9b2020-05-02 11:35:18 +0200175
176 /* power down host ip */
177 setbits_le32(mtk->ippc + IPPC_IP_PW_CTRL1, CTRL1_IP_HOST_PDN);
178
179 return 0;
180}
181
182static int xhci_mtk_ssusb_init(struct mtk_xhci *mtk)
183{
184 u32 value;
185
186 /* reset whole ip */
187 setbits_le32(mtk->ippc + IPPC_IP_PW_CTRL0, CTRL0_IP_SW_RST);
188 udelay(1);
189 clrbits_le32(mtk->ippc + IPPC_IP_PW_CTRL0, CTRL0_IP_SW_RST);
190
191 value = readl(mtk->ippc + IPPC_IP_XHCI_CAP);
192 mtk->num_u3ports = CAP_U3_PORT_NUM(value);
193 mtk->num_u2ports = CAP_U2_PORT_NUM(value);
194 dev_info(mtk->dev, "u2p:%d, u3p:%d\n",
195 mtk->num_u2ports, mtk->num_u3ports);
196
197 return xhci_mtk_host_enable(mtk);
198}
199
200static int xhci_mtk_ofdata_get(struct mtk_xhci *mtk)
201{
202 struct udevice *dev = mtk->dev;
203 int ret = 0;
204
205 mtk->hcd = devfdt_remap_addr_name(dev, "mac");
206 if (!mtk->hcd) {
207 dev_err(dev, "failed to get xHCI base address\n");
208 return -ENXIO;
209 }
210
211 mtk->ippc = devfdt_remap_addr_name(dev, "ippc");
212 if (!mtk->ippc) {
213 dev_err(dev, "failed to get IPPC base address\n");
214 return -ENXIO;
215 }
216
217 dev_info(dev, "hcd: 0x%p, ippc: 0x%p\n", mtk->hcd, mtk->ippc);
218
219 ret = clk_get_bulk(dev, &mtk->clks);
220 if (ret) {
221 dev_err(dev, "failed to get clocks %d!\n", ret);
222 return ret;
223 }
224
225 ret = device_get_supply_regulator(dev, "vusb33-supply",
226 &mtk->vusb33_supply);
227 if (ret)
228 debug("can't get vusb33 regulator %d!\n", ret);
229
230 ret = device_get_supply_regulator(dev, "vbus-supply",
231 &mtk->vbus_supply);
232 if (ret)
233 debug("can't get vbus regulator %d!\n", ret);
234
developer37b83282020-12-23 09:52:20 +0800235 /* optional properties to disable ports, ignore the error */
236 dev_read_u32(dev, "mediatek,u3p-dis-msk", &mtk->u3p_dis_msk);
237 dev_read_u32(dev, "mediatek,u2p-dis-msk", &mtk->u2p_dis_msk);
238 dev_info(dev, "ports disabled mask: u3p-0x%x, u2p-0x%x\n",
239 mtk->u3p_dis_msk, mtk->u2p_dis_msk);
240
developer507fc9b2020-05-02 11:35:18 +0200241 return 0;
242}
243
244static int xhci_mtk_ldos_enable(struct mtk_xhci *mtk)
245{
246 int ret;
247
248 ret = regulator_set_enable(mtk->vusb33_supply, true);
249 if (ret < 0 && ret != -ENOSYS) {
250 dev_err(mtk->dev, "failed to enable vusb33 %d!\n", ret);
251 return ret;
252 }
253
254 ret = regulator_set_enable(mtk->vbus_supply, true);
255 if (ret < 0 && ret != -ENOSYS) {
256 dev_err(mtk->dev, "failed to enable vbus %d!\n", ret);
257 regulator_set_enable(mtk->vusb33_supply, false);
258 return ret;
259 }
260
261 return 0;
262}
263
264static void xhci_mtk_ldos_disable(struct mtk_xhci *mtk)
265{
266 regulator_set_enable(mtk->vbus_supply, false);
267 regulator_set_enable(mtk->vusb33_supply, false);
268}
269
270static int xhci_mtk_phy_setup(struct mtk_xhci *mtk)
271{
272 struct udevice *dev = mtk->dev;
273 struct phy_bulk *phys = &mtk->phys;
274 int ret;
275
276 ret = generic_phy_get_bulk(dev, phys);
277 if (ret)
278 return ret;
279
280 ret = generic_phy_init_bulk(phys);
281 if (ret)
282 return ret;
283
284 ret = generic_phy_power_on_bulk(phys);
285 if (ret)
286 generic_phy_exit_bulk(phys);
287
288 return ret;
289}
290
291static void xhci_mtk_phy_shutdown(struct mtk_xhci *mtk)
292{
293 generic_phy_power_off_bulk(&mtk->phys);
294 generic_phy_exit_bulk(&mtk->phys);
295}
296
297static int xhci_mtk_probe(struct udevice *dev)
298{
299 struct mtk_xhci *mtk = dev_get_priv(dev);
300 struct xhci_hcor *hcor;
301 int ret;
302
303 mtk->dev = dev;
304 ret = xhci_mtk_ofdata_get(mtk);
305 if (ret)
306 return ret;
307
308 ret = xhci_mtk_ldos_enable(mtk);
309 if (ret)
310 goto ldos_err;
311
312 ret = clk_enable_bulk(&mtk->clks);
313 if (ret)
314 goto clks_err;
315
316 ret = xhci_mtk_phy_setup(mtk);
317 if (ret)
318 goto phys_err;
319
320 ret = xhci_mtk_ssusb_init(mtk);
321 if (ret)
322 goto ssusb_init_err;
323
developer005bc332023-02-17 17:04:10 +0800324 xhci_mtk_set_frame_interval(mtk);
325
developer80390532020-09-08 18:59:57 +0200326 mtk->ctrl.quirks = XHCI_MTK_HOST;
developer507fc9b2020-05-02 11:35:18 +0200327 hcor = (struct xhci_hcor *)((uintptr_t)mtk->hcd +
328 HC_LENGTH(xhci_readl(&mtk->hcd->cr_capbase)));
329
330 return xhci_register(dev, mtk->hcd, hcor);
331
332ssusb_init_err:
333 xhci_mtk_phy_shutdown(mtk);
334phys_err:
335 clk_disable_bulk(&mtk->clks);
336clks_err:
337 xhci_mtk_ldos_disable(mtk);
338ldos_err:
339 return ret;
340}
341
342static int xhci_mtk_remove(struct udevice *dev)
343{
344 struct mtk_xhci *mtk = dev_get_priv(dev);
345
346 xhci_deregister(dev);
347 xhci_mtk_host_disable(mtk);
348 xhci_mtk_ldos_disable(mtk);
349 clk_disable_bulk(&mtk->clks);
350
351 return 0;
352}
353
354static const struct udevice_id xhci_mtk_ids[] = {
355 { .compatible = "mediatek,mtk-xhci" },
developer005bc332023-02-17 17:04:10 +0800356 { .compatible = "mediatek,mt8195-xhci" },
developer507fc9b2020-05-02 11:35:18 +0200357 { }
358};
359
360U_BOOT_DRIVER(usb_xhci) = {
361 .name = "xhci-mtk",
362 .id = UCLASS_USB,
363 .of_match = xhci_mtk_ids,
364 .probe = xhci_mtk_probe,
365 .remove = xhci_mtk_remove,
366 .ops = &xhci_usb_ops,
367 .bind = dm_scan_fdt_dev,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700368 .priv_auto = sizeof(struct mtk_xhci),
developer507fc9b2020-05-02 11:35:18 +0200369 .flags = DM_FLAG_ALLOC_PRIV_DMA,
370};