| 1 |
|
| 2 |
|
| 3 |
#include <base3.h> |
| 4 |
#include <console/console.h> |
| 5 |
#include <delay.h> |
| 6 |
#include <gpio.h> |
| 7 |
|
| 8 |
static void _check_num(const char *name, int num) |
| 9 |
{ |
| 10 |
if ((num > 31) || (num < 1)) { |
| 11 |
printk(BIOS_EMERG, "%s: %d ", name, num); |
| 12 |
die("is an invalid number of GPIOs"); |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
static uint32_t _gpio_base2_value(const gpio_t gpio[], int num_gpio) |
| 17 |
{ |
| 18 |
uint32_t result = 0; |
| 19 |
int i; |
| 20 |
|
| 21 |
|
| 22 |
udelay(10); |
| 23 |
|
| 24 |
for (i = 0; i < num_gpio; i++) |
| 25 |
result |= gpio_get(gpio[i]) << i; |
| 26 |
|
| 27 |
return result; |
| 28 |
} |
| 29 |
|
| 30 |
uint32_t gpio_base2_value(const gpio_t gpio[], int num_gpio) |
| 31 |
{ |
| 32 |
int i; |
| 33 |
|
| 34 |
_check_num(__func__, num_gpio); |
| 35 |
for (i = 0; i < num_gpio; i++) |
| 36 |
gpio_input(gpio[i]); |
| 37 |
|
| 38 |
return _gpio_base2_value(gpio, num_gpio); |
| 39 |
} |
| 40 |
|
| 41 |
uint32_t gpio_pulldown_base2_value(const gpio_t gpio[], int num_gpio) |
| 42 |
{ |
| 43 |
int i; |
| 44 |
|
| 45 |
_check_num(__func__, num_gpio); |
| 46 |
for (i = 0; i < num_gpio; i++) |
| 47 |
gpio_input_pulldown(gpio[i]); |
| 48 |
|
| 49 |
return _gpio_base2_value(gpio, num_gpio); |
| 50 |
} |
| 51 |
|
| 52 |
uint32_t gpio_pullup_base2_value(const gpio_t gpio[], int num_gpio) |
| 53 |
{ |
| 54 |
int i; |
| 55 |
|
| 56 |
_check_num(__func__, num_gpio); |
| 57 |
for (i = 0; i < num_gpio; i++) |
| 58 |
gpio_input_pullup(gpio[i]); |
| 59 |
|
| 60 |
return _gpio_base2_value(gpio, num_gpio); |
| 61 |
} |
| 62 |
|
| 63 |
uint32_t _gpio_base3_value(const gpio_t gpio[], int num_gpio, int binary_first) |
| 64 |
{ |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
static const char tristate_char[] = {[0] = '0', [1] = '1', [Z] = 'Z'}; |
| 75 |
uint32_t result = 0; |
| 76 |
int has_z = 0; |
| 77 |
int binary_below = 0; |
| 78 |
int index; |
| 79 |
int temp; |
| 80 |
char value[32]; |
| 81 |
|
| 82 |
_check_num(__func__, num_gpio); |
| 83 |
|
| 84 |
|
| 85 |
for (index = 0; index < num_gpio; ++index) |
| 86 |
gpio_input_pullup(gpio[index]); |
| 87 |
|
| 88 |
|
| 89 |
udelay(10); |
| 90 |
|
| 91 |
|
| 92 |
for (index = 0; index < num_gpio; ++index) |
| 93 |
value[index] = gpio_get(gpio[index]); |
| 94 |
|
| 95 |
|
| 96 |
for (index = 0; index < num_gpio; ++index) |
| 97 |
gpio_input_pulldown(gpio[index]); |
| 98 |
|
| 99 |
|
| 100 |
udelay(10); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
printk(BIOS_DEBUG, "Reading tristate GPIOs: "); |
| 111 |
for (index = num_gpio - 1; index >= 0; --index) { |
| 112 |
temp = gpio_get(gpio[index]); |
| 113 |
temp |= ((value[index] ^ temp) << 1); |
| 114 |
printk(BIOS_DEBUG, "%c ", tristate_char[temp]); |
| 115 |
result = (result * 3) + temp; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
if (temp != Z) |
| 121 |
gpio_input(gpio[index]); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if (binary_first && !has_z) { |
| 145 |
switch (temp) { |
| 146 |
case 0: |
| 147 |
break; |
| 148 |
case 1: |
| 149 |
binary_below += 1 << index; |
| 150 |
break; |
| 151 |
case 2: |
| 152 |
binary_below += 1 << (index + 1); |
| 153 |
has_z = 1; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
if (binary_first) { |
| 159 |
if (has_z) |
| 160 |
result = result + (1 << num_gpio) - binary_below; |
| 161 |
else |
| 162 |
result = binary_below; |
| 163 |
} |
| 164 |
|
| 165 |
printk(BIOS_DEBUG, "= %d (%s base3 number system)\n", result, |
| 166 |
binary_first ? "binary_first" : "standard"); |
| 167 |
|
| 168 |
return result; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
__weak const char *gpio_acpi_path(gpio_t gpio) |
| 173 |
{ |
| 174 |
return NULL; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
__weak uint16_t gpio_acpi_pin(gpio_t gpio) |
| 179 |
{ |
| 180 |
return 0; |
| 181 |
} |