sdmmc: implement driver suitable for fs + bootloader

* sdmmc: begin skeletoning sdmmc driver

* sdmmc: add most of SdHostStandardController

* sdmmc: implement most of SdmmcController

* sdmmc: Sdmmc2Controller

* sdmmc: skeleton implementation of Sdmmc1Controller

* sdmmc: complete abstract logic for Sdmmc1 power controller

* sdmmc: implement gpio handling for sdmmc1-register-control

* sdmmc: implement pinmux handling for sdmmc1-register-control

* sdmmc: fix building for arm32 and in stratosphere context

* sdmmc: implement voltage enable/set for sdmmc1-register-control

* util: move T(V)SNPrintf from kernel to util

* sdmmc: implement BaseDeviceAccessor

* sdmmc: implement MmcDeviceAccessor

* sdmmc: implement clock reset controller for register api

* sdmmc: fix bug in WaitWhileCommandInhibit, add mmc accessors

* exo: add sdmmc test program

* sdmmc: fix speed mode extension, add CheckMmcConnection for debug

* sdmmc: add DeviceDetector, gpio: implement client api

* gpio: modernize client api instead of doing it the lazy way

* sdmmc: SdCardDeviceAccessor impl

* sdmmc: update test program to read first two sectors of sd card

* sdmmc: fix vref sel

* sdmmc: finish outward-facing api (untested)

* ams: changes for libvapours including tegra register defs

* sdmmc: remove hwinit
This commit is contained in:
SciresM 2020-10-30 11:54:30 -07:00 committed by GitHub
parent ac04e02a08
commit 166318ba77
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
143 changed files with 13696 additions and 1569 deletions

View file

@ -167,20 +167,19 @@ namespace ams::boot2 {
}
}
bool GetGpioPadLow(GpioPadName pad) {
GpioPadSession button;
if (R_FAILED(gpioOpenSession(&button, pad))) {
bool GetGpioPadLow(DeviceCode device_code) {
gpio::GpioPadSession button;
if (R_FAILED(gpio::OpenSession(std::addressof(button), device_code))) {
return false;
}
/* Ensure we close even on early return. */
ON_SCOPE_EXIT { gpioPadClose(&button); };
ON_SCOPE_EXIT { gpio::CloseSession(std::addressof(button)); };
/* Set direction input. */
gpioPadSetDirection(&button, GpioDirection_Input);
gpio::SetDirection(std::addressof(button), gpio::Direction_Input);
GpioValue val;
return R_SUCCEEDED(gpioPadGetValue(&button, &val)) && val == GpioValue_Low;
return gpio::GetValue(std::addressof(button)) == gpio::GpioValue_Low;
}
bool IsForceMaintenance() {
@ -197,7 +196,7 @@ namespace ams::boot2 {
/* Contact GPIO, read plus/minus buttons. */
{
return GetGpioPadLow(GpioPadName_ButtonVolUp) && GetGpioPadLow(GpioPadName_ButtonVolDown);
return GetGpioPadLow(gpio::DeviceCode_ButtonVolUp) && GetGpioPadLow(gpio::DeviceCode_ButtonVolDn);
}
}

View file

@ -1,64 +0,0 @@
/*
* 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>
namespace ams::dd {
uintptr_t QueryIoMapping(uintptr_t phys_addr, size_t size) {
u64 virtual_addr;
const u64 aligned_addr = util::AlignDown(phys_addr, os::MemoryPageSize);
const size_t offset = phys_addr - aligned_addr;
const u64 aligned_size = size + offset;
if (hos::GetVersion() >= hos::Version_10_0_0) {
u64 region_size;
R_TRY_CATCH(svcQueryIoMapping(&virtual_addr, &region_size, aligned_addr, aligned_size)) {
/* Official software handles this by returning 0. */
R_CATCH(svc::ResultNotFound) { return 0; }
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
AMS_ASSERT(region_size >= aligned_size);
} else {
R_TRY_CATCH(svcLegacyQueryIoMapping(&virtual_addr, aligned_addr, aligned_size)) {
/* Official software handles this by returning 0. */
R_CATCH(svc::ResultNotFound) { return 0; }
} R_END_TRY_CATCH_WITH_ABORT_UNLESS;
}
return static_cast<uintptr_t>(virtual_addr + offset);
}
namespace {
inline u32 ReadWriteRegisterImpl(uintptr_t phys_addr, u32 value, u32 mask) {
u32 out_value;
R_ABORT_UNLESS(svcReadWriteRegister(&out_value, phys_addr, mask, value));
return out_value;
}
}
u32 ReadRegister(uintptr_t phys_addr) {
return ReadWriteRegisterImpl(phys_addr, 0, 0);
}
void WriteRegister(uintptr_t phys_addr, u32 value) {
ReadWriteRegisterImpl(phys_addr, value, ~u32());
}
u32 ReadWriteRegister(uintptr_t phys_addr, u32 value, u32 mask) {
return ReadWriteRegisterImpl(phys_addr, value, mask);
}
}

View file

@ -0,0 +1,197 @@
/*
* 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 "gpio_remote_manager_impl.hpp"
namespace ams::gpio {
namespace {
/* TODO: Manager object. */
constinit os::SdkMutex g_init_mutex;
constinit int g_initialize_count = 0;
std::shared_ptr<sf::IManager> g_manager;
using InternalSession = std::shared_ptr<gpio::sf::IPadSession>;
InternalSession &GetInterface(GpioPadSession *session) {
AMS_ASSERT(session->_session != nullptr);
return *static_cast<InternalSession *>(session->_session);
}
}
void Initialize() {
std::scoped_lock lk(g_init_mutex);
if ((g_initialize_count++) == 0) {
R_ABORT_UNLESS(::gpioInitialize());
g_manager = ams::sf::MakeShared<sf::IManager, RemoteManagerImpl>();
}
}
void Finalize() {
std::scoped_lock lk(g_init_mutex);
AMS_ASSERT(g_initialize_count > 0);
if ((--g_initialize_count) == 0) {
g_manager.reset();
::gpioExit();
}
}
Result OpenSession(GpioPadSession *out_session, ams::DeviceCode device_code) {
/* Allocate the session. */
InternalSession *internal_session = new (std::nothrow) InternalSession;
AMS_ABORT_UNLESS(internal_session != nullptr);
auto session_guard = SCOPE_GUARD { delete internal_session; };
/* Get the session. */
{
ams::sf::cmif::ServiceObjectHolder object_holder;
if (hos::GetVersion() >= hos::Version_7_0_0) {
R_TRY(g_manager->OpenSession2(std::addressof(object_holder), device_code, ddsf::AccessMode_ReadWrite));
} else {
R_TRY(g_manager->OpenSession(std::addressof(object_holder), ConvertToGpioPadName(device_code)));
}
*internal_session = object_holder.GetServiceObject<sf::IPadSession>();
}
/* Set output. */
out_session->_session = internal_session;
out_session->_event = nullptr;
/* We succeeded. */
session_guard.Cancel();
return ResultSuccess();
}
void CloseSession(GpioPadSession *session) {
AMS_ASSERT(session != nullptr);
/* Unbind the interrupt, if it's still bound. */
if (session->_event != nullptr) {
gpio::UnbindInterrupt(session);
}
/* Close the session. */
delete std::addressof(GetInterface(session));
session->_session = nullptr;
}
Result IsWakeEventActive(bool *out_is_active, ams::DeviceCode device_code) {
if (hos::GetVersion() >= hos::Version_7_0_0) {
R_TRY(g_manager->IsWakeEventActive2(out_is_active, device_code));
} else {
R_TRY(g_manager->IsWakeEventActive(out_is_active, ConvertToGpioPadName(device_code)));
}
return ResultSuccess();
}
Direction GetDirection(GpioPadSession *session) {
Direction out;
R_ABORT_UNLESS(GetInterface(session)->GetDirection(std::addressof(out)));
return out;
}
void SetDirection(GpioPadSession *session, Direction direction) {
R_ABORT_UNLESS(GetInterface(session)->SetDirection(direction));
}
GpioValue GetValue(GpioPadSession *session) {
GpioValue out;
R_ABORT_UNLESS(GetInterface(session)->GetValue(std::addressof(out)));
return out;
}
void SetValue(GpioPadSession *session, GpioValue value) {
R_ABORT_UNLESS(GetInterface(session)->SetValue(value));
}
InterruptMode GetInterruptMode(GpioPadSession *session) {
InterruptMode out;
R_ABORT_UNLESS(GetInterface(session)->GetInterruptMode(std::addressof(out)));
return out;
}
void SetInterruptMode(GpioPadSession *session, InterruptMode mode) {
R_ABORT_UNLESS(GetInterface(session)->SetInterruptMode(mode));
}
bool GetInterruptEnable(GpioPadSession *session) {
bool out;
R_ABORT_UNLESS(GetInterface(session)->GetInterruptEnable(std::addressof(out)));
return out;
}
void SetInterruptEnable(GpioPadSession *session, bool en) {
R_ABORT_UNLESS(GetInterface(session)->SetInterruptEnable(en));
}
InterruptStatus GetInterruptStatus(GpioPadSession *session) {
InterruptStatus out;
R_ABORT_UNLESS(GetInterface(session)->GetInterruptStatus(std::addressof(out)));
return out;
}
void ClearInterruptStatus(GpioPadSession *session) {
R_ABORT_UNLESS(GetInterface(session)->ClearInterruptStatus());
}
int GetDebounceTime(GpioPadSession *session) {
int out;
R_ABORT_UNLESS(GetInterface(session)->GetDebounceTime(std::addressof(out)));
return out;
}
void SetDebounceTime(GpioPadSession *session, int ms) {
R_ABORT_UNLESS(GetInterface(session)->SetDebounceTime(ms));
}
bool GetDebounceEnabled(GpioPadSession *session) {
bool out;
R_ABORT_UNLESS(GetInterface(session)->GetDebounceEnabled(std::addressof(out)));
return out;
}
void SetDebounceEnabled(GpioPadSession *session, bool en) {
R_ABORT_UNLESS(GetInterface(session)->SetDebounceEnabled(en));
}
Result BindInterrupt(os::SystemEventType *event, GpioPadSession *session) {
AMS_ASSERT(session->_event == nullptr);
ams::sf::CopyHandle handle;
R_TRY(GetInterface(session)->BindInterrupt(std::addressof(handle)));
os::AttachReadableHandleToSystemEvent(event, handle.GetValue(), true, os::EventClearMode_ManualClear);
session->_event = event;
return ResultSuccess();
}
void UnbindInterrupt(GpioPadSession *session) {
AMS_ASSERT(session->_event != nullptr);
R_ABORT_UNLESS(GetInterface(session)->UnbindInterrupt());
os::DestroySystemEvent(session->_event);
session->_event = nullptr;
}
}

View file

@ -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/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "gpio_remote_pad_session_impl.hpp"
namespace ams::gpio {
class RemoteManagerImpl {
public:
RemoteManagerImpl() { /* ... */ }
~RemoteManagerImpl() { /* ... */ }
public:
/* Actual commands. */
Result OpenSessionForDev(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, s32 pad_descriptor) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result OpenSession(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
::GpioPadSession p;
R_TRY(::gpioOpenSession(std::addressof(p), static_cast<::GpioPadName>(static_cast<u32>(pad_name))));
out.SetValue(ams::sf::MakeShared<gpio::sf::IPadSession, RemotePadSessionImpl>(p));
return ResultSuccess();
}
Result OpenSessionForTest(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, gpio::GpioPadName pad_name) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result IsWakeEventActive(ams::sf::Out<bool> out, gpio::GpioPadName pad_name) {
return ::gpioIsWakeEventActive2(out.GetPointer(), static_cast<::GpioPadName>(static_cast<u32>(pad_name)));
}
Result GetWakeEventActiveFlagSet(ams::sf::Out<gpio::WakeBitFlag> out) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result SetWakeEventActiveFlagSetForDebug(gpio::GpioPadName pad_name, bool is_enabled) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result SetWakePinDebugMode(s32 mode) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result OpenSession2(ams::sf::Out<std::shared_ptr<gpio::sf::IPadSession>> out, DeviceCode device_code, ddsf::AccessMode access_mode) {
::GpioPadSession p;
R_TRY(::gpioOpenSession2(std::addressof(p), device_code.GetInternalValue(), access_mode));
out.SetValue(ams::sf::MakeShared<gpio::sf::IPadSession, RemotePadSessionImpl>(p));
return ResultSuccess();
}
Result IsWakeEventActive2(ams::sf::Out<bool> out, DeviceCode device_code) {
return ::gpioIsWakeEventActive2(out.GetPointer(), device_code.GetInternalValue());
}
Result SetWakeEventActiveFlagSetForDebug2(DeviceCode device_code, bool is_enabled) {
/* TODO: libnx bindings */
AMS_ABORT();
}
Result SetRetryValues(u32 arg0, u32 arg1) {
/* TODO: libnx bindings */
AMS_ABORT();
}
};
static_assert(gpio::sf::IsIManager<RemoteManagerImpl>);
}

View file

@ -0,0 +1,113 @@
/*
* 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 <stratosphere.hpp>
namespace ams::gpio {
class RemotePadSessionImpl {
private:
::GpioPadSession srv;
public:
RemotePadSessionImpl(::GpioPadSession &p) : srv(p) { /* ... */ }
~RemotePadSessionImpl() { ::gpioPadClose(std::addressof(this->srv)); }
public:
/* Actual commands. */
Result SetDirection(gpio::Direction direction) {
return ::gpioPadSetDirection(std::addressof(this->srv), static_cast<::GpioDirection>(static_cast<u32>(direction)));
}
Result GetDirection(ams::sf::Out<gpio::Direction> out) {
static_assert(sizeof(gpio::Direction) == sizeof(::GpioDirection));
return ::gpioPadGetDirection(std::addressof(this->srv), reinterpret_cast<::GpioDirection *>(out.GetPointer()));
}
Result SetInterruptMode(gpio::InterruptMode mode) {
return ::gpioPadSetInterruptMode(std::addressof(this->srv), static_cast<::GpioInterruptMode>(static_cast<u32>(mode)));
}
Result GetInterruptMode(ams::sf::Out<gpio::InterruptMode> out) {
static_assert(sizeof(gpio::InterruptMode) == sizeof(::GpioInterruptMode));
return ::gpioPadGetInterruptMode(std::addressof(this->srv), reinterpret_cast<::GpioInterruptMode *>(out.GetPointer()));
}
Result SetInterruptEnable(bool enable) {
return ::gpioPadSetInterruptEnable(std::addressof(this->srv), enable);
}
Result GetInterruptEnable(ams::sf::Out<bool> out) {
return ::gpioPadGetInterruptEnable(std::addressof(this->srv), out.GetPointer());
}
Result GetInterruptStatus(ams::sf::Out<gpio::InterruptStatus> out) {
static_assert(sizeof(gpio::InterruptStatus) == sizeof(::GpioInterruptStatus));
return ::gpioPadGetInterruptStatus(std::addressof(this->srv), reinterpret_cast<::GpioInterruptStatus *>(out.GetPointer()));
}
Result ClearInterruptStatus() {
return ::gpioPadClearInterruptStatus(std::addressof(this->srv));
}
Result SetValue(gpio::GpioValue value) {
return ::gpioPadSetValue(std::addressof(this->srv), static_cast<::GpioValue>(static_cast<u32>(value)));
}
Result GetValue(ams::sf::Out<gpio::GpioValue> out) {
static_assert(sizeof(gpio::GpioValue) == sizeof(::GpioValue));
return ::gpioPadGetValue(std::addressof(this->srv), reinterpret_cast<::GpioValue *>(out.GetPointer()));
}
Result BindInterrupt(ams::sf::OutCopyHandle out) {
::Event ev;
R_TRY(::gpioPadBindInterrupt(std::addressof(this->srv), std::addressof(ev)));
out.SetValue(ev.revent);
return ResultSuccess();
}
Result UnbindInterrupt() {
return ::gpioPadUnbindInterrupt(std::addressof(this->srv));
}
Result SetDebounceEnabled(bool enable) {
return ::gpioPadSetDebounceEnabled(std::addressof(this->srv), enable);
}
Result GetDebounceEnabled(ams::sf::Out<bool> out) {
return ::gpioPadGetDebounceEnabled(std::addressof(this->srv), out.GetPointer());
}
Result SetDebounceTime(s32 ms) {
return ::gpioPadSetDebounceTime(std::addressof(this->srv), ms);
}
Result GetDebounceTime(ams::sf::Out<s32> out) {
return ::gpioPadGetDebounceTime(std::addressof(this->srv), out.GetPointer());
}
Result SetValueForSleepState(gpio::GpioValue value) {
/* TODO: libnx bindings. */
AMS_ABORT();
}
Result GetValueForSleepState(ams::sf::Out<gpio::GpioValue> out) {
/* TODO: libnx bindings. */
AMS_ABORT();
}
};
static_assert(gpio::sf::IsIPadSession<RemotePadSessionImpl>);
}