forked from datajerk/x49gp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
s3c2410_sram.c
195 lines (158 loc) · 3.94 KB
/
s3c2410_sram.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* $Id: s3c2410_sram.c,v 1.8 2008/12/11 12:18:17 ecd Exp $
*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <x49gp.h>
#include <s3c2410.h>
#include <byteorder.h>
typedef struct {
void *data;
char *filename;
int fd;
uint32_t offset;
size_t size;
} filemap_t;
static int
s3c2410_sram_load(x49gp_module_t *module, GKeyFile *key)
{
filemap_t *filemap = module->user_data;
char *filename;
int error;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
error = x49gp_module_get_filename(module, key, "filename",
"s3c2410-sram", &(filemap->filename),
&filename);
filemap->fd = open(filename, O_RDWR | O_CREAT, 0644);
if (filemap->fd < 0) {
error = -errno;
fprintf(stderr, "%s: %s:%u: open %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
return error;
}
filemap->size = S3C2410_SRAM_SIZE;
if (ftruncate(filemap->fd, filemap->size) < 0) {
error = -errno;
fprintf(stderr, "%s: %s:%u: ftruncate %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
close(filemap->fd);
filemap->fd = -1;
return error;
}
filemap->data = mmap(phys_ram_base + filemap->offset, filemap->size,
PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED,
filemap->fd, 0);
if (filemap->data == (void *) -1) {
error = -errno;
fprintf(stderr, "%s: %s:%u: mmap %s: %s\n",
module->name, __FUNCTION__, __LINE__,
filename, strerror(errno));
g_free(filename);
close(filemap->fd);
filemap->fd = -1;
return error;
}
g_free(filename);
x49gp_schedule_lcd_update(module->x49gp);
return error;
}
static int
s3c2410_sram_save(x49gp_module_t *module, GKeyFile *key)
{
filemap_t *filemap = module->user_data;
int error;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
x49gp_module_set_filename(module, key, "filename", filemap->filename);
error = msync(filemap->data, filemap->size, MS_ASYNC);
if (error) {
fprintf(stderr, "%s:%u: msync: %s\n",
__FUNCTION__, __LINE__, strerror(errno));
return error;
}
error = fsync(filemap->fd);
if (error) {
fprintf(stderr, "%s:%u: fsync: %s\n",
__FUNCTION__, __LINE__, strerror(errno));
return error;
}
return 0;
}
static int
s3c2410_sram_reset(x49gp_module_t *module, x49gp_reset_t reset)
{
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
return 0;
}
static int
s3c2410_sram_init(x49gp_module_t *module)
{
filemap_t *filemap;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
filemap = malloc(sizeof(filemap_t));
if (NULL == filemap) {
fprintf(stderr, "%s:%u: Out of memory\n",
__FUNCTION__, __LINE__);
return -ENOMEM;
}
filemap->size = 0;
filemap->fd = -1;
module->user_data = filemap;
filemap->data = (void *) -1;
filemap->offset = phys_ram_size;
phys_ram_size += S3C2410_SRAM_SIZE;
cpu_register_physical_memory(S3C2410_SRAM_BASE, S3C2410_SRAM_SIZE,
filemap->offset | IO_MEM_RAM);
return 0;
}
static int
s3c2410_sram_exit(x49gp_module_t *module)
{
filemap_t *filemap;
#ifdef DEBUG_X49GP_MODULES
printf("%s: %s:%u\n", module->name, __FUNCTION__, __LINE__);
#endif
if (module->user_data) {
filemap = module->user_data;
if (filemap->data != (void *) -1) {
munmap(filemap->data, filemap->size);
}
if (filemap->fd >= 0) {
close(filemap->fd);
}
free(filemap);
}
x49gp_module_unregister(module);
free(module);
return 0;
}
int
x49gp_s3c2410_sram_init(x49gp_t *x49gp)
{
x49gp_module_t *module;
if (x49gp_module_init(x49gp, "s3c2410-sram",
s3c2410_sram_init,
s3c2410_sram_exit,
s3c2410_sram_reset,
s3c2410_sram_load,
s3c2410_sram_save,
NULL, &module)) {
return -1;
}
return x49gp_module_register(module);
}