blob: 5097ac99b8f15ae6182e7716c583db779be90ef0 [file] [log] [blame]
hailfinger088dc812009-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.
uwef6f94d42010-03-13 17:28:29 +000014 */
15
16/*
hailfinger088dc812009-12-14 03:32:24 +000017 * Header file for hardware access and OS abstraction. Included from flash.h.
18 */
19
20#ifndef __HWACCESS_H__
21#define __HWACCESS_H__ 1
22
Patrick Georgi32dd1ed2017-03-09 10:15:39 +010023#include "platform.h"
hailfinger324a9cc2010-05-26 01:45:41 +000024
hailfinger088dc812009-12-14 03:32:24 +000025#if NEED_PCI == 1
hailfinger5acbfd62010-06-25 13:18:48 +000026/*
27 * libpci headers use the variable name "index" which triggers shadowing
28 * warnings on systems which have the index() function in a default #include
29 * or as builtin.
30 */
31#define index shadow_workaround_index
Patrick Georgi32dd1ed2017-03-09 10:15:39 +010032
33#if !defined (__NetBSD__)
hailfinger088dc812009-12-14 03:32:24 +000034#include <pci/pci.h>
Patrick Georgi32dd1ed2017-03-09 10:15:39 +010035#else
36#include <pciutils/pci.h>
hailfinger088dc812009-12-14 03:32:24 +000037#endif
38
Patrick Georgi32dd1ed2017-03-09 10:15:39 +010039#undef index
40#endif /* NEED_PCI == 1 */
41
hailfinger324a9cc2010-05-26 01:45:41 +000042#define ___constant_swab8(x) ((uint8_t) ( \
43 (((uint8_t)(x) & (uint8_t)0xffU))))
44
45#define ___constant_swab16(x) ((uint16_t) ( \
46 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \
47 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
48
49#define ___constant_swab32(x) ((uint32_t) ( \
50 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \
51 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \
52 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \
53 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
54
55#define ___constant_swab64(x) ((uint64_t) ( \
56 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \
57 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \
58 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \
59 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \
60 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \
61 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \
62 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \
63 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
64
65#if defined (__FLASHROM_BIG_ENDIAN__)
66
67#define cpu_to_le(bits) \
68static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \
69{ \
70 return ___constant_swab##bits(val); \
71}
72
73cpu_to_le(8)
74cpu_to_le(16)
75cpu_to_le(32)
76cpu_to_le(64)
77
78#define cpu_to_be8
79#define cpu_to_be16
80#define cpu_to_be32
81#define cpu_to_be64
82
83#elif defined (__FLASHROM_LITTLE_ENDIAN__)
84
85#define cpu_to_be(bits) \
86static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \
87{ \
88 return ___constant_swab##bits(val); \
89}
90
91cpu_to_be(8)
92cpu_to_be(16)
93cpu_to_be(32)
94cpu_to_be(64)
95
96#define cpu_to_le8
97#define cpu_to_le16
98#define cpu_to_le32
99#define cpu_to_le64
100
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100101#endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */
hailfinger324a9cc2010-05-26 01:45:41 +0000102
103#define be_to_cpu8 cpu_to_be8
104#define be_to_cpu16 cpu_to_be16
105#define be_to_cpu32 cpu_to_be32
106#define be_to_cpu64 cpu_to_be64
107#define le_to_cpu8 cpu_to_le8
108#define le_to_cpu16 cpu_to_le16
109#define le_to_cpu32 cpu_to_le32
110#define le_to_cpu64 cpu_to_le64
111
Edward O'Callaghan2271a862019-09-09 00:42:55 +1000112#if NEED_RAW_ACCESS == 1
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100113#if IS_X86
114
Anastasia Klimchukac09aa12021-04-27 16:13:32 +1000115#include "hwaccess_x86_io.h"
hailfingerbf8c4de2010-01-08 21:18:08 +0000116
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100117#if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
hailfinger088dc812009-12-14 03:32:24 +0000118typedef struct { uint32_t hi, lo; } msr_t;
119msr_t rdmsr(int addr);
120int wrmsr(int addr, msr_t msr);
121#endif
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100122#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
hailfinger088dc812009-12-14 03:32:24 +0000123/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
124#undef rdmsr
125#undef wrmsr
126#define rdmsr freebsd_rdmsr
127#define wrmsr freebsd_wrmsr
128typedef struct { uint32_t hi, lo; } msr_t;
129msr_t freebsd_rdmsr(int addr);
130int freebsd_wrmsr(int addr, msr_t msr);
131#endif
oxygene50275892010-09-30 17:03:32 +0000132#if defined(__LIBPAYLOAD__)
133#include <arch/io.h>
134#include <arch/msr.h>
135typedef struct { uint32_t hi, lo; } msr_t;
136msr_t libpayload_rdmsr(int addr);
137int libpayload_wrmsr(int addr, msr_t msr);
138#undef rdmsr
139#define rdmsr libpayload_rdmsr
140#define wrmsr libpayload_wrmsr
141#endif
hailfinger088dc812009-12-14 03:32:24 +0000142
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100143#elif IS_PPC
hailfinger324a9cc2010-05-26 01:45:41 +0000144
145/* PCI port I/O is not yet implemented on PowerPC. */
146
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100147#elif IS_MIPS
hailfinger324a9cc2010-05-26 01:45:41 +0000148
149/* PCI port I/O is not yet implemented on MIPS. */
150
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100151#elif IS_SPARC
152
153/* PCI port I/O is not yet implemented on SPARC. */
154
155#elif IS_ARM
David Hendrickscf239a82011-03-24 19:06:23 -0700156
157/* Non memory mapped I/O is not supported on ARM. */
158
Edward O'Callaghanb1047902020-05-14 14:21:41 +1000159#elif IS_ARC
160
161/* Non memory mapped I/O is not supported on ARC. */
162
hailfinger324a9cc2010-05-26 01:45:41 +0000163#else
164
165#error Unknown architecture, please check if it supports PCI port IO.
166
Patrick Georgi32dd1ed2017-03-09 10:15:39 +0100167#endif /* IS_* */
Edward O'Callaghan2271a862019-09-09 00:42:55 +1000168#endif /* NEED_RAW_ACCESS == 1 */
hailfinger324a9cc2010-05-26 01:45:41 +0000169
hailfinger088dc812009-12-14 03:32:24 +0000170#endif /* !__HWACCESS_H__ */