xref: /minix3/sys/arch/i386/stand/lib/test/pci_user.c (revision 58a2b0008e28f606a7f7f5faaeaba4faac57a1ea)
1*58a2b000SEvgeniy Ivanov /*	$NetBSD: pci_user.c,v 1.4 2008/12/14 18:46:33 christos Exp $	*/
2*58a2b000SEvgeniy Ivanov 
3*58a2b000SEvgeniy Ivanov /*
4*58a2b000SEvgeniy Ivanov  * Copyright (c) 1998
5*58a2b000SEvgeniy Ivanov  *	Matthias Drochner.  All rights reserved.
6*58a2b000SEvgeniy Ivanov  *
7*58a2b000SEvgeniy Ivanov  * Redistribution and use in source and binary forms, with or without
8*58a2b000SEvgeniy Ivanov  * modification, are permitted provided that the following conditions
9*58a2b000SEvgeniy Ivanov  * are met:
10*58a2b000SEvgeniy Ivanov  * 1. Redistributions of source code must retain the above copyright
11*58a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer.
12*58a2b000SEvgeniy Ivanov  * 2. Redistributions in binary form must reproduce the above copyright
13*58a2b000SEvgeniy Ivanov  *    notice, this list of conditions and the following disclaimer in the
14*58a2b000SEvgeniy Ivanov  *    documentation and/or other materials provided with the distribution.
15*58a2b000SEvgeniy Ivanov  *
16*58a2b000SEvgeniy Ivanov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17*58a2b000SEvgeniy Ivanov  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18*58a2b000SEvgeniy Ivanov  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*58a2b000SEvgeniy Ivanov  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20*58a2b000SEvgeniy Ivanov  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21*58a2b000SEvgeniy Ivanov  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22*58a2b000SEvgeniy Ivanov  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23*58a2b000SEvgeniy Ivanov  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24*58a2b000SEvgeniy Ivanov  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25*58a2b000SEvgeniy Ivanov  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*58a2b000SEvgeniy Ivanov  *
27*58a2b000SEvgeniy Ivanov  */
28*58a2b000SEvgeniy Ivanov 
29*58a2b000SEvgeniy Ivanov #include "sanamespace.h"
30*58a2b000SEvgeniy Ivanov 
31*58a2b000SEvgeniy Ivanov #include <sys/types.h>
32*58a2b000SEvgeniy Ivanov #include <machine/pio.h>
33*58a2b000SEvgeniy Ivanov 
34*58a2b000SEvgeniy Ivanov #include <pcivar.h>
35*58a2b000SEvgeniy Ivanov 
36*58a2b000SEvgeniy Ivanov extern int mapio(void);
37*58a2b000SEvgeniy Ivanov 
38*58a2b000SEvgeniy Ivanov /*
39*58a2b000SEvgeniy Ivanov  * Replacement for i386/stand/lib/biospci.c.
40*58a2b000SEvgeniy Ivanov  * Very simple functions to access PCI config space from
41*58a2b000SEvgeniy Ivanov  * userland. Works with configuration mode 1 only, can
42*58a2b000SEvgeniy Ivanov  * only access bus number 0.
43*58a2b000SEvgeniy Ivanov  */
44*58a2b000SEvgeniy Ivanov 
45*58a2b000SEvgeniy Ivanov #define	PCI_MODE1_ENABLE	0x80000000UL
46*58a2b000SEvgeniy Ivanov #define	PCI_MODE1_ADDRESS_REG	0x0cf8
47*58a2b000SEvgeniy Ivanov #define	PCI_MODE1_DATA_REG	0x0cfc
48*58a2b000SEvgeniy Ivanov 
49*58a2b000SEvgeniy Ivanov static int
maketag(int bus,int dev,int fcn)50*58a2b000SEvgeniy Ivanov maketag(int bus, int dev, int fcn)
51*58a2b000SEvgeniy Ivanov {
52*58a2b000SEvgeniy Ivanov 
53*58a2b000SEvgeniy Ivanov 	return PCI_MODE1_ENABLE |
54*58a2b000SEvgeniy Ivanov 	    (bus << 16) | (dev << 11) | (fcn << 8);
55*58a2b000SEvgeniy Ivanov }
56*58a2b000SEvgeniy Ivanov 
57*58a2b000SEvgeniy Ivanov int
pcicheck(void)58*58a2b000SEvgeniy Ivanov pcicheck(void)
59*58a2b000SEvgeniy Ivanov {
60*58a2b000SEvgeniy Ivanov 
61*58a2b000SEvgeniy Ivanov 	return mapio() ? -1 : 0;
62*58a2b000SEvgeniy Ivanov }
63*58a2b000SEvgeniy Ivanov 
64*58a2b000SEvgeniy Ivanov int
pcifinddev(int vid,int did,pcihdl_t * handle)65*58a2b000SEvgeniy Ivanov pcifinddev(int vid, int did, pcihdl_t *handle)
66*58a2b000SEvgeniy Ivanov {
67*58a2b000SEvgeniy Ivanov 	int i;
68*58a2b000SEvgeniy Ivanov 
69*58a2b000SEvgeniy Ivanov 	for (i = 0; i < 32; i++) {
70*58a2b000SEvgeniy Ivanov 		pcihdl_t h;
71*58a2b000SEvgeniy Ivanov 		int id;
72*58a2b000SEvgeniy Ivanov 		h = maketag(0, i, 0);
73*58a2b000SEvgeniy Ivanov 		pcicfgread(&h, 0, &id);
74*58a2b000SEvgeniy Ivanov 		if (id == (vid | (did << 16))) {
75*58a2b000SEvgeniy Ivanov 			*handle = h;
76*58a2b000SEvgeniy Ivanov 			return 0;
77*58a2b000SEvgeniy Ivanov 		}
78*58a2b000SEvgeniy Ivanov 	}
79*58a2b000SEvgeniy Ivanov 	return -1;
80*58a2b000SEvgeniy Ivanov }
81*58a2b000SEvgeniy Ivanov 
82*58a2b000SEvgeniy Ivanov int
pcicfgread(pcihdl_t * handle,int off,int * val)83*58a2b000SEvgeniy Ivanov pcicfgread(pcihdl_t *handle, int off, int *val)
84*58a2b000SEvgeniy Ivanov {
85*58a2b000SEvgeniy Ivanov 	int data;
86*58a2b000SEvgeniy Ivanov 
87*58a2b000SEvgeniy Ivanov 	outl(PCI_MODE1_ADDRESS_REG, *handle | off);
88*58a2b000SEvgeniy Ivanov 	data = inl(PCI_MODE1_DATA_REG);
89*58a2b000SEvgeniy Ivanov 	outl(PCI_MODE1_ADDRESS_REG, 0);
90*58a2b000SEvgeniy Ivanov 	*val = data;
91*58a2b000SEvgeniy Ivanov 	return 0;
92*58a2b000SEvgeniy Ivanov }
93*58a2b000SEvgeniy Ivanov 
94*58a2b000SEvgeniy Ivanov int
pcicfgwrite(pcihdl_t * handle,int off,int val)95*58a2b000SEvgeniy Ivanov pcicfgwrite(pcihdl_t *handle, int off, int val)
96*58a2b000SEvgeniy Ivanov {
97*58a2b000SEvgeniy Ivanov 	outl(PCI_MODE1_ADDRESS_REG, *handle | off);
98*58a2b000SEvgeniy Ivanov 	outl(PCI_MODE1_DATA_REG, val);
99*58a2b000SEvgeniy Ivanov 	outl(PCI_MODE1_ADDRESS_REG, 0);
100*58a2b000SEvgeniy Ivanov 	return 0;
101*58a2b000SEvgeniy Ivanov }
102