GNU Linux-libre 4.9.309-gnu1
[releases.git] / drivers / usb / chipidea / host.c
1 /*
2  * host.c - ChipIdea USB host controller driver
3  *
4  * Copyright (c) 2012 Intel Corporation
5  *
6  * Author: Alexander Shishkin
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
35 static struct hc_driver __read_mostly ci_ehci_hc_driver;
36 static int (*orig_bus_suspend)(struct usb_hcd *hcd);
37
38 struct ehci_ci_priv {
39         struct regulator *reg_vbus;
40         bool enabled;
41 };
42
43 static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
44 {
45         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
46         struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
47         struct device *dev = hcd->self.controller;
48         struct ci_hdrc *ci = dev_get_drvdata(dev);
49         int ret = 0;
50         int port = HCS_N_PORTS(ehci->hcs_params);
51
52         if (priv->reg_vbus && enable != priv->enabled) {
53                 if (port > 1) {
54                         dev_warn(dev,
55                                 "Not support multi-port regulator control\n");
56                         return 0;
57                 }
58                 if (enable)
59                         ret = regulator_enable(priv->reg_vbus);
60                 else
61                         ret = regulator_disable(priv->reg_vbus);
62                 if (ret) {
63                         dev_err(dev,
64                                 "Failed to %s vbus regulator, ret=%d\n",
65                                 enable ? "enable" : "disable", ret);
66                         return ret;
67                 }
68                 priv->enabled = enable;
69         }
70
71         if (enable && (ci->platdata->phy_mode == USBPHY_INTERFACE_MODE_HSIC)) {
72                 /*
73                  * Marvell 28nm HSIC PHY requires forcing the port to HS mode.
74                  * As HSIC is always HS, this should be safe for others.
75                  */
76                 hw_port_test_set(ci, 5);
77                 hw_port_test_set(ci, 0);
78         }
79         return 0;
80 };
81
82 static int ehci_ci_reset(struct usb_hcd *hcd)
83 {
84         struct device *dev = hcd->self.controller;
85         struct ci_hdrc *ci = dev_get_drvdata(dev);
86         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
87         int ret;
88
89         ret = ehci_setup(hcd);
90         if (ret)
91                 return ret;
92
93         ehci->need_io_watchdog = 0;
94
95         ci_platform_configure(ci);
96
97         return ret;
98 }
99
100 static const struct ehci_driver_overrides ehci_ci_overrides = {
101         .extra_priv_size = sizeof(struct ehci_ci_priv),
102         .port_power      = ehci_ci_portpower,
103         .reset           = ehci_ci_reset,
104 };
105
106 static irqreturn_t host_irq(struct ci_hdrc *ci)
107 {
108         return usb_hcd_irq(ci->irq, ci->hcd);
109 }
110
111 static int host_start(struct ci_hdrc *ci)
112 {
113         struct usb_hcd *hcd;
114         struct ehci_hcd *ehci;
115         struct ehci_ci_priv *priv;
116         int ret;
117
118         if (usb_disabled())
119                 return -ENODEV;
120
121         hcd = usb_create_hcd(&ci_ehci_hc_driver, ci->dev, dev_name(ci->dev));
122         if (!hcd)
123                 return -ENOMEM;
124
125         dev_set_drvdata(ci->dev, ci);
126         hcd->rsrc_start = ci->hw_bank.phys;
127         hcd->rsrc_len = ci->hw_bank.size;
128         hcd->regs = ci->hw_bank.abs;
129         hcd->has_tt = 1;
130
131         hcd->power_budget = ci->platdata->power_budget;
132         hcd->tpl_support = ci->platdata->tpl_support;
133         if (ci->phy)
134                 hcd->phy = ci->phy;
135         else
136                 hcd->usb_phy = ci->usb_phy;
137
138         ehci = hcd_to_ehci(hcd);
139         ehci->caps = ci->hw_bank.cap;
140         ehci->has_hostpc = ci->hw_bank.lpm;
141         ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
142         ehci->imx28_write_fix = ci->imx28_write_fix;
143
144         priv = (struct ehci_ci_priv *)ehci->priv;
145         priv->reg_vbus = NULL;
146
147         if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
148                 if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
149                         ret = regulator_enable(ci->platdata->reg_vbus);
150                         if (ret) {
151                                 dev_err(ci->dev,
152                                 "Failed to enable vbus regulator, ret=%d\n",
153                                                                         ret);
154                                 goto put_hcd;
155                         }
156                 } else {
157                         priv->reg_vbus = ci->platdata->reg_vbus;
158                 }
159         }
160
161         ret = usb_add_hcd(hcd, 0, 0);
162         if (ret) {
163                 goto disable_reg;
164         } else {
165                 struct usb_otg *otg = &ci->otg;
166
167                 ci->hcd = hcd;
168
169                 if (ci_otg_is_fsm_mode(ci)) {
170                         otg->host = &hcd->self;
171                         hcd->self.otg_port = 1;
172                 }
173         }
174
175         return ret;
176
177 disable_reg:
178         if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
179                         (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
180                 regulator_disable(ci->platdata->reg_vbus);
181 put_hcd:
182         usb_put_hcd(hcd);
183
184         return ret;
185 }
186
187 static void host_stop(struct ci_hdrc *ci)
188 {
189         struct usb_hcd *hcd = ci->hcd;
190
191         if (hcd) {
192                 usb_remove_hcd(hcd);
193                 ci->role = CI_ROLE_END;
194                 synchronize_irq(ci->irq);
195                 usb_put_hcd(hcd);
196                 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
197                         (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
198                                 regulator_disable(ci->platdata->reg_vbus);
199         }
200         ci->hcd = NULL;
201         ci->otg.host = NULL;
202 }
203
204
205 void ci_hdrc_host_destroy(struct ci_hdrc *ci)
206 {
207         if (ci->role == CI_ROLE_HOST && ci->hcd)
208                 host_stop(ci);
209 }
210
211 static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
212 {
213         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
214         int port;
215         u32 tmp;
216
217         int ret = orig_bus_suspend(hcd);
218
219         if (ret)
220                 return ret;
221
222         port = HCS_N_PORTS(ehci->hcs_params);
223         while (port--) {
224                 u32 __iomem *reg = &ehci->regs->port_status[port];
225                 u32 portsc = ehci_readl(ehci, reg);
226
227                 if (portsc & PORT_CONNECT) {
228                         /*
229                          * For chipidea, the resume signal will be ended
230                          * automatically, so for remote wakeup case, the
231                          * usbcmd.rs may not be set before the resume has
232                          * ended if other resume paths consumes too much
233                          * time (~24ms), in that case, the SOF will not
234                          * send out within 3ms after resume ends, then the
235                          * high speed device will enter full speed mode.
236                          */
237
238                         tmp = ehci_readl(ehci, &ehci->regs->command);
239                         tmp |= CMD_RUN;
240                         ehci_writel(ehci, tmp, &ehci->regs->command);
241                         /*
242                          * It needs a short delay between set RS bit and PHCD.
243                          */
244                         usleep_range(150, 200);
245                         break;
246                 }
247         }
248
249         return 0;
250 }
251
252 int ci_hdrc_host_init(struct ci_hdrc *ci)
253 {
254         struct ci_role_driver *rdrv;
255
256         if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
257                 return -ENXIO;
258
259         rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
260         if (!rdrv)
261                 return -ENOMEM;
262
263         rdrv->start     = host_start;
264         rdrv->stop      = host_stop;
265         rdrv->irq       = host_irq;
266         rdrv->name      = "host";
267         ci->roles[CI_ROLE_HOST] = rdrv;
268
269         return 0;
270 }
271
272 void ci_hdrc_host_driver_init(void)
273 {
274         ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
275         orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
276         ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
277 }