blob: ad183bcb1433ab448c002a8907da9d241313bcaa [file] [log] [blame]
Brad Bishop77390492016-04-13 10:47:19 -04001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <sys/stat.h>
7#include <sys/mman.h>
Brad Bishopf6c85682016-06-27 11:56:39 -04008#include <openbmc_intf.h>
9#include <openbmc.h>
10#include <gpio.h>
Brad Bishop77390492016-04-13 10:47:19 -040011
12/* ------------------------------------------------------------------------- */
13static const gchar* dbus_object_path = "/org/openbmc/control";
14static const gchar* instance_name = "host0";
15static const gchar* dbus_name = "org.openbmc.control.Host";
16
17static GDBusObjectManagerServer *manager = NULL;
18
19GPIO fsi_data = (GPIO){ "FSI_DATA" };
20GPIO fsi_clk = (GPIO){ "FSI_CLK" };
21GPIO fsi_enable = (GPIO){ "FSI_ENABLE" };
22GPIO cronus_sel = (GPIO){ "CRONUS_SEL" };
23GPIO Throttle = (GPIO){ "BMC_THROTTLE" };
Ken584747e2016-05-18 15:18:05 +080024GPIO idbtn = (GPIO){ "IDBTN" };
Brad Bishop77390492016-04-13 10:47:19 -040025
26/* Bit bang patterns */
27
28//putcfam pu 281c 30000000 -p0 (Primary Side Select)
29static const char* primary = "000011111111110101111000111001100111111111111111111111111111101111111111";
30//putcfam pu 281c B0000000 -p0
31static const char* go = "000011111111110101111000111000100111111111111111111111111111101101111111";
32//putcfam pu 0x281c 30900000 (Golden Side Select)
33static const char* golden = "000011111111110101111000111001100111101101111111111111111111101001111111";
34
35/* Setup attentions */
36//putcfam pu 0x081C 20000000
37static const char* attnA = "000011111111111101111110001001101111111111111111111111111111110001111111";
38//putcfam pu 0x100D 40000000
39static const char* attnB = "000011111111111011111100101001011111111111111111111111111111110001111111";
40//putcfam pu 0x100B FFFFFFFF
41static const char* attnC = "000011111111111011111101001000000000000000000000000000000000001011111111";
42
43
44
45static gboolean
46on_init(Control *control,
47 GDBusMethodInvocation *invocation,
48 gpointer user_data)
49{
50 control_complete_init(control,invocation);
51 return TRUE;
52}
53
54int
55fsi_bitbang(const char* pattern)
56{
57 int rc=GPIO_OK;
58 int i;
59 for(i=0;i<strlen(pattern);i++) {
60 rc = gpio_writec(&fsi_data,pattern[i]);
61 if(rc!=GPIO_OK) { break; }
62 rc = gpio_clock_cycle(&fsi_clk,1);
63 if(rc!=GPIO_OK) { break; }
64 }
65 return rc;
66}
67
68int
69fsi_standby()
70{
71 int rc=GPIO_OK;
72 rc = gpio_write(&fsi_data,1);
73 if(rc!=GPIO_OK) { return rc; }
74 rc = gpio_clock_cycle(&fsi_clk,5000);
75 if(rc!=GPIO_OK) { return rc; }
76 return rc;
77}
78
79
80static gboolean
81on_boot(ControlHost *host,
82 GDBusMethodInvocation *invocation,
83 gpointer user_data)
84{
85 int rc = GPIO_OK;
Adriana Kobylaka838b2f2016-08-04 13:47:47 -050086 GDBusProxy *proxy;
87 GError *error = NULL;
88 GVariant *result = NULL;
89 GDBusConnection *connection =
90 g_dbus_object_manager_server_get_connection(manager);
Brad Bishop77390492016-04-13 10:47:19 -040091
92 if(control_host_get_debug_mode(host)==1)
93 {
94 g_print("Enabling debug mode; not booting host\n");
95 rc |= gpio_open(&fsi_enable);
96 rc |= gpio_open(&cronus_sel);
97 rc |= gpio_write(&fsi_enable,1);
98 rc |= gpio_write(&cronus_sel,0);
99 if(rc!=GPIO_OK) {
100 g_print("ERROR enabling debug mode: %d\n",rc);
101 }
102 return TRUE;
103 }
104 g_print("Booting host\n");
105 Control* control = object_get_control((Object*)user_data);
106 control_host_complete_boot(host,invocation);
107 do {
108 rc = gpio_open(&fsi_clk);
109 rc |= gpio_open(&fsi_data);
110 rc |= gpio_open(&fsi_enable);
111 rc |= gpio_open(&cronus_sel);
112 rc |= gpio_open(&Throttle);
Ken584747e2016-05-18 15:18:05 +0800113 rc |= gpio_open(&idbtn);
Brad Bishop77390492016-04-13 10:47:19 -0400114 if(rc!=GPIO_OK) { break; }
115
116 //setup dc pins
117 rc = gpio_write(&cronus_sel,1);
118 rc |= gpio_write(&fsi_enable,1);
119 rc |= gpio_write(&fsi_clk,1);
120 rc |= gpio_write(&Throttle,1);
Ken584747e2016-05-18 15:18:05 +0800121 rc |= gpio_write(&idbtn,0);
Brad Bishop77390492016-04-13 10:47:19 -0400122 if(rc!=GPIO_OK) { break; }
123
124 //data standy state
125 rc = fsi_standby();
126
127 //clear out pipes
128 rc |= gpio_write(&fsi_data,0);
129 rc |= gpio_clock_cycle(&fsi_clk,256);
130 rc |= gpio_write(&fsi_data,1);
131 rc |= gpio_clock_cycle(&fsi_clk,50);
132 if(rc!=GPIO_OK) { break; }
133
134 rc = fsi_bitbang(attnA);
135 rc |= fsi_standby();
136
137 rc |= fsi_bitbang(attnB);
138 rc |= fsi_standby();
139
140 rc |= fsi_bitbang(attnC);
141 rc |= fsi_standby();
142 if(rc!=GPIO_OK) { break; }
143
144 const gchar* flash_side = control_host_get_flash_side(host);
145 g_print("Using %s side of the bios flash\n",flash_side);
146 if(strcmp(flash_side,"primary")==0) {
147 rc |= fsi_bitbang(primary);
148 } else if(strcmp(flash_side,"golden") == 0) {
149 rc |= fsi_bitbang(golden);
150 } else {
151 g_print("ERROR: Invalid flash side: %s\n",flash_side);
152 rc = 0xff;
153
154 }
155 rc |= fsi_standby();
156 if(rc!=GPIO_OK) { break; }
157
158 rc = fsi_bitbang(go);
159
160 rc |= gpio_write(&fsi_data,1); /* Data standby state */
161 rc |= gpio_clock_cycle(&fsi_clk,2);
162
163 rc |= gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
164 rc |= gpio_write(&fsi_enable,0);
165 rc |= gpio_clock_cycle(&fsi_clk,16);
166 rc |= gpio_write(&fsi_clk,0); /* Data standby state */
167
168 } while(0);
169 if(rc != GPIO_OK)
170 {
171 g_print("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
172 } else {
173 control_emit_goto_system_state(control,"HOST_BOOTING");
174 }
175 gpio_close(&fsi_clk);
176 gpio_close(&fsi_data);
177 gpio_close(&fsi_enable);
178 gpio_close(&cronus_sel);
179 gpio_close(&Throttle);
Ken584747e2016-05-18 15:18:05 +0800180 gpio_close(&idbtn);
Brad Bishop77390492016-04-13 10:47:19 -0400181
Adriana Kobylaka838b2f2016-08-04 13:47:47 -0500182 // Start watchdog with 30s timeout per the OpenPower Host IPMI Spec.
183 // Once the host starts booting, it'll reset and refresh the timer.
184 error = NULL;
185 proxy = g_dbus_proxy_new_sync(connection,
186 G_DBUS_PROXY_FLAGS_NONE,
187 NULL, /* GDBusInterfaceInfo* */
188 "org.openbmc.watchdog.Host", /* name */
189 "/org/openbmc/watchdog/host0", /* object path */
190 "org.openbmc.Watchdog", /* interface name */
191 NULL, /* GCancellable */
192 &error);
193 g_assert_no_error(error);
194 if(error)
195 goto exit;
196
197 // Set watchdog timer to 30s
198 error = NULL;
199 result = g_dbus_proxy_call_sync(proxy,
200 "set",
201 g_variant_new("(i)", 30000),
202 G_DBUS_CALL_FLAGS_NONE,
203 -1,
204 NULL,
205 &error);
206 g_assert_no_error(error);
207 if (error)
208 goto exit;
209 if (result)
210 g_variant_unref(result);
211
212 // Start watchdog timer
213 error = NULL;
214 result = g_dbus_proxy_call_sync(proxy,
215 "start",
216 NULL,
217 G_DBUS_CALL_FLAGS_NONE,
218 -1,
219 NULL,
220 &error);
221 g_assert_no_error(error);
222 if (error)
223 goto exit;
224
Brad Bishop77390492016-04-13 10:47:19 -0400225 control_host_emit_booted(host);
Adriana Kobylaka838b2f2016-08-04 13:47:47 -0500226
227exit:
228 if (result)
229 g_variant_unref(result);
230
Brad Bishop77390492016-04-13 10:47:19 -0400231 return TRUE;
232}
233
234static void
235on_bus_acquired(GDBusConnection *connection,
236 const gchar *name,
237 gpointer user_data)
238{
239 ObjectSkeleton *object;
240 //g_print ("Acquired a message bus connection: %s\n",name);
Brad Bishop77390492016-04-13 10:47:19 -0400241 manager = g_dbus_object_manager_server_new(dbus_object_path);
242
243 gchar *s;
244 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
245 object = object_skeleton_new(s);
246 g_free(s);
247
248 ControlHost* control_host = control_host_skeleton_new();
249 object_skeleton_set_control_host(object, control_host);
250 g_object_unref(control_host);
251
252 Control* control = control_skeleton_new();
253 object_skeleton_set_control(object, control);
254 g_object_unref(control);
255
Brad Bishop77390492016-04-13 10:47:19 -0400256 //define method callbacks here
257 g_signal_connect(control_host,
258 "handle-boot",
259 G_CALLBACK(on_boot),
260 object); /* user_data */
261 g_signal_connect(control,
262 "handle-init",
263 G_CALLBACK(on_init),
264 NULL); /* user_data */
265
266 control_host_set_debug_mode(control_host,0);
267 control_host_set_flash_side(control_host,"primary");
268
269 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400270 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400271 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
272 g_object_unref(object);
273
Brad Bishop77390492016-04-13 10:47:19 -0400274 gpio_init(connection,&fsi_data);
275 gpio_init(connection,&fsi_clk);
276 gpio_init(connection,&fsi_enable);
277 gpio_init(connection,&cronus_sel);
278 gpio_init(connection,&Throttle);
Ken584747e2016-05-18 15:18:05 +0800279 gpio_init(connection,&idbtn);
Brad Bishop77390492016-04-13 10:47:19 -0400280}
281
282static void
283on_name_acquired(GDBusConnection *connection,
284 const gchar *name,
285 gpointer user_data)
286{
287 // g_print ("Acquired the name %s\n", name);
288}
289
290static void
291on_name_lost(GDBusConnection *connection,
292 const gchar *name,
293 gpointer user_data)
294{
295 // g_print ("Lost the name %s\n", name);
296}
297
298gint
299main(gint argc, gchar *argv[])
300{
301 GMainLoop *loop;
302 cmdline cmd;
303 cmd.argc = argc;
304 cmd.argv = argv;
305
306 guint id;
307 loop = g_main_loop_new(NULL, FALSE);
308
309 id = g_bus_own_name(DBUS_TYPE,
310 dbus_name,
311 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,
316 &cmd,
317 NULL);
318
319 g_main_loop_run(loop);
320
321 g_bus_unown_name(id);
322 g_main_loop_unref(loop);
323 return 0;
324}