mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-06-04 16:53:48 -04:00
boot: refactor to use sts::boot namespace
This commit is contained in:
parent
4fbae9e5a4
commit
06416aeded
91 changed files with 3665 additions and 3155 deletions
|
@ -16,110 +16,103 @@
|
|||
|
||||
#include <switch.h>
|
||||
#include <stratosphere.hpp>
|
||||
#include "boot_functions.hpp"
|
||||
|
||||
#include "boot_i2c_utils.hpp"
|
||||
#include "boot_pmic_driver.hpp"
|
||||
|
||||
void PmicDriver::ShutdownSystem() {
|
||||
if (R_FAILED(this->ShutdownSystem(false))) {
|
||||
std::abort();
|
||||
namespace sts::boot {
|
||||
|
||||
void PmicDriver::ShutdownSystem() {
|
||||
R_ASSERT(this->ShutdownSystem(false));
|
||||
}
|
||||
}
|
||||
|
||||
void PmicDriver::RebootSystem() {
|
||||
if (R_FAILED(this->ShutdownSystem(true))) {
|
||||
std::abort();
|
||||
void PmicDriver::RebootSystem() {
|
||||
R_ASSERT(this->ShutdownSystem(true));
|
||||
}
|
||||
}
|
||||
|
||||
Result PmicDriver::GetAcOk(bool *out) {
|
||||
u8 power_status;
|
||||
R_TRY(this->GetPowerStatus(&power_status));
|
||||
*out = (power_status & 0x02) != 0;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PmicDriver::GetPowerIntr(u8 *out) {
|
||||
const u8 addr = 0x0B;
|
||||
return Boot::ReadI2cRegister(this->i2c_session, out, sizeof(*out), &addr, sizeof(addr));
|
||||
}
|
||||
|
||||
Result PmicDriver::GetPowerStatus(u8 *out) {
|
||||
const u8 addr = 0x15;
|
||||
return Boot::ReadI2cRegister(this->i2c_session, out, sizeof(*out), &addr, sizeof(addr));
|
||||
}
|
||||
|
||||
Result PmicDriver::GetNvErc(u8 *out) {
|
||||
const u8 addr = 0x0C;
|
||||
return Boot::ReadI2cRegister(this->i2c_session, out, sizeof(*out), &addr, sizeof(addr));
|
||||
}
|
||||
|
||||
Result PmicDriver::GetPowerButtonPressed(bool *out) {
|
||||
u8 power_intr;
|
||||
R_TRY(this->GetPowerIntr(&power_intr));
|
||||
*out = (power_intr & 0x08) != 0;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PmicDriver::ShutdownSystem(bool reboot) {
|
||||
const u8 on_off_1_addr = 0x41;
|
||||
const u8 on_off_2_addr = 0x42;
|
||||
|
||||
/* Get value, set or clear software reset mask. */
|
||||
u8 on_off_2_val = 0;
|
||||
if (R_FAILED(Boot::ReadI2cRegister(this->i2c_session, &on_off_2_val, sizeof(on_off_2_val), &on_off_2_addr, sizeof(on_off_2_addr)))) {
|
||||
std::abort();
|
||||
Result PmicDriver::GetAcOk(bool *out) {
|
||||
u8 power_status;
|
||||
R_TRY(this->GetPowerStatus(&power_status));
|
||||
*out = (power_status & 0x02) != 0;
|
||||
return ResultSuccess;
|
||||
}
|
||||
if (reboot) {
|
||||
on_off_2_val |= 0x80;
|
||||
} else {
|
||||
on_off_2_val &= ~0x80;
|
||||
|
||||
Result PmicDriver::GetPowerIntr(u8 *out) {
|
||||
const u8 addr = 0x0B;
|
||||
return ReadI2cRegister(this->i2c_session, out, sizeof(*out), &addr, sizeof(addr));
|
||||
}
|
||||
if (R_FAILED(Boot::WriteI2cRegister(this->i2c_session, &on_off_2_val, sizeof(on_off_2_val), &on_off_2_addr, sizeof(on_off_2_addr)))) {
|
||||
|
||||
Result PmicDriver::GetPowerStatus(u8 *out) {
|
||||
const u8 addr = 0x15;
|
||||
return ReadI2cRegister(this->i2c_session, out, sizeof(*out), &addr, sizeof(addr));
|
||||
}
|
||||
|
||||
Result PmicDriver::GetNvErc(u8 *out) {
|
||||
const u8 addr = 0x0C;
|
||||
return ReadI2cRegister(this->i2c_session, out, sizeof(*out), &addr, sizeof(addr));
|
||||
}
|
||||
|
||||
Result PmicDriver::GetPowerButtonPressed(bool *out) {
|
||||
u8 power_intr;
|
||||
R_TRY(this->GetPowerIntr(&power_intr));
|
||||
*out = (power_intr & 0x08) != 0;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result PmicDriver::ShutdownSystem(bool reboot) {
|
||||
const u8 on_off_1_addr = 0x41;
|
||||
const u8 on_off_2_addr = 0x42;
|
||||
|
||||
/* Get value, set or clear software reset mask. */
|
||||
u8 on_off_2_val = 0;
|
||||
R_ASSERT(ReadI2cRegister(this->i2c_session, &on_off_2_val, sizeof(on_off_2_val), &on_off_2_addr, sizeof(on_off_2_addr)));
|
||||
if (reboot) {
|
||||
on_off_2_val |= 0x80;
|
||||
} else {
|
||||
on_off_2_val &= ~0x80;
|
||||
}
|
||||
R_ASSERT(WriteI2cRegister(this->i2c_session, &on_off_2_val, sizeof(on_off_2_val), &on_off_2_addr, sizeof(on_off_2_addr)));
|
||||
|
||||
/* Get value, set software reset mask. */
|
||||
u8 on_off_1_val = 0;
|
||||
R_ASSERT(ReadI2cRegister(this->i2c_session, &on_off_1_val, sizeof(on_off_1_val), &on_off_1_addr, sizeof(on_off_1_addr)));
|
||||
on_off_1_val |= 0x80;
|
||||
|
||||
/* Finalize the battery. */
|
||||
{
|
||||
BatteryDriver battery_driver;
|
||||
this->FinalizeBattery(&battery_driver);
|
||||
}
|
||||
|
||||
/* Actually write the value to trigger shutdown/reset. */
|
||||
R_ASSERT(WriteI2cRegister(this->i2c_session, &on_off_1_val, sizeof(on_off_1_val), &on_off_1_addr, sizeof(on_off_1_addr)));
|
||||
|
||||
/* Allow up to 5 seconds for shutdown/reboot to take place. */
|
||||
svcSleepThread(5'000'000'000ul);
|
||||
std::abort();
|
||||
}
|
||||
|
||||
/* Get value, set software reset mask. */
|
||||
u8 on_off_1_val = 0;
|
||||
if (R_FAILED(Boot::ReadI2cRegister(this->i2c_session, &on_off_1_val, sizeof(on_off_1_val), &on_off_1_addr, sizeof(on_off_1_addr)))) {
|
||||
std::abort();
|
||||
}
|
||||
on_off_1_val |= 0x80;
|
||||
void PmicDriver::FinalizeBattery(BatteryDriver *battery_driver) {
|
||||
/* Set shutdown timer. */
|
||||
battery_driver->SetShutdownTimer();
|
||||
|
||||
/* Finalize the battery. */
|
||||
{
|
||||
BatteryDriver battery_driver;
|
||||
this->FinalizeBattery(&battery_driver);
|
||||
/* Get whether shutdown is enabled. */
|
||||
bool shutdown_enabled;
|
||||
if (R_FAILED(battery_driver->GetShutdownEnabled(&shutdown_enabled))) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool ac_ok;
|
||||
bool desired_shutdown_enabled;
|
||||
if (R_FAILED(this->GetAcOk(&ac_ok)) || ac_ok) {
|
||||
desired_shutdown_enabled = false;
|
||||
} else {
|
||||
desired_shutdown_enabled = true;
|
||||
}
|
||||
|
||||
if (shutdown_enabled != desired_shutdown_enabled) {
|
||||
battery_driver->SetShutdownEnabled(desired_shutdown_enabled);
|
||||
}
|
||||
}
|
||||
|
||||
/* Actually write the value to trigger shutdown/reset. */
|
||||
if (R_FAILED(Boot::WriteI2cRegister(this->i2c_session, &on_off_1_val, sizeof(on_off_1_val), &on_off_1_addr, sizeof(on_off_1_addr)))) {
|
||||
std::abort();
|
||||
}
|
||||
|
||||
/* Allow up to 5 seconds for shutdown/reboot to take place. */
|
||||
svcSleepThread(5'000'000'000ul);
|
||||
std::abort();
|
||||
}
|
||||
|
||||
void PmicDriver::FinalizeBattery(BatteryDriver *battery_driver) {
|
||||
/* Set shutdown timer. */
|
||||
battery_driver->SetShutdownTimer();
|
||||
|
||||
/* Get whether shutdown is enabled. */
|
||||
bool shutdown_enabled;
|
||||
if (R_FAILED(battery_driver->GetShutdownEnabled(&shutdown_enabled))) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool ac_ok;
|
||||
bool desired_shutdown_enabled;
|
||||
if (R_FAILED(this->GetAcOk(&ac_ok)) || ac_ok) {
|
||||
desired_shutdown_enabled = false;
|
||||
} else {
|
||||
desired_shutdown_enabled = true;
|
||||
}
|
||||
|
||||
if (shutdown_enabled != desired_shutdown_enabled) {
|
||||
battery_driver->SetShutdownEnabled(desired_shutdown_enabled);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue