From ffb3493ab51cfd4e831f6da8d27898a5b744aefa Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 13 May 2020 10:56:07 -0700 Subject: [PATCH] exo2: implement remainder of warmboot tz code --- libexosphere/include/exosphere.hpp | 2 + libexosphere/include/exosphere/charger.hpp | 25 +++ libexosphere/include/exosphere/clkrst.hpp | 3 + libexosphere/include/exosphere/log.hpp | 4 + libexosphere/include/exosphere/pinmux.hpp | 29 +++ .../include/exosphere/tegra/tegra_clkrst.hpp | 15 +- libexosphere/include/exosphere/uart.hpp | 6 +- libexosphere/source/charger/charger_api.cpp | 55 ++++++ libexosphere/source/clkrst/clkrst_api.cpp | 28 ++- libexosphere/source/log/log_api.cpp | 37 ++++ libexosphere/source/pinmux/pinmux_api.cpp | 181 ++++++++++++++++++ .../source/pinmux/pinmux_registers.hpp | 65 +++++++ libexosphere/source/uart/uart_api.cpp | 56 +++++- 13 files changed, 493 insertions(+), 13 deletions(-) create mode 100644 libexosphere/include/exosphere/charger.hpp create mode 100644 libexosphere/include/exosphere/pinmux.hpp create mode 100644 libexosphere/source/charger/charger_api.cpp create mode 100644 libexosphere/source/pinmux/pinmux_api.cpp create mode 100644 libexosphere/source/pinmux/pinmux_registers.hpp diff --git a/libexosphere/include/exosphere.hpp b/libexosphere/include/exosphere.hpp index c6468ab6..e93e8b8f 100644 --- a/libexosphere/include/exosphere.hpp +++ b/libexosphere/include/exosphere.hpp @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -32,6 +33,7 @@ #include #include #include +#include #include #include #include diff --git a/libexosphere/include/exosphere/charger.hpp b/libexosphere/include/exosphere/charger.hpp new file mode 100644 index 00000000..7241f3ab --- /dev/null +++ b/libexosphere/include/exosphere/charger.hpp @@ -0,0 +1,25 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::charger { + + bool IsHiZMode(); + void EnterHiZMode(); + void ExitHiZMode(); + +} diff --git a/libexosphere/include/exosphere/clkrst.hpp b/libexosphere/include/exosphere/clkrst.hpp index 2f014397..e4b31740 100644 --- a/libexosphere/include/exosphere/clkrst.hpp +++ b/libexosphere/include/exosphere/clkrst.hpp @@ -26,5 +26,8 @@ namespace ams::clkrst { void EnableUartBClock(); void EnableUartCClock(); void EnableActmonClock(); + void EnableI2c1Clock(); + + void DisableI2c1Clock(); } \ No newline at end of file diff --git a/libexosphere/include/exosphere/log.hpp b/libexosphere/include/exosphere/log.hpp index 2f1cca8c..3c54fbed 100644 --- a/libexosphere/include/exosphere/log.hpp +++ b/libexosphere/include/exosphere/log.hpp @@ -19,5 +19,9 @@ namespace ams::log { void Initialize(); + void Finalize(); + + void SendText(const void *text, size_t size); + void Flush(); } \ No newline at end of file diff --git a/libexosphere/include/exosphere/pinmux.hpp b/libexosphere/include/exosphere/pinmux.hpp new file mode 100644 index 00000000..f79035b2 --- /dev/null +++ b/libexosphere/include/exosphere/pinmux.hpp @@ -0,0 +1,29 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::pinmux { + + void SetRegisterAddress(uintptr_t pinmux_address, uintptr_t gpio_address); + + void SetupUartA(); + void SetupUartB(); + void SetupUartC(); + void SetupI2c1(); + void SetupI2c5(); + +} diff --git a/libexosphere/include/exosphere/tegra/tegra_clkrst.hpp b/libexosphere/include/exosphere/tegra/tegra_clkrst.hpp index 035b2859..1529b566 100644 --- a/libexosphere/include/exosphere/tegra/tegra_clkrst.hpp +++ b/libexosphere/include/exosphere/tegra/tegra_clkrst.hpp @@ -56,12 +56,16 @@ DEFINE_CLK_RST_REG(MISC_CLK_ENB_CFG_ALL_VISIBLE, 28, 1); #define CLK_RST_CONTROLLER_CLK_OUT_ENB_W (0x364) /* CLK_SOURCE */ +#define CLK_RST_CONTROLLER_CLK_SOURCE_I2C1 (0x124) +#define CLK_RST_CONTROLLER_CLK_SOURCE_I2C5 (0x128) #define CLK_RST_CONTROLLER_CLK_SOURCE_UARTA (0x178) #define CLK_RST_CONTROLLER_CLK_SOURCE_UARTB (0x17C) #define CLK_RST_CONTROLLER_CLK_SOURCE_UARTC (0x1A0) #define CLK_RST_CONTROLLER_CLK_SOURCE_ACTMON (0x3e8) /* CLK_ENB_*_INDEX */ +#define CLK_RST_CONTROLLER_CLK_ENB_I2C1_INDEX (0x0C) +#define CLK_RST_CONTROLLER_CLK_ENB_I2C5_INDEX (0x0F) #define CLK_RST_CONTROLLER_CLK_ENB_UARTA_INDEX (0x06) #define CLK_RST_CONTROLLER_CLK_ENB_UARTB_INDEX (0x07) #define CLK_RST_CONTROLLER_CLK_ENB_UARTC_INDEX (0x17) @@ -80,8 +84,11 @@ DEFINE_CLK_RST_REG_BIT_ENUM(LVL2_CLK_GATE_OVRD_SDMMC2_LEGACY_TMCLK_OVR_ON, 29, O DEFINE_CLK_RST_REG_BIT_ENUM(LVL2_CLK_GATE_OVRD_SDMMC3_LEGACY_TMCLK_OVR_ON, 30, OFF, ON); DEFINE_CLK_RST_REG_BIT_ENUM(LVL2_CLK_GATE_OVRD_SDMMC4_LEGACY_TMCLK_OVR_ON, 31, OFF, ON); -DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_UARTA_UARTA_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2) -DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_UARTB_UARTB_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2) -DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_UARTC_UARTC_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2) +DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_I2C1_I2C1_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2); +DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_I2C5_I2C5_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2); -DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_ACTMON_ACTMON_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, CLK_S, PLLC4_OUT1, CLK_M, PLLC4_OUT2) +DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_UARTA_UARTA_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2); +DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_UARTB_UARTB_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2); +DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_UARTC_UARTC_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, RESERVED4, PLLC4_OUT1, CLK_M, PLLC4_OUT2); + +DEFINE_CLK_RST_REG_THREE_BIT_ENUM(CLK_SOURCE_ACTMON_ACTMON_CLK_SRC, 29, PLLP_OUT0, PLLC2_OUT0, PLLC_OUT0, PLLC4_OUT0, CLK_S, PLLC4_OUT1, CLK_M, PLLC4_OUT2); diff --git a/libexosphere/include/exosphere/uart.hpp b/libexosphere/include/exosphere/uart.hpp index 920f3912..d3f92169 100644 --- a/libexosphere/include/exosphere/uart.hpp +++ b/libexosphere/include/exosphere/uart.hpp @@ -39,4 +39,8 @@ namespace ams::uart { void Initialize(Port port, int baud_rate, u32 flags); -} \ No newline at end of file + void SendText(Port port, const void *data, size_t size); + + void WaitFlush(Port port); + +} diff --git a/libexosphere/source/charger/charger_api.cpp b/libexosphere/source/charger/charger_api.cpp new file mode 100644 index 00000000..7aeb5c37 --- /dev/null +++ b/libexosphere/source/charger/charger_api.cpp @@ -0,0 +1,55 @@ +/* + * 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 . + */ +#include + +namespace ams::charger { + + namespace { + + /* https://www.ti.com/lit/ds/symlink/bq24193.pdf */ + constexpr inline int I2cAddressBq24193 = 0x6B; + + constexpr inline int Bq24193RegisterInputSourceControl = 0x00; + + /* 8.5.1.1 EN_HIZ */ + enum EnHiZ : u8 { + EnHiZ_Disable = (0u << 7), + EnHiZ_Enable = (1u << 7), + + EnHiZ_Mask = (1u << 7), + }; + + } + + bool IsHiZMode() { + return (i2c::QueryByte(i2c::Port_1, I2cAddressBq24193, Bq24193RegisterInputSourceControl) & EnHiZ_Mask) == EnHiZ_Enable; + } + + void EnterHiZMode() { + u8 ctrl = i2c::QueryByte(i2c::Port_1, I2cAddressBq24193, Bq24193RegisterInputSourceControl); + ctrl &= ~EnHiZ_Mask; + ctrl |= EnHiZ_Enable; + i2c::SendByte(i2c::Port_1, I2cAddressBq24193, Bq24193RegisterInputSourceControl, ctrl); + } + + void ExitHiZMode() { + u8 ctrl = i2c::QueryByte(i2c::Port_1, I2cAddressBq24193, Bq24193RegisterInputSourceControl); + ctrl &= ~EnHiZ_Mask; + ctrl |= EnHiZ_Disable; + i2c::SendByte(i2c::Port_1, I2cAddressBq24193, Bq24193RegisterInputSourceControl, ctrl); + } + +} diff --git a/libexosphere/source/clkrst/clkrst_api.cpp b/libexosphere/source/clkrst/clkrst_api.cpp index 76601b6e..c9ac4f98 100644 --- a/libexosphere/source/clkrst/clkrst_api.cpp +++ b/libexosphere/source/clkrst/clkrst_api.cpp @@ -49,13 +49,13 @@ namespace ams::clkrst { reg::ReadWrite(g_register_address + param.reset_offset, REG_BITS_VALUE(param.index, 1, 0)); } - // void DisableClock(const ClockParameters ¶m) { - // /* Hold reset. */ - // reg::ReadWrite(g_register_address + param.reset_offset, REG_BITS_VALUE(param.index, 1, 1)); - // - // /* Disable clock. */ - // reg::ReadWrite(g_register_address + param.clk_enb_offset, REG_BITS_VALUE(param.index, 1, 0)); - // } + void DisableClock(const ClockParameters ¶m) { + /* Hold reset. */ + reg::ReadWrite(g_register_address + param.reset_offset, REG_BITS_VALUE(param.index, 1, 1)); + + /* Disable clock. */ + reg::ReadWrite(g_register_address + param.clk_enb_offset, REG_BITS_VALUE(param.index, 1, 0)); + } #define DEFINE_CLOCK_PARAMETERS(_VARNAME_, _REG_, _NAME_, _CLK_, _DIV_) \ constexpr inline const ClockParameters _VARNAME_ = { \ @@ -70,6 +70,8 @@ namespace ams::clkrst { DEFINE_CLOCK_PARAMETERS(UartAClock, L, UARTA, PLLP_OUT0, 0); DEFINE_CLOCK_PARAMETERS(UartBClock, L, UARTB, PLLP_OUT0, 0); DEFINE_CLOCK_PARAMETERS(UartCClock, H, UARTC, PLLP_OUT0, 0); + DEFINE_CLOCK_PARAMETERS(I2c1Clock, L, I2C1, CLK_M, 0); + DEFINE_CLOCK_PARAMETERS(I2c5Clock, H, I2C5, CLK_M, 0); DEFINE_CLOCK_PARAMETERS(ActmonClock, V, ACTMON, CLK_M, 0); } @@ -98,4 +100,16 @@ namespace ams::clkrst { EnableClock(ActmonClock); } + void EnableI2c1Clock() { + EnableClock(I2c1Clock); + } + + void EnableI2c5Clock() { + EnableClock(I2c1Clock); + } + + void DisableI2c1Clock() { + DisableClock(I2c1Clock); + } + } diff --git a/libexosphere/source/log/log_api.cpp b/libexosphere/source/log/log_api.cpp index 0cf20e20..2ea3c5c7 100644 --- a/libexosphere/source/log/log_api.cpp +++ b/libexosphere/source/log/log_api.cpp @@ -40,9 +40,30 @@ namespace ams::log { } }(); + ALWAYS_INLINE void SetupUart() { + if constexpr (UartLogPort == uart::Port_ReservedDebug) { + /* Logging to the debug port. */ + pinmux::SetupUartA(); + clkrst::EnableUartAClock(); + } else if constexpr (UartLogPort == uart::Port_LeftJoyCon) { + /* Logging to left joy-con (e.g. with Joyless). */ + pinmux::SetupUartB(); + clkrst::EnableUartBClock(); + } else if constexpr (UartLogPort == uart::Port_RightJoyCon) { + /* Logging to right joy-con (e.g. with Joyless). */ + pinmux::SetupUartC(); + clkrst::EnableUartCClock(); + } else { + __builtin_unreachable(); + } + } + } void Initialize() { + /* Initialize pinmux and clock for the target uart port. */ + SetupUart(); + /* Initialize the target uart port. */ uart::Initialize(UartLogPort, 115200, UartPortFlags); @@ -50,4 +71,20 @@ namespace ams::log { g_initialized_uart = true; } + void Finalize() { + g_initialized_uart = false; + } + + void SendText(const void *text, size_t size) { + if (g_initialized_uart) { + uart::SendText(UartLogPort, text, size); + } + } + + void Flush() { + if (g_initialized_uart) { + uart::WaitFlush(UartLogPort); + } + } + } \ No newline at end of file diff --git a/libexosphere/source/pinmux/pinmux_api.cpp b/libexosphere/source/pinmux/pinmux_api.cpp new file mode 100644 index 00000000..513797df --- /dev/null +++ b/libexosphere/source/pinmux/pinmux_api.cpp @@ -0,0 +1,181 @@ +/* + * 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 . + */ +#include +#include "pinmux_registers.hpp" + +namespace ams::pinmux { + + namespace { + + constinit uintptr_t g_pinmux_address = secmon::MemoryRegionPhysicalDeviceApbMisc.GetAddress(); + constinit uintptr_t g_gpio_address = secmon::MemoryRegionPhysicalDeviceGpio.GetAddress(); + + } + + void SetRegisterAddress(uintptr_t pinmux_address, uintptr_t gpio_address) { + g_pinmux_address = pinmux_address; + g_gpio_address = gpio_address; + } + + void SetupUartA() { + /* Get the registers. */ + const uintptr_t PINMUX = g_pinmux_address; + + /* Configure Uart-A. */ + reg::Write(PINMUX + PINMUX_AUX_UART1_TX, PINMUX_REG_BITS_ENUM(AUX_UART1_PM, UARTA), + 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(PINMUX + PINMUX_AUX_UART1_RX, PINMUX_REG_BITS_ENUM(AUX_UART1_PM, UARTA), + PINMUX_REG_BITS_ENUM(AUX_PUPD, PULL_UP), + 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(PINMUX + PINMUX_AUX_UART1_RTS, PINMUX_REG_BITS_ENUM(AUX_UART1_PM, UARTA), + 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(PINMUX + PINMUX_AUX_UART1_CTS, PINMUX_REG_BITS_ENUM(AUX_UART1_PM, UARTA), + PINMUX_REG_BITS_ENUM(AUX_PUPD, PULL_DOWN), + 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)); + } + + void SetupUartB() { + /* Get the registers. */ + const uintptr_t PINMUX = g_pinmux_address; + + /* Configure Uart-B. */ + reg::Write(PINMUX + 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(PINMUX + PINMUX_AUX_UART2_RX, 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, ENABLE), + PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE), + PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE)); + + reg::Write(PINMUX + PINMUX_AUX_UART2_RTS, 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(PINMUX + PINMUX_AUX_UART2_CTS, 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, ENABLE), + PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE), + PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE)); + + /* Configure GPIO for Uart-B. */ + reg::ReadWrite(g_gpio_address + 0x108, REG_BITS_VALUE(0, 4, 0)); + } + + void SetupUartC() { + /* Get the registers. */ + const uintptr_t PINMUX = g_pinmux_address; + + /* Configure Uart-B. */ + reg::Write(PINMUX + 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)); + + reg::Write(PINMUX + PINMUX_AUX_UART3_RX, 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, ENABLE), + PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE), + PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE)); + + reg::Write(PINMUX + PINMUX_AUX_UART3_RTS, 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)); + + reg::Write(PINMUX + PINMUX_AUX_UART3_CTS, 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, ENABLE), + PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE), + PINMUX_REG_BITS_ENUM(AUX_E_OD, DISABLE)); + + /* Configure GPIO for Uart-C. */ + reg::ReadWrite(g_gpio_address + 0x00C, REG_BITS_VALUE(1, 4, 0)); + } + + void SetupI2c1() { + /* Get the registers. */ + const uintptr_t PINMUX = g_pinmux_address; + + /* Configure I2c1 */ + reg::Write(PINMUX + PINMUX_AUX_GEN1_I2C_SCL, PINMUX_REG_BITS_ENUM(AUX_GEN1_I2C_PM, I2C1), + 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(PINMUX + PINMUX_AUX_GEN1_I2C_SDA, PINMUX_REG_BITS_ENUM(AUX_GEN1_I2C_PM, I2C1), + 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)); + } + + void SetupI2c5() { + /* Get the registers. */ + const uintptr_t PINMUX = g_pinmux_address; + + /* Configure I2c5 */ + reg::Write(PINMUX + PINMUX_AUX_PWR_I2C_SCL, PINMUX_REG_BITS_ENUM(AUX_PWR_I2C_PM, I2CPMU), + 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(PINMUX + PINMUX_AUX_PWR_I2C_SDA, PINMUX_REG_BITS_ENUM(AUX_PWR_I2C_PM, I2CPMU), + 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)); + } + +} \ No newline at end of file diff --git a/libexosphere/source/pinmux/pinmux_registers.hpp b/libexosphere/source/pinmux/pinmux_registers.hpp new file mode 100644 index 00000000..4fd1c4d5 --- /dev/null +++ b/libexosphere/source/pinmux/pinmux_registers.hpp @@ -0,0 +1,65 @@ +/* + * 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 . + */ +#include + +namespace ams::pinmux { + + #define PINMUX_AUX_GEN1_I2C_SCL (0x30BC) + #define PINMUX_AUX_GEN1_I2C_SDA (0x30C0) + #define PINMUX_AUX_PWR_I2C_SCL (0x30DC) + #define PINMUX_AUX_PWR_I2C_SDA (0x30E0) + + #define PINMUX_AUX_UART1_TX (0x30E4) + #define PINMUX_AUX_UART1_RX (0x30E8) + #define PINMUX_AUX_UART1_RTS (0x30EC) + #define PINMUX_AUX_UART1_CTS (0x30F0) + #define PINMUX_AUX_UART2_TX (0x30F4) + #define PINMUX_AUX_UART2_RX (0x30F8) + #define PINMUX_AUX_UART2_RTS (0x30FC) + #define PINMUX_AUX_UART2_CTS (0x3100) + #define PINMUX_AUX_UART3_TX (0x3104) + #define PINMUX_AUX_UART3_RX (0x3108) + #define PINMUX_AUX_UART3_RTS (0x310C) + #define PINMUX_AUX_UART3_CTS (0x3110) + + #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_ENUM(NAME, ENUM) REG_NAMED_BITS_ENUM (PINMUX, NAME, ENUM) + #define PINMUX_REG_BITS_ENUM_SEL(NAME, __COND__, TRUE_ENUM, FALSE_ENUM) REG_NAMED_BITS_ENUM_SEL(PINMUX, NAME, __COND__, TRUE_ENUM, FALSE_ENUM) + + #define DEFINE_PINMUX_REG(NAME, __OFFSET__, __WIDTH__) REG_DEFINE_NAMED_REG (PINMUX, NAME, __OFFSET__, __WIDTH__) + #define DEFINE_PINMUX_REG_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE) REG_DEFINE_NAMED_BIT_ENUM (PINMUX, NAME, __OFFSET__, ZERO, ONE) + #define DEFINE_PINMUX_REG_TWO_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE, TWO, THREE) REG_DEFINE_NAMED_TWO_BIT_ENUM (PINMUX, NAME, __OFFSET__, ZERO, ONE, TWO, THREE) + #define DEFINE_PINMUX_REG_THREE_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN) REG_DEFINE_NAMED_THREE_BIT_ENUM(PINMUX, NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN) + #define DEFINE_PINMUX_REG_FOUR_BIT_ENUM(NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ELEVEN, TWELVE, THIRTEEN, FOURTEEN, FIFTEEN) REG_DEFINE_NAMED_FOUR_BIT_ENUM (PINMUX, NAME, __OFFSET__, ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, ELEVEN, TWELVE, THIRTEEN, FOURTEEN, FIFTEEN) + + DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_PUPD, 2, NONE, PULL_DOWN, PULL_UP, RSVD); + DEFINE_PINMUX_REG_BIT_ENUM(AUX_TRISTATE, 4, PASSTHROUGH, TRISTATE); + DEFINE_PINMUX_REG_BIT_ENUM(AUX_PARK, 5, NORMAL, PARKED); + DEFINE_PINMUX_REG_BIT_ENUM(AUX_E_INPUT, 6, DISABLE, ENABLE); + DEFINE_PINMUX_REG_BIT_ENUM(AUX_LOCK, 7, DISABLE, ENABLE); + DEFINE_PINMUX_REG_BIT_ENUM(AUX_E_LPDR, 8, DISABLE, ENABLE); + DEFINE_PINMUX_REG_BIT_ENUM(AUX_E_OD, 11, DISABLE, ENABLE); + DEFINE_PINMUX_REG_BIT_ENUM(AUX_E_SCHMT, 12, DISABLE, ENABLE); + + DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_GEN1_I2C_PM, 0, I2C1, RSVD1, RSVD2, RSVD3); + DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_PWR_I2C_PM, 0, I2CPMU, RSVD1, RSVD2, RSVD3); + + DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_UART1_PM, 0, UARTA, RSVD1, RSVD2, RSVD3); + DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_UART2_PM, 0, UARTB, I2S4A, RSVD2, UART); + DEFINE_PINMUX_REG_TWO_BIT_ENUM(AUX_UART3_PM, 0, UARTC, SPI4, RSVD2, RSVD3); + +} diff --git a/libexosphere/source/uart/uart_api.cpp b/libexosphere/source/uart/uart_api.cpp index 53374810..02e6d59a 100644 --- a/libexosphere/source/uart/uart_api.cpp +++ b/libexosphere/source/uart/uart_api.cpp @@ -58,6 +58,28 @@ namespace ams::uart { } } + constexpr inline u32 LockBit = (1 << 6); + + void Lock(volatile UartRegisters *reg) { + while (true) { + if (reg->mie != 0) { + continue; + } + + reg->irda_csr = LockBit; + + if (reg->mie == 0) { + break; + } + + reg->irda_csr = 0; + } + } + + void Unlock(volatile UartRegisters *reg) { + reg->irda_csr = 0; + } + } void SetRegisterAddress(uintptr_t address) { @@ -110,4 +132,36 @@ namespace ams::uart { uart->spr = 0; } -} \ No newline at end of file + void SendText(Port port, const void *data, size_t size) { + /* Get the registers. */ + auto *uart = GetRegisters(port); + + /* Get pointer to data. */ + const u8 *p = static_cast(data); + + /* Lock the uart registers. */ + Lock(uart); + ON_SCOPE_EXIT { Unlock(uart); }; + + /* Send each byte. */ + for (size_t i = 0; i < size; ++i) { + WaitFifoNotFull(uart); + + if (p[i] == '\n') { + *reinterpret_cast(std::addressof(uart->thr)) = '\r'; + WaitFifoNotFull(uart); + } + + *reinterpret_cast(std::addressof(uart->thr)) = p[i]; + } + } + + void WaitFlush(Port port) { + /* Get the registers. */ + auto *uart = GetRegisters(port); + + /* Wait for idle. */ + WaitIdle(uart, UART_VENDOR_STATE_TX_IDLE); + } + +}