Home | History | Annotate | Line # | Download | only in pci
pci.c revision 1.10
      1 /*	$NetBSD: pci.c,v 1.10 1996/02/28 01:44:41 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 };
     49 
     50 int pcimatch __P((struct device *, void *, void *));
     51 void pciattach __P((struct device *, struct device *, void *));
     52 
     53 struct cfdriver pcicd = {
     54 	NULL, "pci", pcimatch, pciattach, DV_DULL, sizeof(struct pci_softc)
     55 };
     56 
     57 int	pciprint __P((void *, char *));
     58 int	pcisubmatch __P((struct device *, void *, void *));
     59 
     60 int
     61 pcimatch(parent, match, aux)
     62 	struct device *parent;
     63 	void *match, *aux;
     64 {
     65 	struct cfdata *cf = match;
     66 	struct pcibus_attach_args *pba = aux;
     67 
     68 	if (strcmp(pba->pba_busname, cf->cf_driver->cd_name))
     69 		return (0);
     70 
     71 	/* Check the locators */
     72 #ifdef i386 /* XXX should not be attached at root, but is on i386 */
     73 	if (parent != NULL)
     74 #endif /* i386 XXX */
     75 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != pba->pba_bus)
     76 		return (0);
     77 
     78 	/* sanity */
     79 	if (pba->pba_bus < 0 || pba->pba_bus > 255)
     80 		return (0);
     81 	if (pba->pba_maxndevs < 0 || pba->pba_maxndevs > 32)
     82 		return (0);
     83 
     84 	/*
     85 	 * XXX check other (hardware?) indicators
     86 	 */
     87 
     88 	return 1;
     89 }
     90 
     91 void
     92 pciattach(parent, self, aux)
     93 	struct device *parent, *self;
     94 	void *aux;
     95 {
     96 	struct pci_softc *sc = (struct pci_softc *)self;
     97 	struct pcibus_attach_args *pba = aux;
     98 	int maxndevs, device, function, nfunctions;
     99 
    100 #ifdef i386 /* XXX should not be attached at root, but is on i386 */
    101 	if (parent == NULL)
    102 		printf(": bus %d, configuration mode %d", pba->pba_bus,
    103 		    pci_mode);
    104 #endif /* XXX */
    105 	printf("\n");
    106 
    107 	sc->sc_bus = pba->pba_bus;
    108 
    109 	maxndevs = pba->pba_maxndevs;
    110 
    111 	for (device = 0; device < maxndevs; device++) {
    112 		pcitag_t tag;
    113 		pcireg_t id, class;
    114 		struct pci_attach_args pa;
    115 		struct cfdata *cf;
    116 		int supported;
    117 
    118 		tag = pci_make_tag(sc->sc_bus, device, function);
    119 		id = pci_conf_read(tag, PCI_ID_REG);
    120 		if (id == 0 || id == 0xffffffff)
    121 			continue;
    122 
    123 		nfunctions = 1;				/* XXX */
    124 
    125 		for (function = 0; function < nfunctions; function++) {
    126 			tag = pci_make_tag(sc->sc_bus, device, function);
    127 			id = pci_conf_read(tag, PCI_ID_REG);
    128 			if (id == 0 || id == 0xffffffff)
    129 				continue;
    130 			class = pci_conf_read(tag, PCI_CLASS_REG);
    131 
    132 			pa.pa_device = device;
    133 			pa.pa_function = function;
    134 			pa.pa_tag = tag;
    135 			pa.pa_id = id;
    136 			pa.pa_class = class;
    137 
    138 			config_found_sm(self, &pa, pciprint, pcisubmatch);
    139 		}
    140 	}
    141 }
    142 
    143 int
    144 pciprint(aux, pnp)
    145 	void *aux;
    146 	char *pnp;
    147 {
    148 	register struct pci_attach_args *pa = aux;
    149 	char devinfo[256];
    150 
    151 	if (pnp) {
    152 		pci_devinfo(pa->pa_id, pa->pa_class, 1, devinfo);
    153 		printf("%s at %s", devinfo, pnp);
    154 	}
    155 	printf(" dev %d function %d", pa->pa_device, pa->pa_function);
    156 	return (UNCONF);
    157 }
    158 
    159 int
    160 pcisubmatch(parent, match, aux)
    161 	struct device *parent;
    162 	void *match, *aux;
    163 {
    164 	struct cfdata *cf = match;
    165 	struct pci_attach_args *pa = aux;
    166 
    167 	if (cf->cf_loc[0] != -1 && cf->cf_loc[0] != pa->pa_device)
    168 		return 0;
    169 	if (cf->cf_loc[1] != -1 && cf->cf_loc[1] != pa->pa_function)
    170 		return 0;
    171 	return ((*cf->cf_driver->cd_match)(parent, match, aux));
    172 }
    173