blob: 72bd7eccfd592074c717f5e67f40117c6ae13f00 [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
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070018 */
19
Julius Werner8d8799a2014-12-19 16:11:14 -080020#include <assert.h>
Julius Werner886d29b2014-09-24 15:40:49 -070021#include <base3.h>
22#include <console/console.h>
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070023#include <delay.h>
Julius Wernereaa9c452014-09-24 15:40:49 -070024#include <gpio.h>
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070025
David Hendricks21db62e2014-11-06 15:05:35 -080026int gpio_base2_value(gpio_t gpio[], int num_gpio)
27{
28 int i, result = 0;
29
David Hendricksf9b49e82015-01-14 20:41:30 -080030 for (i = 0; i < num_gpio; i++)
David Hendricks21db62e2014-11-06 15:05:35 -080031 gpio_input(gpio[i]);
David Hendricksf9b49e82015-01-14 20:41:30 -080032
33 /* Wait until signals become stable */
34 udelay(10);
35
36 for (i = 0; i < num_gpio; i++)
David Hendricks21db62e2014-11-06 15:05:35 -080037 result |= gpio_get(gpio[i]) << i;
David Hendricks21db62e2014-11-06 15:05:35 -080038
39 return result;
40}
41
David Hendricks3fc63682014-11-06 15:09:27 -080042int gpio_base3_value(gpio_t gpio[], int num_gpio)
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070043{
44 /*
45 * GPIOs which are tied to stronger external pull up or pull down
46 * will stay there regardless of the internal pull up or pull
47 * down setting.
48 *
49 * GPIOs which are floating will go to whatever level they're
50 * internally pulled to.
51 */
52
Julius Werner886d29b2014-09-24 15:40:49 -070053 static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'};
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070054 int temp;
55 int index;
David Hendricks3fc63682014-11-06 15:09:27 -080056 int result = 0;
Julius Werner8d8799a2014-12-19 16:11:14 -080057 char value[32];
58 assert(num_gpio <= 32);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070059
60 /* Enable internal pull up */
61 for (index = 0; index < num_gpio; ++index)
62 gpio_input_pullup(gpio[index]);
63
64 /* Wait until signals become stable */
65 udelay(10);
66
67 /* Get gpio values at internal pull up */
68 for (index = 0; index < num_gpio; ++index)
Julius Wernereaa9c452014-09-24 15:40:49 -070069 value[index] = gpio_get(gpio[index]);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070070
71 /* Enable internal pull down */
72 for (index = 0; index < num_gpio; ++index)
73 gpio_input_pulldown(gpio[index]);
74
75 /* Wait until signals become stable */
76 udelay(10);
77
78 /*
79 * Get gpio values at internal pull down.
80 * Compare with gpio pull up value and then
81 * determine a gpio final value/state:
82 * 0: pull down
83 * 1: pull up
84 * 2: floating
85 */
Julius Werner886d29b2014-09-24 15:40:49 -070086 printk(BIOS_DEBUG, "Reading tristate GPIOs: ");
Daisuke Nojiri573e2112014-08-08 15:38:52 -070087 for (index = num_gpio - 1; index >= 0; --index) {
Julius Wernereaa9c452014-09-24 15:40:49 -070088 temp = gpio_get(gpio[index]);
Julius Werner886d29b2014-09-24 15:40:49 -070089 temp |= ((value[index] ^ temp) << 1);
90 printk(BIOS_DEBUG, "%c ", tristate_char[temp]);
David Hendricks3fc63682014-11-06 15:09:27 -080091 result = (result * 3) + temp;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070092 }
David Hendricks3fc63682014-11-06 15:09:27 -080093 printk(BIOS_DEBUG, "= %d\n", result);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070094
95 /* Disable pull up / pull down to conserve power */
96 for (index = 0; index < num_gpio; ++index)
97 gpio_input(gpio[index]);
98
David Hendricks3fc63682014-11-06 15:09:27 -080099 return result;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -0700100}