From 178deebf24b80cd2ce60deb71df239512eaca57c Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Thu, 2 Jul 2020 14:06:42 -0700 Subject: [PATCH] exo: fixes, now tested working on mariko hardware --- .../include/exosphere/tegra/tegra_pmc.hpp | 1 + libexosphere/source/clkrst/clkrst_api.cpp | 6 +-- libexosphere/source/log/log_api.cpp | 10 +++-- libexosphere/source/pinmux/pinmux_api.cpp | 13 +++--- libexosphere/source/uart/uart_api.cpp | 40 +++++-------------- 5 files changed, 29 insertions(+), 41 deletions(-) diff --git a/libexosphere/include/exosphere/tegra/tegra_pmc.hpp b/libexosphere/include/exosphere/tegra/tegra_pmc.hpp index 6934c8cc..ecd8d186 100644 --- a/libexosphere/include/exosphere/tegra/tegra_pmc.hpp +++ b/libexosphere/include/exosphere/tegra/tegra_pmc.hpp @@ -114,6 +114,7 @@ #define APBDEV_PMC_SECURE_SCRATCH113 (0xB1C) #define APBDEV_PMC_SECURE_SCRATCH114 (0xB20) #define APBDEV_PMC_SECURE_SCRATCH115 (0xB24) +#define APBDEV_PMC_SECURE_SCRATCH119 (0xB34) #define PMC_REG_BITS_MASK(NAME) REG_NAMED_BITS_MASK (APBDEV_PMC, NAME) diff --git a/libexosphere/source/clkrst/clkrst_api.cpp b/libexosphere/source/clkrst/clkrst_api.cpp index c0ae50b2..8b396780 100644 --- a/libexosphere/source/clkrst/clkrst_api.cpp +++ b/libexosphere/source/clkrst/clkrst_api.cpp @@ -38,7 +38,7 @@ namespace ams::clkrst { reg::ReadWrite(g_register_address + param.clk_enb_offset, REG_BITS_VALUE(param.index, 1, 0)); /* Set the clock source. */ - if (param.clk_src != 0) { + if (param.clk_src_offset != 0) { reg::Write(g_register_address + param.clk_src_offset, (param.clk_src << 29) | (param.clk_div << 0)); } @@ -89,11 +89,11 @@ namespace ams::clkrst { } void EnableUartBClock() { - EnableClock(UartAClock); + EnableClock(UartBClock); } void EnableUartCClock() { - EnableClock(UartAClock); + EnableClock(UartCClock); } void EnableActmonClock() { diff --git a/libexosphere/source/log/log_api.cpp b/libexosphere/source/log/log_api.cpp index ad1d89ea..b6c96ed0 100644 --- a/libexosphere/source/log/log_api.cpp +++ b/libexosphere/source/log/log_api.cpp @@ -48,12 +48,14 @@ namespace ams::log { 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). */ + static_assert(uart::Port_LeftJoyCon == uart::Port_C); pinmux::SetupUartC(); clkrst::EnableUartCClock(); + } else if constexpr (UartLogPort == uart::Port_RightJoyCon) { + /* Logging to right joy-con (e.g. with Joyless). */ + static_assert(uart::Port_RightJoyCon == uart::Port_B); + pinmux::SetupUartB(); + clkrst::EnableUartBClock(); } else { __builtin_unreachable(); } diff --git a/libexosphere/source/pinmux/pinmux_api.cpp b/libexosphere/source/pinmux/pinmux_api.cpp index 276c9c8b..3559541c 100644 --- a/libexosphere/source/pinmux/pinmux_api.cpp +++ b/libexosphere/source/pinmux/pinmux_api.cpp @@ -104,7 +104,7 @@ namespace ams::pinmux { /* Get the registers. */ const uintptr_t PINMUX = g_pinmux_address; - /* Configure Uart-B. */ + /* Configure Uart-C. */ 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), @@ -114,13 +114,13 @@ namespace ams::pinmux { 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_TRISTATE, TRISTATE), 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_PUPD, PULL_DOWN), PINMUX_REG_BITS_ENUM(AUX_TRISTATE, PASSTHROUGH), PINMUX_REG_BITS_ENUM(AUX_E_INPUT, DISABLE), PINMUX_REG_BITS_ENUM(AUX_LOCK, DISABLE), @@ -128,13 +128,16 @@ namespace ams::pinmux { 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_TRISTATE, TRISTATE), 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)); + reg::ReadWrite(g_gpio_address + 0x118, REG_BITS_VALUE(0, 1, 1)); + reg::Read(g_gpio_address + 0x118); + reg::ReadWrite(g_gpio_address + 0x00C, REG_BITS_VALUE(1, 1, 0)); + reg::Read(g_gpio_address + 0x00C); } void SetupI2c1() { diff --git a/libexosphere/source/uart/uart_api.cpp b/libexosphere/source/uart/uart_api.cpp index 02e6d59a..aaeaf2a1 100644 --- a/libexosphere/source/uart/uart_api.cpp +++ b/libexosphere/source/uart/uart_api.cpp @@ -33,11 +33,11 @@ namespace ams::uart { } void WaitSymbols(int baud, u32 num) { - util::WaitMicroSeconds(util::DivideUp(1'000'000, baud) * num); + util::WaitMicroSeconds(util::DivideUp(num * 1'000'000, baud)); } void WaitCycles(int baud, u32 num) { - util::WaitMicroSeconds(util::DivideUp(1'000'000, 16 * baud) * num); + util::WaitMicroSeconds(util::DivideUp(num * 1'000'000, 16 * baud)); } ALWAYS_INLINE void WaitFifoNotFull(volatile UartRegisters *uart) { @@ -60,26 +60,6 @@ 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) { @@ -97,7 +77,13 @@ namespace ams::uart { constexpr u32 UartClock = 408000000; const u32 divisor = (UartClock + (baud_rate * 16) / 2) / (baud_rate * 16); - /* Disable DLAB and all interrupts. */ + /* Wait for idle state. */ + WaitIdle(uart, UART_VENDOR_STATE_TX_IDLE); + + /* Wait 100 us. */ + util::WaitMicroSeconds(100); + + /* Disable interrupts. */ uart->lcr = uart->lcr & ~UART_LCR_DLAB; uart->ier = 0; uart->mcr = 0; @@ -128,8 +114,8 @@ namespace ams::uart { /* Wait for idle state. */ WaitIdle(uart, UART_VENDOR_STATE_TX_IDLE | UART_VENDOR_STATE_RX_IDLE); - /* Set scratch register to 0. */ - uart->spr = 0; + /* Wait 100 us. */ + util::WaitMicroSeconds(100); } void SendText(Port port, const void *data, size_t size) { @@ -139,10 +125,6 @@ namespace ams::uart { /* 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);