blob: caba185a15d998d6e046baea31c85e0aa0bf03db [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
23
Norman James8fe5d242015-10-14 09:40:44 -050024#define SPI_BASE (off_t)0x1e630000
25#define SCU_BASE (off_t)0x1e780000
26#define UART_BASE (off_t)0x1e783000
27#define COM_BASE (off_t)0x1e789000
28#define COM_BASE2 (off_t)0x1e789100
29#define GPIO_BASE (off_t)0x1e6e2000
Norman James362a80f2015-09-14 14:04:39 -050030
31static GDBusObjectManagerServer *manager = NULL;
32
Norman James8fe5d242015-10-14 09:40:44 -050033void* memmap(int mem_fd,off_t base)
Norman James65a295a2015-09-26 22:21:10 -050034{
35 void* bmcreg;
36 bmcreg = mmap(NULL, getpagesize(),
37 PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, base);
38
39 if (bmcreg == MAP_FAILED) {
Norman James8c6d8382015-10-06 07:47:16 -050040 printf("ERROR: Unable to map LPC register memory");
Norman James65a295a2015-09-26 22:21:10 -050041 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) {
Norman James8c6d8382015-10-06 07:47:16 -050054 printf("ERROR: Unable to open /dev/mem");
Norman James65a295a2015-09-26 22:21:10 -050055 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,0xFE0001FF);
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
75 bmcreg = memmap(mem_fd,COM_BASE);
76 devmem(bmcreg+0x9C,0x00000000); //Set routing UART1 -> COM 1
77
78 bmcreg = memmap(mem_fd,SCU_BASE);
79 devmem(bmcreg+0x00,0x13008CE7);
80 devmem(bmcreg+0x04,0x0370E677);
81 devmem(bmcreg+0x20,0xDF48F7FF);
82 devmem(bmcreg+0x24,0xC738F202);
83
84
85 //GPIO
86 bmcreg = memmap(mem_fd,GPIO_BASE);
87 devmem(bmcreg+0x84,0x00fff0c0); //Enable UART1
88 devmem(bmcreg+0x70,0x120CE406);
89 devmem(bmcreg+0x80,0xCB000000);
90 devmem(bmcreg+0x88,0x01C000FF);
91 devmem(bmcreg+0x8c,0xC1C000FF);
92 devmem(bmcreg+0x90,0x003FA009);
93
Norman James13be15a2015-10-08 15:11:28 -050094 bmcreg = memmap(mem_fd,COM_BASE);
95 devmem(bmcreg+0x170,0x00000042);
96 devmem(bmcreg+0x174,0x00004000);
97
Norman James65a295a2015-09-26 22:21:10 -050098
99 close(mem_fd);
100}
101
Norman James362a80f2015-09-14 14:04:39 -0500102static gboolean
103on_init (Control *control,
104 GDBusMethodInvocation *invocation,
105 gpointer user_data)
106{
Norman James65a295a2015-09-26 22:21:10 -0500107 #ifdef __arm__
108 reg_init();
109 #endif
Norman James362a80f2015-09-14 14:04:39 -0500110 control_complete_init(control,invocation);
Norman Jamesa3e47c42015-10-18 14:43:10 -0500111 control_emit_goto_system_state(control,"BMC_STARTING");
Norman James362a80f2015-09-14 14:04:39 -0500112
113 return TRUE;
114}
115
116static void
117on_bus_acquired (GDBusConnection *connection,
118 const gchar *name,
119 gpointer user_data)
120{
121 ObjectSkeleton *object;
Norman James362a80f2015-09-14 14:04:39 -0500122 cmdline *cmd = user_data;
123 if (cmd->argc < 2)
124 {
125 g_print("No objects created. Put object name(s) on command line\n");
126 return;
127 }
128 manager = g_dbus_object_manager_server_new (dbus_object_path);
129 int i=0;
130 for (i=1;i<cmd->argc;i++)
131 {
132 gchar *s;
133 s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
134 object = object_skeleton_new (s);
135 g_free (s);
136
137 ControlBmc* control_bmc = control_bmc_skeleton_new ();
138 object_skeleton_set_control_bmc (object, control_bmc);
139 g_object_unref (control_bmc);
140
141 Control* control = control_skeleton_new ();
142 object_skeleton_set_control (object, control);
143 g_object_unref (control);
144
Norman James362a80f2015-09-14 14:04:39 -0500145 //define method callbacks here
146 g_signal_connect (control,
147 "handle-init",
148 G_CALLBACK (on_init),
149 NULL); /* user_data */
150
151 /* Export the object (@manager takes its own reference to @object) */
152 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
153 g_object_unref (object);
154 }
155 /* Export all objects */
156 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James362a80f2015-09-14 14:04:39 -0500157}
158
159static void
160on_name_acquired (GDBusConnection *connection,
161 const gchar *name,
162 gpointer user_data)
163{
Norman James362a80f2015-09-14 14:04:39 -0500164}
165
166static void
167on_name_lost (GDBusConnection *connection,
168 const gchar *name,
169 gpointer user_data)
170{
Norman James362a80f2015-09-14 14:04:39 -0500171}
172
173
174
175
176/*----------------------------------------------------------------*/
177/* Main Event Loop */
178
179gint
180main (gint argc, gchar *argv[])
181{
182 GMainLoop *loop;
183 cmdline cmd;
184 cmd.argc = argc;
185 cmd.argv = argv;
186
187 guint id;
188 loop = g_main_loop_new (NULL, FALSE);
189
Norman James5e792e32015-10-07 17:36:17 -0500190 id = g_bus_own_name (DBUS_TYPE,
Norman James362a80f2015-09-14 14:04:39 -0500191 dbus_name,
192 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
193 G_BUS_NAME_OWNER_FLAGS_REPLACE,
194 on_bus_acquired,
195 on_name_acquired,
196 on_name_lost,
197 &cmd,
198 NULL);
199
200 g_main_loop_run (loop);
201
202 g_bus_unown_name (id);
203 g_main_loop_unref (loop);
204 return 0;
205}