xref: /freebsd/sys/dev/acpica/Osd/OsdHardware.c (revision a220d00e74dd245b4fca59c5eca0c53963686325)
1 /*-
2  * Copyright (c) 2000, 2001 Michael Smith
3  * Copyright (c) 2000 BSDi
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 
30 /*
31  * 6.7 : Hardware Abstraction
32  */
33 
34 #include "acpi.h"
35 
36 #include <machine/bus_pio.h>
37 #include <machine/bus.h>
38 #include <machine/pci_cfgreg.h>
39 
40 /*
41  * ACPICA's rather gung-ho approach to hardware resource ownership is a little
42  * troublesome insofar as there is no easy way for us to know in advance
43  * exactly which I/O resources it's going to want to use.
44  *
45  * In order to deal with this, we ignore resource ownership entirely, and simply
46  * use the native I/O space accessor functionality.  This is Evil, but it works.
47  *
48  * XXX use an intermediate #define for the tag/handle
49  */
50 
51 #ifdef __i386__
52 #define ACPI_BUS_SPACE_IO	I386_BUS_SPACE_IO
53 #define ACPI_BUS_HANDLE		0
54 #endif
55 #ifdef __ia64__
56 #define ACPI_BUS_SPACE_IO	IA64_BUS_SPACE_IO
57 #define ACPI_BUS_HANDLE		0
58 #endif
59 
60 ACPI_STATUS
61 AcpiOsReadPort (
62     ACPI_IO_ADDRESS	InPort,
63     void		*Value,
64     UINT32		Width)
65 {
66     switch (Width) {
67     case 8:
68         *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort);
69         break;
70     case 16:
71         *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort);
72         break;
73     case 32:
74         *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, InPort);
75         break;
76     default:
77         /* debug trap goes here */
78     }
79 
80     return(AE_OK);
81 }
82 
83 ACPI_STATUS
84 AcpiOsWritePort (
85     ACPI_IO_ADDRESS	OutPort,
86     NATIVE_UINT		Value,
87     UINT32		Width)
88 {
89     switch (Width) {
90     case 8:
91         bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
92         break;
93     case 16:
94         bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
95         break;
96     case 32:
97         bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
98         break;
99     default:
100         /* debug trap goes here */
101     }
102 
103     return(AE_OK);
104 }
105 
106 ACPI_STATUS
107 AcpiOsReadPciConfiguration (
108     ACPI_PCI_ID		*PciId,
109     UINT32		Register,
110     void		*Value,
111     UINT32		Width)
112 {
113     u_int32_t	byte_width = Width / 8;
114     u_int32_t	val;
115 
116     if (!pci_cfgregopen())
117         return(AE_NOT_EXIST);
118 
119     val = pci_cfgregread(PciId->Bus, PciId->Device, PciId->Function, Register, byte_width);
120     switch (Width) {
121     case 8:
122 	*(u_int8_t *)Value = val & 0xff;
123 	break;
124     case 16:
125 	*(u_int16_t *)Value = val & 0xffff;
126 	break;
127     case 32:
128 	*(u_int32_t *)Value = val;
129 	break;
130     default:
131 	/* debug trap goes here */
132     }
133 
134 
135     return(AE_OK);
136 }
137 
138 
139 ACPI_STATUS
140 AcpiOsWritePciConfiguration (
141     ACPI_PCI_ID		*PciId,
142     UINT32		Register,
143     NATIVE_UINT		Value,
144     UINT32		Width)
145 {
146     u_int32_t	byte_width = Width / 8;
147 
148     if (!pci_cfgregopen())
149     	return(AE_NOT_EXIST);
150 
151     pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register, Value, byte_width);
152 
153     return(AE_OK);
154 }
155