summaryrefslogtreecommitdiffstats
path: root/op-hostctl/control_host_obj.c
blob: c0b4e1269814ac32f87c10b7b561ef663307d172 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <openbmc_intf.h>
#include <openbmc.h>
#include <gpio.h>
#include <power_gpio.h>

/* ------------------------------------------------------------------------- */
static const gchar* dbus_object_path = "/org/openbmc/control";
static const gchar* instance_name = "host0";
static const gchar* dbus_name = "org.openbmc.control.Host";

static GpioConfigs g_gpio_configs;

static GDBusObjectManagerServer *manager = NULL;

static GPIO* fsi_data;
static GPIO* fsi_clk;
static GPIO* fsi_enable;
static GPIO* cronus_sel;
static size_t num_optionals;
static GPIO* optionals;
static gboolean* optional_pols;

/* Bit bang patterns */

//putcfam pu 281c 30000000 -p0 (Primary Side Select)
static const char* primary = "000011111111110101111000111001100111111111111111111111111111101111111111";
//putcfam pu 281c B0000000 -p0
static const char* go = "000011111111110101111000111000100111111111111111111111111111101101111111";
//putcfam pu 0x281c 30900000 (Golden Side Select)
static const char* golden = "000011111111110101111000111001100111101101111111111111111111101001111111";

/* Setup attentions */
//putcfam pu 0x081C 20000000
static const char* attnA = "000011111111111101111110001001101111111111111111111111111111110001111111";
//putcfam pu 0x100D 40000000
static const char* attnB = "000011111111111011111100101001011111111111111111111111111111110001111111";
//putcfam pu 0x100B FFFFFFFF
static const char* attnC = "000011111111111011111101001000000000000000000000000000000000001011111111";



static gboolean
on_init(Control *control,
		GDBusMethodInvocation *invocation,
		gpointer user_data)
{
	control_complete_init(control,invocation);
	return TRUE;
}

int
fsi_bitbang(const char* pattern)
{
	int rc=GPIO_OK;
	int i;
	for(i=0;i<strlen(pattern);i++) {
		rc = gpio_writec(fsi_data,pattern[i]);
		if(rc!=GPIO_OK) { break; }
		rc = gpio_clock_cycle(fsi_clk,1);
		if(rc!=GPIO_OK) { break; }
	}
	return rc;
}

int
fsi_standby()
{
	int rc=GPIO_OK;
	rc = gpio_write(fsi_data,1);
	if(rc!=GPIO_OK) { return rc; }
	rc = gpio_clock_cycle(fsi_clk,5000);
	if(rc!=GPIO_OK) { return rc; }
	return rc;
}


static gboolean
on_boot(ControlHost *host,
		GDBusMethodInvocation *invocation,
		gpointer user_data)
{
	int rc = GPIO_OK;
	GDBusProxy *proxy;
	GError *error = NULL;
	GVariant *result = NULL;
	GDBusConnection *connection =
		g_dbus_object_manager_server_get_connection(manager);

	if (!(fsi_data && fsi_clk && fsi_enable && cronus_sel)) {
		g_print("ERROR invalid GPIO configuration, will not boot\n");
		return FALSE;
	}
	if(control_host_get_debug_mode(host)==1) {
		g_print("Enabling debug mode; not booting host\n");
		rc |= gpio_open(fsi_enable);
		rc |= gpio_open(cronus_sel);
		rc |= gpio_write(fsi_enable,1);
		rc |= gpio_write(cronus_sel,0);
		if(rc!=GPIO_OK) {
			g_print("ERROR enabling debug mode: %d\n",rc);
		}
		return TRUE;
	}
	g_print("Booting host\n");
	Control* control = object_get_control((Object*)user_data);
	control_host_complete_boot(host,invocation);
	do {
		rc = gpio_open(fsi_clk);
		rc |= gpio_open(fsi_data);
		rc |= gpio_open(fsi_enable);
		rc |= gpio_open(cronus_sel);
		for (size_t i = 0; i < num_optionals; ++i) {
			rc |= gpio_open(&optionals[i]);
		}
		if(rc!=GPIO_OK) { break; }

		//setup dc pins
		rc = gpio_write(cronus_sel,1);
		rc |= gpio_write(fsi_enable,1);
		rc |= gpio_write(fsi_clk,1);
		for (size_t i = 0; i < num_optionals; ++i) {
			rc |= gpio_write(&optionals[i], optional_pols[i]);
		}
		if(rc!=GPIO_OK) { break; }

		//data standy state
		rc = fsi_standby();

		//clear out pipes
		rc |= gpio_write(fsi_data,0);
		rc |= gpio_clock_cycle(fsi_clk,256);
		rc |= gpio_write(fsi_data,1);
		rc |= gpio_clock_cycle(fsi_clk,50);
		if(rc!=GPIO_OK) { break; }

		rc = fsi_bitbang(attnA);
		rc |= fsi_standby();

		rc |= fsi_bitbang(attnB);
		rc |= fsi_standby();

		rc |= fsi_bitbang(attnC);
		rc |= fsi_standby();
		if(rc!=GPIO_OK) { break; }

		const gchar* flash_side = control_host_get_flash_side(host);
		g_print("Using %s side of the bios flash\n",flash_side);
		if(strcmp(flash_side,"primary")==0) {
			rc |= fsi_bitbang(primary);
		} else if(strcmp(flash_side,"golden") == 0) {
			rc |= fsi_bitbang(golden);
		} else {
			g_print("ERROR: Invalid flash side: %s\n",flash_side);
			rc = 0xff;

		}
		rc |= fsi_standby();
		if(rc!=GPIO_OK) { break; }

		rc = fsi_bitbang(go);

		rc |= gpio_write(fsi_data,1); /* Data standby state */
		rc |= gpio_clock_cycle(fsi_clk,2);

		rc |= gpio_write(fsi_clk,0); /* hold clk low for clock mux */
		rc |= gpio_write(fsi_enable,0);
		rc |= gpio_clock_cycle(fsi_clk,16);
		rc |= gpio_write(fsi_clk,0); /* Data standby state */

	} while(0);
	if(rc != GPIO_OK)
	{
		g_print("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
	} else {
		control_emit_goto_system_state(control,"HOST_BOOTING");
	}
	gpio_close(fsi_clk);
	gpio_close(fsi_data);
	gpio_close(fsi_enable);
	gpio_close(cronus_sel);
	for (size_t i = 0; i < num_optionals; ++i) {
		gpio_close(&optionals[i]);
	}

	// Start watchdog with 30s timeout per the OpenPower Host IPMI Spec.
	// Once the host starts booting, it'll reset and refresh the timer.
	error = NULL;
	// TODO Use the object mapper to lookup the bus name when this is
	// refactored to use sdbus.
	proxy = g_dbus_proxy_new_sync(connection,
		G_DBUS_PROXY_FLAGS_NONE,
		NULL, /* GDBusInterfaceInfo* */
		"org.openbmc.watchdog.Host", /* name */
		"/org/openbmc/watchdog/host0", /* object path */
		"org.openbmc.Watchdog", /* interface name */
		NULL, /* GCancellable */
		&error);
	g_assert_no_error(error);
	if(error)
		goto exit;

	// Set watchdog timer to 30s
	error = NULL;
	result = g_dbus_proxy_call_sync(proxy,
		"set",
		g_variant_new("(i)", 30000),
		G_DBUS_CALL_FLAGS_NONE,
		-1,
		NULL,
		&error);
	g_assert_no_error(error);
	if (error)
		goto exit;
	if (result)
		g_variant_unref(result);

	// Start watchdog timer
	error = NULL;
	result = g_dbus_proxy_call_sync(proxy,
		"start",
		NULL,
		G_DBUS_CALL_FLAGS_NONE,
		-1,
		NULL,
		&error);
	g_assert_no_error(error);
	if (error)
		goto exit;

	control_host_emit_booted(host);

exit:
	if (result)
		g_variant_unref(result);

	return TRUE;
}

static void
on_bus_acquired(GDBusConnection *connection,
		const gchar *name,
		gpointer user_data)
{
	ObjectSkeleton *object;
	//g_print ("Acquired a message bus connection: %s\n",name);
	manager = g_dbus_object_manager_server_new(dbus_object_path);

	gchar *s;
	s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
	object = object_skeleton_new(s);
	g_free(s);

	ControlHost* control_host = control_host_skeleton_new();
	object_skeleton_set_control_host(object, control_host);
	g_object_unref(control_host);

	Control* control = control_skeleton_new();
	object_skeleton_set_control(object, control);
	g_object_unref(control);

	//define method callbacks here
	g_signal_connect(control_host,
			"handle-boot",
			G_CALLBACK(on_boot),
			object); /* user_data */
	g_signal_connect(control,
			"handle-init",
			G_CALLBACK(on_init),
			NULL); /* user_data */

	control_host_set_debug_mode(control_host,0);
	control_host_set_flash_side(control_host,"primary");

	/* Export the object (@manager takes its own reference to @object) */
	g_dbus_object_manager_server_set_connection(manager, connection);
	g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
	g_object_unref(object);

	if(read_gpios(connection, &g_gpio_configs) != TRUE) {
		g_print("ERROR Hostctl: could not read GPIO configuration\n");
		return;
	}

	fsi_data = &g_gpio_configs.hostctl_gpio.fsi_data;
	fsi_clk = &g_gpio_configs.hostctl_gpio.fsi_clk;
	fsi_enable = &g_gpio_configs.hostctl_gpio.fsi_enable;
	cronus_sel = &g_gpio_configs.hostctl_gpio.cronus_sel;
	num_optionals = g_gpio_configs.hostctl_gpio.num_optionals;
	optionals = g_gpio_configs.hostctl_gpio.optionals;
	optional_pols = g_gpio_configs.hostctl_gpio.optional_pols;

	gpio_init(connection, fsi_data);
	gpio_init(connection, fsi_clk);
	gpio_init(connection, fsi_enable);
	gpio_init(connection, cronus_sel);
	for (int i = 0; i < num_optionals; ++i) {
		gpio_init(connection, &optionals[i]);
	}
}

static void
on_name_acquired(GDBusConnection *connection,
		const gchar *name,
		gpointer user_data)
{
	// g_print ("Acquired the name %s\n", name);
}

static void
on_name_lost(GDBusConnection *connection,
		const gchar *name,
		gpointer user_data)
{
	// g_print ("Lost the name %s\n", name);
	free_gpios(&g_gpio_configs);
}

gint
main(gint argc, gchar *argv[])
{
	GMainLoop *loop;
	cmdline cmd;
	cmd.argc = argc;
	cmd.argv = argv;

	guint id;
	loop = g_main_loop_new(NULL, FALSE);

	id = g_bus_own_name(DBUS_TYPE,
			dbus_name,
			G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
			G_BUS_NAME_OWNER_FLAGS_REPLACE,
			on_bus_acquired,
			on_name_acquired,
			on_name_lost,
			&cmd,
			NULL);

	g_main_loop_run(loop);

	g_bus_unown_name(id);
	g_main_loop_unref(loop);
	return 0;
}
OpenPOWER on IntegriCloud