|
| 1 | +use alloc::vec::Vec; |
| 2 | +use num_traits::FromPrimitive; |
| 3 | + |
| 4 | +use crate::chromium_ec::command::EcRequestRaw; |
| 5 | +use crate::chromium_ec::commands::{EcRequestGetGpuPcie, GpuVendor}; |
| 6 | +use crate::chromium_ec::i2c_passthrough::*; |
| 7 | +use crate::chromium_ec::{CrosEc, EcResult}; |
| 8 | +use crate::os_specific; |
| 9 | + |
| 10 | +pub fn get_version(ec: &CrosEc) -> EcResult<Option<Vec<u8>>> { |
| 11 | + let res = EcRequestGetGpuPcie {}.send_command(ec)?; |
| 12 | + let vendor: Option<GpuVendor> = FromPrimitive::from_u8(res.gpu_vendor); |
| 13 | + if vendor != Some(GpuVendor::NvidiaGn22) { |
| 14 | + debug!("No compatible retimer present"); |
| 15 | + return Ok(None); |
| 16 | + }; |
| 17 | + |
| 18 | + // I2C Port on the EC |
| 19 | + let i2c_port = 5; |
| 20 | + // 8-bit I2C address of the retimer |
| 21 | + // EC passthrough needs 7-bit, so shift one over before sending to EC |
| 22 | + let i2c_addr = 0x10; |
| 23 | + |
| 24 | + // Check safe mode |
| 25 | + let i2c_response = i2c_read(ec, i2c_port, i2c_addr >> 1, 0x00, 0x01)?; |
| 26 | + if i2c_response.data[0] == 0 { |
| 27 | + // Safe mode not enabled, enable it |
| 28 | + i2c_write(ec, i2c_port, i2c_addr >> 1, 0x00, &[0x01])?; |
| 29 | + } |
| 30 | + |
| 31 | + // Wake up from low power mode |
| 32 | + for _ in 0..3 { |
| 33 | + let i2c_response = i2c_read(ec, i2c_port, (i2c_addr + 2) >> 1, 0x70, 0x01)?; |
| 34 | + if i2c_response.data[0] != 0 { |
| 35 | + i2c_write(ec, i2c_port, (i2c_addr + 2) >> 1, 0x70, &[0x00])?; |
| 36 | + os_specific::sleep(50_000); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + // Read version |
| 41 | + let i2c_response = i2c_read(ec, i2c_port, (i2c_addr + 18) >> 1, 0x01, 0x04)?; |
| 42 | + |
| 43 | + Ok(Some(i2c_response.data)) |
| 44 | +} |
0 commit comments