]> Git Repo - linux.git/blob - drivers/usb/chipidea/host.c
brcmfmac: Fix out of bounds memory access during fw load
[linux.git] / drivers / usb / chipidea / host.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * host.c - ChipIdea USB host controller driver
4  *
5  * Copyright (c) 2012 Intel Corporation
6  *
7  * Author: Alexander Shishkin
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/io.h>
12 #include <linux/usb.h>
13 #include <linux/usb/hcd.h>
14 #include <linux/usb/chipidea.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/pinctrl/consumer.h>
17
18 #include "../host/ehci.h"
19
20 #include "ci.h"
21 #include "bits.h"
22 #include "host.h"
23
24 static struct hc_driver __read_mostly ci_ehci_hc_driver;
25 static int (*orig_bus_suspend)(struct usb_hcd *hcd);
26
27 struct ehci_ci_priv {
28         struct regulator *reg_vbus;
29 };
30
31 static int ehci_ci_portpower(struct usb_hcd *hcd, int portnum, bool enable)
32 {
33         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
34         struct ehci_ci_priv *priv = (struct ehci_ci_priv *)ehci->priv;
35         struct device *dev = hcd->self.controller;
36         struct ci_hdrc *ci = dev_get_drvdata(dev);
37         int ret = 0;
38         int port = HCS_N_PORTS(ehci->hcs_params);
39
40         if (priv->reg_vbus) {
41                 if (port > 1) {
42                         dev_warn(dev,
43                                 "Not support multi-port regulator control\n");
44                         return 0;
45                 }
46                 if (enable)
47                         ret = regulator_enable(priv->reg_vbus);
48                 else
49                         ret = regulator_disable(priv->reg_vbus);
50                 if (ret) {
51                         dev_err(dev,
52                                 "Failed to %s vbus regulator, ret=%d\n",
53                                 enable ? "enable" : "disable", ret);
54                         return ret;
55                 }
56         }
57
58         if (enable && (ci->platdata->phy_mode == USBPHY_INTERFACE_MODE_HSIC)) {
59                 /*
60                  * Marvell 28nm HSIC PHY requires forcing the port to HS mode.
61                  * As HSIC is always HS, this should be safe for others.
62                  */
63                 hw_port_test_set(ci, 5);
64                 hw_port_test_set(ci, 0);
65         }
66         return 0;
67 };
68
69 static int ehci_ci_reset(struct usb_hcd *hcd)
70 {
71         struct device *dev = hcd->self.controller;
72         struct ci_hdrc *ci = dev_get_drvdata(dev);
73         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
74         int ret;
75
76         ret = ehci_setup(hcd);
77         if (ret)
78                 return ret;
79
80         ehci->need_io_watchdog = 0;
81
82         if (ci->platdata->notify_event) {
83                 ret = ci->platdata->notify_event(ci,
84                                 CI_HDRC_CONTROLLER_RESET_EVENT);
85                 if (ret)
86                         return ret;
87         }
88
89         ci_platform_configure(ci);
90
91         return ret;
92 }
93
94 static const struct ehci_driver_overrides ehci_ci_overrides = {
95         .extra_priv_size = sizeof(struct ehci_ci_priv),
96         .port_power      = ehci_ci_portpower,
97         .reset           = ehci_ci_reset,
98 };
99
100 static irqreturn_t host_irq(struct ci_hdrc *ci)
101 {
102         return usb_hcd_irq(ci->irq, ci->hcd);
103 }
104
105 static int host_start(struct ci_hdrc *ci)
106 {
107         struct usb_hcd *hcd;
108         struct ehci_hcd *ehci;
109         struct ehci_ci_priv *priv;
110         int ret;
111
112         if (usb_disabled())
113                 return -ENODEV;
114
115         hcd = __usb_create_hcd(&ci_ehci_hc_driver, ci->dev->parent,
116                                ci->dev, dev_name(ci->dev), NULL);
117         if (!hcd)
118                 return -ENOMEM;
119
120         dev_set_drvdata(ci->dev, ci);
121         hcd->rsrc_start = ci->hw_bank.phys;
122         hcd->rsrc_len = ci->hw_bank.size;
123         hcd->regs = ci->hw_bank.abs;
124         hcd->has_tt = 1;
125
126         hcd->power_budget = ci->platdata->power_budget;
127         hcd->tpl_support = ci->platdata->tpl_support;
128         if (ci->phy || ci->usb_phy) {
129                 hcd->skip_phy_initialization = 1;
130                 if (ci->usb_phy)
131                         hcd->usb_phy = ci->usb_phy;
132         }
133
134         ehci = hcd_to_ehci(hcd);
135         ehci->caps = ci->hw_bank.cap;
136         ehci->has_hostpc = ci->hw_bank.lpm;
137         ehci->has_tdi_phy_lpm = ci->hw_bank.lpm;
138         ehci->imx28_write_fix = ci->imx28_write_fix;
139
140         priv = (struct ehci_ci_priv *)ehci->priv;
141         priv->reg_vbus = NULL;
142
143         if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci)) {
144                 if (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON) {
145                         ret = regulator_enable(ci->platdata->reg_vbus);
146                         if (ret) {
147                                 dev_err(ci->dev,
148                                 "Failed to enable vbus regulator, ret=%d\n",
149                                                                         ret);
150                                 goto put_hcd;
151                         }
152                 } else {
153                         priv->reg_vbus = ci->platdata->reg_vbus;
154                 }
155         }
156
157         if (ci->platdata->pins_host)
158                 pinctrl_select_state(ci->platdata->pctl,
159                                      ci->platdata->pins_host);
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                 if (ci->platdata->notify_event)
193                         ci->platdata->notify_event(ci,
194                                 CI_HDRC_CONTROLLER_STOPPED_EVENT);
195                 usb_remove_hcd(hcd);
196                 ci->role = CI_ROLE_END;
197                 synchronize_irq(ci->irq);
198                 usb_put_hcd(hcd);
199                 if (ci->platdata->reg_vbus && !ci_otg_is_fsm_mode(ci) &&
200                         (ci->platdata->flags & CI_HDRC_TURN_VBUS_EARLY_ON))
201                                 regulator_disable(ci->platdata->reg_vbus);
202         }
203         ci->hcd = NULL;
204         ci->otg.host = NULL;
205
206         if (ci->platdata->pins_host && ci->platdata->pins_default)
207                 pinctrl_select_state(ci->platdata->pctl,
208                                      ci->platdata->pins_default);
209 }
210
211
212 void ci_hdrc_host_destroy(struct ci_hdrc *ci)
213 {
214         if (ci->role == CI_ROLE_HOST && ci->hcd)
215                 host_stop(ci);
216 }
217
218 static int ci_ehci_bus_suspend(struct usb_hcd *hcd)
219 {
220         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
221         int port;
222         u32 tmp;
223
224         int ret = orig_bus_suspend(hcd);
225
226         if (ret)
227                 return ret;
228
229         port = HCS_N_PORTS(ehci->hcs_params);
230         while (port--) {
231                 u32 __iomem *reg = &ehci->regs->port_status[port];
232                 u32 portsc = ehci_readl(ehci, reg);
233
234                 if (portsc & PORT_CONNECT) {
235                         /*
236                          * For chipidea, the resume signal will be ended
237                          * automatically, so for remote wakeup case, the
238                          * usbcmd.rs may not be set before the resume has
239                          * ended if other resume paths consumes too much
240                          * time (~24ms), in that case, the SOF will not
241                          * send out within 3ms after resume ends, then the
242                          * high speed device will enter full speed mode.
243                          */
244
245                         tmp = ehci_readl(ehci, &ehci->regs->command);
246                         tmp |= CMD_RUN;
247                         ehci_writel(ehci, tmp, &ehci->regs->command);
248                         /*
249                          * It needs a short delay between set RS bit and PHCD.
250                          */
251                         usleep_range(150, 200);
252                         break;
253                 }
254         }
255
256         return 0;
257 }
258
259 int ci_hdrc_host_init(struct ci_hdrc *ci)
260 {
261         struct ci_role_driver *rdrv;
262
263         if (!hw_read(ci, CAP_DCCPARAMS, DCCPARAMS_HC))
264                 return -ENXIO;
265
266         rdrv = devm_kzalloc(ci->dev, sizeof(struct ci_role_driver), GFP_KERNEL);
267         if (!rdrv)
268                 return -ENOMEM;
269
270         rdrv->start     = host_start;
271         rdrv->stop      = host_stop;
272         rdrv->irq       = host_irq;
273         rdrv->name      = "host";
274         ci->roles[CI_ROLE_HOST] = rdrv;
275
276         return 0;
277 }
278
279 void ci_hdrc_host_driver_init(void)
280 {
281         ehci_init_driver(&ci_ehci_hc_driver, &ehci_ci_overrides);
282         orig_bus_suspend = ci_ehci_hc_driver.bus_suspend;
283         ci_ehci_hc_driver.bus_suspend = ci_ehci_bus_suspend;
284 }
This page took 0.046641 seconds and 4 git commands to generate.