blob: 06817ab49320e05016f1db6dd2fc1d9ebaf49e02 [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
71 #define OUTB outb
72 #define OUTW outw
73 #define OUTL outl
74 #define INB inb
75 #define INW inw
76 #define INL inl
77#endif
78#endif
79
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +000080#if defined(__NetBSD__)
81 #define off64_t off_t
82 #define lseek64 lseek
83 #if defined(__i386__) || defined(__x86_64__)
84 #include <sys/types.h>
85 #include <machine/sysarch.h>
86 #if defined(__i386__)
87 #define iopl i386_iopl
88 #elif defined(__x86_64__)
89 #define iopl x86_64_iopl
90 #endif
91 #include <stdint.h>
92
Uwe Hermann43959702010-03-13 17:28:29 +000093static inline void outb(uint8_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +000094{
95 asm volatile ("outb %b0,%w1": :"a" (value), "Nd" (port));
96}
97
Uwe Hermann43959702010-03-13 17:28:29 +000098static inline uint8_t inb(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +000099{
100 uint8_t value;
101 asm volatile ("inb %w1,%0":"=a" (value):"Nd" (port));
102 return value;
103}
104
Uwe Hermann43959702010-03-13 17:28:29 +0000105static inline void outw(uint16_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000106{
107 asm volatile ("outw %w0,%w1": :"a" (value), "Nd" (port));
108}
109
Uwe Hermann43959702010-03-13 17:28:29 +0000110static inline uint16_t inw(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000111{
112 uint16_t value;
113 asm volatile ("inw %w1,%0":"=a" (value):"Nd" (port));
114 return value;
115}
116
Uwe Hermann43959702010-03-13 17:28:29 +0000117static inline void outl(uint32_t value, uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000118{
119 asm volatile ("outl %0,%w1": :"a" (value), "Nd" (port));
120}
121
Uwe Hermann43959702010-03-13 17:28:29 +0000122static inline uint32_t inl(uint16_t port)
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000123{
124 uint32_t value;
125 asm volatile ("inl %1,%0":"=a" (value):"Nd" (port));
126 return value;
127}
128 #endif
129#endif
130
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000131#if !defined(__DARWIN__) && !defined(__FreeBSD__) && !defined(__DragonFly__)
132typedef struct { uint32_t hi, lo; } msr_t;
133msr_t rdmsr(int addr);
134int wrmsr(int addr, msr_t msr);
135#endif
136#if defined(__FreeBSD__) || defined(__DragonFly__)
137/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
138#undef rdmsr
139#undef wrmsr
140#define rdmsr freebsd_rdmsr
141#define wrmsr freebsd_wrmsr
142typedef struct { uint32_t hi, lo; } msr_t;
143msr_t freebsd_rdmsr(int addr);
144int freebsd_wrmsr(int addr, msr_t msr);
145#endif
146
147#endif /* !__HWACCESS_H__ */