| 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"; | 
|  | 15 | static const gchar* dbus_name        = "org.openbmc.control.Bmc"; | 
|  | 16 |  | 
| Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 17 | //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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 29 |  | 
|  | 30 | static GDBusObjectManagerServer *manager = NULL; | 
|  | 31 |  | 
| Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 32 | void* 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 James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 39 | printf("ERROR: Unable to map LPC register memory"); | 
| Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 40 | exit(1); | 
|  | 41 | } | 
|  | 42 | return bmcreg; | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | void 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 James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 53 | printf("ERROR: Unable to open /dev/mem"); | 
| Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 54 | 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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 97 | static gboolean | 
|  | 98 | on_init (Control          *control, | 
|  | 99 | GDBusMethodInvocation  *invocation, | 
|  | 100 | gpointer                user_data) | 
|  | 101 | { | 
| Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 102 | #ifdef __arm__ | 
|  | 103 | reg_init(); | 
|  | 104 | #endif | 
| Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 105 | control_complete_init(control,invocation); | 
|  | 106 | control_emit_goto_system_state(control,"STANDBY"); | 
|  | 107 |  | 
|  | 108 | return TRUE; | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | static void | 
|  | 112 | on_bus_acquired (GDBusConnection *connection, | 
|  | 113 | const gchar     *name, | 
|  | 114 | gpointer         user_data) | 
|  | 115 | { | 
|  | 116 | ObjectSkeleton *object; | 
| Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 117 | 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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 140 | //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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
|  | 154 | static void | 
|  | 155 | on_name_acquired (GDBusConnection *connection, | 
|  | 156 | const gchar     *name, | 
|  | 157 | gpointer         user_data) | 
|  | 158 | { | 
| Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
|  | 161 | static void | 
|  | 162 | on_name_lost (GDBusConnection *connection, | 
|  | 163 | const gchar     *name, | 
|  | 164 | gpointer         user_data) | 
|  | 165 | { | 
| Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 166 | } | 
|  | 167 |  | 
|  | 168 |  | 
|  | 169 |  | 
|  | 170 |  | 
|  | 171 | /*----------------------------------------------------------------*/ | 
|  | 172 | /* Main Event Loop                                                */ | 
|  | 173 |  | 
|  | 174 | gint | 
|  | 175 | main (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 James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 185 | id = g_bus_own_name (DBUS_TYPE, | 
| Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 186 | 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 | } |