blob: 9f6fb9288e3dc8bdf4ff4145101921e1437199c6 [file] [log] [blame]
Norman Jamesfa2e76e2015-08-26 17:41:20 -05001#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 James362a80f2015-09-14 14:04:39 -05008#include "interfaces/openbmc_intf.h"
Norman James10ff6a32015-08-27 14:24:17 -05009#include "openbmc.h"
10#include "gpio.h"
Norman Jamesfa2e76e2015-08-26 17:41:20 -050011
12
13/* ---------------------------------------------------------------------------------------------------- */
Norman James362a80f2015-09-14 14:04:39 -050014static const gchar* dbus_object_path = "/org/openbmc/control";
Norman Jamesfa2e76e2015-08-26 17:41:20 -050015static const gchar* dbus_name = "org.openbmc.control.Host";
16
17static GDBusObjectManagerServer *manager = NULL;
Norman Jamesfa2e76e2015-08-26 17:41:20 -050018
19GPIO fsi_data = (GPIO){ "FSI_DATA" };
20GPIO fsi_clk = (GPIO){ "FSI_CLK" };
21GPIO fsi_enable = (GPIO){ "FSI_ENABLE" };
22GPIO cronus_sel = (GPIO){ "CRONUS_SEL" };
23
Norman James362a80f2015-09-14 14:04:39 -050024static gboolean
25on_init (Control *control,
26 GDBusMethodInvocation *invocation,
27 gpointer user_data)
28{
29 control_complete_init(control,invocation);
30 return TRUE;
31}
Norman Jamesfa2e76e2015-08-26 17:41:20 -050032static gboolean
33on_boot (ControlHost *host,
34 GDBusMethodInvocation *invocation,
35 gpointer user_data)
36{
Norman James362a80f2015-09-14 14:04:39 -050037 // TODO: Add error checking
Norman James18998182015-10-11 21:54:53 -050038 g_print("Do Boot\n");
39 int rc = GPIO_OK;
40
Norman James362a80f2015-09-14 14:04:39 -050041 Control* control = object_get_control((Object*)user_data);
Norman Jamesfa2e76e2015-08-26 17:41:20 -050042 control_host_complete_boot(host,invocation);
Norman James18998182015-10-11 21:54:53 -050043 do {
44 rc |= gpio_open(&fsi_clk);
45 rc |= gpio_open(&fsi_data);
46 rc |= gpio_open(&fsi_enable);
47 rc |= gpio_open(&cronus_sel);
48 if (rc!=GPIO_OK) { break; }
49
50 rc = gpio_write(&cronus_sel,1);
51 //putcfam pu 281c 30000000 -p0
52 char a[] = "000011111111110101111000111001100111111111111111111111111111101111111111";
53 //putcfam pu 281c B0000000 -p0
54 char b[] = "000011111111110101111000111000100111111111111111111111111111101101111111";
55
56 gpio_write(&fsi_enable,1);
57 gpio_write(&fsi_clk,1);
58 gpio_write(&fsi_data,1);
59 gpio_clock_cycle(&fsi_clk,5000);
60 gpio_write(&fsi_data,0);
61 gpio_clock_cycle(&fsi_clk,256);
62 gpio_write(&fsi_data,1);
63 gpio_clock_cycle(&fsi_clk,50);
64 uint16_t i=0;
65 for(i=0;i<strlen(a);i++) {
66 gpio_writec(&fsi_data,a[i]);
67 gpio_clock_cycle(&fsi_clk,1);
68 }
69 gpio_write(&fsi_data,1); /* Data standby state */
70 gpio_clock_cycle(&fsi_clk,5000);
71
72 for(i=0;i<strlen(b);i++) {
73 gpio_writec(&fsi_data,b[i]);
74 gpio_clock_cycle(&fsi_clk,1);
75 }
76 gpio_write(&fsi_data,1); /* Data standby state */
77 gpio_clock_cycle(&fsi_clk,2);
78
79 gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
80 gpio_write(&fsi_enable,0);
81 gpio_clock_cycle(&fsi_clk,16);
82 gpio_write(&fsi_clk,0); /* Data standby state */
Norman Jamesfa2e76e2015-08-26 17:41:20 -050083
Norman James18998182015-10-11 21:54:53 -050084 } while(0);
85 if (rc != GPIO_OK)
86 {
87 printf("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
88 }
Norman Jamesfa2e76e2015-08-26 17:41:20 -050089 gpio_close(&fsi_clk);
90 gpio_close(&fsi_data);
91 gpio_close(&fsi_enable);
92 gpio_close(&cronus_sel);
93
Norman James362a80f2015-09-14 14:04:39 -050094 control_emit_goto_system_state(control,"BOOTING");
95
Norman Jamesfa2e76e2015-08-26 17:41:20 -050096 control_host_emit_booted(host);
97 return TRUE;
98}
99
100static void
101on_bus_acquired (GDBusConnection *connection,
102 const gchar *name,
103 gpointer user_data)
104{
105 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -0500106 //g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -0500107 cmdline *cmd = user_data;
108 if (cmd->argc < 2)
109 {
110 g_print("No objects created. Put object name(s) on command line\n");
111 return;
112 }
113 manager = g_dbus_object_manager_server_new (dbus_object_path);
114 int i=0;
115 for (i=1;i<cmd->argc;i++)
116 {
117 gchar *s;
118 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
119 object = object_skeleton_new (s);
120 g_free (s);
121 ControlHost* control_host = control_host_skeleton_new ();
122 object_skeleton_set_control_host (object, control_host);
123 g_object_unref (control_host);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500124
Norman James362a80f2015-09-14 14:04:39 -0500125 Control* control = control_skeleton_new ();
126 object_skeleton_set_control (object, control);
127 g_object_unref (control);
128
Norman James10ff6a32015-08-27 14:24:17 -0500129 //define method callbacks here
130 g_signal_connect (control_host,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500131 "handle-boot",
132 G_CALLBACK (on_boot),
Norman James362a80f2015-09-14 14:04:39 -0500133 object); /* user_data */
134
135 g_signal_connect (control,
136 "handle-init",
137 G_CALLBACK (on_init),
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500138 NULL); /* user_data */
139
Norman James10ff6a32015-08-27 14:24:17 -0500140 /* Export the object (@manager takes its own reference to @object) */
141 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
142 g_object_unref (object);
143 }
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500144 /* Export all objects */
145 g_dbus_object_manager_server_set_connection (manager, connection);
146
147 gpio_init(connection,&fsi_data);
148 gpio_init(connection,&fsi_clk);
149 gpio_init(connection,&fsi_enable);
150 gpio_init(connection,&cronus_sel);
151
152}
153
154static void
155on_name_acquired (GDBusConnection *connection,
156 const gchar *name,
157 gpointer user_data)
158{
Norman James362a80f2015-09-14 14:04:39 -0500159// g_print ("Acquired the name %s\n", name);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500160}
161
162static void
163on_name_lost (GDBusConnection *connection,
164 const gchar *name,
165 gpointer user_data)
166{
Norman James362a80f2015-09-14 14:04:39 -0500167// g_print ("Lost the name %s\n", name);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500168}
169
170gint
171main (gint argc, gchar *argv[])
172{
173 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500174 cmdline cmd;
175 cmd.argc = argc;
176 cmd.argv = argv;
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500177
178 guint id;
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500179 loop = g_main_loop_new (NULL, FALSE);
180
Norman James5e792e32015-10-07 17:36:17 -0500181 id = g_bus_own_name (DBUS_TYPE,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500182 dbus_name,
183 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
184 G_BUS_NAME_OWNER_FLAGS_REPLACE,
185 on_bus_acquired,
186 on_name_acquired,
187 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500188 &cmd,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500189 NULL);
190
191 g_main_loop_run (loop);
192
193 g_bus_unown_name (id);
194 g_main_loop_unref (loop);
195 return 0;
196}