-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuffs_nandif.c
359 lines (293 loc) · 11.3 KB
/
uffs_nandif.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* RT-Thread Device Interface for uffs
*/
#include <rtthread.h>
#include <rtdevice.h>
#include "dfs_uffs.h"
static int nand_init_flash(uffs_Device *dev)
{
return UFFS_FLASH_NO_ERR;
}
static int nand_release_flash(uffs_Device *dev)
{
return UFFS_FLASH_NO_ERR;
}
static int nand_erase_block(uffs_Device *dev, unsigned block)
{
int res;
res = rt_mtd_nand_erase_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR;
}
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
static int nand_check_block(uffs_Device *dev, unsigned block)
{
int res;
res = rt_mtd_nand_check_block(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
}
static int nand_mark_badblock(uffs_Device *dev, unsigned block)
{
int res;
res = rt_mtd_nand_mark_badblock(RT_MTD_NAND_DEVICE(dev->_private), block);
return res == RT_EOK ? UFFS_FLASH_NO_ERR : UFFS_FLASH_IO_ERR;
}
#endif
#if (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_NONE) || (RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_SOFT)
static int nand_read_page(uffs_Device *dev,
u32 block,
u32 page,
u8 *data,
int data_len,
u8 *ecc,
rt_uint8_t *spare,
int spare_len)
{
int res;
page = block * dev->attr->pages_per_block + page;
if (data == NULL && spare == NULL)
{
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here
#else
/* check block status: bad or good */
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
rt_memset(spare, 0, UFFS_MAX_SPARE_SIZE);
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
res = spare[dev->attr->block_status_offs] == 0xFF ?
UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
return res;
#endif
}
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
return UFFS_FLASH_NO_ERR;
}
static int nand_write_page(uffs_Device *dev,
u32 block,
u32 page,
const u8 *data,
int data_len,
const u8 *spare,
int spare_len)
{
int res;
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
page = block * dev->attr->pages_per_block + page;
if (data == NULL && spare == NULL)
{
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here
#else
/* mark bad block */
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE);
spare[dev->attr->block_status_offs] = 0x00;
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
if (res != RT_EOK)
goto __error;
#endif
}
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
if (res != RT_EOK)
goto __error;
return UFFS_FLASH_NO_ERR;
__error:
return UFFS_FLASH_IO_ERR;
}
const uffs_FlashOps nand_ops =
{
nand_init_flash, /* InitFlash() */
nand_release_flash, /* ReleaseFlash() */
nand_read_page, /* ReadPage() */
NULL, /* ReadPageWithLayout */
nand_write_page, /* WritePage() */
NULL, /* WritePageWithLayout */
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
nand_check_block,
nand_mark_badblock,
#else
NULL, /* IsBadBlock(), let UFFS take care of it. */
NULL, /* MarkBadBlock(), let UFFS take care of it. */
#endif
nand_erase_block, /* EraseBlock() */
};
void uffs_setup_storage(struct uffs_StorageAttrSt *attr,
struct rt_mtd_nand_device *nand)
{
rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
// attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */
attr->page_data_size = nand->page_size; /* page data size */
attr->pages_per_block = nand->pages_per_block; /* pages per block */
attr->spare_size = nand->oob_size; /* page spare size */
attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */
attr->ecc_size = 0; /* ecc size is 0 , the uffs will calculate the ecc size*/
attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */
attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */
}
#elif RT_CONFIG_UFFS_ECC_MODE == UFFS_ECC_HW_AUTO
static int WritePageWithLayout(uffs_Device *dev,
u32 block,
u32 page,
const u8 *data,
int data_len,
const u8 *ecc, //NULL
const uffs_TagStore *ts)
{
int res;
int spare_len;
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
page = block * dev->attr->pages_per_block + page;
spare_len = dev->mem.spare_data_size;
if (data == NULL && ts == NULL)
{
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here
#else
/* mark bad block */
rt_memset(spare, 0xFF, UFFS_MAX_SPARE_SIZE);
spare[dev->attr->block_status_offs] = 0x00;
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
if (res != RT_EOK)
goto __error;
dev->st.io_write++;
return UFFS_FLASH_NO_ERR;
#endif
}
if (data != NULL && data_len != 0)
{
RT_ASSERT(data_len == dev->attr->page_data_size);
dev->st.page_write_count++;
dev->st.io_write += data_len;
}
if (ts != RT_NULL)
{
uffs_FlashMakeSpare(dev, ts, RT_NULL, (u8 *)spare);
dev->st.spare_write_count++;
dev->st.io_write += spare_len;
}
res = rt_mtd_nand_write(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
if (res != RT_EOK)
goto __error;
return UFFS_FLASH_NO_ERR;
__error:
return UFFS_FLASH_IO_ERR;
}
static URET ReadPageWithLayout(uffs_Device *dev,
u32 block,
u32 page,
u8 *data,
int data_len,
u8 *ecc, //NULL
uffs_TagStore *ts,
u8 *ecc_store) //NULL
{
int res = UFFS_FLASH_NO_ERR;
int spare_len;
rt_uint8_t spare[UFFS_MAX_SPARE_SIZE];
RT_ASSERT(UFFS_MAX_SPARE_SIZE >= dev->attr->spare_size);
page = block * dev->attr->pages_per_block + page;
spare_len = dev->mem.spare_data_size;
if (data == RT_NULL && ts == RT_NULL)
{
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
RT_ASSERT(0); //should not be here
#else
/* check block good or bad */
rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, RT_NULL, 0,
spare, dev->attr->spare_size);//dev->mem.spare_data_size
dev->st.io_read++;
res = spare[dev->attr->block_status_offs] == 0xFF ?
UFFS_FLASH_NO_ERR : UFFS_FLASH_BAD_BLK;
return res;
#endif
}
if (data != RT_NULL)
{
dev->st.io_read += data_len;
dev->st.page_read_count++;
}
res = rt_mtd_nand_read(RT_MTD_NAND_DEVICE(dev->_private),
page, data, data_len, spare, spare_len);
if (res == 0)
res = UFFS_FLASH_NO_ERR;
else if (res == -1)
{
//TODO ecc correct, add code to use hardware do ecc correct
res = UFFS_FLASH_ECC_OK;
}
else
res = UFFS_FLASH_ECC_FAIL;
if (ts != RT_NULL)
{
// unload ts and ecc from spare, you can modify it if you like
uffs_FlashUnloadSpare(dev, (const u8 *)spare, ts, RT_NULL);
if ((spare[spare_len - 1] == 0xFF) && (res == UFFS_FLASH_NO_ERR))
res = UFFS_FLASH_NOT_SEALED;
dev->st.io_read += spare_len;
dev->st.spare_read_count++;
}
return res;
}
const uffs_FlashOps nand_ops =
{
nand_init_flash, /* InitFlash() */
nand_release_flash, /* ReleaseFlash() */
NULL, /* ReadPage() */
ReadPageWithLayout, /* ReadPageWithLayout */
NULL, /* WritePage() */
WritePageWithLayout,/* WritePageWithLayout */
#if defined(RT_UFFS_USE_CHECK_MARK_FUNCITON)
nand_check_block,
nand_mark_badblock,
#else
NULL, /* IsBadBlock(), let UFFS take care of it. */
NULL, /* MarkBadBlock(), let UFFS take care of it. */
#endif
nand_erase_block, /* EraseBlock() */
};
static rt_uint8_t hw_flash_data_layout[UFFS_SPARE_LAYOUT_SIZE] =
{
0x05, 0x08, 0xFF, 0x00
};
static rt_uint8_t hw_flash_ecc_layout[UFFS_SPARE_LAYOUT_SIZE] =
{
0x00, 0x04, 0xFF, 0x00
};
RT_WEAK void uffs_setup_storage(struct uffs_StorageAttrSt *attr,
struct rt_mtd_nand_device *nand)
{
rt_memset(attr, 0, sizeof(struct uffs_StorageAttrSt));
// attr->total_blocks = nand->end_block - nand->start_block + 1;/* no use */
attr->page_data_size = nand->page_size; /* page data size */
attr->pages_per_block = nand->pages_per_block; /* pages per block */
attr->spare_size = nand->oob_size; /* page spare size */
attr->ecc_opt = RT_CONFIG_UFFS_ECC_MODE; /* ecc option */
attr->ecc_size = nand->oob_size-nand->oob_free; /* ecc size */
attr->block_status_offs = attr->ecc_size; /* indicate block bad or good, offset in spare */
attr->layout_opt = RT_CONFIG_UFFS_LAYOUT; /* let UFFS do the spare layout */
/* calculate the ecc layout array */
hw_flash_data_layout[0] = attr->ecc_size + 1; /* ecc size + 1byte block status */
hw_flash_data_layout[1] = 0x08;
hw_flash_data_layout[2] = 0xFF;
hw_flash_data_layout[3] = 0x00;
hw_flash_ecc_layout[0] = 0;
hw_flash_ecc_layout[1] = attr->ecc_size;
hw_flash_ecc_layout[2] = 0xFF;
hw_flash_ecc_layout[3] = 0x00;
/* initialize _uffs_data_layout and _uffs_ecc_layout */
rt_memcpy(attr->_uffs_data_layout, hw_flash_data_layout, UFFS_SPARE_LAYOUT_SIZE);
rt_memcpy(attr->_uffs_ecc_layout, hw_flash_ecc_layout, UFFS_SPARE_LAYOUT_SIZE);
attr->data_layout = attr->_uffs_data_layout;
attr->ecc_layout = attr->_uffs_ecc_layout;
}
#endif