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 |
|
Norman James | 8fe5d24 | 2015-10-14 09:40:44 -0500 | [diff] [blame] | 19 | #define LPC_BASE (off_t)0x1e789000
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 20 | #define LPC_HICR6 0x80
|
| 21 | #define LPC_HICR7 0x88
|
| 22 | #define LPC_HICR8 0x8c
|
Norman James | 8fe5d24 | 2015-10-14 09:40:44 -0500 | [diff] [blame] | 23 | #define SPI_BASE (off_t)0x1e630000
|
| 24 | #define SCU_BASE (off_t)0x1e780000
|
| 25 | #define UART_BASE (off_t)0x1e783000
|
| 26 | #define COM_BASE (off_t)0x1e789000
|
| 27 | #define COM_BASE2 (off_t)0x1e789100
|
| 28 | #define GPIO_BASE (off_t)0x1e6e2000
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 29 |
|
| 30 | static GDBusObjectManagerServer *manager = NULL;
|
| 31 |
|
Norman James | 8fe5d24 | 2015-10-14 09:40:44 -0500 | [diff] [blame] | 32 | void* memmap(int mem_fd,off_t base)
|
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 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);
|
Norman James | c1eb492 | 2015-10-20 22:11:26 -0500 | [diff] [blame] | 75 | devmem(bmcreg+0x9C,0x08060000); //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
|
| 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 |
|
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 | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 106 | #ifdef __arm__
|
| 107 | reg_init();
|
| 108 | #endif
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 109 | control_complete_init(control,invocation);
|
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 110 | control_emit_goto_system_state(control,"BMC_STARTING");
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 111 |
|
| 112 | return TRUE;
|
| 113 | }
|
| 114 |
|
| 115 | static void
|
| 116 | on_bus_acquired (GDBusConnection *connection,
|
| 117 | const gchar *name,
|
| 118 | gpointer user_data)
|
| 119 | {
|
| 120 | ObjectSkeleton *object;
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 121 | cmdline *cmd = user_data;
|
| 122 | if (cmd->argc < 2)
|
| 123 | {
|
| 124 | g_print("No objects created. Put object name(s) on command line\n");
|
| 125 | return;
|
| 126 | }
|
| 127 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
| 128 | int i=0;
|
| 129 | for (i=1;i<cmd->argc;i++)
|
| 130 | {
|
| 131 | gchar *s;
|
| 132 | s = g_strdup_printf ("%s/%s",dbus_object_path,cmd->argv[i]);
|
| 133 | object = object_skeleton_new (s);
|
| 134 | g_free (s);
|
| 135 |
|
| 136 | ControlBmc* control_bmc = control_bmc_skeleton_new ();
|
| 137 | object_skeleton_set_control_bmc (object, control_bmc);
|
| 138 | g_object_unref (control_bmc);
|
| 139 |
|
| 140 | Control* control = control_skeleton_new ();
|
| 141 | object_skeleton_set_control (object, control);
|
| 142 | g_object_unref (control);
|
| 143 |
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 144 | //define method callbacks here
|
| 145 | g_signal_connect (control,
|
| 146 | "handle-init",
|
| 147 | G_CALLBACK (on_init),
|
| 148 | NULL); /* user_data */
|
| 149 |
|
| 150 | /* Export the object (@manager takes its own reference to @object) */
|
| 151 | g_dbus_object_manager_server_export (manager, G_DBUS_OBJECT_SKELETON (object));
|
| 152 | g_object_unref (object);
|
| 153 | }
|
| 154 | /* Export all objects */
|
| 155 | g_dbus_object_manager_server_set_connection (manager, connection);
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 156 | }
|
| 157 |
|
| 158 | static void
|
| 159 | on_name_acquired (GDBusConnection *connection,
|
| 160 | const gchar *name,
|
| 161 | gpointer user_data)
|
| 162 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 163 | }
|
| 164 |
|
| 165 | static void
|
| 166 | on_name_lost (GDBusConnection *connection,
|
| 167 | const gchar *name,
|
| 168 | gpointer user_data)
|
| 169 | {
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 170 | }
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 | /*----------------------------------------------------------------*/
|
| 176 | /* Main Event Loop */
|
| 177 |
|
| 178 | gint
|
| 179 | main (gint argc, gchar *argv[])
|
| 180 | {
|
| 181 | GMainLoop *loop;
|
| 182 | cmdline cmd;
|
| 183 | cmd.argc = argc;
|
| 184 | cmd.argv = argv;
|
| 185 |
|
| 186 | guint id;
|
| 187 | loop = g_main_loop_new (NULL, FALSE);
|
| 188 |
|
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 189 | id = g_bus_own_name (DBUS_TYPE,
|
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 190 | dbus_name,
|
| 191 | G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
|
| 192 | G_BUS_NAME_OWNER_FLAGS_REPLACE,
|
| 193 | on_bus_acquired,
|
| 194 | on_name_acquired,
|
| 195 | on_name_lost,
|
| 196 | &cmd,
|
| 197 | NULL);
|
| 198 |
|
| 199 | g_main_loop_run (loop);
|
| 200 |
|
| 201 | g_bus_unown_name (id);
|
| 202 | g_main_loop_unref (loop);
|
| 203 | return 0;
|
| 204 | }
|