blob: ddfde88e0938a46e9614a79de7e82565d66ecd2d [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";
Norman James2a760202015-11-11 10:31:26 -060017static const gchar* i2c_dev = "/sys/bus/i2c/devices";
Norman Jamese56d8382015-10-22 14:35:04 -050018
19//this probably should come from some global SOC config
20
21#define LPC_BASE (off_t)0x1e789000
22#define LPC_HICR6 0x80
23#define LPC_HICR7 0x88
24#define LPC_HICR8 0x8c
25#define SPI_BASE (off_t)0x1e630000
26#define SCU_BASE (off_t)0x1e780000
27#define UART_BASE (off_t)0x1e783000
Norman Jamese56d8382015-10-22 14:35:04 -050028#define COM_BASE (off_t)0x1e789000
29#define COM_BASE2 (off_t)0x1e789100
30#define GPIO_BASE (off_t)0x1e6e2000
31
32static GDBusObjectManagerServer *manager = NULL;
33
34void* memmap(int mem_fd,off_t base)
35{
36 void* bmcreg;
37 bmcreg = mmap(NULL, getpagesize(),
38 PROT_READ | PROT_WRITE, MAP_SHARED, mem_fd, base);
39
40 if (bmcreg == MAP_FAILED) {
41 printf("ERROR: Unable to map LPC register memory");
42 exit(1);
43 }
44 return bmcreg;
45}
46
47void reg_init()
48{
49 g_print("BMC init\n");
50 // BMC init done here
51
52 void *bmcreg;
53 int mem_fd = open("/dev/mem", O_RDWR | O_SYNC);
54 if (mem_fd < 0) {
55 printf("ERROR: Unable to open /dev/mem");
56 exit(1);
57 }
58
59 bmcreg = memmap(mem_fd,LPC_BASE);
60 devmem(bmcreg+LPC_HICR6,0x00000500); //Enable LPC FWH cycles, Enable LPC to AHB bridge
Norman Jamesc54a6292015-10-31 17:35:17 -050061 devmem(bmcreg+LPC_HICR7,0x30000C00); //32M PNOR
Norman Jamese56d8382015-10-22 14:35:04 -050062 devmem(bmcreg+LPC_HICR8,0xFC0003FF);
63
64 //flash controller
65 bmcreg = memmap(mem_fd,SPI_BASE);
66 devmem(bmcreg+0x00,0x00000003);
67 devmem(bmcreg+0x04,0x00002404);
68
69 //UART
70
71
72 bmcreg = memmap(mem_fd,UART_BASE);
73 devmem(bmcreg+0x00,0x00000000); //Set Baud rate divisor -> 13 (Baud 115200)
74 devmem(bmcreg+0x04,0x00000000); //Set Baud rate divisor -> 13 (Baud 115200)
75 devmem(bmcreg+0x08,0x000000c1); //Disable Parity, 1 stop bit, 8 bits
Norman James9e9eba12015-11-19 16:05:57 -060076 bmcreg = memmap(mem_fd,COM_BASE);
77 devmem(bmcreg+0x9C,0x00000000); //Set UART routing
Norman Jamese56d8382015-10-22 14:35:04 -050078
79 bmcreg = memmap(mem_fd,SCU_BASE);
Norman James9e9eba12015-11-19 16:05:57 -060080 devmem(bmcreg+0x00,0x9e82fce7);
81 //devmem(bmcreg+0x00,0x9f82fce7); // B2?
Norman Jamese56d8382015-10-22 14:35:04 -050082 devmem(bmcreg+0x04,0x0370e677);
Norman James9e9eba12015-11-19 16:05:57 -060083
84 // do not modify state of power pin, otherwise
85 // if this is a reboot, host will shutdown
86 uint32_t reg_20 = devmem_read(bmcreg+0x20);
87 reg_20 = reg_20 & 0x00000002;
88 devmem(bmcreg+0x20,0xcfc8f7fd | reg_20);
Norman Jamese56d8382015-10-22 14:35:04 -050089 devmem(bmcreg+0x24,0xc738f20a);
90 devmem(bmcreg+0x80,0x0031ffaf);
91
92
93 //GPIO
94 bmcreg = memmap(mem_fd,GPIO_BASE);
95 devmem(bmcreg+0x84,0x00fff0c0); //Enable UART1
Norman Jamese56d8382015-10-22 14:35:04 -050096 devmem(bmcreg+0x80,0xCB000000);
97 devmem(bmcreg+0x88,0x01C000FF);
98 devmem(bmcreg+0x8c,0xC1C000FF);
99 devmem(bmcreg+0x90,0x003FA009);
100 devmem(bmcreg+0x88,0x01C0007F);
101
102
103 bmcreg = memmap(mem_fd,COM_BASE);
104 devmem(bmcreg+0x170,0x00000042);
105 devmem(bmcreg+0x174,0x00004000);
106
Norman Jamese56d8382015-10-22 14:35:04 -0500107 close(mem_fd);
108}
109
Norman James2a760202015-11-11 10:31:26 -0600110int init_i2c_driver(int i2c_bus, const char* device, int address,bool delete)
111{
112 char dev[255];
Norman James4fa8e4f2015-11-11 10:41:38 -0600113 g_print("Initing: device = %s, address = %02x\n",device,address);
Norman James2a760202015-11-11 10:31:26 -0600114 if (!delete) {
Norman James372d7882015-11-11 12:48:44 -0600115 sprintf(dev,"%s/i2c-%d/new_device",i2c_dev,i2c_bus);
Norman James2a760202015-11-11 10:31:26 -0600116 } else {
Norman James372d7882015-11-11 12:48:44 -0600117 sprintf(dev,"%s/i2c-%d/delete_device",i2c_dev,i2c_bus);
Norman James2a760202015-11-11 10:31:26 -0600118 }
119 int fd = open(dev, O_WRONLY);
120 if (fd == -1) {
Norman James4fa8e4f2015-11-11 10:41:38 -0600121 g_print("ERROR control_bmc: Unable to open device %s\n",dev);
Norman James2a760202015-11-11 10:31:26 -0600122 return 1;
123 }
Norman James372d7882015-11-11 12:48:44 -0600124 if (!delete) {
125 sprintf(dev,"%s 0x%02x",device,address);
126 } else {
127 sprintf(dev,"0x%02x",address);
128 }
Norman James2a760202015-11-11 10:31:26 -0600129 int rc = write(fd,dev,strlen(dev));
130 close(fd);
131 if (rc != strlen(dev)) {
Norman James4fa8e4f2015-11-11 10:41:38 -0600132 g_print("ERROR control_bmc: Unable to write %s\n",dev);
Norman James2a760202015-11-11 10:31:26 -0600133 return 2;
134 }
135 return 0;
136}
137
138
Norman Jamese56d8382015-10-22 14:35:04 -0500139static gboolean
140on_init (Control *control,
141 GDBusMethodInvocation *invocation,
142 gpointer user_data)
143{
Norman James978d2f32015-10-28 12:45:52 -0500144 control_complete_init(control,invocation);
Norman James978d2f32015-10-28 12:45:52 -0500145
146 return TRUE;
147}
148gboolean go(gpointer user_data)
149{
150 cmdline *cmd = user_data;
151 Control* control = object_get_control((Object*)cmd->user_data);
Norman Jamese56d8382015-10-22 14:35:04 -0500152 #ifdef __arm__
153 reg_init();
154 #endif
Norman Jamese56d8382015-10-22 14:35:04 -0500155 control_emit_goto_system_state(control,"BMC_STARTING");
156
Norman James978d2f32015-10-28 12:45:52 -0500157 return FALSE;
Norman Jamese56d8382015-10-22 14:35:04 -0500158}
159
160static void
161on_bus_acquired (GDBusConnection *connection,
162 const gchar *name,
163 gpointer user_data)
164{
165 ObjectSkeleton *object;
166 cmdline *cmd = user_data;
Norman Jamese56d8382015-10-22 14:35:04 -0500167 manager = g_dbus_object_manager_server_new (dbus_object_path);
Norman Jamese56d8382015-10-22 14:35:04 -0500168
Norman James978d2f32015-10-28 12:45:52 -0500169 gchar *s;
170 s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
171 object = object_skeleton_new (s);
172 g_free (s);
Norman Jamese56d8382015-10-22 14:35:04 -0500173
Norman James978d2f32015-10-28 12:45:52 -0500174 ControlBmc* control_bmc = control_bmc_skeleton_new ();
175 object_skeleton_set_control_bmc (object, control_bmc);
176 g_object_unref (control_bmc);
Norman Jamese56d8382015-10-22 14:35:04 -0500177
Norman James978d2f32015-10-28 12:45:52 -0500178 Control* control = control_skeleton_new ();
179 object_skeleton_set_control (object, control);
180 g_object_unref (control);
Norman Jamese56d8382015-10-22 14:35:04 -0500181
Norman James978d2f32015-10-28 12:45:52 -0500182 //define method callbacks here
183 g_signal_connect (control,
184 "handle-init",
185 G_CALLBACK (on_init),
186 NULL); /* user_data */
187
188 /* Export the object (@manager takes its own reference to @object) */
189 g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
190 g_object_unref (object);
191
Norman Jamese56d8382015-10-22 14:35:04 -0500192 /* Export all objects */
193 g_dbus_object_manager_server_set_connection (manager, connection);
Norman James978d2f32015-10-28 12:45:52 -0500194
195 //TODO: This is a bad hack to wait for object to be on bus
196 //sleep(1);
197 cmd->user_data = object;
198 g_idle_add(go,cmd);
Norman Jamese56d8382015-10-22 14:35:04 -0500199}
200
Norman James978d2f32015-10-28 12:45:52 -0500201
Norman Jamese56d8382015-10-22 14:35:04 -0500202static void
203on_name_acquired (GDBusConnection *connection,
204 const gchar *name,
205 gpointer user_data)
206{
Norman James978d2f32015-10-28 12:45:52 -0500207
208
Norman Jamese56d8382015-10-22 14:35:04 -0500209}
210
211static void
212on_name_lost (GDBusConnection *connection,
213 const gchar *name,
214 gpointer user_data)
215{
216}
217
218
Norman Jamese56d8382015-10-22 14:35:04 -0500219/*----------------------------------------------------------------*/
220/* Main Event Loop */
221
222gint
223main (gint argc, gchar *argv[])
224{
225 GMainLoop *loop;
226 cmdline cmd;
227 cmd.argc = argc;
228 cmd.argv = argv;
229
230 guint id;
231 loop = g_main_loop_new (NULL, FALSE);
Norman James978d2f32015-10-28 12:45:52 -0500232 cmd.loop = loop;
Norman Jamese56d8382015-10-22 14:35:04 -0500233
234 id = g_bus_own_name (DBUS_TYPE,
235 dbus_name,
236 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
237 G_BUS_NAME_OWNER_FLAGS_REPLACE,
238 on_bus_acquired,
239 on_name_acquired,
240 on_name_lost,
241 &cmd,
242 NULL);
243
244 g_main_loop_run (loop);
245
246 g_bus_unown_name (id);
247 g_main_loop_unref (loop);
248 return 0;
249}