blob: a3a19e808f0e1aac6313a277a9257b01d85ec248 [file] [log] [blame]
Norman Jamese56d8382015-10-22 14:35:04 -05001#include <stdint.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <sys/stat.h>
8#include <sys/mman.h>
9#include "interfaces/openbmc_intf.h"
10#include "openbmc.h"
11#include "gpio.h"
12
13/* ---------------------------------------------------------------------------------------------------- */
14static const gchar* dbus_object_path = "/org/openbmc/control";
Norman James978d2f32015-10-28 12:45:52 -050015static const gchar* instance_name = "bmc0";
Norman Jamese56d8382015-10-22 14:35:04 -050016static const gchar* dbus_name = "org.openbmc.control.Bmc";
17
18//this probably should come from some global SOC config
19
20#define LPC_BASE (off_t)0x1e789000
21#define LPC_HICR6 0x80
22#define LPC_HICR7 0x88
23#define LPC_HICR8 0x8c
24#define SPI_BASE (off_t)0x1e630000
25#define SCU_BASE (off_t)0x1e780000
26#define UART_BASE (off_t)0x1e783000
Norman Jamese56d8382015-10-22 14:35:04 -050027#define COM_BASE (off_t)0x1e789000
28#define COM_BASE2 (off_t)0x1e789100
29#define GPIO_BASE (off_t)0x1e6e2000
30
31static GDBusObjectManagerServer *manager = NULL;
32
33void* memmap(int mem_fd,off_t base)
34{
35 void* bmcreg;
36 bmcreg = mmap(NULL, getpagesize(),
37 PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, base);
38
39 if (bmcreg == MAP_FAILED) {
40 printf("ERROR: Unable to map LPC register memory");
41 exit(1);
42 }
43 return bmcreg;
44}
45
46void reg_init()
47{
48 g_print("BMC init\n");
49 // BMC init done here
50
51 void *bmcreg;
52 int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
53 if (mem_fd < 0) {
54 printf("ERROR: Unable to open /dev/mem");
55 exit(1);
56 }
57
58 bmcreg = memmap(mem_fd,LPC_BASE);
59 devmem(bmcreg+LPC_HICR6,0x00000500); //Enable LPC FWH cycles, Enable LPC to AHB bridge
60 devmem(bmcreg+LPC_HICR7,0x30000E00); //32M PNOR
61 devmem(bmcreg+LPC_HICR8,0xFC0003FF);
62
63 //flash controller
64 bmcreg = memmap(mem_fd,SPI_BASE);
65 devmem(bmcreg+0x00,0x00000003);
66 devmem(bmcreg+0x04,0x00002404);
67
68 //UART
69
70
71 bmcreg = memmap(mem_fd,UART_BASE);
72 devmem(bmcreg+0x00,0x00000000); //Set Baud rate divisor -> 13 (Baud 115200)
73 devmem(bmcreg+0x04,0x00000000); //Set Baud rate divisor -> 13 (Baud 115200)
74 devmem(bmcreg+0x08,0x000000c1); //Disable Parity, 1 stop bit, 8 bits
Norman Jamese56d8382015-10-22 14:35:04 -050075 bmcreg = memmap(mem_fd,COM_BASE);
76 devmem(bmcreg+0x9C,0x08060000); //Set UART routing
77
78 bmcreg = memmap(mem_fd,SCU_BASE);
79 devmem(bmcreg+0x00,0x9f82fce7);
80 devmem(bmcreg+0x04,0x0370e677);
81 devmem(bmcreg+0x20,0xcfc8f7ff);
82 devmem(bmcreg+0x24,0xc738f20a);
83 devmem(bmcreg+0x80,0x0031ffaf);
84
85
86 //GPIO
87 bmcreg = memmap(mem_fd,GPIO_BASE);
88 devmem(bmcreg+0x84,0x00fff0c0); //Enable UART1
89 devmem(bmcreg+0x70,0x120CE406);
90 devmem(bmcreg+0x80,0xCB000000);
91 devmem(bmcreg+0x88,0x01C000FF);
92 devmem(bmcreg+0x8c,0xC1C000FF);
93 devmem(bmcreg+0x90,0x003FA009);
94 devmem(bmcreg+0x88,0x01C0007F);
95
96
97 bmcreg = memmap(mem_fd,COM_BASE);
98 devmem(bmcreg+0x170,0x00000042);
99 devmem(bmcreg+0x174,0x00004000);
100
Norman Jamese56d8382015-10-22 14:35:04 -0500101 close(mem_fd);
102}
103
104static gboolean
105on_init (Control *control,
106 GDBusMethodInvocation *invocation,
107 gpointer user_data)
108{
Norman James978d2f32015-10-28 12:45:52 -0500109 //#ifdef __arm__
110 //reg_init();
111 //#endif
112 control_complete_init(control,invocation);
113 //control_emit_goto_system_state(control,"BMC_STARTING");
114
115 return TRUE;
116}
117gboolean go(gpointer user_data)
118{
119 cmdline *cmd = user_data;
120 Control* control = object_get_control((Object*)cmd->user_data);
Norman Jamese56d8382015-10-22 14:35:04 -0500121 #ifdef __arm__
122 reg_init();
123 #endif
Norman Jamese56d8382015-10-22 14:35:04 -0500124 control_emit_goto_system_state(control,"BMC_STARTING");
125
Norman Jamese7e30762015-10-28 20:27:02 -0500126 //g_main_loop_quit(cmd->loop);
Norman James978d2f32015-10-28 12:45:52 -0500127 return FALSE;
Norman Jamese56d8382015-10-22 14:35:04 -0500128}
129
130static void
131on_bus_acquired (GDBusConnection *connection,
132 const gchar *name,
133 gpointer user_data)
134{
135 ObjectSkeleton *object;
136 cmdline *cmd = user_data;
Norman Jamese56d8382015-10-22 14:35:04 -0500137 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman Jamese56d8382015-10-22 14:35:04 -0500138
Norman James978d2f32015-10-28 12:45:52 -0500139 gchar *s;
140 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
141 object = object_skeleton_new (s);
142 g_free (s);
Norman Jamese56d8382015-10-22 14:35:04 -0500143
Norman James978d2f32015-10-28 12:45:52 -0500144 ControlBmc* control_bmc = control_bmc_skeleton_new ();
145 object_skeleton_set_control_bmc (object, control_bmc);
146 g_object_unref (control_bmc);
Norman Jamese56d8382015-10-22 14:35:04 -0500147
Norman James978d2f32015-10-28 12:45:52 -0500148 Control* control = control_skeleton_new ();
149 object_skeleton_set_control (object, control);
150 g_object_unref (control);
Norman Jamese56d8382015-10-22 14:35:04 -0500151
Norman James978d2f32015-10-28 12:45:52 -0500152 //define method callbacks here
153 g_signal_connect (control,
154 "handle-init",
155 G_CALLBACK (on_init),
156 NULL); /* user_data */
157
158 /* Export the object (@manager takes its own reference to @object) */
159 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
160 g_object_unref (object);
161
Norman Jamese56d8382015-10-22 14:35:04 -0500162 /* Export all objects */
163 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James978d2f32015-10-28 12:45:52 -0500164
165 //TODO: This is a bad hack to wait for object to be on bus
166 //sleep(1);
167 cmd->user_data = object;
168 g_idle_add(go,cmd);
Norman Jamese56d8382015-10-22 14:35:04 -0500169}
170
Norman James978d2f32015-10-28 12:45:52 -0500171
Norman Jamese56d8382015-10-22 14:35:04 -0500172static void
173on_name_acquired (GDBusConnection *connection,
174 const gchar *name,
175 gpointer user_data)
176{
Norman James978d2f32015-10-28 12:45:52 -0500177
178
Norman Jamese56d8382015-10-22 14:35:04 -0500179}
180
181static void
182on_name_lost (GDBusConnection *connection,
183 const gchar *name,
184 gpointer user_data)
185{
186}
187
188
Norman Jamese56d8382015-10-22 14:35:04 -0500189/*----------------------------------------------------------------*/
190/* Main Event Loop */
191
192gint
193main (gint argc, gchar *argv[])
194{
195 GMainLoop *loop;
196 cmdline cmd;
197 cmd.argc = argc;
198 cmd.argv = argv;
199
200 guint id;
201 loop = g_main_loop_new (NULL, FALSE);
Norman James978d2f32015-10-28 12:45:52 -0500202 cmd.loop = loop;
Norman Jamese56d8382015-10-22 14:35:04 -0500203
204 id = g_bus_own_name (DBUS_TYPE,
205 dbus_name,
206 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
207 G_BUS_NAME_OWNER_FLAGS_REPLACE,
208 on_bus_acquired,
209 on_name_acquired,
210 on_name_lost,
211 &cmd,
212 NULL);
213
214 g_main_loop_run (loop);
215
216 g_bus_unown_name (id);
217 g_main_loop_unref (loop);
218 return 0;
219}