blob: ff9e640b1d212fdbc2d447e274b16ddf8c1a61c9 [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>
Lei YU45cb4fc2016-11-29 01:48:26 +080011#include <gpio_configs.h>
Brad Bishop77390492016-04-13 10:47:19 -040012
13/* ------------------------------------------------------------------------- */
14static const gchar* dbus_object_path = "/org/openbmc/control";
15static const gchar* instance_name = "host0";
16static const gchar* dbus_name = "org.openbmc.control.Host";
17
Lei YU75a18a22016-11-22 01:47:47 +080018static GpioConfigs g_gpio_configs;
19
Brad Bishop77390492016-04-13 10:47:19 -040020static GDBusObjectManagerServer *manager = NULL;
21
Lei YU75a18a22016-11-22 01:47:47 +080022static GPIO* fsi_data;
23static GPIO* fsi_clk;
24static GPIO* fsi_enable;
25static GPIO* cronus_sel;
26static size_t num_optionals;
27static GPIO* optionals;
28static gboolean* optional_pols;
Brad Bishop77390492016-04-13 10:47:19 -040029
30/* Bit bang patterns */
31
32//putcfam pu 281c 30000000 -p0 (Primary Side Select)
33static const char* primary = "000011111111110101111000111001100111111111111111111111111111101111111111";
34//putcfam pu 281c B0000000 -p0
35static const char* go = "000011111111110101111000111000100111111111111111111111111111101101111111";
36//putcfam pu 0x281c 30900000 (Golden Side Select)
37static const char* golden = "000011111111110101111000111001100111101101111111111111111111101001111111";
38
39/* Setup attentions */
40//putcfam pu 0x081C 20000000
41static const char* attnA = "000011111111111101111110001001101111111111111111111111111111110001111111";
42//putcfam pu 0x100D 40000000
43static const char* attnB = "000011111111111011111100101001011111111111111111111111111111110001111111";
Lei YU75a18a22016-11-22 01:47:47 +080044//putcfam pu 0x100B FFFFFFFF
Brad Bishop77390492016-04-13 10:47:19 -040045static const char* attnC = "000011111111111011111101001000000000000000000000000000000000001011111111";
46
47
48
49static gboolean
50on_init(Control *control,
51 GDBusMethodInvocation *invocation,
52 gpointer user_data)
53{
54 control_complete_init(control,invocation);
55 return TRUE;
56}
57
58int
59fsi_bitbang(const char* pattern)
60{
61 int rc=GPIO_OK;
62 int i;
63 for(i=0;i<strlen(pattern);i++) {
Lei YU75a18a22016-11-22 01:47:47 +080064 rc = gpio_writec(fsi_data,pattern[i]);
Brad Bishop77390492016-04-13 10:47:19 -040065 if(rc!=GPIO_OK) { break; }
Lei YU75a18a22016-11-22 01:47:47 +080066 rc = gpio_clock_cycle(fsi_clk,1);
Brad Bishop77390492016-04-13 10:47:19 -040067 if(rc!=GPIO_OK) { break; }
68 }
69 return rc;
70}
71
72int
73fsi_standby()
74{
75 int rc=GPIO_OK;
Lei YU75a18a22016-11-22 01:47:47 +080076 rc = gpio_write(fsi_data,1);
Brad Bishop77390492016-04-13 10:47:19 -040077 if(rc!=GPIO_OK) { return rc; }
Lei YU75a18a22016-11-22 01:47:47 +080078 rc = gpio_clock_cycle(fsi_clk,5000);
Brad Bishop77390492016-04-13 10:47:19 -040079 if(rc!=GPIO_OK) { return rc; }
80 return rc;
81}
82
83
84static gboolean
85on_boot(ControlHost *host,
86 GDBusMethodInvocation *invocation,
87 gpointer user_data)
88{
89 int rc = GPIO_OK;
Adriana Kobylak37846da2016-08-19 10:57:18 -050090 GDBusProxy *proxy;
91 GError *error = NULL;
Adriana Kobylak37846da2016-08-19 10:57:18 -050092 GDBusConnection *connection =
93 g_dbus_object_manager_server_get_connection(manager);
Brad Bishop77390492016-04-13 10:47:19 -040094
Lei YU75a18a22016-11-22 01:47:47 +080095 if (!(fsi_data && fsi_clk && fsi_enable && cronus_sel)) {
96 g_print("ERROR invalid GPIO configuration, will not boot\n");
97 return FALSE;
98 }
99 if(control_host_get_debug_mode(host)==1) {
Brad Bishop77390492016-04-13 10:47:19 -0400100 g_print("Enabling debug mode; not booting host\n");
Lei YU75a18a22016-11-22 01:47:47 +0800101 rc |= gpio_open(fsi_enable);
102 rc |= gpio_open(cronus_sel);
103 rc |= gpio_write(fsi_enable,1);
104 rc |= gpio_write(cronus_sel,0);
Brad Bishop77390492016-04-13 10:47:19 -0400105 if(rc!=GPIO_OK) {
106 g_print("ERROR enabling debug mode: %d\n",rc);
107 }
108 return TRUE;
109 }
110 g_print("Booting host\n");
111 Control* control = object_get_control((Object*)user_data);
112 control_host_complete_boot(host,invocation);
113 do {
Lei YU75a18a22016-11-22 01:47:47 +0800114 rc = gpio_open(fsi_clk);
115 rc |= gpio_open(fsi_data);
116 rc |= gpio_open(fsi_enable);
117 rc |= gpio_open(cronus_sel);
118 for (size_t i = 0; i < num_optionals; ++i) {
119 rc |= gpio_open(&optionals[i]);
120 }
Brad Bishop77390492016-04-13 10:47:19 -0400121 if(rc!=GPIO_OK) { break; }
122
123 //setup dc pins
Lei YU75a18a22016-11-22 01:47:47 +0800124 rc = gpio_write(cronus_sel,1);
125 rc |= gpio_write(fsi_enable,1);
126 rc |= gpio_write(fsi_clk,1);
127 for (size_t i = 0; i < num_optionals; ++i) {
128 rc |= gpio_write(&optionals[i], optional_pols[i]);
129 }
Brad Bishop77390492016-04-13 10:47:19 -0400130 if(rc!=GPIO_OK) { break; }
131
132 //data standy state
133 rc = fsi_standby();
134
135 //clear out pipes
Lei YU75a18a22016-11-22 01:47:47 +0800136 rc |= gpio_write(fsi_data,0);
137 rc |= gpio_clock_cycle(fsi_clk,256);
138 rc |= gpio_write(fsi_data,1);
139 rc |= gpio_clock_cycle(fsi_clk,50);
Brad Bishop77390492016-04-13 10:47:19 -0400140 if(rc!=GPIO_OK) { break; }
141
142 rc = fsi_bitbang(attnA);
143 rc |= fsi_standby();
144
145 rc |= fsi_bitbang(attnB);
146 rc |= fsi_standby();
147
148 rc |= fsi_bitbang(attnC);
149 rc |= fsi_standby();
150 if(rc!=GPIO_OK) { break; }
151
152 const gchar* flash_side = control_host_get_flash_side(host);
153 g_print("Using %s side of the bios flash\n",flash_side);
154 if(strcmp(flash_side,"primary")==0) {
155 rc |= fsi_bitbang(primary);
156 } else if(strcmp(flash_side,"golden") == 0) {
157 rc |= fsi_bitbang(golden);
158 } else {
159 g_print("ERROR: Invalid flash side: %s\n",flash_side);
160 rc = 0xff;
161
162 }
163 rc |= fsi_standby();
164 if(rc!=GPIO_OK) { break; }
165
166 rc = fsi_bitbang(go);
167
Lei YU75a18a22016-11-22 01:47:47 +0800168 rc |= gpio_write(fsi_data,1); /* Data standby state */
169 rc |= gpio_clock_cycle(fsi_clk,2);
Brad Bishop77390492016-04-13 10:47:19 -0400170
Lei YU75a18a22016-11-22 01:47:47 +0800171 rc |= gpio_write(fsi_clk,0); /* hold clk low for clock mux */
172 rc |= gpio_write(fsi_enable,0);
173 rc |= gpio_clock_cycle(fsi_clk,16);
174 rc |= gpio_write(fsi_clk,0); /* Data standby state */
Brad Bishop77390492016-04-13 10:47:19 -0400175
176 } while(0);
177 if(rc != GPIO_OK)
178 {
179 g_print("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
Andrew Geissler5987cac2018-02-05 13:56:52 -0800180 }
Lei YU75a18a22016-11-22 01:47:47 +0800181 gpio_close(fsi_clk);
182 gpio_close(fsi_data);
183 gpio_close(fsi_enable);
184 gpio_close(cronus_sel);
185 for (size_t i = 0; i < num_optionals; ++i) {
186 gpio_close(&optionals[i]);
187 }
Brad Bishop77390492016-04-13 10:47:19 -0400188
189 control_host_emit_booted(host);
Adriana Kobylak37846da2016-08-19 10:57:18 -0500190
Brad Bishop77390492016-04-13 10:47:19 -0400191 return TRUE;
192}
193
194static void
195on_bus_acquired(GDBusConnection *connection,
196 const gchar *name,
197 gpointer user_data)
198{
199 ObjectSkeleton *object;
200 //g_print ("Acquired a message bus connection: %s\n",name);
Brad Bishop77390492016-04-13 10:47:19 -0400201 manager = g_dbus_object_manager_server_new(dbus_object_path);
202
203 gchar *s;
204 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
205 object = object_skeleton_new(s);
206 g_free(s);
207
208 ControlHost* control_host = control_host_skeleton_new();
209 object_skeleton_set_control_host(object, control_host);
210 g_object_unref(control_host);
211
212 Control* control = control_skeleton_new();
213 object_skeleton_set_control(object, control);
214 g_object_unref(control);
215
Brad Bishop77390492016-04-13 10:47:19 -0400216 //define method callbacks here
217 g_signal_connect(control_host,
218 "handle-boot",
219 G_CALLBACK(on_boot),
220 object); /* user_data */
221 g_signal_connect(control,
222 "handle-init",
223 G_CALLBACK(on_init),
224 NULL); /* user_data */
225
226 control_host_set_debug_mode(control_host,0);
227 control_host_set_flash_side(control_host,"primary");
228
229 /* Export the object (@manager takes its own reference to @object) */
Brad Bishop58e694d2016-04-13 16:04:32 -0400230 g_dbus_object_manager_server_set_connection(manager, connection);
Brad Bishop77390492016-04-13 10:47:19 -0400231 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
232 g_object_unref(object);
233
Lei YU75a18a22016-11-22 01:47:47 +0800234 if(read_gpios(connection, &g_gpio_configs) != TRUE) {
235 g_print("ERROR Hostctl: could not read GPIO configuration\n");
236 return;
237 }
238
239 fsi_data = &g_gpio_configs.hostctl_gpio.fsi_data;
240 fsi_clk = &g_gpio_configs.hostctl_gpio.fsi_clk;
241 fsi_enable = &g_gpio_configs.hostctl_gpio.fsi_enable;
242 cronus_sel = &g_gpio_configs.hostctl_gpio.cronus_sel;
243 num_optionals = g_gpio_configs.hostctl_gpio.num_optionals;
244 optionals = g_gpio_configs.hostctl_gpio.optionals;
245 optional_pols = g_gpio_configs.hostctl_gpio.optional_pols;
246
247 gpio_init(connection, fsi_data);
248 gpio_init(connection, fsi_clk);
249 gpio_init(connection, fsi_enable);
250 gpio_init(connection, cronus_sel);
251 for (int i = 0; i < num_optionals; ++i) {
252 gpio_init(connection, &optionals[i]);
253 }
Brad Bishop77390492016-04-13 10:47:19 -0400254}
255
256static void
257on_name_acquired(GDBusConnection *connection,
258 const gchar *name,
259 gpointer user_data)
260{
Lei YU75a18a22016-11-22 01:47:47 +0800261 // g_print ("Acquired the name %s\n", name);
Brad Bishop77390492016-04-13 10:47:19 -0400262}
263
264static void
265on_name_lost(GDBusConnection *connection,
266 const gchar *name,
267 gpointer user_data)
268{
Lei YU75a18a22016-11-22 01:47:47 +0800269 // g_print ("Lost the name %s\n", name);
270 free_gpios(&g_gpio_configs);
Brad Bishop77390492016-04-13 10:47:19 -0400271}
272
273gint
274main(gint argc, gchar *argv[])
275{
276 GMainLoop *loop;
277 cmdline cmd;
278 cmd.argc = argc;
279 cmd.argv = argv;
280
281 guint id;
282 loop = g_main_loop_new(NULL, FALSE);
283
284 id = g_bus_own_name(DBUS_TYPE,
285 dbus_name,
286 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
287 G_BUS_NAME_OWNER_FLAGS_REPLACE,
288 on_bus_acquired,
289 on_name_acquired,
290 on_name_lost,
291 &cmd,
292 NULL);
293
294 g_main_loop_run(loop);
295
296 g_bus_unown_name(id);
297 g_main_loop_unref(loop);
298 return 0;
299}