]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/allwinner/aw_usbphy.c
Fix memory disclosure vulnerability in libalias.
[FreeBSD/FreeBSD.git] / sys / arm / allwinner / aw_usbphy.c
1 /*-
2  * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 /*
30  * Allwinner USB PHY
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/bus.h>
39 #include <sys/rman.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/gpio.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/gpio/gpiobusvar.h>
47
48 #include <dev/extres/clk/clk.h>
49 #include <dev/extres/hwreset/hwreset.h>
50 #include <dev/extres/regulator/regulator.h>
51 #include <dev/extres/phy/phy.h>
52
53 #include "phynode_if.h"
54
55 #define USBPHY_NPHYS    4
56
57 static struct ofw_compat_data compat_data[] = {
58         { "allwinner,sun4i-a10-usb-phy",        1 },
59         { "allwinner,sun5i-a13-usb-phy",        1 },
60         { "allwinner,sun6i-a31-usb-phy",        1 },
61         { "allwinner,sun7i-a20-usb-phy",        1 },
62         { "allwinner,sun8i-a83t-usb-phy",       1 },
63         { "allwinner,sun8i-h3-usb-phy",         1 },
64         { NULL,                                 0 }
65 };
66
67 struct awusbphy_softc {
68         regulator_t             reg[USBPHY_NPHYS];
69         gpio_pin_t              id_det_pin;
70         int                     id_det_valid;
71         gpio_pin_t              vbus_det_pin;
72         int                     vbus_det_valid;
73 };
74
75  /* Phy class and methods. */
76 static int awusbphy_phy_enable(struct phynode *phy, bool enable);
77 static phynode_method_t awusbphy_phynode_methods[] = {
78         PHYNODEMETHOD(phynode_enable, awusbphy_phy_enable),
79
80         PHYNODEMETHOD_END
81 };
82 DEFINE_CLASS_1(awusbphy_phynode, awusbphy_phynode_class, awusbphy_phynode_methods,
83     0, phynode_class);
84
85 static int
86 awusbphy_init(device_t dev)
87 {
88         struct awusbphy_softc *sc;
89         phandle_t node;
90         char pname[20];
91         int error, off;
92         regulator_t reg;
93         hwreset_t rst;
94         clk_t clk;
95
96         sc = device_get_softc(dev);
97         node = ofw_bus_get_node(dev);
98
99         /* Enable clocks */
100         for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
101                 error = clk_enable(clk);
102                 if (error != 0) {
103                         device_printf(dev, "couldn't enable clock %s\n",
104                             clk_get_name(clk));
105                         return (error);
106                 }
107         }
108
109         /* De-assert resets */
110         for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
111                 error = hwreset_deassert(rst);
112                 if (error != 0) {
113                         device_printf(dev, "couldn't de-assert reset %d\n",
114                             off);
115                         return (error);
116                 }
117         }
118
119         /* Get regulators */
120         for (off = 0; off < USBPHY_NPHYS; off++) {
121                 snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off);
122                 if (regulator_get_by_ofw_property(dev, 0, pname, &reg) == 0)
123                         sc->reg[off] = reg;
124         }
125
126         /* Get GPIOs */
127         error = gpio_pin_get_by_ofw_property(dev, node, "usb0_id_det-gpios",
128             &sc->id_det_pin);
129         if (error == 0)
130                 sc->id_det_valid = 1;
131         error = gpio_pin_get_by_ofw_property(dev, node, "usb0_vbus_det-gpios",
132             &sc->vbus_det_pin);
133         if (error == 0)
134                 sc->vbus_det_valid = 1;
135
136         return (0);
137 }
138
139 static int
140 awusbphy_vbus_detect(device_t dev, int *val)
141 {
142         struct awusbphy_softc *sc;
143         bool active;
144         int error;
145
146         sc = device_get_softc(dev);
147
148         if (sc->vbus_det_valid) {
149                 error = gpio_pin_is_active(sc->vbus_det_pin, &active);
150                 if (error != 0)
151                         return (error);
152                 *val = active;
153                 return (0);
154         }
155
156         *val = 1;
157         return (0);
158 }
159
160 static int
161 awusbphy_phy_enable(struct phynode *phynode, bool enable)
162 {
163         device_t dev;
164         intptr_t phy;
165         struct awusbphy_softc *sc;
166         regulator_t reg;
167         int error, vbus_det;
168
169         dev = phynode_get_device(phynode);
170         phy = phynode_get_id(phynode);
171         sc = device_get_softc(dev);
172
173         if (phy < 0 || phy >= USBPHY_NPHYS)
174                 return (ERANGE);
175
176         sc = device_get_softc(dev);
177
178         /* Regulators are optional. If not found, return success. */
179         reg = sc->reg[phy];
180         if (reg == NULL)
181                 return (0);
182
183         if (enable) {
184                 /* If an external vbus is detected, do not enable phy 0 */
185                 if (phy == 0) {
186                         error = awusbphy_vbus_detect(dev, &vbus_det);
187                         if (error == 0 && vbus_det == 1)
188                                 return (0);
189                 } else
190                         error = 0;
191                 if (error == 0)
192                         error = regulator_enable(reg);
193         } else
194                 error = regulator_disable(reg);
195         if (error != 0) {
196                 device_printf(dev,
197                     "couldn't %s regulator for phy %jd\n",
198                     enable ? "enable" : "disable", (intmax_t)phy);
199                 return (error);
200         }
201
202         return (0);
203 }
204
205 static int
206 awusbphy_probe(device_t dev)
207 {
208         if (!ofw_bus_status_okay(dev))
209                 return (ENXIO);
210
211         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
212                 return (ENXIO);
213
214         device_set_desc(dev, "Allwinner USB PHY");
215         return (BUS_PROBE_DEFAULT);
216 }
217
218 static int
219 awusbphy_attach(device_t dev)
220 {
221         int error;
222         struct phynode *phynode;
223         struct phynode_init_def phy_init;
224         int i;
225
226         error = awusbphy_init(dev);
227         if (error) {
228                 device_printf(dev, "failed to initialize USB PHY, error %d\n",
229                     error);
230                 return (error);
231         }
232
233         /* Create and register phys. */
234         for (i = 0; i < USBPHY_NPHYS; i++) {
235                 bzero(&phy_init, sizeof(phy_init));
236                 phy_init.id = i;
237                 phy_init.ofw_node = ofw_bus_get_node(dev);
238                 phynode = phynode_create(dev, &awusbphy_phynode_class,
239                     &phy_init);
240                 if (phynode == NULL) {
241                         device_printf(dev, "failed to create USB PHY\n");
242                         return (ENXIO);
243                 }
244                 if (phynode_register(phynode) == NULL) {
245                         device_printf(dev, "failed to create USB PHY\n");
246                         return (ENXIO);
247                 }
248         }
249
250         return (error);
251 }
252
253 static device_method_t awusbphy_methods[] = {
254         /* Device interface */
255         DEVMETHOD(device_probe,         awusbphy_probe),
256         DEVMETHOD(device_attach,        awusbphy_attach),
257
258         DEVMETHOD_END
259 };
260
261 static driver_t awusbphy_driver = {
262         "awusbphy",
263         awusbphy_methods,
264         sizeof(struct awusbphy_softc)
265 };
266
267 static devclass_t awusbphy_devclass;
268
269 EARLY_DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass,
270     0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
271 MODULE_VERSION(awusbphy, 1);