hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 1 | /* |
| 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. |
uwe | f6f94d4 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 17 | * 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 Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 23 | #include "platform.h" |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 24 | |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 25 | #if NEED_PCI == 1 |
hailfinger | 5acbfd6 | 2010-06-25 13:18:48 +0000 | [diff] [blame] | 26 | /* |
| 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 Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 32 | |
| 33 | #if !defined (__NetBSD__) |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 34 | #include <pci/pci.h> |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 35 | #else |
| 36 | #include <pciutils/pci.h> |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 39 | #undef index |
| 40 | #endif /* NEED_PCI == 1 */ |
| 41 | |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 42 | #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) \ |
| 68 | static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \ |
| 69 | { \ |
| 70 | return ___constant_swab##bits(val); \ |
| 71 | } |
| 72 | |
| 73 | cpu_to_le(8) |
| 74 | cpu_to_le(16) |
| 75 | cpu_to_le(32) |
| 76 | cpu_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) \ |
| 86 | static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \ |
| 87 | { \ |
| 88 | return ___constant_swab##bits(val); \ |
| 89 | } |
| 90 | |
| 91 | cpu_to_be(8) |
| 92 | cpu_to_be(16) |
| 93 | cpu_to_be(32) |
| 94 | cpu_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 Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 101 | #endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */ |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 102 | |
| 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'Callaghan | 2271a86 | 2019-09-09 00:42:55 +1000 | [diff] [blame] | 112 | #if NEED_RAW_ACCESS == 1 |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 113 | #if IS_X86 |
| 114 | |
Anastasia Klimchuk | ac09aa1 | 2021-04-27 16:13:32 +1000 | [diff] [blame] | 115 | #include "hwaccess_x86_io.h" |
hailfinger | bf8c4de | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 116 | |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 117 | #if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__) |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 118 | typedef struct { uint32_t hi, lo; } msr_t; |
| 119 | msr_t rdmsr(int addr); |
| 120 | int wrmsr(int addr, msr_t msr); |
| 121 | #endif |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 122 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 123 | /* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */ |
| 124 | #undef rdmsr |
| 125 | #undef wrmsr |
| 126 | #define rdmsr freebsd_rdmsr |
| 127 | #define wrmsr freebsd_wrmsr |
| 128 | typedef struct { uint32_t hi, lo; } msr_t; |
| 129 | msr_t freebsd_rdmsr(int addr); |
| 130 | int freebsd_wrmsr(int addr, msr_t msr); |
| 131 | #endif |
oxygene | 5027589 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 132 | #if defined(__LIBPAYLOAD__) |
| 133 | #include <arch/io.h> |
| 134 | #include <arch/msr.h> |
| 135 | typedef struct { uint32_t hi, lo; } msr_t; |
| 136 | msr_t libpayload_rdmsr(int addr); |
| 137 | int libpayload_wrmsr(int addr, msr_t msr); |
| 138 | #undef rdmsr |
| 139 | #define rdmsr libpayload_rdmsr |
| 140 | #define wrmsr libpayload_wrmsr |
| 141 | #endif |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 142 | |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 143 | #elif IS_PPC |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 144 | |
| 145 | /* PCI port I/O is not yet implemented on PowerPC. */ |
| 146 | |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 147 | #elif IS_MIPS |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 148 | |
| 149 | /* PCI port I/O is not yet implemented on MIPS. */ |
| 150 | |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 151 | #elif IS_SPARC |
| 152 | |
| 153 | /* PCI port I/O is not yet implemented on SPARC. */ |
| 154 | |
| 155 | #elif IS_ARM |
David Hendricks | cf239a8 | 2011-03-24 19:06:23 -0700 | [diff] [blame] | 156 | |
| 157 | /* Non memory mapped I/O is not supported on ARM. */ |
| 158 | |
Edward O'Callaghan | b104790 | 2020-05-14 14:21:41 +1000 | [diff] [blame] | 159 | #elif IS_ARC |
| 160 | |
| 161 | /* Non memory mapped I/O is not supported on ARC. */ |
| 162 | |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 163 | #else |
| 164 | |
| 165 | #error Unknown architecture, please check if it supports PCI port IO. |
| 166 | |
Patrick Georgi | 32dd1ed | 2017-03-09 10:15:39 +0100 | [diff] [blame] | 167 | #endif /* IS_* */ |
Edward O'Callaghan | 2271a86 | 2019-09-09 00:42:55 +1000 | [diff] [blame] | 168 | #endif /* NEED_RAW_ACCESS == 1 */ |
hailfinger | 324a9cc | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 169 | |
hailfinger | 088dc81 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 170 | #endif /* !__HWACCESS_H__ */ |