blob: 2aedd0481862b10810f635f51ee6eb2fa886aadb [file] [log] [blame]
Norman James362a80f2015-09-14 14:04:39 -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";
15static const gchar* dbus_name = "org.openbmc.control.Bmc";
16
Norman James65a295a2015-09-26 22:21:10 -050017//this probably should come from some global SOC config
18
Norman James8fe5d242015-10-14 09:40:44 -050019#define LPC_BASE (off_t)0x1e789000
Norman James65a295a2015-09-26 22:21:10 -050020#define LPC_HICR6 0x80
21#define LPC_HICR7 0x88
22#define LPC_HICR8 0x8c
Norman James8fe5d242015-10-14 09:40:44 -050023#define SPI_BASE (off_t)0x1e630000
24#define SCU_BASE (off_t)0x1e780000
25#define UART_BASE (off_t)0x1e783000
26#define COM_BASE (off_t)0x1e789000
27#define COM_BASE2 (off_t)0x1e789100
28#define GPIO_BASE (off_t)0x1e6e2000
Norman James362a80f2015-09-14 14:04:39 -050029
30static GDBusObjectManagerServer *manager = NULL;
31
Norman James8fe5d242015-10-14 09:40:44 -050032void* memmap(int mem_fd,off_t base)
Norman James65a295a2015-09-26 22:21:10 -050033{
34 void* bmcreg;
35 bmcreg = mmap(NULL, getpagesize(),
36 PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, base);
37
38 if (bmcreg == MAP_FAILED) {
Norman James8c6d8382015-10-06 07:47:16 -050039 printf("ERROR: Unable to map LPC register memory");
Norman James65a295a2015-09-26 22:21:10 -050040 exit(1);
41 }
42 return bmcreg;
43}
44
45void reg_init()
46{
47 g_print("BMC init\n");
48 // BMC init done here
49
50 void *bmcreg;
51 int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
52 if (mem_fd < 0) {
Norman James8c6d8382015-10-06 07:47:16 -050053 printf("ERROR: Unable to open /dev/mem");
Norman James65a295a2015-09-26 22:21:10 -050054 exit(1);
55 }
56
57 bmcreg = memmap(mem_fd,LPC_BASE);
58 devmem(bmcreg+LPC_HICR6,0x00000500); //Enable LPC FWH cycles, Enable LPC to AHB bridge
59 devmem(bmcreg+LPC_HICR7,0x30000E00); //32M PNOR
60 devmem(bmcreg+LPC_HICR8,0xFE0001FF);
61
62 //flash controller
63 bmcreg = memmap(mem_fd,SPI_BASE);
64 devmem(bmcreg+0x00,0x00000003);
65 devmem(bmcreg+0x04,0x00002404);
66
67 //UART
68
69
70 bmcreg = memmap(mem_fd,UART_BASE);
71 devmem(bmcreg+0x00,0x00000000); //Set Baud rate divisor -> 13 (Baud 115200)
72 devmem(bmcreg+0x04,0x00000000); //Set Baud rate divisor -> 13 (Baud 115200)
73 devmem(bmcreg+0x08,0x000000c1); //Disable Parity, 1 stop bit, 8 bits
74 bmcreg = memmap(mem_fd,COM_BASE);
Norman Jamesc1eb4922015-10-20 22:11:26 -050075 devmem(bmcreg+0x9C,0x08060000); //Set UART routing
Norman James65a295a2015-09-26 22:21:10 -050076
77 bmcreg = memmap(mem_fd,SCU_BASE);
78 devmem(bmcreg+0x00,0x13008CE7);
79 devmem(bmcreg+0x04,0x0370E677);
80 devmem(bmcreg+0x20,0xDF48F7FF);
81 devmem(bmcreg+0x24,0xC738F202);
82
83
84 //GPIO
85 bmcreg = memmap(mem_fd,GPIO_BASE);
86 devmem(bmcreg+0x84,0x00fff0c0); //Enable UART1
87 devmem(bmcreg+0x70,0x120CE406);
88 devmem(bmcreg+0x80,0xCB000000);
89 devmem(bmcreg+0x88,0x01C000FF);
90 devmem(bmcreg+0x8c,0xC1C000FF);
91 devmem(bmcreg+0x90,0x003FA009);
92
Norman James13be15a2015-10-08 15:11:28 -050093 bmcreg = memmap(mem_fd,COM_BASE);
94 devmem(bmcreg+0x170,0x00000042);
95 devmem(bmcreg+0x174,0x00004000);
96
Norman James65a295a2015-09-26 22:21:10 -050097
98 close(mem_fd);
99}
100
Norman James362a80f2015-09-14 14:04:39 -0500101static gboolean
102on_init (Control *control,
103 GDBusMethodInvocation *invocation,
104 gpointer user_data)
105{
Norman James65a295a2015-09-26 22:21:10 -0500106 #ifdef __arm__
107 reg_init();
108 #endif
Norman James362a80f2015-09-14 14:04:39 -0500109 control_complete_init(control,invocation);
Norman Jamesa3e47c42015-10-18 14:43:10 -0500110 control_emit_goto_system_state(control,"BMC_STARTING");
Norman James362a80f2015-09-14 14:04:39 -0500111
112 return TRUE;
113}
114
115static void
116on_bus_acquired (GDBusConnection *connection,
117 const gchar *name,
118 gpointer user_data)
119{
120 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -0500121 cmdline *cmd = user_data;
122 if (cmd->argc < 2)
123 {
124 g_print("No objects created. Put object name(s) on command line\n");
125 return;
126 }
127 manager = g_dbus_object_manager_server_new (dbus_object_path);
128 int i=0;
129 for (i=1;i<cmd->argc;i++)
130 {
131 gchar *s;
132 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
133 object = object_skeleton_new (s);
134 g_free (s);
135
136 ControlBmc* control_bmc = control_bmc_skeleton_new ();
137 object_skeleton_set_control_bmc (object, control_bmc);
138 g_object_unref (control_bmc);
139
140 Control* control = control_skeleton_new ();
141 object_skeleton_set_control (object, control);
142 g_object_unref (control);
143
Norman James362a80f2015-09-14 14:04:39 -0500144 //define method callbacks here
145 g_signal_connect (control,
146 "handle-init",
147 G_CALLBACK (on_init),
148 NULL); /* user_data */
149
150 /* Export the object (@manager takes its own reference to @object) */
151 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
152 g_object_unref (object);
153 }
154 /* Export all objects */
155 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James362a80f2015-09-14 14:04:39 -0500156}
157
158static void
159on_name_acquired (GDBusConnection *connection,
160 const gchar *name,
161 gpointer user_data)
162{
Norman James362a80f2015-09-14 14:04:39 -0500163}
164
165static void
166on_name_lost (GDBusConnection *connection,
167 const gchar *name,
168 gpointer user_data)
169{
Norman James362a80f2015-09-14 14:04:39 -0500170}
171
172
173
174
175/*----------------------------------------------------------------*/
176/* Main Event Loop */
177
178gint
179main (gint argc, gchar *argv[])
180{
181 GMainLoop *loop;
182 cmdline cmd;
183 cmd.argc = argc;
184 cmd.argv = argv;
185
186 guint id;
187 loop = g_main_loop_new (NULL, FALSE);
188
Norman James5e792e32015-10-07 17:36:17 -0500189 id = g_bus_own_name (DBUS_TYPE,
Norman James362a80f2015-09-14 14:04:39 -0500190 dbus_name,
191 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
192 G_BUS_NAME_OWNER_FLAGS_REPLACE,
193 on_bus_acquired,
194 on_name_acquired,
195 on_name_lost,
196 &cmd,
197 NULL);
198
199 g_main_loop_run (loop);
200
201 g_bus_unown_name (id);
202 g_main_loop_unref (loop);
203 return 0;
204}