Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 1 | #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>
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 8 | #include "interfaces/openbmc_intf.h"
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 9 | #include "openbmc.h"
|
| 10 | #include "gpio.h"
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 11 | #include "object_mapper.h"
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 12 |
|
| 13 | /* ---------------------------------------------------------------------------------------------------- */
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 14 | static const gchar* dbus_object_path = "/org/openbmc/control";
|
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 15 | static const gchar* instance_name = "host0";
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 16 | static const gchar* dbus_name = "org.openbmc.control.Host";
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 17 |
|
| 18 | static GDBusObjectManagerServer *manager = NULL;
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 19 |
|
| 20 | GPIO fsi_data = (GPIO){ "FSI_DATA" };
|
| 21 | GPIO fsi_clk = (GPIO){ "FSI_CLK" };
|
| 22 | GPIO fsi_enable = (GPIO){ "FSI_ENABLE" };
|
| 23 | GPIO cronus_sel = (GPIO){ "CRONUS_SEL" };
|
Ken | 2fd67f9 | 2015-12-09 01:41:23 +0800 | [diff] [blame^] | 24 | GPIO Throttle = (GPIO){ "BMC_TROTTLE" };
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 25 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 26 | /* Bit bang patterns */
|
| 27 |
|
| 28 | //putcfam pu 281c 30000000 -p0 (Primary Side Select)
|
| 29 | static const char* primary = "000011111111110101111000111001100111111111111111111111111111101111111111";
|
| 30 | //putcfam pu 281c B0000000 -p0
|
| 31 | static const char* go = "000011111111110101111000111000100111111111111111111111111111101101111111";
|
| 32 | //putcfam pu 0x281c 30900000 (Golden Side Select)
|
| 33 | static const char* golden = "000011111111110101111000111001100111101101111111111111111111101001111111";
|
| 34 |
|
| 35 | /* Setup attentions */
|
| 36 | //putcfam pu 0x081C 20000000
|
| 37 | static const char* attnA = "000011111111111101111110001001101111111111111111111111111111110001111111";
|
| 38 | //putcfam pu 0x100D 40000000
|
| 39 | static const char* attnB = "000011111111111011111100101001011111111111111111111111111111110001111111";
|
| 40 | //putcfam pu 0x100B FFFFFFFF
|
| 41 | static const char* attnC = "000011111111111011111101001000000000000000000000000000000000001011111111";
|
| 42 |
|
| 43 |
|
| 44 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 45 | static gboolean
|
| 46 | on_init (Control *control,
|
| 47 | GDBusMethodInvocation *invocation,
|
| 48 | gpointer user_data)
|
| 49 | {
|
| 50 | control_complete_init(control,invocation);
|
| 51 | return TRUE;
|
| 52 | }
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 53 |
|
| 54 | int fsi_bitbang(const char* pattern)
|
| 55 | {
|
| 56 | int rc=GPIO_OK;
|
| 57 | int i;
|
| 58 | for(i=0;i<strlen(pattern);i++) {
|
| 59 | rc = gpio_writec(&fsi_data,pattern[i]);
|
| 60 | if (rc!=GPIO_OK) { break; }
|
| 61 | rc = gpio_clock_cycle(&fsi_clk,1);
|
| 62 | if (rc!=GPIO_OK) { break; }
|
| 63 | }
|
| 64 | return rc;
|
| 65 |
|
| 66 | }
|
| 67 |
|
| 68 | int fsi_standby()
|
| 69 | {
|
| 70 | int rc=GPIO_OK;
|
| 71 | rc = gpio_write(&fsi_data,1);
|
| 72 | if (rc!=GPIO_OK) { return rc; }
|
| 73 | rc = gpio_clock_cycle(&fsi_clk,5000);
|
| 74 | if (rc!=GPIO_OK) { return rc; }
|
| 75 | return rc;
|
| 76 | }
|
| 77 |
|
| 78 |
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 79 | static gboolean
|
| 80 | on_boot (ControlHost *host,
|
| 81 | GDBusMethodInvocation *invocation,
|
| 82 | gpointer user_data)
|
| 83 | {
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 84 | int rc = GPIO_OK;
|
| 85 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 86 | if (control_host_get_debug_mode(host)==1)
|
| 87 | {
|
| 88 | g_print("Enabling debug mode; not booting host\n");
|
| 89 | rc |= gpio_open(&fsi_enable);
|
| 90 | rc |= gpio_open(&cronus_sel);
|
| 91 | rc |= gpio_write(&fsi_enable,1);
|
| 92 | rc |= gpio_write(&cronus_sel,0);
|
| 93 | if (rc!=GPIO_OK) {
|
| 94 | g_print("ERROR enabling debug mode: %d\n",rc);
|
| 95 | }
|
| 96 | return TRUE;
|
| 97 | }
|
| 98 | g_print("Booting host\n");
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 99 | Control* control = object_get_control((Object*)user_data);
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 100 | control_host_complete_boot(host,invocation);
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 101 | do {
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 102 | rc = gpio_open(&fsi_clk);
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 103 | rc |= gpio_open(&fsi_data);
|
| 104 | rc |= gpio_open(&fsi_enable);
|
| 105 | rc |= gpio_open(&cronus_sel);
|
Ken | 2fd67f9 | 2015-12-09 01:41:23 +0800 | [diff] [blame^] | 106 | rc |= gpio_open(&Throttle);
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 107 | if (rc!=GPIO_OK) { break; }
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 108 |
|
| 109 | //setup dc pins
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 110 | rc = gpio_write(&cronus_sel,1);
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 111 | rc |= gpio_write(&fsi_enable,1);
|
| 112 | rc |= gpio_write(&fsi_clk,1);
|
Ken | 2fd67f9 | 2015-12-09 01:41:23 +0800 | [diff] [blame^] | 113 | rc |= gpio_write(&Throttle,1);
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 114 | if (rc!=GPIO_OK) { break; }
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 115 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 116 | //data standy state
|
| 117 | rc = fsi_standby();
|
| 118 |
|
| 119 | //clear out pipes
|
| 120 | rc |= gpio_write(&fsi_data,0);
|
| 121 | rc |= gpio_clock_cycle(&fsi_clk,256);
|
| 122 | rc |= gpio_write(&fsi_data,1);
|
| 123 | rc |= gpio_clock_cycle(&fsi_clk,50);
|
| 124 | if (rc!=GPIO_OK) { break; }
|
| 125 |
|
| 126 | rc = fsi_bitbang(attnA);
|
| 127 | rc |= fsi_standby();
|
| 128 |
|
| 129 | rc |= fsi_bitbang(attnB);
|
| 130 | rc |= fsi_standby();
|
| 131 |
|
| 132 | rc |= fsi_bitbang(attnC);
|
| 133 | rc |= fsi_standby();
|
| 134 | if (rc!=GPIO_OK) { break; }
|
| 135 |
|
| 136 | const gchar* flash_side = control_host_get_flash_side(host);
|
| 137 | g_print("Using %s side of the bios flash\n",flash_side);
|
| 138 | if (strcmp(flash_side,"primary")==0) {
|
| 139 | rc |= fsi_bitbang(primary);
|
| 140 | } else if (strcmp(flash_side,"golden") == 0) {
|
| 141 | rc |= fsi_bitbang(golden);
|
| 142 | } else {
|
| 143 | g_print("ERROR: Invalid flash side: %s\n",flash_side);
|
| 144 | rc = 0xff;
|
| 145 |
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 146 | }
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 147 | rc |= fsi_standby();
|
| 148 | if (rc!=GPIO_OK) { break; }
|
| 149 |
|
| 150 | rc = fsi_bitbang(go);
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 151 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 152 | rc |= gpio_write(&fsi_data,1); /* Data standby state */
|
| 153 | rc |= gpio_clock_cycle(&fsi_clk,2);
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 154 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 155 | rc |= gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
|
| 156 | rc |= gpio_write(&fsi_enable,0);
|
| 157 | rc |= gpio_clock_cycle(&fsi_clk,16);
|
| 158 | rc |= gpio_write(&fsi_clk,0); /* Data standby state */
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 159 |
|
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 160 | } while(0);
|
| 161 | if (rc != GPIO_OK)
|
| 162 | {
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 163 | g_print("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
|
| 164 | } else {
|
| 165 | control_emit_goto_system_state(control,"HOST_BOOTING");
|
| 166 | }
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 167 | gpio_close(&fsi_clk);
|
| 168 | gpio_close(&fsi_data);
|
| 169 | gpio_close(&fsi_enable);
|
| 170 | gpio_close(&cronus_sel);
|
Ken | 2fd67f9 | 2015-12-09 01:41:23 +0800 | [diff] [blame^] | 171 | gpio_close(&Throttle);
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 172 |
|
| 173 | control_host_emit_booted(host);
|
| 174 | return TRUE;
|
| 175 | }
|
| 176 |
|
| 177 | static void
|
| 178 | on_bus_acquired (GDBusConnection *connection,
|
| 179 | const gchar *name,
|
| 180 | gpointer user_data)
|
| 181 | {
|
| 182 | ObjectSkeleton *object;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 183 | //g_print ("Acquired a message bus connection: %s\n",name);
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 184 | cmdline *cmd = user_data;
|
Norman James | 10ff6a3 | 2015-08-27 14:24:17 -0500 | [diff] [blame] | 185 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 186 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 187 | gchar *s;
|
| 188 | s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
|
| 189 | object = object_skeleton_new (s);
|
| 190 | g_free (s);
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 191 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 192 | ControlHost* control_host = control_host_skeleton_new ();
|
| 193 | object_skeleton_set_control_host (object, control_host);
|
| 194 | g_object_unref (control_host);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 195 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 196 | Control* control = control_skeleton_new ();
|
| 197 | object_skeleton_set_control (object, control);
|
| 198 | g_object_unref (control);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 199 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 200 | ObjectMapper* mapper = object_mapper_skeleton_new ();
|
| 201 | object_skeleton_set_object_mapper (object, mapper);
|
| 202 | g_object_unref (mapper);
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 203 |
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 204 | //define method callbacks here
|
| 205 | g_signal_connect (control_host,
|
| 206 | "handle-boot",
|
| 207 | G_CALLBACK (on_boot),
|
| 208 | object); /* user_data */
|
| 209 | g_signal_connect (control,
|
| 210 | "handle-init",
|
| 211 | G_CALLBACK (on_init),
|
| 212 | NULL); /* user_data */
|
| 213 |
|
| 214 | control_host_set_debug_mode(control_host,0);
|
| 215 | control_host_set_flash_side(control_host,"primary");
|
| 216 |
|
| 217 | /* Export the object (@manager takes its own reference to @object) */
|
| 218 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 219 | g_object_unref (object);
|
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 220 |
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 221 | /* Export all objects */
|
| 222 | g_dbus_object_manager_server_set_connection (manager, connection);
|
| 223 |
|
| 224 | gpio_init(connection,&fsi_data);
|
| 225 | gpio_init(connection,&fsi_clk);
|
| 226 | gpio_init(connection,&fsi_enable);
|
| 227 | gpio_init(connection,&cronus_sel);
|
Ken | 2fd67f9 | 2015-12-09 01:41:23 +0800 | [diff] [blame^] | 228 | gpio_init(connection,&Throttle);
|
Norman James | f378e3f | 2015-10-31 17:36:43 -0500 | [diff] [blame] | 229 | emit_object_added((GDBusObjectManager*)manager);
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 230 | }
|
| 231 |
|
| 232 | static void
|
| 233 | on_name_acquired (GDBusConnection *connection,
|
| 234 | const gchar *name,
|
| 235 | gpointer user_data)
|
| 236 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 237 | // g_print ("Acquired the name %s\n", name);
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 238 | }
|
| 239 |
|
| 240 | static void
|
| 241 | on_name_lost (GDBusConnection *connection,
|
| 242 | const gchar *name,
|
| 243 | gpointer user_data)
|
| 244 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 245 | // g_print ("Lost the name %s\n", name);
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 246 | }
|
| 247 |
|
| 248 | gint
|
| 249 | main (gint argc, gchar *argv[])
|
| 250 | {
|
| 251 | GMainLoop *loop;
|
Norman James | 952b38d | 2015-08-27 21:30:06 -0500 | [diff] [blame] | 252 | cmdline cmd;
|
| 253 | cmd.argc = argc;
|
| 254 | cmd.argv = argv;
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 255 |
|
| 256 | guint id;
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 257 | loop = g_main_loop_new (NULL, FALSE);
|
| 258 |
|
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 259 | id = g_bus_own_name (DBUS_TYPE,
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 260 | dbus_name,
|
| 261 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 262 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 263 | on_bus_acquired,
|
| 264 | on_name_acquired,
|
| 265 | on_name_lost,
|
Norman James | 952b38d | 2015-08-27 21:30:06 -0500 | [diff] [blame] | 266 | &cmd,
|
Norman James | fa2e76e | 2015-08-26 17:41:20 -0500 | [diff] [blame] | 267 | NULL);
|
| 268 |
|
| 269 | g_main_loop_run (loop);
|
| 270 |
|
| 271 | g_bus_unown_name (id);
|
| 272 | g_main_loop_unref (loop);
|
| 273 | return 0;
|
| 274 | }
|