]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/samsung/exynos/exynos5_ehci.c
Change POSIX compliance level for visibility of strerror_l(3).
[FreeBSD/FreeBSD.git] / sys / arm / samsung / exynos / exynos5_ehci.c
1 /*-
2  * Copyright (c) 2013-2014 Ruslan Bukin <br@bsdpad.com>
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 AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, 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
27 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
29 #else
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_bus.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/module.h>
39 #include <sys/bus.h>
40 #include <sys/condvar.h>
41 #include <sys/rman.h>
42 #include <sys/gpio.h>
43
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usb_busdma.h>
50 #include <dev/usb/usb_process.h>
51 #include <dev/usb/usb_controller.h>
52 #include <dev/usb/usb_bus.h>
53 #include <dev/usb/controller/ehci.h>
54 #include <dev/usb/controller/ehcireg.h>
55
56 #include <machine/bus.h>
57 #include <machine/resource.h>
58
59 #include <arm/samsung/exynos/exynos5_common.h>
60 #include <arm/samsung/exynos/exynos5_pmu.h>
61
62 #include "gpio_if.h"
63
64 #include "opt_platform.h"
65 #endif
66
67 /* GPIO control */
68 #define GPIO_OUTPUT     1
69 #define GPIO_INPUT      0
70 #define PIN_USB         161
71
72 /* SYSREG */
73 #define EXYNOS5_SYSREG_USB2_PHY 0x0
74 #define USB2_MODE_HOST          0x1
75
76 /* USB HOST */
77 #define HOST_CTRL_CLK_24MHZ     (5 << 16)
78 #define HOST_CTRL_CLK_MASK      (7 << 16)
79 #define HOST_CTRL_SIDDQ         (1 << 6)
80 #define HOST_CTRL_SLEEP         (1 << 5)
81 #define HOST_CTRL_SUSPEND       (1 << 4)
82 #define HOST_CTRL_RESET_LINK    (1 << 1)
83 #define HOST_CTRL_RESET_PHY     (1 << 0)
84 #define HOST_CTRL_RESET_PHY_ALL (1U << 31)
85
86 /* Forward declarations */
87 static int      exynos_ehci_attach(device_t dev);
88 static int      exynos_ehci_detach(device_t dev);
89 static int      exynos_ehci_probe(device_t dev);
90
91 struct exynos_ehci_softc {
92         device_t                dev;
93         ehci_softc_t            base;
94         struct resource         *res[4];
95         bus_space_tag_t         host_bst;
96         bus_space_tag_t         sysreg_bst;
97         bus_space_handle_t      host_bsh;
98         bus_space_handle_t      sysreg_bsh;
99
100 };
101
102 static struct resource_spec exynos_ehci_spec[] = {
103         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
104         { SYS_RES_MEMORY,       1,      RF_ACTIVE },
105         { SYS_RES_MEMORY,       2,      RF_ACTIVE },
106         { SYS_RES_IRQ,          0,      RF_ACTIVE },
107         { -1, 0 }
108 };
109
110 static device_method_t ehci_methods[] = {
111         /* Device interface */
112         DEVMETHOD(device_probe, exynos_ehci_probe),
113         DEVMETHOD(device_attach, exynos_ehci_attach),
114         DEVMETHOD(device_detach, exynos_ehci_detach),
115         DEVMETHOD(device_suspend, bus_generic_suspend),
116         DEVMETHOD(device_resume, bus_generic_resume),
117         DEVMETHOD(device_shutdown, bus_generic_shutdown),
118
119         /* Bus interface */
120         DEVMETHOD(bus_print_child, bus_generic_print_child),
121         { 0, 0 }
122 };
123
124 /* kobj_class definition */
125 static driver_t ehci_driver = {
126         "ehci",
127         ehci_methods,
128         sizeof(struct exynos_ehci_softc)
129 };
130
131 static devclass_t ehci_devclass;
132
133 DRIVER_MODULE(exynos_ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
134 MODULE_DEPEND(exynos_ehci, usb, 1, 1, 1);
135
136 /*
137  * Public methods
138  */
139 static int
140 exynos_ehci_probe(device_t dev)
141 {
142
143         if (!ofw_bus_status_okay(dev))
144                 return (ENXIO);
145
146         if (ofw_bus_is_compatible(dev, "exynos,usb-ehci") == 0)
147                 return (ENXIO);
148
149         device_set_desc(dev, "Exynos integrated USB controller");
150         return (BUS_PROBE_DEFAULT);
151 }
152
153 static int
154 gpio_ctrl(struct exynos_ehci_softc *esc, int dir, int power)
155 {
156         device_t gpio_dev;
157
158         /* Get the GPIO device, we need this to give power to USB */
159         gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
160         if (gpio_dev == NULL) {
161                 device_printf(esc->dev, "cant find gpio_dev\n");
162                 return (1);
163         }
164
165         if (power)
166                 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_HIGH);
167         else
168                 GPIO_PIN_SET(gpio_dev, PIN_USB, GPIO_PIN_LOW);
169
170         if (dir)
171                 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_OUTPUT);
172         else
173                 GPIO_PIN_SETFLAGS(gpio_dev, PIN_USB, GPIO_PIN_INPUT);
174
175         return (0);
176 }
177
178 static int
179 reset_hsic_hub(struct exynos_ehci_softc *esc, phandle_t hub)
180 {
181         device_t gpio_dev;
182         pcell_t pin;
183
184         /* TODO: check that hub is compatible with "smsc,usb3503" */
185         if (!OF_hasprop(hub, "freebsd,reset-gpio")) {
186                 return (1);
187         }
188
189         if (OF_getencprop(hub, "freebsd,reset-gpio", &pin, sizeof(pin)) < 0) {
190                 device_printf(esc->dev,
191                     "failed to decode reset GPIO pin number for HSIC hub\n");
192                 return (1);
193         }
194
195         /* Get the GPIO device, we need this to give power to USB */
196         gpio_dev = devclass_get_device(devclass_find("gpio"), 0);
197         if (gpio_dev == NULL) {
198                 device_printf(esc->dev, "Cant find gpio device\n");
199                 return (1);
200         }
201
202         GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_LOW);
203         DELAY(100);
204         GPIO_PIN_SET(gpio_dev, pin, GPIO_PIN_HIGH);
205
206         return (0);
207 }
208
209 static int
210 phy_init(struct exynos_ehci_softc *esc)
211 {
212         int reg;
213         phandle_t hub;
214
215         gpio_ctrl(esc, GPIO_INPUT, 1);
216
217         /* set USB HOST mode */
218         bus_space_write_4(esc->sysreg_bst, esc->sysreg_bsh,
219             EXYNOS5_SYSREG_USB2_PHY, USB2_MODE_HOST);
220
221         /* Power ON phy */
222         usb2_phy_power_on();
223
224         reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
225         reg &= ~(HOST_CTRL_CLK_MASK |
226             HOST_CTRL_RESET_PHY |
227             HOST_CTRL_RESET_PHY_ALL |
228             HOST_CTRL_SIDDQ |
229             HOST_CTRL_SUSPEND |
230             HOST_CTRL_SLEEP);
231
232         reg |= (HOST_CTRL_CLK_24MHZ |
233             HOST_CTRL_RESET_LINK);
234         bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
235
236         DELAY(10);
237
238         reg = bus_space_read_4(esc->host_bst, esc->host_bsh, 0x0);
239         reg &= ~(HOST_CTRL_RESET_LINK);
240         bus_space_write_4(esc->host_bst, esc->host_bsh, 0x0, reg);
241
242         if ((hub = OF_finddevice("/hsichub")) != -1) {
243                 reset_hsic_hub(esc, hub);
244         }
245
246         gpio_ctrl(esc, GPIO_OUTPUT, 1);
247
248         return (0);
249 }
250
251 static int
252 exynos_ehci_attach(device_t dev)
253 {
254         struct exynos_ehci_softc *esc;
255         ehci_softc_t *sc;
256         bus_space_handle_t bsh;
257         int err;
258
259         esc = device_get_softc(dev);
260         esc->dev = dev;
261         sc = &esc->base;
262         sc->sc_bus.parent = dev;
263         sc->sc_bus.devices = sc->sc_devices;
264         sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
265         sc->sc_bus.dma_bits = 32;
266
267         if (bus_alloc_resources(dev, exynos_ehci_spec, esc->res)) {
268                 device_printf(dev, "could not allocate resources\n");
269                 return (ENXIO);
270         }
271
272         /* EHCI registers */
273         sc->sc_io_tag = rman_get_bustag(esc->res[0]);
274         bsh = rman_get_bushandle(esc->res[0]);
275         sc->sc_io_size = rman_get_size(esc->res[0]);
276
277         /* EHCI HOST ctrl registers */
278         esc->host_bst = rman_get_bustag(esc->res[1]);
279         esc->host_bsh = rman_get_bushandle(esc->res[1]);
280
281         /* SYSREG */
282         esc->sysreg_bst = rman_get_bustag(esc->res[2]);
283         esc->sysreg_bsh = rman_get_bushandle(esc->res[2]);
284
285         /* get all DMA memory */
286         if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
287                 &ehci_iterate_hw_softc))
288                 return (ENXIO);
289
290         /*
291          * Set handle to USB related registers subregion used by
292          * generic EHCI driver.
293          */
294         err = bus_space_subregion(sc->sc_io_tag, bsh, 0x0,
295             sc->sc_io_size, &sc->sc_io_hdl);
296         if (err != 0)
297                 return (ENXIO);
298
299         phy_init(esc);
300
301         /* Setup interrupt handler */
302         err = bus_setup_intr(dev, esc->res[3], INTR_TYPE_BIO | INTR_MPSAFE,
303             NULL, (driver_intr_t *)ehci_interrupt, sc,
304             &sc->sc_intr_hdl);
305         if (err) {
306                 device_printf(dev, "Could not setup irq, "
307                     "%d\n", err);
308                 return (1);
309         }
310
311         /* Add USB device */
312         sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
313         if (!sc->sc_bus.bdev) {
314                 device_printf(dev, "Could not add USB device\n");
315                 err = bus_teardown_intr(dev, esc->res[3],
316                     sc->sc_intr_hdl);
317                 if (err)
318                         device_printf(dev, "Could not tear down irq,"
319                             " %d\n", err);
320                 return (1);
321         }
322         device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
323
324         strlcpy(sc->sc_vendor, "Samsung", sizeof(sc->sc_vendor));
325
326         err = ehci_init(sc);
327         if (!err) {
328                 sc->sc_flags |= EHCI_SCFLG_DONEINIT;
329                 err = device_probe_and_attach(sc->sc_bus.bdev);
330         } else {
331                 device_printf(dev, "USB init failed err=%d\n", err);
332
333                 device_delete_child(dev, sc->sc_bus.bdev);
334                 sc->sc_bus.bdev = NULL;
335
336                 err = bus_teardown_intr(dev, esc->res[3],
337                     sc->sc_intr_hdl);
338                 if (err)
339                         device_printf(dev, "Could not tear down irq,"
340                             " %d\n", err);
341                 return (1);
342         }
343         return (0);
344 }
345
346 static int
347 exynos_ehci_detach(device_t dev)
348 {
349         struct exynos_ehci_softc *esc;
350         ehci_softc_t *sc;
351         int err;
352
353         esc = device_get_softc(dev);
354         sc = &esc->base;
355
356         if (sc->sc_flags & EHCI_SCFLG_DONEINIT)
357                 return (0);
358
359         /*
360          * only call ehci_detach() after ehci_init()
361          */
362         if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
363                 ehci_detach(sc);
364                 sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
365         }
366
367         /*
368          * Disable interrupts that might have been switched on in
369          * ehci_init.
370          */
371         if (sc->sc_io_tag && sc->sc_io_hdl)
372                 bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
373                     EHCI_USBINTR, 0);
374
375         if (esc->res[3] && sc->sc_intr_hdl) {
376                 err = bus_teardown_intr(dev, esc->res[3],
377                     sc->sc_intr_hdl);
378                 if (err) {
379                         device_printf(dev, "Could not tear down irq,"
380                             " %d\n", err);
381                         return (err);
382                 }
383                 sc->sc_intr_hdl = NULL;
384         }
385
386         if (sc->sc_bus.bdev) {
387                 device_delete_child(dev, sc->sc_bus.bdev);
388                 sc->sc_bus.bdev = NULL;
389         }
390
391         /* During module unload there are lots of children leftover */
392         device_delete_children(dev);
393
394         bus_release_resources(dev, exynos_ehci_spec, esc->res);
395
396         return (0);
397 }