Some minor changes
[dragonfly.git] / sys / dev / acpica5 / Osd / OsdHardware.c
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: src/sys/dev/acpica/Osd/OsdHardware.c,v 1.13 2004/04/14 03:39:08 njl Exp $
28  * $DragonFly: src/sys/dev/acpica5/Osd/OsdHardware.c,v 1.5 2008/08/02 01:14:42 dillon Exp $
29  */
30
31 /*
32  * 6.7 : Hardware Abstraction
33  */
34
35 #include "acpi.h"
36
37 #include <sys/bus.h>
38 #include <bus/pci/pci_cfgreg.h>
39 #include <bus/pci/pcireg.h>
40
41 /*
42  * ACPICA's rather gung-ho approach to hardware resource ownership is a little
43  * troublesome insofar as there is no easy way for us to know in advance 
44  * exactly which I/O resources it's going to want to use.
45  * 
46  * In order to deal with this, we ignore resource ownership entirely, and simply
47  * use the native I/O space accessor functionality.  This is Evil, but it works.
48  *
49  * XXX use an intermediate #define for the tag/handle
50  */
51
52 #ifdef __i386__
53 #define ACPI_BUS_SPACE_IO       I386_BUS_SPACE_IO
54 #define ACPI_BUS_HANDLE         0
55 #endif
56 #ifdef __ia64__
57 #define ACPI_BUS_SPACE_IO       IA64_BUS_SPACE_IO
58 #define ACPI_BUS_HANDLE         0
59 #endif
60 #ifdef __x86_64__
61 #define ACPI_BUS_SPACE_IO       X86_64_BUS_SPACE_IO
62 #define ACPI_BUS_HANDLE         0
63 #endif
64
65 ACPI_STATUS
66 AcpiOsReadPort(ACPI_IO_ADDRESS InPort, UINT32 *Value, UINT32 Width)
67 {
68     switch (Width) {
69     case 8:
70         *(u_int8_t *)Value = bus_space_read_1(ACPI_BUS_SPACE_IO,
71             ACPI_BUS_HANDLE, InPort);
72         break;
73     case 16:
74         *(u_int16_t *)Value = bus_space_read_2(ACPI_BUS_SPACE_IO,
75             ACPI_BUS_HANDLE, InPort);
76         break;
77     case 32:
78         *(u_int32_t *)Value = bus_space_read_4(ACPI_BUS_SPACE_IO,
79             ACPI_BUS_HANDLE, InPort);
80         break;
81     default:
82         /* debug trap goes here */
83         break;
84     }
85
86     return (AE_OK);
87 }
88
89 ACPI_STATUS
90 AcpiOsWritePort(ACPI_IO_ADDRESS OutPort, UINT32 Value, UINT32 Width)
91 {
92     switch (Width) {
93     case 8:
94         bus_space_write_1(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
95         break;
96     case 16:
97         bus_space_write_2(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
98         break;
99     case 32:
100         bus_space_write_4(ACPI_BUS_SPACE_IO, ACPI_BUS_HANDLE, OutPort, Value);
101         break;
102     default:
103         /* debug trap goes here */
104         break;
105     }
106
107     return (AE_OK);
108 }
109
110 ACPI_STATUS
111 AcpiOsReadPciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register, UINT64 *Value,
112     UINT32 Width)
113 {
114     int bytes = Width / 8;
115
116     if (Width == 64)
117         return (AE_SUPPORT);
118
119     if (!pci_cfgregopen())
120         return (AE_NOT_EXIST);
121
122     *(UINT64 *)Value = pci_cfgregread(PciId->Bus, PciId->Device,
123                                       PciId->Function, Register, bytes);
124     *Value &= (1 << (bytes * 8)) - 1;
125
126     return (AE_OK);
127 }
128
129
130 ACPI_STATUS
131 AcpiOsWritePciConfiguration(ACPI_PCI_ID *PciId, UINT32 Register,
132     UINT64 Value, UINT32 Width)
133 {
134     if (Width == 64)
135         return (AE_SUPPORT);
136
137     if (!pci_cfgregopen())
138         return (AE_NOT_EXIST);
139
140     pci_cfgregwrite(PciId->Bus, PciId->Device, PciId->Function, Register,
141                     (u_int32_t) Value, Width / 8); /* XXX casting */
142
143     return (AE_OK);
144 }