1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Qualcomm EHCI driver
4  *
5  * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
6  *
7  * Based on Linux driver
8  */
9 
10 #include <common.h>
11 #include <dm.h>
12 #include <errno.h>
13 #include <usb.h>
14 #include <usb/ehci-ci.h>
15 #include <usb/ulpi.h>
16 #include <wait_bit.h>
17 #include <asm/gpio.h>
18 #include <asm/io.h>
19 #include <linux/compat.h>
20 #include "ehci.h"
21 
22 struct msm_ehci_priv {
23 	struct ehci_ctrl ctrl; /* Needed by EHCI */
24 	struct usb_ehci *ehci; /* Start of IP core*/
25 	struct ulpi_viewport ulpi_vp; /* ULPI Viewport */
26 	struct phy phy;
27 };
28 
msm_init_after_reset(struct ehci_ctrl * dev)29 static int msm_init_after_reset(struct ehci_ctrl *dev)
30 {
31 	struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl);
32 	struct usb_ehci *ehci = p->ehci;
33 
34 	generic_phy_reset(&p->phy);
35 
36 	/* set mode to host controller */
37 	writel(CM_HOST, &ehci->usbmode);
38 
39 	return 0;
40 }
41 
42 static const struct ehci_ops msm_ehci_ops = {
43 	.init_after_reset = msm_init_after_reset
44 };
45 
ehci_usb_probe(struct udevice * dev)46 static int ehci_usb_probe(struct udevice *dev)
47 {
48 	struct msm_ehci_priv *p = dev_get_priv(dev);
49 	struct usb_ehci *ehci = p->ehci;
50 	struct usb_plat *plat = dev_get_plat(dev);
51 	struct ehci_hccr *hccr;
52 	struct ehci_hcor *hcor;
53 	int ret;
54 
55 	hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength);
56 	hcor = (struct ehci_hcor *)((phys_addr_t)hccr +
57 			HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
58 
59 	ret = ehci_setup_phy(dev, &p->phy, 0);
60 	if (ret)
61 		return ret;
62 
63 	ret = board_usb_init(0, plat->init_type);
64 	if (ret < 0)
65 		return ret;
66 
67 	return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0,
68 			     plat->init_type);
69 }
70 
ehci_usb_remove(struct udevice * dev)71 static int ehci_usb_remove(struct udevice *dev)
72 {
73 	struct msm_ehci_priv *p = dev_get_priv(dev);
74 	struct usb_ehci *ehci = p->ehci;
75 	int ret;
76 
77 	ret = ehci_deregister(dev);
78 	if (ret)
79 		return ret;
80 
81 	/* Stop controller. */
82 	clrbits_le32(&ehci->usbcmd, CMD_RUN);
83 
84 	ret = ehci_shutdown_phy(dev, &p->phy);
85 	if (ret)
86 		return ret;
87 
88 	ret = board_usb_init(0, USB_INIT_DEVICE); /* Board specific hook */
89 	if (ret < 0)
90 		return ret;
91 
92 	/* Reset controller */
93 	setbits_le32(&ehci->usbcmd, CMD_RESET);
94 
95 	/* Wait for reset */
96 	if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) {
97 		printf("Stuck on USB reset.\n");
98 		return -ETIMEDOUT;
99 	}
100 
101 	return 0;
102 }
103 
ehci_usb_of_to_plat(struct udevice * dev)104 static int ehci_usb_of_to_plat(struct udevice *dev)
105 {
106 	struct msm_ehci_priv *priv = dev_get_priv(dev);
107 
108 	priv->ulpi_vp.port_num = 0;
109 	priv->ehci = dev_read_addr_ptr(dev);
110 
111 	if (priv->ehci == (void *)FDT_ADDR_T_NONE)
112 		return -EINVAL;
113 
114 	/* Warning: this will not work if viewport address is > 64 bit due to
115 	 * ULPI design.
116 	 */
117 	priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint;
118 
119 	return 0;
120 }
121 
122 #if defined(CONFIG_CI_UDC)
123 /* Little quirk that MSM needs with Chipidea controller
124  * Must reinit phy after reset
125  */
ci_init_after_reset(struct ehci_ctrl * ctrl)126 void ci_init_after_reset(struct ehci_ctrl *ctrl)
127 {
128 	struct msm_ehci_priv *p = ctrl->priv;
129 
130 	generic_phy_reset(&p->phy);
131 }
132 #endif
133 
134 static const struct udevice_id ehci_usb_ids[] = {
135 	{ .compatible = "qcom,ehci-host", },
136 	{ }
137 };
138 
139 U_BOOT_DRIVER(usb_ehci) = {
140 	.name	= "ehci_msm",
141 	.id	= UCLASS_USB,
142 	.of_match = ehci_usb_ids,
143 	.of_to_plat = ehci_usb_of_to_plat,
144 	.probe = ehci_usb_probe,
145 	.remove = ehci_usb_remove,
146 	.ops	= &ehci_usb_ops,
147 	.priv_auto	= sizeof(struct msm_ehci_priv),
148 	.plat_auto	= sizeof(struct usb_plat),
149 	.flags	= DM_FLAG_ALLOC_PRIV_DMA,
150 };
151