blob: 52ca3746601351778c9ec6ca9fed2e0b489f4d9d [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.
Uwe Hermann43959702010-03-13 17:28:29 +000014 */
15
16/*
Carl-Daniel Hailfinger5d5c0722009-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
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000023#include "platform.h"
24
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000025#if NEED_PCI == 1
Carl-Daniel Hailfinger72376832010-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
Stefan Taunerc65b8552013-09-12 15:48:39 +000032
Nico Huberc0e1c4b2022-12-13 21:55:22 +000033#if !defined (PCIUTILS_PCI_H)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +000034#include <pci/pci.h>
Stefan Taunerc65b8552013-09-12 15:48:39 +000035#else
36#include <pciutils/pci.h>
37#endif
38
Carl-Daniel Hailfinger72376832010-06-25 13:18:48 +000039#undef index
Stefan Taunerb0eee9b2015-01-10 09:32:50 +000040#endif /* NEED_PCI == 1 */
41
Thomas Heijligencce1e5b2021-11-02 20:33:35 +010042void mmio_writeb(uint8_t val, void *addr);
43void mmio_writew(uint16_t val, void *addr);
44void mmio_writel(uint32_t val, void *addr);
45uint8_t mmio_readb(const void *addr);
46uint16_t mmio_readw(const void *addr);
47uint32_t mmio_readl(const void *addr);
48void mmio_readn(const void *addr, uint8_t *buf, size_t len);
49void mmio_le_writeb(uint8_t val, void *addr);
50void mmio_le_writew(uint16_t val, void *addr);
51void mmio_le_writel(uint32_t val, void *addr);
52uint8_t mmio_le_readb(const void *addr);
53uint16_t mmio_le_readw(const void *addr);
54uint32_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
61void rmmio_writeb(uint8_t val, void *addr);
62void rmmio_writew(uint16_t val, void *addr);
63void rmmio_writel(uint32_t val, void *addr);
64void rmmio_le_writeb(uint8_t val, void *addr);
65void rmmio_le_writew(uint16_t val, void *addr);
66void 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
70void rmmio_valb(void *addr);
71void rmmio_valw(void *addr);
72void rmmio_vall(void *addr);
73
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +000074#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) \
100static inline uint##bits##_t cpu_to_le##bits(uint##bits##_t val) \
101{ \
102 return ___constant_swab##bits(val); \
103}
104
105cpu_to_le(8)
106cpu_to_le(16)
107cpu_to_le(32)
108cpu_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) \
118static inline uint##bits##_t cpu_to_be##bits(uint##bits##_t val) \
119{ \
120 return ___constant_swab##bits(val); \
121}
122
123cpu_to_be(8)
124cpu_to_be(16)
125cpu_to_be(32)
126cpu_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 Taunerb0eee9b2015-01-10 09:32:50 +0000133#endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000134
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 Hailfinger16c0aec2016-02-20 21:43:56 +0000144#if NEED_RAW_ACCESS == 1
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000145#if IS_X86
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000146
Anastasia Klimchuk340b20f2021-04-27 16:13:32 +1000147#include "hwaccess_x86_io.h"
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000148
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000149#if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000150typedef struct { uint32_t hi, lo; } msr_t;
151msr_t rdmsr(int addr);
152int wrmsr(int addr, msr_t msr);
153#endif
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000154#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000155/* FreeBSD already has conflicting definitions for wrmsr/rdmsr. */
156#undef rdmsr
157#undef wrmsr
158#define rdmsr freebsd_rdmsr
159#define wrmsr freebsd_wrmsr
160typedef struct { uint32_t hi, lo; } msr_t;
161msr_t freebsd_rdmsr(int addr);
162int freebsd_wrmsr(int addr, msr_t msr);
163#endif
Patrick Georgia9095a92010-09-30 17:03:32 +0000164#if defined(__LIBPAYLOAD__)
165#include <arch/io.h>
166#include <arch/msr.h>
167typedef struct { uint32_t hi, lo; } msr_t;
168msr_t libpayload_rdmsr(int addr);
169int libpayload_wrmsr(int addr, msr_t msr);
170#undef rdmsr
171#define rdmsr libpayload_rdmsr
172#define wrmsr libpayload_wrmsr
173#endif
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000174
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000175#elif IS_PPC
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000176
177/* PCI port I/O is not yet implemented on PowerPC. */
178
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000179#elif IS_MIPS
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000180
181/* PCI port I/O is not yet implemented on MIPS. */
182
Stefan Taunerfb2d77c2015-02-10 08:03:10 +0000183#elif IS_SPARC
184
185/* PCI port I/O is not yet implemented on SPARC. */
186
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000187#elif IS_ARM
David Hendricksb286da72012-02-13 00:35:35 +0000188
189/* Non memory mapped I/O is not supported on ARM. */
190
Rosen Penev34d07f02019-07-02 00:14:01 -0700191#elif IS_ARC
192
193/* Non memory mapped I/O is not supported on ARC. */
194
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000195#else
196
197#error Unknown architecture, please check if it supports PCI port IO.
198
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000199#endif /* IS_* */
Carl-Daniel Hailfinger16c0aec2016-02-20 21:43:56 +0000200#endif /* NEED_RAW_ACCESS == 1 */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000201
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000202#endif /* !__HWACCESS_H__ */