2019-02-20 12:52:11 +00:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-02-20 12:52:11 +00: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "gpio.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2020-03-09 19:33:39 +00:00
|
|
|
/* Set GPIO's value. */
|
|
|
|
static void gpio_register_set(uint32_t pin, bool do_set, uint32_t offset) {
|
2019-02-20 12:52:11 +00:00
|
|
|
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
2020-03-09 19:33:39 +00:00
|
|
|
|
2019-07-23 18:33:34 +01:00
|
|
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
2020-03-09 19:33:39 +00:00
|
|
|
volatile uint32_t *cluster = (uint32_t *)((uintptr_t)&gpio->bank[(pin >> GPIO_BANK_SHIFT)] + offset);
|
2019-02-20 12:52:11 +00:00
|
|
|
|
2019-07-23 18:33:34 +01:00
|
|
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
2020-03-09 19:33:39 +00:00
|
|
|
uint32_t port = ((pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK);
|
|
|
|
uint32_t mask = (1 << (pin & GPIO_PIN_MASK));
|
2019-02-20 12:52:11 +00:00
|
|
|
|
2019-07-23 18:33:34 +01:00
|
|
|
/* Set or clear the bit, as appropriate. */
|
2020-03-09 19:33:39 +00:00
|
|
|
if (do_set)
|
2019-07-23 18:33:34 +01:00
|
|
|
cluster[port] |= mask;
|
2019-02-20 12:52:11 +00:00
|
|
|
else
|
|
|
|
cluster[port] &= ~mask;
|
2019-07-23 18:33:34 +01:00
|
|
|
|
|
|
|
/* Dummy read. */
|
2020-03-09 19:33:39 +00:00
|
|
|
cluster[port];
|
2019-02-20 12:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:33:39 +00:00
|
|
|
/* Get GPIO's value. */
|
|
|
|
static bool gpio_register_get(uint32_t pin, uint32_t offset) {
|
|
|
|
volatile tegra_gpio_t *gpio = gpio_get_regs();
|
|
|
|
|
2019-07-23 18:33:34 +01:00
|
|
|
/* Retrieve the register set that corresponds to the given pin and offset. */
|
2020-03-09 19:33:39 +00:00
|
|
|
volatile uint32_t *cluster = (uint32_t *)((uintptr_t)&gpio->bank[(pin >> GPIO_BANK_SHIFT)] + offset);
|
2019-02-20 12:52:11 +00:00
|
|
|
|
2019-07-23 18:33:34 +01:00
|
|
|
/* Figure out the offset into the cluster, and the mask to be used. */
|
2020-03-09 19:33:39 +00:00
|
|
|
uint32_t port = ((pin >> GPIO_PORT_SHIFT) & GPIO_PORT_MASK);
|
|
|
|
uint32_t mask = (1 << (pin & GPIO_PIN_MASK));
|
2019-02-20 12:52:11 +00:00
|
|
|
|
2019-07-23 18:33:34 +01:00
|
|
|
/* Convert the given value to a boolean. */
|
2019-02-20 12:52:11 +00:00
|
|
|
return !!(cluster[port] & mask);
|
|
|
|
}
|
|
|
|
|
2020-03-09 19:33:39 +00:00
|
|
|
/* Configure GPIO's mode. */
|
2019-07-23 18:33:34 +01:00
|
|
|
void gpio_configure_mode(uint32_t pin, uint32_t mode) {
|
2020-03-09 19:33:39 +00:00
|
|
|
gpio_register_set(pin, mode == GPIO_MODE_GPIO, offsetof(tegra_gpio_bank_t, config));
|
2019-02-20 12:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:33:39 +00:00
|
|
|
/* Configure GPIO's direction. */
|
2019-07-23 18:33:34 +01:00
|
|
|
void gpio_configure_direction(uint32_t pin, uint32_t dir) {
|
2020-03-09 19:33:39 +00:00
|
|
|
gpio_register_set(pin, dir == GPIO_DIRECTION_OUTPUT, offsetof(tegra_gpio_bank_t, direction));
|
2019-02-20 12:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:33:39 +00:00
|
|
|
/* Write to GPIO. */
|
2019-07-23 18:33:34 +01:00
|
|
|
void gpio_write(uint32_t pin, uint32_t value) {
|
2020-03-09 19:33:39 +00:00
|
|
|
gpio_register_set(pin, value == GPIO_LEVEL_HIGH, offsetof(tegra_gpio_bank_t, out));
|
2019-02-20 12:52:11 +00:00
|
|
|
}
|
|
|
|
|
2020-03-09 19:33:39 +00:00
|
|
|
/* Read from GPIO. */
|
2019-07-23 18:33:34 +01:00
|
|
|
uint32_t gpio_read(uint32_t pin) {
|
2020-03-09 19:33:39 +00:00
|
|
|
return gpio_register_get(pin, offsetof(tegra_gpio_bank_t, in));
|
2019-02-20 12:52:11 +00:00
|
|
|
}
|