Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.17
      1 /*	$NetBSD: pci.c,v 1.17 1996/03/27 00:13:50 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 int pcimatch __P((struct device *, void *, void *));
     45 void pciattach __P((struct device *, struct device *, void *));
     46 
     47 struct cfattach pci_ca = {
     48 	sizeof(struct device), pcimatch, pciattach
     49 };
     50 
     51 struct cfdriver pci_cd = {
     52 	NULL, "pci", DV_DULL
     53 };
     54 
     55 int	pciprint __P((void *, char *));
     56 int	pcisubmatch __P((struct device *, void *, void *));
     57 
     58 int
     59 pcimatch(parent, match, aux)
     60 	struct device *parent;
     61 	void *match, *aux;
     62 {
     63 	struct cfdata *cf = match;
     64 	struct pcibus_attach_args *pba = aux;
     65 
     66 	if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
     67 		return (0);
     68 
     69 	/* Check the locators */
     70 	if (cf->pcibuscf_bus != PCIBUS_UNK_BUS &&
     71 	    cf->pcibuscf_bus != pba->pba_bus)
     72 		return (0);
     73 
     74 	/* sanity */
     75 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
     76 		return (0);
     77 
     78 	/*
     79 	 * XXX check other (hardware?) indicators
     80 	 */
     81 
     82 	return 1;
     83 }
     84 
     85 void
     86 pciattach(parent, self, aux)
     87 	struct device *parent, *self;
     88 	void *aux;
     89 {
     90 	struct pcibus_attach_args *pba = aux;
     91 	bus_chipset_tag_t bc;
     92 	int device, function, nfunctions;
     93 
     94 	pci_md_attach_hook(parent, self, pba);
     95 	printf("\n");
     96 
     97 	for (device = 0; device < PCI_MAX_DEVICE_NUMBER; device++) {
     98 		pcitag_t tag;
     99 		pcireg_t id, class, bhlcr;
    100 		struct pci_attach_args pa;
    101 		struct cfdata *cf;
    102 		int supported;
    103 
    104 		tag = pci_make_tag(pba->pba_bus, device, 0);
    105 		id = pci_conf_read(tag, PCI_ID_REG);
    106 		if (id == 0 || id == 0xffffffff)
    107 			continue;
    108 
    109 		bhlcr = pci_conf_read(tag, PCI_BHLC_REG);
    110 		nfunctions = PCI_HDRTYPE_MULTIFN(bhlcr) ? 8 : 1;
    111 
    112 		for (function = 0; function < nfunctions; function++) {
    113 			tag = pci_make_tag(pba->pba_bus, device, function);
    114 			id = pci_conf_read(tag, PCI_ID_REG);
    115 			if (id == 0 || id == 0xffffffff)
    116 				continue;
    117 			class = pci_conf_read(tag, PCI_CLASS_REG);
    118 
    119 			pa.pa_bc = pba->pba_bc;
    120 			pa.pa_device = device;
    121 			pa.pa_function = function;
    122 			pa.pa_tag = tag;
    123 			pa.pa_id = id;
    124 			pa.pa_class = class;
    125 
    126 			config_found_sm(self, &pa, pciprint, pcisubmatch);
    127 		}
    128 	}
    129 }
    130 
    131 int
    132 pciprint(aux, pnp)
    133 	void *aux;
    134 	char *pnp;
    135 {
    136 	register struct pci_attach_args *pa = aux;
    137 	char devinfo[256];
    138 
    139 	if (pnp) {
    140 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
    141 		printf("%s at %s", devinfo, pnp);
    142 	}
    143 	printf(" dev %d function %d", pa->pa_device, pa->pa_function);
    144 	return (UNCONF);
    145 }
    146 
    147 int
    148 pcisubmatch(parent, match, aux)
    149 	struct device *parent;
    150 	void *match, *aux;
    151 {
    152 	struct cfdata *cf = match;
    153 	struct pci_attach_args *pa = aux;
    154 
    155 	if (cf->pcicf_dev != PCI_UNK_DEV &&
    156 	    cf->pcicf_dev != pa->pa_device)
    157 		return 0;
    158 	if (cf->pcicf_function != PCI_UNK_FUNCTION &&
    159 	    cf->pcicf_function != pa->pa_function)
    160 		return 0;
    161 	return ((*cf->cf_attach->ca_match)(parent, match, aux));
    162 }
    163