summaryrefslogtreecommitdiff
path: root/src/cpu/ti/am335x/gpio.c
blob: de7f7404f72a1c8e6a2b342bef97eb43ee83204b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <device/mmio.h>
#include <console/console.h>
#include <cpu/ti/am335x/gpio.h>
#include <stdint.h>

static struct am335x_gpio_regs *gpio_regs_and_bit(unsigned int gpio,
						  uint32_t *bit)
{
	unsigned int bank = gpio / AM335X_GPIO_BITS_PER_BANK;

	if (bank >= ARRAY_SIZE(am335x_gpio_banks)) {
		printk(BIOS_ERR, "Bad gpio index %d.\n", gpio);
		return NULL;
	}
	*bit = 1 << (gpio % 32);
	return am335x_gpio_banks[bank];
}

void am335x_disable_gpio_irqs(void)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(am335x_gpio_banks); i++)
		write32(&am335x_gpio_banks[i]->irqstatus_clr_0, 0xffffffff);
}

int gpio_direction_input(unsigned int gpio)
{
	uint32_t bit;
	struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit);

	if (!regs)
		return -1;
	setbits32(&regs->oe, bit);
	return 0;
}

int gpio_direction_output(unsigned int gpio, int value)
{
	uint32_t bit;
	struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit);

	if (!regs)
		return -1;
	if (value)
		write32(&regs->setdataout, bit);
	else
		write32(&regs->cleardataout, bit);
	clrbits32(&regs->oe, bit);
	return 0;
}

int gpio_get_value(unsigned int gpio)
{
	uint32_t bit;
	struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit);

	if (!regs)
		return -1;
	return (read32(&regs->datain) & bit) ? 1 : 0;
}

int gpio_set_value(unsigned int gpio, int value)
{
	uint32_t bit;
	struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit);

	if (!regs)
		return -1;
	if (value)
		write32(&regs->setdataout, bit);
	else
		write32(&regs->cleardataout, bit);
	return 0;
}