-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnvram.c
270 lines (202 loc) · 6.3 KB
/
nvram.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*************** NVRAM data ****************************/
#define MRAM_NAME "MRAM"
#define MRAM_NUMB 1
static struct cdev mram_cdev;
static int mram_dev_major = 0;
static struct class *mram_class = NULL;
struct device *mram_device = NULL;
//static int mram_major = 289, mram_minor = 0;
//static dev_t dev_mram;
static int counter_mram = 0;
#define IO_BUF_SIZE (4*1024*1024)
#define IO_BUF_SIZE_METERS (4*1024*1023)
#define RW_BLOCK_SIZE 8
static void *io_buf = NULL;
/*************** NVRAM functions ****************************/
static int open_mram(struct inode *inode, struct file *file)
{
char *kbuf = kmalloc(DGT_BUF_SIZE, GFP_KERNEL);
memcpy(kbuf, &drv_access, DGT_BUF_SIZE);
file->private_data = kbuf;
printk(KERN_INFO "!!! open /dev/%s buf_size=%ld\n", MRAM_NAME, DGT_BUF_SIZE);
counter_mram++;
printk(KERN_INFO "!!! counter: %d\n\n", counter_mram);
printk(KERN_INFO "!!! module refcounter: %d\n\n", module_refcount(THIS_MODULE));
return 0;
}
static int release_mram(struct inode *inode, struct file *file)
{
char *kbuf;
printk(KERN_INFO "!!! release /dev/%s\n", MRAM_NAME);
kbuf = file->private_data;
printk(KERN_INFO "!!! free buffer \n\n");
if(kbuf)
{
kfree(kbuf);
}
file->private_data = NULL;
return 0;
}
static ssize_t read_mram(struct file *file, char __user *buf, size_t lbuf, loff_t *ppos)
{
dgt_xpdev_t *kbuf;
int nbytes;
if(lbuf > IO_BUF_SIZE)
{
printk(KERN_INFO "!!! Read Error too big request block=%ld MAX_SIZE=%d\n", lbuf, IO_BUF_SIZE);
return -EFAULT;
}
if(*ppos > IO_BUF_SIZE)
{
printk(KERN_INFO "!!! Read Error too big request offset=%lld MAX_SIZE=%d\n", *ppos, IO_BUF_SIZE);
return -ENOSPC;
}
//printk(KERN_INFO "!!! read mram lbuf=%ld ppos=%d\n", lbuf, (int)*ppos);
kbuf = (dgt_xpdev_t *)file->private_data;
memcpy_fromio(io_buf, kbuf->iobase + AXI4_MRAM_BANK0_REG + *ppos, lbuf);
// {
// int i;
// unsigned int lCycl, lrest;
// lCycl = lbuf/RW_BLOCK_SIZE;
// lrest = lbuf%RW_BLOCK_SIZE;
// for(i = 0; i < lCycl; i++)
// {
// *((u64*)io_buf + i) = dgt_xpdev_readq(kbuf, AXI4_MRAM_BANK0_REG + *ppos + i*RW_BLOCK_SIZE);
// }
// for(i = 0; i < lrest; i++)
// {
// *((u8*)io_buf + i + lCycl*RW_BLOCK_SIZE) = dgt_xpdev_readb(kbuf, AXI4_MRAM_BANK0_REG + *ppos + lCycl*RW_BLOCK_SIZE + i);
// }
// }
if(copy_to_user(buf, io_buf, lbuf))
{
printk(KERN_INFO "!!! copy data\n");
return -EFAULT;
}
nbytes = lbuf;
*ppos += nbytes;
//printk(KERN_INFO "!!! read device=%s lbuf=%ld nbytes=%d ppos=%d\n\n", MRAM_NAME, lbuf, nbytes, (int)*ppos);
return nbytes;
}
static ssize_t write_mram(struct file *file, const char __user *buf, size_t lbuf, loff_t *ppos)
{
dgt_xpdev_t *kbuf;
//unsigned char data[MAX_RW_SIZE];
int nbytes;
if(*ppos > IO_BUF_SIZE)
{
printk(KERN_INFO "!!! Write Error too big request offset=%lld MAX_SIZE=%d\n", *ppos, IO_BUF_SIZE);
return -ENOSPC;
}
if ((IO_BUF_SIZE_METERS <= *ppos) && (*ppos < IO_BUF_SIZE))
{
printk(KERN_INFO "!!! Try write in meters area offset=%lld\n", *ppos);
return lbuf;
}
kbuf = (dgt_xpdev_t *)file->private_data;
copy_from_user((unsigned char*)io_buf, buf, lbuf);
memcpy_toio(kbuf->iobase + AXI4_MRAM_BANK0_REG + *ppos, io_buf, lbuf);
memcpy_toio(kbuf->iobase + AXI4_MRAM_BANK1_REG + *ppos, io_buf, lbuf);
// {
// int i;
// unsigned int lCycl, lrest;
// lCycl = lbuf/RW_BLOCK_SIZE;
// lrest = lbuf%RW_BLOCK_SIZE;
// for(i = 0; i < lCycl; i++)
// {
// dgt_xpdev_writeq(kbuf, AXI4_MRAM_BANK0_REG + *ppos + i*RW_BLOCK_SIZE, *((u64*)io_buf + i));
// dgt_xpdev_writeq(kbuf, AXI4_MRAM_BANK1_REG + *ppos + i*RW_BLOCK_SIZE, *((u64*)io_buf + i));
// }
// for(i = 0; i < lrest; i++)
// {
// dgt_xpdev_writeb(kbuf, AXI4_MRAM_BANK0_REG + *ppos + lCycl*RW_BLOCK_SIZE + i, *((u8*)io_buf + i));
// dgt_xpdev_writeb(kbuf, AXI4_MRAM_BANK1_REG + *ppos + lCycl*RW_BLOCK_SIZE + i, *((u8*)io_buf + i));
// }
// }
nbytes = lbuf;
*ppos += nbytes;
//printk(KERN_INFO "!!! write device=%s nbytes=%d ppos=%d\n\n", MRAM_NAME, nbytes, (int)*ppos);
return nbytes;
}
static loff_t lseek_mram (struct file *file, loff_t offset, int orig)
{
loff_t testpos = 0;
//printk(KERN_INFO "!!! lseek request offset=%lld MAX_SIZE=%d\n", offset, IO_BUF_SIZE);
if(offset > IO_BUF_SIZE)
{
printk(KERN_INFO "!!! lseek Error too big request offset=%lld MAX_SIZE=%d\n", offset, IO_BUF_SIZE);
return -ENOSPC;
}
switch(orig)
{
case SEEK_SET:
testpos = offset;
break;
case SEEK_CUR:
testpos = file->f_pos + offset;
break;
case SEEK_END:
testpos = IO_BUF_SIZE + offset;
break;
default:
return ~EINVAL;
}
testpos = testpos < IO_BUF_SIZE ? testpos : IO_BUF_SIZE;
testpos = testpos >= 0 ? testpos : 0;
file->f_pos = testpos;
//printk(KERN_INFO "!!! seeking to %ld position orig=%d\n", (long)testpos, orig);
return testpos;
}
static struct file_operations mram_foops =
{
.owner = THIS_MODULE,
.read = read_mram,
.write = write_mram,
.open = open_mram,
.llseek = lseek_mram,
.unlocked_ioctl = NULL,
.release = release_mram,
.mmap = NULL,
};
static void create_mramdev(void)
{
int i = 0;
int err;
dev_t dev;
printk(KERN_DEBUG PREFIX "!!!! create NVRAM\n");
err = alloc_chrdev_region(&dev, 0, MRAM_NUMB, MRAM_NAME);
mram_dev_major = MAJOR(dev);
printk(KERN_DEBUG PREFIX "!!!! create NVRAM mram_dev_major=%d\n", mram_dev_major);
mram_class = class_create(THIS_MODULE, MRAM_NAME);
cdev_init(&mram_cdev, &mram_foops);
mram_cdev.owner = THIS_MODULE;
cdev_add(&mram_cdev, MKDEV(mram_dev_major, 0), 1);
mram_device = device_create(mram_class, NULL, MKDEV(mram_dev_major, 0), NULL, "nvram%d", i);
if (IS_ERR(mram_device))
{
printk(KERN_DEBUG PREFIX "!!!! Error create NVRAM\n");
}
else
{
printk(KERN_DEBUG PREFIX "!!!! create device OK\n");
}
io_buf = kmalloc(IO_BUF_SIZE, GFP_USER);
if(io_buf == NULL)
{
printk(KERN_DEBUG PREFIX "!!!! Error alloc io_buf NVRAM\n");
}
printk(KERN_DEBUG PREFIX "!!!! create_mramdev OK\n");
}
static void remove_mramdev(void)
{
printk(KERN_INFO PREFIX "!!! Remove mram_chrdev\n");
if(io_buf)
{
kfree(io_buf);
io_buf = NULL;
}
device_destroy(mram_class, MKDEV(mram_dev_major, 0));
class_unregister(mram_class);
class_destroy(mram_class);
unregister_chrdev_region(MKDEV(mram_dev_major, 0), MINORMASK);
}