blob: c5ec5e5a451dfe6f3068798314b23faea2fc1b66 [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#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 Bishopf6c85682016-06-27 11:56:39 -040010#include <openbmc_intf.h>
11#include <openbmc.h>
12#include <gpio.h>
Lei YU45cb4fc2016-11-29 01:48:26 +080013#include <gpio_configs.h>
Brad Bishop77390492016-04-13 10:47:19 -040014
15/* ------------------------------------------------------------------------- */
16static const gchar* dbus_object_path = "/org/openbmc/control";
17static const gchar* instance_name = "power0";
18static const gchar* dbus_name = "org.openbmc.control.Power";
19
Yi Li0475f652016-10-25 13:19:59 +080020static int g_pci_reset_held = 1;
21
Lei YU75a18a22016-11-22 01:47:47 +080022static GpioConfigs g_gpio_configs;
Brad Bishop77390492016-04-13 10:47:19 -040023
24static GDBusObjectManagerServer *manager = NULL;
25
26time_t pgood_timeout_start = 0;
27
28// TODO: Change to interrupt driven instead of polling
29static gboolean
30poll_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 Bishop77390492016-04-13 10:47:19 -040036 guint poll_int = control_get_poll_interval(control);
37 if(poll_int == 0)
38 {
Xo Wang20a19412016-09-22 11:26:14 -070039 g_print("ERROR PowerControl: Poll interval cannot be 0\n");
Brad Bishop77390492016-04-13 10:47:19 -040040 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 Wang20a19412016-09-22 11:26:14 -070047 g_print("ERROR PowerControl: Pgood poll timeout\n");
Brad Bishop77390492016-04-13 10:47:19 -040048 // 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 Williams27eaf902017-03-21 15:46:40 -050051 return TRUE;
Brad Bishop77390492016-04-13 10:47:19 -040052 }
Xo Wang20a19412016-09-22 11:26:14 -070053 uint8_t pgood_state;
Brad Bishop77390492016-04-13 10:47:19 -040054
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060055 int rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in, 0);
Xo Wang20a19412016-09-22 11:26:14 -070056 if(rc != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060057 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -070058 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +080059 g_gpio_configs.power_gpio.power_good_in.name, rc);
Xo Wang20a19412016-09-22 11:26:14 -070060 return FALSE;
61 }
Lei YU75a18a22016-11-22 01:47:47 +080062 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 Bishop77390492016-04-13 10:47:19 -040064 if(rc == GPIO_OK)
65 {
66 //if changed, set property and emit signal
Xo Wang20a19412016-09-22 11:26:14 -070067 if(pgood_state != control_power_get_pgood(control_power))
Brad Bishop77390492016-04-13 10:47:19 -040068 {
Patrick Williams600ff332024-10-04 09:10:56 -040069 size_t i;
Xo Wang20a19412016-09-22 11:26:14 -070070 uint8_t reset_state;
71 control_power_set_pgood(control_power, pgood_state);
72 if(pgood_state == 0)
Brad Bishop77390492016-04-13 10:47:19 -040073 {
74 control_power_emit_power_lost(control_power);
Yi Li0475f652016-10-25 13:19:59 +080075 g_pci_reset_held = 1;
Brad Bishop77390492016-04-13 10:47:19 -040076 }
77 else
78 {
79 control_power_emit_power_good(control_power);
Xo Wang20a19412016-09-22 11:26:14 -070080 }
Brad Bishop77390492016-04-13 10:47:19 -040081
Lei YU75a18a22016-11-22 01:47:47 +080082 for(i = 0; i < g_gpio_configs.power_gpio.num_reset_outs; i++)
Xo Wang20a19412016-09-22 11:26:14 -070083 {
Lei YU75a18a22016-11-22 01:47:47 +080084 GPIO *reset_out = &g_gpio_configs.power_gpio.reset_outs[i];
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060085 reset_state = pgood_state ^ g_gpio_configs.power_gpio.reset_pols[i];
86 rc = gpio_open(reset_out, reset_state);
Xo Wang20a19412016-09-22 11:26:14 -070087 if(rc != GPIO_OK)
88 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -060089 gpio_close(reset_out);
Xo Wang20a19412016-09-22 11:26:14 -070090 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
91 reset_out->name, rc);
92 continue;
93 }
94
Yi Li39df4032016-12-16 16:06:50 +080095 g_print("PowerControl: pgood: %d, setting reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +080096 (int)pgood_state, reset_out->name, (int)reset_state);
Xo Wang20a19412016-09-22 11:26:14 -070097 gpio_write(reset_out, reset_state);
98 gpio_close(reset_out);
Brad Bishop77390492016-04-13 10:47:19 -040099 }
Yi Li0475f652016-10-25 13:19:59 +0800100
Lei YU75a18a22016-11-22 01:47:47 +0800101 for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++)
Yi Li0475f652016-10-25 13:19:59 +0800102 {
Lei YU75a18a22016-11-22 01:47:47 +0800103 GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i];
Yi Li0475f652016-10-25 13:19:59 +0800104 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 YU75a18a22016-11-22 01:47:47 +0800110 if(g_gpio_configs.power_gpio.pci_reset_holds[i])
Yi Li0475f652016-10-25 13:19:59 +0800111 {
112 g_print("Holding pci reset: %s\n", pci_reset_out->name);
113 continue;
114 }
115 }
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600116 reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i];
117 rc = gpio_open(pci_reset_out, reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800118 if(rc != GPIO_OK)
119 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600120 gpio_close(pci_reset_out);
Yi Li0475f652016-10-25 13:19:59 +0800121 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
122 pci_reset_out->name, rc);
123 continue;
124 }
125
Yi Li39df4032016-12-16 16:06:50 +0800126 g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +0800127 (int)pgood_state, pci_reset_out->name, (int)reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800128 gpio_write(pci_reset_out, reset_state);
129 gpio_close(pci_reset_out);
130 }
Brad Bishop77390492016-04-13 10:47:19 -0400131 }
132 } else {
Xo Wang20a19412016-09-22 11:26:14 -0700133 g_print("ERROR PowerControl: GPIO read error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +0800134 g_gpio_configs.power_gpio.power_good_in.name, rc);
Brad Bishop77390492016-04-13 10:47:19 -0400135 //return false so poll won't get called anymore
136 return FALSE;
137 }
138 //pgood is not at desired state yet
Xo Wang20a19412016-09-22 11:26:14 -0700139 if(pgood_state != control_power_get_state(control_power) &&
Brad Bishop77390492016-04-13 10:47:19 -0400140 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 Li0475f652016-10-25 13:19:59 +0800153/* Handler for BootProgress signal from BootProgress sensor */
154static void
155on_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 Williams600ff332024-10-04 09:10:56 -0400163 (void) connection;
164 (void) sender_name;
165 (void) object_path;
166 (void) interface_name;
167 (void) signal_name;
168 (void) user_data;
169
Lei YU93b84e42017-10-11 15:06:20 +0800170 gchar *interface;
171 GVariantIter *properties;
172 GVariantIter *dummy;
173 gchar *boot_progress = NULL;
174 gchar *property;
175 GVariant *value;
Yi Li0475f652016-10-25 13:19:59 +0800176 uint8_t pgood_state;
177 uint8_t reset_state;
178 int rc;
Patrick Williams600ff332024-10-04 09:10:56 -0400179 size_t i;
Lei YU93b84e42017-10-11 15:06:20 +0800180 int ignore;
Yi Li0475f652016-10-25 13:19:59 +0800181
182 if(!parameters)
183 return;
184
185 /* prevent release again */
186 if(!g_pci_reset_held)
187 return;
188
Lei YU93b84e42017-10-11 15:06:20 +0800189 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 Li0475f652016-10-25 13:19:59 +0800207 /* Release PCI reset when FW boot progress goes beyond 'Baseboard Init' */
Lei YU93b84e42017-10-11 15:06:20 +0800208 ignore = strcmp(boot_progress,
209 "xyz.openbmc_project.State.Boot.Progress.ProgressStages.MotherboardInit") == 0;
210 g_free(boot_progress);
211
212 if (ignore)
Yi Li0475f652016-10-25 13:19:59 +0800213 return;
214
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600215 rc = gpio_open(&g_gpio_configs.power_gpio.power_good_in, 0);
Yi Li0475f652016-10-25 13:19:59 +0800216 if(rc != GPIO_OK)
217 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600218 gpio_close(&g_gpio_configs.power_gpio.power_good_in);
Yi Li0475f652016-10-25 13:19:59 +0800219 g_print("ERROR PowerControl: on_boot_progress(): GPIO open error (gpio=%s,rc=%d)\n",
Lei YU75a18a22016-11-22 01:47:47 +0800220 g_gpio_configs.power_gpio.power_good_in.name, rc);
Yi Li0475f652016-10-25 13:19:59 +0800221 return;
222 }
Lei YU75a18a22016-11-22 01:47:47 +0800223 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 Li0475f652016-10-25 13:19:59 +0800225 if(rc != GPIO_OK || pgood_state != 1)
226 return;
227
Lei YU75a18a22016-11-22 01:47:47 +0800228 for(i = 0; i < g_gpio_configs.power_gpio.num_pci_reset_outs; i++)
Yi Li0475f652016-10-25 13:19:59 +0800229 {
Lei YU75a18a22016-11-22 01:47:47 +0800230 GPIO *pci_reset_out = &g_gpio_configs.power_gpio.pci_reset_outs[i];
Yi Li0475f652016-10-25 13:19:59 +0800231
Lei YU75a18a22016-11-22 01:47:47 +0800232 if(!g_gpio_configs.power_gpio.pci_reset_holds[i])
Yi Li0475f652016-10-25 13:19:59 +0800233 continue;
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600234 reset_state = pgood_state ^ g_gpio_configs.power_gpio.pci_reset_pols[i];
235 rc = gpio_open(pci_reset_out, reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800236 if(rc != GPIO_OK)
237 {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600238 gpio_close(pci_reset_out);
Yi Li0475f652016-10-25 13:19:59 +0800239 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
240 pci_reset_out->name, rc);
241 continue;
242 }
243
Yi Li39df4032016-12-16 16:06:50 +0800244 g_print("PowerControl: pgood: %d, setting pci reset %s to %d\n",
Lei YU75a18a22016-11-22 01:47:47 +0800245 (int)pgood_state, pci_reset_out->name, (int)reset_state);
Yi Li0475f652016-10-25 13:19:59 +0800246 gpio_write(pci_reset_out, reset_state);
247 gpio_close(pci_reset_out);
Lei YU93b84e42017-10-11 15:06:20 +0800248 g_print("Released pci reset: %s\n", pci_reset_out->name);
Yi Li0475f652016-10-25 13:19:59 +0800249 }
250 g_pci_reset_held = 0;
251}
252
Brad Bishop77390492016-04-13 10:47:19 -0400253static gboolean
254on_set_power_state(ControlPower *pwr,
255 GDBusMethodInvocation *invocation,
256 guint state,
257 gpointer user_data)
258{
Patrick Williams600ff332024-10-04 09:10:56 -0400259 (void) user_data;
Xo Wangaa6c76f2017-03-03 14:25:01 -0800260 PowerGpio *power_gpio = &g_gpio_configs.power_gpio;
Brad Bishop77390492016-04-13 10:47:19 -0400261 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 Williams600ff332024-10-04 09:10:56 -0400270 if(state == (guint) control_power_get_state(pwr))
Brad Bishop77390492016-04-13 10:47:19 -0400271 {
272 g_print("Power already at requested state: %d\n",state);
273 }
274 else
275 {
276 int error = 0;
277 do {
Patrick Williams600ff332024-10-04 09:10:56 -0400278 size_t i;
Xo Wang20a19412016-09-22 11:26:14 -0700279 uint8_t power_up_out;
Xo Wangaa6c76f2017-03-03 14:25:01 -0800280 for (i = 0; i < power_gpio->num_power_up_outs; i++) {
281 GPIO *power_pin = &power_gpio->power_up_outs[i];
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600282 power_up_out = state ^ !power_gpio->power_up_pols[i];
283 error = gpio_open(power_pin, power_up_out);
Xo Wang20a19412016-09-22 11:26:14 -0700284 if(error != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600285 gpio_close(power_pin);
Xo Wang20a19412016-09-22 11:26:14 -0700286 g_print("ERROR PowerControl: GPIO open error (gpio=%s,rc=%d)\n",
Xo Wangaa6c76f2017-03-03 14:25:01 -0800287 power_gpio->power_up_outs[i].name, error);
Xo Wang20a19412016-09-22 11:26:14 -0700288 continue;
289 }
Xo Wang20a19412016-09-22 11:26:14 -0700290 g_print("PowerControl: setting power up %s to %d\n",
Xo Wangaa6c76f2017-03-03 14:25:01 -0800291 power_gpio->power_up_outs[i].name, (int)power_up_out);
Xo Wang20a19412016-09-22 11:26:14 -0700292 error = gpio_write(power_pin, power_up_out);
293 if(error != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600294 gpio_close(power_pin);
Xo Wang20a19412016-09-22 11:26:14 -0700295 continue;
296 }
297 gpio_close(power_pin);
298 }
Brad Bishop77390492016-04-13 10:47:19 -0400299 if(error != GPIO_OK) { break; }
Brad Bishop77390492016-04-13 10:47:19 -0400300 control_power_set_state(pwr,state);
301 } while(0);
302 if(error != GPIO_OK)
303 {
Xo Wang20a19412016-09-22 11:26:14 -0700304 g_print("ERROR PowerControl: GPIO set power state (rc=%d)\n",error);
Brad Bishop77390492016-04-13 10:47:19 -0400305 }
Xo Wangaa6c76f2017-03-03 14:25:01 -0800306
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 Wilsonc0c74e72019-03-06 10:54:58 -0600311 uint8_t latch_value = 1;
312 rc = gpio_open(&power_gpio->latch_out, latch_value);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800313 if (rc != GPIO_OK) {
314 /* Failures are non-fatal. */
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600315 gpio_close(&power_gpio->latch_out);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800316 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 Wilsonc0c74e72019-03-06 10:54:58 -0600321 rc = gpio_write(&power_gpio->latch_out, latch_value--);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800322 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 Wilsonc0c74e72019-03-06 10:54:58 -0600329 rc = gpio_write(&power_gpio->latch_out, latch_value);
Xo Wangaa6c76f2017-03-03 14:25:01 -0800330 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 Bishop77390492016-04-13 10:47:19 -0400336 }
Xo Wangaa6c76f2017-03-03 14:25:01 -0800337
Brad Bishop77390492016-04-13 10:47:19 -0400338 return TRUE;
339}
340
341static gboolean
342on_init(Control *control,
343 GDBusMethodInvocation *invocation,
344 gpointer user_data)
345{
Patrick Williams600ff332024-10-04 09:10:56 -0400346 (void) user_data;
Brad Bishop77390492016-04-13 10:47:19 -0400347 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
354static gboolean
355on_get_power_state(ControlPower *pwr,
356 GDBusMethodInvocation *invocation,
357 gpointer user_data)
358{
Patrick Williams600ff332024-10-04 09:10:56 -0400359 (void) user_data;
Brad Bishop77390492016-04-13 10:47:19 -0400360 guint pgood = control_power_get_pgood(pwr);
361 control_power_complete_get_power_state(pwr,invocation,pgood);
362 return TRUE;
363}
364
Xo Wang20a19412016-09-22 11:26:14 -0700365static int
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500366set_up_gpio(PowerGpio *power_gpio, ControlPower* control_power)
Xo Wang20a19412016-09-22 11:26:14 -0700367{
368 int error = GPIO_OK;
369 int rc;
Patrick Williams600ff332024-10-04 09:10:56 -0400370 size_t i;
Xo Wang20a19412016-09-22 11:26:14 -0700371 uint8_t pgood_state;
372
373 // get gpio device paths
Xo Wangc1ce68b2016-09-22 16:34:37 -0700374 if(power_gpio->latch_out.name != NULL) { /* latch is optional */
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600375 rc = gpio_get_params(&power_gpio->latch_out);
Xo Wangc1ce68b2016-09-22 16:34:37 -0700376 if(rc != GPIO_OK) {
377 error = rc;
378 }
379 }
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600380 rc = gpio_get_params(&power_gpio->power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -0700381 if(rc != GPIO_OK) {
382 error = rc;
383 }
Patrick Williams600ff332024-10-04 09:10:56 -0400384 for(i = 0; i < power_gpio->num_power_up_outs; i++) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600385 rc = gpio_get_params(&power_gpio->power_up_outs[i]);
Xo Wang20a19412016-09-22 11:26:14 -0700386 if(rc != GPIO_OK) {
387 error = rc;
388 }
389 }
Patrick Williams600ff332024-10-04 09:10:56 -0400390 for(i = 0; i < power_gpio->num_reset_outs; i++) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600391 rc = gpio_get_params(&power_gpio->reset_outs[i]);
Xo Wang20a19412016-09-22 11:26:14 -0700392 if(rc != GPIO_OK) {
393 error = rc;
394 }
395 }
Patrick Williams600ff332024-10-04 09:10:56 -0400396 for(i = 0; i < power_gpio->num_pci_reset_outs; i++) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600397 rc = gpio_get_params(&power_gpio->pci_reset_outs[i]);
Yi Li0475f652016-10-25 13:19:59 +0800398 if(rc != GPIO_OK) {
399 error = rc;
400 }
401 }
Xo Wang20a19412016-09-22 11:26:14 -0700402
Matt Spinler0f0946b2018-08-07 16:26:55 -0500403 gpio_inits_done();
404
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600405 rc = gpio_open(&power_gpio->power_good_in, 0);
Xo Wang20a19412016-09-22 11:26:14 -0700406 if(rc != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600407 gpio_close(&power_gpio->power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -0700408 return rc;
409 }
410 rc = gpio_read(&power_gpio->power_good_in, &pgood_state);
411 if(rc != GPIO_OK) {
Anthony Wilsonc0c74e72019-03-06 10:54:58 -0600412 gpio_close(&power_gpio->power_good_in);
Xo Wang20a19412016-09-22 11:26:14 -0700413 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 Bishop77390492016-04-13 10:47:19 -0400423static void
424on_bus_acquired(GDBusConnection *connection,
425 const gchar *name,
426 gpointer user_data)
427{
Patrick Williams600ff332024-10-04 09:10:56 -0400428 (void) name;
Brad Bishop77390492016-04-13 10:47:19 -0400429 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 Bishop77390492016-04-13 10:47:19 -0400450 //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 Li0475f652016-10-25 13:19:59 +0800466 /* Listen for BootProgress signal from BootProgress sensor */
467 g_dbus_connection_signal_subscribe(connection,
468 NULL, /* service */
Lei YU93b84e42017-10-11 15:06:20 +0800469 "org.freedesktop.DBus.Properties", /* interface_name */
470 "PropertiesChanged", /* member: name of the signal */
471 "/xyz/openbmc_project/state/host0", /* obj path */
Yi Li0475f652016-10-25 13:19:59 +0800472 NULL, /* arg0 */
473 G_DBUS_SIGNAL_FLAGS_NONE,
474 (GDBusSignalCallback) on_boot_progress,
475 object, /* user data */
476 NULL );
Brad Bishop77390492016-04-13 10:47:19 -0400477
478 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400479 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400480 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
481 g_object_unref(object);
482
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500483 if(read_gpios(&g_gpio_configs) != TRUE) {
Xo Wang20a19412016-09-22 11:26:14 -0700484 g_print("ERROR PowerControl: could not read power GPIO configuration\n");
Yong Li1acfd752018-04-04 13:01:23 +0800485 exit(-1);
Xo Wang20a19412016-09-22 11:26:14 -0700486 }
Brad Bishop77390492016-04-13 10:47:19 -0400487
Matt Spinler0f3fd5a2018-08-08 11:15:26 -0500488 int rc = set_up_gpio(&g_gpio_configs.power_gpio, control_power);
Xo Wang20a19412016-09-22 11:26:14 -0700489 if(rc != GPIO_OK) {
490 g_print("ERROR PowerControl: GPIO setup (rc=%d)\n",rc);
Yong Li1acfd752018-04-04 13:01:23 +0800491 exit(-1);
Brad Bishop77390492016-04-13 10:47:19 -0400492 }
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 Spinler89e6c912017-12-14 09:55:13 -0600497 if(poll_interval < 500 || pgood_timeout <5) {
498 g_print("ERROR PowerControl: poll_interval < 500 or pgood_timeout < 5\n");
Yong Li1acfd752018-04-04 13:01:23 +0800499 exit(-1);
Brad Bishop77390492016-04-13 10:47:19 -0400500 } 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 Bishop77390492016-04-13 10:47:19 -0400505}
506
507static void
508on_name_acquired(GDBusConnection *connection,
509 const gchar *name,
510 gpointer user_data)
511{
Patrick Williams600ff332024-10-04 09:10:56 -0400512 (void) connection;
513 (void) name;
514 (void) user_data;
Brad Bishop77390492016-04-13 10:47:19 -0400515}
516
517static void
518on_name_lost(GDBusConnection *connection,
519 const gchar *name,
520 gpointer user_data)
521{
Patrick Williams600ff332024-10-04 09:10:56 -0400522 (void) connection;
523 (void) name;
524 (void) user_data;
Lei YU75a18a22016-11-22 01:47:47 +0800525 free_gpios(&g_gpio_configs);
Brad Bishop77390492016-04-13 10:47:19 -0400526}
527
528/*----------------------------------------------------------------*/
529/* Main Event Loop */
530
531gint
532main(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}