Biao Huang | 8c83c65 | 2015-07-31 17:10:55 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2015 MediaTek 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. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | #include <arch/io.h> |
| 20 | #include <assert.h> |
| 21 | #include <console/console.h> |
| 22 | #include <gpio.h> |
| 23 | #include <types.h> |
| 24 | #include <soc/addressmap.h> |
| 25 | #include <soc/gpio.h> |
| 26 | |
| 27 | enum { |
| 28 | MAX_8173_GPIO = 134, |
| 29 | MAX_GPIO_REG_BITS = 16, |
| 30 | MAX_GPIO_MODE_PER_REG = 5, |
| 31 | GPIO_MODE_BITS = 3, |
| 32 | }; |
| 33 | |
| 34 | enum { |
| 35 | GPIO_DIRECTION_IN = 0, |
| 36 | GPIO_DIRECTION_OUT = 1, |
| 37 | }; |
| 38 | |
| 39 | enum { |
| 40 | GPIO_MODE = 0, |
| 41 | }; |
| 42 | |
| 43 | static void pos_bit_calc(u32 pin, u32 *pos, u32 *bit) |
| 44 | { |
| 45 | *pos = pin / MAX_GPIO_REG_BITS; |
| 46 | *bit = pin % MAX_GPIO_REG_BITS; |
| 47 | } |
| 48 | |
| 49 | static void pos_bit_calc_for_mode(u32 pin, u32 *pos, u32 *bit) |
| 50 | { |
| 51 | *pos = pin / MAX_GPIO_MODE_PER_REG; |
| 52 | *bit = (pin % MAX_GPIO_MODE_PER_REG) * GPIO_MODE_BITS; |
| 53 | } |
| 54 | |
| 55 | static s32 gpio_set_dir(u32 pin, u32 dir) |
| 56 | { |
| 57 | u32 pos; |
| 58 | u32 bit; |
| 59 | u16 *reg; |
| 60 | |
| 61 | assert(pin <= MAX_8173_GPIO); |
| 62 | |
| 63 | pos_bit_calc(pin, &pos, &bit); |
| 64 | |
| 65 | if (dir == GPIO_DIRECTION_IN) |
| 66 | reg = &mt8173_gpio->dir[pos].rst; |
| 67 | else |
| 68 | reg = &mt8173_gpio->dir[pos].set; |
| 69 | |
| 70 | write16(reg, 1L << bit); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | void gpio_set_pull(gpio_t pin, enum pull_enable enable, |
| 76 | enum pull_select select) |
| 77 | { |
| 78 | u32 pos; |
| 79 | u32 bit; |
| 80 | u16 *en_reg, *sel_reg; |
| 81 | |
| 82 | assert(pin <= MAX_8173_GPIO); |
| 83 | |
| 84 | pos_bit_calc(pin, &pos, &bit); |
| 85 | |
| 86 | if (enable == GPIO_PULL_DISABLE) { |
| 87 | en_reg = &mt8173_gpio->pullen[pos].rst; |
| 88 | } else { |
| 89 | /* These pins' pulls can't be set through GPIO controller. */ |
| 90 | assert(pin < 22 || pin > 27); |
| 91 | assert(pin < 47 || pin > 56); |
| 92 | assert(pin < 57 || pin > 68); |
| 93 | assert(pin < 73 || pin > 78); |
| 94 | assert(pin < 100 || pin > 105); |
| 95 | assert(pin < 119 || pin > 124); |
| 96 | |
| 97 | en_reg = &mt8173_gpio->pullen[pos].set; |
| 98 | sel_reg = (select == GPIO_PULL_DOWN) ? |
| 99 | (&mt8173_gpio->pullsel[pos].rst) : |
| 100 | (&mt8173_gpio->pullsel[pos].set); |
| 101 | write16(sel_reg, 1L << bit); |
| 102 | } |
| 103 | write16(en_reg, 1L << bit); |
| 104 | } |
| 105 | |
| 106 | int gpio_get(gpio_t pin) |
| 107 | { |
| 108 | u32 pos; |
| 109 | u32 bit; |
| 110 | u16 *reg; |
| 111 | s32 data; |
| 112 | |
| 113 | assert(pin <= MAX_8173_GPIO); |
| 114 | |
| 115 | pos_bit_calc(pin, &pos, &bit); |
| 116 | |
| 117 | reg = &mt8173_gpio->din[pos].val; |
| 118 | data = read32(reg); |
| 119 | |
| 120 | return (data & (1L << bit)) ? 1 : 0; |
| 121 | } |
| 122 | |
| 123 | void gpio_set(gpio_t pin, int output) |
| 124 | { |
| 125 | u32 pos; |
| 126 | u32 bit; |
| 127 | u16 *reg; |
| 128 | |
| 129 | assert(pin <= MAX_8173_GPIO); |
| 130 | |
| 131 | pos_bit_calc(pin, &pos, &bit); |
| 132 | |
| 133 | if (output == 0) |
| 134 | reg = &mt8173_gpio->dout[pos].rst; |
| 135 | else |
| 136 | reg = &mt8173_gpio->dout[pos].set; |
| 137 | write16(reg, 1L << bit); |
| 138 | } |
| 139 | |
| 140 | void gpio_set_mode(gpio_t pin, int mode) |
| 141 | { |
| 142 | u32 pos; |
| 143 | u32 bit; |
| 144 | u32 mask = (1L << GPIO_MODE_BITS) - 1; |
| 145 | |
| 146 | assert(pin <= MAX_8173_GPIO); |
| 147 | |
| 148 | pos_bit_calc_for_mode(pin, &pos, &bit); |
| 149 | |
| 150 | clrsetbits_le32(&mt8173_gpio->mode[pos].val, |
| 151 | mask << bit, mode << bit); |
| 152 | } |
| 153 | |
| 154 | void gpio_input_pulldown(gpio_t gpio) |
| 155 | { |
| 156 | gpio_set_pull(gpio, GPIO_PULL_ENABLE, GPIO_PULL_DOWN); |
| 157 | gpio_set_dir(gpio, GPIO_DIRECTION_IN); |
| 158 | gpio_set_mode(gpio, GPIO_MODE); |
| 159 | } |
| 160 | |
| 161 | void gpio_input_pullup(gpio_t gpio) |
| 162 | { |
| 163 | gpio_set_pull(gpio, GPIO_PULL_ENABLE, GPIO_PULL_UP); |
| 164 | gpio_set_dir(gpio, GPIO_DIRECTION_IN); |
| 165 | gpio_set_mode(gpio, GPIO_MODE); |
| 166 | } |
| 167 | |
| 168 | void gpio_input(gpio_t gpio) |
| 169 | { |
| 170 | gpio_set_pull(gpio, GPIO_PULL_DISABLE, GPIO_PULL_DOWN); |
| 171 | gpio_set_dir(gpio, GPIO_DIRECTION_IN); |
| 172 | gpio_set_mode(gpio, GPIO_MODE); |
| 173 | } |
| 174 | |
| 175 | void gpio_output(gpio_t gpio, int value) |
| 176 | { |
| 177 | gpio_set_pull(gpio, GPIO_PULL_DISABLE, GPIO_PULL_DOWN); |
| 178 | gpio_set(gpio, value); |
| 179 | gpio_set_dir(gpio, GPIO_DIRECTION_OUT); |
| 180 | gpio_set_mode(gpio, GPIO_MODE); |
| 181 | } |