summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2021-06-09 17:50:21 +0200
committerDenis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>2021-06-09 17:54:11 +0200
commit9890249a384db95450f3f1511ee39aaa766f04f4 (patch)
tree7de7d5ad0cad73227b1f834d452c9c5a086c74f4
parent2214b529ca9f57d7bc9d81c78a701b7109b8e69f (diff)
downloadexynos-gpio-tool-9890249a384db95450f3f1511ee39aaa766f04f4.tar.gz
exynos-gpio-tool-9890249a384db95450f3f1511ee39aaa766f04f4.tar.bz2
exynos-gpio-tool-9890249a384db95450f3f1511ee39aaa766f04f4.zip
Add devmem2 backend
Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
-rw-r--r--Makefile3
-rw-r--r--devmem2.c125
-rw-r--r--devmem2.h22
-rw-r--r--exynos4412_gpios_data.c4
-rw-r--r--main.c2
-rw-r--r--memory.c2
-rw-r--r--memory.h2
-rw-r--r--memory_devmem2.c48
8 files changed, 202 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 0000e8f..fc7524b 100644
--- a/Makefile
+++ b/Makefile
@@ -2,10 +2,11 @@
SOURCE := \
compatible_devices.c \
+ devmem2.c \
exynos4412_gpios.c \
exynos4412_gpios_data.c \
main.c \
- memory.c \
+ memory_devmem2.c \
modem_gpios_data.c \
tests.c \
diff --git a/devmem2.c b/devmem2.c
new file mode 100644
index 0000000..967dbc2
--- /dev/null
+++ b/devmem2.c
@@ -0,0 +1,125 @@
+/*
+ * Modified from devmem2.c which has the following header:
+ */
+/*
+ * devmem2.c: Simple program to read/write from/to any location in memory.
+ *
+ * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
+ *
+ *
+ * This software has been developed for the LART computing board
+ * (http://www.lart.tudelft.nl/). The development has been sponsored by
+ * the Mobile MultiMedia Communications (http://www.mmc.tudelft.nl/)
+ * and Ubiquitous Communications (http://www.ubicom.tudelft.nl/)
+ * projects.
+ *
+ * The author can be reached at:
+ *
+ * Jan-Derk Bakker
+ * Information and Communication Theory Group
+ * Faculty of Information Technology and Systems
+ * Delft University of Technology
+ * P.O. Box 5031
+ * 2600 GA Delft
+ * The Netherlands
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <termios.h>
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include "devmem2.h"
+
+#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
+ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
+
+#define MAP_SIZE 4096UL
+#define MAP_MASK (MAP_SIZE - 1)
+
+int devmem2(int debug, off_t target, int access_type,
+ unsigned long *read_result, unsigned long *writeval)
+{
+ int fd;
+ void *map_base, *virt_addr;
+
+ if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
+ if (debug)
+ printf("/dev/mem opened.\n");
+ fflush(stdout);
+
+ /* Map one page */
+ map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
+ if(map_base == (void *) -1) FATAL;
+ if (debug)
+ printf("Memory mapped at address %p.\n", map_base);
+ fflush(stdout);
+
+ virt_addr = map_base + (target & MAP_MASK);
+ switch(access_type) {
+ case 'b':
+ *read_result = *((unsigned char *) virt_addr);
+ break;
+ case 'h':
+ *read_result = *((unsigned short *) virt_addr);
+ break;
+ case 'w':
+ *read_result = *((unsigned long *) virt_addr);
+ break;
+ default:
+ fprintf(stderr, "Illegal data type '%c'.\n", access_type);
+ exit(2);
+ }
+ if (debug)
+ printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr,
+ *read_result);
+ fflush(stdout);
+
+ if(writeval != NULL) {
+ switch(access_type) {
+ case 'b':
+ *((unsigned char *) virt_addr) = *writeval;
+ *read_result = *((unsigned char *) virt_addr);
+ break;
+ case 'h':
+ *((unsigned short *) virt_addr) = *writeval;
+ *read_result = *((unsigned short *) virt_addr);
+ break;
+ case 'w':
+ *((unsigned long *) virt_addr) = *writeval;
+ *read_result = *((unsigned long *) virt_addr);
+ break;
+ }
+ if (debug)
+ printf("Written 0x%X; readback 0x%X\n", *writeval, *read_result);
+ fflush(stdout);
+ }
+
+ if(munmap(map_base, MAP_SIZE) == -1) FATAL;
+ close(fd);
+ return 0;
+}
+
diff --git a/devmem2.h b/devmem2.h
new file mode 100644
index 0000000..41afa5c
--- /dev/null
+++ b/devmem2.h
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef DEVMEM2_H
+#define DEVMEM2_H
+int devmem2(int debug, off_t target, int access_type,
+ unsigned long *read_result, unsigned long *writeval);
+#endif /* DEVMEM2_H */
diff --git a/exynos4412_gpios_data.c b/exynos4412_gpios_data.c
index deed252..9b82cff 100644
--- a/exynos4412_gpios_data.c
+++ b/exynos4412_gpios_data.c
@@ -385,7 +385,7 @@ int decode_gpio_data(int debug, char *bank, uint32_t gpio_offset,
struct gpio_bank_data *gpio_bank_data;
struct gpio_register_data *gpio_register_data;
unsigned int addr;
- unsigned int register_value;
+ unsigned long register_value;
int rc;
gpio_bank_data = get_gpio_bank_data(bank);
@@ -420,7 +420,7 @@ int decode_gpio_data(int debug, char *bank, uint32_t gpio_offset,
}
if (debug)
- printf("%s: %s @ 0x%x = 0x%x\n", __func__, bank,
+ printf("%s: %s @ 0x%x = 0x%lx\n", __func__, bank,
addr, register_value);
return gpio_register_data->get_value(register_value, gpio_offset);
diff --git a/main.c b/main.c
index e608022..e6b88f8 100644
--- a/main.c
+++ b/main.c
@@ -124,7 +124,7 @@ int main(int argc, char *argv[])
{
struct memory_mapping *memory_mapping;
- int debug = 1;
+ int debug = 0;
int rc;
assert(argc >= 1);
diff --git a/memory.c b/memory.c
index a162b1d..f5da796 100644
--- a/memory.c
+++ b/memory.c
@@ -211,7 +211,7 @@ off_t *phys_to_virt(int debug, struct memory_mapping *memory_mapping, off_t phys
}
int read_word(int debug, struct memory_mapping *memory_mapping,
- off_t addr, unsigned int *result)
+ off_t addr, unsigned long *result)
{
off_t *virt_addr;
diff --git a/memory.h b/memory.h
index 5ba91bf..ff2591f 100644
--- a/memory.h
+++ b/memory.h
@@ -24,6 +24,6 @@ int init_memory_mapping(int debug, struct memory_mapping **memory_mapping_pp);
int exit_memory_mapping(int debug, struct memory_mapping *memory_mapping);
int read_word(int debug, struct memory_mapping *memory_mapping,
- off_t addr, unsigned int *result);
+ off_t addr, unsigned long *result);
#endif /* MEMORY_H */
diff --git a/memory_devmem2.c b/memory_devmem2.c
new file mode 100644
index 0000000..ac25fc2
--- /dev/null
+++ b/memory_devmem2.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2021 Denis 'GNUtoo' Carikli <GNUtoo@cyberdimension.org>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include "devmem2.h"
+#include "memory.h"
+
+int init_memory_mapping(int debug, struct memory_mapping **memory_mapping_pp)
+{
+ return 0;
+}
+
+int read_word(int debug, struct memory_mapping *memory_mapping,
+ off_t addr, unsigned long *result)
+{
+ int rc;
+
+ rc = devmem2(debug, addr, 'w', result, NULL);
+ if (rc)
+ printf("%s: devmem2 failed with error %d\n", __func__, rc);
+
+ return rc;
+}