diff options
Diffstat (limited to 'libopenbmc_intf')
-rw-r--r-- | libopenbmc_intf/Makefile | 20 | ||||
-rw-r--r-- | libopenbmc_intf/gpio.c | 232 | ||||
-rw-r--r-- | libopenbmc_intf/gpio.h | 36 | ||||
-rw-r--r-- | libopenbmc_intf/openbmc.h | 59 | ||||
l--------- | libopenbmc_intf/openbmc_intf.c | 1 | ||||
l--------- | libopenbmc_intf/openbmc_intf.h | 1 |
6 files changed, 349 insertions, 0 deletions
diff --git a/libopenbmc_intf/Makefile b/libopenbmc_intf/Makefile new file mode 100644 index 0000000..9cd7be6 --- /dev/null +++ b/libopenbmc_intf/Makefile @@ -0,0 +1,20 @@ +PACKAGE_DEPS=gio-unix-2.0 glib-2.0 +INSTALLDEPS=install-lib +CLEANDEPS=clean-lib +DEFAULT_ALL=$(LIBOBMC) +CFLAGS+=-iquote ../gdbus +LIBOBMC=openbmc_intf + +$(LIBOBMC): %: %.o gpio.o + $(CC) -shared $(LDFLAGS) -Wl,-soname,lib$(LIBOBMC).so \ + -o lib$@.so.1 $^ $(LDLIBS) + +install-lib: + @mkdir -p $(DESTDIR)$(libdir) + install lib$(LIBOBMC).so.1 $(DESTDIR)$(libdir) + ln -s lib$(LIBOBMC).so.1 $(DESTDIR)$(libdir)/lib$(LIBOBMC).so + +clean-lib: + rm -f lib$(LIBOBMC).so.1 + +include ../rules.mk diff --git a/libopenbmc_intf/gpio.c b/libopenbmc_intf/gpio.c new file mode 100644 index 0000000..25a9df8 --- /dev/null +++ b/libopenbmc_intf/gpio.c @@ -0,0 +1,232 @@ +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <fcntl.h> +#include <unistd.h> +#include <argp.h> +#include <sys/stat.h> +#include <sys/mman.h> +#include "interfaces/openbmc_intf.h" +#include "gpio.h" + + +int gpio_writec(GPIO* gpio, char value) +{ + g_assert (gpio != NULL); + int rc = GPIO_OK; + char buf[1]; + buf[0] = value; + if (write(gpio->fd, buf, 1) != 1) + { + rc = GPIO_WRITE_ERROR; + } + return rc; +} + +int gpio_write(GPIO* gpio, uint8_t value) +{ + g_assert (gpio != NULL); + int rc = GPIO_OK; + char buf[1]; + buf[0] = '0'; + if (value==1) + { + buf[0]='1'; + } + if (write(gpio->fd, buf, 1) != 1) + { + rc = GPIO_WRITE_ERROR; + } + return rc; +} + +int gpio_read(GPIO* gpio, uint8_t *value) +{ + g_assert (gpio != NULL); + char buf[1]; + int r = GPIO_OK; + if (gpio->fd <= 0) + { + r = GPIO_ERROR; + } + else + { + if (read(gpio->fd,&buf,1) != 1) + { + r = GPIO_READ_ERROR; + } else { + if (buf[0]=='1') { + *value = 1; + } else { + *value = 0; + } + } + } + return r; +} +int gpio_clock_cycle(GPIO* gpio, int num_clks) { + g_assert (gpio != NULL); + int i=0; + int r=GPIO_OK; + for (i=0;i<num_clks;i++) { + if (gpio_writec(gpio,'0') == -1) { + r = GPIO_WRITE_ERROR; + break; + } + if (gpio_writec(gpio,'1') == -1) { + r = GPIO_WRITE_ERROR; + break; + } + } + return r; +} + +// Gets the gpio device path from gpio manager object +int gpio_init(GDBusConnection *connection, GPIO* gpio) +{ + int rc = GPIO_OK; + GDBusProxy *proxy; + GError *error; + GVariant *result; + + error = NULL; + g_assert_no_error (error); + error = NULL; + proxy = g_dbus_proxy_new_sync (connection, + G_DBUS_PROXY_FLAGS_NONE, + NULL, /* GDBusInterfaceInfo */ + "org.openbmc.managers.System", /* name */ + "/org/openbmc/managers/System", /* object path */ + "org.openbmc.managers.System", /* interface */ + NULL, /* GCancellable */ + &error); + if (error != NULL) { + return GPIO_LOOKUP_ERROR; + } + + result = g_dbus_proxy_call_sync (proxy, + "gpioInit", + g_variant_new ("(s)", gpio->name), + G_DBUS_CALL_FLAGS_NONE, + -1, + NULL, + &error); + + if (error != NULL) { + return GPIO_LOOKUP_ERROR; + } + g_assert (result != NULL); + g_variant_get (result, "(&si&s)", &gpio->dev,&gpio->num,&gpio->direction); + g_print("GPIO Lookup: %s = %d,%s\n",gpio->name,gpio->num,gpio->direction); + + //export and set direction + char dev[254]; + char data[4]; + int fd; + do { + struct stat st; + + sprintf(dev,"%s/gpio%d/value",gpio->dev,gpio->num); + //check if gpio is exported, if not export + int result = stat(dev, &st); + if (result) + { + sprintf(dev,"%s/export",gpio->dev); + fd = open(dev, O_WRONLY); + if (fd == GPIO_ERROR) { + rc = GPIO_OPEN_ERROR; + break; + } + sprintf(data,"%d",gpio->num); + rc = write(fd,data,strlen(data)); + close(fd); + if (rc != strlen(data)) { + rc = GPIO_WRITE_ERROR; + break; + } + } + const char* file = "edge"; + if (strcmp(gpio->direction,"in")==0 || strcmp(gpio->direction,"out")==0) + { + file = "direction"; + } + sprintf(dev,"%s/gpio%d/%s",gpio->dev,gpio->num,file); + fd = open(dev,O_WRONLY); + if (fd == GPIO_ERROR) { + rc = GPIO_WRITE_ERROR; + break; + } + rc = write(fd,gpio->direction,strlen(gpio->direction)); + if (rc != strlen(gpio->direction)) { + rc = GPIO_WRITE_ERROR; + break; + } + + close(fd); + rc = GPIO_OK; + } while(0); + + return rc; +} + + + + +char* get_gpio_dev(GPIO* gpio) +{ + char* buf; + sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); + return buf; +} + +int gpio_open_interrupt(GPIO* gpio, GIOFunc func, gpointer user_data) +{ + int rc = GPIO_OK; + char buf[255]; + sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); + gpio->fd = open(buf, O_RDONLY | O_NONBLOCK ); + gpio->irq_inited = false; + if (gpio->fd == -1) + { + rc = GPIO_OPEN_ERROR; + } + else + { + GIOChannel* channel = g_io_channel_unix_new( gpio->fd); + guint id = g_io_add_watch( channel, G_IO_PRI, func, user_data ); + } + return rc; +} + +int gpio_open(GPIO* gpio) +{ + g_assert (gpio != NULL); + // open gpio for writing or reading + char buf[254]; + int rc = 0; + gpio->fd = -1; + if (gpio->direction == NULL) { + return GPIO_OPEN_ERROR; + } + if (strcmp(gpio->direction,"in")==0) + { + sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); + gpio->fd = open(buf, O_RDONLY); + } + else + { + sprintf(buf, "%s/gpio%d/value", gpio->dev, gpio->num); + gpio->fd = open(buf, O_RDWR); + + } + if (gpio->fd == -1) { + return GPIO_OPEN_ERROR; + } + return GPIO_OK; +} + +void gpio_close(GPIO* gpio) +{ + close(gpio->fd); +} diff --git a/libopenbmc_intf/gpio.h b/libopenbmc_intf/gpio.h new file mode 100644 index 0000000..8cfc59f --- /dev/null +++ b/libopenbmc_intf/gpio.h @@ -0,0 +1,36 @@ +#ifndef __OBJECTS_GPIO_UTILITIES_H__ +#define __OBJECTS_GPIO_UTILITIES_H__ + +#include <stdint.h> +#include <gio/gio.h> +#include <stdbool.h> + +typedef struct { + gchar* name; + gchar* dev; + uint16_t num; + gchar* direction; + int fd; + bool irq_inited; +} GPIO; + + +//gpio functions +#define GPIO_OK 0x00 +#define GPIO_ERROR 0x01 +#define GPIO_OPEN_ERROR 0x02 +#define GPIO_INIT_ERROR 0x04 +#define GPIO_READ_ERROR 0x08 +#define GPIO_WRITE_ERROR 0x10 +#define GPIO_LOOKUP_ERROR 0x20 + +int gpio_init(GDBusConnection*, GPIO*); +void gpio_close(GPIO*); +int gpio_open(GPIO*); +int gpio_open_interrupt(GPIO*, GIOFunc, gpointer); +int gpio_write(GPIO*, uint8_t); +int gpio_writec(GPIO*, char); +int gpio_clock_cycle(GPIO*, int); +int gpio_read(GPIO*,uint8_t*); + +#endif diff --git a/libopenbmc_intf/openbmc.h b/libopenbmc_intf/openbmc.h new file mode 100644 index 0000000..c1c0335 --- /dev/null +++ b/libopenbmc_intf/openbmc.h @@ -0,0 +1,59 @@ +#ifndef __OPENBMC_H__ +#define __OPENBMC_H__ + +#include <stdint.h> +#include <stdio.h> + +//select which dbus +#define DBUS_TYPE G_BUS_TYPE_SYSTEM + +// Macros +#define GET_VARIANT(v) g_variant_get_variant(v) +#define GET_VARIANT_D(v) g_variant_get_double(g_variant_get_variant(v)) +#define GET_VARIANT_U(v) g_variant_get_uint32(g_variant_get_variant(v)) +#define GET_VARIANT_B(v) g_variant_get_byte(g_variant_get_variant(v)) +#define NEW_VARIANT_D(v) g_variant_new_variant(g_variant_new_double(v)) +#define NEW_VARIANT_U(v) g_variant_new_variant(g_variant_new_uint32(v)) +#define NEW_VARIANT_B(v) g_variant_new_variant(g_variant_new_byte(v)) +#define VARIANT_COMPARE(x,y) g_variant_compare(GET_VARIANT(x),GET_VARIANT(y)) + +#ifdef __arm__ +static inline void devmem(void* addr, uint32_t val) +{ + printf("devmem %p = 0x%08x\n",addr,val); + asm volatile("" : : : "memory"); + *(volatile uint32_t *)addr = val; +} +static inline uint32_t devmem_read(void* addr) +{ + asm volatile("" : : : "memory"); + return *(volatile uint32_t *)addr; +} +//static inline devmem(uint32_t reg, uint32_t val) +//{ +// printf("devmem 0x%08x = 0x%08x\n",reg,val); +// //void* r = (void*)reg; + // write_reg(reg,val); +//} +#else +static inline devmem(uint32_t val, uint32_t reg) +{ +} +static inline uint32_t devmem_read(void* addr) +{ + return 0; +} + +#endif + +typedef struct { + gint argc; + gchar **argv; + GMainLoop *loop; + gpointer user_data; + +} cmdline; + + + +#endif diff --git a/libopenbmc_intf/openbmc_intf.c b/libopenbmc_intf/openbmc_intf.c new file mode 120000 index 0000000..3ac5de7 --- /dev/null +++ b/libopenbmc_intf/openbmc_intf.c @@ -0,0 +1 @@ +../gdbus/interfaces/openbmc_intf.c
\ No newline at end of file diff --git a/libopenbmc_intf/openbmc_intf.h b/libopenbmc_intf/openbmc_intf.h new file mode 120000 index 0000000..957c7b7 --- /dev/null +++ b/libopenbmc_intf/openbmc_intf.h @@ -0,0 +1 @@ +../gdbus/interfaces/openbmc_intf.h
\ No newline at end of file |