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