167fb10f3SMarcel Moolenaar /*-
2be00e098SMarcel Moolenaar * Copyright (c) 2014, 2015 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/param.h>
2767fb10f3SMarcel Moolenaar #include <sys/systm.h>
2867fb10f3SMarcel Moolenaar #include <sys/bus.h>
2967fb10f3SMarcel Moolenaar #include <sys/conf.h>
3067fb10f3SMarcel Moolenaar #include <sys/kernel.h>
3167fb10f3SMarcel Moolenaar #include <sys/module.h>
3267fb10f3SMarcel Moolenaar #include <machine/bus.h>
3367fb10f3SMarcel Moolenaar #include <sys/rman.h>
3467fb10f3SMarcel Moolenaar #include <machine/resource.h>
3567fb10f3SMarcel Moolenaar
3667fb10f3SMarcel Moolenaar #include <dev/pci/pcireg.h>
3767fb10f3SMarcel Moolenaar #include <dev/pci/pcivar.h>
3867fb10f3SMarcel Moolenaar
3967fb10f3SMarcel Moolenaar #include <dev/proto/proto.h>
4067fb10f3SMarcel Moolenaar
4167fb10f3SMarcel Moolenaar static int proto_pci_probe(device_t dev);
4267fb10f3SMarcel Moolenaar static int proto_pci_attach(device_t dev);
4367fb10f3SMarcel Moolenaar
4467fb10f3SMarcel Moolenaar static device_method_t proto_pci_methods[] = {
4567fb10f3SMarcel Moolenaar /* Device interface */
4667fb10f3SMarcel Moolenaar DEVMETHOD(device_probe, proto_pci_probe),
4767fb10f3SMarcel Moolenaar DEVMETHOD(device_attach, proto_pci_attach),
4867fb10f3SMarcel Moolenaar DEVMETHOD(device_detach, proto_detach),
4967fb10f3SMarcel Moolenaar DEVMETHOD_END
5067fb10f3SMarcel Moolenaar };
5167fb10f3SMarcel Moolenaar
5267fb10f3SMarcel Moolenaar static driver_t proto_pci_driver = {
5367fb10f3SMarcel Moolenaar proto_driver_name,
5467fb10f3SMarcel Moolenaar proto_pci_methods,
5567fb10f3SMarcel Moolenaar sizeof(struct proto_softc),
5667fb10f3SMarcel Moolenaar };
5767fb10f3SMarcel Moolenaar
58be00e098SMarcel Moolenaar static char proto_pci_prefix[] = "pci";
59be00e098SMarcel Moolenaar static char **proto_pci_devnames;
60be00e098SMarcel Moolenaar
6167fb10f3SMarcel Moolenaar static int
proto_pci_probe(device_t dev)6267fb10f3SMarcel Moolenaar proto_pci_probe(device_t dev)
6367fb10f3SMarcel Moolenaar {
643f745144SMarcel Moolenaar if ((pci_read_config(dev, PCIR_HDRTYPE, 1) & PCIM_HDRTYPE) != 0)
6567fb10f3SMarcel Moolenaar return (ENXIO);
6667fb10f3SMarcel Moolenaar
67*542f9494SMark Johnston device_set_descf(dev, "%s%d:%d:%d:%d", proto_pci_prefix,
68*542f9494SMark Johnston pci_get_domain(dev), pci_get_bus(dev), pci_get_slot(dev),
69*542f9494SMark Johnston pci_get_function(dev));
70be00e098SMarcel Moolenaar return (proto_probe(dev, proto_pci_prefix, &proto_pci_devnames));
7167fb10f3SMarcel Moolenaar }
7267fb10f3SMarcel Moolenaar
7367fb10f3SMarcel Moolenaar static int
proto_pci_attach(device_t dev)7467fb10f3SMarcel Moolenaar proto_pci_attach(device_t dev)
7567fb10f3SMarcel Moolenaar {
7667fb10f3SMarcel Moolenaar struct proto_softc *sc;
7767fb10f3SMarcel Moolenaar struct resource *res;
7800f73819SMarcel Moolenaar uint32_t val;
7967fb10f3SMarcel Moolenaar int bar, rid, type;
8067fb10f3SMarcel Moolenaar
8167fb10f3SMarcel Moolenaar sc = device_get_softc(dev);
8267fb10f3SMarcel Moolenaar
8367fb10f3SMarcel Moolenaar proto_add_resource(sc, PROTO_RES_PCICFG, 0, NULL);
844f027abdSMarcel Moolenaar proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
8567fb10f3SMarcel Moolenaar
8667fb10f3SMarcel Moolenaar for (bar = 0; bar < PCIR_MAX_BAR_0; bar++) {
8767fb10f3SMarcel Moolenaar rid = PCIR_BAR(bar);
8800f73819SMarcel Moolenaar val = pci_read_config(dev, rid, 4);
8900f73819SMarcel Moolenaar type = (PCI_BAR_IO(val)) ? SYS_RES_IOPORT : SYS_RES_MEMORY;
9067fb10f3SMarcel Moolenaar res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
9100f73819SMarcel Moolenaar if (res == NULL)
9200f73819SMarcel Moolenaar continue;
9367fb10f3SMarcel Moolenaar proto_add_resource(sc, type, rid, res);
9400f73819SMarcel Moolenaar if (type == SYS_RES_IOPORT)
9500f73819SMarcel Moolenaar continue;
9600f73819SMarcel Moolenaar /* Skip over adjacent BAR for 64-bit memory BARs. */
9700f73819SMarcel Moolenaar if ((val & PCIM_BAR_MEM_TYPE) == PCIM_BAR_MEM_64)
9800f73819SMarcel Moolenaar bar++;
9967fb10f3SMarcel Moolenaar }
10067fb10f3SMarcel Moolenaar
10167fb10f3SMarcel Moolenaar rid = 0;
10267fb10f3SMarcel Moolenaar type = SYS_RES_IRQ;
10367fb10f3SMarcel Moolenaar res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE | RF_SHAREABLE);
10467fb10f3SMarcel Moolenaar if (res != NULL)
10567fb10f3SMarcel Moolenaar proto_add_resource(sc, type, rid, res);
10667fb10f3SMarcel Moolenaar return (proto_attach(dev));
10767fb10f3SMarcel Moolenaar }
10867fb10f3SMarcel Moolenaar
10968781fb3SJohn Baldwin DRIVER_MODULE(proto, pci, proto_pci_driver, NULL, NULL);
110