-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsram.c
179 lines (144 loc) · 4.17 KB
/
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
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <linux/errno.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/ioport.h>
#include <asm/delay.h>
#include <linux/delay.h>
#include <asm/system.h>
#include <linux/module.h>
#include "sram.h"
#ifdef CONFIG_DEVFS_FS
#include <linux/devfs_fs_kernel.h>
static devfs_handle_t sram_devfs_handle = NULL;
const char devfsname[]=SRAMNAME;
#endif
static u32* sram_physaddr = ((u32 *) SRAMBASE);
static u32* sram_virtbase;
static unsigned long sram_size = ((unsigned long) SRAMSIZE);
static u32* mmu_physaddr = ((u32 *) MMUBASE);
static int majornum;
static int sram_read (struct file *filp, char *buf, size_t count, loff_t *unused_loff_t);
static int sram_write (struct file *filp, const char *buf, size_t count, loff_t *unused_loff_t);
static loff_t sram_llseek (struct file *filp, loff_t off, int whence);
static int sram_open (struct inode *inode, struct file *filp);
static int sram_release (struct inode *inode, struct file *filp);
static struct file_operations sram_fops = {
owner : THIS_MODULE,
read : sram_read,
write : sram_write,
llseek : sram_llseek,
open : sram_open,
release : sram_release,
};
MODULE_AUTHOR("Jochen Klein");
MODULE_DESCRIPTION("SRAM module");
MODULE_LICENSE("GPL");
static int sram_read(struct file *filp, char *buf, size_t count, loff_t *unused_loff_t)
{
unsigned long f_pos = filp->f_pos;
if (f_pos > sram_size)
return 0;
if (f_pos + count > sram_size)
count = sram_size - f_pos;
copy_to_user(buf, (unsigned char *) sram_virtbase + f_pos, count);
filp->f_pos += count;
return count;
}
static int sram_write (struct file *filp, const char *buf, size_t count, loff_t *unused_loff_t)
{
unsigned long f_pos = filp->f_pos;
if (f_pos > sram_size)
return 0;
if (f_pos + count > sram_size)
count = sram_size - f_pos;
copy_from_user((unsigned char *) sram_virtbase + filp->f_pos, buf, count);
filp->f_pos += count;
return count;
}
loff_t sram_llseek (struct file *filp, loff_t off, int whence)
{
loff_t newpos;
switch (whence) {
case 0: /* SEEK_SET */
newpos = off;
break;
case 1: /* SEEK_CUR */
newpos = filp->f_pos + off;
break;
case 2: /* SEEK_END */
newpos = sram_size + off;
break;
default:
return -EINVAL;
}
if (newpos < 0)
return -EINVAL;
/* newpos %= sram_size; */
filp->f_pos = newpos;
return newpos;
}
static int sram_open (struct inode *inode, struct file *filp)
{
int minor = MINOR(inode->i_rdev);
if (minor != 0) {
printk("Only minor 0 actual\n");
return -ENODEV;
}
/* if (MOD_IN_USE) */
/* return -EBUSY; */
MOD_INC_USE_COUNT;
return 0;
}
static int sram_release (struct inode *inode, struct file *filp)
{
MOD_DEC_USE_COUNT;
return 0;
}
static int sram_init ()
{
int result;
result = devfs_register_chrdev(SRAMMAJO, SRAMNAME, &sram_fops);
if (result < 0) {
printk(KERN_ERR "Can not get major number %d\n", SRAMMAJO);
return result;
} else {
majornum = result;
printk(KERN_INFO "Major number of sram-Module: %d\n", majornum);
}
sram_devfs_handle = devfs_register(NULL,
devfsname,
DEVFS_FL_DEFAULT,
result,
0,
S_IFCHR | S_IRUGO | S_IWUGO,
&sram_fops,
NULL );
// map SRAM to address space
u32* mmu_virtbase = (u32*) ioremap_nocache((u32)mmu_physaddr, MMUSIZE);
printk(KERN_INFO "mmap register: 0x%08x\n", mmu_virtbase[0x20]);
mmu_virtbase[0x90 / 4] = 0xa0000d01;
iounmap((void*) mmu_virtbase);
// remapping of SRAM
sram_virtbase = (u32*) ioremap_nocache((u32)sram_physaddr, SRAMSIZE);
return 0;
}
int init_module(void)
{
printk(KERN_INFO "sram module init (compiled "__DATE__", "__TIME__")\n");
return sram_init();
}
void cleanup_module(void)
{
devfs_unregister(sram_devfs_handle);
iounmap((void *)sram_virtbase);
unregister_chrdev(majornum, SRAMNAME);
}