Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 1 | #include <stdint.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <unistd.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/mman.h> |
| 9 | #include <syslog.h> |
Brad Bishop | f6c8568 | 2016-06-27 11:56:39 -0400 | [diff] [blame] | 10 | #include <openbmc_intf.h> |
| 11 | #include <openbmc.h> |
| 12 | #include <gpio.h> |
Lei YU | 45cb4fc | 2016-11-29 01:48:26 +0800 | [diff] [blame] | 13 | #include <gpio_configs.h> |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 14 | |
| 15 | /* ------------------------------------------------------------------------- */ |
| 16 | static const gchar* dbus_object_path = "/org/openbmc/control"; |
| 17 | static const gchar* instance_name = "power0"; |
| 18 | static const gchar* dbus_name = "org.openbmc.control.Power"; |
| 19 | |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 20 | static int g_pci_reset_held = 1; |
| 21 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 22 | static GpioConfigs g_gpio_configs; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 23 | |
| 24 | static GDBusObjectManagerServer *manager = NULL; |
| 25 | |
| 26 | time_t pgood_timeout_start = 0; |
| 27 | |
| 28 | // TODO: Change to interrupt driven instead of polling |
| 29 | static gboolean |
| 30 | poll_pgood(gpointer user_data) |
| 31 | { |
| 32 | ControlPower *control_power = object_get_control_power((Object*)user_data); |
| 33 | Control* control = object_get_control((Object*)user_data); |
| 34 | |
| 35 | //send the heartbeat |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 36 | guint poll_int = control_get_poll_interval(control); |
| 37 | if(poll_int == 0) |
| 38 | { |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 39 | g_print("ERROR PowerControl: Poll interval cannot be 0\n"); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 40 | return FALSE; |
| 41 | } |
| 42 | //handle timeout |
| 43 | time_t current_time = time(NULL); |
| 44 | if(difftime(current_time,pgood_timeout_start) > control_power_get_pgood_timeout(control_power) |
| 45 | && pgood_timeout_start != 0) |
| 46 | { |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 47 | g_print("ERROR PowerControl: Pgood poll timeout\n"); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 48 | // set timeout to 0 so timeout doesn't happen again |
| 49 | control_power_set_pgood_timeout(control_power,0); |
| 50 | pgood_timeout_start = 0; |
Patrick Williams | 27eaf90 | 2017-03-21 15:46:40 -0500 | [diff] [blame] | 51 | return TRUE; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 52 | } |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 53 | uint8_t pgood_state; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 54 | |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 55 | int rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in, 0); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 56 | if(rc != GPIO_OK) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 57 | gpio_close(&g_gpio_configs.power_gpio.power_good_in); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 58 | g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n", |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 59 | g_gpio_configs.power_gpio.power_good_in.name, rc); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 60 | return FALSE; |
| 61 | } |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 62 | rc = gpio_read(&g_gpio_configs.power_gpio.power_good_in, &pgood_state); |
| 63 | gpio_close(&g_gpio_configs.power_gpio.power_good_in); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 64 | if(rc == GPIO_OK) |
| 65 | { |
| 66 | //if changed, set property and emit signal |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 67 | if(pgood_state != control_power_get_pgood(control_power)) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 68 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 69 | size_t i; |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 70 | uint8_t reset_state; |
| 71 | control_power_set_pgood(control_power, pgood_state); |
| 72 | if(pgood_state == 0) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 73 | { |
| 74 | control_power_emit_power_lost(control_power); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 75 | g_pci_reset_held = 1; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 76 | } |
| 77 | else |
| 78 | { |
| 79 | control_power_emit_power_good(control_power); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 80 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 81 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 82 | for(i = 0; i < g_gpio_configs.power_gpio.num_reset_outs; i++) |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 83 | { |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 84 | GPIO *reset_out = &g_gpio_configs.power_gpio.reset_outs[i]; |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 85 | reset_state = pgood_state ^ g_gpio_configs.power_gpio.reset_pols[i]; |
| 86 | rc = gpio_open(reset_out, reset_state); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 87 | if(rc != GPIO_OK) |
| 88 | { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 89 | gpio_close(reset_out); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 90 | g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n", |
| 91 | reset_out->name, rc); |
| 92 | continue; |
| 93 | } |
| 94 | |
Yi Li | 39df403 | 2016-12-16 16:06:50 +0800 | [diff] [blame] | 95 | g_print("PowerControl: pgood: %d, setting reset %s to %d\n", |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 96 | (int)pgood_state, reset_out->name, (int)reset_state); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 97 | gpio_write(reset_out, reset_state); |
| 98 | gpio_close(reset_out); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 99 | } |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 100 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 101 | for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++) |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 102 | { |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 103 | GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i]; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 104 | if(pgood_state == 1) |
| 105 | { |
| 106 | /* |
| 107 | * When powering on, hold PCI reset until |
| 108 | * the processor can forward clocks and control reset. |
| 109 | */ |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 110 | if(g_gpio_configs.power_gpio.pci_reset_holds[i]) |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 111 | { |
| 112 | g_print("Holding pci reset: %s\n", pci_reset_out->name); |
| 113 | continue; |
| 114 | } |
| 115 | } |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 116 | reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i]; |
| 117 | rc = gpio_open(pci_reset_out, reset_state); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 118 | if(rc != GPIO_OK) |
| 119 | { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 120 | gpio_close(pci_reset_out); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 121 | g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n", |
| 122 | pci_reset_out->name, rc); |
| 123 | continue; |
| 124 | } |
| 125 | |
Yi Li | 39df403 | 2016-12-16 16:06:50 +0800 | [diff] [blame] | 126 | g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n", |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 127 | (int)pgood_state, pci_reset_out->name, (int)reset_state); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 128 | gpio_write(pci_reset_out, reset_state); |
| 129 | gpio_close(pci_reset_out); |
| 130 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 131 | } |
| 132 | } else { |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 133 | g_print("ERROR PowerControl: GPIO read error (gpio=%s,rc=%d)\n", |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 134 | g_gpio_configs.power_gpio.power_good_in.name, rc); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 135 | //return false so poll won't get called anymore |
| 136 | return FALSE; |
| 137 | } |
| 138 | //pgood is not at desired state yet |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 139 | if(pgood_state != control_power_get_state(control_power) && |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 140 | control_power_get_pgood_timeout(control_power) > 0) |
| 141 | { |
| 142 | if(pgood_timeout_start == 0 ) { |
| 143 | pgood_timeout_start = current_time; |
| 144 | } |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | pgood_timeout_start = 0; |
| 149 | } |
| 150 | return TRUE; |
| 151 | } |
| 152 | |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 153 | /* Handler for BootProgress signal from BootProgress sensor */ |
| 154 | static void |
| 155 | on_boot_progress(GDBusConnection *connection, |
| 156 | const gchar *sender_name, |
| 157 | const gchar *object_path, |
| 158 | const gchar *interface_name, |
| 159 | const gchar *signal_name, |
| 160 | GVariant *parameters, |
| 161 | gpointer user_data) |
| 162 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 163 | (void) connection; |
| 164 | (void) sender_name; |
| 165 | (void) object_path; |
| 166 | (void) interface_name; |
| 167 | (void) signal_name; |
| 168 | (void) user_data; |
| 169 | |
Lei YU | 93b84e4 | 2017-10-11 15:06:20 +0800 | [diff] [blame] | 170 | gchar *interface; |
| 171 | GVariantIter *properties; |
| 172 | GVariantIter *dummy; |
| 173 | gchar *boot_progress = NULL; |
| 174 | gchar *property; |
| 175 | GVariant *value; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 176 | uint8_t pgood_state; |
| 177 | uint8_t reset_state; |
| 178 | int rc; |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 179 | size_t i; |
Lei YU | 93b84e4 | 2017-10-11 15:06:20 +0800 | [diff] [blame] | 180 | int ignore; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 181 | |
| 182 | if(!parameters) |
| 183 | return; |
| 184 | |
| 185 | /* prevent release again */ |
| 186 | if(!g_pci_reset_held) |
| 187 | return; |
| 188 | |
Lei YU | 93b84e4 | 2017-10-11 15:06:20 +0800 | [diff] [blame] | 189 | g_variant_get(parameters, "(&sa{sv}as)", &interface, &properties, &dummy); |
| 190 | for(i = 0; g_variant_iter_next(properties, "{&sv}", &property, &value); i++) |
| 191 | { |
| 192 | if (strcmp(property, "BootProgress") == 0) |
| 193 | { |
| 194 | gchar* tmp; |
| 195 | g_variant_get(value, "&s", &tmp); |
| 196 | boot_progress = g_strdup(tmp); |
| 197 | g_print("BootProgress: %s\n", boot_progress); |
| 198 | g_variant_unref(value); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | g_variant_iter_free(properties); |
| 203 | g_variant_iter_free(dummy); |
| 204 | if (boot_progress == NULL) |
| 205 | return; |
| 206 | |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 207 | /* Release PCI reset when FW boot progress goes beyond 'Baseboard Init' */ |
Lei YU | 93b84e4 | 2017-10-11 15:06:20 +0800 | [diff] [blame] | 208 | ignore = strcmp(boot_progress, |
| 209 | "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit") == 0; |
| 210 | g_free(boot_progress); |
| 211 | |
| 212 | if (ignore) |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 213 | return; |
| 214 | |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 215 | rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in, 0); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 216 | if(rc != GPIO_OK) |
| 217 | { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 218 | gpio_close(&g_gpio_configs.power_gpio.power_good_in); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 219 | g_print("ERROR PowerControl: on_boot_progress(): GPIO open error (gpio=%s,rc=%d)\n", |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 220 | g_gpio_configs.power_gpio.power_good_in.name, rc); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 221 | return; |
| 222 | } |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 223 | rc = gpio_read(&g_gpio_configs.power_gpio.power_good_in, &pgood_state); |
| 224 | gpio_close(&g_gpio_configs.power_gpio.power_good_in); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 225 | if(rc != GPIO_OK || pgood_state != 1) |
| 226 | return; |
| 227 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 228 | for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++) |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 229 | { |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 230 | GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i]; |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 231 | |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 232 | if(!g_gpio_configs.power_gpio.pci_reset_holds[i]) |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 233 | continue; |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 234 | reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i]; |
| 235 | rc = gpio_open(pci_reset_out, reset_state); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 236 | if(rc != GPIO_OK) |
| 237 | { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 238 | gpio_close(pci_reset_out); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 239 | g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n", |
| 240 | pci_reset_out->name, rc); |
| 241 | continue; |
| 242 | } |
| 243 | |
Yi Li | 39df403 | 2016-12-16 16:06:50 +0800 | [diff] [blame] | 244 | g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n", |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 245 | (int)pgood_state, pci_reset_out->name, (int)reset_state); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 246 | gpio_write(pci_reset_out, reset_state); |
| 247 | gpio_close(pci_reset_out); |
Lei YU | 93b84e4 | 2017-10-11 15:06:20 +0800 | [diff] [blame] | 248 | g_print("Released pci reset: %s\n", pci_reset_out->name); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 249 | } |
| 250 | g_pci_reset_held = 0; |
| 251 | } |
| 252 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 253 | static gboolean |
| 254 | on_set_power_state(ControlPower *pwr, |
| 255 | GDBusMethodInvocation *invocation, |
| 256 | guint state, |
| 257 | gpointer user_data) |
| 258 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 259 | (void) user_data; |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 260 | PowerGpio *power_gpio = &g_gpio_configs.power_gpio; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 261 | if(state > 1) |
| 262 | { |
| 263 | g_dbus_method_invocation_return_dbus_error(invocation, |
| 264 | "org.openbmc.ControlPower.Error.Failed", |
| 265 | "Invalid power state"); |
| 266 | return TRUE; |
| 267 | } |
| 268 | // return from method call |
| 269 | control_power_complete_set_power_state(pwr,invocation); |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 270 | if(state == (guint) control_power_get_state(pwr)) |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 271 | { |
| 272 | g_print("Power already at requested state: %d\n",state); |
| 273 | } |
| 274 | else |
| 275 | { |
| 276 | int error = 0; |
| 277 | do { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 278 | size_t i; |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 279 | uint8_t power_up_out; |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 280 | for (i = 0; i < power_gpio->num_power_up_outs; i++) { |
| 281 | GPIO *power_pin = &power_gpio->power_up_outs[i]; |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 282 | power_up_out = state ^ !power_gpio->power_up_pols[i]; |
| 283 | error = gpio_open(power_pin, power_up_out); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 284 | if(error != GPIO_OK) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 285 | gpio_close(power_pin); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 286 | g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n", |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 287 | power_gpio->power_up_outs[i].name, error); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 288 | continue; |
| 289 | } |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 290 | g_print("PowerControl: setting power up %s to %d\n", |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 291 | power_gpio->power_up_outs[i].name, (int)power_up_out); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 292 | error = gpio_write(power_pin, power_up_out); |
| 293 | if(error != GPIO_OK) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 294 | gpio_close(power_pin); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 295 | continue; |
| 296 | } |
| 297 | gpio_close(power_pin); |
| 298 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 299 | if(error != GPIO_OK) { break; } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 300 | control_power_set_state(pwr,state); |
| 301 | } while(0); |
| 302 | if(error != GPIO_OK) |
| 303 | { |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 304 | g_print("ERROR PowerControl: GPIO set power state (rc=%d)\n",error); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 305 | } |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 306 | |
| 307 | /* If there's a latch, it should be enabled following changes to the |
| 308 | * power pins' states. This commits the changes to the latch states. */ |
| 309 | if (power_gpio->latch_out.name != NULL) { |
| 310 | int rc; |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 311 | uint8_t latch_value = 1; |
| 312 | rc = gpio_open(&power_gpio->latch_out, latch_value); |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 313 | if (rc != GPIO_OK) { |
| 314 | /* Failures are non-fatal. */ |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 315 | gpio_close(&power_gpio->latch_out); |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 316 | g_print("PowerControl ERROR failed to open latch %s rc=%d\n", |
| 317 | power_gpio->latch_out.name, rc); |
| 318 | return TRUE; |
| 319 | } |
| 320 | /* Make the latch transparent for as brief of a time as possible. */ |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 321 | rc = gpio_write(&power_gpio->latch_out, latch_value--); |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 322 | if (rc != GPIO_OK) { |
| 323 | g_print("PowerControl ERROR failed to assert latch %s rc=%d\n", |
| 324 | power_gpio->latch_out.name, rc); |
| 325 | } else { |
| 326 | g_print("PowerControl asserted latch %s\n", |
| 327 | power_gpio->latch_out.name); |
| 328 | } |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 329 | rc = gpio_write(&power_gpio->latch_out, latch_value); |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 330 | if (rc != GPIO_OK) { |
| 331 | g_print("PowerControl ERROR failed to clear latch %s rc=%d\n", |
| 332 | power_gpio->latch_out.name, rc); |
| 333 | } |
| 334 | gpio_close(&power_gpio->latch_out); |
| 335 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 336 | } |
Xo Wang | aa6c76f | 2017-03-03 14:25:01 -0800 | [diff] [blame] | 337 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 338 | return TRUE; |
| 339 | } |
| 340 | |
| 341 | static gboolean |
| 342 | on_init(Control *control, |
| 343 | GDBusMethodInvocation *invocation, |
| 344 | gpointer user_data) |
| 345 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 346 | (void) user_data; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 347 | pgood_timeout_start = 0; |
| 348 | //guint poll_interval = control_get_poll_interval(control); |
| 349 | //g_timeout_add(poll_interval, poll_pgood, user_data); |
| 350 | control_complete_init(control,invocation); |
| 351 | return TRUE; |
| 352 | } |
| 353 | |
| 354 | static gboolean |
| 355 | on_get_power_state(ControlPower *pwr, |
| 356 | GDBusMethodInvocation *invocation, |
| 357 | gpointer user_data) |
| 358 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 359 | (void) user_data; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 360 | guint pgood = control_power_get_pgood(pwr); |
| 361 | control_power_complete_get_power_state(pwr,invocation,pgood); |
| 362 | return TRUE; |
| 363 | } |
| 364 | |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 365 | static int |
Matt Spinler | 0f3fd5a | 2018-08-08 11:15:26 -0500 | [diff] [blame] | 366 | set_up_gpio(PowerGpio *power_gpio, ControlPower* control_power) |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 367 | { |
| 368 | int error = GPIO_OK; |
| 369 | int rc; |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 370 | size_t i; |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 371 | uint8_t pgood_state; |
| 372 | |
| 373 | // get gpio device paths |
Xo Wang | c1ce68b | 2016-09-22 16:34:37 -0700 | [diff] [blame] | 374 | if(power_gpio->latch_out.name != NULL) { /* latch is optional */ |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 375 | rc = gpio_get_params(&power_gpio->latch_out); |
Xo Wang | c1ce68b | 2016-09-22 16:34:37 -0700 | [diff] [blame] | 376 | if(rc != GPIO_OK) { |
| 377 | error = rc; |
| 378 | } |
| 379 | } |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 380 | rc = gpio_get_params(&power_gpio->power_good_in); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 381 | if(rc != GPIO_OK) { |
| 382 | error = rc; |
| 383 | } |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 384 | for(i = 0; i < power_gpio->num_power_up_outs; i++) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 385 | rc = gpio_get_params(&power_gpio->power_up_outs[i]); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 386 | if(rc != GPIO_OK) { |
| 387 | error = rc; |
| 388 | } |
| 389 | } |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 390 | for(i = 0; i < power_gpio->num_reset_outs; i++) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 391 | rc = gpio_get_params(&power_gpio->reset_outs[i]); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 392 | if(rc != GPIO_OK) { |
| 393 | error = rc; |
| 394 | } |
| 395 | } |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 396 | for(i = 0; i < power_gpio->num_pci_reset_outs; i++) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 397 | rc = gpio_get_params(&power_gpio->pci_reset_outs[i]); |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 398 | if(rc != GPIO_OK) { |
| 399 | error = rc; |
| 400 | } |
| 401 | } |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 402 | |
Matt Spinler | 0f0946b | 2018-08-07 16:26:55 -0500 | [diff] [blame] | 403 | gpio_inits_done(); |
| 404 | |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 405 | rc = gpio_open(&power_gpio->power_good_in, 0); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 406 | if(rc != GPIO_OK) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 407 | gpio_close(&power_gpio->power_good_in); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 408 | return rc; |
| 409 | } |
| 410 | rc = gpio_read(&power_gpio->power_good_in, &pgood_state); |
| 411 | if(rc != GPIO_OK) { |
Anthony Wilson | c0c74e7 | 2019-03-06 10:54:58 -0600 | [diff] [blame] | 412 | gpio_close(&power_gpio->power_good_in); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 413 | return rc; |
| 414 | } |
| 415 | gpio_close(&power_gpio->power_good_in); |
| 416 | control_power_set_pgood(control_power, pgood_state); |
| 417 | control_power_set_state(control_power, pgood_state); |
| 418 | g_print("Pgood state: %d\n", pgood_state); |
| 419 | |
| 420 | return error; |
| 421 | } |
| 422 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 423 | static void |
| 424 | on_bus_acquired(GDBusConnection *connection, |
| 425 | const gchar *name, |
| 426 | gpointer user_data) |
| 427 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 428 | (void) name; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 429 | ObjectSkeleton *object; |
| 430 | cmdline *cmd = user_data; |
| 431 | if(cmd->argc < 3) |
| 432 | { |
| 433 | g_print("Usage: power_control.exe [poll interval] [timeout]\n"); |
| 434 | return; |
| 435 | } |
| 436 | manager = g_dbus_object_manager_server_new(dbus_object_path); |
| 437 | gchar *s; |
| 438 | s = g_strdup_printf("%s/%s",dbus_object_path,instance_name); |
| 439 | object = object_skeleton_new(s); |
| 440 | g_free(s); |
| 441 | |
| 442 | ControlPower* control_power = control_power_skeleton_new(); |
| 443 | object_skeleton_set_control_power(object, control_power); |
| 444 | g_object_unref(control_power); |
| 445 | |
| 446 | Control* control = control_skeleton_new(); |
| 447 | object_skeleton_set_control(object, control); |
| 448 | g_object_unref(control); |
| 449 | |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 450 | //define method callbacks here |
| 451 | g_signal_connect(control_power, |
| 452 | "handle-set-power-state", |
| 453 | G_CALLBACK(on_set_power_state), |
| 454 | object); /* user_data */ |
| 455 | |
| 456 | g_signal_connect(control_power, |
| 457 | "handle-get-power-state", |
| 458 | G_CALLBACK(on_get_power_state), |
| 459 | NULL); /* user_data */ |
| 460 | |
| 461 | g_signal_connect(control, |
| 462 | "handle-init", |
| 463 | G_CALLBACK(on_init), |
| 464 | object); /* user_data */ |
| 465 | |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 466 | /* Listen for BootProgress signal from BootProgress sensor */ |
| 467 | g_dbus_connection_signal_subscribe(connection, |
| 468 | NULL, /* service */ |
Lei YU | 93b84e4 | 2017-10-11 15:06:20 +0800 | [diff] [blame] | 469 | "org.freedesktop.DBus.Properties", /* interface_name */ |
| 470 | "PropertiesChanged", /* member: name of the signal */ |
| 471 | "/xyz/openbmc_project/state/host0", /* obj path */ |
Yi Li | 0475f65 | 2016-10-25 13:19:59 +0800 | [diff] [blame] | 472 | NULL, /* arg0 */ |
| 473 | G_DBUS_SIGNAL_FLAGS_NONE, |
| 474 | (GDBusSignalCallback) on_boot_progress, |
| 475 | object, /* user data */ |
| 476 | NULL ); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 477 | |
| 478 | /* Export the object (@manager takes its own reference to @object) */ |
Brad Bishop | 58e694d | 2016-04-13 16:04:32 -0400 | [diff] [blame] | 479 | g_dbus_object_manager_server_set_connection(manager, connection); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 480 | g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object)); |
| 481 | g_object_unref(object); |
| 482 | |
Matt Spinler | 0f3fd5a | 2018-08-08 11:15:26 -0500 | [diff] [blame] | 483 | if(read_gpios(&g_gpio_configs) != TRUE) { |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 484 | g_print("ERROR PowerControl: could not read power GPIO configuration\n"); |
Yong Li | 1acfd75 | 2018-04-04 13:01:23 +0800 | [diff] [blame] | 485 | exit(-1); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 486 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 487 | |
Matt Spinler | 0f3fd5a | 2018-08-08 11:15:26 -0500 | [diff] [blame] | 488 | int rc = set_up_gpio(&g_gpio_configs.power_gpio, control_power); |
Xo Wang | 20a1941 | 2016-09-22 11:26:14 -0700 | [diff] [blame] | 489 | if(rc != GPIO_OK) { |
| 490 | g_print("ERROR PowerControl: GPIO setup (rc=%d)\n",rc); |
Yong Li | 1acfd75 | 2018-04-04 13:01:23 +0800 | [diff] [blame] | 491 | exit(-1); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 492 | } |
| 493 | //start poll |
| 494 | pgood_timeout_start = 0; |
| 495 | int poll_interval = atoi(cmd->argv[1]); |
| 496 | int pgood_timeout = atoi(cmd->argv[2]); |
Matt Spinler | 89e6c91 | 2017-12-14 09:55:13 -0600 | [diff] [blame] | 497 | if(poll_interval < 500 || pgood_timeout <5) { |
| 498 | g_print("ERROR PowerControl: poll_interval < 500 or pgood_timeout < 5\n"); |
Yong Li | 1acfd75 | 2018-04-04 13:01:23 +0800 | [diff] [blame] | 499 | exit(-1); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 500 | } else { |
| 501 | control_set_poll_interval(control,poll_interval); |
| 502 | control_power_set_pgood_timeout(control_power,pgood_timeout); |
| 503 | g_timeout_add(poll_interval, poll_pgood, object); |
| 504 | } |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static void |
| 508 | on_name_acquired(GDBusConnection *connection, |
| 509 | const gchar *name, |
| 510 | gpointer user_data) |
| 511 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 512 | (void) connection; |
| 513 | (void) name; |
| 514 | (void) user_data; |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static void |
| 518 | on_name_lost(GDBusConnection *connection, |
| 519 | const gchar *name, |
| 520 | gpointer user_data) |
| 521 | { |
Patrick Williams | 600ff33 | 2024-10-04 09:10:56 -0400 | [diff] [blame^] | 522 | (void) connection; |
| 523 | (void) name; |
| 524 | (void) user_data; |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 525 | free_gpios(&g_gpio_configs); |
Brad Bishop | 7739049 | 2016-04-13 10:47:19 -0400 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /*----------------------------------------------------------------*/ |
| 529 | /* Main Event Loop */ |
| 530 | |
| 531 | gint |
| 532 | main(gint argc, gchar *argv[]) |
| 533 | { |
| 534 | GMainLoop *loop; |
| 535 | cmdline cmd; |
| 536 | cmd.argc = argc; |
| 537 | cmd.argv = argv; |
| 538 | |
| 539 | guint id; |
| 540 | loop = g_main_loop_new(NULL, FALSE); |
| 541 | |
| 542 | id = g_bus_own_name(DBUS_TYPE, |
| 543 | dbus_name, |
| 544 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT | |
| 545 | G_BUS_NAME_OWNER_FLAGS_REPLACE, |
| 546 | on_bus_acquired, |
| 547 | on_name_acquired, |
| 548 | on_name_lost, |
| 549 | &cmd, |
| 550 | NULL); |
| 551 | |
| 552 | g_main_loop_run(loop); |
| 553 | |
| 554 | g_bus_unown_name(id); |
| 555 | g_main_loop_unref(loop); |
| 556 | return 0; |
| 557 | } |