blob: 0875538bae26f7faced0910432faf162ddd74032 [file] [log] [blame]
Vadim Bendebury9c9c3362014-07-23 09:40:02 -07001/*
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 Werner886d29b2014-09-24 15:40:49 -070020#include <base3.h>
21#include <console/console.h>
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070022#include <delay.h>
Julius Wernereaa9c452014-09-24 15:40:49 -070023#include <gpio.h>
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070024
David Hendricks21db62e2014-11-06 15:05:35 -080025int gpio_base2_value(gpio_t gpio[], int num_gpio)
26{
27 int i, result = 0;
28
29 for (i = 0; i < num_gpio; i++) {
30 gpio_input(gpio[i]);
31 result |= gpio_get(gpio[i]) << i;
32 }
33
34 return result;
35}
36
David Hendricks3fc63682014-11-06 15:09:27 -080037int gpio_base3_value(gpio_t gpio[], int num_gpio)
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070038{
39 /*
40 * GPIOs which are tied to stronger external pull up or pull down
41 * will stay there regardless of the internal pull up or pull
42 * down setting.
43 *
44 * GPIOs which are floating will go to whatever level they're
45 * internally pulled to.
46 */
47
Julius Werner886d29b2014-09-24 15:40:49 -070048 static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'};
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070049 int temp;
50 int index;
David Hendricks3fc63682014-11-06 15:09:27 -080051 int result = 0;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070052 char value[num_gpio];
53
54 /* Enable internal pull up */
55 for (index = 0; index < num_gpio; ++index)
56 gpio_input_pullup(gpio[index]);
57
58 /* Wait until signals become stable */
59 udelay(10);
60
61 /* Get gpio values at internal pull up */
62 for (index = 0; index < num_gpio; ++index)
Julius Wernereaa9c452014-09-24 15:40:49 -070063 value[index] = gpio_get(gpio[index]);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070064
65 /* Enable internal pull down */
66 for (index = 0; index < num_gpio; ++index)
67 gpio_input_pulldown(gpio[index]);
68
69 /* Wait until signals become stable */
70 udelay(10);
71
72 /*
73 * Get gpio values at internal pull down.
74 * Compare with gpio pull up value and then
75 * determine a gpio final value/state:
76 * 0: pull down
77 * 1: pull up
78 * 2: floating
79 */
Julius Werner886d29b2014-09-24 15:40:49 -070080 printk(BIOS_DEBUG, "Reading tristate GPIOs: ");
Daisuke Nojiri573e2112014-08-08 15:38:52 -070081 for (index = num_gpio - 1; index >= 0; --index) {
Julius Wernereaa9c452014-09-24 15:40:49 -070082 temp = gpio_get(gpio[index]);
Julius Werner886d29b2014-09-24 15:40:49 -070083 temp |= ((value[index] ^ temp) << 1);
84 printk(BIOS_DEBUG, "%c ", tristate_char[temp]);
David Hendricks3fc63682014-11-06 15:09:27 -080085 result = (result * 3) + temp;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070086 }
David Hendricks3fc63682014-11-06 15:09:27 -080087 printk(BIOS_DEBUG, "= %d\n", result);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070088
89 /* Disable pull up / pull down to conserve power */
90 for (index = 0; index < num_gpio; ++index)
91 gpio_input(gpio[index]);
92
David Hendricks3fc63682014-11-06 15:09:27 -080093 return result;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070094}