-
Notifications
You must be signed in to change notification settings - Fork 0
/
mzapo_phys.c
56 lines (42 loc) · 1.5 KB
/
mzapo_phys.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*******************************************************************
Simple program to check LCD functionality on MicroZed
based MZ_APO board designed by Petr Porazil at PiKRON
mzapo_phys.h - mapping of the physical address to process
(C) Copyright 2017 by Pavel Pisa
e-mail: [email protected]
homepage: http://cmp.felk.cvut.cz/~pisa
company: http://www.pikron.com/
license: any combination of GPL, LGPL, MPL or BSD licenses
*******************************************************************/
#define _POSIX_C_SOURCE 200112L
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include "mzapo_phys.h"
const char *map_phys_memdev="/dev/mem";
void *map_phys_address(off_t region_base, size_t region_size, int opt_cached)
{
unsigned long mem_window_size;
unsigned long pagesize;
unsigned char *mm;
unsigned char *mem;
int fd;
fd = open(map_phys_memdev, O_RDWR | (!opt_cached? O_SYNC: 0));
if (fd < 0) {
fprintf(stderr, "cannot open %s\n", map_phys_memdev);
return NULL;
}
pagesize=sysconf(_SC_PAGESIZE);
mem_window_size = ((region_base & (pagesize-1)) + region_size + pagesize-1) & ~(pagesize-1);
mm = (unsigned char *)mmap(NULL, mem_window_size, PROT_WRITE|PROT_READ,
MAP_SHARED, fd, region_base & ~(pagesize-1));
mem = mm + (region_base & (pagesize-1));
if (mm == MAP_FAILED) {
fprintf(stderr,"mmap error\n");
return NULL;
}
return (void *)mem;
}