xref: /freebsd/sys/dev/proto/proto_bus_pci.c (revision 3f745144d99d23b4819d3ce255ff333e8ee51c6f)
167fb10f3SMarcel Moolenaar /*-
267fb10f3SMarcel Moolenaar  * Copyright (c) 2014 Marcel Moolenaar
367fb10f3SMarcel Moolenaar  * All rights reserved.
467fb10f3SMarcel Moolenaar  *
567fb10f3SMarcel Moolenaar  * Redistribution and use in source and binary forms, with or without
667fb10f3SMarcel Moolenaar  * modification, are permitted provided that the following conditions
767fb10f3SMarcel Moolenaar  * are met:
867fb10f3SMarcel Moolenaar  * 1. Redistributions of source code must retain the above copyright
967fb10f3SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer.
1067fb10f3SMarcel Moolenaar  * 2. Redistributions in binary form must reproduce the above copyright
1167fb10f3SMarcel Moolenaar  *    notice, this list of conditions and the following disclaimer in the
1267fb10f3SMarcel Moolenaar  *    documentation and/or other materials provided with the distribution.
1367fb10f3SMarcel Moolenaar  *
1467fb10f3SMarcel Moolenaar  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1567fb10f3SMarcel Moolenaar  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1667fb10f3SMarcel Moolenaar  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1767fb10f3SMarcel Moolenaar  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1867fb10f3SMarcel Moolenaar  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1967fb10f3SMarcel Moolenaar  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2067fb10f3SMarcel Moolenaar  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2167fb10f3SMarcel Moolenaar  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2267fb10f3SMarcel Moolenaar  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2367fb10f3SMarcel Moolenaar  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2467fb10f3SMarcel Moolenaar  */
2567fb10f3SMarcel Moolenaar 
2667fb10f3SMarcel Moolenaar #include <sys/cdefs.h>
2767fb10f3SMarcel Moolenaar __FBSDID("$FreeBSD$");
2867fb10f3SMarcel Moolenaar 
2967fb10f3SMarcel Moolenaar #include <sys/param.h>
3067fb10f3SMarcel Moolenaar #include <sys/systm.h>
3167fb10f3SMarcel Moolenaar #include <sys/bus.h>
3267fb10f3SMarcel Moolenaar #include <sys/conf.h>
3367fb10f3SMarcel Moolenaar #include <sys/kernel.h>
3467fb10f3SMarcel Moolenaar #include <sys/module.h>
3567fb10f3SMarcel Moolenaar #include <machine/bus.h>
3667fb10f3SMarcel Moolenaar #include <sys/rman.h>
3767fb10f3SMarcel Moolenaar #include <machine/resource.h>
3867fb10f3SMarcel Moolenaar #include <sys/sbuf.h>
3967fb10f3SMarcel Moolenaar 
4067fb10f3SMarcel Moolenaar #include <dev/pci/pcireg.h>
4167fb10f3SMarcel Moolenaar #include <dev/pci/pcivar.h>
4267fb10f3SMarcel Moolenaar 
4367fb10f3SMarcel Moolenaar #include <dev/proto/proto.h>
4467fb10f3SMarcel Moolenaar 
4567fb10f3SMarcel Moolenaar static int proto_pci_probe(device_t dev);
4667fb10f3SMarcel Moolenaar static int proto_pci_attach(device_t dev);
4767fb10f3SMarcel Moolenaar 
4867fb10f3SMarcel Moolenaar static device_method_t proto_pci_methods[] = {
4967fb10f3SMarcel Moolenaar 	/* Device interface */
5067fb10f3SMarcel Moolenaar 	DEVMETHOD(device_probe,		proto_pci_probe),
5167fb10f3SMarcel Moolenaar 	DEVMETHOD(device_attach,	proto_pci_attach),
5267fb10f3SMarcel Moolenaar 	DEVMETHOD(device_detach,	proto_detach),
5367fb10f3SMarcel Moolenaar 	DEVMETHOD_END
5467fb10f3SMarcel Moolenaar };
5567fb10f3SMarcel Moolenaar 
5667fb10f3SMarcel Moolenaar static driver_t proto_pci_driver = {
5767fb10f3SMarcel Moolenaar 	proto_driver_name,
5867fb10f3SMarcel Moolenaar 	proto_pci_methods,
5967fb10f3SMarcel Moolenaar 	sizeof(struct proto_softc),
6067fb10f3SMarcel Moolenaar };
6167fb10f3SMarcel Moolenaar 
6267fb10f3SMarcel Moolenaar static int
6367fb10f3SMarcel Moolenaar proto_pci_probe(device_t dev)
6467fb10f3SMarcel Moolenaar {
6567fb10f3SMarcel Moolenaar 	struct sbuf *sb;
6667fb10f3SMarcel Moolenaar 
67*3f745144SMarcel Moolenaar 	if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
6867fb10f3SMarcel Moolenaar 		return (ENXIO);
6967fb10f3SMarcel Moolenaar 
7067fb10f3SMarcel Moolenaar 	sb = sbuf_new_auto();
7167fb10f3SMarcel Moolenaar 	sbuf_printf(sb, "pci%d:%d:%d:%d", pci_get_domain(dev),
7267fb10f3SMarcel Moolenaar 	    pci_get_bus(dev), pci_get_slot(dev), pci_get_function(dev));
7367fb10f3SMarcel Moolenaar 	sbuf_finish(sb);
7467fb10f3SMarcel Moolenaar 	device_set_desc_copy(dev, sbuf_data(sb));
7567fb10f3SMarcel Moolenaar 	sbuf_delete(sb);
7667fb10f3SMarcel Moolenaar 	return (BUS_PROBE_HOOVER);
7767fb10f3SMarcel Moolenaar }
7867fb10f3SMarcel Moolenaar 
7967fb10f3SMarcel Moolenaar static int
8067fb10f3SMarcel Moolenaar proto_pci_attach(device_t dev)
8167fb10f3SMarcel Moolenaar {
8267fb10f3SMarcel Moolenaar 	struct proto_softc *sc;
8367fb10f3SMarcel Moolenaar 	struct resource *res;
8400f73819SMarcel Moolenaar 	uint32_t val;
8567fb10f3SMarcel Moolenaar 	int bar, rid, type;
8667fb10f3SMarcel Moolenaar 
8767fb10f3SMarcel Moolenaar 	sc = device_get_softc(dev);
8867fb10f3SMarcel Moolenaar 
8967fb10f3SMarcel Moolenaar 	proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL);
904f027abdSMarcel Moolenaar 	proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
9167fb10f3SMarcel Moolenaar 
9267fb10f3SMarcel Moolenaar 	for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) {
9367fb10f3SMarcel Moolenaar 		rid = PCIR_BAR(bar);
9400f73819SMarcel Moolenaar 		val = pci_read_config(dev, rid, 4);
9500f73819SMarcel Moolenaar 		type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY;
9667fb10f3SMarcel Moolenaar 		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
9700f73819SMarcel Moolenaar 		if (res == NULL)
9800f73819SMarcel Moolenaar 			continue;
9967fb10f3SMarcel Moolenaar 		proto_add_resource(sc, type, rid, res);
10000f73819SMarcel Moolenaar 		if (type == SYS_RES_IOPORT)
10100f73819SMarcel Moolenaar 			continue;
10200f73819SMarcel Moolenaar 		/* Skip over adjacent BAR for 64-bit memory BARs. */
10300f73819SMarcel Moolenaar 		if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64)
10400f73819SMarcel Moolenaar 			bar++;
10567fb10f3SMarcel Moolenaar 	}
10667fb10f3SMarcel Moolenaar 
10767fb10f3SMarcel Moolenaar 	rid = 0;
10867fb10f3SMarcel Moolenaar 	type = SYS_RES_IRQ;
10967fb10f3SMarcel Moolenaar 	res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE);
11067fb10f3SMarcel Moolenaar 	if (res != NULL)
11167fb10f3SMarcel Moolenaar 		proto_add_resource(sc, type, rid, res);
11267fb10f3SMarcel Moolenaar 	return (proto_attach(dev));
11367fb10f3SMarcel Moolenaar }
11467fb10f3SMarcel Moolenaar 
11567fb10f3SMarcel Moolenaar DRIVER_MODULE(proto, pci, proto_pci_driver, proto_devclass, NULL, NULL);
116