proto_bus_pci.c revision 284315
1265055Smarcel/*-
2265055Smarcel * Copyright (c) 2014 Marcel Moolenaar
3265055Smarcel * All rights reserved.
4265055Smarcel *
5265055Smarcel * Redistribution and use in source and binary forms, with or without
6265055Smarcel * modification, are permitted provided that the following conditions
7265055Smarcel * are met:
8265055Smarcel * 1. Redistributions of source code must retain the above copyright
9265055Smarcel *    notice, this list of conditions and the following disclaimer.
10265055Smarcel * 2. Redistributions in binary form must reproduce the above copyright
11265055Smarcel *    notice, this list of conditions and the following disclaimer in the
12265055Smarcel *    documentation and/or other materials provided with the distribution.
13265055Smarcel *
14265055Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15265055Smarcel * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16265055Smarcel * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17265055Smarcel * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18265055Smarcel * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19265055Smarcel * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20265055Smarcel * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21265055Smarcel * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22265055Smarcel * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23265055Smarcel * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24265055Smarcel */
25265055Smarcel
26265055Smarcel#include <sys/cdefs.h>
27265055Smarcel__FBSDID("$FreeBSD: head/sys/dev/proto/proto_bus_pci.c 284315 2015-06-12 12:27:10Z marcel $");
28265055Smarcel
29265055Smarcel#include <sys/param.h>
30265055Smarcel#include <sys/systm.h>
31265055Smarcel#include <sys/bus.h>
32265055Smarcel#include <sys/conf.h>
33265055Smarcel#include <sys/kernel.h>
34265055Smarcel#include <sys/module.h>
35265055Smarcel#include <machine/bus.h>
36265055Smarcel#include <sys/rman.h>
37265055Smarcel#include <machine/resource.h>
38265055Smarcel#include <sys/sbuf.h>
39265055Smarcel
40265055Smarcel#include <dev/pci/pcireg.h>
41265055Smarcel#include <dev/pci/pcivar.h>
42265055Smarcel
43265055Smarcel#include <dev/proto/proto.h>
44265055Smarcel
45265055Smarcelstatic int proto_pci_probe(device_t dev);
46265055Smarcelstatic int proto_pci_attach(device_t dev);
47265055Smarcel
48265055Smarcelstatic device_method_t proto_pci_methods[] = {
49265055Smarcel	/* Device interface */
50265055Smarcel	DEVMETHOD(device_probe,		proto_pci_probe),
51265055Smarcel	DEVMETHOD(device_attach,	proto_pci_attach),
52265055Smarcel	DEVMETHOD(device_detach,	proto_detach),
53265055Smarcel	DEVMETHOD_END
54265055Smarcel};
55265055Smarcel
56265055Smarcelstatic driver_t proto_pci_driver = {
57265055Smarcel	proto_driver_name,
58265055Smarcel	proto_pci_methods,
59265055Smarcel	sizeof(struct proto_softc),
60265055Smarcel};
61265055Smarcel
62265055Smarcelstatic int
63265055Smarcelproto_pci_probe(device_t dev)
64265055Smarcel{
65265055Smarcel	struct sbuf *sb;
66265055Smarcel
67265055Smarcel	/* For now we only attach to function 0 devices. */
68265055Smarcel	if (pci_get_function(dev) != 0)
69265055Smarcel		return (ENXIO);
70265055Smarcel
71265055Smarcel	sb = sbuf_new_auto();
72265055Smarcel	sbuf_printf(sb, "pci%d:%d:%d:%d", pci_get_domain(dev),
73265055Smarcel	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
74265055Smarcel	sbuf_finish(sb);
75265055Smarcel	device_set_desc_copy(dev, sbuf_data(sb));
76265055Smarcel	sbuf_delete(sb);
77265055Smarcel	return (BUS_PROBE_HOOVER);
78265055Smarcel}
79265055Smarcel
80265055Smarcelstatic int
81265055Smarcelproto_pci_attach(device_t dev)
82265055Smarcel{
83265055Smarcel	struct proto_softc *sc;
84265055Smarcel	struct resource *res;
85284315Smarcel	uint32_t val;
86265055Smarcel	int bar, rid, type;
87265055Smarcel
88265055Smarcel	sc = device_get_softc(dev);
89265055Smarcel
90265055Smarcel	proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL);
91284079Smarcel	proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
92265055Smarcel
93265055Smarcel	for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) {
94265055Smarcel		rid = PCIR_BAR(bar);
95284315Smarcel		val = pci_read_config(dev, rid, 4);
96284315Smarcel		type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY;
97265055Smarcel		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
98284315Smarcel		if (res == NULL)
99284315Smarcel			continue;
100284315Smarcel		proto_add_resource(sc, type, rid, res);
101284315Smarcel		if (type == SYS_RES_IOPORT)
102284315Smarcel			continue;
103284315Smarcel		/* Skip over adjacent BAR for 64-bit memory BARs. */
104284315Smarcel		if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64)
105284315Smarcel			bar++;
106265055Smarcel	}
107265055Smarcel
108265055Smarcel	rid = 0;
109265055Smarcel	type = SYS_RES_IRQ;
110265055Smarcel	res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE);
111265055Smarcel	if (res != NULL)
112265055Smarcel		proto_add_resource(sc, type, rid, res);
113265055Smarcel	return (proto_attach(dev));
114265055Smarcel}
115265055Smarcel
116265055SmarcelDRIVER_MODULE(proto, pci, proto_pci_driver, proto_devclass, NULL, NULL);
117