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