blob: 5147cfebaceba79777ea08464848896823446ce7 [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.
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070014 */
15
Julius Werner8d8799a2014-12-19 16:11:14 -080016#include <assert.h>
Julius Werner886d29b2014-09-24 15:40:49 -070017#include <base3.h>
18#include <console/console.h>
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070019#include <delay.h>
Julius Wernereaa9c452014-09-24 15:40:49 -070020#include <gpio.h>
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070021
David Hendricks21db62e2014-11-06 15:05:35 -080022int gpio_base2_value(gpio_t gpio[], int num_gpio)
23{
24 int i, result = 0;
25
David Hendricksf9b49e82015-01-14 20:41:30 -080026 for (i = 0; i < num_gpio; i++)
David Hendricks21db62e2014-11-06 15:05:35 -080027 gpio_input(gpio[i]);
David Hendricksf9b49e82015-01-14 20:41:30 -080028
29 /* Wait until signals become stable */
30 udelay(10);
31
32 for (i = 0; i < num_gpio; i++)
David Hendricks21db62e2014-11-06 15:05:35 -080033 result |= gpio_get(gpio[i]) << i;
David Hendricks21db62e2014-11-06 15:05:35 -080034
35 return result;
36}
37
David Hendricks3fc63682014-11-06 15:09:27 -080038int gpio_base3_value(gpio_t gpio[], int num_gpio)
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070039{
40 /*
41 * GPIOs which are tied to stronger external pull up or pull down
42 * will stay there regardless of the internal pull up or pull
43 * down setting.
44 *
45 * GPIOs which are floating will go to whatever level they're
46 * internally pulled to.
47 */
48
Julius Werner886d29b2014-09-24 15:40:49 -070049 static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'};
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070050 int temp;
51 int index;
David Hendricks3fc63682014-11-06 15:09:27 -080052 int result = 0;
Julius Werner8d8799a2014-12-19 16:11:14 -080053 char value[32];
54 assert(num_gpio <= 32);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070055
56 /* Enable internal pull up */
57 for (index = 0; index < num_gpio; ++index)
58 gpio_input_pullup(gpio[index]);
59
60 /* Wait until signals become stable */
61 udelay(10);
62
63 /* Get gpio values at internal pull up */
64 for (index = 0; index < num_gpio; ++index)
Julius Wernereaa9c452014-09-24 15:40:49 -070065 value[index] = gpio_get(gpio[index]);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070066
67 /* Enable internal pull down */
68 for (index = 0; index < num_gpio; ++index)
69 gpio_input_pulldown(gpio[index]);
70
71 /* Wait until signals become stable */
72 udelay(10);
73
74 /*
75 * Get gpio values at internal pull down.
76 * Compare with gpio pull up value and then
77 * determine a gpio final value/state:
78 * 0: pull down
79 * 1: pull up
80 * 2: floating
81 */
Julius Werner886d29b2014-09-24 15:40:49 -070082 printk(BIOS_DEBUG, "Reading tristate GPIOs: ");
Daisuke Nojiri573e2112014-08-08 15:38:52 -070083 for (index = num_gpio - 1; index >= 0; --index) {
Julius Wernereaa9c452014-09-24 15:40:49 -070084 temp = gpio_get(gpio[index]);
Julius Werner886d29b2014-09-24 15:40:49 -070085 temp |= ((value[index] ^ temp) << 1);
86 printk(BIOS_DEBUG, "%c ", tristate_char[temp]);
David Hendricks3fc63682014-11-06 15:09:27 -080087 result = (result * 3) + temp;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070088 }
David Hendricks3fc63682014-11-06 15:09:27 -080089 printk(BIOS_DEBUG, "= %d\n", result);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070090
91 /* Disable pull up / pull down to conserve power */
92 for (index = 0; index < num_gpio; ++index)
93 gpio_input(gpio[index]);
94
David Hendricks3fc63682014-11-06 15:09:27 -080095 return result;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070096}