forked from flashrom/flashrom
-
Notifications
You must be signed in to change notification settings - Fork 1
/
acpi_ec.c
117 lines (90 loc) · 2.64 KB
/
acpi_ec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* This file is part of the flashrom project.
*
* Copyright (C) 2021, TUXEDO Computers GmbH
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#if defined(__i386__) || defined(__x86_64__)
#include <stdbool.h>
#include <stdint.h>
#include "acpi_ec.h"
#include "flash.h"
#include "hwaccess_x86_io.h"
/* Standard ports */
#define EC_DATA 0x62
#define EC_STS_CMD 0x66
/* Standard commands */
#define EC_CMD_READ_REG 0x80 /* Read register's value */
#define EC_CMD_WRITE_REG 0x81 /* Write register's value */
/* Some of the status bits */
#define EC_STS_IBF (1 << 1) /* EC's input buffer full (host can't write) */
#define EC_STS_OBF (1 << 0) /* EC's output buffer full (host can read) */
bool ec_wait_for_ibuf(unsigned int max_checks)
{
unsigned int i;
if (!max_checks)
max_checks = EC_MAX_STATUS_CHECKS;
for (i = 0; (INB(EC_STS_CMD) & EC_STS_IBF) != 0; ++i) {
if (i == max_checks)
return false;
}
return true;
}
bool ec_wait_for_obuf(unsigned int max_checks)
{
unsigned int i;
if (!max_checks)
max_checks = EC_MAX_STATUS_CHECKS;
for (i = 0; (INB(EC_STS_CMD) & EC_STS_OBF) == 0; ++i) {
if (i == max_checks)
return false;
}
return true;
}
bool ec_write_cmd(uint8_t cmd, unsigned int max_checks)
{
const bool success = ec_wait_for_ibuf(max_checks);
if (success)
OUTB(cmd, EC_STS_CMD);
return success;
}
bool ec_read_byte(uint8_t *data, unsigned int max_checks)
{
const bool success = ec_wait_for_obuf(max_checks);
if (success)
*data = INB(EC_DATA);
return success;
}
bool ec_write_byte(uint8_t data, unsigned int max_checks)
{
const bool success = ec_wait_for_ibuf(max_checks);
if (success)
OUTB(data, EC_DATA);
return success;
}
bool ec_read_reg(uint8_t address, uint8_t *data, unsigned int max_checks)
{
if (!ec_write_cmd(EC_CMD_READ_REG, max_checks))
return false;
if (!ec_write_byte(address, max_checks))
return false;
return ec_read_byte(data, max_checks);
}
bool ec_write_reg(uint8_t address, uint8_t data, unsigned int max_checks)
{
if (!ec_write_cmd(EC_CMD_WRITE_REG, max_checks))
return false;
if (!ec_write_byte(address, max_checks))
return false;
return ec_write_byte(data, max_checks);
}
#endif /* defined(__i386__) || defined(__x86_64__) */