]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/nvidia/tegra_ehci.c
Update DTS files from Linux v5.10
[FreeBSD/FreeBSD.git] / sys / arm / nvidia / tegra_ehci.c
1 /*-
2  * Copyright (c) 2016 Michal Meloun <mmel@FreeBSD.org>
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 /*
31  * EHCI driver for Tegra SoCs.
32  */
33 #include "opt_bus.h"
34 #include "opt_platform.h"
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/condvar.h>
42 #include <sys/rman.h>
43
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46
47 #include <dev/extres/clk/clk.h>
48 #include <dev/extres/hwreset/hwreset.h>
49 #include <dev/extres/phy/phy.h>
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #include <dev/usb/usb_busdma.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_controller.h>
57 #include <dev/usb/usb_bus.h>
58 #include <dev/usb/controller/ehci.h>
59 #include <dev/usb/controller/ehcireg.h>
60
61 #include "usbdevs.h"
62
63 #define TEGRA_EHCI_REG_OFF      0x100
64 #define TEGRA_EHCI_REG_SIZE     0x100
65
66 /* Compatible devices. */
67 #define TEGRA124_EHCI           1
68 #define TEGRA210_EHCI           2
69 static struct ofw_compat_data compat_data[] = {
70         {"nvidia,tegra124-ehci",        (uintptr_t)TEGRA124_EHCI},
71         {"nvidia,tegra210-ehci",        (uintptr_t)TEGRA210_EHCI},
72         {NULL,                  0},
73 };
74
75 struct tegra_ehci_softc {
76         ehci_softc_t    ehci_softc;
77         device_t        dev;
78         struct resource *ehci_mem_res;  /* EHCI core regs. */
79         struct resource *ehci_irq_res;  /* EHCI core IRQ. */
80         int             usb_alloc_called;
81         clk_t           clk;
82         phy_t           phy;
83         hwreset_t       reset;
84 };
85
86 static void
87 tegra_ehci_post_reset(struct ehci_softc *ehci_softc)
88 {
89         uint32_t usbmode;
90
91         /* Force HOST mode. */
92         usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_LPM);
93         usbmode &= ~EHCI_UM_CM;
94         usbmode |= EHCI_UM_CM_HOST;
95         device_printf(ehci_softc->sc_bus.bdev, "set host controller mode\n");
96         EOWRITE4(ehci_softc, EHCI_USBMODE_LPM, usbmode);
97 }
98
99 static int
100 tegra_ehci_probe(device_t dev)
101 {
102
103         if (!ofw_bus_status_okay(dev))
104                 return (ENXIO);
105
106         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
107                 device_set_desc(dev, "Nvidia Tegra EHCI controller");
108                 return (BUS_PROBE_DEFAULT);
109         }
110         return (ENXIO);
111 }
112
113 static int
114 tegra_ehci_detach(device_t dev)
115 {
116         struct tegra_ehci_softc *sc;
117         ehci_softc_t *esc;
118
119         sc = device_get_softc(dev);
120
121         esc = &sc->ehci_softc;
122         if (sc->clk != NULL)
123                 clk_release(sc->clk);
124         if (esc->sc_bus.bdev != NULL)
125                 device_delete_child(dev, esc->sc_bus.bdev);
126         if (esc->sc_flags & EHCI_SCFLG_DONEINIT)
127                 ehci_detach(esc);
128         if (esc->sc_intr_hdl != NULL)
129                 bus_teardown_intr(dev, esc->sc_irq_res,
130                     esc->sc_intr_hdl);
131         if (sc->ehci_irq_res != NULL)
132                 bus_release_resource(dev, SYS_RES_IRQ, 0,
133                     sc->ehci_irq_res);
134         if (sc->ehci_mem_res != NULL)
135                 bus_release_resource(dev, SYS_RES_MEMORY, 0,
136                     sc->ehci_mem_res);
137         if (sc->usb_alloc_called)
138                 usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc);
139
140         /* During module unload there are lots of children leftover. */
141         device_delete_children(dev);
142
143         return (0);
144 }
145
146 static int
147 tegra_ehci_attach(device_t dev)
148 {
149         struct tegra_ehci_softc *sc;
150         ehci_softc_t *esc;
151         int rv, rid;
152         uint64_t freq;
153         phandle_t node;
154
155         sc = device_get_softc(dev);
156         sc->dev = dev;
157         node = ofw_bus_get_node(dev);
158         esc = &sc->ehci_softc;
159
160         /* Allocate resources. */
161         rid = 0;
162         sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
163             RF_ACTIVE | RF_SHAREABLE);
164         if (sc->ehci_mem_res == NULL) {
165                 device_printf(dev, "Cannot allocate memory resources\n");
166                 rv = ENXIO;
167                 goto out;
168         }
169
170         rid = 0;
171         sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
172             RF_ACTIVE);
173         if (sc->ehci_irq_res == NULL) {
174                 device_printf(dev, "Cannot allocate IRQ resources\n");
175                 rv = ENXIO;
176                 goto out;
177         }
178
179         rv = hwreset_get_by_ofw_name(dev, 0, "usb", &sc->reset);
180         if (rv != 0) {
181                 device_printf(dev, "Cannot get reset\n");
182                 rv = ENXIO;
183                 goto out;
184         }
185
186         rv = phy_get_by_ofw_property(sc->dev, 0, "nvidia,phy", &sc->phy);
187         if (rv != 0) {
188                 device_printf(sc->dev, "Cannot get 'nvidia,phy' phy\n");
189                 rv = ENXIO;
190                 goto out;
191         }
192
193         rv = clk_get_by_ofw_index(sc->dev, 0, 0, &sc->clk);
194         if (rv != 0) {
195                 device_printf(dev, "Cannot get clock\n");
196                 goto out;
197         }
198
199         rv = clk_enable(sc->clk);
200         if (rv != 0) {
201                 device_printf(dev, "Cannot enable clock\n");
202                 goto out;
203         }
204
205         freq = 0;
206         rv = clk_get_freq(sc->clk, &freq);
207         if (rv != 0) {
208                 device_printf(dev, "Cannot get clock frequency\n");
209                 goto out;
210         }
211
212         rv = hwreset_deassert(sc->reset);
213         if (rv != 0) {
214                 device_printf(dev, "Cannot clear reset: %d\n", rv);
215                 rv = ENXIO;
216                 goto out;
217         }
218
219         rv = phy_enable(sc->phy);
220         if (rv != 0) {
221                 device_printf(dev, "Cannot enable phy: %d\n", rv);
222                 goto out;
223         }
224
225         /* Fill data for EHCI driver. */
226         esc->sc_vendor_get_port_speed = ehci_get_port_speed_hostc;
227         esc->sc_vendor_post_reset = tegra_ehci_post_reset;
228         esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res);
229         esc->sc_bus.parent = dev;
230         esc->sc_bus.devices = esc->sc_devices;
231         esc->sc_bus.devices_max = EHCI_MAX_DEVICES;
232         esc->sc_bus.dma_bits = 32;
233
234         /* Allocate all DMA memory. */
235         rv = usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev),
236             &ehci_iterate_hw_softc);
237         sc->usb_alloc_called = 1;
238         if (rv != 0) {
239                 device_printf(dev, "usb_bus_mem_alloc_all() failed\n");
240                 rv = ENOMEM;
241                 goto out;
242         }
243
244         /*
245          * Set handle to USB related registers subregion used by
246          * generic EHCI driver.
247          */
248         rv = bus_space_subregion(esc->sc_io_tag,
249             rman_get_bushandle(sc->ehci_mem_res),
250             TEGRA_EHCI_REG_OFF, TEGRA_EHCI_REG_SIZE, &esc->sc_io_hdl);
251         if (rv != 0) {
252                 device_printf(dev, "Could not create USB memory subregion\n");
253                 rv = ENXIO;
254                 goto out;
255         }
256
257         /* Setup interrupt handler. */
258         rv = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
259             NULL, (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl);
260         if (rv != 0) {
261                 device_printf(dev, "Could not setup IRQ\n");
262                 goto out;
263         }
264
265         /* Add USB bus device. */
266         esc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
267         if (esc->sc_bus.bdev == NULL) {
268                 device_printf(dev, "Could not add USB device\n");
269                 goto out;
270         }
271         device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus);
272
273         esc->sc_id_vendor = USB_VENDOR_FREESCALE;
274         strlcpy(esc->sc_vendor, "Nvidia", sizeof(esc->sc_vendor));
275
276         /* Set flags that affect ehci_init() behavior. */
277         esc->sc_flags |= EHCI_SCFLG_TT;
278         esc->sc_flags |= EHCI_SCFLG_NORESTERM;
279         rv = ehci_init(esc);
280         if (rv != 0) {
281                 device_printf(dev, "USB init failed: %d\n",
282                     rv);
283                 goto out;
284         }
285         esc->sc_flags |= EHCI_SCFLG_DONEINIT;
286
287         /* Probe the bus. */
288         rv = device_probe_and_attach(esc->sc_bus.bdev);
289         if (rv != 0) {
290                 device_printf(dev,
291                     "device_probe_and_attach() failed\n");
292                 goto out;
293         }
294         return (0);
295
296 out:
297         tegra_ehci_detach(dev);
298         return (rv);
299 }
300
301 static device_method_t ehci_methods[] = {
302         /* Device interface */
303         DEVMETHOD(device_probe, tegra_ehci_probe),
304         DEVMETHOD(device_attach, tegra_ehci_attach),
305         DEVMETHOD(device_detach, tegra_ehci_detach),
306         DEVMETHOD(device_suspend, bus_generic_suspend),
307         DEVMETHOD(device_resume, bus_generic_resume),
308         DEVMETHOD(device_shutdown, bus_generic_shutdown),
309
310         /* Bus interface */
311         DEVMETHOD(bus_print_child, bus_generic_print_child),
312
313         DEVMETHOD_END
314 };
315
316 static devclass_t ehci_devclass;
317 static DEFINE_CLASS_0(ehci, ehci_driver, ehci_methods,
318     sizeof(struct tegra_ehci_softc));
319 DRIVER_MODULE(tegra_ehci, simplebus, ehci_driver, ehci_devclass, NULL, NULL);
320 MODULE_DEPEND(tegra_ehci, usb, 1, 1, 1);