blob: 4dacbe92e69f06b94763926c729f3c6d88646986 [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 Jamese2765102015-08-19 22:00:55 -05009#include "interfaces/power_control.h"
10
11/* ---------------------------------------------------------------------------------------------------- */
Norman James26072c02015-08-25 07:14:29 -050012static const gchar* dbus_object_path = "/org/openbmc/control/Power";
13static const gchar* dbus_name = "org.openbmc.control.Power";
14
15// Platform specific config
16
17#define FSI_CLK 4 //GPIOA4
18#define FSI_DAT 5 //GPIOA5
19#define CRONUS_SEL 6 //GPIOA6
20#define PCIE_RST_N 13 //GPIOB5
21#define PEX_PERST_N 14 //GPIOB6
22#define POWER 33 //GPIOE1
23#define PGOOD 23 //GPIOC7
24#define FSI_ENABLE 24 //GPIOD0
25
26/* Where to put the firmware image if booting from memory */
27#define MEM_IMG_BASE (0x54000000)
28
29/* Start of flash memory if booting from flash */
30#define FLASH_IMG_BASE (0x30000000)
31
32/* LPC registers */
33#define LPC_BASE 0x1e789000
34#define LPC_HICR6 0x80
35#define LPC_HICR7 0x88
36#define LPC_HICR8 0x8c
37
38
39
Norman Jamese2765102015-08-19 22:00:55 -050040
41static GDBusObjectManagerServer *manager = NULL;
42static PowerControl *power_control = NULL;
43
Norman Jamese2765102015-08-19 22:00:55 -050044static guint tmp_pgood = 0;
45
46static gboolean
47on_set_power_state (PowerControl *pwr,
48 GDBusMethodInvocation *invocation,
49 guint state,
50 gpointer user_data)
51{
52 if (state > 1)
53 {
54 g_dbus_method_invocation_return_dbus_error (invocation,
55 "org.openbmc.PowerControl.Error.Failed",
56 "Invalid power state");
57 return TRUE;
58 }
59 if (state == power_control_get_state(pwr))
60 {
61 g_dbus_method_invocation_return_dbus_error (invocation,
62 "org.openbmc.PowerControl.Error.Failed",
63 "Power Control is already at requested state");
64 return TRUE;
65 }
66
67 // TODO: Implement gpio toggling
68 g_print("Set power state: %d\n",state);
69 if (state==1)
70 {
71 g_print("Turn on\n");
72 tmp_pgood=1;
73 }
74 else
75 {
76 g_print("Turn off\n");
77 tmp_pgood=0;
78 }
79 power_control_set_state(pwr,state);
80 power_control_complete_set_power_state(pwr,invocation);
81 return TRUE;
82}
83
84static gboolean
85on_get_power_state (PowerControl *pwr,
86 GDBusMethodInvocation *invocation,
87 gpointer user_data)
88{
89 guint pgood = power_control_get_pgood(pwr);
90 power_control_complete_get_power_state(pwr,invocation,pgood);
91 return TRUE;
92}
93
94static void
95on_bus_acquired (GDBusConnection *connection,
96 const gchar *name,
97 gpointer user_data)
98{
99 ObjectSkeleton *object;
100 guint n;
101
102 g_print ("Acquired a message bus connection: %s\n",name);
103
Norman James26072c02015-08-25 07:14:29 -0500104 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman Jamese2765102015-08-19 22:00:55 -0500105
106 gchar *s;
Norman James26072c02015-08-25 07:14:29 -0500107 s = g_strdup_printf ("%s/0",dbus_object_path);
Norman Jamese2765102015-08-19 22:00:55 -0500108 object = object_skeleton_new (s);
109 g_free (s);
110
111 power_control = power_control_skeleton_new ();
112 object_skeleton_set_power_control (object, power_control);
113 g_object_unref (power_control);
114
115 //define method callbacks here
116 g_signal_connect (power_control,
117 "handle-set-power-state",
118 G_CALLBACK (on_set_power_state),
119 NULL); /* user_data */
120
121 g_signal_connect (power_control,
122 "handle-get-power-state",
123 G_CALLBACK (on_get_power_state),
124 NULL); /* user_data */
125
126 /* Export the object (@manager takes its own reference to @object) */
127 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
128 g_object_unref (object);
129
130 /* Export all objects */
131 g_dbus_object_manager_server_set_connection (manager, connection);
132}
133
134static void
135on_name_acquired (GDBusConnection *connection,
136 const gchar *name,
137 gpointer user_data)
138{
139 g_print ("Acquired the name %s\n", name);
140}
141
142static void
143on_name_lost (GDBusConnection *connection,
144 const gchar *name,
145 gpointer user_data)
146{
147 g_print ("Lost the name %s\n", name);
148}
149
150static gboolean
151poll_pgood()
152{
153 guint pgood = power_control_get_pgood(power_control);
154 //TOOD: Change to actually read gpio
155 guint gpio = tmp_pgood;
156
157 g_print("Polling pgood: %d\n",gpio);
158
159 //if changed, set property and emit signal
160 if (gpio != power_control_get_pgood(power_control))
161 {
162 power_control_set_pgood(power_control,gpio);
163 if (gpio==0)
164 {
165 power_control_emit_power_lost(power_control);
166 }
167 else
168 {
169 power_control_emit_power_good(power_control);
170 }
171 }
172 return TRUE;
173}
174
175gint
176main (gint argc, gchar *argv[])
177{
178 GMainLoop *loop;
179
180 guint id;
181 //g_type_init ();
182 loop = g_main_loop_new (NULL, FALSE);
183
184 id = g_bus_own_name (G_BUS_TYPE_SESSION,
Norman James26072c02015-08-25 07:14:29 -0500185 dbus_name,
Norman Jamese2765102015-08-19 22:00:55 -0500186 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
187 G_BUS_NAME_OWNER_FLAGS_REPLACE,
188 on_bus_acquired,
189 on_name_acquired,
190 on_name_lost,
191 loop,
192 NULL);
193
194 g_timeout_add(5000, poll_pgood, NULL);
195 g_main_loop_run (loop);
196
197 g_bus_unown_name (id);
198 g_main_loop_unref (loop);
199 return 0;
200}