1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2021 Nuvoton Technology Corp.
4  */
5 
6 #include <dm.h>
7 #include <generic-phy.h>
8 #include <reset.h>
9 #include <asm/io.h>
10 #include <dm/device_compat.h>
11 #include <linux/delay.h>
12 #include "ehci.h"
13 
14 struct npcm_ehci_priv {
15 	struct ehci_ctrl ctrl;
16 	struct ehci_hccr *hcd;
17 	struct phy phy;
18 };
19 
npcm_ehci_setup_phy(struct udevice * dev,struct phy * phy)20 static int npcm_ehci_setup_phy(struct udevice *dev, struct phy *phy)
21 {
22 	int ret;
23 
24 	if (!phy)
25 		return 0;
26 
27 	ret = generic_phy_get_by_index(dev, 0, phy);
28 	if (ret) {
29 		if (ret != -ENOENT) {
30 			dev_err(dev, "failed to get usb phy\n");
31 			return ret;
32 		}
33 	} else {
34 		ret = generic_phy_init(phy);
35 		if (ret) {
36 			dev_err(dev, "failed to init usb phy\n");
37 			return ret;
38 		}
39 	}
40 
41 	return 0;
42 }
43 
npcm_ehci_init(struct udevice * dev)44 static int npcm_ehci_init(struct udevice *dev)
45 {
46 	struct npcm_ehci_priv *priv = dev_get_priv(dev);
47 	struct reset_ctl reset;
48 	int ret;
49 
50 	ret = reset_get_by_index(dev, 0, &reset);
51 	if (ret && ret != -ENOENT && ret != -ENOTSUPP) {
52 		dev_err(dev, "failed to get reset\n");
53 		return ret;
54 	}
55 
56 	/* reset controller */
57 	if (reset_valid(&reset))
58 		reset_assert(&reset);
59 
60 	/* setup phy */
61 	ret = npcm_ehci_setup_phy(dev, &priv->phy);
62 	if (ret)
63 		return ret;
64 
65 	/* release controller from reset */
66 	if (reset_valid(&reset))
67 		reset_deassert(&reset);
68 
69 	return 0;
70 }
71 
npcm_ehci_probe(struct udevice * dev)72 static int npcm_ehci_probe(struct udevice *dev)
73 {
74 	struct npcm_ehci_priv *priv = dev_get_priv(dev);
75 	struct ehci_hcor *hcor;
76 	enum usb_init_type type = dev_get_driver_data(dev);
77 	int ret;
78 
79 	ret = npcm_ehci_init(dev);
80 	if (ret)
81 		return ret;
82 
83 	priv->hcd = (struct ehci_hccr *)dev_read_addr_ptr(dev);
84 	debug("USB HCD @0x%p\n", priv->hcd);
85 	hcor = (struct ehci_hcor *)((uintptr_t)priv->hcd +
86 			HC_LENGTH(ehci_readl(&priv->hcd->cr_capbase)));
87 
88 	return ehci_register(dev, priv->hcd, hcor, NULL, 0, type);
89 }
90 
npcm_ehci_remove(struct udevice * dev)91 static int npcm_ehci_remove(struct udevice *dev)
92 {
93 	struct npcm_ehci_priv *priv = dev_get_priv(dev);
94 
95 	generic_phy_exit(&priv->phy);
96 
97 	return ehci_deregister(dev);
98 }
99 
100 static const struct udevice_id npcm_ehci_ids[] = {
101 	{ .compatible = "nuvoton,npcm845-ehci", .data = USB_INIT_HOST },
102 	{ .compatible = "nuvoton,npcm845-udc", .data = USB_INIT_DEVICE },
103 	{ .compatible = "nuvoton,npcm750-ehci", .data = USB_INIT_HOST },
104 	{ .compatible = "nuvoton,npcm750-udc", .data = USB_INIT_DEVICE },
105 	{ }
106 };
107 
108 U_BOOT_DRIVER(ehci_npcm) = {
109 	.name	= "ehci_npcm",
110 	.id	= UCLASS_USB,
111 	.of_match = npcm_ehci_ids,
112 	.probe = npcm_ehci_probe,
113 	.remove = npcm_ehci_remove,
114 	.ops	= &ehci_usb_ops,
115 	.priv_auto = sizeof(struct npcm_ehci_priv),
116 	.plat_auto = sizeof(struct usb_plat),
117 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
118 };
119