Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
6
//    This file is part of emBIOS.
7
//
8
//    emBIOS is free software: you can redistribute it and/or
9
//    modify it under the terms of the GNU General Public License as
10
//    published by the Free Software Foundation, either version 2 of the
11
//    License, or (at your option) any later version.
12
//
13
//    emBIOS is distributed in the hope that it will be useful,
14
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
15
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
//    See the GNU General Public License for more details.
17
//
18
//    You should have received a copy of the GNU General Public License along
19
//    with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include "global.h"
54 theseven 25
#include "panic.h"
26
#include "thread.h"
2 theseven 27
#include "util.h"
28
#include "timer.h"
29
#include "nand.h"
54 theseven 30
#include "pmu.h"
31
#include "mmu.h"
2 theseven 32
 
33
#define NAND_CMD_READ       0x00
34
#define NAND_CMD_PROGCNFRM  0x10
35
#define NAND_CMD_READ2      0x30
36
#define NAND_CMD_BLOCKERASE 0x60
37
#define NAND_CMD_GET_STATUS 0x70
38
#define NAND_CMD_PROGRAM    0x80
39
#define NAND_CMD_ERASECNFRM 0xD0
40
#define NAND_CMD_RESET      0xFF
41
 
42
#define NAND_STATUS_READY   0x40
43
 
44
static const struct nand_device_info_type nand_deviceinfotable[] =
45
{
46
    {0x1580F1EC, 1024, 968, 0x40, 6, 2, 1, 2, 1},
47
    {0x1580DAEC, 2048, 1936, 0x40, 6, 2, 1, 2, 1},
48
    {0x15C1DAEC, 2048, 1936, 0x40, 6, 2, 1, 2, 1},
49
    {0x1510DCEC, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
50
    {0x95C1DCEC, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
51
    {0x2514DCEC, 2048, 1936, 0x80, 7, 2, 1, 2, 1},
52
    {0x2514D3EC, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
53
    {0x2555D3EC, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
54
    {0x2555D5EC, 8192, 7744, 0x80, 7, 2, 1, 2, 1},
55
    {0x2585D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
56
    {0x9580DCAD, 4096, 3872, 0x40, 6, 3, 2, 3, 2},
57
    {0xA514D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
58
    {0xA550D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
59
    {0xA560D5AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
60
    {0xA555D5AD, 8192, 7744, 0x80, 7, 3, 2, 3, 2},
61
    {0xA585D598, 8320, 7744, 0x80, 7, 3, 1, 2, 1},
62
    {0xA584D398, 4160, 3872, 0x80, 7, 3, 1, 2, 1},
63
    {0x95D1D32C, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
64
    {0x1580DC2C, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
65
    {0x15C1D32C, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
66
    {0x9590DC2C, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
67
    {0xA594D32C, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
68
    {0x2584DC2C, 2048, 1936, 0x80, 7, 2, 1, 2, 1},
69
    {0xA5D5D52C, 8192, 7744, 0x80, 7, 3, 2, 2, 1},
70
    {0x95D1D389, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
71
    {0x1580DC89, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
72
    {0x15C1D389, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
73
    {0x9590DC89, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
74
    {0xA594D389, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
75
    {0x2584DC89, 2048, 1936, 0x80, 7, 2, 1, 2, 1},
76
    {0xA5D5D589, 8192, 7744, 0x80, 7, 2, 1, 2, 1},
77
    {0xA514D320, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
78
    {0xA555D520, 8192, 3872, 0x80, 7, 2, 1, 2, 1}
79
};
80
 
81
uint8_t nand_tunk1[4];
82
uint8_t nand_twp[4];
83
uint8_t nand_tunk2[4];
84
uint8_t nand_tunk3[4];
61 theseven 85
int nand_type[4];
54 theseven 86
int nand_powered = 0;
87
int nand_interleaved = 0;
88
int nand_cached = 0;
89
long nand_last_activity_value = -1;
90
static uint32_t nand_stack[0x80];
2 theseven 91
 
54 theseven 92
static struct mutex nand_mtx;
93
static struct wakeup nand_wakeup;
94
static struct mutex ecc_mtx;
95
static struct wakeup ecc_wakeup;
2 theseven 96
 
54 theseven 97
static uint8_t nand_data[0x800] CACHEALIGN_ATTR;
98
static uint8_t nand_ctrl[0x200] CACHEALIGN_ATTR;
99
static uint8_t nand_spare[0x40] CACHEALIGN_ATTR;
100
static uint8_t nand_ecc[0x30] CACHEALIGN_ATTR;
2 theseven 101
 
54 theseven 102
 
103
uint32_t nand_unlock(uint32_t rc)
104
{
105
    nand_last_activity_value = USEC_TIMER;
106
    mutex_unlock(&nand_mtx);
107
    return rc;
108
}
109
 
110
uint32_t ecc_unlock(uint32_t rc)
111
{
112
    mutex_unlock(&ecc_mtx);
113
    return rc;
114
}
115
 
116
uint32_t nand_timeout(uint32_t timeout)
117
{
118
    if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
119
    else
120
    {
121
        yield();
122
        return 0;
123
    }
124
}
125
 
2 theseven 126
uint32_t nand_wait_rbbdone(void)
127
{
54 theseven 128
    uint32_t timeout = USEC_TIMER + 20000;
129
    while (!(FMCSTAT & FMCSTAT_RBBDONE))
130
        if (nand_timeout(timeout)) return 1;
2 theseven 131
    FMCSTAT = FMCSTAT_RBBDONE;
132
    return 0;
133
}
134
 
135
uint32_t nand_wait_cmddone(void)
136
{
54 theseven 137
    uint32_t timeout = USEC_TIMER + 20000;
138
    while (!(FMCSTAT & FMCSTAT_CMDDONE))
139
        if (nand_timeout(timeout)) return 1;
2 theseven 140
    FMCSTAT = FMCSTAT_CMDDONE;
141
    return 0;
142
}
143
 
144
uint32_t nand_wait_addrdone(void)
145
{
54 theseven 146
    uint32_t timeout = USEC_TIMER + 20000;
147
    while (!(FMCSTAT & FMCSTAT_ADDRDONE))
148
        if (nand_timeout(timeout)) return 1;
2 theseven 149
    FMCSTAT = FMCSTAT_ADDRDONE;
150
    return 0;
151
}
152
 
153
uint32_t nand_wait_chip_ready(uint32_t bank)
154
{
54 theseven 155
    uint32_t timeout = USEC_TIMER + 20000;
156
    while (!(FMCSTAT & (FMCSTAT_BANK0READY << bank)))
157
        if (nand_timeout(timeout)) return 1;
2 theseven 158
    FMCSTAT = (FMCSTAT_BANK0READY << bank);
159
    return 0;
160
}
161
 
162
void nand_set_fmctrl0(uint32_t bank, uint32_t flags)
163
{
164
    FMCTRL0 = (nand_tunk1[bank] << 16) | (nand_twp[bank] << 12)
165
            | (1 << 11) | 1 | (1 << (bank + 1)) | flags;
166
}
167
 
168
uint32_t nand_send_cmd(uint32_t cmd)
169
{
170
    FMCMD = cmd;
171
    return nand_wait_rbbdone();
172
}
173
 
174
uint32_t nand_send_address(uint32_t page, uint32_t offset)
175
{
176
    FMANUM = 4;
177
    FMADDR0 = (page << 16) | offset;
178
    FMADDR1 = (page >> 16) & 0xFF;
179
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
180
    return nand_wait_cmddone();
181
}
182
 
183
uint32_t nand_reset(uint32_t bank)
184
{
185
    nand_set_fmctrl0(bank, 0);
186
    if (nand_send_cmd(NAND_CMD_RESET)) return 1;
187
    if (nand_wait_chip_ready(bank)) return 1;
188
    FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
189
    sleep(1000);
190
    return 0;
191
}
192
 
193
uint32_t nand_wait_status_ready(uint32_t bank)
194
{
54 theseven 195
    uint32_t timeout = USEC_TIMER + 20000;
2 theseven 196
    nand_set_fmctrl0(bank, 0);
197
    if ((FMCSTAT & (FMCSTAT_BANK0READY << bank)))
198
        FMCSTAT = (FMCSTAT_BANK0READY << bank);
199
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
200
    if (nand_send_cmd(NAND_CMD_GET_STATUS)) return 1;
201
    while (1)
202
    {
54 theseven 203
        if (nand_timeout(timeout)) return 1;
2 theseven 204
        FMDNUM = 0;
205
        FMCTRL1 = FMCTRL1_DOREADDATA;
206
        if (nand_wait_addrdone()) return 1;
207
        if ((FMFIFO & NAND_STATUS_READY)) break;
208
        FMCTRL1 = FMCTRL1_CLEARRFIFO;
209
    }
210
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
211
    return nand_send_cmd(NAND_CMD_READ);
212
}
213
 
54 theseven 214
void nand_transfer_data_start(uint32_t bank, uint32_t direction,
215
                              void* buffer, uint32_t size)
2 theseven 216
{
217
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
218
    FMDNUM = size - 1;
219
    FMCTRL1 = FMCTRL1_DOREADDATA << direction;
220
    DMACON3 = (2 << DMACON_DEVICE_SHIFT)
221
            | (direction << DMACON_DIRECTION_SHIFT)
222
            | (2 << DMACON_DATA_SIZE_SHIFT)
223
            | (3 << DMACON_BURST_LEN_SHIFT);
224
    while ((DMAALLST & DMAALLST_CHAN3_MASK))
225
        DMACOM3 = DMACOM_CLEARBOTHDONE;
226
    DMABASE3 = (uint32_t)buffer;
227
    DMATCNT3 = (size >> 4) - 1;
228
    clean_dcache();
229
    DMACOM3 = 4;
54 theseven 230
}
231
 
232
uint32_t nand_transfer_data_collect(uint32_t direction)
233
{
234
    uint32_t timeout = USEC_TIMER + 20000;
235
    while ((DMAALLST & DMAALLST_DMABUSY3))
236
        if (nand_timeout(timeout)) return 1;
2 theseven 237
    if (!direction) invalidate_dcache();
238
    if (nand_wait_addrdone()) return 1;
239
    if (!direction) FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
240
    else FMCTRL1 = FMCTRL1_CLEARRFIFO;
241
    return 0;
242
}
243
 
54 theseven 244
uint32_t nand_transfer_data(uint32_t bank, uint32_t direction,
245
                            void* buffer, uint32_t size)
2 theseven 246
{
54 theseven 247
    nand_transfer_data_start(bank, direction, buffer, size);
248
    uint32_t rc = nand_transfer_data_collect(direction);
249
    return rc;
250
}
251
 
252
void ecc_start(uint32_t size, void* databuffer, void* sparebuffer, uint32_t type)
253
{
254
    mutex_lock(&ecc_mtx, TIMEOUT_BLOCK);
2 theseven 255
    ECC_INT_CLR = 1;
256
    SRCPND = INTMSK_ECC;
257
    ECC_UNK1 = size;
258
    ECC_DATA_PTR = (uint32_t)databuffer;
259
    ECC_SPARE_PTR = (uint32_t)sparebuffer;
260
    clean_dcache();
54 theseven 261
    ECC_CTRL = type;
262
}
263
 
264
uint32_t ecc_collect(void)
265
{
266
    uint32_t timeout = USEC_TIMER + 20000;
267
    while (!(SRCPND & INTMSK_ECC))
268
        if (nand_timeout(timeout)) return ecc_unlock(1);
2 theseven 269
    invalidate_dcache();
270
    ECC_INT_CLR = 1;
271
    SRCPND = INTMSK_ECC;
54 theseven 272
    return ecc_unlock(ECC_RESULT);
2 theseven 273
}
274
 
54 theseven 275
uint32_t ecc_decode(uint32_t size, void* databuffer, void* sparebuffer)
276
{
277
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTDECODING);
278
    uint32_t rc = ecc_collect();
279
    return rc;
280
}
281
 
2 theseven 282
uint32_t ecc_encode(uint32_t size, void* databuffer, void* sparebuffer)
283
{
54 theseven 284
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTENCODING);
285
    ecc_collect();
2 theseven 286
    return 0;
287
}
288
 
289
uint32_t nand_check_empty(uint8_t* buffer)
290
{
291
    uint32_t i, count;
292
    count = 0;
293
    for (i = 0; i < 0x40; i++) if (buffer[i] != 0xFF) count++;
294
    if (count < 2) return 1;
295
    return 0;
296
}
297
 
298
uint32_t nand_get_chip_type(uint32_t bank)
299
{
54 theseven 300
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
2 theseven 301
    uint32_t result;
61 theseven 302
    if (nand_reset(bank)) return nand_unlock(0xFFFFFFFE);
303
    if (nand_send_cmd(0x90)) return nand_unlock(0xFFFFFFFD);
2 theseven 304
    FMANUM = 0;
305
    FMADDR0 = 0;
306
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
61 theseven 307
    if (nand_wait_cmddone()) return nand_unlock(0xFFFFFFFC);
2 theseven 308
    FMDNUM = 4;
309
    FMCTRL1 = FMCTRL1_DOREADDATA;
61 theseven 310
    if (nand_wait_addrdone()) return nand_unlock(0xFFFFFFFB);
2 theseven 311
    result = FMFIFO;
312
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
54 theseven 313
    return nand_unlock(result);
2 theseven 314
}
315
 
54 theseven 316
void nand_set_active(void)
2 theseven 317
{
54 theseven 318
    nand_last_activity_value = USEC_TIMER;
319
}
320
 
321
long nand_last_activity(void)
322
{
323
    return nand_last_activity_value;
324
}
325
 
326
void nand_power_up(void)
327
{
328
    uint32_t i;
329
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
330
    nand_last_activity_value = USEC_TIMER;
331
    PWRCONEXT &= ~0x40;
332
    PWRCON &= ~0x100000;
333
    PCON2 = 0x33333333;
334
    PDAT2 = 0;
335
    PCON3 = 0x11113333;
336
    PDAT3 = 0;
337
    PCON4 = 0x33333333;
338
    PDAT4 = 0;
339
    PCON5 = (PCON5 & ~0xF) | 3;
340
    PUNK5 = 1;
341
    pmu_ldo_set_voltage(4, 0x15);
342
    pmu_ldo_power_on(4);
343
    sleep(50000);
344
    nand_last_activity_value = USEC_TIMER;
345
    for (i = 0; i < 4; i++)
61 theseven 346
        if (nand_type[i] >= 0)
347
            if (nand_reset(i))
54 theseven 348
				panicf(PANIC_FATAL, "nand_power_up: nand_reset(bank=%d) failed.", (unsigned int)i);
349
    nand_powered = 1;
350
    nand_last_activity_value = USEC_TIMER;
351
    mutex_unlock(&nand_mtx);
352
}
353
 
354
void nand_power_down(void)
355
{
356
    if (!nand_powered) return;
357
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
358
    pmu_ldo_power_off(4);
359
    PCON2 = 0x11111111;
360
    PDAT2 = 0;
361
    PCON3 = 0x11111111;
362
    PDAT3 = 0;
363
    PCON4 = 0x11111111;
364
    PDAT4 = 0;
365
    PCON5 = (PCON5 & ~0xF) | 1;
366
    PUNK5 = 1;
367
    PWRCONEXT |= 0x40;
368
    PWRCON |= 0x100000;
369
    nand_powered = 0;
370
    mutex_unlock(&nand_mtx);
371
}
372
 
373
uint32_t nand_read_page(uint32_t bank, uint32_t page, void* databuffer,
374
                        void* sparebuffer, uint32_t doecc,
375
                        uint32_t checkempty)
376
{
377
    uint8_t* data = nand_data;
378
    uint8_t* spare = nand_spare;
379
    if (databuffer && !((uint32_t)databuffer & 0xf))
380
        data = (uint8_t*)databuffer;
381
    if (sparebuffer && !((uint32_t)sparebuffer & 0xf))
382
        spare = (uint8_t*)sparebuffer;
383
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
384
    nand_last_activity_value = USEC_TIMER;
385
    if (!nand_powered) nand_power_up();
2 theseven 386
    uint32_t rc, eccresult;
387
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
54 theseven 388
    if (nand_send_cmd(NAND_CMD_READ)) return nand_unlock(1);
389
    if (nand_send_address(page, databuffer ? 0 : 0x800))
390
        return nand_unlock(1);
391
    if (nand_send_cmd(NAND_CMD_READ2)) return nand_unlock(1);
392
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
393
    if (databuffer)
394
        if (nand_transfer_data(bank, 0, data, 0x800))
395
            return nand_unlock(1);
2 theseven 396
    rc = 0;
397
    if (!doecc)
398
    {
54 theseven 399
        if (databuffer && data != databuffer) memcpy(databuffer, data, 0x800);
2 theseven 400
        if (sparebuffer)
401
        {
54 theseven 402
            if (nand_transfer_data(bank, 0, spare, 0x40))
403
                return nand_unlock(1);
404
            if (sparebuffer && spare != sparebuffer) 
405
                memcpy(sparebuffer, spare, 0x800);
2 theseven 406
            if (checkempty)
54 theseven 407
                rc = nand_check_empty((uint8_t*)sparebuffer) << 1;
2 theseven 408
        }
54 theseven 409
        return nand_unlock(rc);
2 theseven 410
    }
54 theseven 411
    if (nand_transfer_data(bank, 0, spare, 0x40)) return nand_unlock(1);
412
    if (databuffer)
2 theseven 413
    {
54 theseven 414
        memcpy(nand_ecc, &spare[0xC], 0x28);
2 theseven 415
        rc |= (ecc_decode(3, data, nand_ecc) & 0xF) << 4;
54 theseven 416
        if (data != databuffer) memcpy(databuffer, data, 0x800);
2 theseven 417
    }
418
    memset(nand_ctrl, 0xFF, 0x200);
419
    memcpy(nand_ctrl, spare, 0xC);
54 theseven 420
    memcpy(nand_ecc, &spare[0x34], 0xC);
2 theseven 421
    eccresult = ecc_decode(0, nand_ctrl, nand_ecc);
422
    rc |= (eccresult & 0xF) << 8;
54 theseven 423
    if (sparebuffer)
2 theseven 424
    {
54 theseven 425
        if (spare != sparebuffer) memcpy(sparebuffer, spare, 0x40);
426
        if (eccresult & 1) memset(sparebuffer, 0xFF, 0xC);
427
        else memcpy(sparebuffer, nand_ctrl, 0xC);
2 theseven 428
    }
429
    if (checkempty) rc |= nand_check_empty(spare) << 1;
54 theseven 430
 
431
    return nand_unlock(rc);
2 theseven 432
}
433
 
54 theseven 434
uint32_t nand_write_page_int(uint32_t bank, uint32_t page, void* databuffer,
435
                             void* sparebuffer, uint32_t doecc, uint32_t wait)
2 theseven 436
{
54 theseven 437
    uint8_t* data = nand_data;
2 theseven 438
    uint8_t* spare = nand_spare;
54 theseven 439
    if (databuffer && !((uint32_t)databuffer & 0xf))
440
        data = (uint8_t*)databuffer;
441
    if (sparebuffer && !((uint32_t)sparebuffer & 0xf))
442
        spare = (uint8_t*)sparebuffer;
443
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
444
    nand_last_activity_value = USEC_TIMER;
445
    if (!nand_powered) nand_power_up();
446
    if (sparebuffer)
447
    {
448
        if (spare != sparebuffer) memcpy(spare, sparebuffer, 0x40);
449
    }
2 theseven 450
    else memset(spare, 0xFF, 0x40);
54 theseven 451
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
452
    if (nand_send_cmd(NAND_CMD_PROGRAM)) return nand_unlock(1);
453
    if (nand_send_address(page, databuffer ? 0 : 0x800))
454
        return nand_unlock(1);
455
    if (databuffer && data != databuffer) memcpy(data, databuffer, 0x800);
456
    if (databuffer) nand_transfer_data_start(bank, 1, data, 0x800);
2 theseven 457
    if (doecc)
458
    {
54 theseven 459
        if (ecc_encode(3, data, nand_ecc)) return nand_unlock(1);
2 theseven 460
        memcpy(&spare[0xC], nand_ecc, 0x28);
461
        memset(nand_ctrl, 0xFF, 0x200);
462
        memcpy(nand_ctrl, spare, 0xC);
54 theseven 463
        if (ecc_encode(0, nand_ctrl, nand_ecc)) return nand_unlock(1);
2 theseven 464
        memcpy(&spare[0x34], nand_ecc, 0xC);
465
    }
54 theseven 466
    if (databuffer)
467
        if (nand_transfer_data_collect(1))
468
            return nand_unlock(1);
2 theseven 469
    if (sparebuffer || doecc)
54 theseven 470
        if (nand_transfer_data(bank, 1, spare, 0x40))
471
            return nand_unlock(1);
472
    if (nand_send_cmd(NAND_CMD_PROGCNFRM)) return nand_unlock(1);
473
    if (wait) if (nand_wait_status_ready(bank)) return nand_unlock(1);
474
    return nand_unlock(0);
2 theseven 475
}
476
 
477
uint32_t nand_block_erase(uint32_t bank, uint32_t page)
478
{
54 theseven 479
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
480
    nand_last_activity_value = USEC_TIMER;
481
    if (!nand_powered) nand_power_up();
2 theseven 482
    nand_set_fmctrl0(bank, 0);
54 theseven 483
    if (nand_send_cmd(NAND_CMD_BLOCKERASE)) return nand_unlock(1);
2 theseven 484
    FMANUM = 2;
485
    FMADDR0 = page;
486
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
54 theseven 487
    if (nand_wait_cmddone()) return nand_unlock(1);
488
    if (nand_send_cmd(NAND_CMD_ERASECNFRM)) return nand_unlock(1);
489
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
490
    return nand_unlock(0);
491
}
492
 
493
uint32_t nand_read_page_fast(uint32_t page, void* databuffer,
494
                             void* sparebuffer, uint32_t doecc,
495
                             uint32_t checkempty)
496
{
497
    uint32_t i, rc = 0;
498
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
499
     || !databuffer || !sparebuffer || !doecc)
500
    {
501
        for (i = 0; i < 4; i++)
502
        {
61 theseven 503
            if (nand_type[i] < 0) continue;
54 theseven 504
            void* databuf = (void*)0;
505
            void* sparebuf = (void*)0;
506
            if (databuffer) databuf = (void*)((uint32_t)databuffer + 0x800 * i);
507
            if (sparebuffer) sparebuf = (void*)((uint32_t)sparebuffer + 0x40 * i);
508
            uint32_t ret = nand_read_page(i, page, databuf, sparebuf, doecc, checkempty);
509
            if (ret & 1) rc |= 1 << (i << 2);
510
            if (ret & 2) rc |= 2 << (i << 2);
511
            if (ret & 0x10) rc |= 4 << (i << 2);
512
            if (ret & 0x100) rc |= 8 << (i << 2);
513
        }
514
        return rc;
515
    }
516
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
517
    nand_last_activity_value = USEC_TIMER;
518
    if (!nand_powered) nand_power_up();
519
    uint8_t status[4];
61 theseven 520
    for (i = 0; i < 4; i++) status[i] = (nand_type[i] < 0);
54 theseven 521
    for (i = 0; i < 4; i++)
522
    {
523
        if (!status[i])
524
        {
525
            nand_set_fmctrl0(i, FMCTRL0_ENABLEDMA);
526
            if (nand_send_cmd(NAND_CMD_READ))
527
                status[i] = 1;
528
        }
529
        if (!status[i])
530
            if (nand_send_address(page, 0))
531
                status[i] = 1;
532
        if (!status[i])
533
            if (nand_send_cmd(NAND_CMD_READ2))
534
                status[i] = 1;
535
    }
536
    if (!status[0])
537
        if (nand_wait_status_ready(0))
538
            status[0] = 1;
539
    if (!status[0])
540
        if (nand_transfer_data(0, 0, databuffer, 0x800))
541
            status[0] = 1;
542
    if (!status[0])
543
        if (nand_transfer_data(0, 0, sparebuffer, 0x40))
544
            status[0] = 1;
545
    for (i = 1; i < 4; i++)
546
    {
547
        if (!status[i])
548
            if (nand_wait_status_ready(i))
549
                status[i] = 1;
550
        if (!status[i])
551
            nand_transfer_data_start(i, 0, (void*)((uint32_t)databuffer
552
                                                 + 0x800 * i), 0x800);
553
        if (!status[i - 1])
554
        {
555
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
556
            ecc_start(3, (void*)((uint32_t)databuffer
557
                               + 0x800 * (i - 1)), nand_ecc, ECCCTRL_STARTDECODING);
558
        }
559
        if (!status[i])
560
            if (nand_transfer_data_collect(0))
561
                status[i] = 1;
562
        if (!status[i])
563
            nand_transfer_data_start(i, 0, (void*)((uint32_t)sparebuffer
564
                                                 + 0x40 * i), 0x40);
565
        if (!status[i - 1])
566
            if (ecc_collect() & 1)
567
                status[i - 1] = 4;
568
        if (!status[i - 1])
569
        {
570
            memset(nand_ctrl, 0xFF, 0x200);
571
            memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
572
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
573
            ecc_start(0, nand_ctrl, nand_ecc, ECCCTRL_STARTDECODING);
574
        }
575
        if (!status[i])
576
            if (nand_transfer_data_collect(0))
577
                status[i] = 1;
578
        if (!status[i - 1])
579
        {
580
            if (ecc_collect() & 1)
581
            {
582
                status[i - 1] |= 8;
583
                memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
584
            }
585
            else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
586
            if (checkempty)
587
                status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
588
                                                        + 0x40 * (i - 1))) << 1;
589
        }
590
    }
591
    if (!status[i - 1])
592
    {
593
        memcpy(nand_ecc,(void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
594
        if (ecc_decode(3, (void*)((uint32_t)databuffer
595
                                + 0x800 * (i - 1)), nand_ecc) & 1)
596
            status[i - 1] = 4;
597
    }
598
    if (!status[i - 1])
599
    {
600
        memset(nand_ctrl, 0xFF, 0x200);
601
        memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
602
        memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
603
        if (ecc_decode(0, nand_ctrl, nand_ecc) & 1)
604
        {
605
            status[i - 1] |= 8;
606
            memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
607
        }
608
        else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
609
        if (checkempty)
610
            status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
611
                                                    + 0x40 * (i - 1))) << 1;
612
    }
613
    for (i = 0; i < 4; i++)
61 theseven 614
        if (nand_type[i] < 0)
54 theseven 615
            rc |= status[i] << (i << 2);
616
    return nand_unlock(rc);
617
}
618
 
619
uint32_t nand_write_page(uint32_t bank, uint32_t page, void* databuffer,
620
                         void* sparebuffer, uint32_t doecc)
621
{
622
    return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, 1);
623
}
624
 
625
uint32_t nand_write_page_start(uint32_t bank, uint32_t page, void* databuffer,
626
                               void* sparebuffer, uint32_t doecc)
627
{
628
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
629
     || !databuffer || !sparebuffer || !doecc || !nand_interleaved)
630
        return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, !nand_interleaved);
631
 
632
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
633
    nand_last_activity_value = USEC_TIMER;
634
    if (!nand_powered) nand_power_up();
635
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
636
    if (nand_send_cmd(NAND_CMD_PROGRAM))
637
        return nand_unlock(1);
638
    if (nand_send_address(page, 0))
639
        return nand_unlock(1);
640
    nand_transfer_data_start(bank, 1, databuffer, 0x800);
641
    if (ecc_encode(3, databuffer, nand_ecc))
642
        return nand_unlock(1);
643
    memcpy((void*)((uint32_t)sparebuffer + 0xC), nand_ecc, 0x28);
644
    memset(nand_ctrl, 0xFF, 0x200);
645
    memcpy(nand_ctrl, sparebuffer, 0xC);
646
    if (ecc_encode(0, nand_ctrl, nand_ecc))
647
        return nand_unlock(1);
648
    memcpy((void*)((uint32_t)sparebuffer + 0x34), nand_ecc, 0xC);
649
    if (nand_transfer_data_collect(0))
650
        return nand_unlock(1);
651
    if (nand_transfer_data(bank, 1, sparebuffer, 0x40))
652
        return nand_unlock(1);
653
    return nand_unlock(nand_send_cmd(NAND_CMD_PROGCNFRM));
654
}
655
 
656
uint32_t nand_write_page_collect(uint32_t bank)
657
{
2 theseven 658
    return nand_wait_status_ready(bank);
659
}
660
 
661
uint32_t nand_block_erase_fast(uint32_t page)
662
{
663
    uint32_t i, rc = 0;
54 theseven 664
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
665
    nand_last_activity_value = USEC_TIMER;
666
    if (!nand_powered) nand_power_up();
2 theseven 667
    for (i = 0; i < 4; i++)
668
    {
61 theseven 669
        if (nand_type[i] < 0) continue;
2 theseven 670
        nand_set_fmctrl0(i, 0);
671
        if (nand_send_cmd(NAND_CMD_BLOCKERASE))
672
        {
673
            rc |= 1 << i;
674
            continue;
675
        }
676
        FMANUM = 2;
677
        FMADDR0 = page;
678
        FMCTRL1 = FMCTRL1_DOTRANSADDR;
679
        if (nand_wait_cmddone())
680
        {
681
            rc |= 1 << i;
682
            continue;
683
        }
684
        if (nand_send_cmd(NAND_CMD_ERASECNFRM)) rc |= 1 << i;
685
    }
686
    for (i = 0; i < 4; i++)
687
    {
61 theseven 688
        if (nand_type[i] < 0) continue;
2 theseven 689
        if (rc & (1 << i)) continue;
690
        if (nand_wait_status_ready(i)) rc |= 1 << i;
691
    }
54 theseven 692
    return nand_unlock(rc);
2 theseven 693
}
694
 
695
const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
696
{
61 theseven 697
    if (nand_type[bank] < 0)
2 theseven 698
        return (struct nand_device_info_type*)0;
699
    return &nand_deviceinfotable[nand_type[bank]];
700
}
701
 
54 theseven 702
static void nand_thread(void)
2 theseven 703
{
54 theseven 704
    while (1)
705
    {
706
        if (TIME_AFTER(USEC_TIMER, nand_last_activity_value + 200000) && nand_powered)
707
            nand_power_down();
708
        sleep(100000);
709
    }
710
}
711
 
61 theseven 712
int nand_device_init(void)
54 theseven 713
{
714
    mutex_init(&nand_mtx);
715
    wakeup_init(&nand_wakeup);
716
    mutex_init(&ecc_mtx);
717
    wakeup_init(&ecc_wakeup);
718
 
2 theseven 719
    uint32_t type;
720
    uint32_t i, j;
54 theseven 721
 
722
    /* Assume there are 0 banks, to prevent
723
       nand_power_up from talking with them yet. */
61 theseven 724
    for (i = 0; i < 4; i++) nand_type[i] = -1;
54 theseven 725
    nand_power_up();
726
 
727
    /* Now that the flash is powered on, detect how
728
       many banks we really have and initialize them. */
2 theseven 729
    for (i = 0; i < 4; i++)
730
    {
731
        nand_tunk1[i] = 7;
732
        nand_twp[i] = 7;
733
        nand_tunk2[i] = 7;
734
        nand_tunk3[i] = 7;
735
        type = nand_get_chip_type(i);
61 theseven 736
        if (type >= 0xFFFFFFF0)
737
        {
738
            nand_type[i] = (int)type;
739
            continue;
740
        }
2 theseven 741
        for (j = 0; ; j++)
742
        {
58 theseven 743
            if (j == ARRAYLEN(nand_deviceinfotable)) break;
2 theseven 744
            else if (nand_deviceinfotable[j].id == type)
745
            {
746
                nand_type[i] = j;
747
                break;
748
            }
749
        }
750
        nand_tunk1[i] = nand_deviceinfotable[nand_type[i]].tunk1;
751
        nand_twp[i] = nand_deviceinfotable[nand_type[i]].twp;
752
        nand_tunk2[i] = nand_deviceinfotable[nand_type[i]].tunk2;
753
        nand_tunk3[i] = nand_deviceinfotable[nand_type[i]].tunk3;
754
    }
61 theseven 755
    if (nand_type[0] < 0) return nand_type[0];
54 theseven 756
    nand_interleaved = ((nand_type[0] >> 22) & 1);
757
    nand_cached = ((nand_type[0] >> 23) & 1);
758
 
759
    nand_last_activity_value = USEC_TIMER;
760
    thread_create("NAND idle monitor", nand_thread, nand_stack,
761
                  sizeof(nand_stack), USER_THREAD, 1, true);
762
 
2 theseven 763
    return 0;
764
}