strat9_kernel/hardware/ec.rs
1/*
2 * This file is part of strat9-os.
3 *
4 * strat9-os is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * strat9-os is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with strat9-os. If not, see <https://www.gnu.org/licenses/>.
16 *
17 * Website: https://www.strat9-os.org
18 * Repository: https://github.com/strat9-os/strat9-os
19 *
20 * Copyright (C) 2023-26 Guillaume Gielly
21 */
22
23//! Embedded Controller (EC) driver for x86.
24//!
25//! Provides direct access to the ACPI Embedded Controller via I/O ports
26//! 0x62 (data) and 0x66 (command/status). Used for temperature reading,
27//! fan control, and battery management on laptops (ThinkPad X13, etc.).
28//!
29//! Reference: ACPI Embedded Controller Interface Specification 1.0a
30
31#![allow(dead_code)]
32
33use crate::arch::io::{inb, outb};
34use core::sync::atomic::{AtomicBool, Ordering};
35use spin::Mutex;
36
37/// EC data port (read/write data bytes).
38const EC_DATA_PORT: u16 = 0x62;
39
40/// EC command/status port (write commands, read status).
41const EC_SC_PORT: u16 = 0x66;
42
43/// EC status register bits.
44const EC_STATUS_OBF: u8 = 1 << 0; // Output Buffer Full : data available to read
45const EC_STATUS_IBF: u8 = 1 << 1; // Input Buffer Full : controller busy, don't write
46const EC_STATUS_SCI: u8 = 1 << 5; // SCI event pending
47
48/// EC commands.
49const EC_CMD_READ: u8 = 0x80; // Read EC register
50const EC_CMD_WRITE: u8 = 0x81; // Write EC register
51const EC_CMD_QUERY: u8 = 0x84; // Query event
52
53/// Maximum number of retries when waiting for EC to become ready.
54const EC_TIMEOUT: u32 = 100_000;
55
56/// Whether the EC was detected and initialized successfully.
57static EC_AVAILABLE: AtomicBool = AtomicBool::new(false);
58
59/// Serialize all EC access to prevent interleaved command/data sequences.
60static EC_LOCK: Mutex<()> = Mutex::new(());
61
62/// Wait for the EC input buffer to be empty (ready to accept a write).
63///
64/// Returns `true` if the IBF cleared, `false` on timeout.
65fn ec_wait_ibf() -> bool {
66 for _ in 0..EC_TIMEOUT {
67 unsafe {
68 if inb(EC_SC_PORT) & EC_STATUS_IBF == 0 {
69 return true;
70 }
71 }
72 core::hint::spin_loop();
73 }
74 false
75}
76
77/// Wait for the EC output buffer to become full (data available to read).
78///
79/// Returns `Some(byte)` if data is ready, `None` on timeout.
80fn ec_wait_obf() -> Option<u8> {
81 for _ in 0..EC_TIMEOUT {
82 unsafe {
83 let status = inb(EC_SC_PORT);
84 if status & EC_STATUS_OBF != 0 {
85 return Some(inb(EC_DATA_PORT));
86 }
87 }
88 core::hint::spin_loop();
89 }
90 None
91}
92
93/// Read a single byte from an EC register.
94///
95/// # Safety
96/// The caller must ensure no concurrent EC access occurs without holding `EC_LOCK`.
97unsafe fn ec_read_byte(reg: u8) -> Option<u8> {
98 if !ec_wait_ibf() {
99 return None;
100 }
101 outb(EC_SC_PORT, EC_CMD_READ);
102 if !ec_wait_ibf() {
103 return None;
104 }
105 outb(EC_DATA_PORT, reg);
106 ec_wait_obf()
107}
108
109/// Write a single byte to an EC register.
110///
111/// # Safety
112/// The caller must ensure no concurrent EC access occurs without holding `EC_LOCK`.
113unsafe fn ec_write_byte(reg: u8, val: u8) -> bool {
114 if !ec_wait_ibf() {
115 return false;
116 }
117 outb(EC_SC_PORT, EC_CMD_WRITE);
118 if !ec_wait_ibf() {
119 return false;
120 }
121 outb(EC_DATA_PORT, reg);
122 if !ec_wait_ibf() {
123 return false;
124 }
125 outb(EC_DATA_PORT, val);
126 true
127}
128
129/// Query the EC for a pending event. Returns the event byte (Query ID).
130///
131/// # Safety
132/// The caller must ensure no concurrent EC access occurs without holding `EC_LOCK`.
133unsafe fn ec_query() -> Option<u8> {
134 if !ec_wait_ibf() {
135 return None;
136 }
137 outb(EC_SC_PORT, EC_CMD_QUERY);
138 ec_wait_obf()
139}
140
141// =========================================================================
142// Public API
143// =========================================================================
144
145/// Detect and initialize the Embedded Controller.
146///
147/// Probes port 0x66 to verify the EC is present. On most x86 laptops the EC
148/// is always present; this mainly serves to confirm the I/O ports are wired.
149pub fn init() {
150 // Try reading EC status to see if the port responds.
151 // A non-zero/0xFF response indicates a live EC.
152 let _lock = EC_LOCK.lock();
153 unsafe {
154 let status = inb(EC_SC_PORT);
155 if status != 0xFF {
156 EC_AVAILABLE.store(true, Ordering::Release);
157 log::info!(
158 "[EC] Embedded Controller detected (status=0x{:02x})",
159 status
160 );
161 } else {
162 log::warn!("[EC] No Embedded Controller found (status=0xFF)");
163 }
164 }
165}
166
167/// Returns whether the EC was detected.
168pub fn is_available() -> bool {
169 EC_AVAILABLE.load(Ordering::Acquire)
170}
171
172/// Read a byte from an EC register.
173pub fn read(reg: u8) -> Option<u8> {
174 let _lock = EC_LOCK.lock();
175 if !EC_AVAILABLE.load(Ordering::Acquire) {
176 return None;
177 }
178 unsafe { ec_read_byte(reg) }
179}
180
181/// Write a byte to an EC register.
182pub fn write(reg: u8, val: u8) -> bool {
183 let _lock = EC_LOCK.lock();
184 if !EC_AVAILABLE.load(Ordering::Acquire) {
185 return false;
186 }
187 unsafe { ec_write_byte(reg, val) }
188}
189
190/// Query a pending EC event (SCI). Returns the query byte.
191pub fn query_event() -> Option<u8> {
192 let _lock = EC_LOCK.lock();
193 if !EC_AVAILABLE.load(Ordering::Acquire) {
194 return None;
195 }
196 unsafe { ec_query() }
197}
198
199/// Check if there is a pending SCI event in the EC.
200pub fn has_sci_event() -> bool {
201 if !EC_AVAILABLE.load(Ordering::Acquire) {
202 return false;
203 }
204 unsafe { inb(EC_SC_PORT) & EC_STATUS_SCI != 0 }
205}