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 |
Patrick Georgi | b890a12 | 2015-03-26 15:17:45 +0100 | [diff] [blame] | 17 | * Foundation, Inc. |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 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 | |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 25 | int gpio_base2_value(gpio_t gpio[], int num_gpio) |
| 26 | { |
| 27 | int i, result = 0; |
| 28 | |
David Hendricks | f9b49e8 | 2015-01-14 20:41:30 -0800 | [diff] [blame] | 29 | for (i = 0; i < num_gpio; i++) |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 30 | gpio_input(gpio[i]); |
David Hendricks | f9b49e8 | 2015-01-14 20:41:30 -0800 | [diff] [blame] | 31 | |
| 32 | /* Wait until signals become stable */ |
| 33 | udelay(10); |
| 34 | |
| 35 | for (i = 0; i < num_gpio; i++) |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 36 | result |= gpio_get(gpio[i]) << i; |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 37 | |
| 38 | return result; |
| 39 | } |
| 40 | |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 41 | int gpio_base3_value(gpio_t gpio[], int num_gpio) |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 42 | { |
| 43 | /* |
| 44 | * GPIOs which are tied to stronger external pull up or pull down |
| 45 | * will stay there regardless of the internal pull up or pull |
| 46 | * down setting. |
| 47 | * |
| 48 | * GPIOs which are floating will go to whatever level they're |
| 49 | * internally pulled to. |
| 50 | */ |
| 51 | |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 52 | static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'}; |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 53 | int temp; |
| 54 | int index; |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 55 | int result = 0; |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 56 | char value[num_gpio]; |
| 57 | |
| 58 | /* Enable internal pull up */ |
| 59 | for (index = 0; index < num_gpio; ++index) |
| 60 | gpio_input_pullup(gpio[index]); |
| 61 | |
| 62 | /* Wait until signals become stable */ |
| 63 | udelay(10); |
| 64 | |
| 65 | /* Get gpio values at internal pull up */ |
| 66 | for (index = 0; index < num_gpio; ++index) |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 67 | value[index] = gpio_get(gpio[index]); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 68 | |
| 69 | /* Enable internal pull down */ |
| 70 | for (index = 0; index < num_gpio; ++index) |
| 71 | gpio_input_pulldown(gpio[index]); |
| 72 | |
| 73 | /* Wait until signals become stable */ |
| 74 | udelay(10); |
| 75 | |
| 76 | /* |
| 77 | * Get gpio values at internal pull down. |
| 78 | * Compare with gpio pull up value and then |
| 79 | * determine a gpio final value/state: |
| 80 | * 0: pull down |
| 81 | * 1: pull up |
| 82 | * 2: floating |
| 83 | */ |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 84 | printk(BIOS_DEBUG, "Reading tristate GPIOs: "); |
Daisuke Nojiri | 573e211 | 2014-08-08 15:38:52 -0700 | [diff] [blame] | 85 | for (index = num_gpio - 1; index >= 0; --index) { |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 86 | temp = gpio_get(gpio[index]); |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 87 | temp |= ((value[index] ^ temp) << 1); |
| 88 | printk(BIOS_DEBUG, "%c ", tristate_char[temp]); |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 89 | result = (result * 3) + temp; |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 90 | } |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 91 | printk(BIOS_DEBUG, "= %d\n", result); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 92 | |
| 93 | /* Disable pull up / pull down to conserve power */ |
| 94 | for (index = 0; index < num_gpio; ++index) |
| 95 | gpio_input(gpio[index]); |
| 96 | |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 97 | return result; |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 98 | } |