xref: /freebsd/sys/dev/usb/controller/generic_xhci_fdt.c (revision ca48e43ba9ee73a07cdbad8365117793b01273bb)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2015 Semihalf.
5  * Copyright (c) 2015 Stormshield.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include "opt_bus.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/condvar.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 
39 #include <dev/usb/usb.h>
40 #include <dev/usb/usbdi.h>
41 
42 #include <dev/usb/usb_core.h>
43 #include <dev/usb/usb_busdma.h>
44 #include <dev/usb/usb_process.h>
45 
46 #include <dev/usb/usb_controller.h>
47 #include <dev/usb/usb_bus.h>
48 #include <dev/usb/controller/xhci.h>
49 
50 #include <dev/ofw/ofw_bus.h>
51 #include <dev/ofw/ofw_bus_subr.h>
52 
53 #include <dev/phy/phy.h>
54 
55 #include "generic_xhci.h"
56 
57 /* Flags for the OFW compat data table */
58 #define	XHCI_FDT_MATCH		0x01
59 #define	XHCI_FDT_32BIT_DMA	0x02	/* Controller needs 32-bit DMA */
60 
61 static struct ofw_compat_data compat_data[] = {
62 	{"marvell,armada-380-xhci",	XHCI_FDT_MATCH},
63 	{"marvell,armada3700-xhci",	XHCI_FDT_MATCH},
64 	{"marvell,armada-8k-xhci",	XHCI_FDT_MATCH},
65 	{"generic-xhci",		XHCI_FDT_MATCH},
66 	{NULL,				0}
67 };
68 
69 static int
generic_xhci_fdt_probe(device_t dev)70 generic_xhci_fdt_probe(device_t dev)
71 {
72 
73 	if (!ofw_bus_status_okay(dev))
74 		return (ENXIO);
75 
76 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
77 		return (ENXIO);
78 
79 	device_set_desc(dev, XHCI_HC_DEVSTR);
80 
81 	return (BUS_PROBE_DEFAULT);
82 }
83 
84 static int
generic_xhci_fdt_attach(device_t dev)85 generic_xhci_fdt_attach(device_t dev)
86 {
87 	struct xhci_softc *sc = device_get_softc(dev);
88 	phandle_t node;
89 	phy_t phy;
90 	int flags;
91 
92 	node = ofw_bus_get_node(dev);
93 	if (phy_get_by_ofw_property(dev, node, "usb-phy", &phy) == 0)
94 		if (phy_enable(phy) != 0)
95 			device_printf(dev, "Cannot enable phy\n");
96 
97 	flags = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
98 	if ((flags & XHCI_FDT_32BIT_DMA) != 0)
99 		sc->sc_quirks |= XHCI_QUIRK_DMA_32B;
100 
101 	return (generic_xhci_attach(dev));
102 }
103 
104 static int
generic_xhci_fdt_detach(device_t dev)105 generic_xhci_fdt_detach(device_t dev)
106 {
107 	phandle_t node;
108 	phy_t phy;
109 	int err;
110 
111 	err = generic_xhci_detach(dev);
112 	if (err != 0)
113 		return (err);
114 
115 	node = ofw_bus_get_node(dev);
116 	if (phy_get_by_ofw_property(dev, node, "usb-phy", &phy) == 0)
117 		phy_release(phy);
118 
119 	return (0);
120 }
121 
122 static device_method_t xhci_fdt_methods[] = {
123 	/* Device interface */
124 	DEVMETHOD(device_probe, generic_xhci_fdt_probe),
125 	DEVMETHOD(device_attach, generic_xhci_fdt_attach),
126 	DEVMETHOD(device_detach, generic_xhci_fdt_detach),
127 
128 	DEVMETHOD_END
129 };
130 
131 DEFINE_CLASS_1(xhci, xhci_fdt_driver, xhci_fdt_methods,
132     sizeof(struct xhci_softc), generic_xhci_driver);
133 
134 DRIVER_MODULE(xhci, simplebus, xhci_fdt_driver, 0, 0);
135 MODULE_DEPEND(xhci, usb, 1, 1, 1);
136