Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 1 | #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 | /* ---------------------------------------------------------------------------------------------------- */
|
| 14 | static const gchar* dbus_object_path = "/org/openbmc/control";
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 15 | static const gchar* instance_name = "bmc0";
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 16 | static const gchar* dbus_name = "org.openbmc.control.Bmc";
|
| 17 |
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 18 | //this probably should come from some global SOC config
|
| 19 |
|
Norman James | 8fe5d24 | 2015-10-14 09:40:44 -0500 | [diff] [blame] | 20 | #define LPC_BASE (off_t)0x1e789000
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 21 | #define LPC_HICR6 0x80
|
| 22 | #define LPC_HICR7 0x88
|
| 23 | #define LPC_HICR8 0x8c
|
Norman James | 8fe5d24 | 2015-10-14 09:40:44 -0500 | [diff] [blame] | 24 | #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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 30 |
|
| 31 | static GDBusObjectManagerServer *manager = NULL;
|
| 32 |
|
Norman James | 8fe5d24 | 2015-10-14 09:40:44 -0500 | [diff] [blame] | 33 | void* memmap(int mem_fd,off_t base)
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 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) {
|
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 40 | printf("ERROR: Unable to map LPC register memory");
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 41 | exit(1);
|
| 42 | }
|
| 43 | return bmcreg;
|
| 44 | }
|
| 45 |
|
| 46 | void 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 James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 54 | printf("ERROR: Unable to open /dev/mem");
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 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,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 |
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 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 James | 826c577 | 2015-11-20 09:18:50 -0600 | [diff] [blame] | 75 | devmem(bmcreg+0x9C,0x00000000); //Set UART routing
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 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
|
Ken | 14937cb | 2015-12-12 02:59:21 +0800 | [diff] [blame] | 87 | devmem(bmcreg+0x70,0x120CE406);
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 88 | devmem(bmcreg+0x80,0xCB000000);
|
| 89 | devmem(bmcreg+0x88,0x01C000FF);
|
| 90 | devmem(bmcreg+0x8c,0xC1C000FF);
|
| 91 | devmem(bmcreg+0x90,0x003FA009);
|
| 92 |
|
Norman James | 13be15a | 2015-10-08 15:11:28 -0500 | [diff] [blame] | 93 | bmcreg = memmap(mem_fd,COM_BASE);
|
| 94 | devmem(bmcreg+0x170,0x00000042);
|
| 95 | devmem(bmcreg+0x174,0x00004000);
|
| 96 |
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 97 |
|
| 98 | close(mem_fd);
|
| 99 | }
|
| 100 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 101 | static gboolean
|
| 102 | on_init (Control *control,
|
| 103 | GDBusMethodInvocation *invocation,
|
| 104 | gpointer user_data)
|
| 105 | {
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 106 | //#ifdef __arm__
|
| 107 | //reg_init();
|
| 108 | //#endif
|
| 109 | control_complete_init(control,invocation);
|
| 110 | //control_emit_goto_system_state(control,"BMC_STARTING");
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 111 | return TRUE;
|
| 112 | }
|
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 113 | |
| 114 | static gboolean |
| 115 | on_warm_reset (ControlBmc *bmc, |
| 116 | GDBusMethodInvocation *invocation, |
| 117 | gpointer user_data) |
| 118 | { |
| 119 | GError *err = NULL; |
| 120 | /* Wait a while before reboot, so the caller can be responded. |
| 121 | * Note that g_spawn_command_line_async() cannot parse ';' as |
| 122 | * a command separator. Need to use 'sh -c' to let shell parse it. |
| 123 | */ |
| 124 | gchar *reboot_command = "/bin/sh -c 'sleep 3;reboot'"; |
| 125 | |
| 126 | g_spawn_command_line_async(reboot_command, &err); |
| 127 | if (err != NULL) { |
| 128 | fprintf(stderr, "warmReset() error: %s\n", err->message); |
| 129 | g_error_free(err); |
| 130 | } |
| 131 | |
| 132 | control_bmc_complete_warm_reset(bmc, invocation); |
| 133 | return TRUE; |
| 134 | } |
| 135 | |
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 136 | gboolean go(gpointer user_data)
|
| 137 | {
|
| 138 | cmdline *cmd = user_data;
|
| 139 | Control* control = object_get_control((Object*)cmd->user_data);
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 140 | #ifdef __arm__
|
| 141 | reg_init();
|
| 142 | #endif
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 143 | control_emit_goto_system_state(control,"BMC_STARTING");
|
Norman James | e7e3076 | 2015-10-28 20:27:02 -0500 | [diff] [blame] | 144 |
|
| 145 | //g_main_loop_quit(cmd->loop);
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 146 | return FALSE;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 147 | }
|
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 148 | |
| 149 | static void |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 150 | on_bus_acquired (GDBusConnection *connection,
|
| 151 | const gchar *name,
|
| 152 | gpointer user_data)
|
| 153 | {
|
| 154 | ObjectSkeleton *object;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 155 | cmdline *cmd = user_data;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 156 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 157 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 158 | gchar *s;
|
| 159 | s = g_strdup_printf ("%s/%s",dbus_object_path,instance_name);
|
| 160 | object = object_skeleton_new (s);
|
| 161 | g_free (s);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 162 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 163 | ControlBmc* control_bmc = control_bmc_skeleton_new ();
|
| 164 | object_skeleton_set_control_bmc (object, control_bmc);
|
| 165 | g_object_unref (control_bmc);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 166 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 167 | Control* control = control_skeleton_new ();
|
| 168 | object_skeleton_set_control (object, control);
|
| 169 | g_object_unref (control);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 170 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 171 | //define method callbacks here
|
| 172 | g_signal_connect (control,
|
| 173 | "handle-init",
|
| 174 | G_CALLBACK (on_init),
|
| 175 | NULL); /* user_data */
|
| 176 |
|
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 177 | |
| 178 | g_signal_connect (control_bmc, |
| 179 | "handle-warm-reset", |
| 180 | G_CALLBACK (on_warm_reset), |
| 181 | NULL); /* user_data */ |
| 182 | |
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 183 | /* Export the object (@manager takes its own reference to @object) */
|
| 184 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 185 | g_object_unref (object);
|
| 186 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 187 | /* Export all objects */
|
| 188 | g_dbus_object_manager_server_set_connection (manager, connection);
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 189 |
|
| 190 | //TODO: This is a bad hack to wait for object to be on bus
|
| 191 | //sleep(1);
|
| 192 | cmd->user_data = object;
|
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 193 | //g_idle_add(go,cmd);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 194 | }
|
| 195 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 196 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 197 | static void
|
| 198 | on_name_acquired (GDBusConnection *connection,
|
| 199 | const gchar *name,
|
| 200 | gpointer user_data)
|
| 201 | {
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 202 |
|
| 203 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 204 | }
|
| 205 |
|
| 206 | static void
|
| 207 | on_name_lost (GDBusConnection *connection,
|
| 208 | const gchar *name,
|
| 209 | gpointer user_data)
|
| 210 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 211 | }
|
| 212 |
|
| 213 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 214 | /*----------------------------------------------------------------*/
|
| 215 | /* Main Event Loop */
|
| 216 |
|
| 217 | gint
|
| 218 | main (gint argc, gchar *argv[])
|
| 219 | {
|
| 220 | GMainLoop *loop;
|
| 221 | cmdline cmd;
|
| 222 | cmd.argc = argc;
|
| 223 | cmd.argv = argv;
|
| 224 |
|
| 225 | guint id;
|
| 226 | loop = g_main_loop_new (NULL, FALSE);
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 227 | cmd.loop = loop;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 228 |
|
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 229 | id = g_bus_own_name (DBUS_TYPE,
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 230 | dbus_name,
|
| 231 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 232 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 233 | on_bus_acquired,
|
| 234 | on_name_acquired,
|
| 235 | on_name_lost,
|
| 236 | &cmd,
|
| 237 | NULL);
|
| 238 |
|
| 239 | g_main_loop_run (loop);
|
William | f784d75 | 2016-01-19 12:28:49 +0800 | [diff] [blame] | 240 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 241 | g_bus_unown_name (id);
|
| 242 | g_main_loop_unref (loop);
|
| 243 | return 0;
|
| 244 | }
|