blob: 7a769254e416f303e4a43dc0922d346a903abf57 [file] [log] [blame]
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2009 Carl-Daniel Hailfinger
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Uwe Hermann43959702010-03-13 17:28:29 +000018 */
19
20/*
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000021 * Header file for hardware access and OS abstraction. Included from flash.h.
22 */
23
24#ifndef __HWACCESS_H__
25#define __HWACCESS_H__ 1
26
27#if defined(__GLIBC__)
28#include <sys/io.h>
29#endif
30#if NEED_PCI == 1
31#include <pci/pci.h>
32#endif
33
34/* for iopl and outb under Solaris */
35#if defined (__sun) && (defined(__i386) || defined(__amd64))
36#include <strings.h>
37#include <sys/sysi86.h>
38#include <sys/psw.h>
39#include <asm/sunddi.h>
40#endif
41
42#if (defined(__MACH__) && defined(__APPLE__))
43#define __DARWIN__
44#endif
45
46#if defined(__FreeBSD__) || defined(__DragonFly__)
47 #include <machine/cpufunc.h>
48 #define off64_t off_t
49 #define lseek64 lseek
Michael Karchere7f32092010-01-12 15:36:24 +000050 #define OUTB(x, y) do { u_int outb_tmp = (y); outb(outb_tmp, (x)); } while (0)
51 #define OUTW(x, y) do { u_int outw_tmp = (y); outw(outw_tmp, (x)); } while (0)
52 #define OUTL(x, y) do { u_int outl_tmp = (y); outl(outl_tmp, (x)); } while (0)
53 #define INB(x) __extension__ ({ u_int inb_tmp = (x); inb(inb_tmp); })
54 #define INW(x) __extension__ ({ u_int inw_tmp = (x); inw(inw_tmp); })
55 #define INL(x) __extension__ ({ u_int inl_tmp = (x); inl(inl_tmp); })
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000056#else
57#if defined(__DARWIN__)
58 #include <DirectIO/darwinio.h>
59 #define off64_t off_t
60 #define lseek64 lseek
61#endif
62#if defined (__sun) && (defined(__i386) || defined(__amd64))
63 /* Note different order for outb */
64 #define OUTB(x,y) outb(y, x)
65 #define OUTW(x,y) outw(y, x)
66 #define OUTL(x,y) outl(y, x)
67 #define INB inb
68 #define INW inw
69 #define INL inl
70#else
Rudolf Marek03ae5c12010-03-16 23:59:19 +000071
72#ifdef __DJGPP__
73
74#include <pc.h>
75
76 #define OUTB(x,y) outportb(y, x)
77 #define OUTW(x,y) outportw(y, x)
78 #define OUTL(x,y) outportl(y, x)
79
80 #define INB inportb
81 #define INW inportw
82 #define INL inportl
83
84#else
85
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000086 #define OUTB outb
87 #define OUTW outw
88 #define OUTL outl
89 #define INB inb
90 #define INW inw
91 #define INL inl
Rudolf Marek03ae5c12010-03-16 23:59:19 +000092
93#endif
94
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000095#endif
96#endif
97
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +000098#if defined(__NetBSD__)
99 #define off64_t off_t
100 #define lseek64 lseek
101 #if defined(__i386__) || defined(__x86_64__)
102 #include <sys/types.h>
103 #include <machine/sysarch.h>
104 #if defined(__i386__)
105 #define iopl i386_iopl
106 #elif defined(__x86_64__)
107 #define iopl x86_64_iopl
108 #endif
109 #include <stdint.h>
110
Uwe Hermann43959702010-03-13 17:28:29 +0000111static inline void outb(uint8_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000112{
113 asm volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
114}
115
Uwe Hermann43959702010-03-13 17:28:29 +0000116static inline uint8_t inb(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000117{
118 uint8_t value;
119 asm volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
120 return value;
121}
122
Uwe Hermann43959702010-03-13 17:28:29 +0000123static inline void outw(uint16_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000124{
125 asm volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
126}
127
Uwe Hermann43959702010-03-13 17:28:29 +0000128static inline uint16_t inw(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000129{
130 uint16_t value;
131 asm volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
132 return value;
133}
134
Uwe Hermann43959702010-03-13 17:28:29 +0000135static inline void outl(uint32_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000136{
137 asm volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
138}
139
Uwe Hermann43959702010-03-13 17:28:29 +0000140static inline uint32_t inl(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000141{
142 uint32_t value;
143 asm volatile ("inl %1,%0":"=a" (value):"Nd" (port));
144 return value;
145}
146 #endif
147#endif
148
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000149#if !defined(__DARWIN__) && !defined(__FreeBSD__) && !defined(__DragonFly__)
150typedef struct { uint32_t hi, lo; } msr_t;
151msr_t rdmsr(int addr);
152int wrmsr(int addr, msr_t msr);
153#endif
154#if defined(__FreeBSD__) || defined(__DragonFly__)
155/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
156#undef rdmsr
157#undef wrmsr
158#define rdmsr freebsd_rdmsr
159#define wrmsr freebsd_wrmsr
160typedef struct { uint32_t hi, lo; } msr_t;
161msr_t freebsd_rdmsr(int addr);
162int freebsd_wrmsr(int addr, msr_t msr);
163#endif
164
165#endif /* !__HWACCESS_H__ */