xref: /freebsd/sys/dev/ofw/ofw_pci.c (revision 315ee00f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Alstom Group.
5  * Copyright (c) 2021 Semihalf.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/rman.h>
35 
36 #include <dev/pci/pcireg.h>
37 #include <dev/pci/pcivar.h>
38 #include <dev/pci/pci_private.h>
39 
40 #include <dev/ofw/openfirm.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 
44 #include "pcib_if.h"
45 #include "pci_if.h"
46 
47 static int	ofw_pci_probe(device_t);
48 static const struct ofw_bus_devinfo* pci_ofw_get_devinfo(device_t, device_t);
49 
50 static device_method_t ofw_pci_methods[] = {
51 	DEVMETHOD(device_probe,		ofw_pci_probe),
52 
53 	/* ofw_bus interface */
54 	DEVMETHOD(ofw_bus_get_devinfo,	pci_ofw_get_devinfo),
55 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
56 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
57 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
58 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
59 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
60 
61 	DEVMETHOD_END
62 };
63 
64 DEFINE_CLASS_1(pci, ofw_pci_driver, ofw_pci_methods, sizeof(struct pci_softc),
65     pci_driver);
66 DRIVER_MODULE(ofw_pci, pcib, ofw_pci_driver, 0, 0);
67 MODULE_DEPEND(ofw_pci, simplebus, 1, 1, 1);
68 MODULE_DEPEND(ofw_pci, pci, 1, 1, 1);
69 MODULE_VERSION(ofw_pci, 1);
70 
71 static int
72 ofw_pci_probe(device_t dev)
73 {
74 	device_t parent;
75 
76 	parent = device_get_parent(dev);
77 	if (ofw_bus_get_node(parent) == -1)
78 		return (ENXIO);
79 
80 	device_set_desc(dev, "OFW PCI bus");
81 	return (BUS_PROBE_DEFAULT);
82 }
83 
84 /* Pass the request up to our parent. */
85 static const struct ofw_bus_devinfo*
86 pci_ofw_get_devinfo(device_t bus, device_t dev)
87 {
88 
89 	return OFW_BUS_GET_DEVINFO(device_get_parent(bus), dev);
90 }
91