blob: 63a8d640e34ce88033c7c412972618ad91bd881e [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 James8fee6f22015-10-28 12:48:43 -050015static const gchar* instance_name = "host0";
Norman Jamesfa2e76e2015-08-26 17:41:20 -050016static const gchar* dbus_name = "org.openbmc.control.Host";
17
18static GDBusObjectManagerServer *manager = NULL;
Norman Jamesfa2e76e2015-08-26 17:41:20 -050019
20GPIO fsi_data = (GPIO){ "FSI_DATA" };
21GPIO fsi_clk = (GPIO){ "FSI_CLK" };
22GPIO fsi_enable = (GPIO){ "FSI_ENABLE" };
23GPIO cronus_sel = (GPIO){ "CRONUS_SEL" };
24
Norman James362a80f2015-09-14 14:04:39 -050025static gboolean
26on_init (Control *control,
27 GDBusMethodInvocation *invocation,
28 gpointer user_data)
29{
30 control_complete_init(control,invocation);
31 return TRUE;
32}
Norman Jamesfa2e76e2015-08-26 17:41:20 -050033static gboolean
34on_boot (ControlHost *host,
35 GDBusMethodInvocation *invocation,
36 gpointer user_data)
37{
Norman James362a80f2015-09-14 14:04:39 -050038 // TODO: Add error checking
Norman James18998182015-10-11 21:54:53 -050039 g_print("Do Boot\n");
40 int rc = GPIO_OK;
41
Norman James362a80f2015-09-14 14:04:39 -050042 Control* control = object_get_control((Object*)user_data);
Norman Jamesfa2e76e2015-08-26 17:41:20 -050043 control_host_complete_boot(host,invocation);
Norman James18998182015-10-11 21:54:53 -050044 do {
45 rc |= gpio_open(&fsi_clk);
46 rc |= gpio_open(&fsi_data);
47 rc |= gpio_open(&fsi_enable);
48 rc |= gpio_open(&cronus_sel);
49 if (rc!=GPIO_OK) { break; }
50
51 rc = gpio_write(&cronus_sel,1);
52 //putcfam pu 281c 30000000 -p0
53 char a[] = "000011111111110101111000111001100111111111111111111111111111101111111111";
54 //putcfam pu 281c B0000000 -p0
55 char b[] = "000011111111110101111000111000100111111111111111111111111111101101111111";
56
57 gpio_write(&fsi_enable,1);
58 gpio_write(&fsi_clk,1);
59 gpio_write(&fsi_data,1);
60 gpio_clock_cycle(&fsi_clk,5000);
61 gpio_write(&fsi_data,0);
62 gpio_clock_cycle(&fsi_clk,256);
63 gpio_write(&fsi_data,1);
64 gpio_clock_cycle(&fsi_clk,50);
65 uint16_t i=0;
66 for(i=0;i<strlen(a);i++) {
67 gpio_writec(&fsi_data,a[i]);
68 gpio_clock_cycle(&fsi_clk,1);
69 }
70 gpio_write(&fsi_data,1); /* Data standby state */
71 gpio_clock_cycle(&fsi_clk,5000);
72
73 for(i=0;i<strlen(b);i++) {
74 gpio_writec(&fsi_data,b[i]);
75 gpio_clock_cycle(&fsi_clk,1);
76 }
77 gpio_write(&fsi_data,1); /* Data standby state */
78 gpio_clock_cycle(&fsi_clk,2);
79
80 gpio_write(&fsi_clk,0); /* hold clk low for clock mux */
81 gpio_write(&fsi_enable,0);
82 gpio_clock_cycle(&fsi_clk,16);
83 gpio_write(&fsi_clk,0); /* Data standby state */
Norman Jamesfa2e76e2015-08-26 17:41:20 -050084
Norman James18998182015-10-11 21:54:53 -050085 } while(0);
86 if (rc != GPIO_OK)
87 {
88 printf("ERROR HostControl: GPIO sequence failed (rc=%d)\n",rc);
89 }
Norman Jamesfa2e76e2015-08-26 17:41:20 -050090 gpio_close(&fsi_clk);
91 gpio_close(&fsi_data);
92 gpio_close(&fsi_enable);
93 gpio_close(&cronus_sel);
94
Norman James0c06f472015-10-19 11:14:01 -050095 control_emit_goto_system_state(control,"HOST_BOOTING");
Norman James362a80f2015-09-14 14:04:39 -050096
Norman Jamesfa2e76e2015-08-26 17:41:20 -050097 control_host_emit_booted(host);
98 return TRUE;
99}
100
101static void
102on_bus_acquired (GDBusConnection *connection,
103 const gchar *name,
104 gpointer user_data)
105{
106 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -0500107 //g_print ("Acquired a message bus connection: %s\n",name);
Norman James10ff6a32015-08-27 14:24:17 -0500108 cmdline *cmd = user_data;
Norman James10ff6a32015-08-27 14:24:17 -0500109 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman James8fee6f22015-10-28 12:48:43 -0500110
Norman James10ff6a32015-08-27 14:24:17 -0500111 gchar *s;
Norman James8fee6f22015-10-28 12:48:43 -0500112 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
Norman James10ff6a32015-08-27 14:24:17 -0500113 object = object_skeleton_new (s);
114 g_free (s);
115 ControlHost* control_host = control_host_skeleton_new ();
116 object_skeleton_set_control_host (object, control_host);
117 g_object_unref (control_host);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500118
Norman James362a80f2015-09-14 14:04:39 -0500119 Control* control = control_skeleton_new ();
120 object_skeleton_set_control (object, control);
121 g_object_unref (control);
122
Norman James10ff6a32015-08-27 14:24:17 -0500123 //define method callbacks here
124 g_signal_connect (control_host,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500125 "handle-boot",
126 G_CALLBACK (on_boot),
Norman James362a80f2015-09-14 14:04:39 -0500127 object); /* user_data */
128
129 g_signal_connect (control,
130 "handle-init",
131 G_CALLBACK (on_init),
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500132 NULL); /* user_data */
133
Norman James10ff6a32015-08-27 14:24:17 -0500134 /* Export the object (@manager takes its own reference to @object) */
135 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
136 g_object_unref (object);
Norman James8fee6f22015-10-28 12:48:43 -0500137
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500138 /* Export all objects */
139 g_dbus_object_manager_server_set_connection (manager, connection);
140
141 gpio_init(connection,&fsi_data);
142 gpio_init(connection,&fsi_clk);
143 gpio_init(connection,&fsi_enable);
144 gpio_init(connection,&cronus_sel);
145
146}
147
148static void
149on_name_acquired (GDBusConnection *connection,
150 const gchar *name,
151 gpointer user_data)
152{
Norman James362a80f2015-09-14 14:04:39 -0500153// g_print ("Acquired the name %s\n", name);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500154}
155
156static void
157on_name_lost (GDBusConnection *connection,
158 const gchar *name,
159 gpointer user_data)
160{
Norman James362a80f2015-09-14 14:04:39 -0500161// g_print ("Lost the name %s\n", name);
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500162}
163
164gint
165main (gint argc, gchar *argv[])
166{
167 GMainLoop *loop;
Norman James952b38d2015-08-27 21:30:06 -0500168 cmdline cmd;
169 cmd.argc = argc;
170 cmd.argv = argv;
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500171
172 guint id;
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500173 loop = g_main_loop_new (NULL, FALSE);
174
Norman James5e792e32015-10-07 17:36:17 -0500175 id = g_bus_own_name (DBUS_TYPE,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500176 dbus_name,
177 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
178 G_BUS_NAME_OWNER_FLAGS_REPLACE,
179 on_bus_acquired,
180 on_name_acquired,
181 on_name_lost,
Norman James952b38d2015-08-27 21:30:06 -0500182 &cmd,
Norman Jamesfa2e76e2015-08-26 17:41:20 -0500183 NULL);
184
185 g_main_loop_run (loop);
186
187 g_bus_unown_name (id);
188 g_main_loop_unref (loop);
189 return 0;
190}