coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
gpio.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #include <device/mmio.h>
4 #include <console/console.h>
5 #include <soc/ti/am335x/gpio.h>
6 #include <stdint.h>
7 
8 static struct am335x_gpio_regs *gpio_regs_and_bit(unsigned int gpio,
9  uint32_t *bit)
10 {
11  unsigned int bank = gpio / AM335X_GPIO_BITS_PER_BANK;
12 
13  if (bank >= ARRAY_SIZE(am335x_gpio_banks)) {
14  printk(BIOS_ERR, "Bad gpio index %d.\n", gpio);
15  return NULL;
16  }
17  *bit = 1 << (gpio % 32);
18  return am335x_gpio_banks[bank];
19 }
20 
22 {
23  int i;
24 
25  for (i = 0; i < ARRAY_SIZE(am335x_gpio_banks); i++)
26  write32(&am335x_gpio_banks[i]->irqstatus_clr_0, 0xffffffff);
27 }
28 
29 int gpio_direction_input(unsigned int gpio)
30 {
31  uint32_t bit;
33 
34  if (!regs)
35  return -1;
36  setbits32(&regs->oe, bit);
37  return 0;
38 }
39 
40 int gpio_direction_output(unsigned int gpio, int value)
41 {
42  uint32_t bit;
44 
45  if (!regs)
46  return -1;
47  if (value)
48  write32(&regs->setdataout, bit);
49  else
50  write32(&regs->cleardataout, bit);
51  clrbits32(&regs->oe, bit);
52  return 0;
53 }
54 
55 int gpio_get_value(unsigned int gpio)
56 {
57  uint32_t bit;
59 
60  if (!regs)
61  return -1;
62  return (read32(&regs->datain) & bit) ? 1 : 0;
63 }
64 
65 int gpio_set_value(unsigned int gpio, int value)
66 {
67  uint32_t bit;
69 
70  if (!regs)
71  return -1;
72  if (value)
73  write32(&regs->setdataout, bit);
74  else
75  write32(&regs->cleardataout, bit);
76  return 0;
77 }
pte_t value
Definition: mmu.c:91
static void write32(void *addr, uint32_t val)
Definition: mmio.h:40
static uint32_t read32(const void *addr)
Definition: mmio.h:22
#define ARRAY_SIZE(a)
Definition: helpers.h:12
#define printk(level,...)
Definition: stdlib.h:16
#define setbits32(addr, set)
Definition: mmio.h:21
#define clrbits32(addr, clear)
Definition: mmio.h:26
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
int gpio_direction_output(unsigned int gpio, int value)
Make a GPIO an output, and set its value.
Definition: gpio.c:151
int gpio_direction_input(unsigned int gpio)
Make a GPIO an input.
Definition: gpio.c:144
int gpio_set_value(unsigned int gpio, int value)
Set an output GPIO's value.
Definition: gpio.c:176
int gpio_get_value(unsigned int gpio)
Get a GPIO's value.
Definition: gpio.c:167
void am335x_disable_gpio_irqs(void)
Definition: gpio.c:21
static struct am335x_gpio_regs * gpio_regs_and_bit(unsigned int gpio, uint32_t *bit)
Definition: gpio.c:8
@ AM335X_GPIO_BITS_PER_BANK
Definition: gpio.h:9
static struct am335x_gpio_regs *const am335x_gpio_banks[]
Definition: gpio.h:46
#define NULL
Definition: stddef.h:19
unsigned int uint32_t
Definition: stdint.h:14
uint32_t irqstatus_clr_0
Definition: gpio.h:24
Definition: pinmux.c:36