David Hendricks (dhendrix@chromium.org) just uploaded a new patch set to gerrit, which you can find at https://review.coreboot.org/14143
-gerrit
commit a759a4e137679da3a11587e3334b98443436d73d Author: David Hendricks dhendrix@chromium.org Date: Fri Mar 18 20:23:05 2016 -0700
gpio: Add support for extended binary number system
This adds yet another number system which logically extends the binary representation to allow floating/high-Z values to count in the result. A 'Z' value at position N will yield a value of 0 at position N and a value of 1 at position (1 << N << num_gpio). For example: 000Z => 0b1_0000 100Z => 0b1_1000
BRANCH=none BUG=none TEST=stubbed out and tested for a 3-wide raw value, see comments in gerrit for the full table.
Change-Id: I121e0cac9fa84fcbb261d1d84dcb20cfd49e5518 Signed-off-by: David Hendricks dhendrix@chromium.org --- src/include/gpio.h | 13 +++++++++++++ src/lib/gpio.c | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+)
diff --git a/src/include/gpio.h b/src/include/gpio.h index ea03a23..3b22154 100644 --- a/src/include/gpio.h +++ b/src/include/gpio.h @@ -41,6 +41,19 @@ int gpio_base2_value(gpio_t gpio[], int num_gpio);
/* * Read the value presented by the set of GPIOs, when each pin is interpreted + * as a base-2 digit (LOW = 0, HIGH = 1). If a pin is in its Z/floating state + * the value becomes (1 << pin_num << num_gpio). + * + * Caveat: This scheme makes the number space discontinuous. The advantage is + * that it's simple to compute and translate to/from hex. + * + * gpio[]: pin positions to read. gpio[0] is less significant than gpio[1]. + * num_gpio: number of pins to read. + */ +int gpio_base2_ext_value(gpio_t gpio[], int num_gpio); + +/* + * Read the value presented by the set of GPIOs, when each pin is interpreted * as a base-3 digit (LOW = 0, HIGH = 1, Z/floating = 2). * Example: X1 = Z, X2 = 1 -> gpio_base3_value({GPIO(X1), GPIO(X2)}) = 5 * BASE3() from <base3.h> can generate numbers to compare the result to. diff --git a/src/lib/gpio.c b/src/lib/gpio.c index 5147cfe..cfe1786 100644 --- a/src/lib/gpio.c +++ b/src/lib/gpio.c @@ -35,6 +35,25 @@ int gpio_base2_value(gpio_t gpio[], int num_gpio) return result; }
+int gpio_base2_ext_value(gpio_t gpio[], int num_gpio) +{ + static const char tristate_char[] = {[0] = '0', [1] = '1', [2] = 'Z'}; + int temp, index, result = 0; + + for (index = num_gpio - 1; index >= 0; --index) { + temp = gpio_get(gpio[index]); + printk(BIOS_DEBUG, "%c ", tristate_char[temp]); + + if (temp == 2) /* Z-state */ + result += (1 << index << num_gpio); + else + result += temp * (1 << index); + } + + printk(BIOS_DEBUG, "= 0x%x (extended binary number system)\n", result); + return result; +} + int gpio_base3_value(gpio_t gpio[], int num_gpio) { /*