2019-04-29 17:43:48 +01:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-04-29 17:43:48 +01:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
#include <stratosphere/reg.hpp>
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-06-22 19:34:18 +01:00
|
|
|
#include "gpio_utils.hpp"
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::gpio {
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
namespace {
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Pull in GPIO map definitions. */
|
2019-06-22 19:34:18 +01:00
|
|
|
#include "gpio_map.inc"
|
2019-06-22 08:10:21 +01:00
|
|
|
|
|
|
|
constexpr u32 PhysicalBase = 0x6000D000;
|
|
|
|
|
|
|
|
/* Globals. */
|
|
|
|
bool g_initialized_gpio_vaddr = false;
|
|
|
|
uintptr_t g_gpio_vaddr = 0;
|
|
|
|
|
|
|
|
/* Helpers. */
|
|
|
|
inline u32 GetPadDescriptor(u32 gpio_pad_name) {
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(gpio_pad_name < PadNameMax);
|
2019-06-22 08:10:21 +01:00
|
|
|
return Map[gpio_pad_name];
|
|
|
|
}
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
uintptr_t GetBaseAddress() {
|
|
|
|
if (!g_initialized_gpio_vaddr) {
|
2019-12-06 07:41:33 +00:00
|
|
|
g_gpio_vaddr = dd::GetIoMapping(PhysicalBase, os::MemoryPageSize);
|
2019-06-22 08:10:21 +01:00
|
|
|
g_initialized_gpio_vaddr = true;
|
|
|
|
}
|
|
|
|
return g_gpio_vaddr;
|
|
|
|
}
|
2019-04-29 17:43:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
u32 Configure(u32 gpio_pad_name) {
|
|
|
|
uintptr_t gpio_base_vaddr = GetBaseAddress();
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Fetch this GPIO's pad descriptor */
|
|
|
|
const u32 gpio_pad_desc = GetPadDescriptor(gpio_pad_name);
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Discard invalid GPIOs */
|
|
|
|
if (gpio_pad_desc == InvalidPadName) {
|
|
|
|
return InvalidPadName;
|
|
|
|
}
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Convert the GPIO pad descriptor into its register offset */
|
|
|
|
u32 gpio_reg_offset = (((gpio_pad_desc << 0x03) & 0xFFFFFF00) | ((gpio_pad_desc >> 0x01) & 0x0C));
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Extract the bit and lock values from the GPIO pad descriptor */
|
|
|
|
u32 gpio_cnf_val = ((0x01 << ((gpio_pad_desc & 0x07) | 0x08)) | (0x01 << (gpio_pad_desc & 0x07)));
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Write to the appropriate GPIO_CNF_x register (upper offset) */
|
|
|
|
reg::Write(gpio_base_vaddr + gpio_reg_offset + 0x80, gpio_cnf_val);
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Do a dummy read from GPIO_CNF_x register (lower offset) */
|
|
|
|
gpio_cnf_val = reg::Read(gpio_base_vaddr + gpio_reg_offset + 0x00);
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
return gpio_cnf_val;
|
2019-04-29 17:43:48 +01:00
|
|
|
}
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
u32 SetDirection(u32 gpio_pad_name, GpioDirection dir) {
|
|
|
|
uintptr_t gpio_base_vaddr = GetBaseAddress();
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Fetch this GPIO's pad descriptor */
|
|
|
|
const u32 gpio_pad_desc = GetPadDescriptor(gpio_pad_name);
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Discard invalid GPIOs */
|
|
|
|
if (gpio_pad_desc == InvalidPadName) {
|
|
|
|
return InvalidPadName;
|
|
|
|
}
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Convert the GPIO pad descriptor into its register offset */
|
|
|
|
u32 gpio_reg_offset = (((gpio_pad_desc << 0x03) & 0xFFFFFF00) | ((gpio_pad_desc >> 0x01) & 0x0C));
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Set the direction bit and lock values */
|
|
|
|
u32 gpio_oe_val = ((0x01 << ((gpio_pad_desc & 0x07) | 0x08)) | (static_cast<u32>(dir) << (gpio_pad_desc & 0x07)));
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Write to the appropriate GPIO_OE_x register (upper offset) */
|
|
|
|
reg::Write(gpio_base_vaddr + gpio_reg_offset + 0x90, gpio_oe_val);
|
2019-04-29 17:43:48 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Do a dummy read from GPIO_OE_x register (lower offset) */
|
|
|
|
gpio_oe_val = reg::Read(gpio_base_vaddr + gpio_reg_offset + 0x10);
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
return gpio_oe_val;
|
2019-04-29 17:43:48 +01:00
|
|
|
}
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
u32 SetValue(u32 gpio_pad_name, GpioValue val) {
|
|
|
|
uintptr_t gpio_base_vaddr = GetBaseAddress();
|
|
|
|
|
|
|
|
/* Fetch this GPIO's pad descriptor */
|
|
|
|
const u32 gpio_pad_desc = GetPadDescriptor(gpio_pad_name);
|
|
|
|
|
|
|
|
/* Discard invalid GPIOs */
|
|
|
|
if (gpio_pad_desc == InvalidPadName) {
|
|
|
|
return InvalidPadName;
|
|
|
|
}
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Convert the GPIO pad descriptor into its register offset */
|
|
|
|
u32 gpio_reg_offset = (((gpio_pad_desc << 0x03) & 0xFFFFFF00) | ((gpio_pad_desc >> 0x01) & 0x0C));
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Set the output bit and lock values */
|
|
|
|
u32 gpio_out_val = ((0x01 << ((gpio_pad_desc & 0x07) | 0x08)) | (static_cast<u32>(val) << (gpio_pad_desc & 0x07)));
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-06-22 08:10:21 +01:00
|
|
|
/* Write to the appropriate GPIO_OUT_x register (upper offset) */
|
|
|
|
reg::Write(gpio_base_vaddr + gpio_reg_offset + 0xA0, gpio_out_val);
|
|
|
|
|
|
|
|
/* Do a dummy read from GPIO_OUT_x register (lower offset) */
|
|
|
|
gpio_out_val = reg::Read(gpio_base_vaddr + gpio_reg_offset + 0x20);
|
|
|
|
|
|
|
|
return gpio_out_val;
|
|
|
|
}
|
2019-05-03 14:20:50 +01:00
|
|
|
|
2019-04-29 17:43:48 +01:00
|
|
|
}
|