xref: /netbsd/sys/arch/i386/stand/lib/test/pci_user.c (revision bf9ec67e)
1 /*	$NetBSD: pci_user.c,v 1.1 1998/05/15 17:07:16 drochner Exp $	*/
2 
3 /*
4  * Copyright (c) 1998
5  *	Matthias Drochner.  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 for the NetBSD Project
18  *	by Matthias Drochner.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 #include "sanamespace.h"
36 
37 #include <sys/types.h>
38 #include <machine/pio.h>
39 
40 #include <pcivar.h>
41 
42 extern int mapio __P((void));
43 
44 /*
45  * Replacement for i386/stand/lib/biospci.c.
46  * Very simple functions to access PCI config space from
47  * userland. Works with configuration mode 1 only, can
48  * only access bus number 0.
49  */
50 
51 #define	PCI_MODE1_ENABLE	0x80000000UL
52 #define	PCI_MODE1_ADDRESS_REG	0x0cf8
53 #define	PCI_MODE1_DATA_REG	0x0cfc
54 
55 static int
56 maketag(bus, dev, fcn)
57 	int bus, dev, fcn;
58 {
59 	return (PCI_MODE1_ENABLE |
60 		(bus << 16) | (dev << 11) | (fcn << 8));
61 }
62 
63 int
64 pcicheck()
65 {
66 	return (mapio() ? -1 : 0);
67 }
68 
69 int
70 pcifinddev(vid, did, handle)
71 	int             vid, did;
72 	pcihdl_t       *handle;
73 {
74 	int i;
75 	for (i = 0; i < 32; i++) {
76 		pcihdl_t h;
77 		int id;
78 		h = maketag(0, i, 0);
79 		pcicfgread(&h, 0, &id);
80 		if (id == (vid | (did << 16))) {
81 			*handle = h;
82 			return (0);
83 		}
84 	}
85 	return (-1);
86 }
87 
88 int
89 pcicfgread(handle, off, val)
90 	pcihdl_t       *handle;
91 	int             off;
92 	int            *val;
93 {
94 	int data;
95 
96 	outl(PCI_MODE1_ADDRESS_REG, *handle | off);
97 	data = inl(PCI_MODE1_DATA_REG);
98 	outl(PCI_MODE1_ADDRESS_REG, 0);
99 	*val = data;
100 	return (0);
101 }
102 
103 int
104 pcicfgwrite(handle, off, val)
105 	pcihdl_t       *handle;
106 	int             off;
107 	int             val;
108 {
109 	outl(PCI_MODE1_ADDRESS_REG, *handle | off);
110 	outl(PCI_MODE1_DATA_REG, val);
111 	outl(PCI_MODE1_ADDRESS_REG, 0);
112 	return (0);
113 }
114