fusee-cpp: implement SecureInitialize besides InitializeClock()

This commit is contained in:
Michael Scire 2021-08-21 18:00:44 -07:00 committed by SciresM
parent c9bd97192f
commit 669564b022
10 changed files with 314 additions and 115 deletions

View file

@ -23,6 +23,7 @@ namespace ams::nxboot {
constexpr inline const uintptr_t CLKRST = secmon::MemoryRegionPhysicalDeviceClkRst.GetAddress(); constexpr inline const uintptr_t CLKRST = secmon::MemoryRegionPhysicalDeviceClkRst.GetAddress();
constexpr inline const uintptr_t PMC = secmon::MemoryRegionPhysicalDevicePmc.GetAddress(); constexpr inline const uintptr_t PMC = secmon::MemoryRegionPhysicalDevicePmc.GetAddress();
constexpr inline const uintptr_t APB = secmon::MemoryRegionPhysicalDeviceApbMisc.GetAddress();
constexpr inline const uintptr_t AHB = AHB_ARBC(0); constexpr inline const uintptr_t AHB = AHB_ARBC(0);
constexpr inline const uintptr_t I2S = I2S_REG(0); constexpr inline const uintptr_t I2S = I2S_REG(0);
constexpr inline const uintptr_t DISP1 = secmon::MemoryRegionPhysicalDeviceDisp1.GetAddress(); constexpr inline const uintptr_t DISP1 = secmon::MemoryRegionPhysicalDeviceDisp1.GetAddress();
@ -166,7 +167,18 @@ namespace ams::nxboot {
} }
void InitializePinmux(fuse::HardwareType hw_type) { void InitializePinmux(fuse::HardwareType hw_type) {
/* TODO */ /* Clear global pinmux control register */
reg::Write(APB + APB_MISC_PP_PINMUX_GLOBAL_0, 0);
/* Perform initial pinmux setup. */
pinmux::SetupFirst(hw_type);
/* Setup important pinmux devices. */
pinmux::SetupI2c1();
pinmux::SetupI2c5();
pinmux::SetupUartA();
pinmux::SetupVolumeButton();
pinmux::SetupHomeButton();
} }
@ -211,10 +223,10 @@ namespace ams::nxboot {
i2c::Initialize(i2c::Port_5); i2c::Initialize(i2c::Port_5);
/* Configure pmic system setting. */ /* Configure pmic system setting. */
pmic::SetSystemSetting(); pmic::SetSystemSetting(soc_type);
/* Enable VDD core */ /* Enable VDD core */
pmic::EnableVddCore(); pmic::EnableVddCore(soc_type);
/* On hoag, enable Ldo8 */ /* On hoag, enable Ldo8 */
if (hw_type == fuse::HardwareType_Hoag) { if (hw_type == fuse::HardwareType_Hoag) {
@ -229,6 +241,17 @@ namespace ams::nxboot {
CLK_RST_REG_BITS_ENUM(SCLK_BURST_POLICY_SWAKEUP_IRQ_SOURCE, PLLP_OUT0), CLK_RST_REG_BITS_ENUM(SCLK_BURST_POLICY_SWAKEUP_IRQ_SOURCE, PLLP_OUT0),
CLK_RST_REG_BITS_ENUM(SCLK_BURST_POLICY_SWAKEUP_RUN_SOURCE, PLLP_OUT0), CLK_RST_REG_BITS_ENUM(SCLK_BURST_POLICY_SWAKEUP_RUN_SOURCE, PLLP_OUT0),
CLK_RST_REG_BITS_ENUM(SCLK_BURST_POLICY_SWAKEUP_IDLE_SOURCE, PLLP_OUT0)); CLK_RST_REG_BITS_ENUM(SCLK_BURST_POLICY_SWAKEUP_IDLE_SOURCE, PLLP_OUT0));
/* Do mariko-only TZRAM configuration. */
if (soc_type == fuse::SocType_Mariko) {
reg::ReadWrite(PMC + APBDEV_PMC_TZRAM_PWR_CNTRL, PMC_REG_BITS_VALUE(TZRAM_PWR_CNTRL_TZRAM_SD, 0));
reg::Write(PMC + APBDEV_PMC_TZRAM_NON_SEC_DISABLE, PMC_REG_BITS_ENUM(TZRAM_NON_SEC_DISABLE_SD_WRITE, ON),
PMC_REG_BITS_ENUM(TZRAM_NON_SEC_DISABLE_SD_READ, ON));
reg::Write(PMC + APBDEV_PMC_TZRAM_SEC_DISABLE, PMC_REG_BITS_ENUM(TZRAM_SEC_DISABLE_SD_WRITE, ON),
PMC_REG_BITS_ENUM(TZRAM_SEC_DISABLE_SD_READ, ON));
}
} }
} }

View file

@ -34,6 +34,7 @@
#include <exosphere/uart.hpp> #include <exosphere/uart.hpp>
#include <exosphere/pinmux.hpp> #include <exosphere/pinmux.hpp>
#include <exosphere/pmic.hpp> #include <exosphere/pmic.hpp>
#include <exosphere/pmic_setup.hpp>
#include <exosphere/rtc.hpp> #include <exosphere/rtc.hpp>
#include <exosphere/log.hpp> #include <exosphere/log.hpp>
#include <exosphere/clkrst.hpp> #include <exosphere/clkrst.hpp>

View file

@ -15,15 +15,21 @@
*/ */
#pragma once #pragma once
#include <vapours.hpp> #include <vapours.hpp>
#include <exosphere/fuse.hpp>
namespace ams::pinmux { namespace ams::pinmux {
void SetRegisterAddress(uintptr_t pinmux_address, uintptr_t gpio_address); void SetRegisterAddress(uintptr_t pinmux_address, uintptr_t gpio_address);
void SetupFirst(fuse::HardwareType hw_type);
void SetupUartA(); void SetupUartA();
void SetupUartB(); void SetupUartB();
void SetupUartC(); void SetupUartC();
void SetupI2c1(); void SetupI2c1();
void SetupI2c5(); void SetupI2c5();
void SetupVolumeButton();
void SetupHomeButton();
} }

View file

@ -36,8 +36,4 @@ namespace ams::pmic {
bool IsAcOk(); bool IsAcOk();
bool IsPowerButtonPressed(); bool IsPowerButtonPressed();
void SetSystemSetting();
void EnableVddCore();
void EnableLdo8();
} }

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <vapours.hpp>
#include <exosphere/pmic.hpp>
#include <exosphere/fuse.hpp>
namespace ams::pmic {
void SetSystemSetting(fuse::SocType soc_type);
void EnableVddCore(fuse::SocType soc_type);
void EnableLdo8();
}

View file

@ -22,6 +22,51 @@ namespace ams::pinmux {
constinit uintptr_t g_pinmux_address = secmon::MemoryRegionPhysicalDeviceApbMisc.GetAddress(); constinit uintptr_t g_pinmux_address = secmon::MemoryRegionPhysicalDeviceApbMisc.GetAddress();
constinit uintptr_t g_gpio_address = secmon::MemoryRegionPhysicalDeviceGpio.GetAddress(); constinit uintptr_t g_gpio_address = secmon::MemoryRegionPhysicalDeviceGpio.GetAddress();
void SetupFirstImpl(bool tx_cross_ext_con) {
if (tx_cross_ext_con) {
reg::Write(g_pinmux_address + PINMUX_AUX_UART2_TX, PINMUX_REG_BITS_ENUM(AUX_UART2_PM, UARTB),
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
PINMUX_REG_BITS_ENUM(AUX_E_INPUT, DISABLE),
PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE),
PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE));
reg::Write(g_pinmux_address + PINMUX_AUX_UART3_TX, PINMUX_REG_BITS_ENUM(AUX_UART3_PM, UARTC),
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
PINMUX_REG_BITS_ENUM(AUX_E_INPUT, DISABLE),
PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE),
PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE));
/* Configure GPIO for Uart-B/Uart-C. */
reg::ReadWrite(g_gpio_address + 0x108, REG_BITS_VALUE(0, 1, 1));
reg::ReadWrite(g_gpio_address + 0x00C, REG_BITS_VALUE(1, 1, 1));
reg::ReadWrite(g_gpio_address + 0x118, REG_BITS_VALUE(0, 1, 0));
reg::ReadWrite(g_gpio_address + 0x01C, REG_BITS_VALUE(1, 1, 0));
}
/* Configure PE6/PH6 */
reg::Write(g_pinmux_address + PINMUX_AUX_GPIO_PE6, PINMUX_REG_BITS_ENUM(AUX_GPIO_PE6_PM, RSVD0),
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
PINMUX_REG_BITS_ENUM(AUX_E_INPUT, ENABLE),
PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE),
PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE));
reg::Write(g_pinmux_address + PINMUX_AUX_GPIO_PH6, PINMUX_REG_BITS_ENUM(AUX_GPIO_PH6_PM, RSVD0),
PINMUX_REG_BITS_ENUM(AUX_PUPD, NONE),
PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH),
PINMUX_REG_BITS_ENUM(AUX_E_INPUT, ENABLE),
PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE),
PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE));
/* Configure GPIO E6/H6. */
reg::ReadWrite(g_gpio_address + 0x100, REG_BITS_VALUE(6, 1, 1));
reg::ReadWrite(g_gpio_address + 0x10C, REG_BITS_VALUE(6, 1, 1));
reg::ReadWrite(g_gpio_address + 0x110, REG_BITS_VALUE(6, 1, 0));
reg::ReadWrite(g_gpio_address + 0x11C, REG_BITS_VALUE(6, 1, 0));
}
} }
void SetRegisterAddress(uintptr_t pinmux_address, uintptr_t gpio_address) { void SetRegisterAddress(uintptr_t pinmux_address, uintptr_t gpio_address) {
@ -29,6 +74,23 @@ namespace ams::pinmux {
g_gpio_address = gpio_address; g_gpio_address = gpio_address;
} }
void SetupFirst(fuse::HardwareType hw_type) {
switch (hw_type) {
case fuse::HardwareType_Icosa:
case fuse::HardwareType_Iowa:
case fuse::HardwareType_Aula:
SetupFirstImpl(true);
break;
case fuse::HardwareType_Hoag:
case fuse::HardwareType_Calcio:
SetupFirstImpl(false);
break;
case fuse::HardwareType_Copper:
case fuse::HardwareType_Undefined:
break;
}
}
void SetupUartA() { void SetupUartA() {
/* Get the registers. */ /* Get the registers. */
const uintptr_t PINMUX = g_pinmux_address; const uintptr_t PINMUX = g_pinmux_address;
@ -180,4 +242,18 @@ namespace ams::pinmux {
PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE)); PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE));
} }
void SetupVolumeButton() {
/* Configure VOL_UP/VOL_DOWN */
reg::ReadWrite(g_gpio_address + 0x50C, REG_BITS_VALUE(6, 1, 1));
reg::ReadWrite(g_gpio_address + 0x50C, REG_BITS_VALUE(7, 1, 1));
reg::ReadWrite(g_gpio_address + 0x51C, REG_BITS_VALUE(6, 1, 0));
reg::ReadWrite(g_gpio_address + 0x51C, REG_BITS_VALUE(7, 1, 0));
}
void SetupHomeButton() {
/* Configure BUTTON_HOME */
reg::ReadWrite(g_gpio_address + 0x600, REG_BITS_VALUE(1, 1, 1));
reg::ReadWrite(g_gpio_address + 0x610, REG_BITS_VALUE(1, 1, 0));
}
} }

View file

@ -32,10 +32,22 @@ namespace ams::pmic {
/* TODO: Find datasheet, link to it instead. */ /* TODO: Find datasheet, link to it instead. */
/* NOTE: Tentatively, Max77620 "mostly" matches https://datasheets.maximintegrated.com/en/ds/MAX77863.pdf. */ /* NOTE: Tentatively, Max77620 "mostly" matches https://datasheets.maximintegrated.com/en/ds/MAX77863.pdf. */
/* This does not contain Max77621 documentation, though. */ /* This does not contain Max77621 documentation, though. */
constexpr inline int Max77620RegisterCnfgBbc = 0x04;
constexpr inline int Max77620RegisterOnOffStat = 0x15; constexpr inline int Max77620RegisterOnOffStat = 0x15;
constexpr inline int Max77620RegisterSd0 = 0x16;
constexpr inline int Max77620RegisterCnfg1Ldo8 = 0x33;
constexpr inline int Max77620RegisterGpio0 = 0x36; constexpr inline int Max77620RegisterGpio0 = 0x36;
constexpr inline int Max77620RegisterAmeGpio = 0x40; constexpr inline int Max77620RegisterAmeGpio = 0x40;
constexpr inline int Max77620RegisterOnOffCnfg1 = 0x41; constexpr inline int Max77620RegisterOnOffCnfg1 = 0x41;
constexpr inline int Max77620RegisterCnfgFps0 = 0x43;
constexpr inline int Max77620RegisterCnfgFps1 = 0x44;
constexpr inline int Max77620RegisterCnfgFps2 = 0x45;
constexpr inline int Max77620RegisterFpsLdo4 = 0x4A;
constexpr inline int Max77620RegisterFpsLdo8 = 0x4E;
constexpr inline int Max77620RegisterFpsSd0 = 0x4F;
constexpr inline int Max77620RegisterFpsSd1 = 0x50;
constexpr inline int Max77620RegisterFpsSd3 = 0x52;
constexpr inline int Max77620RegisterFpsGpio3 = 0x56;
constexpr inline int Max77621RegisterVOut = 0x00; constexpr inline int Max77621RegisterVOut = 0x00;
constexpr inline int Max77621RegisterVOutDvc = 0x01; constexpr inline int Max77621RegisterVOutDvc = 0x01;
@ -148,6 +160,35 @@ namespace ams::pmic {
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, MAX77620_REG_ONOFFCNFG1, on_off_1_val); i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, MAX77620_REG_ONOFFCNFG1, on_off_1_val);
} }
void SetBackupBatteryConfig() {
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterCnfgBbc, 0x40);
}
void SetForcePowerOffTimeConfig() {
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterOnOffCnfg1, 0x58);
}
void SetFlexiblePowerSequencer() {
/* Configure FPS registers. */
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterCnfgFps0, 0x38);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterCnfgFps1, 0x3A);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterCnfgFps2, 0x38);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterFpsLdo4, 0x0F);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterFpsLdo8, 0xC7);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterFpsSd0, 0x4F);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterFpsSd1, 0x29);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterFpsSd3, 0x1B);
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterFpsGpio3, 0x22);
}
void SetVoltage(int reg, int mv) {
const u8 v = ((mv - 600) * 1000) / 12500;
i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, reg, v);
}
} }
void SetEnBit(Regulator regulator) { void SetEnBit(Regulator regulator) {
@ -215,15 +256,24 @@ namespace ams::pmic {
return (GetPmicOnOffStat() & (1 << 2)) != 0; return (GetPmicOnOffStat() & (1 << 2)) != 0;
} }
void SetSystemSetting() { void SetSystemSetting(fuse::SocType soc_type) {
/* TODO */ SetBackupBatteryConfig();
SetForcePowerOffTimeConfig();
if (soc_type == fuse::SocType_Erista) {
SetFlexiblePowerSequencer();
}
}
void EnableVddCore(fuse::SocType soc_type) {
if (soc_type == fuse::SocType_Erista) {
SetVoltage(Max77620RegisterSd0, 1125);
} else /* if (soc_type == fuse::SocType_Mariko) */ {
SetVoltage(Max77620RegisterSd0, 1050);
} }
void EnableVddCore() {
/* TODO */
} }
void EnableLdo8() { void EnableLdo8() {
/* TODO */ i2c::SendByte(i2c::Port_5, I2cAddressMax77620Pmic, Max77620RegisterCnfg1Ldo8, 0xE8);
} }
} }

View file

@ -23,6 +23,8 @@
#define APB_MISC_PP_CONFIG_CTL (0x024) #define APB_MISC_PP_CONFIG_CTL (0x024)
#define APB_MISC_PP_PINMUX_GLOBAL_0 (0x040)
#define APB_MISC_GP_ASDBGREG (0x810) #define APB_MISC_GP_ASDBGREG (0x810)
#define APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL (0xA98) #define APB_MISC_GP_SDMMC1_PAD_CFGPADCTRL (0xA98)

View file

@ -55,6 +55,9 @@
#define PINMUX_AUX_LCD_BL_EN (0x3200) #define PINMUX_AUX_LCD_BL_EN (0x3200)
#define PINMUX_AUX_LCD_RST (0x3204) #define PINMUX_AUX_LCD_RST (0x3204)
#define PINMUX_AUX_GPIO_PA6 (0x3244) #define PINMUX_AUX_GPIO_PA6 (0x3244)
#define PINMUX_AUX_GPIO_PE6 (0x3248)
#define PINMUX_AUX_GPIO_PH6 (0x3250)
#define PINMUX_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (PINMUX, NAME) #define PINMUX_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (PINMUX, NAME)
#define PINMUX_REG_BITS_VALUE(NAME, VALUE) REG_NAMED_BITS_VALUE (PINMUX, NAME, VALUE) #define PINMUX_REG_BITS_VALUE(NAME, VALUE) REG_NAMED_BITS_VALUE (PINMUX, NAME, VALUE)
@ -98,3 +101,5 @@ DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_DVFS_PWM_PM, 0, RSVD0, CLDVFS, SPI3, RSVD3);
DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_LCD_BL_PWM_PM, 0, DISPLAYA, PWM0, SOR0, RSVD3); DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_LCD_BL_PWM_PM, 0, DISPLAYA, PWM0, SOR0, RSVD3);
DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_GPIO_PA6_PM, 0, SATA, RSVD1, RSVD2, RSVD3); DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_GPIO_PA6_PM, 0, SATA, RSVD1, RSVD2, RSVD3);
DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_GPIO_PE6_PM, 0, RSVD0, I2S5A, PWM2, RSVD3);
DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_GPIO_PH6_PM, 0, RSVD0, RSVD1, RSVD2, RSVD3);

View file

@ -124,6 +124,10 @@
#define APBDEV_PMC_SECURE_SCRATCH115 (0xB24) #define APBDEV_PMC_SECURE_SCRATCH115 (0xB24)
#define APBDEV_PMC_SECURE_SCRATCH119 (0xB34) #define APBDEV_PMC_SECURE_SCRATCH119 (0xB34)
#define APBDEV_PMC_TZRAM_PWR_CNTRL (0xBE8)
#define APBDEV_PMC_TZRAM_SEC_DISABLE (0xBEC)
#define APBDEV_PMC_TZRAM_NON_SEC_DISABLE (0xBF0)
#define PMC_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (APBDEV_PMC, NAME) #define PMC_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (APBDEV_PMC, NAME)
#define PMC_REG_BITS_VALUE(NAME, VALUE) REG_NAMED_BITS_VALUE (APBDEV_PMC, NAME, VALUE) #define PMC_REG_BITS_VALUE(NAME, VALUE) REG_NAMED_BITS_VALUE (APBDEV_PMC, NAME, VALUE)
@ -247,3 +251,12 @@ DEFINE_PMC_REG_BIT_ENUM(STICKY_BITS_JTAG_STS, 6, ENABLE, DISABLE);
DEFINE_PMC_REG_BIT_ENUM(CNTRL2_WAKE_DET_EN, 9, DISABLE, ENABLE); DEFINE_PMC_REG_BIT_ENUM(CNTRL2_WAKE_DET_EN, 9, DISABLE, ENABLE);
DEFINE_PMC_REG_BIT_ENUM(SEC_DISABLE2_WRITE21, 26, OFF, ON); DEFINE_PMC_REG_BIT_ENUM(SEC_DISABLE2_WRITE21, 26, OFF, ON);
DEFINE_PMC_REG(TZRAM_PWR_CNTRL_TZRAM_SD, 0, 1);
DEFINE_PMC_REG(TZRAM_PWR_CNTRL_TZRAM_SLCG_OVR, 1, 1);
DEFINE_PMC_REG_BIT_ENUM(TZRAM_SEC_DISABLE_SD_WRITE, 0, OFF, ON);
DEFINE_PMC_REG_BIT_ENUM(TZRAM_SEC_DISABLE_SD_READ, 1, OFF, ON);
DEFINE_PMC_REG_BIT_ENUM(TZRAM_NON_SEC_DISABLE_SD_WRITE, 0, OFF, ON);
DEFINE_PMC_REG_BIT_ENUM(TZRAM_NON_SEC_DISABLE_SD_READ, 1, OFF, ON);