2019-05-07 18:32:37 +02:00
|
|
|
/*
|
2020-01-24 11:10:40 +01:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-05-07 18:32:37 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-22 09:10:21 +02:00
|
|
|
#include "boot_pmc_wrapper.hpp"
|
|
|
|
#include "boot_wake_pins.hpp"
|
|
|
|
|
2019-05-07 18:32:37 +02:00
|
|
|
#include "boot_registers_pmc.hpp"
|
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::boot {
|
2019-05-07 18:32:37 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
/* Include configuration into anonymous namespace. */
|
|
|
|
namespace {
|
2019-05-07 18:32:37 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
struct WakePinConfig {
|
|
|
|
u32 index;
|
|
|
|
bool enabled;
|
|
|
|
u32 level;
|
|
|
|
};
|
|
|
|
|
|
|
|
#include "boot_wake_control_configs.inc"
|
2020-02-24 17:21:31 +01:00
|
|
|
#include "boot_wake_pin_configuration_icosa.inc"
|
2019-06-22 09:10:21 +02:00
|
|
|
#include "boot_wake_pin_configuration_copper.inc"
|
2020-02-24 17:21:31 +01:00
|
|
|
#include "boot_wake_pin_configuration_hoag.inc"
|
|
|
|
#include "boot_wake_pin_configuration_iowa.inc"
|
|
|
|
#include "boot_wake_pin_configuration_calcio.inc"
|
2019-05-07 18:32:37 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
namespace {
|
2019-05-07 18:32:37 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
/* Helpers. */
|
|
|
|
void UpdatePmcControlBit(const u32 reg_offset, const u32 mask_val, const bool flag) {
|
|
|
|
WritePmcRegister(PmcBase + reg_offset, flag ? UINT32_MAX : 0, mask_val);
|
|
|
|
ReadPmcRegister(PmcBase + reg_offset);
|
|
|
|
}
|
2019-05-07 18:32:37 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
void InitializePmcWakeConfiguration(const bool is_blink) {
|
|
|
|
/* Initialize APBDEV_PMC_WAKE_DEBOUNCE_EN, do a dummy read. */
|
|
|
|
WritePmcRegister(PmcBase + APBDEV_PMC_WAKE_DEBOUNCE_EN, 0);
|
|
|
|
ReadPmcRegister(PmcBase + APBDEV_PMC_WAKE_DEBOUNCE_EN);
|
2019-05-07 19:02:53 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
/* Initialize APBDEV_PMC_BLINK_TIMER, do a dummy read. */
|
|
|
|
WritePmcRegister(PmcBase + APBDEV_PMC_BLINK_TIMER, 0x8008800);
|
|
|
|
ReadPmcRegister(PmcBase + APBDEV_PMC_BLINK_TIMER);
|
2019-05-07 19:02:53 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
/* Set control bits, do dummy reads. */
|
|
|
|
for (size_t i = 0; i < NumWakeControlConfigs; i++) {
|
|
|
|
UpdatePmcControlBit(WakeControlConfigs[i].reg_offset, WakeControlConfigs[i].mask_val, WakeControlConfigs[i].flag_val);
|
|
|
|
}
|
2019-05-07 19:02:53 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
/* Set bit 0x80 in APBDEV_PMC_CNTRL based on is_blink, do dummy read. */
|
|
|
|
UpdatePmcControlBit(APBDEV_PMC_CNTRL, 0x80, is_blink);
|
2019-05-07 19:02:53 +02:00
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
/* Set bit 0x100000 in APBDEV_PMC_DPD_PADS_ORIDE based on is_blink, do dummy read. */
|
|
|
|
UpdatePmcControlBit(APBDEV_PMC_DPD_PADS_ORIDE, 0x100000, is_blink);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWakeEventLevel(u32 index, u32 level) {
|
|
|
|
u32 pmc_wake_level_reg_offset = index <= 0x1F ? APBDEV_PMC_WAKE_LVL : APBDEV_PMC_WAKE2_LVL;
|
|
|
|
u32 pmc_wake_level_mask_reg_offset = index <= 0x1F ? APBDEV_PMC_AUTO_WAKE_LVL_MASK : APBDEV_PMC_AUTO_WAKE2_LVL_MASK;
|
|
|
|
if (level != 2) {
|
|
|
|
std::swap(pmc_wake_level_reg_offset, pmc_wake_level_mask_reg_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
const u32 mask_val = (1 << (index & 0x1F));
|
|
|
|
|
|
|
|
/* Clear level reg bit. */
|
|
|
|
UpdatePmcControlBit(pmc_wake_level_reg_offset, mask_val, false);
|
|
|
|
|
|
|
|
/* Set or clear mask reg bit. */
|
|
|
|
UpdatePmcControlBit(pmc_wake_level_mask_reg_offset, mask_val, level > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetWakeEventEnabled(u32 index, bool enabled) {
|
|
|
|
/* Set or clear enabled bit. */
|
|
|
|
UpdatePmcControlBit(index <= 0x1F ? APBDEV_PMC_WAKE_MASK : APBDEV_PMC_WAKE2_MASK, (1 << (index & 0x1F)), enabled);
|
|
|
|
}
|
2019-05-07 19:02:53 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
void SetInitialWakePinConfiguration() {
|
|
|
|
InitializePmcWakeConfiguration(false);
|
|
|
|
|
|
|
|
/* Set wake event levels, wake event enables. */
|
2020-02-24 17:21:31 +01:00
|
|
|
const WakePinConfig *configs = nullptr;
|
|
|
|
size_t num_configs = 0;
|
|
|
|
|
|
|
|
switch (spl::GetHardwareType()) {
|
|
|
|
case spl::HardwareType::Icosa:
|
2020-02-24 18:49:43 +01:00
|
|
|
configs = WakePinConfigsIcosa;
|
|
|
|
num_configs = NumWakePinConfigsIcosa;
|
2020-02-24 17:21:31 +01:00
|
|
|
break;
|
|
|
|
case spl::HardwareType::Copper:
|
2020-02-24 18:49:43 +01:00
|
|
|
configs = WakePinConfigsCopper;
|
|
|
|
num_configs = NumWakePinConfigsCopper;
|
2020-02-24 17:21:31 +01:00
|
|
|
break;
|
|
|
|
case spl::HardwareType::Hoag:
|
2020-02-24 18:49:43 +01:00
|
|
|
configs = WakePinConfigsHoag;
|
|
|
|
num_configs = NumWakePinConfigsHoag;
|
2020-02-24 17:21:31 +01:00
|
|
|
break;
|
|
|
|
case spl::HardwareType::Iowa:
|
2020-02-24 18:49:43 +01:00
|
|
|
configs = WakePinConfigsIowa;
|
|
|
|
num_configs = NumWakePinConfigsIowa;
|
2020-02-24 17:21:31 +01:00
|
|
|
case spl::HardwareType::Calcio:
|
2020-02-24 18:49:43 +01:00
|
|
|
configs = WakePinConfigsCalcio;
|
|
|
|
num_configs = NumWakePinConfigsCalcio;
|
2020-02-24 17:21:31 +01:00
|
|
|
break;
|
|
|
|
AMS_UNREACHABLE_DEFAULT_CASE();
|
2019-06-22 09:10:21 +02:00
|
|
|
}
|
|
|
|
|
2020-02-24 17:21:31 +01:00
|
|
|
AMS_ABORT_UNLESS(configs != nullptr);
|
|
|
|
|
2019-06-22 09:10:21 +02:00
|
|
|
for (size_t i = 0; i < num_configs; i++) {
|
|
|
|
SetWakeEventLevel(configs[i].index, configs[i].level);
|
|
|
|
SetWakeEventEnabled(configs[i].index, configs[i].enabled);
|
|
|
|
}
|
2019-05-07 19:02:53 +02:00
|
|
|
}
|
2019-06-22 09:10:21 +02:00
|
|
|
|
2019-05-07 18:32:37 +02:00
|
|
|
}
|