blob: 2bc2927b07bd043e4ddf1f466cbef1b5f7d999d6 [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
18 *
19 *
20 * Header file for hardware access and OS abstraction. Included from flash.h.
21 */
22
23#ifndef __HWACCESS_H__
24#define __HWACCESS_H__ 1
25
26#if defined(__GLIBC__)
27#include <sys/io.h>
28#endif
29#if NEED_PCI == 1
30#include <pci/pci.h>
31#endif
32
33/* for iopl and outb under Solaris */
34#if defined (__sun) && (defined(__i386) || defined(__amd64))
35#include <strings.h>
36#include <sys/sysi86.h>
37#include <sys/psw.h>
38#include <asm/sunddi.h>
39#endif
40
41#if (defined(__MACH__) && defined(__APPLE__))
42#define __DARWIN__
43#endif
44
45#if defined(__FreeBSD__) || defined(__DragonFly__)
46 #include <machine/cpufunc.h>
47 #define off64_t off_t
48 #define lseek64 lseek
49 #define OUTB(x, y) do { u_int tmp = (y); outb(tmp, (x)); } while (0)
50 #define OUTW(x, y) do { u_int tmp = (y); outw(tmp, (x)); } while (0)
51 #define OUTL(x, y) do { u_int tmp = (y); outl(tmp, (x)); } while (0)
52 #define INB(x) __extension__ ({ u_int tmp = (x); inb(tmp); })
53 #define INW(x) __extension__ ({ u_int tmp = (x); inw(tmp); })
54 #define INL(x) __extension__ ({ u_int tmp = (x); inl(tmp); })
55#else
56#if defined(__DARWIN__)
57 #include <DirectIO/darwinio.h>
58 #define off64_t off_t
59 #define lseek64 lseek
60#endif
61#if defined (__sun) && (defined(__i386) || defined(__amd64))
62 /* Note different order for outb */
63 #define OUTB(x,y) outb(y, x)
64 #define OUTW(x,y) outw(y, x)
65 #define OUTL(x,y) outl(y, x)
66 #define INB inb
67 #define INW inw
68 #define INL inl
69#else
70 #define OUTB outb
71 #define OUTW outw
72 #define OUTL outl
73 #define INB inb
74 #define INW inw
75 #define INL inl
76#endif
77#endif
78
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +000079#if defined(__NetBSD__)
80 #define off64_t off_t
81 #define lseek64 lseek
82 #if defined(__i386__) || defined(__x86_64__)
83 #include <sys/types.h>
84 #include <machine/sysarch.h>
85 #if defined(__i386__)
86 #define iopl i386_iopl
87 #elif defined(__x86_64__)
88 #define iopl x86_64_iopl
89 #endif
90 #include <stdint.h>
91
92static inline void
93outb(uint8_t value, uint16_t port)
94{
95 asm volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
96}
97
98static inline uint8_t
99inb(uint16_t port)
100{
101 uint8_t value;
102 asm volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
103 return value;
104}
105
106static inline void
107outw(uint16_t value, uint16_t port)
108{
109 asm volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
110}
111
112static inline uint16_t
113inw(uint16_t port)
114{
115 uint16_t value;
116 asm volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
117 return value;
118}
119
120static inline void
121outl(uint32_t value, uint16_t port)
122{
123 asm volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
124}
125
126static inline uint32_t
127inl(uint16_t port)
128{
129 uint32_t value;
130 asm volatile ("inl %1,%0":"=a" (value):"Nd" (port));
131 return value;
132}
133 #endif
134#endif
135
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000136#if defined(__FreeBSD__) || defined(__DragonFly__)
137extern int io_fd;
138#endif
139
140#if !defined(__DARWIN__) && !defined(__FreeBSD__) && !defined(__DragonFly__)
141typedef struct { uint32_t hi, lo; } msr_t;
142msr_t rdmsr(int addr);
143int wrmsr(int addr, msr_t msr);
144#endif
145#if defined(__FreeBSD__) || defined(__DragonFly__)
146/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
147#undef rdmsr
148#undef wrmsr
149#define rdmsr freebsd_rdmsr
150#define wrmsr freebsd_wrmsr
151typedef struct { uint32_t hi, lo; } msr_t;
152msr_t freebsd_rdmsr(int addr);
153int freebsd_wrmsr(int addr, msr_t msr);
154#endif
155
156#endif /* !__HWACCESS_H__ */