Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2013 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <arch/io.h> |
| 17 | #include <console/console.h> |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 18 | #include <gpio.h> |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 19 | #include <soc/addressmap.h> |
| 20 | #include <stddef.h> |
| 21 | #include <stdint.h> |
Tom Warren | 64982c50 | 2014-01-23 13:37:50 -0700 | [diff] [blame] | 22 | #include <delay.h> |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 23 | |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 24 | #include "pinmux.h" |
| 25 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 26 | static void __gpio_input(gpio_t gpio, u32 pull) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 27 | { |
Ken Chang | 41359bd | 2014-04-21 17:54:28 +0800 | [diff] [blame] | 28 | u32 pinmux_config = PINMUX_INPUT_ENABLE | pull; |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 29 | |
| 30 | gpio_set_int_enable(gpio, 0); |
| 31 | gpio_set_out_enable(gpio, 0); |
| 32 | gpio_set_mode(gpio, GPIO_MODE_GPIO); |
| 33 | pinmux_set_config(gpio >> GPIO_PINMUX_SHIFT, pinmux_config); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 36 | static void __gpio_output(gpio_t gpio, int value, u32 od) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 37 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 38 | gpio_set_int_enable(gpio, 0); |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 39 | gpio_set(gpio, value); |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 40 | gpio_set_out_enable(gpio, 1); |
| 41 | gpio_set_mode(gpio, GPIO_MODE_GPIO); |
Hung-Te Lin | 2fc3b62 | 2013-10-21 21:43:03 +0800 | [diff] [blame] | 42 | pinmux_set_config(gpio >> GPIO_PINMUX_SHIFT, PINMUX_PULL_NONE | od); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 45 | static const struct gpio_bank *gpio_banks = (void *)TEGRA_GPIO_BASE; |
| 46 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 47 | static u32 gpio_read_port(int index, size_t offset) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 48 | { |
| 49 | int bank = index / GPIO_GPIOS_PER_BANK; |
| 50 | int port = (index - bank * GPIO_GPIOS_PER_BANK) / GPIO_GPIOS_PER_PORT; |
| 51 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 52 | return read32((u8 *)&gpio_banks[bank] + offset + |
| 53 | port * sizeof(u32)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 56 | static void gpio_write_port(int index, size_t offset, u32 mask, u32 value) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 57 | { |
| 58 | int bank = index / GPIO_GPIOS_PER_BANK; |
| 59 | int port = (index - bank * GPIO_GPIOS_PER_BANK) / GPIO_GPIOS_PER_PORT; |
| 60 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 61 | u32 reg = read32((u8 *)&gpio_banks[bank] + offset + |
| 62 | port * sizeof(u32)); |
| 63 | u32 new_reg = (reg & ~mask) | (value & mask); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 64 | |
| 65 | if (new_reg != reg) { |
Julius Werner | 2f37bd6 | 2015-02-19 14:51:15 -0800 | [diff] [blame] | 66 | write32((u8 *)&gpio_banks[bank] + offset + port * sizeof(u32), |
| 67 | new_reg); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 71 | void gpio_set_mode(gpio_t gpio, enum gpio_mode mode) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 72 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 73 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 74 | gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 75 | offsetof(struct gpio_bank, config), |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 76 | 1 << bit, mode ? (1 << bit) : 0); |
| 77 | } |
| 78 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 79 | int gpio_get_mode(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 80 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 81 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 82 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 83 | offsetof(struct gpio_bank, config)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 84 | return (port & (1 << bit)) != 0; |
| 85 | } |
| 86 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 87 | void gpio_set_lock(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 88 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 89 | int bit = gpio % GPIO_GPIOS_PER_PORT + GPIO_GPIOS_PER_PORT; |
| 90 | gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 91 | offsetof(struct gpio_bank, config), |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 92 | 1 << bit, 1 << bit); |
| 93 | } |
| 94 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 95 | int gpio_get_lock(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 96 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 97 | int bit = gpio % GPIO_GPIOS_PER_PORT + GPIO_GPIOS_PER_PORT; |
| 98 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 99 | offsetof(struct gpio_bank, config)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 100 | return (port & (1 << bit)) != 0; |
| 101 | } |
| 102 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 103 | void gpio_set_out_enable(gpio_t gpio, int enable) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 104 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 105 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 106 | gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 107 | offsetof(struct gpio_bank, out_enable), |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 108 | 1 << bit, enable ? (1 << bit) : 0); |
| 109 | } |
| 110 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 111 | int gpio_get_out_enable(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 112 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 113 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 114 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 115 | offsetof(struct gpio_bank, out_enable)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 116 | return (port & (1 << bit)) != 0; |
| 117 | } |
| 118 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 119 | void gpio_set(gpio_t gpio, int value) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 120 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 121 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 122 | gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 123 | offsetof(struct gpio_bank, out_value), |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 124 | 1 << bit, value ? (1 << bit) : 0); |
| 125 | } |
| 126 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 127 | int gpio_get_out_value(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 128 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 129 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 130 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 131 | offsetof(struct gpio_bank, out_value)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 132 | return (port & (1 << bit)) != 0; |
| 133 | } |
| 134 | |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 135 | int gpio_get(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 136 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 137 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 138 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 139 | offsetof(struct gpio_bank, in_value)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 140 | return (port & (1 << bit)) != 0; |
| 141 | } |
| 142 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 143 | int gpio_get_int_status(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 144 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 145 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 146 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 147 | offsetof(struct gpio_bank, int_status)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 148 | return (port & (1 << bit)) != 0; |
| 149 | } |
| 150 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 151 | void gpio_set_int_enable(gpio_t gpio, int enable) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 152 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 153 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 154 | gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 155 | offsetof(struct gpio_bank, int_enable), |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 156 | 1 << bit, enable ? (1 << bit) : 0); |
| 157 | } |
| 158 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 159 | int gpio_get_int_enable(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 160 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 161 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 162 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 163 | offsetof(struct gpio_bank, int_enable)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 164 | return (port & (1 << bit)) != 0; |
| 165 | } |
| 166 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 167 | void gpio_set_int_level(gpio_t gpio, int high_rise, int edge, int delta) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 168 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 169 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 170 | u32 value = (high_rise ? (0x000001 << bit) : 0) | |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 171 | (edge ? (0x000100 << bit) : 0) | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 172 | (delta ? (0x010000 << bit) : 0); |
| 173 | gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 174 | offsetof(struct gpio_bank, config), |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 175 | 0x010101 << bit, value); |
| 176 | } |
| 177 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 178 | void gpio_get_int_level(gpio_t gpio, int *high_rise, int *edge, int *delta) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 179 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 180 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 181 | u32 port = gpio_read_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 182 | offsetof(struct gpio_bank, int_level)); |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 183 | *high_rise = ((port & (0x000001 << bit)) != 0); |
| 184 | *edge = ((port & (0x000100 << bit)) != 0); |
| 185 | *delta = ((port & (0x010000 << bit)) != 0); |
| 186 | } |
| 187 | |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 188 | void gpio_set_int_clear(gpio_t gpio) |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 189 | { |
Gabe Black | d40be11 | 2013-10-09 23:45:07 -0700 | [diff] [blame] | 190 | int bit = gpio % GPIO_GPIOS_PER_PORT; |
| 191 | gpio_write_port(gpio & ((1 << GPIO_PINMUX_SHIFT) - 1), |
| 192 | offsetof(struct gpio_bank, int_clear), |
Gabe Black | 08d5a89 | 2013-10-03 04:35:01 -0700 | [diff] [blame] | 193 | 1 << bit, 1 << bit); |
| 194 | } |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 195 | |
| 196 | void gpio_input_pulldown(gpio_t gpio) |
| 197 | { |
| 198 | __gpio_input(gpio, PINMUX_PULL_DOWN); |
| 199 | } |
| 200 | |
| 201 | void gpio_input_pullup(gpio_t gpio) |
| 202 | { |
| 203 | __gpio_input(gpio, PINMUX_PULL_UP); |
| 204 | } |
| 205 | |
| 206 | void gpio_input(gpio_t gpio) |
| 207 | { |
| 208 | __gpio_input(gpio, PINMUX_PULL_NONE); |
| 209 | } |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 210 | |
| 211 | void gpio_output(gpio_t gpio, int value) |
| 212 | { |
| 213 | __gpio_output(gpio, value, 0); |
| 214 | } |
| 215 | |
| 216 | void gpio_output_open_drain(gpio_t gpio, int value) |
| 217 | { |
| 218 | __gpio_output(gpio, value, PINMUX_OPEN_DRAIN); |
| 219 | } |