1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <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/bus.h>
38 #include <sys/condvar.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
41 
42 #include <dev/usb/usb.h>
43 #include <dev/usb/usbdi.h>
44 
45 #include <dev/usb/usb_core.h>
46 #include <dev/usb/usb_busdma.h>
47 #include <dev/usb/usb_process.h>
48 
49 #include <dev/usb/usb_controller.h>
50 #include <dev/usb/usb_bus.h>
51 #include <dev/usb/controller/xhci.h>
52 
53 #include <dev/ofw/ofw_bus.h>
54 #include <dev/ofw/ofw_bus_subr.h>
55 
56 #include <dev/extres/phy/phy.h>
57 
58 #include "generic_xhci.h"
59 
60 static struct ofw_compat_data compat_data[] = {
61 	{"marvell,armada-380-xhci",	true},
62 	{"marvell,armada3700-xhci",	true},
63 	{"marvell,armada-8k-xhci",	true},
64 	{"generic-xhci",		true},
65 	{NULL,				false}
66 };
67 
68 static int
69 generic_xhci_fdt_probe(device_t dev)
70 {
71 
72 	if (!ofw_bus_status_okay(dev))
73 		return (ENXIO);
74 
75 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
76 		return (ENXIO);
77 
78 	device_set_desc(dev, XHCI_HC_DEVSTR);
79 
80 	return (BUS_PROBE_DEFAULT);
81 }
82 
83 static int
84 generic_xhci_fdt_attach(device_t dev)
85 {
86 	phandle_t node;
87 	phy_t phy;
88 
89 	node = ofw_bus_get_node(dev);
90 	if (phy_get_by_ofw_property(dev, node, "usb-phy", &phy) == 0)
91 		if (phy_enable(phy) != 0)
92 			device_printf(dev, "Cannot enable phy\n");
93 
94 	return (generic_xhci_attach(dev));
95 }
96 
97 static int
98 generic_xhci_fdt_detach(device_t dev)
99 {
100 	phandle_t node;
101 	phy_t phy;
102 	int err;
103 
104 	err = generic_xhci_detach(dev);
105 	if (err != 0)
106 		return (err);
107 
108 	node = ofw_bus_get_node(dev);
109 	if (phy_get_by_ofw_property(dev, node, "usb-phy", &phy) == 0)
110 		phy_release(phy);
111 
112 	return (0);
113 }
114 
115 static device_method_t xhci_fdt_methods[] = {
116 	/* Device interface */
117 	DEVMETHOD(device_probe, generic_xhci_fdt_probe),
118 	DEVMETHOD(device_attach, generic_xhci_fdt_attach),
119 	DEVMETHOD(device_detach, generic_xhci_fdt_detach),
120 
121 	DEVMETHOD_END
122 };
123 
124 DEFINE_CLASS_1(xhci, xhci_fdt_driver, xhci_fdt_methods,
125     sizeof(struct xhci_softc), generic_xhci_driver);
126 
127 DRIVER_MODULE(xhci, simplebus, xhci_fdt_driver, 0, 0);
128 MODULE_DEPEND(xhci, usb, 1, 1, 1);
129