From bb1ae1d81647a662da34202503438eca3ed9bea1 Mon Sep 17 00:00:00 2001 From: Lightos1 <124387232+Lightos1@users.noreply.github.com> Date: Wed, 1 Apr 2026 15:38:45 +0200 Subject: [PATCH] Fix sleep --- .../sysmodule/src/board/board.cpp | 57 +---------- .../sysmodule/src/board/board_sensor.cpp | 2 +- .../sysmodule/src/soctherm.cpp | 94 ++++++++++--------- .../sysmodule/src/soctherm.hpp | 2 - 4 files changed, 52 insertions(+), 103 deletions(-) diff --git a/Source/rewrite-hoc-clk/sysmodule/src/board/board.cpp b/Source/rewrite-hoc-clk/sysmodule/src/board/board.cpp index 8a3452a3..6cab6cc6 100644 --- a/Source/rewrite-hoc-clk/sysmodule/src/board/board.cpp +++ b/Source/rewrite-hoc-clk/sysmodule/src/board/board.cpp @@ -53,46 +53,6 @@ namespace board { u32 fd = 0, fd2 = 0; - constexpr u32 pscDependencies[] = {PscPmModuleId_Display}; // Our dependencies - constexpr PscPmModuleId pscModuleId = (PscPmModuleId)692; // Our ID - static PscPmModule pscModule; // Module to listen for events with - - Thread SleepWakeListener; - - void SleepWakeEventListener(void *ptr) { - while (true) { - Result rc = eventWait(&pscModule.event, 10'000'000); - if (R_VALUE(rc) == KERNELRESULT(TimedOut)) - continue; - if (R_VALUE(rc) == KERNELRESULT(Cancelled)) - break; - - PscPmState eventState; - u32 flags; - if (R_FAILED(pscPmModuleGetRequest(&pscModule, &eventState, &flags))) - break; - - switch (eventState) { - case PscPmState_ReadySleep: - fileUtils::LogLine("Sleep"); - soctherm::StopSensors(); - break; - case PscPmState_ReadyAwaken: - fileUtils::LogLine("Wake"); - soctherm::StartSensors(); - break; - case PscPmState_ReadyShutdown: - fileUtils::LogLine("Shutdown"); - soctherm::StopSensors(); - break; - default: - break; - } - - pscPmModuleAcknowledge(&pscModule, eventState); - } - } - void FetchHardwareInfos() { ReadFuses(fuseData); SetGpuBracket(fuseData.gpuSpeedo, speedoBracket); @@ -171,16 +131,6 @@ namespace board { rc = pmdmntInitialize(); ASSERT_RESULT_OK(rc, "pmdmntInitialize"); - rc = pscmInitialize(); - ASSERT_RESULT_OK(rc, "pscmInitialize"); - - rc = pscmGetPmModule(&pscModule, pscModuleId, pscDependencies, sizeof(pscDependencies)/sizeof(u32), true); - ASSERT_RESULT_OK(rc, "pscmGetPmModule"); - - threadCreate(&SleepWakeListener, SleepWakeEventListener, NULL, NULL, 0x1000, 0x0, 3); - - threadStart(&SleepWakeListener); - StartLoad(nvCheck, fd); batteryInfoInitialize(); @@ -211,7 +161,7 @@ namespace board { void Exit() { - soctherm::StopSensors(); + //soctherm::StopSensors(); if (HOSSVC_HAS_CLKRST) { clkrstExit(); @@ -240,10 +190,6 @@ namespace board { pmdmntExit(); display::Shutdown(); nvExit(); - threadClose(&SleepWakeListener); - pscPmModuleFinalize(&pscModule); - pscPmModuleClose(&pscModule); - pscmExit(); } SysClkSocType GetSocType() { @@ -290,4 +236,5 @@ namespace board { bool IsUsingRetroSuperDisplay() { return false; /* stub for now. */ } + } diff --git a/Source/rewrite-hoc-clk/sysmodule/src/board/board_sensor.cpp b/Source/rewrite-hoc-clk/sysmodule/src/board/board_sensor.cpp index 21c059fa..d5e6ba28 100644 --- a/Source/rewrite-hoc-clk/sysmodule/src/board/board_sensor.cpp +++ b/Source/rewrite-hoc-clk/sysmodule/src/board/board_sensor.cpp @@ -39,7 +39,7 @@ namespace board { s32 millis = 0; BatteryChargeInfo info; - soctherm::TSensorTemps temps; + soctherm::TSensorTemps temps = {}; soctherm::ReadSensors(temps); switch(sensor) { diff --git a/Source/rewrite-hoc-clk/sysmodule/src/soctherm.cpp b/Source/rewrite-hoc-clk/sysmodule/src/soctherm.cpp index cd2603a2..49bf2221 100644 --- a/Source/rewrite-hoc-clk/sysmodule/src/soctherm.cpp +++ b/Source/rewrite-hoc-clk/sysmodule/src/soctherm.cpp @@ -44,6 +44,12 @@ namespace soctherm { #define CAR_CLK_SOURCE_TSENSOR 0x3B8 #define CAR_CLK_OUT_ENB_V 0x360 + #define CLK_RST_CONTROLLER_RST_DEVICES 0xC + #define SWR_SOC_THERM_RST 1 << 14 + + #define CLK_RST_CONTROLLER_CLK_OUT_ENB 0x18 + #define CLK_ENB_SOC_THERM 1 << 14 + #define CAR_CLK_SOURCE_TSENSOR_VAL 0x8000005E #define NOMINAL_CALIB_FT 105 @@ -470,6 +476,14 @@ namespace soctherm { return r >> 16; } + bool IsDisabledThroughSleep() { + return (ReadReg(carVa, CLK_RST_CONTROLLER_RST_DEVICES) & SWR_SOC_THERM_RST) || !(ReadReg(carVa, CLK_RST_CONTROLLER_CLK_OUT_ENB) & CLK_ENB_SOC_THERM); + } + + bool IsSensorEnabled() { + return ReadReg(socthermVa, TSENSOR_TSENSOR_CLKEN); + } + void EnableSensor(const TSensor *sensor, u32 sensorIdx) { u32 val = sensor->config->tall << SENSOR_CONFIG0_TALL_SHIFT; WriteReg(socthermVa, sensor->base + SENSOR_CONFIG0, val); @@ -497,7 +511,42 @@ namespace soctherm { return t; } + void StartSensors() { + u32 pdiv, hotspot; + + if (isMariko) { + for (u32 i = 0; i < std::size(marikoTSensors); ++i) { + EnableSensor(&marikoTSensors[i], i); + } + + pdiv = (ReadReg(socthermVa, SENSOR_PDIV) & PDIV_MASK_T210B0) | PDIV_RATE_T210B0; + hotspot = (ReadReg(socthermVa, SENSOR_HOTSPOT_OFF) & HOTSPOT_MASK_T210B0) | HOTSPOT_VAL; + } else { + for (u32 i = 0; i < std::size(eristaTSensors); ++i) { + EnableSensor(&eristaTSensors[i], i); + } + + pdiv = (ReadReg(socthermVa, SENSOR_PDIV) & PDIV_MASK_T210) | PDIV_RATE_T210; + hotspot = (ReadReg(socthermVa, SENSOR_HOTSPOT_OFF) & HOTSPOT_MASK_T210) | HOTSPOT_VAL; + + EnableSensor(&eristaTSensors[SocthermTSensor_MEM0], SocthermTSensor_MEM0); + EnableSensor(&eristaTSensors[SocthermTSensor_MEM1], SocthermTSensor_MEM1); + } + + WriteReg(socthermVa, SENSOR_PDIV, pdiv); + WriteReg(socthermVa, SENSOR_HOTSPOT_OFF, hotspot); + WriteReg(socthermVa, TSENSOR_TSENSOR_CLKEN, TSENSOR_TSENSOR_ENABLE); + } + void ReadSensors(TSensorTemps &temps) { + if (IsDisabledThroughSleep()) { + return; + } + + if (!IsSensorEnabled()) { + StartSensors(); + } + temps.cpu = TranslateTemp(ReadReg(socthermVa, SENSOR_TEMP1) >> 16); temps.gpu = TranslateTemp(ReadReg(socthermVa, SENSOR_TEMP1) & SENSOR_TEMP1_GPU_TEMP_MASK); temps.pllx = TranslateTemp(ReadReg(socthermVa, SENSOR_TEMP2) & SENSOR_TEMP2_PLLX_TEMP_MASK); @@ -509,51 +558,6 @@ namespace soctherm { } } - void StopSensors() { - const TSensor *sensors = isMariko ? marikoTSensors : eristaTSensors; - u32 count = isMariko ? std::size(marikoTSensors) : std::size(eristaTSensors); - - for (u32 i = 0; i < count; ++i) { - SetBits(socthermVa, sensors[i].base + SENSOR_CONFIG0, SENSOR_CONFIG0_STOP); - ClearBits(socthermVa, sensors[i].base + SENSOR_CONFIG1, SENSOR_CONFIG1_TEMP_ENABLE); - WriteReg(socthermVa, sensors[i].base + SENSOR_CONFIG2, 0); - } - - WriteReg(socthermVa, TSENSOR_TSENSOR_CLKEN, 0); - WriteReg(carVa, CAR_CLK_SOURCE_TSENSOR, 0); - SetBits(carVa, CAR_CLK_OUT_ENB_V, 0); - - } - - void StartSensors() { - if (!ReadReg(socthermVa, TSENSOR_TSENSOR_CLKEN)) { - u32 pdiv, hotspot; - - if (isMariko) { - for (u32 i = 0; i < std::size(marikoTSensors); ++i) { - EnableSensor(&marikoTSensors[i], i); - } - - pdiv = (ReadReg(socthermVa, SENSOR_PDIV) & PDIV_MASK_T210B0) | PDIV_RATE_T210B0; - hotspot = (ReadReg(socthermVa, SENSOR_HOTSPOT_OFF) & HOTSPOT_MASK_T210B0) | HOTSPOT_VAL; - } else { - for (u32 i = 0; i < std::size(eristaTSensors); ++i) { - EnableSensor(&eristaTSensors[i], i); - } - - pdiv = (ReadReg(socthermVa, SENSOR_PDIV) & PDIV_MASK_T210) | PDIV_RATE_T210; - hotspot = (ReadReg(socthermVa, SENSOR_HOTSPOT_OFF) & HOTSPOT_MASK_T210) | HOTSPOT_VAL; - - EnableSensor(&eristaTSensors[SocthermTSensor_MEM0], SocthermTSensor_MEM0); - EnableSensor(&eristaTSensors[SocthermTSensor_MEM1], SocthermTSensor_MEM1); - } - - WriteReg(socthermVa, SENSOR_PDIV, pdiv); - WriteReg(socthermVa, SENSOR_HOTSPOT_OFF, hotspot); - WriteReg(socthermVa, TSENSOR_TSENSOR_CLKEN, TSENSOR_TSENSOR_ENABLE); - } - } - void CalcSharedCal(const TSensorFuse *tfuse, TSensorSharedCalib *shared, u64 fuseVa) { s32 shifted_cp, shifted_ft; diff --git a/Source/rewrite-hoc-clk/sysmodule/src/soctherm.hpp b/Source/rewrite-hoc-clk/sysmodule/src/soctherm.hpp index 23575c2b..96ca391d 100644 --- a/Source/rewrite-hoc-clk/sysmodule/src/soctherm.hpp +++ b/Source/rewrite-hoc-clk/sysmodule/src/soctherm.hpp @@ -130,8 +130,6 @@ namespace soctherm { }; void Initialize(); - void StartSensors(); - void StopSensors(); void ReadSensors(TSensorTemps &temps); }