xref: /freebsd/sys/dev/proto/proto_bus_pci.c (revision 44096ebd22ddd0081a357011714eff8963614b65)
1 /*-
2  * Copyright (c) 2014, 2015 Marcel Moolenaar
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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/bus.h>
29 #include <sys/conf.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <machine/bus.h>
33 #include <sys/rman.h>
34 #include <machine/resource.h>
35 
36 #include <dev/pci/pcireg.h>
37 #include <dev/pci/pcivar.h>
38 
39 #include <dev/proto/proto.h>
40 
41 static int proto_pci_probe(device_t dev);
42 static int proto_pci_attach(device_t dev);
43 
44 static device_method_t proto_pci_methods[] = {
45 	/* Device interface */
46 	DEVMETHOD(device_probe,		proto_pci_probe),
47 	DEVMETHOD(device_attach,	proto_pci_attach),
48 	DEVMETHOD(device_detach,	proto_detach),
49 	DEVMETHOD_END
50 };
51 
52 static driver_t proto_pci_driver = {
53 	proto_driver_name,
54 	proto_pci_methods,
55 	sizeof(struct proto_softc),
56 };
57 
58 static char proto_pci_prefix[] = "pci";
59 static char **proto_pci_devnames;
60 
61 static int
62 proto_pci_probe(device_t dev)
63 {
64 	if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
65 		return (ENXIO);
66 
67 	device_set_descf(dev, "%s%d:%d:%d:%d", proto_pci_prefix,
68 	    pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),
69 	    pci_get_function(dev));
70 	return (proto_probe(dev, proto_pci_prefix, &proto_pci_devnames));
71 }
72 
73 static int
74 proto_pci_attach(device_t dev)
75 {
76 	struct proto_softc *sc;
77 	struct resource *res;
78 	uint32_t val;
79 	int bar, rid, type;
80 
81 	sc = device_get_softc(dev);
82 
83 	proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL);
84 	proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
85 
86 	for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) {
87 		rid = PCIR_BAR(bar);
88 		val = pci_read_config(dev, rid, 4);
89 		type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY;
90 		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
91 		if (res == NULL)
92 			continue;
93 		proto_add_resource(sc, type, rid, res);
94 		if (type == SYS_RES_IOPORT)
95 			continue;
96 		/* Skip over adjacent BAR for 64-bit memory BARs. */
97 		if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64)
98 			bar++;
99 	}
100 
101 	rid = 0;
102 	type = SYS_RES_IRQ;
103 	res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE);
104 	if (res != NULL)
105 		proto_add_resource(sc, type, rid, res);
106 	return (proto_attach(dev));
107 }
108 
109 DRIVER_MODULE(proto, pci, proto_pci_driver, NULL, NULL);
110