1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <linux/kernel.h>
23#include <linux/io.h>
24#include <linux/usb.h>
25#include <linux/usb/hcd.h>
26#include <linux/usb/chipidea.h>
27#include <linux/regulator/consumer.h>
28
29#include "../host/ehci.h"
30
31#include "ci.h"
32#include "bits.h"
33#include "host.h"
34
35static struct hc_driver __read_mostly ci_ehci_hc_driver;
36static int (*orig_bus_suspend)(struct usb_hcd *hcd);
37
38struct ehci_ci_priv {
39 struct regulator *reg_vbus;
40};
41
42static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
43{
44 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
45 struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
46 struct device *dev = hcd->self.controller;
47 struct ci_hdrc *ci = dev_get_drvdata(dev);
48 int ret = 0;
49 int port = HCS_N_PORTS(ehci->hcs_params);
50
51 if (priv->reg_vbus) {
52 if (port > 1) {
53 dev_warn(dev,
54 "Not support multi-port regulator control\n");
55 return 0;
56 }
57 if (enable)
58 ret = regulator_enable(priv->reg_vbus);
59 else
60 ret = regulator_disable(priv->reg_vbus);
61 if (ret) {
62 dev_err(dev,
63 "Failed to %s vbus regulator, ret=%d\n",
64 enable ? "enable" : "disable", ret);
65 return ret;
66 }
67 }
68
69 if (ci->platdata->flags & CI_HDRC_PHY_VBUS_CONTROL &&
70 ci->usb_phy && ci->usb_phy->set_vbus) {
71 if (enable)
72 ci->usb_phy->set_vbus(ci->usb_phy, 1);
73 else
74 ci->usb_phy->set_vbus(ci->usb_phy, 0);
75 }
76
77 if (enable && (ci->platdata->phy_mode == USBPHY_INTERFACE_MODE_HSIC)) {
78
79
80
81
82 hw_port_test_set(ci, 5);
83 hw_port_test_set(ci, 0);
84 }
85
86 return 0;
87};
88
89static int ehci_ci_reset(struct usb_hcd *hcd)
90{
91 struct device *dev = hcd->self.controller;
92 struct ci_hdrc *ci = dev_get_drvdata(dev);
93 int ret;
94
95 ret = ehci_setup(hcd);
96 if (ret)
97 return ret;
98
99 ci_platform_configure(ci);
100
101 return ret;
102}
103
104static const struct ehci_driver_overrides ehci_ci_overrides = {
105 .extra_priv_size = sizeof(struct ehci_ci_priv),
106 .port_power = ehci_ci_portpower,
107 .reset = ehci_ci_reset,
108};
109
110static irqreturn_t host_irq(struct ci_hdrc *ci)
111{
112 return usb_hcd_irq(ci->irq, ci->hcd);
113}
114
115static int host_start(struct ci_hdrc *ci)
116{
117 struct usb_hcd *hcd;
118 struct ehci_hcd *ehci;
119 struct ehci_ci_priv *priv;
120 int ret;
121
122 if (usb_disabled())
123 return -ENODEV;
124
125 hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
126 if (!hcd)
127 return -ENOMEM;
128
129 dev_set_drvdata(ci->dev, ci);
130 hcd->rsrc_start = ci->hw_bank.phys;
131 hcd->rsrc_len = ci->hw_bank.size;
132 hcd->regs = ci->hw_bank.abs;
133 hcd->has_tt = 1;
134
135 hcd->power_budget = ci->platdata->power_budget;
136 hcd->tpl_support = ci->platdata->tpl_support;
137 if (ci->phy)
138 hcd->phy = ci->phy;
139 else
140 hcd->usb_phy = ci->usb_phy;
141
142 ehci = hcd_to_ehci(hcd);
143 ehci->caps = ci->hw_bank.cap;
144 ehci->has_hostpc = ci->hw_bank.lpm;
145 ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
146 ehci->imx28_write_fix = ci->imx28_write_fix;
147
148 priv = (struct ehci_ci_priv *)ehci->priv;
149 priv->reg_vbus = NULL;
150
151 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
152 if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
153 ret = regulator_enable(ci->platdata->reg_vbus);
154 if (ret) {
155 dev_err(ci->dev,
156 "Failed to enable vbus regulator, ret=%d\n",
157 ret);
158 goto put_hcd;
159 }
160 } else {
161 priv->reg_vbus = ci->platdata->reg_vbus;
162 }
163 }
164
165 ret = usb_add_hcd(hcd, 0, 0);
166 if (ret) {
167 goto disable_reg;
168 } else {
169 struct usb_otg *otg = &ci->otg;
170
171 ci->hcd = hcd;
172
173 if (ci_otg_is_fsm_mode(ci)) {
174 otg->host = &hcd->self;
175 hcd->self.otg_port = 1;
176 }
177 }
178
179 return ret;
180
181disable_reg:
182 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
183 (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
184 regulator_disable(ci->platdata->reg_vbus);
185put_hcd:
186 usb_put_hcd(hcd);
187
188 return ret;
189}
190
191static void host_stop(struct ci_hdrc *ci)
192{
193 struct usb_hcd *hcd = ci->hcd;
194
195 if (hcd) {
196 usb_remove_hcd(hcd);
197 usb_put_hcd(hcd);
198 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
199 (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
200 regulator_disable(ci->platdata->reg_vbus);
201 }
202 ci->hcd = NULL;
203 ci->otg.host = NULL;
204}
205
206
207void ci_hdrc_host_destroy(struct ci_hdrc *ci)
208{
209 if (ci->role == CI_ROLE_HOST && ci->hcd)
210 host_stop(ci);
211}
212
213static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
214{
215 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
216 int port;
217 u32 tmp;
218
219 int ret = orig_bus_suspend(hcd);
220
221 if (ret)
222 return ret;
223
224 port = HCS_N_PORTS(ehci->hcs_params);
225 while (port--) {
226 u32 __iomem *reg = &ehci->regs->port_status[port];
227 u32 portsc = ehci_readl(ehci, reg);
228
229 if (portsc & PORT_CONNECT) {
230
231
232
233
234
235
236
237
238
239
240 tmp = ehci_readl(ehci, &ehci->regs->command);
241 tmp |= CMD_RUN;
242 ehci_writel(ehci, tmp, &ehci->regs->command);
243
244
245
246 usleep_range(150, 200);
247 break;
248 }
249 }
250
251 return 0;
252}
253
254int ci_hdrc_host_init(struct ci_hdrc *ci)
255{
256 struct ci_role_driver *rdrv;
257
258 if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
259 return -ENXIO;
260
261 rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
262 if (!rdrv)
263 return -ENOMEM;
264
265 rdrv->start = host_start;
266 rdrv->stop = host_stop;
267 rdrv->irq = host_irq;
268 rdrv->name = "host";
269 ci->roles[CI_ROLE_HOST] = rdrv;
270
271 return 0;
272}
273
274void ci_hdrc_host_driver_init(void)
275{
276 ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
277 orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
278 ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
279}
280