diff --git a/libraries/libstratosphere/include/stratosphere/i2c/driver/i2c_bus_api.hpp b/libraries/libstratosphere/include/stratosphere/i2c/driver/i2c_bus_api.hpp index 85968e765..e6f198246 100644 --- a/libraries/libstratosphere/include/stratosphere/i2c/driver/i2c_bus_api.hpp +++ b/libraries/libstratosphere/include/stratosphere/i2c/driver/i2c_bus_api.hpp @@ -39,7 +39,7 @@ namespace ams::i2c::driver { Result ExecuteCommandList(void *dst, size_t dst_size, I2cSession &session, const void *src, size_t src_size); - Result SetRetryPolicy(I2cSession &session, int max_retry_count, int retry_interval_ms); + Result SetRetryPolicy(I2cSession &session, int max_retry_count, int retry_interval_us); } diff --git a/libraries/libstratosphere/include/stratosphere/i2c/driver/impl/i2c_i2c_session_impl.hpp b/libraries/libstratosphere/include/stratosphere/i2c/driver/impl/i2c_i2c_session_impl.hpp index 23f561a5e..2bb24c70a 100644 --- a/libraries/libstratosphere/include/stratosphere/i2c/driver/impl/i2c_i2c_session_impl.hpp +++ b/libraries/libstratosphere/include/stratosphere/i2c/driver/impl/i2c_i2c_session_impl.hpp @@ -57,7 +57,7 @@ namespace ams::i2c::driver::impl { Result Send(const void *src, size_t src_size, TransactionOption option); Result Receive(void *dst, size_t dst_size, TransactionOption option); Result ExecuteCommandList(void *dst, size_t dst_size, const void *src, size_t src_size); - Result SetRetryPolicy(int mr, int interval_ms); + Result SetRetryPolicy(int mr, int interval_us); }; static_assert( sizeof(I2cSessionImpl) <= I2cSessionSize); static_assert(alignof(I2cSessionImpl) <= I2cSessionAlign); diff --git a/libraries/libstratosphere/include/stratosphere/i2c/i2c_bus_api.hpp b/libraries/libstratosphere/include/stratosphere/i2c/i2c_bus_api.hpp index 699a18042..005ba9884 100644 --- a/libraries/libstratosphere/include/stratosphere/i2c/i2c_bus_api.hpp +++ b/libraries/libstratosphere/include/stratosphere/i2c/i2c_bus_api.hpp @@ -31,6 +31,6 @@ namespace ams::i2c { Result ExecuteCommandList(void *dst, size_t dst_size, const I2cSession &session, const void *src, size_t src_size); - void SetRetryPolicy(const I2cSession &session, int max_retry_count, int retry_interval_ms); + void SetRetryPolicy(const I2cSession &session, int max_retry_count, int retry_interval_us); } diff --git a/libraries/libstratosphere/include/stratosphere/i2c/sf/i2c_sf_i_session.hpp b/libraries/libstratosphere/include/stratosphere/i2c/sf/i2c_sf_i_session.hpp index a4ecf7f18..605f4e2ba 100644 --- a/libraries/libstratosphere/include/stratosphere/i2c/sf/i2c_sf_i_session.hpp +++ b/libraries/libstratosphere/include/stratosphere/i2c/sf/i2c_sf_i_session.hpp @@ -27,7 +27,7 @@ namespace ams::i2c::sf { AMS_SF_METHOD_INFO(C, H, 10, Result, Send, (const ams::sf::InAutoSelectBuffer &in_data, i2c::TransactionOption option) ) \ AMS_SF_METHOD_INFO(C, H, 11, Result, Receive, (const ams::sf::OutAutoSelectBuffer &out_data, i2c::TransactionOption option) ) \ AMS_SF_METHOD_INFO(C, H, 12, Result, ExecuteCommandList, (const ams::sf::OutAutoSelectBuffer &rcv_buf, const ams::sf::InPointerArray &command_list) ) \ - AMS_SF_METHOD_INFO(C, H, 13, Result, SetRetryPolicy, (s32 max_retry_count, s32 retry_interval_ms), hos::Version_6_0_0 ) + AMS_SF_METHOD_INFO(C, H, 13, Result, SetRetryPolicy, (s32 max_retry_count, s32 retry_interval_us), hos::Version_6_0_0 ) AMS_SF_DEFINE_INTERFACE(ISession, AMS_I2C_I_SESSION_INTERFACE_INFO) diff --git a/libraries/libstratosphere/source/i2c/driver/board/nintendo_nx/impl/i2c_bus_accessor.cpp b/libraries/libstratosphere/source/i2c/driver/board/nintendo_nx/impl/i2c_bus_accessor.cpp index 5287d9101..5e8dff88a 100644 --- a/libraries/libstratosphere/source/i2c/driver/board/nintendo_nx/impl/i2c_bus_accessor.cpp +++ b/libraries/libstratosphere/source/i2c/driver/board/nintendo_nx/impl/i2c_bus_accessor.cpp @@ -397,7 +397,7 @@ namespace ams::i2c::driver::board::nintendo_nx::impl { this->DisableInterruptMask(); os::ClearInterruptEvent(std::addressof(this->interrupt_event)); - return i2c::ResultInterruptTimeout(); + return i2c::ResultTimeout(); } /* Check and handle any errors. */ @@ -427,7 +427,7 @@ namespace ams::i2c::driver::board::nintendo_nx::impl { this->DisableInterruptMask(); os::ClearInterruptEvent(std::addressof(this->interrupt_event)); - return i2c::ResultInterruptTimeout(); + return i2c::ResultTimeout(); } } @@ -472,7 +472,7 @@ namespace ams::i2c::driver::board::nintendo_nx::impl { this->DisableInterruptMask(); os::ClearInterruptEvent(std::addressof(this->interrupt_event)); - return i2c::ResultInterruptTimeout(); + return i2c::ResultTimeout(); } /* Check and handle any errors. */ diff --git a/libraries/libstratosphere/source/i2c/driver/i2c_driver_bus_api.cpp b/libraries/libstratosphere/source/i2c/driver/i2c_driver_bus_api.cpp new file mode 100644 index 000000000..3b11792ae --- /dev/null +++ b/libraries/libstratosphere/source/i2c/driver/i2c_driver_bus_api.cpp @@ -0,0 +1,91 @@ +/* + * 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 "impl/i2c_driver_core.hpp" + +namespace ams::i2c::driver { + + namespace { + + constexpr inline int DefaultRetryCount = 3; + constexpr inline TimeSpan DefaultRetryInterval = TimeSpan::FromMilliSeconds(5); + + Result OpenSessionImpl(I2cSession *out, I2cDeviceProperty *device) { + /* Construct the session. */ + auto *session = new (std::addressof(impl::GetI2cSessionImpl(*out))) impl::I2cSessionImpl(DefaultRetryCount, DefaultRetryInterval); + auto session_guard = SCOPE_GUARD { session->~I2cSessionImpl(); }; + + /* Open the session. */ + R_TRY(session->Open(device, ddsf::AccessMode_ReadWrite)); + + /* We succeeded. */ + session_guard.Cancel(); + return ResultSuccess(); + } + + } + + Result OpenSession(I2cSession *out, DeviceCode device_code) { + AMS_ASSERT(out != nullptr); + + /* Find the device. */ + I2cDeviceProperty *device = nullptr; + R_TRY(impl::FindDevice(std::addressof(device), device_code)); + AMS_ASSERT(device != nullptr); + + /* Open the session. */ + R_TRY(OpenSessionImpl(out, device)); + + return ResultSuccess(); + } + + void CloseSession(I2cSession &session) { + impl::GetOpenI2cSessionImpl(session).~I2cSessionImpl(); + } + + Result Send(I2cSession &session, const void *src, size_t src_size, TransactionOption option) { + AMS_ASSERT(src != nullptr); + AMS_ABORT_UNLESS(src_size > 0); + + return impl::GetOpenI2cSessionImpl(session).Send(src, src_size, option); + } + + Result Receive(void *dst, size_t dst_size, I2cSession &session, TransactionOption option) { + AMS_ASSERT(dst != nullptr); + AMS_ABORT_UNLESS(dst_size > 0); + + return impl::GetOpenI2cSessionImpl(session).Receive(dst, dst_size, option); + } + + Result ExecuteCommandList(void *dst, size_t dst_size, I2cSession &session, const void *src, size_t src_size) { + AMS_ASSERT(src != nullptr); + AMS_ASSERT(dst != nullptr); + + AMS_ABORT_UNLESS(src_size > 0); + AMS_ABORT_UNLESS(dst_size > 0); + + return impl::GetOpenI2cSessionImpl(session).ExecuteCommandList(dst, dst_size, src, src_size); + } + + Result SetRetryPolicy(I2cSession &session, int max_retry_count, int retry_interval_us) { + AMS_ASSERT(max_retry_count > 0); + AMS_ASSERT(retry_interval_us > 0); + + return impl::GetOpenI2cSessionImpl(session).SetRetryPolicy(max_retry_count, retry_interval_us); + } + +} diff --git a/libraries/libstratosphere/source/i2c/driver/impl/i2c_i2c_session_impl.cpp b/libraries/libstratosphere/source/i2c/driver/impl/i2c_i2c_session_impl.cpp new file mode 100644 index 000000000..83c48991c --- /dev/null +++ b/libraries/libstratosphere/source/i2c/driver/impl/i2c_i2c_session_impl.cpp @@ -0,0 +1,199 @@ +/* + * 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 "i2c_driver_core.hpp" +#include "../../impl/i2c_command_list_format.hpp" + +namespace ams::i2c::driver::impl { + + namespace { + + constexpr TransactionOption EncodeTransactionOption(bool start, bool stop) { + return static_cast((start ? TransactionOption_StartCondition : 0) | (stop ? TransactionOption_StopCondition : 0)); + } + + } + + Result I2cSessionImpl::Open(I2cDeviceProperty *device, ddsf::AccessMode access_mode) { + AMS_ASSERT(device != nullptr); + + /* Check if we're the device's first session. */ + const bool first = !device->HasAnyOpenSession(); + + /* Open the session. */ + R_TRY(ddsf::OpenSession(device, this, access_mode)); + auto guard = SCOPE_GUARD { ddsf::CloseSession(this); }; + + /* If we're the first session, initialize the device. */ + if (first) { + R_TRY(device->GetDriver().SafeCastTo().InitializeDevice(device)); + } + + /* We're opened. */ + guard.Cancel(); + return ResultSuccess(); + } + + void I2cSessionImpl::Close() { + /* If we're not open, do nothing. */ + if (!this->IsOpen()) { + return; + } + + /* Get the device. */ + auto &device = this->GetDevice().SafeCastTo(); + + /* Close the session. */ + ddsf::CloseSession(this); + + /* If there are no remaining sessions, finalize the device. */ + if (!device.HasAnyOpenSession()) { + device.GetDriver().SafeCastTo().FinalizeDevice(std::addressof(device)); + } + } + + Result I2cSessionImpl::SendHandler(const u8 **cur_cmd, u8 **cur_dst) { + /* Read the header bytes. */ + const util::BitPack8 hdr0{*(*cur_cmd++)}; + const util::BitPack8 hdr1{*(*cur_cmd++)}; + + /* Decode the header. */ + const bool start = hdr0.Get(); + const bool stop = hdr0.Get(); + const size_t size = hdr1.Get(); + + /* Execute the transaction. */ + R_TRY(this->ExecuteTransactionWithRetry(nullptr, Command::Send, *cur_cmd, size, EncodeTransactionOption(start, stop))); + + /* Advance. */ + *cur_cmd += size; + + return ResultSuccess(); + } + + Result I2cSessionImpl::ReceiveHandler(const u8 **cur_cmd, u8 **cur_dst) { + /* Read the header bytes. */ + const util::BitPack8 hdr0{*(*cur_cmd++)}; + const util::BitPack8 hdr1{*(*cur_cmd++)}; + + /* Decode the header. */ + const bool start = hdr0.Get(); + const bool stop = hdr0.Get(); + const size_t size = hdr1.Get(); + + /* Execute the transaction. */ + R_TRY(this->ExecuteTransactionWithRetry(*cur_dst, Command::Receive, nullptr, size, EncodeTransactionOption(start, stop))); + + /* Advance. */ + *cur_dst += size; + + return ResultSuccess(); + } + + Result I2cSessionImpl::ExtensionHandler(const u8 **cur_cmd, u8 **cur_dst) { + /* Read the header bytes. */ + const util::BitPack8 hdr0{*(*cur_cmd++)}; + + /* Execute the subcommand. */ + switch (hdr0.Get()) { + case i2c::impl::SubCommandId_Sleep: + { + const util::BitPack8 param{*(*cur_cmd++)}; + + os::SleepThread(TimeSpan::FromMicroSeconds(param.Get())); + } + break; + AMS_UNREACHABLE_DEFAULT_CASE(); + } + + return ResultSuccess(); + } + + Result I2cSessionImpl::ExecuteTransactionWithRetry(void *dst, Command command, const void *src, size_t size, TransactionOption option) { + /* Get the device. */ + auto &device = GetDevice().SafeCastTo(); + + /* Repeatedly try to execute the transaction. */ + int retry_count; + while (true) { + /* Execute the transaction. */ + Result result; + switch (command) { + case Command::Send: result = device.GetDriver().SafeCastTo().Send(std::addressof(device), src, size, option); break; + case Command::Receive: result = device.GetDriver().SafeCastTo().Receive(dst, size, std::addressof(device), option); break; + AMS_UNREACHABLE_DEFAULT_CASE(); + } + + /* If we timed out, retry up to our max retry count. */ + R_TRY_CATCH(result) { + R_CATCH(i2c::ResultTimeout) { + if ((++retry_count) <= this->max_retry_count) { + os::SleepThread(this->retry_interval); + continue; + } + return i2c::ResultBusBusy(); + } + } R_END_TRY_CATCH; + + return ResultSuccess(); + } + } + + Result I2cSessionImpl::Send(const void *src, size_t src_size, TransactionOption option) { + /* Acquire exclusive access to the device. */ + std::scoped_lock lk(this->GetDevice().SafeCastTo().GetDriver().SafeCastTo().GetTransactionOrderMutex()); + + return this->ExecuteTransactionWithRetry(nullptr, Command::Send, src, src_size, option); + } + + Result I2cSessionImpl::Receive(void *dst, size_t dst_size, TransactionOption option) { + /* Acquire exclusive access to the device. */ + std::scoped_lock lk(this->GetDevice().SafeCastTo().GetDriver().SafeCastTo().GetTransactionOrderMutex()); + + return this->ExecuteTransactionWithRetry(dst, Command::Receive, nullptr, dst_size, option); + } + + Result I2cSessionImpl::ExecuteCommandList(void *dst, size_t dst_size, const void *src, size_t src_size) { + /* Acquire exclusive access to the device. */ + std::scoped_lock lk(this->GetDevice().SafeCastTo().GetDriver().SafeCastTo().GetTransactionOrderMutex()); + + /* Prepare to process the command list. */ + const u8 * cur_u8 = static_cast(src); + const u8 * const end_u8 = cur_u8 + src_size; + u8 * dst_u8 = static_cast(dst); + + /* Process commands. */ + while (cur_u8 < end_u8) { + const util::BitPack8 hdr{*cur_u8}; + + switch (hdr.Get()) { + case i2c::impl::CommandId_Send: R_TRY(this->SendHandler(std::addressof(cur_u8), std::addressof(dst_u8))); break; + case i2c::impl::CommandId_Receive: R_TRY(this->ReceiveHandler(std::addressof(cur_u8), std::addressof(dst_u8))); break; + case i2c::impl::CommandId_Extension: R_TRY(this->ExtensionHandler(std::addressof(cur_u8), std::addressof(dst_u8))); break; + AMS_UNREACHABLE_DEFAULT_CASE(); + } + } + + return ResultSuccess(); + } + + Result I2cSessionImpl::SetRetryPolicy(int mr, int interval_us) { + this->max_retry_count = mr; + this->retry_interval = TimeSpan::FromMicroSeconds(interval_us); + return ResultSuccess(); + } + +} diff --git a/libraries/libstratosphere/source/i2c/i2c_client_api.cpp b/libraries/libstratosphere/source/i2c/i2c_client_api.cpp index fdce3c579..f0acae7d3 100644 --- a/libraries/libstratosphere/source/i2c/i2c_client_api.cpp +++ b/libraries/libstratosphere/source/i2c/i2c_client_api.cpp @@ -151,11 +151,11 @@ namespace ams::i2c { return GetInterface(session)->ExecuteCommandList(buf, arr); } - void SetRetryPolicy(const I2cSession &session, int max_retry_count, int retry_interval_ms) { + void SetRetryPolicy(const I2cSession &session, int max_retry_count, int retry_interval_us) { AMS_ASSERT(max_retry_count >= 0); - AMS_ASSERT(retry_interval_ms >= 0); + AMS_ASSERT(retry_interval_us >= 0); - R_ABORT_UNLESS(GetInterface(session)->SetRetryPolicy(max_retry_count, retry_interval_ms)); + R_ABORT_UNLESS(GetInterface(session)->SetRetryPolicy(max_retry_count, retry_interval_us)); } } diff --git a/libraries/libstratosphere/source/i2c/server/i2c_server_session_impl.hpp b/libraries/libstratosphere/source/i2c/server/i2c_server_session_impl.hpp index 9fc87889f..11c52d38e 100644 --- a/libraries/libstratosphere/source/i2c/server/i2c_server_session_impl.hpp +++ b/libraries/libstratosphere/source/i2c/server/i2c_server_session_impl.hpp @@ -67,8 +67,8 @@ namespace ams::i2c::server { return i2c::driver::ExecuteCommandList(rcv_buf.GetPointer(), rcv_buf.GetSize(), this->internal_session, command_list.GetPointer(), command_list.GetSize() * sizeof(i2c::I2cCommand)); } - Result SetRetryPolicy(s32 max_retry_count, s32 retry_interval_ms) { - return i2c::driver::SetRetryPolicy(this->internal_session, max_retry_count, retry_interval_ms); + Result SetRetryPolicy(s32 max_retry_count, s32 retry_interval_us) { + return i2c::driver::SetRetryPolicy(this->internal_session, max_retry_count, retry_interval_us); } }; static_assert(i2c::sf::IsISession); diff --git a/libraries/libvapours/include/vapours/results/i2c_results.hpp b/libraries/libvapours/include/vapours/results/i2c_results.hpp index c1aa308af..0ba35650b 100644 --- a/libraries/libvapours/include/vapours/results/i2c_results.hpp +++ b/libraries/libvapours/include/vapours/results/i2c_results.hpp @@ -27,6 +27,6 @@ namespace ams::i2c { R_DEFINE_ERROR_RESULT(UnknownDevice, 5); - R_DEFINE_ERROR_RESULT(InterruptTimeout, 253); + R_DEFINE_ERROR_RESULT(Timeout, 253); } diff --git a/stratosphere/boot/source/boot_battery_driver.hpp b/stratosphere/boot/source/boot_battery_driver.hpp index 52a9c1a28..688b1754b 100644 --- a/stratosphere/boot/source/boot_battery_driver.hpp +++ b/stratosphere/boot/source/boot_battery_driver.hpp @@ -22,7 +22,7 @@ namespace ams::boot { i2c::driver::I2cSession i2c_session; public: BatteryDriver() { - i2c::driver::OpenSession(std::addressof(this->i2c_session), i2c::DeviceCode_Max17050); + R_ABORT_UNLESS(i2c::driver::OpenSession(std::addressof(this->i2c_session), i2c::DeviceCode_Max17050)); } ~BatteryDriver() { diff --git a/stratosphere/boot/source/boot_charger_driver.hpp b/stratosphere/boot/source/boot_charger_driver.hpp index 29d550928..a1fa357e4 100644 --- a/stratosphere/boot/source/boot_charger_driver.hpp +++ b/stratosphere/boot/source/boot_charger_driver.hpp @@ -26,7 +26,7 @@ namespace ams::boot { i2c::driver::I2cSession i2c_session; public: ChargerDriver() { - i2c::driver::OpenSession(std::addressof(this->i2c_session), i2c::DeviceCode_Bq24193); + R_ABORT_UNLESS(i2c::driver::OpenSession(std::addressof(this->i2c_session), i2c::DeviceCode_Bq24193)); //boot::gpio::Configure(GpioPadName_Bq24193Charger); //boot::gpio::SetDirection(GpioPadName_Bq24193Charger, GpioDirection_Output); diff --git a/stratosphere/boot/source/boot_display.cpp b/stratosphere/boot/source/boot_display.cpp index eb070b84d..65068ff89 100644 --- a/stratosphere/boot/source/boot_display.cpp +++ b/stratosphere/boot/source/boot_display.cpp @@ -197,7 +197,7 @@ namespace ams::boot { /* Turn on DSI/voltage rail. */ { i2c::driver::I2cSession i2c_session; - i2c::driver::OpenSession(std::addressof(i2c_session), i2c::DeviceCode_Max77620Pmic); + R_ABORT_UNLESS(i2c::driver::OpenSession(std::addressof(i2c_session), i2c::DeviceCode_Max77620Pmic)); if (g_soc_type == spl::SocType_Mariko) { WriteI2cRegister(i2c_session, 0x18, 0x3A); diff --git a/stratosphere/boot/source/boot_pmic_driver.hpp b/stratosphere/boot/source/boot_pmic_driver.hpp index 7d2a07f44..256e0ccea 100644 --- a/stratosphere/boot/source/boot_pmic_driver.hpp +++ b/stratosphere/boot/source/boot_pmic_driver.hpp @@ -24,7 +24,7 @@ namespace ams::boot { i2c::driver::I2cSession i2c_session; public: PmicDriver() { - i2c::driver::OpenSession(std::addressof(this->i2c_session), i2c::DeviceCode_Max77620Pmic); + R_ABORT_UNLESS(i2c::driver::OpenSession(std::addressof(this->i2c_session), i2c::DeviceCode_Max77620Pmic)); } ~PmicDriver() {