mirror of
https://github.com/Atmosphere-NX/Atmosphere
synced 2024-11-12 16:16:35 +00:00
i2c: implement remaining required driver logic
This commit is contained in:
parent
258cfb62a2
commit
323e893433
14 changed files with 307 additions and 17 deletions
|
@ -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);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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<i2c::I2cCommand> &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)
|
||||
|
||||
|
|
|
@ -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. */
|
||||
|
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stratosphere.hpp>
|
||||
#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<TransactionOption>((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<II2cDriver>().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<I2cDeviceProperty>();
|
||||
|
||||
/* Close the session. */
|
||||
ddsf::CloseSession(this);
|
||||
|
||||
/* If there are no remaining sessions, finalize the device. */
|
||||
if (!device.HasAnyOpenSession()) {
|
||||
device.GetDriver().SafeCastTo<II2cDriver>().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<i2c::impl::SendCommandFormat::StartCondition>();
|
||||
const bool stop = hdr0.Get<i2c::impl::SendCommandFormat::StopCondition>();
|
||||
const size_t size = hdr1.Get<i2c::impl::SendCommandFormat::Size>();
|
||||
|
||||
/* 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<i2c::impl::ReceiveCommandFormat::StartCondition>();
|
||||
const bool stop = hdr0.Get<i2c::impl::ReceiveCommandFormat::StopCondition>();
|
||||
const size_t size = hdr1.Get<i2c::impl::ReceiveCommandFormat::Size>();
|
||||
|
||||
/* 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<i2c::impl::CommonCommandFormat::SubCommandId>()) {
|
||||
case i2c::impl::SubCommandId_Sleep:
|
||||
{
|
||||
const util::BitPack8 param{*(*cur_cmd++)};
|
||||
|
||||
os::SleepThread(TimeSpan::FromMicroSeconds(param.Get<i2c::impl::SleepCommandFormat::MicroSeconds>()));
|
||||
}
|
||||
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<I2cDeviceProperty>();
|
||||
|
||||
/* 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<II2cDriver>().Send(std::addressof(device), src, size, option); break;
|
||||
case Command::Receive: result = device.GetDriver().SafeCastTo<II2cDriver>().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<I2cDeviceProperty>().GetDriver().SafeCastTo<II2cDriver>().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<I2cDeviceProperty>().GetDriver().SafeCastTo<II2cDriver>().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<I2cDeviceProperty>().GetDriver().SafeCastTo<II2cDriver>().GetTransactionOrderMutex());
|
||||
|
||||
/* Prepare to process the command list. */
|
||||
const u8 * cur_u8 = static_cast<const u8 *>(src);
|
||||
const u8 * const end_u8 = cur_u8 + src_size;
|
||||
u8 * dst_u8 = static_cast<u8 *>(dst);
|
||||
|
||||
/* Process commands. */
|
||||
while (cur_u8 < end_u8) {
|
||||
const util::BitPack8 hdr{*cur_u8};
|
||||
|
||||
switch (hdr.Get<i2c::impl::CommonCommandFormat::CommandId>()) {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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<SessionImpl>);
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in a new issue