| 1 |
|
| 2 |
|
| 3 |
#include <device/mmio.h> |
| 4 |
#include <soc/addressmap.h> |
| 5 |
#include <soc/padconfig.h> |
| 6 |
|
| 7 |
static uint32_t * const pinmux_regs = (void *)(uintptr_t)TEGRA_APB_PINMUX_BASE; |
| 8 |
static struct gpio_bank * const gpio_regs = (void *)(uintptr_t)TEGRA_GPIO_BASE; |
| 9 |
|
| 10 |
static inline struct gpio_bank * const get_gpio_bank_regs(int index) |
| 11 |
{ |
| 12 |
return &gpio_regs[gpio_index_to_bank(index)]; |
| 13 |
} |
| 14 |
|
| 15 |
static inline uint32_t pad_get_pinmux(int index) |
| 16 |
{ |
| 17 |
return read32(&pinmux_regs[index]); |
| 18 |
} |
| 19 |
|
| 20 |
static inline void pad_set_pinmux(int index, uint32_t reg) |
| 21 |
{ |
| 22 |
return write32(&pinmux_regs[index], reg); |
| 23 |
} |
| 24 |
|
| 25 |
static inline void pad_set_gpio_out(int gpio_index, int val) |
| 26 |
{ |
| 27 |
struct gpio_bank * const regs = get_gpio_bank_regs(gpio_index); |
| 28 |
int port = gpio_index_to_port(gpio_index); |
| 29 |
int bit = gpio_to_bit(gpio_index); |
| 30 |
|
| 31 |
write32(®s->out_value_mask[port], |
| 32 |
(1 << (bit + GPIO_GPIOS_PER_PORT)) | (val << bit)); |
| 33 |
write32(®s->out_enable_mask[port], |
| 34 |
(1 << (bit + GPIO_GPIOS_PER_PORT)) | (1 << bit)); |
| 35 |
} |
| 36 |
|
| 37 |
static inline void pad_set_mode(int gpio_index, int sfio_or_gpio) |
| 38 |
{ |
| 39 |
struct gpio_bank * const regs = get_gpio_bank_regs(gpio_index); |
| 40 |
int port = gpio_index_to_port(gpio_index); |
| 41 |
int bit = gpio_to_bit(gpio_index); |
| 42 |
|
| 43 |
write32(®s->config_mask[port], |
| 44 |
(1 << (bit + GPIO_GPIOS_PER_PORT)) | (sfio_or_gpio << bit)); |
| 45 |
} |
| 46 |
|
| 47 |
static inline void pad_set_gpio_mode(int gpio_index) |
| 48 |
{ |
| 49 |
pad_set_mode(gpio_index, 1); |
| 50 |
} |
| 51 |
|
| 52 |
static inline void pad_set_sfio_mode(int gpio_index) |
| 53 |
{ |
| 54 |
pad_set_mode(gpio_index, 0); |
| 55 |
} |
| 56 |
|
| 57 |
static void configure_unused_pad(const struct pad_config * const entry) |
| 58 |
{ |
| 59 |
uint32_t reg; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
reg = pad_get_pinmux(entry->pinmux_index); |
| 66 |
reg &= ~PINMUX_INPUT_ENABLE; |
| 67 |
reg |= PINMUX_TRISTATE; |
| 68 |
reg &= ~PINMUX_PULL_MASK; |
| 69 |
if (entry->por_pullup) |
| 70 |
reg |= PINMUX_PULL_UP; |
| 71 |
else |
| 72 |
reg |= PINMUX_PULL_DOWN; |
| 73 |
pad_set_pinmux(entry->pinmux_index, reg); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
if (entry->pad_has_gpio) |
| 80 |
pad_set_gpio_mode(entry->gpio_index); |
| 81 |
} |
| 82 |
|
| 83 |
static void configure_sfio_pad(const struct pad_config * const entry) |
| 84 |
{ |
| 85 |
pad_set_pinmux(entry->pinmux_index, entry->pinmux_flags); |
| 86 |
pad_set_sfio_mode(entry->gpio_index); |
| 87 |
} |
| 88 |
|
| 89 |
static void configure_gpio_pad(const struct pad_config * const entry) |
| 90 |
{ |
| 91 |
uint32_t reg; |
| 92 |
|
| 93 |
if (entry->gpio_out0 || entry->gpio_out1) |
| 94 |
pad_set_gpio_out(entry->gpio_index, entry->gpio_out1 ? 1 : 0); |
| 95 |
|
| 96 |
|
| 97 |
reg = pinmux_get_config(entry->pinmux_index); |
| 98 |
reg &= PINMUX_FUNC_MASK; |
| 99 |
reg |= entry->pinmux_flags; |
| 100 |
|
| 101 |
pad_set_pinmux(entry->pinmux_index, reg); |
| 102 |
pad_set_gpio_mode(entry->gpio_index); |
| 103 |
} |
| 104 |
|
| 105 |
void soc_configure_pads(const struct pad_config * const entries, size_t num) |
| 106 |
{ |
| 107 |
size_t i; |
| 108 |
|
| 109 |
for (i = 0; i < num; i++) { |
| 110 |
const struct pad_config * const entry = &entries[i]; |
| 111 |
|
| 112 |
if (entry->unused) { |
| 113 |
configure_unused_pad(entry); |
| 114 |
} else if (entry->sfio) { |
| 115 |
configure_sfio_pad(entry); |
| 116 |
} else { |
| 117 |
configure_gpio_pad(entry); |
| 118 |
} |
| 119 |
} |
| 120 |
} |