blob: d6f3fbf6c4e0720ca74e84e1fc9ee03966a16668 [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
19#define LPC_BASE 0x1e789000
20#define LPC_HICR6 0x80
21#define LPC_HICR7 0x88
22#define LPC_HICR8 0x8c
23
24#define SPI_BASE 0x1e630000
25#define SCU_BASE 0x1e780000
26#define UART_BASE 0x1e783000
27#define COM_BASE 0x1e789000
28#define GPIO_BASE 0x1e6e2000
Norman James362a80f2015-09-14 14:04:39 -050029
30static GDBusObjectManagerServer *manager = NULL;
31
Norman James65a295a2015-09-26 22:21:10 -050032void* memmap(int mem_fd,uint32_t* base)
33{
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);
75 devmem(bmcreg+0x9C,0x00000000); //Set routing UART1 -> COM 1
76
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
93
94 close(mem_fd);
95}
96
Norman James362a80f2015-09-14 14:04:39 -050097static gboolean
98on_init (Control *control,
99 GDBusMethodInvocation *invocation,
100 gpointer user_data)
101{
Norman James65a295a2015-09-26 22:21:10 -0500102 #ifdef __arm__
103 reg_init();
104 #endif
Norman James362a80f2015-09-14 14:04:39 -0500105 control_complete_init(control,invocation);
106 control_emit_goto_system_state(control,"STANDBY");
107
108 return TRUE;
109}
110
111static void
112on_bus_acquired (GDBusConnection *connection,
113 const gchar *name,
114 gpointer user_data)
115{
116 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -0500117 cmdline *cmd = user_data;
118 if (cmd->argc < 2)
119 {
120 g_print("No objects created. Put object name(s) on command line\n");
121 return;
122 }
123 manager = g_dbus_object_manager_server_new (dbus_object_path);
124 int i=0;
125 for (i=1;i<cmd->argc;i++)
126 {
127 gchar *s;
128 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
129 object = object_skeleton_new (s);
130 g_free (s);
131
132 ControlBmc* control_bmc = control_bmc_skeleton_new ();
133 object_skeleton_set_control_bmc (object, control_bmc);
134 g_object_unref (control_bmc);
135
136 Control* control = control_skeleton_new ();
137 object_skeleton_set_control (object, control);
138 g_object_unref (control);
139
Norman James362a80f2015-09-14 14:04:39 -0500140 //define method callbacks here
141 g_signal_connect (control,
142 "handle-init",
143 G_CALLBACK (on_init),
144 NULL); /* user_data */
145
146 /* Export the object (@manager takes its own reference to @object) */
147 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
148 g_object_unref (object);
149 }
150 /* Export all objects */
151 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James362a80f2015-09-14 14:04:39 -0500152}
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}
160
161static void
162on_name_lost (GDBusConnection *connection,
163 const gchar *name,
164 gpointer user_data)
165{
Norman James362a80f2015-09-14 14:04:39 -0500166}
167
168
169
170
171/*----------------------------------------------------------------*/
172/* Main Event Loop */
173
174gint
175main (gint argc, gchar *argv[])
176{
177 GMainLoop *loop;
178 cmdline cmd;
179 cmd.argc = argc;
180 cmd.argv = argv;
181
182 guint id;
183 loop = g_main_loop_new (NULL, FALSE);
184
Norman James5e792e32015-10-07 17:36:17 -0500185 id = g_bus_own_name (DBUS_TYPE,
Norman James362a80f2015-09-14 14:04:39 -0500186 dbus_name,
187 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
188 G_BUS_NAME_OWNER_FLAGS_REPLACE,
189 on_bus_acquired,
190 on_name_acquired,
191 on_name_lost,
192 &cmd,
193 NULL);
194
195 g_main_loop_run (loop);
196
197 g_bus_unown_name (id);
198 g_main_loop_unref (loop);
199 return 0;
200}