blob: a7abba4113272eb27e23b199b3db5a92ca7a719d [file] [log] [blame]
Norman James26072c02015-08-25 07:14:29 -05001#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>
Norman James8abb50c2015-09-16 10:58:16 -05009#include <syslog.h>
Norman James362a80f2015-09-14 14:04:39 -050010#include "interfaces/openbmc_intf.h"
Norman James10ff6a32015-08-27 14:24:17 -050011#include "openbmc.h"
12#include "gpio.h"
Norman Jamescfc2b442015-10-31 17:31:46 -050013#include "object_mapper.h"
Norman Jamese2765102015-08-19 22:00:55 -050014
15/* ---------------------------------------------------------------------------------------------------- */
Norman James362a80f2015-09-14 14:04:39 -050016static const gchar* dbus_object_path = "/org/openbmc/control";
Norman James8fee6f22015-10-28 12:48:43 -050017static const gchar* instance_name = "power0";
Norman James26072c02015-08-25 07:14:29 -050018static const gchar* dbus_name = "org.openbmc.control.Power";
19
Norman James32e74e22015-09-15 21:28:06 -050020//This object will use these GPIOs
Norman James3f97c5d2015-08-26 17:44:14 -050021GPIO power_pin = (GPIO){ "POWER_PIN" };
22GPIO pgood = (GPIO){ "PGOOD" };
Norman Jamescfc2b442015-10-31 17:31:46 -050023GPIO usb_reset = (GPIO){ "USB_RESET" };
24GPIO pcie_reset = (GPIO){ "PCIE_RESET" };
25
Norman Jamese2765102015-08-19 22:00:55 -050026
27static GDBusObjectManagerServer *manager = NULL;
Norman Jamese2765102015-08-19 22:00:55 -050028
Norman James88872672015-09-21 16:51:35 -050029time_t pgood_timeout_start = 0;
Norman James362a80f2015-09-14 14:04:39 -050030
Norman James3d3b7fb2015-10-06 16:54:06 -050031// TODO: Change to interrupt driven instead of polling
Norman Jamesce46e3e2015-08-30 22:25:55 -050032static gboolean poll_pgood(gpointer user_data)
33{
34 ControlPower *control_power = object_get_control_power((Object*)user_data);
35 Control* control = object_get_control((Object*)user_data);
Norman Jamesbd4abd12015-09-16 22:43:46 -050036
37 //send the heartbeat
Norman James32e74e22015-09-15 21:28:06 -050038 const gchar* obj_path = g_dbus_object_get_object_path((GDBusObject*)user_data);
Norman Jamesce46e3e2015-08-30 22:25:55 -050039
Norman James6a1283c2015-09-16 22:21:11 -050040 guint poll_int = control_get_poll_interval(control);
41 if (poll_int == 0)
42 {
Norman James2891c532015-10-06 08:01:56 -050043 printf("ERROR PowerControl: Poll interval cannot be 0\n");
Norman James6a1283c2015-09-16 22:21:11 -050044 return FALSE;
45 }
Norman James88872672015-09-21 16:51:35 -050046 //handle timeout
47 time_t current_time = time(NULL);
48 if (difftime(current_time,pgood_timeout_start) > control_power_get_pgood_timeout(control_power)
49 && pgood_timeout_start != 0)
Norman James32e74e22015-09-15 21:28:06 -050050 {
Norman James2891c532015-10-06 08:01:56 -050051 printf("ERROR PowerControl: Pgood poll timeout\n");
Norman Jamesbd4abd12015-09-16 22:43:46 -050052 // set timeout to 0 so timeout doesn't happen again
Norman James32e74e22015-09-15 21:28:06 -050053 control_power_set_pgood_timeout(control_power,0);
Norman James88872672015-09-21 16:51:35 -050054 pgood_timeout_start = 0;
Norman James8abb50c2015-09-16 10:58:16 -050055 return TRUE;
Norman James32e74e22015-09-15 21:28:06 -050056 }
Norman James32e74e22015-09-15 21:28:06 -050057 uint8_t gpio;
Norman James88872672015-09-21 16:51:35 -050058
59 int rc = gpio_open(&pgood);
60 rc = gpio_read(&pgood,&gpio);
61 gpio_close(&pgood);
Norman James32e74e22015-09-15 21:28:06 -050062 if (rc == GPIO_OK)
Norman James362a80f2015-09-14 14:04:39 -050063 {
Norman James32e74e22015-09-15 21:28:06 -050064 //if changed, set property and emit signal
65 if (gpio != control_power_get_pgood(control_power))
66 {
67 control_power_set_pgood(control_power,gpio);
68 if (gpio==0)
69 {
70 control_power_emit_power_lost(control_power);
Norman Jamesa3e47c42015-10-18 14:43:10 -050071 control_emit_goto_system_state(control,"HOST_POWERED_OFF");
Norman Jamescfc2b442015-10-31 17:31:46 -050072 rc = gpio_open(&pcie_reset);
73 rc = gpio_write(&pcie_reset,0);
74 gpio_close(&pcie_reset);
75
76 rc = gpio_open(&usb_reset);
77 rc = gpio_write(&usb_reset,0);
78 gpio_close(&usb_reset);
79
Norman James32e74e22015-09-15 21:28:06 -050080 }
81 else
82 {
83 control_power_emit_power_good(control_power);
Norman Jamesa3e47c42015-10-18 14:43:10 -050084 control_emit_goto_system_state(control,"HOST_POWERED_ON");
Norman Jamescfc2b442015-10-31 17:31:46 -050085 rc = gpio_open(&pcie_reset);
86 rc = gpio_write(&pcie_reset,1);
87 gpio_close(&pcie_reset);
88
89 rc = gpio_open(&usb_reset);
90 rc = gpio_write(&usb_reset,1);
91 gpio_close(&usb_reset);
Norman James32e74e22015-09-15 21:28:06 -050092 }
93 }
94 } else {
Norman James2891c532015-10-06 08:01:56 -050095 printf("ERROR PowerControl: GPIO read error (gpio=%s,rc=%d)\n",pgood.name,rc);
Norman Jamesa3e47c42015-10-18 14:43:10 -050096 //return false so poll won't get called anymore
97 return FALSE;
Norman James32e74e22015-09-15 21:28:06 -050098 }
99 //pgood is not at desired state yet
100 if (gpio != control_power_get_state(control_power) &&
Norman James88872672015-09-21 16:51:35 -0500101 control_power_get_pgood_timeout(control_power) > 0)
Norman James32e74e22015-09-15 21:28:06 -0500102 {
Norman James88872672015-09-21 16:51:35 -0500103 if (pgood_timeout_start == 0 ) {
104 pgood_timeout_start = current_time;
105 }
Norman James32e74e22015-09-15 21:28:06 -0500106 }
107 else
108 {
Norman James88872672015-09-21 16:51:35 -0500109 pgood_timeout_start = 0;
Norman James362a80f2015-09-14 14:04:39 -0500110 }
Norman Jamesce46e3e2015-08-30 22:25:55 -0500111 return TRUE;
112}
113
114
115
Norman Jamese2765102015-08-19 22:00:55 -0500116static gboolean
Norman James3f97c5d2015-08-26 17:44:14 -0500117on_set_power_state (ControlPower *pwr,
Norman Jamese2765102015-08-19 22:00:55 -0500118 GDBusMethodInvocation *invocation,
119 guint state,
120 gpointer user_data)
121{
Norman James362a80f2015-09-14 14:04:39 -0500122 Control* control = object_get_control((Object*)user_data);
Norman James32e74e22015-09-15 21:28:06 -0500123 const gchar* obj_path = g_dbus_object_get_object_path((GDBusObject*)user_data);
Norman James3f97c5d2015-08-26 17:44:14 -0500124 if (state > 1)
125 {
126 g_dbus_method_invocation_return_dbus_error (invocation,
127 "org.openbmc.ControlPower.Error.Failed",
128 "Invalid power state");
129 return TRUE;
130 }
Norman James9e6acf92015-09-08 07:00:04 -0500131 // return from method call
132 control_power_complete_set_power_state(pwr,invocation);
Norman James3f97c5d2015-08-26 17:44:14 -0500133 if (state == control_power_get_state(pwr))
134 {
Norman James9e6acf92015-09-08 07:00:04 -0500135 g_print("Power already at requested state: %d\n",state);
Norman James3f97c5d2015-08-26 17:44:14 -0500136 }
Norman James9e6acf92015-09-08 07:00:04 -0500137 else
138 {
Norman James32e74e22015-09-15 21:28:06 -0500139 int error = 0;
140 do {
Norman James19e45912015-10-04 20:19:41 -0500141 if (state == 1) {
Norman Jamesa3e47c42015-10-18 14:43:10 -0500142 control_emit_goto_system_state(control,"HOST_POWERING_ON");
Norman James19e45912015-10-04 20:19:41 -0500143 } else {
Norman Jamesa3e47c42015-10-18 14:43:10 -0500144 control_emit_goto_system_state(control,"HOST_POWERING_OFF");
Norman James19e45912015-10-04 20:19:41 -0500145 }
Norman James32e74e22015-09-15 21:28:06 -0500146 error = gpio_open(&power_pin);
Norman James88872672015-09-21 16:51:35 -0500147 if (error != GPIO_OK) { break; }
Norman James32e74e22015-09-15 21:28:06 -0500148 error = gpio_write(&power_pin,!state);
Norman James88872672015-09-21 16:51:35 -0500149 if (error != GPIO_OK) { break; }
Norman James32e74e22015-09-15 21:28:06 -0500150 gpio_close(&power_pin);
151 control_power_set_state(pwr,state);
Norman James32e74e22015-09-15 21:28:06 -0500152 } while(0);
153 if (error != GPIO_OK)
Norman James362a80f2015-09-14 14:04:39 -0500154 {
Norman James2891c532015-10-06 08:01:56 -0500155 printf("ERROR PowerControl: GPIO set power state (rc=%d)\n",error);
Norman James362a80f2015-09-14 14:04:39 -0500156 }
Norman James9e6acf92015-09-08 07:00:04 -0500157 }
158 return TRUE;
159}
Norman Jamese2765102015-08-19 22:00:55 -0500160
Norman James9e6acf92015-09-08 07:00:04 -0500161static gboolean
162on_init (Control *control,
163 GDBusMethodInvocation *invocation,
164 gpointer user_data)
165{
Norman James88872672015-09-21 16:51:35 -0500166 pgood_timeout_start = 0;
Norman James8fee6f22015-10-28 12:48:43 -0500167 //guint poll_interval = control_get_poll_interval(control);
168 //g_timeout_add(poll_interval, poll_pgood, user_data);
Norman James9e6acf92015-09-08 07:00:04 -0500169 control_complete_init(control,invocation);
Norman James3f97c5d2015-08-26 17:44:14 -0500170 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -0500171}
172
173static gboolean
Norman James3f97c5d2015-08-26 17:44:14 -0500174on_get_power_state (ControlPower *pwr,
Norman Jamese2765102015-08-19 22:00:55 -0500175 GDBusMethodInvocation *invocation,
176 gpointer user_data)
177{
Norman James3f97c5d2015-08-26 17:44:14 -0500178 guint pgood = control_power_get_pgood(pwr);
179 control_power_complete_get_power_state(pwr,invocation,pgood);
180 return TRUE;
Norman Jamese2765102015-08-19 22:00:55 -0500181}
182
183static void
184on_bus_acquired (GDBusConnection *connection,
185 const gchar *name,
186 gpointer user_data)
187{
Norman James3f97c5d2015-08-26 17:44:14 -0500188 ObjectSkeleton *object;
Norman James10ff6a32015-08-27 14:24:17 -0500189 cmdline *cmd = user_data;
Norman James8fee6f22015-10-28 12:48:43 -0500190 if (cmd->argc < 3)
Norman James10ff6a32015-08-27 14:24:17 -0500191 {
Norman James8fee6f22015-10-28 12:48:43 -0500192 g_print("Usage: power_control.exe [poll interval] [timeout]\n");
Norman James10ff6a32015-08-27 14:24:17 -0500193 return;
194 }
195 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman James88872672015-09-21 16:51:35 -0500196 gchar *s;
Norman James8fee6f22015-10-28 12:48:43 -0500197 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
Norman James88872672015-09-21 16:51:35 -0500198 object = object_skeleton_new (s);
199 g_free (s);
Norman Jamese2765102015-08-19 22:00:55 -0500200
Norman James88872672015-09-21 16:51:35 -0500201 ControlPower* control_power = control_power_skeleton_new ();
202 object_skeleton_set_control_power (object, control_power);
203 g_object_unref (control_power);
204
205 Control* control = control_skeleton_new ();
206 object_skeleton_set_control (object, control);
207 g_object_unref (control);
Norman Jamese2765102015-08-19 22:00:55 -0500208
Norman Jamescfc2b442015-10-31 17:31:46 -0500209 ObjectMapper* mapper = object_mapper_skeleton_new ();
210 object_skeleton_set_object_mapper (object, mapper);
211 g_object_unref (mapper);
212
Norman James88872672015-09-21 16:51:35 -0500213 //define method callbacks here
214 g_signal_connect (control_power,
215 "handle-set-power-state",
216 G_CALLBACK (on_set_power_state),
217 object); /* user_data */
Norman Jamesce46e3e2015-08-30 22:25:55 -0500218
Norman James88872672015-09-21 16:51:35 -0500219 g_signal_connect (control_power,
220 "handle-get-power-state",
221 G_CALLBACK (on_get_power_state),
222 NULL); /* user_data */
Norman Jamese2765102015-08-19 22:00:55 -0500223
Norman James88872672015-09-21 16:51:35 -0500224 g_signal_connect (control,
225 "handle-init",
226 G_CALLBACK (on_init),
227 object); /* user_data */
Norman James9e6acf92015-09-08 07:00:04 -0500228
229
Norman James88872672015-09-21 16:51:35 -0500230 /* Export the object (@manager takes its own reference to @object) */
231 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
232 g_object_unref (object);
Norman Jamesce46e3e2015-08-30 22:25:55 -0500233
Norman James3f97c5d2015-08-26 17:44:14 -0500234 /* Export all objects */
235 g_dbus_object_manager_server_set_connection (manager, connection);
236
237 // get gpio device paths
Norman Jamesbd4abd12015-09-16 22:43:46 -0500238 int rc = GPIO_OK;
Norman James32e74e22015-09-15 21:28:06 -0500239 do {
Norman James32e74e22015-09-15 21:28:06 -0500240 rc = gpio_init(connection,&power_pin);
241 if (rc != GPIO_OK) { break; }
242 rc = gpio_init(connection,&pgood);
243 if (rc != GPIO_OK) { break; }
Norman Jamescfc2b442015-10-31 17:31:46 -0500244 rc = gpio_init(connection,&pcie_reset);
245 if (rc != GPIO_OK) { break; }
246 rc = gpio_init(connection,&usb_reset);
247 if (rc != GPIO_OK) { break; }
248
Norman James65a295a2015-09-26 22:21:10 -0500249 uint8_t gpio;
Norman James32e74e22015-09-15 21:28:06 -0500250 rc = gpio_open(&pgood);
Norman James65a295a2015-09-26 22:21:10 -0500251 if (rc != GPIO_OK) { break; }
252 rc = gpio_read(&pgood,&gpio);
253 if (rc != GPIO_OK) { break; }
254 gpio_close(&pgood);
255 control_power_set_pgood(control_power,gpio);
Norman James8fec6812015-11-24 22:18:22 -0600256 control_power_set_state(control_power,gpio);
Norman Jamesf66005a2015-10-08 15:11:44 -0500257 printf("Pgood state: %d\n",gpio);
Norman Jamescfc2b442015-10-31 17:31:46 -0500258
Norman James32e74e22015-09-15 21:28:06 -0500259 } while(0);
Norman Jamesbd4abd12015-09-16 22:43:46 -0500260 if (rc != GPIO_OK)
261 {
Norman James2891c532015-10-06 08:01:56 -0500262 printf("ERROR PowerControl: GPIO setup (rc=%d)\n",rc);
Norman James8fee6f22015-10-28 12:48:43 -0500263 }
264 //start poll
265 pgood_timeout_start = 0;
266 int poll_interval = atoi(cmd->argv[1]);
267 int pgood_timeout = atoi(cmd->argv[2]);
268 if (poll_interval < 1000 || pgood_timeout <5) {
269 printf("ERROR PowerControl: poll_interval < 1000 or pgood_timeout < 5\n");
270 } else {
271 control_set_poll_interval(control,poll_interval);
272 control_power_set_pgood_timeout(control_power,pgood_timeout);
273 g_timeout_add(poll_interval, poll_pgood, object);
274 }
Norman Jamescfc2b442015-10-31 17:31:46 -0500275 emit_object_added((GDBusObjectManager*)manager);
Norman Jamese2765102015-08-19 22:00:55 -0500276}
277
278static void
279on_name_acquired (GDBusConnection *connection,
280 const gchar *name,
281 gpointer user_data)
282{
Norman Jamese2765102015-08-19 22:00:55 -0500283}
284
285static void
286on_name_lost (GDBusConnection *connection,
287 const gchar *name,
288 gpointer user_data)
289{
Norman Jamese2765102015-08-19 22:00:55 -0500290}
291
Norman Jamese2765102015-08-19 22:00:55 -0500292
Norman James3f97c5d2015-08-26 17:44:14 -0500293
294
295/*----------------------------------------------------------------*/
296/* Main Event Loop */
297
Norman Jamese2765102015-08-19 22:00:55 -0500298gint
299main (gint argc, gchar *argv[])
300{
301 GMainLoop *loop;
Norman James90caa3c2015-08-27 21:28:48 -0500302 cmdline cmd;
303 cmd.argc = argc;
304 cmd.argv = argv;
Norman Jamese2765102015-08-19 22:00:55 -0500305
306 guint id;
Norman Jamese2765102015-08-19 22:00:55 -0500307 loop = g_main_loop_new (NULL, FALSE);
308
Norman James5e792e32015-10-07 17:36:17 -0500309 id = g_bus_own_name (DBUS_TYPE,
Norman James26072c02015-08-25 07:14:29 -0500310 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500311 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
312 G_BUS_NAME_OWNER_FLAGS_REPLACE,
313 on_bus_acquired,
314 on_name_acquired,
315 on_name_lost,
Norman James90caa3c2015-08-27 21:28:48 -0500316 &cmd,
Norman Jamese2765102015-08-19 22:00:55 -0500317 NULL);
318
Norman Jamesce46e3e2015-08-30 22:25:55 -0500319 g_main_loop_run (loop);
Norman Jamese2765102015-08-19 22:00:55 -0500320
321 g_bus_unown_name (id);
322 g_main_loop_unref (loop);
323 return 0;
324}