1 /*
2 * Qualcomm EHCI driver
3 *
4 * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com>
5 *
6 * Based on Linux driver
7 *
8 * SPDX-License-Identifier: GPL-2.0+
9 */
10
11 #include <common.h>
12 #include <dm.h>
13 #include <errno.h>
14 #include <usb.h>
15 #include <usb/ehci-ci.h>
16 #include <usb/ulpi.h>
17 #include <wait_bit.h>
18 #include <asm/gpio.h>
19 #include <asm/io.h>
20 #include <linux/compat.h>
21 #include "ehci.h"
22
23 /* PHY viewport regs */
24 #define ULPI_MISC_A_READ 0x96
25 #define ULPI_MISC_A_SET 0x97
26 #define ULPI_MISC_A_CLEAR 0x98
27 #define ULPI_MISC_A_VBUSVLDEXTSEL (1 << 1)
28 #define ULPI_MISC_A_VBUSVLDEXT (1 << 0)
29
30 #define GEN2_SESS_VLD_CTRL_EN (1 << 7)
31
32 #define SESS_VLD_CTRL (1 << 25)
33
34 struct msm_ehci_priv {
35 struct ehci_ctrl ctrl; /* Needed by EHCI */
36 struct usb_ehci *ehci; /* Start of IP core*/
37 struct ulpi_viewport ulpi_vp; /* ULPI Viewport */
38 };
39
board_prepare_usb(enum usb_init_type type)40 int __weak board_prepare_usb(enum usb_init_type type)
41 {
42 return 0;
43 }
44
setup_usb_phy(struct msm_ehci_priv * priv)45 static void setup_usb_phy(struct msm_ehci_priv *priv)
46 {
47 /* Select and enable external configuration with USB PHY */
48 ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_SET,
49 ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT);
50 }
51
reset_usb_phy(struct msm_ehci_priv * priv)52 static void reset_usb_phy(struct msm_ehci_priv *priv)
53 {
54 /* Disable VBUS mimicing in the controller. */
55 ulpi_write(&priv->ulpi_vp, (u8 *)ULPI_MISC_A_CLEAR,
56 ULPI_MISC_A_VBUSVLDEXTSEL | ULPI_MISC_A_VBUSVLDEXT);
57 }
58
59
msm_init_after_reset(struct ehci_ctrl * dev)60 static int msm_init_after_reset(struct ehci_ctrl *dev)
61 {
62 struct msm_ehci_priv *p = container_of(dev, struct msm_ehci_priv, ctrl);
63 struct usb_ehci *ehci = p->ehci;
64
65 /* select ULPI phy */
66 writel(PORT_PTS_ULPI, &ehci->portsc);
67 setup_usb_phy(p);
68
69 /* Enable sess_vld */
70 setbits_le32(&ehci->genconfig2, GEN2_SESS_VLD_CTRL_EN);
71
72 /* Enable external vbus configuration in the LINK */
73 setbits_le32(&ehci->usbcmd, SESS_VLD_CTRL);
74
75 /* USB_OTG_HS_AHB_BURST */
76 writel(0x0, &ehci->sbuscfg);
77
78 /* USB_OTG_HS_AHB_MODE: HPROT_MODE */
79 /* Bus access related config. */
80 writel(0x08, &ehci->sbusmode);
81
82 /* set mode to host controller */
83 writel(CM_HOST, &ehci->usbmode);
84
85 return 0;
86 }
87
88 static const struct ehci_ops msm_ehci_ops = {
89 .init_after_reset = msm_init_after_reset
90 };
91
ehci_usb_probe(struct udevice * dev)92 static int ehci_usb_probe(struct udevice *dev)
93 {
94 struct msm_ehci_priv *p = dev_get_priv(dev);
95 struct usb_ehci *ehci = p->ehci;
96 struct ehci_hccr *hccr;
97 struct ehci_hcor *hcor;
98 int ret;
99
100 hccr = (struct ehci_hccr *)((phys_addr_t)&ehci->caplength);
101 hcor = (struct ehci_hcor *)((phys_addr_t)hccr +
102 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
103
104 ret = board_prepare_usb(USB_INIT_HOST);
105 if (ret < 0)
106 return ret;
107
108 return ehci_register(dev, hccr, hcor, &msm_ehci_ops, 0, USB_INIT_HOST);
109 }
110
ehci_usb_remove(struct udevice * dev)111 static int ehci_usb_remove(struct udevice *dev)
112 {
113 struct msm_ehci_priv *p = dev_get_priv(dev);
114 struct usb_ehci *ehci = p->ehci;
115 int ret;
116
117 ret = ehci_deregister(dev);
118 if (ret)
119 return ret;
120
121 /* Stop controller. */
122 clrbits_le32(&ehci->usbcmd, CMD_RUN);
123
124 reset_usb_phy(p);
125
126 ret = board_prepare_usb(USB_INIT_DEVICE); /* Board specific hook */
127 if (ret < 0)
128 return ret;
129
130 /* Reset controller */
131 setbits_le32(&ehci->usbcmd, CMD_RESET);
132
133 /* Wait for reset */
134 if (wait_for_bit_le32(&ehci->usbcmd, CMD_RESET, false, 30, false)) {
135 printf("Stuck on USB reset.\n");
136 return -ETIMEDOUT;
137 }
138
139 return 0;
140 }
141
ehci_usb_ofdata_to_platdata(struct udevice * dev)142 static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
143 {
144 struct msm_ehci_priv *priv = dev_get_priv(dev);
145
146 priv->ulpi_vp.port_num = 0;
147 priv->ehci = dev_read_addr_ptr(dev);
148
149 if (priv->ehci == (void *)FDT_ADDR_T_NONE)
150 return -EINVAL;
151
152 /* Warning: this will not work if viewport address is > 64 bit due to
153 * ULPI design.
154 */
155 priv->ulpi_vp.viewport_addr = (phys_addr_t)&priv->ehci->ulpi_viewpoint;
156
157 return 0;
158 }
159
160 static const struct udevice_id ehci_usb_ids[] = {
161 { .compatible = "qcom,ehci-host", },
162 { }
163 };
164
165 U_BOOT_DRIVER(usb_ehci) = {
166 .name = "ehci_msm",
167 .id = UCLASS_USB,
168 .of_match = ehci_usb_ids,
169 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
170 .probe = ehci_usb_probe,
171 .remove = ehci_usb_remove,
172 .ops = &ehci_usb_ops,
173 .priv_auto_alloc_size = sizeof(struct msm_ehci_priv),
174 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
175 .flags = DM_FLAG_ALLOC_PRIV_DMA,
176 };
177