blob: 5097ac99b8f15ae6182e7716c583db779be90ef0 [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
Stefan Tauner8d21ff12015-01-10 09:33:06 +000033#if !defined (__NetBSD__)
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
Carl-Daniel Hailfingercceafa22010-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
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000101#endif /* __FLASHROM_BIG_ENDIAN__ / __FLASHROM_LITTLE_ENDIAN__ */
Carl-Daniel Hailfingercceafa22010-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
Carl-Daniel Hailfinger16c0aec2016-02-20 21:43:56 +0000112#if NEED_RAW_ACCESS == 1
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000113#if IS_X86
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000114
Anastasia Klimchuk3f19ba92021-04-27 16:13:32 +1000115#include "hwaccess_x86_io.h"
Jonathan A. Kollasch3646c8f2010-01-08 21:18:08 +0000116
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +0000117#if !(defined(__MACH__) && defined(__APPLE__)) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__) && !defined(__DragonFly__) && !defined(__LIBPAYLOAD__)
Carl-Daniel Hailfinger5d5c0722009-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
Carl-Daniel Hailfingera5eecda2012-02-25 22:50:21 +0000122#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
Carl-Daniel Hailfinger5d5c0722009-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
Patrick Georgia9095a92010-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
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000142
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000143#elif IS_PPC
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000144
145/* PCI port I/O is not yet implemented on PowerPC. */
146
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000147#elif IS_MIPS
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000148
149/* PCI port I/O is not yet implemented on MIPS. */
150
Stefan Taunerfb2d77c2015-02-10 08:03:10 +0000151#elif IS_SPARC
152
153/* PCI port I/O is not yet implemented on SPARC. */
154
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000155#elif IS_ARM
David Hendricksb286da72012-02-13 00:35:35 +0000156
157/* Non memory mapped I/O is not supported on ARM. */
158
Rosen Penev34d07f02019-07-02 00:14:01 -0700159#elif IS_ARC
160
161/* Non memory mapped I/O is not supported on ARC. */
162
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000163#else
164
165#error Unknown architecture, please check if it supports PCI port IO.
166
Stefan Taunerb0eee9b2015-01-10 09:32:50 +0000167#endif /* IS_* */
Carl-Daniel Hailfinger16c0aec2016-02-20 21:43:56 +0000168#endif /* NEED_RAW_ACCESS == 1 */
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000169
Carl-Daniel Hailfinger5d5c0722009-12-14 03:32:24 +0000170#endif /* !__HWACCESS_H__ */