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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/**
******************************************************************************
* @file rt_mtd_nor_partition.c
* @author Urey
* @version V1.0.0
* @date 2017��2��11��
* @brief TODO
******************************************************************************
**/
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <drivers/mtd_nor.h>
#include "mtd_nor_partition.h"
// #define MTD_DEBUG 1
#ifdef MTD_DEBUG
#define MTD_DBG(...) rt_kprintf("[MTD]"),rt_kprintf(__VA_ARGS__)
#else
#define MTD_DBG(...)
#endif
/* RT-Thread device interface */
static rt_err_t mtd_part_blk_init(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t mtd_part_blk_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t mtd_part_blk_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_err_t mtd_part_blk_control(rt_device_t dev, int cmd, void *args)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
RT_ASSERT(dev != RT_NULL);
mtd_part = (struct rt_mtd_nor_partition *)dev;
mtd_nor = (struct rt_mtd_nor_device *)mtd_part->user_data;
switch (cmd)
{
case RT_DEVICE_CTRL_BLK_GETGEOME:
{
struct rt_device_blk_geometry *geometry;
geometry = (struct rt_device_blk_geometry *)args;
if (geometry == RT_NULL)
return -RT_ERROR;
geometry->bytes_per_sector = mtd_nor->block_size;
geometry->sector_count = mtd_part->size / mtd_nor->block_size;
geometry->block_size = mtd_nor->block_size;
break;
}
default:
break;
}
return RT_EOK;
}
static rt_size_t mtd_part_blk_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
rt_size_t read_count = 0;
rt_uint8_t *ptr = (rt_uint8_t *)buffer;
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(size != 0);
mtd_part = (struct rt_mtd_nor_partition *)dev;
mtd_nor = (struct rt_mtd_nor_device *)mtd_part->user_data;
MTD_DBG("%s name = %s,position = %08x,size = %08x\n",__func__,mtd_part->name,pos,size);
if(!(mtd_part->mask_flags & PART_FLAG_RDONLY))
{
MTD_DBG("ERROR: this device is unreadable,mask_flags = %04x\n", mtd_part->mask_flags);
return 0;
}
while(read_count < size)
{/* It'a BLOCK device */
if(((pos + 1) * mtd_nor->block_size) > (mtd_part->offset + mtd_part->size))
{
MTD_DBG("ERROR: read overrun!\n");
break;
}
rt_mtd_nor_read(mtd_nor,pos * mtd_nor->block_size + mtd_part->offset,ptr,mtd_nor->block_size);
pos++;
ptr += mtd_nor->block_size;
read_count++;
}
return read_count;
}
static rt_size_t mtd_part_blk_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
rt_size_t write_count = 0;
rt_uint8_t *ptr = (rt_uint8_t *)buffer;
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(size != 0);
mtd_part = (struct rt_mtd_nor_partition *)dev;
mtd_nor = (struct rt_mtd_nor_device *)mtd_part->user_data;
MTD_DBG("%s name = %s,position = %08x,size = %08x\n",__func__,mtd_part->name,pos,size);
if(!(mtd_part->mask_flags & PART_FLAG_WRONLY))
{
MTD_DBG("ERROR: this device is unwritable,mask_flags = %04x\n", mtd_part->mask_flags);
/* read only partition, ignore this data */
return size;
}
while(write_count < size)
{/* It'a BLOCK device */
if((pos + 1) * mtd_nor->block_size > (mtd_part->offset + mtd_part->size))
{
MTD_DBG("ERROR: write overrun!\n");
break;
}
rt_mtd_nor_erase_block(mtd_nor,(pos * mtd_nor->block_size + mtd_part->offset),mtd_nor->block_size);
rt_mtd_nor_write(mtd_nor,(pos * mtd_nor->block_size + mtd_part->offset),ptr,mtd_nor->block_size);
pos++;
ptr += mtd_nor->block_size;
write_count++;
}
return write_count;
}
static rt_base_t mtd_part_mtd_read_id(struct rt_mtd_nor_device *dev)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
RT_ASSERT(dev != RT_NULL);
mtd_part = (struct rt_mtd_nor_partition *)dev;
mtd_nor = (struct rt_mtd_nor_device *)mtd_part->user_data;
return rt_mtd_nor_read_id(mtd_nor);
}
static rt_size_t mtd_part_mtd_read(struct rt_mtd_nor_device *dev, rt_off_t offset, rt_uint8_t *buffer, rt_uint32_t length)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
RT_ASSERT(dev != RT_NULL);
mtd_part = (struct rt_mtd_nor_partition *)dev;
mtd_nor = (struct rt_mtd_nor_device *)mtd_part->user_data;
MTD_DBG("%s offset = %08x,size = %08x\n",__func__,offset,length);
if(!(mtd_part->mask_flags & PART_FLAG_RDONLY))
{
MTD_DBG("ERROR: this device is unreadable,mask_flags = %04x\n", mtd_part->mask_flags);
return 0;
}
if(mtd_part->mask_flags & PART_TYPE_MTD)
{/* It'a MTD device */
if((offset + length) > mtd_part->size)
{
MTD_DBG("ERROR: read size > partition size, pos=%d, size=%d, partition_size=%d\n", offset, length, mtd_part->size);
return 0;
}
rt_mtd_nor_read(mtd_nor,(mtd_part->offset + offset),buffer,length);
return length;
}
MTD_DBG("ERROR: unknown device type..\n");
return 0;
}
static rt_size_t mtd_part_mtd_write(struct rt_mtd_nor_device *dev, rt_off_t offset, const rt_uint8_t *buffer, rt_uint32_t length)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
RT_ASSERT(dev != RT_NULL);
mtd_part = (struct rt_mtd_nor_partition *)dev;
mtd_nor = (struct rt_mtd_nor_device *)mtd_part->user_data;
MTD_DBG("%s offset = %08x,size = %08x\n",__func__,offset,length);
if(!(mtd_part->mask_flags & PART_FLAG_WRONLY))
{
MTD_DBG("ERROR: this device is unwritable,mask_flags = %04x\n", mtd_part->mask_flags);
/* read only partition, ignore this data */
return length;
}
if(mtd_part->mask_flags & PART_TYPE_MTD)
{ /* It'a MTD device */
if((offset + length) > mtd_part->size)
{
MTD_DBG("ERROR: write size > partition size, pos=%d, size=%d, partition_size=%d\n", offset, length, mtd_part->size);
return 0;
}
/* MTD device skip erase,user do it by himself */
rt_mtd_nor_write(mtd_nor,(mtd_part->offset + offset),buffer,length);
return length;
}
MTD_DBG("ERROR: unknown device type..\n");
return 0;
}
static rt_err_t mtd_part_mtd_erase_block(struct rt_mtd_nor_device* dev, rt_off_t offset, rt_uint32_t length)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
RT_ASSERT(dev != RT_NULL);
mtd_part = (struct rt_mtd_nor_partition *)dev;
mtd_nor = (struct rt_mtd_nor_device *)mtd_part->user_data;
MTD_DBG("%s offset = %08x,size = %08x\n",__func__,offset,length);
if(mtd_part->mask_flags & PART_TYPE_MTD)
{ /* It'a MTD device */
if((offset + length) > mtd_part->size)
{
MTD_DBG("ERROR: erase size > partition size, pos=%d, size=%d, partition_size=%d\n", offset, length, mtd_part->size);
return 0;
}
if(length % mtd_nor->block_size != 0)
{
MTD_DBG("ERROR: erase size must align to BLOCK SIZE\n");
return 0;
}
rt_mtd_nor_erase_block(mtd_nor,(mtd_part->offset + offset),length);
return length;
}
MTD_DBG("ERROR: unknown device type..\n");
return 0;
}
const static struct rt_mtd_nor_driver_ops mtd_part_mtd_ops =
{
mtd_part_mtd_read_id,
mtd_part_mtd_read,
mtd_part_mtd_write,
mtd_part_mtd_erase_block,
};
rt_err_t mtd_nor_init_partition(const char *mtd_name,struct rt_mtd_nor_partition *parts)
{
struct rt_mtd_nor_partition *mtd_part;
struct rt_mtd_nor_device *mtd_nor;
mtd_nor = (struct rt_mtd_nor_device *)rt_device_find(mtd_name);
if(mtd_nor == RT_NULL)
return -RT_EIO;
for (mtd_part = parts; mtd_part->name != RT_NULL; mtd_part++)
{
MTD_DBG("part name: %s\n",mtd_part->name);
/* get partition type */
if(mtd_part->mask_flags & PART_TYPE_BLK)
{ /* It'a a BLOCK device */
/* set device interface */
mtd_part->blk.type = RT_Device_Class_Block;
mtd_part->blk.init = mtd_part_blk_init;
mtd_part->blk.open = mtd_part_blk_open;
mtd_part->blk.read = mtd_part_blk_read;
mtd_part->blk.write = mtd_part_blk_write;
mtd_part->blk.close = mtd_part_blk_close;
mtd_part->blk.control = mtd_part_blk_control;
mtd_part->user_data = mtd_nor;
/* register device */
rt_device_register(&mtd_part->blk,mtd_part->name,RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_STANDALONE);
}
else if(mtd_part->mask_flags & PART_TYPE_MTD)
{ /* It's a MTD device */
MTD_DBG("part name: %s\n",mtd_part->name);
mtd_part->user_data = mtd_nor;
/* Init MTD NOR device interface ... */
mtd_part->mtd.block_size = mtd_nor->block_size;
mtd_part->mtd.block_start = 0;
mtd_part->mtd.block_end = mtd_part->size / mtd_nor->block_size;
mtd_part->mtd.ops = &mtd_part_mtd_ops;
rt_mtd_nor_register_device(mtd_part->name,&mtd_part->mtd);
}
else
{
MTD_DBG("ERROR: unknown device type..\n");
}
}
return RT_EOK;
}
|