Carl-Daniel Hailfinger | 5d5c072 | 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 Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | /* |
Carl-Daniel Hailfinger | 5d5c072 | 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 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 23 | #include "platform.h" |
| 24 | |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 25 | #if NEED_PCI == 1 |
Carl-Daniel Hailfinger | 7237683 | 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 |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 32 | |
Nico Huber | c0e1c4b | 2022-12-13 21:55:22 +0000 | [diff] [blame] | 33 | #if !defined (PCIUTILS_PCI_H) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 34 | #include <pci/pci.h> |
Stefan Tauner | c65b855 | 2013-09-12 15:48:39 +0000 | [diff] [blame] | 35 | #else |
| 36 | #include <pciutils/pci.h> |
| 37 | #endif |
| 38 | |
Carl-Daniel Hailfinger | 7237683 | 2010-06-25 13:18:48 +0000 | [diff] [blame] | 39 | #undef index |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 40 | #endif /* NEED_PCI == 1 */ |
| 41 | |
Thomas Heijligen | cce1e5b | 2021-11-02 20:33:35 +0100 | [diff] [blame] | 42 | void mmio_writeb(uint8_t val, void *addr); |
| 43 | void mmio_writew(uint16_t val, void *addr); |
| 44 | void mmio_writel(uint32_t val, void *addr); |
| 45 | uint8_t mmio_readb(const void *addr); |
| 46 | uint16_t mmio_readw(const void *addr); |
| 47 | uint32_t mmio_readl(const void *addr); |
| 48 | void mmio_readn(const void *addr, uint8_t *buf, size_t len); |
| 49 | void mmio_le_writeb(uint8_t val, void *addr); |
| 50 | void mmio_le_writew(uint16_t val, void *addr); |
| 51 | void mmio_le_writel(uint32_t val, void *addr); |
| 52 | uint8_t mmio_le_readb(const void *addr); |
| 53 | uint16_t mmio_le_readw(const void *addr); |
| 54 | uint32_t mmio_le_readl(const void *addr); |
| 55 | #define pci_mmio_writeb mmio_le_writeb |
| 56 | #define pci_mmio_writew mmio_le_writew |
| 57 | #define pci_mmio_writel mmio_le_writel |
| 58 | #define pci_mmio_readb mmio_le_readb |
| 59 | #define pci_mmio_readw mmio_le_readw |
| 60 | #define pci_mmio_readl mmio_le_readl |
| 61 | void rmmio_writeb(uint8_t val, void *addr); |
| 62 | void rmmio_writew(uint16_t val, void *addr); |
| 63 | void rmmio_writel(uint32_t val, void *addr); |
| 64 | void rmmio_le_writeb(uint8_t val, void *addr); |
| 65 | void rmmio_le_writew(uint16_t val, void *addr); |
| 66 | void rmmio_le_writel(uint32_t val, void *addr); |
| 67 | #define pci_rmmio_writeb rmmio_le_writeb |
| 68 | #define pci_rmmio_writew rmmio_le_writew |
| 69 | #define pci_rmmio_writel rmmio_le_writel |
| 70 | void rmmio_valb(void *addr); |
| 71 | void rmmio_valw(void *addr); |
| 72 | void rmmio_vall(void *addr); |
| 73 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 74 | #define ___constant_swab8(x) ((uint8_t) ( \ |
| 75 | (((uint8_t)(x) & (uint8_t)0xffU)))) |
| 76 | |
| 77 | #define ___constant_swab16(x) ((uint16_t) ( \ |
| 78 | (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \ |
| 79 | (((uint16_t)(x) & (uint16_t)0xff00U) >> 8))) |
| 80 | |
| 81 | #define ___constant_swab32(x) ((uint32_t) ( \ |
| 82 | (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ |
| 83 | (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ |
| 84 | (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ |
| 85 | (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24))) |
| 86 | |
| 87 | #define ___constant_swab64(x) ((uint64_t) ( \ |
| 88 | (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ |
| 89 | (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ |
| 90 | (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ |
| 91 | (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ |
| 92 | (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ |
| 93 | (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ |
| 94 | (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ |
| 95 | (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56))) |
| 96 | |
| 97 | #if defined (__FLASHROM_BIG_ENDIAN__) |
| 98 | |
| 99 | #define cpu_to_le(bits) \ |
| 100 | static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \ |
| 101 | { \ |
| 102 | return ___constant_swab##bits(val); \ |
| 103 | } |
| 104 | |
| 105 | cpu_to_le(8) |
| 106 | cpu_to_le(16) |
| 107 | cpu_to_le(32) |
| 108 | cpu_to_le(64) |
| 109 | |
| 110 | #define cpu_to_be8 |
| 111 | #define cpu_to_be16 |
| 112 | #define cpu_to_be32 |
| 113 | #define cpu_to_be64 |
| 114 | |
| 115 | #elif defined (__FLASHROM_LITTLE_ENDIAN__) |
| 116 | |
| 117 | #define cpu_to_be(bits) \ |
| 118 | static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \ |
| 119 | { \ |
| 120 | return ___constant_swab##bits(val); \ |
| 121 | } |
| 122 | |
| 123 | cpu_to_be(8) |
| 124 | cpu_to_be(16) |
| 125 | cpu_to_be(32) |
| 126 | cpu_to_be(64) |
| 127 | |
| 128 | #define cpu_to_le8 |
| 129 | #define cpu_to_le16 |
| 130 | #define cpu_to_le32 |
| 131 | #define cpu_to_le64 |
| 132 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 133 | #endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 134 | |
| 135 | #define be_to_cpu8 cpu_to_be8 |
| 136 | #define be_to_cpu16 cpu_to_be16 |
| 137 | #define be_to_cpu32 cpu_to_be32 |
| 138 | #define be_to_cpu64 cpu_to_be64 |
| 139 | #define le_to_cpu8 cpu_to_le8 |
| 140 | #define le_to_cpu16 cpu_to_le16 |
| 141 | #define le_to_cpu32 cpu_to_le32 |
| 142 | #define le_to_cpu64 cpu_to_le64 |
| 143 | |
Carl-Daniel Hailfinger | 16c0aec | 2016-02-20 21:43:56 +0000 | [diff] [blame] | 144 | #if NEED_RAW_ACCESS == 1 |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 145 | #if IS_X86 |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 146 | |
Anastasia Klimchuk | 340b20f | 2021-04-27 16:13:32 +1000 | [diff] [blame] | 147 | #include "hwaccess_x86_io.h" |
Jonathan A. Kollasch | 3646c8f | 2010-01-08 21:18:08 +0000 | [diff] [blame] | 148 | |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 149 | #if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 150 | typedef struct { uint32_t hi, lo; } msr_t; |
| 151 | msr_t rdmsr(int addr); |
| 152 | int wrmsr(int addr, msr_t msr); |
| 153 | #endif |
Carl-Daniel Hailfinger | a5eecda | 2012-02-25 22:50:21 +0000 | [diff] [blame] | 154 | #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__) |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 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 |
| 160 | typedef struct { uint32_t hi, lo; } msr_t; |
| 161 | msr_t freebsd_rdmsr(int addr); |
| 162 | int freebsd_wrmsr(int addr, msr_t msr); |
| 163 | #endif |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 164 | #if defined(__LIBPAYLOAD__) |
| 165 | #include <arch/io.h> |
| 166 | #include <arch/msr.h> |
| 167 | typedef struct { uint32_t hi, lo; } msr_t; |
| 168 | msr_t libpayload_rdmsr(int addr); |
| 169 | int libpayload_wrmsr(int addr, msr_t msr); |
| 170 | #undef rdmsr |
| 171 | #define rdmsr libpayload_rdmsr |
| 172 | #define wrmsr libpayload_wrmsr |
| 173 | #endif |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 174 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 175 | #elif IS_PPC |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 176 | |
| 177 | /* PCI port I/O is not yet implemented on PowerPC. */ |
| 178 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 179 | #elif IS_MIPS |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 180 | |
| 181 | /* PCI port I/O is not yet implemented on MIPS. */ |
| 182 | |
Stefan Tauner | fb2d77c | 2015-02-10 08:03:10 +0000 | [diff] [blame] | 183 | #elif IS_SPARC |
| 184 | |
| 185 | /* PCI port I/O is not yet implemented on SPARC. */ |
| 186 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 187 | #elif IS_ARM |
David Hendricks | b286da7 | 2012-02-13 00:35:35 +0000 | [diff] [blame] | 188 | |
| 189 | /* Non memory mapped I/O is not supported on ARM. */ |
| 190 | |
Rosen Penev | 34d07f0 | 2019-07-02 00:14:01 -0700 | [diff] [blame] | 191 | #elif IS_ARC |
| 192 | |
| 193 | /* Non memory mapped I/O is not supported on ARC. */ |
| 194 | |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 195 | #else |
| 196 | |
| 197 | #error Unknown architecture, please check if it supports PCI port IO. |
| 198 | |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 199 | #endif /* IS_* */ |
Carl-Daniel Hailfinger | 16c0aec | 2016-02-20 21:43:56 +0000 | [diff] [blame] | 200 | #endif /* NEED_RAW_ACCESS == 1 */ |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 201 | |
Carl-Daniel Hailfinger | 5d5c072 | 2009-12-14 03:32:24 +0000 | [diff] [blame] | 202 | #endif /* !__HWACCESS_H__ */ |