guowenxue
2020-08-22 4f9a1657573f0a1ce24e63ad618ab60d238287b2
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
/*
|  ECC MAIN  | BAD  | RES  |       YAFFS_TAG       |
|     4      |  4   |  8   |          48           |
*/
 
#include "k9f2g08_s3c.h"
 
/* From yaffs_guts.h */
#define YAFFS_OK    1
#define YAFFS_FAIL  0
 
 
int k9f2g08_init(void)
{
    return YAFFS_OK;
}
 
int k9f2g08_deinit(void)
{
    return YAFFS_OK;
}
 
static unsigned char wait_busy_status(void)
{
    unsigned char rc;
 
    for (rc=0; rc <40; rc++);
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x70);  // read status
    do {
        rc = bsp_nand_read_data8();
    } while ((rc & (1<<6)) == 0);
    bsp_nand_set_cs(1);
 
    return rc;
}
 
int k9f2g08_mark_block_bad(unsigned int blk_num)
{
    int row_addr = blk_num * 64;
    int col_addr = 2048;
 
    unsigned char status;
 
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x80);  // write command
    bsp_nand_write_addr((unsigned char)(col_addr >> 0));
    bsp_nand_write_addr((unsigned char)(col_addr >> 8));
    bsp_nand_write_addr((unsigned char)(row_addr >> 0));
    bsp_nand_write_addr((unsigned char)(row_addr >> 8));
    bsp_nand_write_addr((unsigned char)(row_addr >> 16));
 
    bsp_nand_write_data32(0xFFFFFFFF);
    bsp_nand_write_data32(0x00000000);
    bsp_nand_write_data32(0xFFFFFFFF);
    bsp_nand_write_data32(0xFFFFFFFF);
    bsp_nand_write_cmd(0x10);
    bsp_nand_set_cs(1);
 
    status = wait_busy_status();
 
    return (status & (1<<0)) ? YAFFS_FAIL : YAFFS_OK;
}
 
int k9f2g08_write(unsigned int chunk, const unsigned char *dat, const unsigned char *spare, unsigned int spare_size)
{
    int i;
    int col_addr;
    unsigned char status;
 
    if (dat) {
        col_addr = 0;
    } else {
        col_addr = 2048;
    }
 
 
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x80);  // write command
    bsp_nand_write_addr((unsigned char)(col_addr >> 0));
    bsp_nand_write_addr((unsigned char)(col_addr >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 0));
    bsp_nand_write_addr((unsigned char)(chunk >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 16));
 
    bsp_nand_start_main_ecc();
    if (dat) {
        for (i=0; i<2048; i++)
            bsp_nand_write_data8(*dat++);
    }
 
    bsp_nand_stop_main_ecc();
 
    bsp_nand_write_data32(bsp_nand_read_spare_ecc0());
    bsp_nand_write_data32(0xFFFFFFFF);
    bsp_nand_write_data32(0xFFFFFFFF);
    bsp_nand_write_data32(0xFFFFFFFF);
 
    if (spare) {
        for (i=0; i<spare_size; i++)
            bsp_nand_write_data8(*spare++);
    }
 
    bsp_nand_write_cmd(0x10);
    bsp_nand_set_cs(1);
 
    status = wait_busy_status();
 
    return (status & (1<<0)) ? YAFFS_FAIL : YAFFS_OK;
}
 
int k9f2g08_read(unsigned int chunk, unsigned char *dat, unsigned char *spare, unsigned int spare_size, int *eccStatus)
{
    int i;
    int col_addr;
    unsigned char *p = dat;
    unsigned int ecc = 0;
    int err_bit;
    int err_byte;
 
    if (dat) {
        col_addr = 0;
    } else {
        col_addr = 2048 + 16;
    }
 
 
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x00);  // read command
    bsp_nand_write_addr((unsigned char)(col_addr >> 0));
    bsp_nand_write_addr((unsigned char)(col_addr >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 0));
    bsp_nand_write_addr((unsigned char)(chunk >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 16));
    bsp_nand_write_cmd(0x30);  // read
    bsp_nand_wait_busy();
 
    if (dat) {
        bsp_nand_start_main_ecc();
        for (i=0; i<2048; i++)
            *p++ = bsp_nand_read_data8();
 
        bsp_nand_stop_main_ecc();
 
        ecc = bsp_nand_read_data32();
 
        bsp_nand_read_data32();
        bsp_nand_read_data32();
        bsp_nand_read_data32();
    }
    if (spare) {
        p = spare;
        for (i=0; i<spare_size; i++)
            *p++ = bsp_nand_read_data8();
    }
 
    bsp_nand_set_cs(1);
 
    if (dat) {
        i = bsp_nand_main_check_ecc(ecc, &err_byte, &err_bit);
        if (i == 0) {
            *eccStatus = 0;
            return 0;
        }
 
        if (i < 0) {
            *eccStatus = -1;
            return 0;
        }
 
        *eccStatus = 1;
        dat[err_byte] ^= (1<<err_bit);
    }
 
    return YAFFS_OK;
}
 
int k9f2g08_erase_block(unsigned int blk_num)
{
    unsigned char status;
    int chunk = blk_num * 64;
 
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x60);  // erase command
    bsp_nand_write_addr((unsigned char)(chunk >> 0));
    bsp_nand_write_addr((unsigned char)(chunk >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 16));
    bsp_nand_write_cmd(0xD0);  // erase
    bsp_nand_set_cs(1);
 
    status = wait_busy_status();
    return (status & (1<<0)) ? YAFFS_FAIL : YAFFS_OK;
}
 
int k9f2g08_is_block_ok(unsigned int blk_num)
{
    int chunk = blk_num * 64;
    int col_addr = 2048 + 4;
 
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x00);  // read command
    bsp_nand_write_addr((unsigned char)(col_addr >> 0));
    bsp_nand_write_addr((unsigned char)(col_addr >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 0));
    bsp_nand_write_addr((unsigned char)(chunk >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 16));
    bsp_nand_write_cmd(0x30);  // read
    bsp_nand_wait_busy();
 
    int tmp = bsp_nand_read_data32();
    bsp_nand_set_cs(1);
 
    if (tmp == 0xFFFFFFFF) {
        return 1;
    }
    return 0;
}
 
 
int k9f2g08_read_id(unsigned int *id)
{
    int rc = -1;
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x90);  // read command
    bsp_nand_write_addr((unsigned char)0);
    do {
        if ((rc = bsp_nand_read_data8()) != 0xEC)
            break;
        *id = bsp_nand_read_data32();
        rc = 0;
    } while(0);
 
    bsp_nand_set_cs(1);
 
    return rc;
}
 
int k9f2g08_read_ll(unsigned int chunk, unsigned char *dat, unsigned char *spare)
{
    int i;
    int col_addr;
    unsigned char *p;
 
    if (dat) {
        col_addr = 0;
    } else {
        col_addr = 2048;
    }
 
 
    bsp_nand_set_cs(0);
    bsp_nand_write_cmd(0x00);  // read command
    bsp_nand_write_addr((unsigned char)(col_addr >> 0));
    bsp_nand_write_addr((unsigned char)(col_addr >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 0));
    bsp_nand_write_addr((unsigned char)(chunk >> 8));
    bsp_nand_write_addr((unsigned char)(chunk >> 16));
    bsp_nand_write_cmd(0x30);  // read
 
    bsp_nand_wait_busy();
 
    if (dat) {
        p = dat;
        for (i=0; i<2048; i++)
            *p++ = bsp_nand_read_data8();
    }
    if (spare) {
        p = spare;
        for (i=0; i<64; i++)
            *p++ = bsp_nand_read_data8();
    }
 
    bsp_nand_set_cs(1);
 
    return 1;
}