| 1 |
|
| 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 |
|
| 21 |
void am335x_disable_gpio_irqs(void) |
| 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; |
| 32 |
struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit); |
| 33 |
|
| 34 |
if (!regs) |
| 35 |
return -1; |
| 36 |
setbits32(®s->oe, bit); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
int gpio_direction_output(unsigned int gpio, int value) |
| 41 |
{ |
| 42 |
uint32_t bit; |
| 43 |
struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit); |
| 44 |
|
| 45 |
if (!regs) |
| 46 |
return -1; |
| 47 |
if (value) |
| 48 |
write32(®s->setdataout, bit); |
| 49 |
else |
| 50 |
write32(®s->cleardataout, bit); |
| 51 |
clrbits32(®s->oe, bit); |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
int gpio_get_value(unsigned int gpio) |
| 56 |
{ |
| 57 |
uint32_t bit; |
| 58 |
struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit); |
| 59 |
|
| 60 |
if (!regs) |
| 61 |
return -1; |
| 62 |
return (read32(®s->datain) & bit) ? 1 : 0; |
| 63 |
} |
| 64 |
|
| 65 |
int gpio_set_value(unsigned int gpio, int value) |
| 66 |
{ |
| 67 |
uint32_t bit; |
| 68 |
struct am335x_gpio_regs *regs = gpio_regs_and_bit(gpio, &bit); |
| 69 |
|
| 70 |
if (!regs) |
| 71 |
return -1; |
| 72 |
if (value) |
| 73 |
write32(®s->setdataout, bit); |
| 74 |
else |
| 75 |
write32(®s->cleardataout, bit); |
| 76 |
return 0; |
| 77 |
} |