Norman James | e56d838 | 2015-10-22 14:35:04 -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 | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 16 | static const gchar* dbus_name = "org.openbmc.control.Bmc";
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 17 | static const gchar* i2c_dev = "/sys/bus/i2c/devices";
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 18 |
|
| 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 James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 28 | #define COM_BASE (off_t)0x1e789000
|
| 29 | #define COM_BASE2 (off_t)0x1e789100
|
| 30 | #define GPIO_BASE (off_t)0x1e6e2000
|
| 31 |
|
| 32 | static GDBusObjectManagerServer *manager = NULL;
|
| 33 |
|
| 34 | void* 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 |
|
| 47 | void 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 James | c54a629 | 2015-10-31 17:35:17 -0500 | [diff] [blame] | 61 | devmem(bmcreg+LPC_HICR7,0x30000C00); //32M PNOR
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 62 | 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 James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 76 | bmcreg = memmap(mem_fd,COM_BASE);
|
| 77 | devmem(bmcreg+0x9C,0x00000000); //Set UART routing
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 78 |
|
| 79 | bmcreg = memmap(mem_fd,SCU_BASE);
|
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 80 | devmem(bmcreg+0x00,0x9e82fce7);
|
| 81 | //devmem(bmcreg+0x00,0x9f82fce7); // B2?
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 82 | devmem(bmcreg+0x04,0x0370e677);
|
Norman James | 9e9eba1 | 2015-11-19 16:05:57 -0600 | [diff] [blame] | 83 |
|
| 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 James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 89 | 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 James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 96 | 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 James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 107 | close(mem_fd);
|
| 108 | }
|
| 109 |
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 110 | int init_i2c_driver(int i2c_bus, const char* device, int address,bool delete)
|
| 111 | {
|
| 112 | char dev[255];
|
Norman James | 4fa8e4f | 2015-11-11 10:41:38 -0600 | [diff] [blame] | 113 | g_print("Initing: device = %s, address = %02x\n",device,address);
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 114 | if (!delete) {
|
Norman James | 372d788 | 2015-11-11 12:48:44 -0600 | [diff] [blame] | 115 | sprintf(dev,"%s/i2c-%d/new_device",i2c_dev,i2c_bus);
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 116 | } else {
|
Norman James | 372d788 | 2015-11-11 12:48:44 -0600 | [diff] [blame] | 117 | sprintf(dev,"%s/i2c-%d/delete_device",i2c_dev,i2c_bus);
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 118 | }
|
| 119 | int fd = open(dev, O_WRONLY);
|
| 120 | if (fd == -1) {
|
Norman James | 4fa8e4f | 2015-11-11 10:41:38 -0600 | [diff] [blame] | 121 | g_print("ERROR control_bmc: Unable to open device %s\n",dev);
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 122 | return 1;
|
| 123 | }
|
Norman James | 372d788 | 2015-11-11 12:48:44 -0600 | [diff] [blame] | 124 | if (!delete) {
|
| 125 | sprintf(dev,"%s 0x%02x",device,address);
|
| 126 | } else {
|
| 127 | sprintf(dev,"0x%02x",address);
|
| 128 | }
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 129 | int rc = write(fd,dev,strlen(dev));
|
| 130 | close(fd);
|
| 131 | if (rc != strlen(dev)) {
|
Norman James | 4fa8e4f | 2015-11-11 10:41:38 -0600 | [diff] [blame] | 132 | g_print("ERROR control_bmc: Unable to write %s\n",dev);
|
Norman James | 2a76020 | 2015-11-11 10:31:26 -0600 | [diff] [blame] | 133 | return 2;
|
| 134 | }
|
| 135 | return 0;
|
| 136 | }
|
| 137 |
|
| 138 |
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 139 | static gboolean
|
| 140 | on_init (Control *control,
|
| 141 | GDBusMethodInvocation *invocation,
|
| 142 | gpointer user_data)
|
| 143 | {
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 144 | control_complete_init(control,invocation);
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 145 |
|
| 146 | return TRUE;
|
| 147 | }
|
| 148 | gboolean go(gpointer user_data)
|
| 149 | {
|
| 150 | cmdline *cmd = user_data;
|
| 151 | Control* control = object_get_control((Object*)cmd->user_data);
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 152 | #ifdef __arm__
|
| 153 | reg_init();
|
| 154 | #endif
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 155 | control_emit_goto_system_state(control,"BMC_STARTING");
|
| 156 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 157 | return FALSE;
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 158 | }
|
| 159 |
|
| 160 | static void
|
| 161 | on_bus_acquired (GDBusConnection *connection,
|
| 162 | const gchar *name,
|
| 163 | gpointer user_data)
|
| 164 | {
|
| 165 | ObjectSkeleton *object;
|
| 166 | cmdline *cmd = user_data;
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 167 | manager = g_dbus_object_manager_server_new (dbus_object_path);
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 168 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 169 | 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 James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 173 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 174 | ControlBmc* control_bmc = control_bmc_skeleton_new ();
|
| 175 | object_skeleton_set_control_bmc (object, control_bmc);
|
| 176 | g_object_unref (control_bmc);
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 177 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 178 | Control* control = control_skeleton_new ();
|
| 179 | object_skeleton_set_control (object, control);
|
| 180 | g_object_unref (control);
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 181 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 182 | //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 James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 192 | /* Export all objects */
|
| 193 | g_dbus_object_manager_server_set_connection (manager, connection);
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 194 |
|
| 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 James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 199 | }
|
| 200 |
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 201 |
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 202 | static void
|
| 203 | on_name_acquired (GDBusConnection *connection,
|
| 204 | const gchar *name,
|
| 205 | gpointer user_data)
|
| 206 | {
|
Norman James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 207 |
|
| 208 |
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 209 | }
|
| 210 |
|
| 211 | static void
|
| 212 | on_name_lost (GDBusConnection *connection,
|
| 213 | const gchar *name,
|
| 214 | gpointer user_data)
|
| 215 | {
|
| 216 | }
|
| 217 |
|
| 218 |
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 219 | /*----------------------------------------------------------------*/
|
| 220 | /* Main Event Loop */
|
| 221 |
|
| 222 | gint
|
| 223 | main (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 James | 978d2f3 | 2015-10-28 12:45:52 -0500 | [diff] [blame] | 232 | cmd.loop = loop;
|
Norman James | e56d838 | 2015-10-22 14:35:04 -0500 | [diff] [blame] | 233 |
|
| 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 | }
|