Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.14
      1 /*	$NetBSD: pci.c,v 1.14 1996/03/14 02:35:32 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1994 Charles Hannum.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Charles Hannum.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * PCI bus autoconfiguration.
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/systm.h>
     39 #include <sys/device.h>
     40 
     41 #include <dev/pci/pcireg.h>
     42 #include <dev/pci/pcivar.h>
     43 
     44 struct pci_softc {
     45 	struct device	sc_dev;
     46 
     47 	int		sc_bus;
     48 	bus_chipset_tag_t sc_bc;
     49 };
     50 
     51 int pcimatch __P((struct device *, void *, void *));
     52 void pciattach __P((struct device *, struct device *, void *));
     53 
     54 struct cfdriver pcicd = {
     55 	NULL, "pci", pcimatch, pciattach, DV_DULL, sizeof(struct pci_softc)
     56 };
     57 
     58 int	pciprint __P((void *, char *));
     59 int	pcisubmatch __P((struct device *, void *, void *));
     60 
     61 int
     62 pcimatch(parent, match, aux)
     63 	struct device *parent;
     64 	void *match, *aux;
     65 {
     66 	struct cfdata *cf = match;
     67 	struct pcibus_attach_args *pba = aux;
     68 
     69 	if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
     70 		return (0);
     71 
     72 	/* Check the locators */
     73 	if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
     74 	    cf->pcibuscf_bus != pba->pba_bus)
     75 		return (0);
     76 
     77 	/* sanity */
     78 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
     79 		return (0);
     80 
     81 	/*
     82 	 * XXX check other (hardware?) indicators
     83 	 */
     84 
     85 	return 1;
     86 }
     87 
     88 void
     89 pciattach(parent, self, aux)
     90 	struct device *parent, *self;
     91 	void *aux;
     92 {
     93 	struct pci_softc *sc = (struct pci_softc *)self;
     94 	struct pcibus_attach_args *pba = aux;
     95 	bus_chipset_tag_t bc;
     96 	int device, function, nfunctions;
     97 
     98 	sc->sc_bus = pba->pba_bus;
     99 	sc->sc_bc = bc = pba->pba_bc;
    100 
    101 	pci_md_attach_hook(parent, sc, pba);
    102 	printf("\n");
    103 
    104 	for (device = 0; device < PCI_MAX_DEVICE_NUMBER; device++) {
    105 		pcitag_t tag;
    106 		pcireg_t id, class;
    107 		struct pci_attach_args pa;
    108 		struct cfdata *cf;
    109 		int supported;
    110 
    111 		tag = pci_make_tag(sc->sc_bus, device, 0);
    112 		id = pci_conf_read(tag, PCI_ID_REG);
    113 		if (id == 0 || id == 0xffffffff)
    114 			continue;
    115 
    116 		nfunctions = 1;				/* XXX */
    117 
    118 		for (function = 0; function < nfunctions; function++) {
    119 			tag = pci_make_tag(sc->sc_bus, device, function);
    120 			id = pci_conf_read(tag, PCI_ID_REG);
    121 			if (id == 0 || id == 0xffffffff)
    122 				continue;
    123 			class = pci_conf_read(tag, PCI_CLASS_REG);
    124 
    125 			pa.pa_bc = bc;
    126 			pa.pa_device = device;
    127 			pa.pa_function = function;
    128 			pa.pa_tag = tag;
    129 			pa.pa_id = id;
    130 			pa.pa_class = class;
    131 
    132 			config_found_sm(self, &pa, pciprint, pcisubmatch);
    133 		}
    134 	}
    135 }
    136 
    137 int
    138 pciprint(aux, pnp)
    139 	void *aux;
    140 	char *pnp;
    141 {
    142 	register struct pci_attach_args *pa = aux;
    143 	char devinfo[256];
    144 
    145 	if (pnp) {
    146 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
    147 		printf("%s at %s", devinfo, pnp);
    148 	}
    149 	printf(" dev %d function %d", pa->pa_device, pa->pa_function);
    150 	return (UNCONF);
    151 }
    152 
    153 int
    154 pcisubmatch(parent, match, aux)
    155 	struct device *parent;
    156 	void *match, *aux;
    157 {
    158 	struct cfdata *cf = match;
    159 	struct pci_attach_args *pa = aux;
    160 
    161 	if (cf->pcicf_dev != PCI_UNK_DEV &&
    162 	    cf->pcicf_dev != pa->pa_device)
    163 		return 0;
    164 	if (cf->pcicf_function != PCI_UNK_FUNCTION &&
    165 	    cf->pcicf_function != pa->pa_function)
    166 		return 0;
    167 	return ((*cf->cf_driver->cd_match)(parent, match, aux));
    168 }
    169