xref: /freebsd/sys/dev/proto/proto_bus_pci.c (revision 67fb10f30cc61e1e29301db356dc81f4d30c3615)
1*67fb10f3SMarcel Moolenaar /*-
2*67fb10f3SMarcel Moolenaar  * Copyright (c) 2014 Marcel Moolenaar
3*67fb10f3SMarcel Moolenaar  * All rights reserved.
4*67fb10f3SMarcel Moolenaar  *
5*67fb10f3SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
6*67fb10f3SMarcel Moolenaar  * modification, are permitted provided that the following conditions
7*67fb10f3SMarcel Moolenaar  * are met:
8*67fb10f3SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
9*67fb10f3SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
10*67fb10f3SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
11*67fb10f3SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
12*67fb10f3SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
13*67fb10f3SMarcel Moolenaar  *
14*67fb10f3SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15*67fb10f3SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16*67fb10f3SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17*67fb10f3SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18*67fb10f3SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19*67fb10f3SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20*67fb10f3SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21*67fb10f3SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22*67fb10f3SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23*67fb10f3SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24*67fb10f3SMarcel Moolenaar  */
25*67fb10f3SMarcel Moolenaar 
26*67fb10f3SMarcel Moolenaar #include <sys/cdefs.h>
27*67fb10f3SMarcel Moolenaar __FBSDID("$FreeBSD$");
28*67fb10f3SMarcel Moolenaar 
29*67fb10f3SMarcel Moolenaar #include <sys/param.h>
30*67fb10f3SMarcel Moolenaar #include <sys/systm.h>
31*67fb10f3SMarcel Moolenaar #include <sys/bus.h>
32*67fb10f3SMarcel Moolenaar #include <sys/conf.h>
33*67fb10f3SMarcel Moolenaar #include <sys/kernel.h>
34*67fb10f3SMarcel Moolenaar #include <sys/module.h>
35*67fb10f3SMarcel Moolenaar #include <machine/bus.h>
36*67fb10f3SMarcel Moolenaar #include <sys/rman.h>
37*67fb10f3SMarcel Moolenaar #include <machine/resource.h>
38*67fb10f3SMarcel Moolenaar #include <sys/sbuf.h>
39*67fb10f3SMarcel Moolenaar 
40*67fb10f3SMarcel Moolenaar #include <dev/pci/pcireg.h>
41*67fb10f3SMarcel Moolenaar #include <dev/pci/pcivar.h>
42*67fb10f3SMarcel Moolenaar 
43*67fb10f3SMarcel Moolenaar #include <dev/proto/proto.h>
44*67fb10f3SMarcel Moolenaar 
45*67fb10f3SMarcel Moolenaar static int proto_pci_probe(device_t dev);
46*67fb10f3SMarcel Moolenaar static int proto_pci_attach(device_t dev);
47*67fb10f3SMarcel Moolenaar 
48*67fb10f3SMarcel Moolenaar static device_method_t proto_pci_methods[] = {
49*67fb10f3SMarcel Moolenaar 	/* Device interface */
50*67fb10f3SMarcel Moolenaar 	DEVMETHOD(device_probe,		proto_pci_probe),
51*67fb10f3SMarcel Moolenaar 	DEVMETHOD(device_attach,	proto_pci_attach),
52*67fb10f3SMarcel Moolenaar 	DEVMETHOD(device_detach,	proto_detach),
53*67fb10f3SMarcel Moolenaar 	DEVMETHOD_END
54*67fb10f3SMarcel Moolenaar };
55*67fb10f3SMarcel Moolenaar 
56*67fb10f3SMarcel Moolenaar static driver_t proto_pci_driver = {
57*67fb10f3SMarcel Moolenaar 	proto_driver_name,
58*67fb10f3SMarcel Moolenaar 	proto_pci_methods,
59*67fb10f3SMarcel Moolenaar 	sizeof(struct proto_softc),
60*67fb10f3SMarcel Moolenaar };
61*67fb10f3SMarcel Moolenaar 
62*67fb10f3SMarcel Moolenaar static int
63*67fb10f3SMarcel Moolenaar proto_pci_probe(device_t dev)
64*67fb10f3SMarcel Moolenaar {
65*67fb10f3SMarcel Moolenaar 	struct sbuf *sb;
66*67fb10f3SMarcel Moolenaar 
67*67fb10f3SMarcel Moolenaar 	/* For now we only attach to function 0 devices. */
68*67fb10f3SMarcel Moolenaar 	if (pci_get_function(dev) != 0)
69*67fb10f3SMarcel Moolenaar 		return (ENXIO);
70*67fb10f3SMarcel Moolenaar 
71*67fb10f3SMarcel Moolenaar 	sb = sbuf_new_auto();
72*67fb10f3SMarcel Moolenaar 	sbuf_printf(sb, "pci%d:%d:%d:%d", pci_get_domain(dev),
73*67fb10f3SMarcel Moolenaar 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
74*67fb10f3SMarcel Moolenaar 	sbuf_finish(sb);
75*67fb10f3SMarcel Moolenaar 	device_set_desc_copy(dev, sbuf_data(sb));
76*67fb10f3SMarcel Moolenaar 	sbuf_delete(sb);
77*67fb10f3SMarcel Moolenaar 	return (BUS_PROBE_HOOVER);
78*67fb10f3SMarcel Moolenaar }
79*67fb10f3SMarcel Moolenaar 
80*67fb10f3SMarcel Moolenaar static int
81*67fb10f3SMarcel Moolenaar proto_pci_attach(device_t dev)
82*67fb10f3SMarcel Moolenaar {
83*67fb10f3SMarcel Moolenaar 	struct proto_softc *sc;
84*67fb10f3SMarcel Moolenaar 	struct resource *res;
85*67fb10f3SMarcel Moolenaar 	int bar, rid, type;
86*67fb10f3SMarcel Moolenaar 
87*67fb10f3SMarcel Moolenaar 	sc = device_get_softc(dev);
88*67fb10f3SMarcel Moolenaar 
89*67fb10f3SMarcel Moolenaar 	proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL);
90*67fb10f3SMarcel Moolenaar 
91*67fb10f3SMarcel Moolenaar 	for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) {
92*67fb10f3SMarcel Moolenaar 		rid = PCIR_BAR(bar);
93*67fb10f3SMarcel Moolenaar 		type = SYS_RES_MEMORY;
94*67fb10f3SMarcel Moolenaar 		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
95*67fb10f3SMarcel Moolenaar 		if (res == NULL) {
96*67fb10f3SMarcel Moolenaar 			type = SYS_RES_IOPORT;
97*67fb10f3SMarcel Moolenaar 			res = bus_alloc_resource_any(dev, type, &rid,
98*67fb10f3SMarcel Moolenaar 			    RF_ACTIVE);
99*67fb10f3SMarcel Moolenaar 		}
100*67fb10f3SMarcel Moolenaar 		if (res != NULL)
101*67fb10f3SMarcel Moolenaar 			proto_add_resource(sc, type, rid, res);
102*67fb10f3SMarcel Moolenaar 	}
103*67fb10f3SMarcel Moolenaar 
104*67fb10f3SMarcel Moolenaar 	rid = 0;
105*67fb10f3SMarcel Moolenaar 	type = SYS_RES_IRQ;
106*67fb10f3SMarcel Moolenaar 	res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE);
107*67fb10f3SMarcel Moolenaar 	if (res != NULL)
108*67fb10f3SMarcel Moolenaar 		proto_add_resource(sc, type, rid, res);
109*67fb10f3SMarcel Moolenaar 	return (proto_attach(dev));
110*67fb10f3SMarcel Moolenaar }
111*67fb10f3SMarcel Moolenaar 
112*67fb10f3SMarcel Moolenaar DRIVER_MODULE(proto, pci, proto_pci_driver, proto_devclass, NULL, NULL);
113