summaryrefslogtreecommitdiff
path: root/bsp/simulator/drivers/nanddrv_file.c
blob: 7d465db9a6c2b52d45412bc96632a7228730a8df (plain)
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 */
#include <rtdevice.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NAND_SIM  "nand.bin"

#if 1
#define OOB_SIZE        64
#define PAGE_DATA_SIZE  2048
#define PAGE_PER_BLOCK  64
#define ECC_SIZE       ((PAGE_DATA_SIZE) * 3 / 256)
#define BLOCK_NUM       512
#else
#define OOB_SIZE        16
#define PAGE_DATA_SIZE  512
#define PAGE_PER_BLOCK  32
#define ECC_SIZE       ((PAGE_DATA_SIZE) * 3 / 256)
#define BLOCK_NUM       512
#endif

#define BLOCK_SIZE      (PAGE_SIZE * PAGE_PER_BLOCK)
#define PAGE_SIZE       (PAGE_DATA_SIZE + OOB_SIZE)

static unsigned char block_data[BLOCK_SIZE];
static struct rt_mtd_nand_device _nanddrv_file_device;
static FILE *file = NULL;

static rt_uint8_t CountBitsInByte(rt_uint8_t byte)
{
    rt_uint8_t count = 0;

    while (byte > 0)
    {
        if (byte & 1)
        {
            count++;
        }
        byte >>= 1;
    }

    return count;
}

static void Compute256(const rt_uint8_t *data, rt_uint8_t *code)
{
    rt_uint32_t i;
    rt_uint8_t columnSum = 0;
    rt_uint8_t evenLineCode = 0;
    rt_uint8_t oddLineCode = 0;
    rt_uint8_t evenColumnCode = 0;
    rt_uint8_t oddColumnCode = 0;

    // Xor all bytes together to get the column sum;
    // At the same time, calculate the even and odd line codes
    for (i = 0; i < 256; i++)
    {
        columnSum ^= data[i];

        // If the xor sum of the byte is 0, then this byte has no incidence on
        // the computed code; so check if the sum is 1.
        if ((CountBitsInByte(data[i]) & 1) == 1)
        {
            // Parity groups are formed by forcing a particular index bit to 0
            // (even) or 1 (odd).
            // Example on one byte:
            //
            // bits (dec)  7   6   5   4   3   2   1   0
            //      (bin) 111 110 101 100 011 010 001 000
            //                            '---'---'---'----------.
            //                                                   |
            // groups P4' ooooooooooooooo eeeeeeeeeeeeeee P4     |
            //        P2' ooooooo eeeeeee ooooooo eeeeeee P2     |
            //        P1' ooo eee ooo eee ooo eee ooo eee P1     |
            //                                                   |
            // We can see that:                                  |
            //  - P4  -> bit 2 of index is 0 --------------------'
            //  - P4' -> bit 2 of index is 1.
            //  - P2  -> bit 1 of index if 0.
            //  - etc...
            // We deduce that a bit position has an impact on all even Px if
            // the log2(x)nth bit of its index is 0
            //     ex: log2(4) = 2, bit2 of the index must be 0 (-> 0 1 2 3)
            // and on all odd Px' if the log2(x)nth bit of its index is 1
            //     ex: log2(2) = 1, bit1 of the index must be 1 (-> 0 1 4 5)
            //
            // As such, we calculate all the possible Px and Px' values at the
            // same time in two variables, evenLineCode and oddLineCode, such as
            //     evenLineCode bits: P128  P64  P32  P16  P8  P4  P2  P1
            //     oddLineCode  bits: P128' P64' P32' P16' P8' P4' P2' P1'
            //
            evenLineCode ^= (255 - i);
            oddLineCode ^= i;
        }
    }

    // At this point, we have the line parities, and the column sum. First, We
    // must caculate the parity group values on the column sum.
    for (i = 0; i < 8; i++)
    {
        if (columnSum & 1)
        {
            evenColumnCode ^= (7 - i);
            oddColumnCode ^= i;
        }
        columnSum >>= 1;
    }

    // Now, we must interleave the parity values, to obtain the following layout:
    // Code[0] = Line1
    // Code[1] = Line2
    // Code[2] = Column
    // Line = Px' Px P(x-1)- P(x-1) ...
    // Column = P4' P4 P2' P2 P1' P1 PadBit PadBit
    code[0] = 0;
    code[1] = 0;
    code[2] = 0;

    for (i = 0; i < 4; i++)
    {
        code[0] <<= 2;
        code[1] <<= 2;
        code[2] <<= 2;

        // Line 1
        if ((oddLineCode & 0x80) != 0)
        {
            code[0] |= 2;
        }

        if ((evenLineCode & 0x80) != 0)
        {
            code[0] |= 1;
        }

        // Line 2
        if ((oddLineCode & 0x08) != 0)
        {
            code[1] |= 2;
        }

        if ((evenLineCode & 0x08) != 0)
        {
            code[1] |= 1;
        }

        // Column
        if ((oddColumnCode & 0x04) != 0)
        {
            code[2] |= 2;
        }

        if ((evenColumnCode & 0x04) != 0)
        {
            code[2] |= 1;
        }

        oddLineCode <<= 1;
        evenLineCode <<= 1;
        oddColumnCode <<= 1;
        evenColumnCode <<= 1;
    }

    // Invert codes (linux compatibility)
    code[0] = (~(rt_uint32_t)code[0]);
    code[1] = (~(rt_uint32_t)code[1]);
    code[2] = (~(rt_uint32_t)code[2]);
}

void ecc_hamming_compute256x(const rt_uint8_t *pucData, rt_uint32_t dwSize, rt_uint8_t *puCode)
{
    while (dwSize > 0)
    {
        Compute256(pucData, puCode) ;

        pucData += 256;
        puCode += 3;
        dwSize -= 256;
    }
}

/* read chip id */
static rt_uint32_t nanddrv_file_read_id(struct rt_mtd_nand_device *device)
{
    return 0x00;
}

/* read/write/move page */
static rt_err_t nanddrv_file_read_page(struct rt_mtd_nand_device *device,
                                       rt_off_t page,
                                       rt_uint8_t *data, rt_uint32_t data_len,
                                       rt_uint8_t *spare, rt_uint32_t spare_len)
{
    rt_uint32_t offset;
    rt_uint8_t oob_ecc [ECC_SIZE];
    rt_uint8_t ecc [ECC_SIZE];

    page = page + device->block_start * device->pages_per_block;

    if (page / device->pages_per_block > device->block_end)
    {
        return -RT_EIO;
    }

    /* write page */
    offset = page * PAGE_SIZE;
    if (data != NULL && data_len != 0)
    {
        fseek(file, offset, SEEK_SET);
        fread(data, data_len, 1, file);
        if (data_len == PAGE_DATA_SIZE)
        {
            /* read ecc size */
            fread(oob_ecc, ECC_SIZE, 1, file);

            /* verify ECC */
            ecc_hamming_compute256x(data, PAGE_DATA_SIZE, &ecc[0]);
            if (memcmp(&oob_ecc[0], &ecc[0], ECC_SIZE) != 0)
                return -RT_MTD_EECC;
        }
    }

    if (spare != NULL && spare_len)
    {
        offset = page * PAGE_SIZE + PAGE_DATA_SIZE;
        fseek(file, offset, SEEK_SET);
        fread(spare, spare_len, 1, file);
    }

    return RT_EOK;
}

static rt_err_t nanddrv_file_write_page(struct rt_mtd_nand_device *device,
                                        rt_off_t page,
                                        const rt_uint8_t *data, rt_uint32_t data_len,
                                        const rt_uint8_t *oob, rt_uint32_t spare_len)
{
    rt_uint32_t offset;
    rt_uint8_t ecc[ECC_SIZE];

    page = page + device->block_start * device->pages_per_block;
    if (page / device->pages_per_block > device->block_end)
    {
        return -RT_EIO;
    }

    /* write page */
    offset = page * PAGE_SIZE;
    if (data != RT_NULL && data_len != 0)
    {
        fseek(file, offset, SEEK_SET);
        fwrite(data, data_len, 1, file);

        if (data_len == PAGE_DATA_SIZE)
        {
            /*write the ecc information */
            ecc_hamming_compute256x(data, PAGE_DATA_SIZE, ecc);

            fwrite(ecc, ECC_SIZE, 1, file);
        }
    }

    if (oob != RT_NULL && spare_len != 0)
    {
        offset = page * PAGE_SIZE + PAGE_DATA_SIZE + ECC_SIZE;
        fseek(file, offset, SEEK_SET);
        fwrite(&oob[ECC_SIZE], spare_len-ECC_SIZE, 1, file);
    }

    return RT_EOK;
}

static rt_err_t nanddrv_file_move_page(struct rt_mtd_nand_device *device, rt_off_t from, rt_off_t to)
{
    rt_uint32_t offset;
    rt_uint8_t  page_buffer[PAGE_DATA_SIZE];
    rt_uint8_t  oob_buffer[OOB_SIZE];

    from = from + device->block_start * device->pages_per_block;
    to = to + device->block_start * device->pages_per_block;

    if (from / device->pages_per_block > device->block_end ||
            to / device->pages_per_block > device->block_end)
    {
        return -RT_EIO;
    }

    if (device->plane_num > 1)
    {
        rt_uint32_t mask;
        rt_uint16_t from_block, to_block;

        from_block = (rt_uint16_t)(from / PAGE_PER_BLOCK);
        to_block = (rt_uint16_t)(to / PAGE_PER_BLOCK);
        mask = device->plane_num - 1;

        if ((from_block & mask) != (to_block & mask))
        {
            rt_kprintf("invalid page copy on the block. from [%d] --> to[%d]\n", from_block, to_block);
            return -RT_EIO;
        }
    }

    /* read page */
    offset = from * PAGE_SIZE;
    fseek(file, offset, SEEK_SET);
    fread(page_buffer, sizeof(page_buffer), 1, file);
    fread(oob_buffer, sizeof(oob_buffer), 1, file);

    /* write page */
    offset = to * PAGE_SIZE;
    fseek(file, offset, SEEK_SET);
    fwrite(page_buffer, sizeof(page_buffer), 1, file);
    fwrite(oob_buffer, sizeof(oob_buffer), 1, file);

    return RT_EOK;
}

/* erase block */
static rt_err_t nanddrv_file_erase_block(struct rt_mtd_nand_device *device, rt_uint32_t block)
{
    if (block > BLOCK_NUM) return -RT_EIO;

    /* add the start blocks */
    block = block + device->block_start;

    fseek(file, block * BLOCK_SIZE, SEEK_SET);
    fwrite(block_data, sizeof(block_data), 1, file);

    return RT_EOK;
}

const static struct rt_mtd_nand_driver_ops _ops =
{
    nanddrv_file_read_id,
    nanddrv_file_read_page,
    nanddrv_file_write_page,
    nanddrv_file_move_page,
    nanddrv_file_erase_block,
    RT_NULL,
    RT_NULL,
};

void nand_eraseall(void);

void rt_hw_mtd_nand_init(void)
{
    rt_uint16_t ecc_size;
    rt_uint32_t size;

    memset(block_data, 0xff, sizeof(block_data));
    /* open file */
    file = fopen(NAND_SIM, "rb+");
    if (file == NULL)
    {
        file = fopen(NAND_SIM, "wb+");
    }
    fseek(file, 0, SEEK_END);
    size = ftell(file);

    fseek(file, 0, SEEK_SET);
    if (size < BLOCK_NUM * BLOCK_SIZE)
    {
        rt_uint32_t index;
        fseek(file, 0, SEEK_SET);
        for (index = 0; index < BLOCK_NUM; index ++)
        {
            fwrite(block_data, sizeof(block_data), 1, file);
        }
    }
    fseek(file, 0, SEEK_SET);

    ecc_size = (PAGE_DATA_SIZE) * 3 / 256;
    _nanddrv_file_device.plane_num = 2;
    _nanddrv_file_device.oob_size = OOB_SIZE;
    _nanddrv_file_device.oob_free = OOB_SIZE - ecc_size;
    _nanddrv_file_device.page_size = PAGE_DATA_SIZE;
    _nanddrv_file_device.pages_per_block = PAGE_PER_BLOCK;
    _nanddrv_file_device.block_start = 0;
    _nanddrv_file_device.block_end = BLOCK_NUM / 2;
    _nanddrv_file_device.block_total = _nanddrv_file_device.block_end - _nanddrv_file_device.block_start;
    _nanddrv_file_device.ops = &_ops;

    rt_mtd_nand_register_device("nand0", &_nanddrv_file_device);
}

#if defined(RT_USING_FINSH)
void nand_eraseall()
{
    int index;
    for (index = 0; index < _nanddrv_file_device.block_total; index ++)
    {
        nanddrv_file_erase_block(&_nanddrv_file_device, index);
    }
}

#endif //RT_USING_FINSH