xref: /netbsd-src/lib/libpci/pci_bus.c (revision d8c110686926247d432b594f9071f6f1a2aeecb3)
1*d8c11068Slukem /*	$NetBSD: pci_bus.c,v 1.2 2003/03/08 09:53:45 lukem Exp $	*/
216487fdfSthorpej 
316487fdfSthorpej /*
416487fdfSthorpej  * Copyright 2001 Wasabi Systems, Inc.
516487fdfSthorpej  * All rights reserved.
616487fdfSthorpej  *
716487fdfSthorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
816487fdfSthorpej  *
916487fdfSthorpej  * Redistribution and use in source and binary forms, with or without
1016487fdfSthorpej  * modification, are permitted provided that the following conditions
1116487fdfSthorpej  * are met:
1216487fdfSthorpej  * 1. Redistributions of source code must retain the above copyright
1316487fdfSthorpej  *    notice, this list of conditions and the following disclaimer.
1416487fdfSthorpej  * 2. Redistributions in binary form must reproduce the above copyright
1516487fdfSthorpej  *    notice, this list of conditions and the following disclaimer in the
1616487fdfSthorpej  *    documentation and/or other materials provided with the distribution.
1716487fdfSthorpej  * 3. All advertising materials mentioning features or use of this software
1816487fdfSthorpej  *    must display the following acknowledgement:
1916487fdfSthorpej  *	This product includes software developed for the NetBSD Project by
2016487fdfSthorpej  *	Wasabi Systems, Inc.
2116487fdfSthorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
2216487fdfSthorpej  *    or promote products derived from this software without specific prior
2316487fdfSthorpej  *    written permission.
2416487fdfSthorpej  *
2516487fdfSthorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
2616487fdfSthorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2716487fdfSthorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2816487fdfSthorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
2916487fdfSthorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3016487fdfSthorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3116487fdfSthorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3216487fdfSthorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3316487fdfSthorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3416487fdfSthorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3516487fdfSthorpej  * POSSIBILITY OF SUCH DAMAGE.
3616487fdfSthorpej  */
3716487fdfSthorpej 
3816487fdfSthorpej /*
3916487fdfSthorpej  * Interface to PCI config space registers for a PCI domain.
4016487fdfSthorpej  */
4116487fdfSthorpej 
42*d8c11068Slukem #include <sys/cdefs.h>
43*d8c11068Slukem __RCSID("$NetBSD: pci_bus.c,v 1.2 2003/03/08 09:53:45 lukem Exp $");
44*d8c11068Slukem 
4516487fdfSthorpej #include <sys/types.h>
4616487fdfSthorpej #include <sys/ioctl.h>
4716487fdfSthorpej 
4816487fdfSthorpej #include <pci.h>
4916487fdfSthorpej 
5016487fdfSthorpej #include <dev/pci/pciio.h>
5116487fdfSthorpej 
5216487fdfSthorpej /*
5316487fdfSthorpej  * pcibus_conf_read:
5416487fdfSthorpej  *
5516487fdfSthorpej  *	Read a config word for a given bus/device/function.
5616487fdfSthorpej  */
5716487fdfSthorpej int
pcibus_conf_read(int fd,u_int bus,u_int device,u_int func,u_int reg,uint32_t * valp)5816487fdfSthorpej pcibus_conf_read(int fd, u_int bus, u_int device, u_int func, u_int reg,
5916487fdfSthorpej     uint32_t *valp)
6016487fdfSthorpej {
6116487fdfSthorpej 	struct pciio_bdf_cfgreg bdfr;
6216487fdfSthorpej 	int rv;
6316487fdfSthorpej 
6416487fdfSthorpej 	bdfr.bus = bus;
6516487fdfSthorpej 	bdfr.device = device;
6616487fdfSthorpej 	bdfr.function = func;
6716487fdfSthorpej 	bdfr.cfgreg.reg = reg;
6816487fdfSthorpej 
6916487fdfSthorpej 	rv = ioctl(fd, PCI_IOC_BDF_CFGREAD, &bdfr);
7016487fdfSthorpej 	if (rv == -1) {
7116487fdfSthorpej 		/* errno already set. */
7216487fdfSthorpej 		return (-1);
7316487fdfSthorpej 	}
7416487fdfSthorpej 
7516487fdfSthorpej 	*valp = bdfr.cfgreg.val;
7616487fdfSthorpej 	return (0);
7716487fdfSthorpej }
7816487fdfSthorpej 
7916487fdfSthorpej /*
8016487fdfSthorpej  * pcibus_conf_write:
8116487fdfSthorpej  *
8216487fdfSthorpej  *	Write a config word for a given bus/device/function.
8316487fdfSthorpej  */
8416487fdfSthorpej int
pcibus_conf_write(int fd,u_int bus,u_int device,u_int func,u_int reg,uint32_t val)8516487fdfSthorpej pcibus_conf_write(int fd, u_int bus, u_int device, u_int func, u_int reg,
8616487fdfSthorpej     uint32_t val)
8716487fdfSthorpej {
8816487fdfSthorpej 	struct pciio_bdf_cfgreg bdfr;
8916487fdfSthorpej 
9016487fdfSthorpej 	bdfr.bus = bus;
9116487fdfSthorpej 	bdfr.device = device;
9216487fdfSthorpej 	bdfr.function = func;
9316487fdfSthorpej 	bdfr.cfgreg.reg = reg;
9416487fdfSthorpej 	bdfr.cfgreg.val = val;
9516487fdfSthorpej 
9616487fdfSthorpej 	return (ioctl(fd, PCI_IOC_BDF_CFGWRITE, &bdfr));
9716487fdfSthorpej }
98