blob: 3e7f2aeae1bdca0c1843745911e65cbde51dff78 [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>
8#include "interfaces/openbmc_intf.h"
9#include "openbmc.h"
10#include "gpio.h"
11#include "object_mapper.h"
12
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
18static GDBusObjectManagerServer *manager = NULL;
19
20GPIO fsi_data = (GPIO){ "FSI_DATA" };
21GPIO fsi_clk = (GPIO){ "FSI_CLK" };
22GPIO fsi_enable = (GPIO){ "FSI_ENABLE" };
23GPIO cronus_sel = (GPIO){ "CRONUS_SEL" };
24GPIO Throttle = (GPIO){ "BMC_THROTTLE" };
25
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;
86
87 if(control_host_get_debug_mode(host)==1)
88 {
89 g_print("Enabling debug mode; not booting host\n");
90 rc |= gpio_open(&fsi_enable);
91 rc |= gpio_open(&cronus_sel);
92 rc |= gpio_write(&fsi_enable,1);
93 rc |= gpio_write(&cronus_sel,0);
94 if(rc!=GPIO_OK) {
95 g_print("ERROR enabling debug mode: %d\n",rc);
96 }
97 return TRUE;
98 }
99 g_print("Booting host\n");
100 Control* control = object_get_control((Object*)user_data);
101 control_host_complete_boot(host,invocation);
102 do {
103 rc = gpio_open(&fsi_clk);
104 rc |= gpio_open(&fsi_data);
105 rc |= gpio_open(&fsi_enable);
106 rc |= gpio_open(&cronus_sel);
107 rc |= gpio_open(&Throttle);
108 if(rc!=GPIO_OK) { break; }
109
110 //setup dc pins
111 rc = gpio_write(&cronus_sel,1);
112 rc |= gpio_write(&fsi_enable,1);
113 rc |= gpio_write(&fsi_clk,1);
114 rc |= gpio_write(&Throttle,1);
115 if(rc!=GPIO_OK) { break; }
116
117 //data standy state
118 rc = fsi_standby();
119
120 //clear out pipes
121 rc |= gpio_write(&fsi_data,0);
122 rc |= gpio_clock_cycle(&fsi_clk,256);
123 rc |= gpio_write(&fsi_data,1);
124 rc |= gpio_clock_cycle(&fsi_clk,50);
125 if(rc!=GPIO_OK) { break; }
126
127 rc = fsi_bitbang(attnA);
128 rc |= fsi_standby();
129
130 rc |= fsi_bitbang(attnB);
131 rc |= fsi_standby();
132
133 rc |= fsi_bitbang(attnC);
134 rc |= fsi_standby();
135 if(rc!=GPIO_OK) { break; }
136
137 const gchar* flash_side = control_host_get_flash_side(host);
138 g_print("Using %s side of the bios flash\n",flash_side);
139 if(strcmp(flash_side,"primary")==0) {
140 rc |= fsi_bitbang(primary);
141 } else if(strcmp(flash_side,"golden") == 0) {
142 rc |= fsi_bitbang(golden);
143 } else {
144 g_print("ERROR: Invalid flash side: %s\n",flash_side);
145 rc = 0xff;
146
147 }
148 rc |= fsi_standby();
149 if(rc!=GPIO_OK) { break; }
150
151 rc = fsi_bitbang(go);
152
153 rc |= gpio_write(&fsi_data,1); /* Data standby state */
154 rc |= gpio_clock_cycle(&fsi_clk,2);
155
156 rc |= gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
157 rc |= gpio_write(&fsi_enable,0);
158 rc |= gpio_clock_cycle(&fsi_clk,16);
159 rc |= gpio_write(&fsi_clk,0); /* Data standby state */
160
161 } while(0);
162 if(rc != GPIO_OK)
163 {
164 g_print("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
165 } else {
166 control_emit_goto_system_state(control,"HOST_BOOTING");
167 }
168 gpio_close(&fsi_clk);
169 gpio_close(&fsi_data);
170 gpio_close(&fsi_enable);
171 gpio_close(&cronus_sel);
172 gpio_close(&Throttle);
173
174 control_host_emit_booted(host);
175 return TRUE;
176}
177
178static void
179on_bus_acquired(GDBusConnection *connection,
180 const gchar *name,
181 gpointer user_data)
182{
183 ObjectSkeleton *object;
184 //g_print ("Acquired a message bus connection: %s\n",name);
185 cmdline *cmd = user_data;
186 manager = g_dbus_object_manager_server_new(dbus_object_path);
187
188 gchar *s;
189 s = g_strdup_printf("%s/%s",dbus_object_path,instance_name);
190 object = object_skeleton_new(s);
191 g_free(s);
192
193 ControlHost* control_host = control_host_skeleton_new();
194 object_skeleton_set_control_host(object, control_host);
195 g_object_unref(control_host);
196
197 Control* control = control_skeleton_new();
198 object_skeleton_set_control(object, control);
199 g_object_unref(control);
200
201 ObjectMapper* mapper = object_mapper_skeleton_new();
202 object_skeleton_set_object_mapper(object, mapper);
203 g_object_unref(mapper);
204
205 //define method callbacks here
206 g_signal_connect(control_host,
207 "handle-boot",
208 G_CALLBACK(on_boot),
209 object); /* user_data */
210 g_signal_connect(control,
211 "handle-init",
212 G_CALLBACK(on_init),
213 NULL); /* user_data */
214
215 control_host_set_debug_mode(control_host,0);
216 control_host_set_flash_side(control_host,"primary");
217
218 /* Export the object (@manager takes its own reference to @object) */
219 g_dbus_object_manager_server_export(manager, G_DBUS_OBJECT_SKELETON(object));
220 g_object_unref(object);
221
222 /* Export all objects */
223 g_dbus_object_manager_server_set_connection(manager, connection);
224
225 gpio_init(connection,&fsi_data);
226 gpio_init(connection,&fsi_clk);
227 gpio_init(connection,&fsi_enable);
228 gpio_init(connection,&cronus_sel);
229 gpio_init(connection,&Throttle);
230 emit_object_added((GDBusObjectManager*)manager);
231}
232
233static void
234on_name_acquired(GDBusConnection *connection,
235 const gchar *name,
236 gpointer user_data)
237{
238 // g_print ("Acquired the name %s\n", name);
239}
240
241static void
242on_name_lost(GDBusConnection *connection,
243 const gchar *name,
244 gpointer user_data)
245{
246 // g_print ("Lost the name %s\n", name);
247}
248
249gint
250main(gint argc, gchar *argv[])
251{
252 GMainLoop *loop;
253 cmdline cmd;
254 cmd.argc = argc;
255 cmd.argv = argv;
256
257 guint id;
258 loop = g_main_loop_new(NULL, FALSE);
259
260 id = g_bus_own_name(DBUS_TYPE,
261 dbus_name,
262 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
263 G_BUS_NAME_OWNER_FLAGS_REPLACE,
264 on_bus_acquired,
265 on_name_acquired,
266 on_name_lost,
267 &cmd,
268 NULL);
269
270 g_main_loop_run(loop);
271
272 g_bus_unown_name(id);
273 g_main_loop_unref(loop);
274 return 0;
275}