Subversion Repositories freemyipod

Rev

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
 
66 theseven 81
static uint8_t nand_tunk1[4];
82
static uint8_t nand_twp[4];
83
static uint8_t nand_tunk2[4];
84
static uint8_t nand_tunk3[4];
85
static int nand_type[4];
86
static int nand_powered = 0;
87
static int nand_interleaved = 0;
88
static int nand_cached = 0;
89
static long nand_last_activity_value = -1;
54 theseven 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_ctrl[0x200] CACHEALIGN_ATTR;
98
static uint8_t nand_spare[0x40] CACHEALIGN_ATTR;
99
static uint8_t nand_ecc[0x30] CACHEALIGN_ATTR;
2 theseven 100
 
54 theseven 101
 
66 theseven 102
static uint32_t nand_unlock(uint32_t rc)
54 theseven 103
{
104
    nand_last_activity_value = USEC_TIMER;
105
    mutex_unlock(&nand_mtx);
106
    return rc;
107
}
108
 
66 theseven 109
static uint32_t ecc_unlock(uint32_t rc)
54 theseven 110
{
111
    mutex_unlock(&ecc_mtx);
112
    return rc;
113
}
114
 
66 theseven 115
static uint32_t nand_timeout(long timeout)
54 theseven 116
{
117
    if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
118
    else
119
    {
120
        yield();
121
        return 0;
122
    }
123
}
124
 
66 theseven 125
static uint32_t nand_wait_rbbdone(void)
2 theseven 126
{
54 theseven 127
    uint32_t timeout = USEC_TIMER + 20000;
128
    while (!(FMCSTAT & FMCSTAT_RBBDONE))
129
        if (nand_timeout(timeout)) return 1;
2 theseven 130
    FMCSTAT = FMCSTAT_RBBDONE;
131
    return 0;
132
}
133
 
66 theseven 134
static uint32_t nand_wait_cmddone(void)
2 theseven 135
{
54 theseven 136
    uint32_t timeout = USEC_TIMER + 20000;
137
    while (!(FMCSTAT & FMCSTAT_CMDDONE))
138
        if (nand_timeout(timeout)) return 1;
2 theseven 139
    FMCSTAT = FMCSTAT_CMDDONE;
140
    return 0;
141
}
142
 
66 theseven 143
static uint32_t nand_wait_addrdone(void)
2 theseven 144
{
54 theseven 145
    uint32_t timeout = USEC_TIMER + 20000;
146
    while (!(FMCSTAT & FMCSTAT_ADDRDONE))
147
        if (nand_timeout(timeout)) return 1;
2 theseven 148
    FMCSTAT = FMCSTAT_ADDRDONE;
149
    return 0;
150
}
151
 
66 theseven 152
static uint32_t nand_wait_chip_ready(uint32_t bank)
2 theseven 153
{
54 theseven 154
    uint32_t timeout = USEC_TIMER + 20000;
155
    while (!(FMCSTAT & (FMCSTAT_BANK0READY << bank)))
156
        if (nand_timeout(timeout)) return 1;
2 theseven 157
    FMCSTAT = (FMCSTAT_BANK0READY << bank);
158
    return 0;
159
}
160
 
66 theseven 161
static void nand_set_fmctrl0(uint32_t bank, uint32_t flags)
2 theseven 162
{
163
    FMCTRL0 = (nand_tunk1[bank] << 16) | (nand_twp[bank] << 12)
164
            | (1 << 11) | 1 | (1 << (bank + 1)) | flags;
165
}
166
 
66 theseven 167
static uint32_t nand_send_cmd(uint32_t cmd)
2 theseven 168
{
169
    FMCMD = cmd;
170
    return nand_wait_rbbdone();
171
}
172
 
66 theseven 173
static uint32_t nand_send_address(uint32_t page, uint32_t offset)
2 theseven 174
{
175
    FMANUM = 4;
176
    FMADDR0 = (page << 16) | offset;
177
    FMADDR1 = (page >> 16) & 0xFF;
178
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
179
    return nand_wait_cmddone();
180
}
181
 
182
uint32_t nand_reset(uint32_t bank)
183
{
184
    nand_set_fmctrl0(bank, 0);
185
    if (nand_send_cmd(NAND_CMD_RESET)) return 1;
186
    if (nand_wait_chip_ready(bank)) return 1;
187
    FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
188
    sleep(1000);
189
    return 0;
190
}
191
 
66 theseven 192
static uint32_t nand_wait_status_ready(uint32_t bank)
2 theseven 193
{
54 theseven 194
    uint32_t timeout = USEC_TIMER + 20000;
2 theseven 195
    nand_set_fmctrl0(bank, 0);
196
    if ((FMCSTAT & (FMCSTAT_BANK0READY << bank)))
197
        FMCSTAT = (FMCSTAT_BANK0READY << bank);
198
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
199
    if (nand_send_cmd(NAND_CMD_GET_STATUS)) return 1;
200
    while (1)
201
    {
54 theseven 202
        if (nand_timeout(timeout)) return 1;
2 theseven 203
        FMDNUM = 0;
204
        FMCTRL1 = FMCTRL1_DOREADDATA;
205
        if (nand_wait_addrdone()) return 1;
206
        if ((FMFIFO & NAND_STATUS_READY)) break;
207
        FMCTRL1 = FMCTRL1_CLEARRFIFO;
208
    }
209
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
210
    return nand_send_cmd(NAND_CMD_READ);
211
}
212
 
66 theseven 213
static void nand_transfer_data_start(uint32_t bank, uint32_t direction,
214
                                     void* buffer, uint32_t size)
2 theseven 215
{
216
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
217
    FMDNUM = size - 1;
218
    FMCTRL1 = FMCTRL1_DOREADDATA << direction;
219
    DMACON3 = (2 << DMACON_DEVICE_SHIFT)
220
            | (direction << DMACON_DIRECTION_SHIFT)
221
            | (2 << DMACON_DATA_SIZE_SHIFT)
222
            | (3 << DMACON_BURST_LEN_SHIFT);
223
    while ((DMAALLST & DMAALLST_CHAN3_MASK))
224
        DMACOM3 = DMACOM_CLEARBOTHDONE;
225
    DMABASE3 = (uint32_t)buffer;
226
    DMATCNT3 = (size >> 4) - 1;
227
    clean_dcache();
228
    DMACOM3 = 4;
54 theseven 229
}
230
 
66 theseven 231
static uint32_t nand_transfer_data_collect(uint32_t direction)
54 theseven 232
{
233
    uint32_t timeout = USEC_TIMER + 20000;
234
    while ((DMAALLST & DMAALLST_DMABUSY3))
235
        if (nand_timeout(timeout)) return 1;
2 theseven 236
    if (!direction) invalidate_dcache();
237
    if (nand_wait_addrdone()) return 1;
238
    if (!direction) FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
239
    else FMCTRL1 = FMCTRL1_CLEARRFIFO;
240
    return 0;
241
}
242
 
66 theseven 243
static uint32_t nand_transfer_data(uint32_t bank, uint32_t direction,
244
                                   void* buffer, uint32_t size)
2 theseven 245
{
54 theseven 246
    nand_transfer_data_start(bank, direction, buffer, size);
247
    uint32_t rc = nand_transfer_data_collect(direction);
248
    return rc;
249
}
250
 
66 theseven 251
static void ecc_start(uint32_t size, void* databuffer, void* sparebuffer,
252
                      uint32_t type)
54 theseven 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
 
66 theseven 264
static uint32_t ecc_collect(void)
54 theseven 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
 
66 theseven 275
static uint32_t ecc_decode(uint32_t size, void* databuffer, void* sparebuffer)
54 theseven 276
{
277
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTDECODING);
278
    uint32_t rc = ecc_collect();
279
    return rc;
280
}
281
 
66 theseven 282
static uint32_t ecc_encode(uint32_t size, void* databuffer, void* sparebuffer)
2 theseven 283
{
54 theseven 284
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTENCODING);
285
    ecc_collect();
2 theseven 286
    return 0;
287
}
288
 
66 theseven 289
static uint32_t nand_check_empty(uint8_t* buffer)
2 theseven 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
 
66 theseven 298
static uint32_t nand_get_chip_type(uint32_t bank)
2 theseven 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
{
68 theseven 377
    uint8_t* data = (uint8_t*)databuffer;
54 theseven 378
    uint8_t* spare = nand_spare;
68 theseven 379
    if (sparebuffer) spare = (uint8_t*)sparebuffer;
380
	if ((uint32_t)databuffer & 0xf)
381
		panicf(PANIC_KILLUSERTHREADS,
382
	           "nand_read_page: Misaligned data buffer at %08X (bank %lu, page %lu)",
383
			   (unsigned int)databuffer, bank, page);
384
	if ((uint32_t)sparebuffer & 0xf)
385
		panicf(PANIC_KILLUSERTHREADS,
386
	           "nand_read_page: Misaligned spare buffer at %08X (bank %lu, page %lu)",
387
			   (unsigned int)sparebuffer, bank, page);
54 theseven 388
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
389
    nand_last_activity_value = USEC_TIMER;
390
    if (!nand_powered) nand_power_up();
2 theseven 391
    uint32_t rc, eccresult;
392
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
54 theseven 393
    if (nand_send_cmd(NAND_CMD_READ)) return nand_unlock(1);
68 theseven 394
    if (nand_send_address(page, data ? 0 : 0x800))
54 theseven 395
        return nand_unlock(1);
396
    if (nand_send_cmd(NAND_CMD_READ2)) return nand_unlock(1);
397
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
68 theseven 398
    if (data)
54 theseven 399
        if (nand_transfer_data(bank, 0, data, 0x800))
400
            return nand_unlock(1);
2 theseven 401
    rc = 0;
402
    if (!doecc)
403
    {
404
        if (sparebuffer)
405
        {
54 theseven 406
            if (nand_transfer_data(bank, 0, spare, 0x40))
407
                return nand_unlock(1);
68 theseven 408
            if (sparebuffer) 
54 theseven 409
                memcpy(sparebuffer, spare, 0x800);
2 theseven 410
            if (checkempty)
54 theseven 411
                rc = nand_check_empty((uint8_t*)sparebuffer) << 1;
2 theseven 412
        }
54 theseven 413
        return nand_unlock(rc);
2 theseven 414
    }
54 theseven 415
    if (nand_transfer_data(bank, 0, spare, 0x40)) return nand_unlock(1);
416
    if (databuffer)
2 theseven 417
    {
54 theseven 418
        memcpy(nand_ecc, &spare[0xC], 0x28);
2 theseven 419
        rc |= (ecc_decode(3, data, nand_ecc) & 0xF) << 4;
420
    }
421
    memset(nand_ctrl, 0xFF, 0x200);
422
    memcpy(nand_ctrl, spare, 0xC);
54 theseven 423
    memcpy(nand_ecc, &spare[0x34], 0xC);
2 theseven 424
    eccresult = ecc_decode(0, nand_ctrl, nand_ecc);
425
    rc |= (eccresult & 0xF) << 8;
54 theseven 426
    if (sparebuffer)
2 theseven 427
    {
54 theseven 428
        if (eccresult & 1) memset(sparebuffer, 0xFF, 0xC);
429
        else memcpy(sparebuffer, nand_ctrl, 0xC);
2 theseven 430
    }
431
    if (checkempty) rc |= nand_check_empty(spare) << 1;
54 theseven 432
 
433
    return nand_unlock(rc);
2 theseven 434
}
435
 
66 theseven 436
static uint32_t nand_write_page_int(uint32_t bank, uint32_t page,
437
                                    void* databuffer, void* sparebuffer,
438
                                    uint32_t doecc, uint32_t wait)
2 theseven 439
{
68 theseven 440
    uint8_t* data = (uint8_t*)databuffer;
2 theseven 441
    uint8_t* spare = nand_spare;
68 theseven 442
    if (sparebuffer) spare = (uint8_t*)sparebuffer;
443
	if ((uint32_t)databuffer & 0xf)
444
		panicf(PANIC_KILLUSERTHREADS,
445
	           "nand_write_page: Misaligned data buffer at %08X (bank %lu, page %lu)",
446
			   (unsigned int)databuffer, bank, page);
447
	if ((uint32_t)sparebuffer & 0xf)
448
		panicf(PANIC_KILLUSERTHREADS,
449
	           "nand_write_page: Misaligned spare buffer at %08X (bank %lu, page %lu)",
450
			   (unsigned int)sparebuffer, bank, page);
54 theseven 451
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
452
    nand_last_activity_value = USEC_TIMER;
453
    if (!nand_powered) nand_power_up();
68 theseven 454
    if (!sparebuffer) memset(spare, 0xFF, 0x40);
54 theseven 455
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
456
    if (nand_send_cmd(NAND_CMD_PROGRAM)) return nand_unlock(1);
68 theseven 457
    if (nand_send_address(page, data ? 0 : 0x800))
54 theseven 458
        return nand_unlock(1);
68 theseven 459
    if (data) nand_transfer_data_start(bank, 1, data, 0x800);
2 theseven 460
    if (doecc)
461
    {
54 theseven 462
        if (ecc_encode(3, data, nand_ecc)) return nand_unlock(1);
2 theseven 463
        memcpy(&spare[0xC], nand_ecc, 0x28);
464
        memset(nand_ctrl, 0xFF, 0x200);
465
        memcpy(nand_ctrl, spare, 0xC);
54 theseven 466
        if (ecc_encode(0, nand_ctrl, nand_ecc)) return nand_unlock(1);
2 theseven 467
        memcpy(&spare[0x34], nand_ecc, 0xC);
468
    }
68 theseven 469
    if (data)
54 theseven 470
        if (nand_transfer_data_collect(1))
471
            return nand_unlock(1);
2 theseven 472
    if (sparebuffer || doecc)
54 theseven 473
        if (nand_transfer_data(bank, 1, spare, 0x40))
474
            return nand_unlock(1);
475
    if (nand_send_cmd(NAND_CMD_PROGCNFRM)) return nand_unlock(1);
476
    if (wait) if (nand_wait_status_ready(bank)) return nand_unlock(1);
477
    return nand_unlock(0);
2 theseven 478
}
479
 
480
uint32_t nand_block_erase(uint32_t bank, uint32_t page)
481
{
54 theseven 482
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
483
    nand_last_activity_value = USEC_TIMER;
484
    if (!nand_powered) nand_power_up();
2 theseven 485
    nand_set_fmctrl0(bank, 0);
54 theseven 486
    if (nand_send_cmd(NAND_CMD_BLOCKERASE)) return nand_unlock(1);
2 theseven 487
    FMANUM = 2;
488
    FMADDR0 = page;
489
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
54 theseven 490
    if (nand_wait_cmddone()) return nand_unlock(1);
491
    if (nand_send_cmd(NAND_CMD_ERASECNFRM)) return nand_unlock(1);
492
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
493
    return nand_unlock(0);
494
}
495
 
496
uint32_t nand_read_page_fast(uint32_t page, void* databuffer,
497
                             void* sparebuffer, uint32_t doecc,
498
                             uint32_t checkempty)
499
{
500
    uint32_t i, rc = 0;
501
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
502
     || !databuffer || !sparebuffer || !doecc)
503
    {
504
        for (i = 0; i < 4; i++)
505
        {
61 theseven 506
            if (nand_type[i] < 0) continue;
54 theseven 507
            void* databuf = (void*)0;
508
            void* sparebuf = (void*)0;
509
            if (databuffer) databuf = (void*)((uint32_t)databuffer + 0x800 * i);
510
            if (sparebuffer) sparebuf = (void*)((uint32_t)sparebuffer + 0x40 * i);
511
            uint32_t ret = nand_read_page(i, page, databuf, sparebuf, doecc, checkempty);
512
            if (ret & 1) rc |= 1 << (i << 2);
513
            if (ret & 2) rc |= 2 << (i << 2);
514
            if (ret & 0x10) rc |= 4 << (i << 2);
515
            if (ret & 0x100) rc |= 8 << (i << 2);
516
        }
517
        return rc;
518
    }
519
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
520
    nand_last_activity_value = USEC_TIMER;
521
    if (!nand_powered) nand_power_up();
522
    uint8_t status[4];
61 theseven 523
    for (i = 0; i < 4; i++) status[i] = (nand_type[i] < 0);
54 theseven 524
    for (i = 0; i < 4; i++)
525
    {
526
        if (!status[i])
527
        {
528
            nand_set_fmctrl0(i, FMCTRL0_ENABLEDMA);
529
            if (nand_send_cmd(NAND_CMD_READ))
530
                status[i] = 1;
531
        }
532
        if (!status[i])
533
            if (nand_send_address(page, 0))
534
                status[i] = 1;
535
        if (!status[i])
536
            if (nand_send_cmd(NAND_CMD_READ2))
537
                status[i] = 1;
538
    }
539
    if (!status[0])
540
        if (nand_wait_status_ready(0))
541
            status[0] = 1;
542
    if (!status[0])
543
        if (nand_transfer_data(0, 0, databuffer, 0x800))
544
            status[0] = 1;
545
    if (!status[0])
546
        if (nand_transfer_data(0, 0, sparebuffer, 0x40))
547
            status[0] = 1;
548
    for (i = 1; i < 4; i++)
549
    {
550
        if (!status[i])
551
            if (nand_wait_status_ready(i))
552
                status[i] = 1;
553
        if (!status[i])
554
            nand_transfer_data_start(i, 0, (void*)((uint32_t)databuffer
555
                                                 + 0x800 * i), 0x800);
556
        if (!status[i - 1])
557
        {
558
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
559
            ecc_start(3, (void*)((uint32_t)databuffer
560
                               + 0x800 * (i - 1)), nand_ecc, ECCCTRL_STARTDECODING);
561
        }
562
        if (!status[i])
563
            if (nand_transfer_data_collect(0))
564
                status[i] = 1;
565
        if (!status[i])
566
            nand_transfer_data_start(i, 0, (void*)((uint32_t)sparebuffer
567
                                                 + 0x40 * i), 0x40);
568
        if (!status[i - 1])
569
            if (ecc_collect() & 1)
570
                status[i - 1] = 4;
571
        if (!status[i - 1])
572
        {
573
            memset(nand_ctrl, 0xFF, 0x200);
574
            memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
575
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
576
            ecc_start(0, nand_ctrl, nand_ecc, ECCCTRL_STARTDECODING);
577
        }
578
        if (!status[i])
579
            if (nand_transfer_data_collect(0))
580
                status[i] = 1;
581
        if (!status[i - 1])
582
        {
583
            if (ecc_collect() & 1)
584
            {
585
                status[i - 1] |= 8;
586
                memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
587
            }
588
            else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
589
            if (checkempty)
590
                status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
591
                                                        + 0x40 * (i - 1))) << 1;
592
        }
593
    }
594
    if (!status[i - 1])
595
    {
596
        memcpy(nand_ecc,(void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
597
        if (ecc_decode(3, (void*)((uint32_t)databuffer
598
                                + 0x800 * (i - 1)), nand_ecc) & 1)
599
            status[i - 1] = 4;
600
    }
601
    if (!status[i - 1])
602
    {
603
        memset(nand_ctrl, 0xFF, 0x200);
604
        memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
605
        memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
606
        if (ecc_decode(0, nand_ctrl, nand_ecc) & 1)
607
        {
608
            status[i - 1] |= 8;
609
            memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
610
        }
611
        else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
612
        if (checkempty)
613
            status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
614
                                                    + 0x40 * (i - 1))) << 1;
615
    }
616
    for (i = 0; i < 4; i++)
61 theseven 617
        if (nand_type[i] < 0)
54 theseven 618
            rc |= status[i] << (i << 2);
619
    return nand_unlock(rc);
620
}
621
 
622
uint32_t nand_write_page(uint32_t bank, uint32_t page, void* databuffer,
623
                         void* sparebuffer, uint32_t doecc)
624
{
625
    return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, 1);
626
}
627
 
628
uint32_t nand_write_page_start(uint32_t bank, uint32_t page, void* databuffer,
629
                               void* sparebuffer, uint32_t doecc)
630
{
631
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
632
     || !databuffer || !sparebuffer || !doecc || !nand_interleaved)
633
        return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, !nand_interleaved);
634
 
635
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
636
    nand_last_activity_value = USEC_TIMER;
637
    if (!nand_powered) nand_power_up();
638
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
639
    if (nand_send_cmd(NAND_CMD_PROGRAM))
640
        return nand_unlock(1);
641
    if (nand_send_address(page, 0))
642
        return nand_unlock(1);
643
    nand_transfer_data_start(bank, 1, databuffer, 0x800);
644
    if (ecc_encode(3, databuffer, nand_ecc))
645
        return nand_unlock(1);
646
    memcpy((void*)((uint32_t)sparebuffer + 0xC), nand_ecc, 0x28);
647
    memset(nand_ctrl, 0xFF, 0x200);
648
    memcpy(nand_ctrl, sparebuffer, 0xC);
649
    if (ecc_encode(0, nand_ctrl, nand_ecc))
650
        return nand_unlock(1);
651
    memcpy((void*)((uint32_t)sparebuffer + 0x34), nand_ecc, 0xC);
652
    if (nand_transfer_data_collect(0))
653
        return nand_unlock(1);
654
    if (nand_transfer_data(bank, 1, sparebuffer, 0x40))
655
        return nand_unlock(1);
656
    return nand_unlock(nand_send_cmd(NAND_CMD_PROGCNFRM));
657
}
658
 
659
uint32_t nand_write_page_collect(uint32_t bank)
660
{
2 theseven 661
    return nand_wait_status_ready(bank);
662
}
663
 
66 theseven 664
static uint32_t nand_block_erase_fast(uint32_t page)
2 theseven 665
{
666
    uint32_t i, rc = 0;
54 theseven 667
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
668
    nand_last_activity_value = USEC_TIMER;
669
    if (!nand_powered) nand_power_up();
2 theseven 670
    for (i = 0; i < 4; i++)
671
    {
61 theseven 672
        if (nand_type[i] < 0) continue;
2 theseven 673
        nand_set_fmctrl0(i, 0);
674
        if (nand_send_cmd(NAND_CMD_BLOCKERASE))
675
        {
676
            rc |= 1 << i;
677
            continue;
678
        }
679
        FMANUM = 2;
680
        FMADDR0 = page;
681
        FMCTRL1 = FMCTRL1_DOTRANSADDR;
682
        if (nand_wait_cmddone())
683
        {
684
            rc |= 1 << i;
685
            continue;
686
        }
687
        if (nand_send_cmd(NAND_CMD_ERASECNFRM)) rc |= 1 << i;
688
    }
689
    for (i = 0; i < 4; i++)
690
    {
61 theseven 691
        if (nand_type[i] < 0) continue;
2 theseven 692
        if (rc & (1 << i)) continue;
693
        if (nand_wait_status_ready(i)) rc |= 1 << i;
694
    }
54 theseven 695
    return nand_unlock(rc);
2 theseven 696
}
697
 
698
const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
699
{
61 theseven 700
    if (nand_type[bank] < 0)
2 theseven 701
        return (struct nand_device_info_type*)0;
702
    return &nand_deviceinfotable[nand_type[bank]];
703
}
704
 
54 theseven 705
static void nand_thread(void)
2 theseven 706
{
54 theseven 707
    while (1)
708
    {
709
        if (TIME_AFTER(USEC_TIMER, nand_last_activity_value + 200000) && nand_powered)
710
            nand_power_down();
711
        sleep(100000);
712
    }
713
}
714
 
61 theseven 715
int nand_device_init(void)
54 theseven 716
{
717
    mutex_init(&nand_mtx);
718
    wakeup_init(&nand_wakeup);
719
    mutex_init(&ecc_mtx);
720
    wakeup_init(&ecc_wakeup);
721
 
2 theseven 722
    uint32_t type;
723
    uint32_t i, j;
54 theseven 724
 
725
    /* Assume there are 0 banks, to prevent
726
       nand_power_up from talking with them yet. */
61 theseven 727
    for (i = 0; i < 4; i++) nand_type[i] = -1;
54 theseven 728
    nand_power_up();
729
 
730
    /* Now that the flash is powered on, detect how
731
       many banks we really have and initialize them. */
2 theseven 732
    for (i = 0; i < 4; i++)
733
    {
734
        nand_tunk1[i] = 7;
735
        nand_twp[i] = 7;
736
        nand_tunk2[i] = 7;
737
        nand_tunk3[i] = 7;
738
        type = nand_get_chip_type(i);
61 theseven 739
        if (type >= 0xFFFFFFF0)
740
        {
741
            nand_type[i] = (int)type;
742
            continue;
743
        }
2 theseven 744
        for (j = 0; ; j++)
745
        {
58 theseven 746
            if (j == ARRAYLEN(nand_deviceinfotable)) break;
2 theseven 747
            else if (nand_deviceinfotable[j].id == type)
748
            {
749
                nand_type[i] = j;
750
                break;
751
            }
752
        }
753
        nand_tunk1[i] = nand_deviceinfotable[nand_type[i]].tunk1;
754
        nand_twp[i] = nand_deviceinfotable[nand_type[i]].twp;
755
        nand_tunk2[i] = nand_deviceinfotable[nand_type[i]].tunk2;
756
        nand_tunk3[i] = nand_deviceinfotable[nand_type[i]].tunk3;
757
    }
61 theseven 758
    if (nand_type[0] < 0) return nand_type[0];
54 theseven 759
    nand_interleaved = ((nand_type[0] >> 22) & 1);
760
    nand_cached = ((nand_type[0] >> 23) & 1);
761
 
762
    nand_last_activity_value = USEC_TIMER;
763
    thread_create("NAND idle monitor", nand_thread, nand_stack,
764
                  sizeof(nand_stack), USER_THREAD, 1, true);
765
 
2 theseven 766
    return 0;
767
}