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/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/bus.h>
32 #include <sys/conf.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <machine/bus.h>
36 #include <sys/rman.h>
37 #include <machine/resource.h>
38 #include <sys/sbuf.h>
39
40 #include <dev/pci/pcireg.h>
41 #include <dev/pci/pcivar.h>
42
43 #include <dev/proto/proto.h>
44
45 static int proto_pci_probe(device_t dev);
46 static int proto_pci_attach(device_t dev);
47
48 static device_method_t proto_pci_methods[] = {
49 /* Device interface */
50 DEVMETHOD(device_probe, proto_pci_probe),
51 DEVMETHOD(device_attach, proto_pci_attach),
52 DEVMETHOD(device_detach, proto_detach),
53 DEVMETHOD_END
54 };
55
56 static driver_t proto_pci_driver = {
57 proto_driver_name,
58 proto_pci_methods,
59 sizeof(struct proto_softc),
60 };
61
62 static char proto_pci_prefix[] = "pci";
63 static char **proto_pci_devnames;
64
65 static int
66 proto_pci_probe(device_t dev)
67 {
68 struct sbuf *sb;
69
70 if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
71 return (ENXIO);
72
73 sb = sbuf_new_auto();
74 sbuf_printf(sb, "%s%d:%d:%d:%d", proto_pci_prefix, pci_get_domain(dev),
75 pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
76 sbuf_finish(sb);
77 device_set_desc_copy(dev, sbuf_data(sb));
78 sbuf_delete(sb);
79 return (proto_probe(dev, proto_pci_prefix, &proto_pci_devnames));
80 }
81
82 static int
83 proto_pci_attach(device_t dev)
84 {
85 struct proto_softc *sc;
86 struct resource *res;
87 uint32_t val;
88 int bar, rid, type;
89
90 sc = device_get_softc(dev);
91
92 proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL);
93 proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
94
95 for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) {
96 rid = PCIR_BAR(bar);
97 val = pci_read_config(dev, rid, 4);
98 type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY;
99 res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
100 if (res == NULL)
101 continue;
102 proto_add_resource(sc, type, rid, res);
103 if (type == SYS_RES_IOPORT)
104 continue;
105 /* Skip over adjacent BAR for 64-bit memory BARs. */
106 if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64)
107 bar++;
108 }
109
110 rid = 0;
111 type = SYS_RES_IRQ;
112 res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE);
113 if (res != NULL)
114 proto_add_resource(sc, type, rid, res);
115 return (proto_attach(dev));
116 }
117
118 DRIVER_MODULE(proto, pci, proto_pci_driver, NULL, NULL);
Cache object: b26776d562f8a3a462e2b07c6da678b1
|