blob: ca0aebedf5fa49c54cfb3f762f65ca4229bd6c73 [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
Aaron Durbinf41ac222016-07-06 22:37:10 -050022static int _gpio_base2_value(gpio_t gpio[], int num_gpio)
David Hendricks21db62e2014-11-06 15:05:35 -080023{
24 int i, result = 0;
25
David Hendricksf9b49e82015-01-14 20:41:30 -080026 /* Wait until signals become stable */
27 udelay(10);
28
29 for (i = 0; i < num_gpio; i++)
David Hendricks21db62e2014-11-06 15:05:35 -080030 result |= gpio_get(gpio[i]) << i;
David Hendricks21db62e2014-11-06 15:05:35 -080031
32 return result;
33}
34
Aaron Durbinf41ac222016-07-06 22:37:10 -050035int gpio_base2_value(gpio_t gpio[], int num_gpio)
36{
37 int i;
38
39 for (i = 0; i < num_gpio; i++)
40 gpio_input(gpio[i]);
41
42 return _gpio_base2_value(gpio, num_gpio);
43}
44
45int gpio_pulldown_base2_value(gpio_t gpio[], int num_gpio)
46{
47 int i;
48
49 for (i = 0; i < num_gpio; i++)
50 gpio_input_pulldown(gpio[i]);
51
52 return _gpio_base2_value(gpio, num_gpio);
53}
54
55int gpio_pullup_base2_value(gpio_t gpio[], int num_gpio)
56{
57 int i;
58
59 for (i = 0; i < num_gpio; i++)
60 gpio_input_pullup(gpio[i]);
61
62 return _gpio_base2_value(gpio, num_gpio);
63}
64
Julius Werner2b239482016-03-16 17:11:55 -070065int _gpio_base3_value(gpio_t gpio[], int num_gpio, int binary_first)
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070066{
67 /*
68 * GPIOs which are tied to stronger external pull up or pull down
69 * will stay there regardless of the internal pull up or pull
70 * down setting.
71 *
72 * GPIOs which are floating will go to whatever level they're
73 * internally pulled to.
74 */
75
Julius Werner886d29b2014-09-24 15:40:49 -070076 static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'};
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070077 int temp;
78 int index;
David Hendricks3fc63682014-11-06 15:09:27 -080079 int result = 0;
Julius Werner2b239482016-03-16 17:11:55 -070080 int has_z = 0;
81 int binary_below = 0;
Julius Werner8d8799a2014-12-19 16:11:14 -080082 char value[32];
83 assert(num_gpio <= 32);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070084
85 /* Enable internal pull up */
86 for (index = 0; index < num_gpio; ++index)
87 gpio_input_pullup(gpio[index]);
88
89 /* Wait until signals become stable */
90 udelay(10);
91
92 /* Get gpio values at internal pull up */
93 for (index = 0; index < num_gpio; ++index)
Julius Wernereaa9c452014-09-24 15:40:49 -070094 value[index] = gpio_get(gpio[index]);
Vadim Bendebury9c9c3362014-07-23 09:40:02 -070095
96 /* Enable internal pull down */
97 for (index = 0; index < num_gpio; ++index)
98 gpio_input_pulldown(gpio[index]);
99
100 /* Wait until signals become stable */
101 udelay(10);
102
103 /*
104 * Get gpio values at internal pull down.
105 * Compare with gpio pull up value and then
106 * determine a gpio final value/state:
107 * 0: pull down
108 * 1: pull up
109 * 2: floating
110 */
Julius Werner886d29b2014-09-24 15:40:49 -0700111 printk(BIOS_DEBUG, "Reading tristate GPIOs: ");
Daisuke Nojiri573e2112014-08-08 15:38:52 -0700112 for (index = num_gpio - 1; index >= 0; --index) {
Julius Wernereaa9c452014-09-24 15:40:49 -0700113 temp = gpio_get(gpio[index]);
Julius Werner886d29b2014-09-24 15:40:49 -0700114 temp |= ((value[index] ^ temp) << 1);
115 printk(BIOS_DEBUG, "%c ", tristate_char[temp]);
David Hendricks3fc63682014-11-06 15:09:27 -0800116 result = (result * 3) + temp;
Julius Werner2b239482016-03-16 17:11:55 -0700117
118 /*
119 * For binary_first we keep track of the normal ternary result
120 * and whether we found any pin that was a Z. We also determine
121 * the amount of numbers that can be represented with only
122 * binary digits (no Z) whose value in the normal ternary system
123 * is lower than the one we are parsing. Counting from the left,
124 * we add 2^i for any '1' digit to account for the binary
125 * numbers whose values would be below it if all following
126 * digits we parsed would be '0'. As soon as we find a '2' digit
127 * we can total the remaining binary numbers below as 2^(i+1)
128 * because we know that all binary representations counting only
129 * this and following digits must have values below our number
130 * (since 1xxx is always smaller than 2xxx).
131 *
132 * Example: 1 0 2 1 (counting from the left / most significant)
133 * '1' at 3^3: Add 2^3 = 8 to account for binaries 0000-0111
134 * '0' at 3^2: Ignore (not all binaries 1000-1100 are below us)
135 * '2' at 3^1: Add 2^(1+1) = 4 to account for binaries 1000-1011
136 * Stop adding for lower digits (3^0), all already accounted
137 * now. We know that there can be no binary numbers 1020-102X.
138 */
139 if (binary_first && !has_z) {
Lee Leahy45fde702017-03-08 18:02:24 -0800140 switch (temp) {
Julius Werner2b239482016-03-16 17:11:55 -0700141 case 0: /* Ignore '0' digits. */
142 break;
143 case 1: /* Account for binaries 0 to 2^index - 1. */
144 binary_below += 1 << index;
145 break;
146 case 2: /* Account for binaries 0 to 2^(index+1) - 1. */
147 binary_below += 1 << (index + 1);
148 has_z = 1;
149 }
150 }
Vadim Bendebury9c9c3362014-07-23 09:40:02 -0700151 }
Julius Werner2b239482016-03-16 17:11:55 -0700152
153 if (binary_first) {
154 if (has_z)
155 result = result + (1 << num_gpio) - binary_below;
156 else /* binary_below is normal binary system value if !has_z. */
157 result = binary_below;
158 }
159
160 printk(BIOS_DEBUG, "= %d (%s base3 number system)\n", result,
161 binary_first ? "binary_first" : "standard");
Vadim Bendebury9c9c3362014-07-23 09:40:02 -0700162
163 /* Disable pull up / pull down to conserve power */
164 for (index = 0; index < num_gpio; ++index)
165 gpio_input(gpio[index]);
166
David Hendricks3fc63682014-11-06 15:09:27 -0800167 return result;
Vadim Bendebury9c9c3362014-07-23 09:40:02 -0700168}
Duncan Laurie3a39f442016-05-09 16:09:25 -0700169
170/* Default handler for ACPI path is to return NULL */
171__attribute__((weak)) const char *gpio_acpi_path(gpio_t gpio)
172{
173 return NULL;
174}
Duncan Laurie5b6c28c2016-06-29 10:47:22 -0700175
176/* Default handler returns 0 because type of gpio_t is unknown */
177__attribute__((weak)) uint16_t gpio_acpi_pin(gpio_t gpio)
178{
179 return 0;
180}