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"
85 theseven 32
#include "s5l8701.h"
2 theseven 33
 
34
#define NAND_CMD_READ       0x00
35
#define NAND_CMD_PROGCNFRM  0x10
36
#define NAND_CMD_READ2      0x30
37
#define NAND_CMD_BLOCKERASE 0x60
38
#define NAND_CMD_GET_STATUS 0x70
39
#define NAND_CMD_PROGRAM    0x80
40
#define NAND_CMD_ERASECNFRM 0xD0
41
#define NAND_CMD_RESET      0xFF
42
 
43
#define NAND_STATUS_READY   0x40
44
 
45
static const struct nand_device_info_type nand_deviceinfotable[] =
46
{
47
    {0x1580F1EC, 1024, 968, 0x40, 6, 2, 1, 2, 1},
48
    {0x1580DAEC, 2048, 1936, 0x40, 6, 2, 1, 2, 1},
49
    {0x15C1DAEC, 2048, 1936, 0x40, 6, 2, 1, 2, 1},
50
    {0x1510DCEC, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
51
    {0x95C1DCEC, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
52
    {0x2514DCEC, 2048, 1936, 0x80, 7, 2, 1, 2, 1},
53
    {0x2514D3EC, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
54
    {0x2555D3EC, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
55
    {0x2555D5EC, 8192, 7744, 0x80, 7, 2, 1, 2, 1},
56
    {0x2585D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
57
    {0x9580DCAD, 4096, 3872, 0x40, 6, 3, 2, 3, 2},
58
    {0xA514D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
59
    {0xA550D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
60
    {0xA560D5AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2},
61
    {0xA555D5AD, 8192, 7744, 0x80, 7, 3, 2, 3, 2},
62
    {0xA585D598, 8320, 7744, 0x80, 7, 3, 1, 2, 1},
63
    {0xA584D398, 4160, 3872, 0x80, 7, 3, 1, 2, 1},
64
    {0x95D1D32C, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
65
    {0x1580DC2C, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
66
    {0x15C1D32C, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
67
    {0x9590DC2C, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
68
    {0xA594D32C, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
69
    {0x2584DC2C, 2048, 1936, 0x80, 7, 2, 1, 2, 1},
70
    {0xA5D5D52C, 8192, 7744, 0x80, 7, 3, 2, 2, 1},
71
    {0x95D1D389, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
72
    {0x1580DC89, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
73
    {0x15C1D389, 8192, 7744, 0x40, 6, 2, 1, 2, 1},
74
    {0x9590DC89, 4096, 3872, 0x40, 6, 2, 1, 2, 1},
75
    {0xA594D389, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
76
    {0x2584DC89, 2048, 1936, 0x80, 7, 2, 1, 2, 1},
77
    {0xA5D5D589, 8192, 7744, 0x80, 7, 2, 1, 2, 1},
78
    {0xA514D320, 4096, 3872, 0x80, 7, 2, 1, 2, 1},
79
    {0xA555D520, 8192, 3872, 0x80, 7, 2, 1, 2, 1}
80
};
81
 
66 theseven 82
static uint8_t nand_tunk1[4];
83
static uint8_t nand_twp[4];
84
static uint8_t nand_tunk2[4];
85
static uint8_t nand_tunk3[4];
86
static int nand_type[4];
87
static int nand_powered = 0;
88
static int nand_interleaved = 0;
89
static int nand_cached = 0;
90
static long nand_last_activity_value = -1;
54 theseven 91
static uint32_t nand_stack[0x80];
2 theseven 92
 
54 theseven 93
static struct mutex nand_mtx;
94
static struct wakeup nand_wakeup;
95
static struct mutex ecc_mtx;
96
static struct wakeup ecc_wakeup;
2 theseven 97
 
54 theseven 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
 
66 theseven 103
static uint32_t nand_unlock(uint32_t rc)
54 theseven 104
{
105
    nand_last_activity_value = USEC_TIMER;
106
    mutex_unlock(&nand_mtx);
107
    return rc;
108
}
109
 
66 theseven 110
static uint32_t ecc_unlock(uint32_t rc)
54 theseven 111
{
112
    mutex_unlock(&ecc_mtx);
113
    return rc;
114
}
115
 
66 theseven 116
static uint32_t nand_timeout(long timeout)
54 theseven 117
{
118
    if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
119
    else
120
    {
121
        yield();
122
        return 0;
123
    }
124
}
125
 
66 theseven 126
static uint32_t nand_wait_rbbdone(void)
2 theseven 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
 
66 theseven 135
static uint32_t nand_wait_cmddone(void)
2 theseven 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
 
66 theseven 144
static uint32_t nand_wait_addrdone(void)
2 theseven 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
 
66 theseven 153
static uint32_t nand_wait_chip_ready(uint32_t bank)
2 theseven 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
 
66 theseven 162
static void nand_set_fmctrl0(uint32_t bank, uint32_t flags)
2 theseven 163
{
164
    FMCTRL0 = (nand_tunk1[bank] << 16) | (nand_twp[bank] << 12)
165
            | (1 << 11) | 1 | (1 << (bank + 1)) | flags;
166
}
167
 
66 theseven 168
static uint32_t nand_send_cmd(uint32_t cmd)
2 theseven 169
{
170
    FMCMD = cmd;
171
    return nand_wait_rbbdone();
172
}
173
 
66 theseven 174
static uint32_t nand_send_address(uint32_t page, uint32_t offset)
2 theseven 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
 
66 theseven 193
static uint32_t nand_wait_status_ready(uint32_t bank)
2 theseven 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
 
66 theseven 214
static 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
 
66 theseven 232
static uint32_t nand_transfer_data_collect(uint32_t direction)
54 theseven 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
 
66 theseven 244
static 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
 
66 theseven 252
static void ecc_start(uint32_t size, void* databuffer, void* sparebuffer,
253
                      uint32_t type)
54 theseven 254
{
255
    mutex_lock(&ecc_mtx, TIMEOUT_BLOCK);
2 theseven 256
    ECC_INT_CLR = 1;
85 theseven 257
    SRCPND = (1 << IRQ_ECC);
2 theseven 258
    ECC_UNK1 = size;
259
    ECC_DATA_PTR = (uint32_t)databuffer;
260
    ECC_SPARE_PTR = (uint32_t)sparebuffer;
261
    clean_dcache();
54 theseven 262
    ECC_CTRL = type;
263
}
264
 
66 theseven 265
static uint32_t ecc_collect(void)
54 theseven 266
{
267
    uint32_t timeout = USEC_TIMER + 20000;
85 theseven 268
    while (!(SRCPND & (1 << IRQ_ECC)))
54 theseven 269
        if (nand_timeout(timeout)) return ecc_unlock(1);
2 theseven 270
    invalidate_dcache();
271
    ECC_INT_CLR = 1;
85 theseven 272
    SRCPND = (1 << IRQ_ECC);
54 theseven 273
    return ecc_unlock(ECC_RESULT);
2 theseven 274
}
275
 
66 theseven 276
static uint32_t ecc_decode(uint32_t size, void* databuffer, void* sparebuffer)
54 theseven 277
{
278
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTDECODING);
279
    uint32_t rc = ecc_collect();
280
    return rc;
281
}
282
 
66 theseven 283
static uint32_t ecc_encode(uint32_t size, void* databuffer, void* sparebuffer)
2 theseven 284
{
54 theseven 285
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTENCODING);
286
    ecc_collect();
2 theseven 287
    return 0;
288
}
289
 
66 theseven 290
static uint32_t nand_check_empty(uint8_t* buffer)
2 theseven 291
{
292
    uint32_t i, count;
293
    count = 0;
294
    for (i = 0; i < 0x40; i++) if (buffer[i] != 0xFF) count++;
295
    if (count < 2) return 1;
296
    return 0;
297
}
298
 
66 theseven 299
static uint32_t nand_get_chip_type(uint32_t bank)
2 theseven 300
{
54 theseven 301
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
2 theseven 302
    uint32_t result;
61 theseven 303
    if (nand_reset(bank)) return nand_unlock(0xFFFFFFFE);
304
    if (nand_send_cmd(0x90)) return nand_unlock(0xFFFFFFFD);
2 theseven 305
    FMANUM = 0;
306
    FMADDR0 = 0;
307
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
61 theseven 308
    if (nand_wait_cmddone()) return nand_unlock(0xFFFFFFFC);
2 theseven 309
    FMDNUM = 4;
310
    FMCTRL1 = FMCTRL1_DOREADDATA;
61 theseven 311
    if (nand_wait_addrdone()) return nand_unlock(0xFFFFFFFB);
2 theseven 312
    result = FMFIFO;
313
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
54 theseven 314
    return nand_unlock(result);
2 theseven 315
}
316
 
54 theseven 317
void nand_set_active(void)
2 theseven 318
{
54 theseven 319
    nand_last_activity_value = USEC_TIMER;
320
}
321
 
322
long nand_last_activity(void)
323
{
324
    return nand_last_activity_value;
325
}
326
 
327
void nand_power_up(void)
328
{
329
    uint32_t i;
330
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
331
    nand_last_activity_value = USEC_TIMER;
88 theseven 332
    PWRCON(1) &= ~0x40;
333
    PWRCON(0) &= ~0x100000;
54 theseven 334
    PCON2 = 0x33333333;
335
    PDAT2 = 0;
336
    PCON3 = 0x11113333;
337
    PDAT3 = 0;
338
    PCON4 = 0x33333333;
339
    PDAT4 = 0;
340
    PCON5 = (PCON5 & ~0xF) | 3;
341
    PUNK5 = 1;
342
    pmu_ldo_set_voltage(4, 0x15);
343
    pmu_ldo_power_on(4);
344
    sleep(50000);
345
    nand_last_activity_value = USEC_TIMER;
346
    for (i = 0; i < 4; i++)
61 theseven 347
        if (nand_type[i] >= 0)
348
            if (nand_reset(i))
54 theseven 349
				panicf(PANIC_FATAL, "nand_power_up: nand_reset(bank=%d) failed.", (unsigned int)i);
350
    nand_powered = 1;
351
    nand_last_activity_value = USEC_TIMER;
352
    mutex_unlock(&nand_mtx);
353
}
354
 
355
void nand_power_down(void)
356
{
357
    if (!nand_powered) return;
358
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
359
    pmu_ldo_power_off(4);
360
    PCON2 = 0x11111111;
361
    PDAT2 = 0;
362
    PCON3 = 0x11111111;
363
    PDAT3 = 0;
364
    PCON4 = 0x11111111;
365
    PDAT4 = 0;
366
    PCON5 = (PCON5 & ~0xF) | 1;
367
    PUNK5 = 1;
88 theseven 368
    PWRCON(1) |= 0x40;
369
    PWRCON(0) |= 0x100000;
54 theseven 370
    nand_powered = 0;
371
    mutex_unlock(&nand_mtx);
372
}
373
 
374
uint32_t nand_read_page(uint32_t bank, uint32_t page, void* databuffer,
375
                        void* sparebuffer, uint32_t doecc,
376
                        uint32_t checkempty)
377
{
224 theseven 378
#ifdef NAND_TRACE
379
    DEBUGF("NAND: Read bank %d, page %d", bank, page);
380
#endif
68 theseven 381
    uint8_t* data = (uint8_t*)databuffer;
54 theseven 382
    uint8_t* spare = nand_spare;
68 theseven 383
    if (sparebuffer) spare = (uint8_t*)sparebuffer;
69 theseven 384
	if ((uint32_t)databuffer & (CACHEALIGN_SIZE - 1))
68 theseven 385
		panicf(PANIC_KILLUSERTHREADS,
386
	           "nand_read_page: Misaligned data buffer at %08X (bank %lu, page %lu)",
387
			   (unsigned int)databuffer, bank, page);
69 theseven 388
	if ((uint32_t)sparebuffer & (CACHEALIGN_SIZE - 1))
68 theseven 389
		panicf(PANIC_KILLUSERTHREADS,
390
	           "nand_read_page: Misaligned spare buffer at %08X (bank %lu, page %lu)",
391
			   (unsigned int)sparebuffer, bank, page);
54 theseven 392
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
393
    nand_last_activity_value = USEC_TIMER;
394
    if (!nand_powered) nand_power_up();
2 theseven 395
    uint32_t rc, eccresult;
396
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
54 theseven 397
    if (nand_send_cmd(NAND_CMD_READ)) return nand_unlock(1);
68 theseven 398
    if (nand_send_address(page, data ? 0 : 0x800))
54 theseven 399
        return nand_unlock(1);
400
    if (nand_send_cmd(NAND_CMD_READ2)) return nand_unlock(1);
401
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
68 theseven 402
    if (data)
54 theseven 403
        if (nand_transfer_data(bank, 0, data, 0x800))
404
            return nand_unlock(1);
2 theseven 405
    rc = 0;
406
    if (!doecc)
407
    {
408
        if (sparebuffer)
409
        {
54 theseven 410
            if (nand_transfer_data(bank, 0, spare, 0x40))
411
                return nand_unlock(1);
68 theseven 412
            if (sparebuffer) 
54 theseven 413
                memcpy(sparebuffer, spare, 0x800);
2 theseven 414
            if (checkempty)
54 theseven 415
                rc = nand_check_empty((uint8_t*)sparebuffer) << 1;
2 theseven 416
        }
54 theseven 417
        return nand_unlock(rc);
2 theseven 418
    }
54 theseven 419
    if (nand_transfer_data(bank, 0, spare, 0x40)) return nand_unlock(1);
420
    if (databuffer)
2 theseven 421
    {
54 theseven 422
        memcpy(nand_ecc, &spare[0xC], 0x28);
2 theseven 423
        rc |= (ecc_decode(3, data, nand_ecc) & 0xF) << 4;
424
    }
425
    memset(nand_ctrl, 0xFF, 0x200);
426
    memcpy(nand_ctrl, spare, 0xC);
54 theseven 427
    memcpy(nand_ecc, &spare[0x34], 0xC);
2 theseven 428
    eccresult = ecc_decode(0, nand_ctrl, nand_ecc);
429
    rc |= (eccresult & 0xF) << 8;
54 theseven 430
    if (sparebuffer)
2 theseven 431
    {
54 theseven 432
        if (eccresult & 1) memset(sparebuffer, 0xFF, 0xC);
433
        else memcpy(sparebuffer, nand_ctrl, 0xC);
2 theseven 434
    }
435
    if (checkempty) rc |= nand_check_empty(spare) << 1;
224 theseven 436
#ifdef NAND_DEBUG
437
    if ((rc & 2) == 0)
438
    {
439
        if ((rc & 0x10) != 0)
440
            DEBUGF("NAND: ECC failed to correct bank %d page %d user data!", bank, page);
441
        if ((rc & 0xE0) != 0)
442
            DEBUGF("NAND: ECC corrected %d errors in bank %d page %d user data!", rc >> 5, bank, page);
443
        if ((rc & 0x100) != 0)
444
            DEBUGF("NAND: ECC failed to correct bank %d page %d control data!", bank, page);
445
        if ((rc & 0xE00) != 0)
446
            DEBUGF("NAND: ECC corrected %d errors in bank %d page %d control data!", (rc >> 9) & 7, bank, page);
447
    }
448
#ifdef NAND_TRACE
449
    else DEBUGF("NAND: Bank %d page %d: Erased page!", bank, page);
450
#endif
451
#endif
54 theseven 452
 
224 theseven 453
#ifdef NAND_TRACE
454
    DEBUGF("NAND: Read success, RC=%X", rc);
455
#endif
54 theseven 456
    return nand_unlock(rc);
2 theseven 457
}
458
 
66 theseven 459
static uint32_t nand_write_page_int(uint32_t bank, uint32_t page,
460
                                    void* databuffer, void* sparebuffer,
461
                                    uint32_t doecc, uint32_t wait)
2 theseven 462
{
224 theseven 463
#ifdef NAND_TRACE
464
    DEBUGF("NAND: Write bank %d, page %d", bank, page);
465
#endif
68 theseven 466
    uint8_t* data = (uint8_t*)databuffer;
2 theseven 467
    uint8_t* spare = nand_spare;
68 theseven 468
    if (sparebuffer) spare = (uint8_t*)sparebuffer;
469
	if ((uint32_t)databuffer & 0xf)
470
		panicf(PANIC_KILLUSERTHREADS,
471
	           "nand_write_page: Misaligned data buffer at %08X (bank %lu, page %lu)",
472
			   (unsigned int)databuffer, bank, page);
473
	if ((uint32_t)sparebuffer & 0xf)
474
		panicf(PANIC_KILLUSERTHREADS,
475
	           "nand_write_page: Misaligned spare buffer at %08X (bank %lu, page %lu)",
476
			   (unsigned int)sparebuffer, bank, page);
54 theseven 477
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
478
    nand_last_activity_value = USEC_TIMER;
479
    if (!nand_powered) nand_power_up();
68 theseven 480
    if (!sparebuffer) memset(spare, 0xFF, 0x40);
54 theseven 481
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
482
    if (nand_send_cmd(NAND_CMD_PROGRAM)) return nand_unlock(1);
68 theseven 483
    if (nand_send_address(page, data ? 0 : 0x800))
54 theseven 484
        return nand_unlock(1);
68 theseven 485
    if (data) nand_transfer_data_start(bank, 1, data, 0x800);
2 theseven 486
    if (doecc)
487
    {
54 theseven 488
        if (ecc_encode(3, data, nand_ecc)) return nand_unlock(1);
2 theseven 489
        memcpy(&spare[0xC], nand_ecc, 0x28);
490
        memset(nand_ctrl, 0xFF, 0x200);
491
        memcpy(nand_ctrl, spare, 0xC);
54 theseven 492
        if (ecc_encode(0, nand_ctrl, nand_ecc)) return nand_unlock(1);
2 theseven 493
        memcpy(&spare[0x34], nand_ecc, 0xC);
494
    }
68 theseven 495
    if (data)
54 theseven 496
        if (nand_transfer_data_collect(1))
497
            return nand_unlock(1);
2 theseven 498
    if (sparebuffer || doecc)
54 theseven 499
        if (nand_transfer_data(bank, 1, spare, 0x40))
500
            return nand_unlock(1);
501
    if (nand_send_cmd(NAND_CMD_PROGCNFRM)) return nand_unlock(1);
502
    if (wait) if (nand_wait_status_ready(bank)) return nand_unlock(1);
503
    return nand_unlock(0);
2 theseven 504
}
505
 
506
uint32_t nand_block_erase(uint32_t bank, uint32_t page)
507
{
224 theseven 508
#ifdef NAND_TRACE
509
    DEBUGF("NAND: Block erase starting at bank %d, page %d", bank, page);
510
#endif
54 theseven 511
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
512
    nand_last_activity_value = USEC_TIMER;
513
    if (!nand_powered) nand_power_up();
2 theseven 514
    nand_set_fmctrl0(bank, 0);
54 theseven 515
    if (nand_send_cmd(NAND_CMD_BLOCKERASE)) return nand_unlock(1);
2 theseven 516
    FMANUM = 2;
517
    FMADDR0 = page;
518
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
54 theseven 519
    if (nand_wait_cmddone()) return nand_unlock(1);
520
    if (nand_send_cmd(NAND_CMD_ERASECNFRM)) return nand_unlock(1);
521
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
522
    return nand_unlock(0);
523
}
524
 
525
uint32_t nand_read_page_fast(uint32_t page, void* databuffer,
526
                             void* sparebuffer, uint32_t doecc,
527
                             uint32_t checkempty)
528
{
224 theseven 529
#ifdef NAND_TRACE
530
    DEBUGF("NAND: Read all banks, page %d", page);
531
#endif
54 theseven 532
    uint32_t i, rc = 0;
533
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
534
     || !databuffer || !sparebuffer || !doecc)
535
    {
536
        for (i = 0; i < 4; i++)
537
        {
61 theseven 538
            if (nand_type[i] < 0) continue;
54 theseven 539
            void* databuf = (void*)0;
540
            void* sparebuf = (void*)0;
541
            if (databuffer) databuf = (void*)((uint32_t)databuffer + 0x800 * i);
542
            if (sparebuffer) sparebuf = (void*)((uint32_t)sparebuffer + 0x40 * i);
543
            uint32_t ret = nand_read_page(i, page, databuf, sparebuf, doecc, checkempty);
544
            if (ret & 1) rc |= 1 << (i << 2);
545
            if (ret & 2) rc |= 2 << (i << 2);
546
            if (ret & 0x10) rc |= 4 << (i << 2);
547
            if (ret & 0x100) rc |= 8 << (i << 2);
548
        }
549
        return rc;
550
    }
551
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
552
    nand_last_activity_value = USEC_TIMER;
553
    if (!nand_powered) nand_power_up();
554
    uint8_t status[4];
61 theseven 555
    for (i = 0; i < 4; i++) status[i] = (nand_type[i] < 0);
54 theseven 556
    for (i = 0; i < 4; i++)
557
    {
558
        if (!status[i])
559
        {
560
            nand_set_fmctrl0(i, FMCTRL0_ENABLEDMA);
561
            if (nand_send_cmd(NAND_CMD_READ))
562
                status[i] = 1;
563
        }
564
        if (!status[i])
565
            if (nand_send_address(page, 0))
566
                status[i] = 1;
567
        if (!status[i])
568
            if (nand_send_cmd(NAND_CMD_READ2))
569
                status[i] = 1;
570
    }
571
    if (!status[0])
572
        if (nand_wait_status_ready(0))
573
            status[0] = 1;
574
    if (!status[0])
575
        if (nand_transfer_data(0, 0, databuffer, 0x800))
576
            status[0] = 1;
577
    if (!status[0])
578
        if (nand_transfer_data(0, 0, sparebuffer, 0x40))
579
            status[0] = 1;
580
    for (i = 1; i < 4; i++)
581
    {
582
        if (!status[i])
583
            if (nand_wait_status_ready(i))
584
                status[i] = 1;
585
        if (!status[i])
586
            nand_transfer_data_start(i, 0, (void*)((uint32_t)databuffer
587
                                                 + 0x800 * i), 0x800);
588
        if (!status[i - 1])
589
        {
590
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
591
            ecc_start(3, (void*)((uint32_t)databuffer
592
                               + 0x800 * (i - 1)), nand_ecc, ECCCTRL_STARTDECODING);
593
        }
594
        if (!status[i])
595
            if (nand_transfer_data_collect(0))
596
                status[i] = 1;
597
        if (!status[i])
598
            nand_transfer_data_start(i, 0, (void*)((uint32_t)sparebuffer
599
                                                 + 0x40 * i), 0x40);
600
        if (!status[i - 1])
601
            if (ecc_collect() & 1)
602
                status[i - 1] = 4;
603
        if (!status[i - 1])
604
        {
605
            memset(nand_ctrl, 0xFF, 0x200);
606
            memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
607
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
608
            ecc_start(0, nand_ctrl, nand_ecc, ECCCTRL_STARTDECODING);
609
        }
610
        if (!status[i])
611
            if (nand_transfer_data_collect(0))
612
                status[i] = 1;
613
        if (!status[i - 1])
614
        {
615
            if (ecc_collect() & 1)
616
            {
617
                status[i - 1] |= 8;
618
                memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
619
            }
620
            else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
621
            if (checkempty)
622
                status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
623
                                                        + 0x40 * (i - 1))) << 1;
624
        }
625
    }
626
    if (!status[i - 1])
627
    {
628
        memcpy(nand_ecc,(void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
629
        if (ecc_decode(3, (void*)((uint32_t)databuffer
630
                                + 0x800 * (i - 1)), nand_ecc) & 1)
631
            status[i - 1] = 4;
632
    }
633
    if (!status[i - 1])
634
    {
635
        memset(nand_ctrl, 0xFF, 0x200);
636
        memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
637
        memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
638
        if (ecc_decode(0, nand_ctrl, nand_ecc) & 1)
639
        {
640
            status[i - 1] |= 8;
641
            memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
642
        }
643
        else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
644
        if (checkempty)
645
            status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
646
                                                    + 0x40 * (i - 1))) << 1;
647
    }
648
    for (i = 0; i < 4; i++)
61 theseven 649
        if (nand_type[i] < 0)
54 theseven 650
            rc |= status[i] << (i << 2);
651
    return nand_unlock(rc);
652
}
653
 
654
uint32_t nand_write_page(uint32_t bank, uint32_t page, void* databuffer,
655
                         void* sparebuffer, uint32_t doecc)
656
{
657
    return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, 1);
658
}
659
 
660
uint32_t nand_write_page_start(uint32_t bank, uint32_t page, void* databuffer,
661
                               void* sparebuffer, uint32_t doecc)
662
{
224 theseven 663
#ifdef NAND_TRACE
664
    DEBUGF("NAND: Write all banks, page %d", page);
665
#endif
54 theseven 666
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
667
     || !databuffer || !sparebuffer || !doecc || !nand_interleaved)
668
        return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, !nand_interleaved);
669
 
670
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
671
    nand_last_activity_value = USEC_TIMER;
672
    if (!nand_powered) nand_power_up();
673
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
674
    if (nand_send_cmd(NAND_CMD_PROGRAM))
675
        return nand_unlock(1);
676
    if (nand_send_address(page, 0))
677
        return nand_unlock(1);
678
    nand_transfer_data_start(bank, 1, databuffer, 0x800);
679
    if (ecc_encode(3, databuffer, nand_ecc))
680
        return nand_unlock(1);
681
    memcpy((void*)((uint32_t)sparebuffer + 0xC), nand_ecc, 0x28);
682
    memset(nand_ctrl, 0xFF, 0x200);
683
    memcpy(nand_ctrl, sparebuffer, 0xC);
684
    if (ecc_encode(0, nand_ctrl, nand_ecc))
685
        return nand_unlock(1);
686
    memcpy((void*)((uint32_t)sparebuffer + 0x34), nand_ecc, 0xC);
687
    if (nand_transfer_data_collect(0))
688
        return nand_unlock(1);
689
    if (nand_transfer_data(bank, 1, sparebuffer, 0x40))
690
        return nand_unlock(1);
691
    return nand_unlock(nand_send_cmd(NAND_CMD_PROGCNFRM));
692
}
693
 
694
uint32_t nand_write_page_collect(uint32_t bank)
695
{
2 theseven 696
    return nand_wait_status_ready(bank);
697
}
698
 
66 theseven 699
static uint32_t nand_block_erase_fast(uint32_t page)
2 theseven 700
{
701
    uint32_t i, rc = 0;
54 theseven 702
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
703
    nand_last_activity_value = USEC_TIMER;
704
    if (!nand_powered) nand_power_up();
2 theseven 705
    for (i = 0; i < 4; i++)
706
    {
61 theseven 707
        if (nand_type[i] < 0) continue;
2 theseven 708
        nand_set_fmctrl0(i, 0);
709
        if (nand_send_cmd(NAND_CMD_BLOCKERASE))
710
        {
711
            rc |= 1 << i;
712
            continue;
713
        }
714
        FMANUM = 2;
715
        FMADDR0 = page;
716
        FMCTRL1 = FMCTRL1_DOTRANSADDR;
717
        if (nand_wait_cmddone())
718
        {
719
            rc |= 1 << i;
720
            continue;
721
        }
722
        if (nand_send_cmd(NAND_CMD_ERASECNFRM)) rc |= 1 << i;
723
    }
724
    for (i = 0; i < 4; i++)
725
    {
61 theseven 726
        if (nand_type[i] < 0) continue;
2 theseven 727
        if (rc & (1 << i)) continue;
728
        if (nand_wait_status_ready(i)) rc |= 1 << i;
729
    }
54 theseven 730
    return nand_unlock(rc);
2 theseven 731
}
732
 
733
const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
734
{
61 theseven 735
    if (nand_type[bank] < 0)
2 theseven 736
        return (struct nand_device_info_type*)0;
737
    return &nand_deviceinfotable[nand_type[bank]];
738
}
739
 
54 theseven 740
static void nand_thread(void)
2 theseven 741
{
54 theseven 742
    while (1)
743
    {
744
        if (TIME_AFTER(USEC_TIMER, nand_last_activity_value + 200000) && nand_powered)
745
            nand_power_down();
746
        sleep(100000);
747
    }
748
}
749
 
61 theseven 750
int nand_device_init(void)
54 theseven 751
{
752
    mutex_init(&nand_mtx);
753
    wakeup_init(&nand_wakeup);
754
    mutex_init(&ecc_mtx);
755
    wakeup_init(&ecc_wakeup);
756
 
2 theseven 757
    uint32_t type;
758
    uint32_t i, j;
54 theseven 759
 
760
    /* Assume there are 0 banks, to prevent
761
       nand_power_up from talking with them yet. */
61 theseven 762
    for (i = 0; i < 4; i++) nand_type[i] = -1;
54 theseven 763
    nand_power_up();
764
 
765
    /* Now that the flash is powered on, detect how
766
       many banks we really have and initialize them. */
2 theseven 767
    for (i = 0; i < 4; i++)
768
    {
769
        nand_tunk1[i] = 7;
770
        nand_twp[i] = 7;
771
        nand_tunk2[i] = 7;
772
        nand_tunk3[i] = 7;
773
        type = nand_get_chip_type(i);
61 theseven 774
        if (type >= 0xFFFFFFF0)
775
        {
776
            nand_type[i] = (int)type;
777
            continue;
778
        }
2 theseven 779
        for (j = 0; ; j++)
780
        {
58 theseven 781
            if (j == ARRAYLEN(nand_deviceinfotable)) break;
2 theseven 782
            else if (nand_deviceinfotable[j].id == type)
783
            {
784
                nand_type[i] = j;
785
                break;
786
            }
787
        }
788
        nand_tunk1[i] = nand_deviceinfotable[nand_type[i]].tunk1;
789
        nand_twp[i] = nand_deviceinfotable[nand_type[i]].twp;
790
        nand_tunk2[i] = nand_deviceinfotable[nand_type[i]].tunk2;
791
        nand_tunk3[i] = nand_deviceinfotable[nand_type[i]].tunk3;
792
    }
61 theseven 793
    if (nand_type[0] < 0) return nand_type[0];
54 theseven 794
    nand_interleaved = ((nand_type[0] >> 22) & 1);
795
    nand_cached = ((nand_type[0] >> 23) & 1);
796
 
797
    nand_last_activity_value = USEC_TIMER;
798
    thread_create("NAND idle monitor", nand_thread, nand_stack,
799
                  sizeof(nand_stack), USER_THREAD, 1, true);
800
 
2 theseven 801
    return 0;
802
}