Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2014 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. |
| 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 | |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 20 | #include <base3.h> |
| 21 | #include <console/console.h> |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 22 | #include <delay.h> |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 23 | #include <gpio.h> |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 24 | |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 25 | int gpio_get_tristates(gpio_t gpio[], int num_gpio) |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 26 | { |
| 27 | /* |
| 28 | * GPIOs which are tied to stronger external pull up or pull down |
| 29 | * will stay there regardless of the internal pull up or pull |
| 30 | * down setting. |
| 31 | * |
| 32 | * GPIOs which are floating will go to whatever level they're |
| 33 | * internally pulled to. |
| 34 | */ |
| 35 | |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 36 | static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'}; |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 37 | int temp; |
| 38 | int index; |
| 39 | int id = 0; |
| 40 | char value[num_gpio]; |
| 41 | |
| 42 | /* Enable internal pull up */ |
| 43 | for (index = 0; index < num_gpio; ++index) |
| 44 | gpio_input_pullup(gpio[index]); |
| 45 | |
| 46 | /* Wait until signals become stable */ |
| 47 | udelay(10); |
| 48 | |
| 49 | /* Get gpio values at internal pull up */ |
| 50 | for (index = 0; index < num_gpio; ++index) |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 51 | value[index] = gpio_get(gpio[index]); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 52 | |
| 53 | /* Enable internal pull down */ |
| 54 | for (index = 0; index < num_gpio; ++index) |
| 55 | gpio_input_pulldown(gpio[index]); |
| 56 | |
| 57 | /* Wait until signals become stable */ |
| 58 | udelay(10); |
| 59 | |
| 60 | /* |
| 61 | * Get gpio values at internal pull down. |
| 62 | * Compare with gpio pull up value and then |
| 63 | * determine a gpio final value/state: |
| 64 | * 0: pull down |
| 65 | * 1: pull up |
| 66 | * 2: floating |
| 67 | */ |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 68 | printk(BIOS_DEBUG, "Reading tristate GPIOs: "); |
Daisuke Nojiri | 573e211 | 2014-08-08 15:38:52 -0700 | [diff] [blame] | 69 | for (index = num_gpio - 1; index >= 0; --index) { |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 70 | temp = gpio_get(gpio[index]); |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 71 | temp |= ((value[index] ^ temp) << 1); |
| 72 | printk(BIOS_DEBUG, "%c ", tristate_char[temp]); |
| 73 | id = (id * 3) + temp; |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 74 | } |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame^] | 75 | printk(BIOS_DEBUG, "= %d\n", id); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 76 | |
| 77 | /* Disable pull up / pull down to conserve power */ |
| 78 | for (index = 0; index < num_gpio; ++index) |
| 79 | gpio_input(gpio[index]); |
| 80 | |
| 81 | return id; |
| 82 | } |