blob: 857f30016dbbd0538d48f100d27c3492ff2c36a9 [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 Jamesfa2e76e2015-08-26 17:41:20 -050038 g_print("Boot\n");
Norman James362a80f2015-09-14 14:04:39 -050039 Control* control = object_get_control((Object*)user_data);
Norman Jamesfa2e76e2015-08-26 17:41:20 -050040 control_host_complete_boot(host,invocation);
41
42 gpio_open(&fsi_clk);
43 gpio_open(&fsi_data);
44 gpio_open(&fsi_enable);
45 gpio_open(&cronus_sel);
46
47 gpio_write(&cronus_sel,1);
48 //putcfam pu 281c 30000000 -p0
49 char a[] = "000011111111110101111000111001100111111111111111111111111111101111111111";
50 //putcfam pu 281c B0000000 -p0
51 char b[] = "000011111111110101111000111000100111111111111111111111111111101101111111";
52
53 gpio_write(&fsi_enable,1);
54 gpio_write(&fsi_clk,1);
55 gpio_write(&fsi_data,1);
56 gpio_clock_cycle(&fsi_clk,5000);
57 gpio_write(&fsi_data,0);
58 gpio_clock_cycle(&fsi_clk,256);
59 gpio_write(&fsi_data,1);
60 gpio_clock_cycle(&fsi_clk,50);
61 uint16_t i=0;
62 for(i=0;i<strlen(a);i++) {
63 gpio_writec(&fsi_data,a[i]);
64 gpio_clock_cycle(&fsi_clk,1);
65 }
66 gpio_write(&fsi_data,1); /* Data standby state */
67 gpio_clock_cycle(&fsi_clk,5000);
68
69 for(i=0;i<strlen(b);i++) {
70 gpio_writec(&fsi_data,b[i]);
71 gpio_clock_cycle(&fsi_clk,1);
72 }
73 gpio_write(&fsi_data,1); /* Data standby state */
74 gpio_clock_cycle(&fsi_clk,2);
75
76 gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
77 gpio_write(&fsi_enable,0);
78 gpio_clock_cycle(&fsi_clk,16);
79 gpio_write(&fsi_clk,0); /* Data standby state */
80
81 gpio_close(&fsi_clk);
82 gpio_close(&fsi_data);
83 gpio_close(&fsi_enable);
84 gpio_close(&cronus_sel);
85
Norman James362a80f2015-09-14 14:04:39 -050086 control_emit_goto_system_state(control,"BOOTING");
87
Norman Jamesfa2e76e2015-08-26 17:41:20 -050088 control_host_emit_booted(host);
89 return TRUE;
90}
91
92static void
93on_bus_acquired (GDBusConnection *connection,
94 const gchar *name,
95 gpointer user_data)
96{
97 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -050098 //g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -050099 cmdline *cmd = user_data;
100 if (cmd->argc < 2)
101 {
102 g_print("No objects created. Put object name(s) on command line\n");
103 return;
104 }
105 manager = g_dbus_object_manager_server_new (dbus_object_path);
106 int i=0;
107 for (i=1;i<cmd->argc;i++)
108 {
109 gchar *s;
110 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
111 object = object_skeleton_new (s);
112 g_free (s);
113 ControlHost* control_host = control_host_skeleton_new ();
114 object_skeleton_set_control_host (object, control_host);
115 g_object_unref (control_host);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500116
Norman James362a80f2015-09-14 14:04:39 -0500117 Control* control = control_skeleton_new ();
118 object_skeleton_set_control (object, control);
119 g_object_unref (control);
120
Norman James10ff6a32015-08-27 14:24:17 -0500121 //define method callbacks here
122 g_signal_connect (control_host,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500123 "handle-boot",
124 G_CALLBACK (on_boot),
Norman James362a80f2015-09-14 14:04:39 -0500125 object); /* user_data */
126
127 g_signal_connect (control,
128 "handle-init",
129 G_CALLBACK (on_init),
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500130 NULL); /* user_data */
131
Norman James10ff6a32015-08-27 14:24:17 -0500132 /* Export the object (@manager takes its own reference to @object) */
133 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
134 g_object_unref (object);
135 }
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500136 /* Export all objects */
137 g_dbus_object_manager_server_set_connection (manager, connection);
138
139 gpio_init(connection,&fsi_data);
140 gpio_init(connection,&fsi_clk);
141 gpio_init(connection,&fsi_enable);
142 gpio_init(connection,&cronus_sel);
143
144}
145
146static void
147on_name_acquired (GDBusConnection *connection,
148 const gchar *name,
149 gpointer user_data)
150{
Norman James362a80f2015-09-14 14:04:39 -0500151// g_print ("Acquired the name %s\n", name);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500152}
153
154static void
155on_name_lost (GDBusConnection *connection,
156 const gchar *name,
157 gpointer user_data)
158{
Norman James362a80f2015-09-14 14:04:39 -0500159// g_print ("Lost the name %s\n", name);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500160}
161
162gint
163main (gint argc, gchar *argv[])
164{
165 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500166 cmdline cmd;
167 cmd.argc = argc;
168 cmd.argv = argv;
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500169
170 guint id;
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500171 loop = g_main_loop_new (NULL, FALSE);
172
173 id = g_bus_own_name (G_BUS_TYPE_SESSION,
174 dbus_name,
175 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
176 G_BUS_NAME_OWNER_FLAGS_REPLACE,
177 on_bus_acquired,
178 on_name_acquired,
179 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500180 &cmd,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500181 NULL);
182
183 g_main_loop_run (loop);
184
185 g_bus_unown_name (id);
186 g_main_loop_unref (loop);
187 return 0;
188}