From: Eric van Tassell <evt@evtM17x.(none)>
--- Android.mk | 2 +- Makefile | 2 +- mmioreg.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mmioreg.h | 26 +++++++++++++++++++++ 4 files changed, 104 insertions(+), 2 deletions(-) create mode 100644 mmioreg.c create mode 100644 mmioreg.h
diff --git a/Android.mk b/Android.mk index f448d48..c6ec0d7 100644 --- a/Android.mk +++ b/Android.mk @@ -29,6 +29,6 @@ LOCAL_C_INCLUDES += external/stlport/stlport/ \
LOCAL_SRC_FILES += \ powerdebug.c sensor.c clocks.c regulator.c gpio.c \ - display.c tree.c utils.c mainloop.c delimi.c + display.c tree.c utils.c mainloop.c delimi.c mmioreg.c
include $(BUILD_EXECUTABLE) diff --git a/Makefile b/Makefile index 5007972..71537c9 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ CFLAGS?=-O1 -g -Wall -Wshadow CC?=gcc
OBJS = powerdebug.o sensor.o clocks.o regulator.o gpio.o \ - display.o tree.o utils.o mainloop.o delimi.o + display.o tree.o utils.o mainloop.o delimi.o mmioreg.o
default: powerdebug
diff --git a/mmioreg.c b/mmioreg.c new file mode 100644 index 0000000..6dab4f6 --- /dev/null +++ b/mmioreg.c @@ -0,0 +1,76 @@ +/****************************************************************************** + * Copyright (C) 2012, Linaro Limited. + * + * This file is part of PowerDebug. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eric van Tassell + * - initial API and implementation + *****************************************************************************/ + +#define _GNU_SOURCE +#include <stdio.h> +#undef _GNU_SOURCE +#include <sys/types.h> +#include <stdbool.h> +#include <dirent.h> +#include <string.h> +#include <stdlib.h> +#include <ctype.h> +#include <fcntl.h> +#include <sys/mman.h> +#include "utils.h" +#include "delimi.h" +#include "mmioreg.h" + +struct memobj devmem = { + .path = "/dev/mem", + .fd = -1, + .map_base = NULL, + .last_map_req = 0, + .map_size = 4096, + .map_mask = 4096 - 1 +}; + +void memobj_open(struct memobj *m) +{ + m->fd = open("/dev/mem", O_RDWR | O_SYNC); + if (m->fd == -1) + BAIL_OUT(("%s: failed to open %s\n", __func__, m->path)); +} + +uint32_t memobj_readu32(struct memobj *m, uint32_t addr) +{ + uint32_t ret; + + if (m->fd == -1) + memobj_open(m); + + if (m->last_map_req != addr) { + if (m->map_base != NULL) + munmap(m->map_base, m->map_size); + m->map_base = mmap(0, + m->map_size, + PROT_READ|PROT_WRITE, MAP_SHARED, + m->fd, + addr & ~m->map_mask); + if (m->map_base == (void *)-1) + BAIL_OUT(("could not map system memory")); + m->last_map_req = addr; + } + ret = *((uint32_t *)(((uint32_t)m->map_base) + (addr & m->map_mask))); + return ret; +} + +static void memobj_close(struct memobj *m) +{ + close(m->fd); + m->map_base = NULL; + m->last_map_req = 0; +} + diff --git a/mmioreg.h b/mmioreg.h new file mode 100644 index 0000000..900b1fd --- /dev/null +++ b/mmioreg.h @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright (C) 2012, Linaro Limited. + * + * This file is part of PowerDebug. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Eric van Tassell + * - initial API and implementation + ******************************************************************************/ + +struct memobj { + char *path; + int fd; + void *map_base; + uint32_t last_map_req; + unsigned long map_size; + unsigned long map_mask; +}; + +/* prototypes */ +uint32_t memobj_readu32(struct memobj *m, uint32_t addr);