-
-
Notifications
You must be signed in to change notification settings - Fork 340
/
norflash.cpp
275 lines (206 loc) · 4.85 KB
/
norflash.cpp
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
270
271
272
273
274
275
/*
* Copyright 2012, Haiku, Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Ithamar R. Adema <ithamar@upgrade-android.com>
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <drivers/device_manager.h>
#include <drivers/KernelExport.h>
#include <drivers/Drivers.h>
#include <kernel/OS.h>
//#define TRACE_NORFLASH
#ifdef TRACE_NORFLASH
#define TRACE(x...) dprintf("nor: " x)
#else
#define TRACE(x...)
#endif
#define NORFLASH_DEVICE_MODULE_NAME "drivers/disk/norflash/device_v1"
#define NORFLASH_DRIVER_MODULE_NAME "drivers/disk/norflash/driver_v1"
#define NORFLASH_ADDR 0x00000000
struct nor_driver_info {
device_node *node;
size_t blocksize;
size_t totalsize;
area_id id;
void *mapped;
};
static device_manager_info *sDeviceManager;
static status_t
nor_init_device(void *_info, void **_cookie)
{
TRACE("init_device\n");
nor_driver_info *info = (nor_driver_info*)_info;
info->mapped = NULL;
info->blocksize = 128 * 1024;
info->totalsize = info->blocksize * 256;
info->id = map_physical_memory("NORFlash", NORFLASH_ADDR, info->totalsize, B_ANY_KERNEL_ADDRESS, B_READ_AREA, &info->mapped);
if (info->id < 0)
return info->id;
*_cookie = info;
return B_OK;
}
static void
nor_uninit_device(void *_cookie)
{
TRACE("uninit_device\n");
nor_driver_info *info = (nor_driver_info*)_cookie;
if (info)
delete_area(info->id);
}
static status_t
nor_open(void *deviceCookie, const char *path, int openMode,
void **_cookie)
{
TRACE("open(%s)\n", path);
*_cookie = deviceCookie;
return B_OK;
}
static status_t
nor_close(void *_cookie)
{
TRACE("close()\n");
return B_OK;
}
static status_t
nor_free(void *_cookie)
{
TRACE("free()\n");
return B_OK;
}
static status_t
nor_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{
nor_driver_info *info = (nor_driver_info*)cookie;
TRACE("ioctl(%ld,%lu)\n", op, length);
switch (op) {
case B_GET_GEOMETRY:
{
device_geometry *deviceGeometry = (device_geometry*)buffer;
deviceGeometry->removable = false;
deviceGeometry->bytes_per_sector = info->blocksize;
deviceGeometry->sectors_per_track = info->totalsize / info->blocksize;
deviceGeometry->cylinder_count = 1;
deviceGeometry->head_count = 1;
deviceGeometry->device_type = B_DISK;
deviceGeometry->removable = false;
deviceGeometry->read_only = true;
deviceGeometry->write_once = false;
return B_OK;
}
break;
case B_GET_DEVICE_NAME:
strlcpy((char*)buffer, "NORFlash", length);
break;
}
return B_ERROR;
}
static status_t
nor_read(void *_cookie, off_t position, void *data, size_t *numbytes)
{
nor_driver_info *info = (nor_driver_info*)_cookie;
TRACE("read(%Ld,%lu)\n", position, *numbytes);
if (position + *numbytes > info->totalsize)
*numbytes = info->totalsize - (position + *numbytes);
memcpy(data, info->mapped + position, *numbytes);
return B_OK;
}
static status_t
nor_write(void *_cookie, off_t position, const void *data, size_t *numbytes)
{
TRACE("write(%Ld,%lu)\n", position, *numbytes);
*numbytes = 0;
return B_ERROR;
}
static float
nor_supports_device(device_node *parent)
{
TRACE("supports_device\n");
return 0.6;
}
static status_t
nor_register_device(device_node *node)
{
TRACE("register_device\n");
// ready to register
device_attr attrs[] = {
{ NULL }
};
return sDeviceManager->register_node(node, NORFLASH_DRIVER_MODULE_NAME,
attrs, NULL, NULL);
}
static status_t
nor_init_driver(device_node *node, void **cookie)
{
TRACE("init_driver\n");
nor_driver_info *info = (nor_driver_info*)malloc(sizeof(nor_driver_info));
if (info == NULL)
return B_NO_MEMORY;
memset(info, 0, sizeof(*info));
info->node = node;
*cookie = info;
return B_OK;
}
static void
nor_uninit_driver(void *_cookie)
{
TRACE("uninit_driver\n");
nor_driver_info *info = (nor_driver_info*)_cookie;
free(info);
}
static status_t
nor_register_child_devices(void *_cookie)
{
TRACE("register_child_devices\n");
nor_driver_info *info = (nor_driver_info*)_cookie;
status_t status;
status = sDeviceManager->publish_device(info->node, "disk/nor/0/raw",
NORFLASH_DEVICE_MODULE_NAME);
return status;
}
struct device_module_info sNORFlashDiskDevice = {
{
NORFLASH_DEVICE_MODULE_NAME,
0,
NULL
},
nor_init_device,
nor_uninit_device,
NULL, //nor_remove,
nor_open,
nor_close,
nor_free,
nor_read,
nor_write,
NULL, // nor_io,
nor_ioctl,
NULL, // select
NULL, // deselect
};
struct driver_module_info sNORFlashDiskDriver = {
{
NORFLASH_DRIVER_MODULE_NAME,
0,
NULL
},
nor_supports_device,
nor_register_device,
nor_init_driver,
nor_uninit_driver,
nor_register_child_devices,
NULL, // rescan
NULL, // removed
};
module_dependency module_dependencies[] = {
{ B_DEVICE_MANAGER_MODULE_NAME, (module_info**)&sDeviceManager },
{ }
};
module_info *modules[] = {
(module_info*)&sNORFlashDiskDriver,
(module_info*)&sNORFlashDiskDevice,
NULL
};