Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 3 | |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 4 | #include <base3.h> |
| 5 | #include <console/console.h> |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 6 | #include <delay.h> |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 7 | #include <gpio.h> |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 8 | |
Richard Spiegel | 7160766e | 2018-09-03 08:55:51 -0700 | [diff] [blame] | 9 | static void _check_num(const char *name, int num) |
| 10 | { |
| 11 | if ((num > 31) || (num < 1)) { |
| 12 | printk(BIOS_EMERG, "%s: %d ", name, num); |
| 13 | die("is an invalid number of GPIOs"); |
| 14 | } |
| 15 | } |
| 16 | |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 17 | static uint32_t _gpio_base2_value(const gpio_t gpio[], int num_gpio) |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 18 | { |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 19 | uint32_t result = 0; |
| 20 | int i; |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 21 | |
David Hendricks | f9b49e8 | 2015-01-14 20:41:30 -0800 | [diff] [blame] | 22 | /* Wait until signals become stable */ |
| 23 | udelay(10); |
| 24 | |
| 25 | for (i = 0; i < num_gpio; i++) |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 26 | result |= gpio_get(gpio[i]) << i; |
David Hendricks | 21db62e | 2014-11-06 15:05:35 -0800 | [diff] [blame] | 27 | |
| 28 | return result; |
| 29 | } |
| 30 | |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 31 | uint32_t gpio_base2_value(const gpio_t gpio[], int num_gpio) |
Aaron Durbin | f41ac22 | 2016-07-06 22:37:10 -0500 | [diff] [blame] | 32 | { |
| 33 | int i; |
| 34 | |
Richard Spiegel | 7160766e | 2018-09-03 08:55:51 -0700 | [diff] [blame] | 35 | _check_num(__func__, num_gpio); |
Aaron Durbin | f41ac22 | 2016-07-06 22:37:10 -0500 | [diff] [blame] | 36 | for (i = 0; i < num_gpio; i++) |
| 37 | gpio_input(gpio[i]); |
| 38 | |
| 39 | return _gpio_base2_value(gpio, num_gpio); |
| 40 | } |
| 41 | |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 42 | uint32_t gpio_pulldown_base2_value(const gpio_t gpio[], int num_gpio) |
Aaron Durbin | f41ac22 | 2016-07-06 22:37:10 -0500 | [diff] [blame] | 43 | { |
| 44 | int i; |
| 45 | |
Richard Spiegel | 7160766e | 2018-09-03 08:55:51 -0700 | [diff] [blame] | 46 | _check_num(__func__, num_gpio); |
Aaron Durbin | f41ac22 | 2016-07-06 22:37:10 -0500 | [diff] [blame] | 47 | for (i = 0; i < num_gpio; i++) |
| 48 | gpio_input_pulldown(gpio[i]); |
| 49 | |
| 50 | return _gpio_base2_value(gpio, num_gpio); |
| 51 | } |
| 52 | |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 53 | uint32_t gpio_pullup_base2_value(const gpio_t gpio[], int num_gpio) |
Aaron Durbin | f41ac22 | 2016-07-06 22:37:10 -0500 | [diff] [blame] | 54 | { |
| 55 | int i; |
| 56 | |
Richard Spiegel | 7160766e | 2018-09-03 08:55:51 -0700 | [diff] [blame] | 57 | _check_num(__func__, num_gpio); |
Aaron Durbin | f41ac22 | 2016-07-06 22:37:10 -0500 | [diff] [blame] | 58 | for (i = 0; i < num_gpio; i++) |
| 59 | gpio_input_pullup(gpio[i]); |
| 60 | |
| 61 | return _gpio_base2_value(gpio, num_gpio); |
| 62 | } |
| 63 | |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 64 | uint32_t _gpio_base3_value(const gpio_t gpio[], int num_gpio, int binary_first) |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 65 | { |
| 66 | /* |
| 67 | * GPIOs which are tied to stronger external pull up or pull down |
| 68 | * will stay there regardless of the internal pull up or pull |
| 69 | * down setting. |
| 70 | * |
| 71 | * GPIOs which are floating will go to whatever level they're |
| 72 | * internally pulled to. |
| 73 | */ |
| 74 | |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 75 | static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'}; |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 76 | uint32_t result = 0; |
Julius Werner | 2b23948 | 2016-03-16 17:11:55 -0700 | [diff] [blame] | 77 | int has_z = 0; |
| 78 | int binary_below = 0; |
Julius Werner | 0a0340e | 2018-08-02 11:45:07 -0700 | [diff] [blame] | 79 | int index; |
| 80 | int temp; |
Julius Werner | 8d8799a | 2014-12-19 16:11:14 -0800 | [diff] [blame] | 81 | char value[32]; |
Richard Spiegel | 7160766e | 2018-09-03 08:55:51 -0700 | [diff] [blame] | 82 | |
| 83 | _check_num(__func__, num_gpio); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 84 | |
| 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 Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 94 | value[index] = gpio_get(gpio[index]); |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 95 | |
| 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 Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 111 | printk(BIOS_DEBUG, "Reading tristate GPIOs: "); |
Daisuke Nojiri | 573e211 | 2014-08-08 15:38:52 -0700 | [diff] [blame] | 112 | for (index = num_gpio - 1; index >= 0; --index) { |
Julius Werner | eaa9c45 | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 113 | temp = gpio_get(gpio[index]); |
Julius Werner | 886d29b | 2014-09-24 15:40:49 -0700 | [diff] [blame] | 114 | temp |= ((value[index] ^ temp) << 1); |
| 115 | printk(BIOS_DEBUG, "%c ", tristate_char[temp]); |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 116 | result = (result * 3) + temp; |
Julius Werner | 2b23948 | 2016-03-16 17:11:55 -0700 | [diff] [blame] | 117 | |
| 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 Leahy | 45fde70 | 2017-03-08 18:02:24 -0800 | [diff] [blame] | 140 | switch (temp) { |
Julius Werner | 2b23948 | 2016-03-16 17:11:55 -0700 | [diff] [blame] | 141 | 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 Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 151 | } |
Julius Werner | 2b23948 | 2016-03-16 17:11:55 -0700 | [diff] [blame] | 152 | |
| 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 Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 162 | |
| 163 | /* Disable pull up / pull down to conserve power */ |
| 164 | for (index = 0; index < num_gpio; ++index) |
| 165 | gpio_input(gpio[index]); |
| 166 | |
David Hendricks | 3fc6368 | 2014-11-06 15:09:27 -0800 | [diff] [blame] | 167 | return result; |
Vadim Bendebury | 9c9c336 | 2014-07-23 09:40:02 -0700 | [diff] [blame] | 168 | } |
Duncan Laurie | 3a39f44 | 2016-05-09 16:09:25 -0700 | [diff] [blame] | 169 | |
| 170 | /* Default handler for ACPI path is to return NULL */ |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 171 | __weak const char *gpio_acpi_path(gpio_t gpio) |
Duncan Laurie | 3a39f44 | 2016-05-09 16:09:25 -0700 | [diff] [blame] | 172 | { |
| 173 | return NULL; |
| 174 | } |
Duncan Laurie | 5b6c28c | 2016-06-29 10:47:22 -0700 | [diff] [blame] | 175 | |
| 176 | /* Default handler returns 0 because type of gpio_t is unknown */ |
Aaron Durbin | 6403167 | 2018-04-21 14:45:32 -0600 | [diff] [blame] | 177 | __weak uint16_t gpio_acpi_pin(gpio_t gpio) |
Duncan Laurie | 5b6c28c | 2016-06-29 10:47:22 -0700 | [diff] [blame] | 178 | { |
| 179 | return 0; |
| 180 | } |