Subversion Repositories freemyipod

Rev

Rev 835 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
427 farthen 6
//    This file is part of emCORE.
2 theseven 7
//
427 farthen 8
//    emCORE is free software: you can redistribute it and/or
2 theseven 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
//
427 farthen 13
//    emCORE is distributed in the hope that it will be useful,
2 theseven 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
427 farthen 19
//    with emCORE.  If not, see <http://www.gnu.org/licenses/>.
2 theseven 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;
429 theseven 91
static struct scheduler_thread nand_thread_handle;
92
static uint32_t nand_stack[0x80] STACK_ATTR;
2 theseven 93
 
54 theseven 94
static struct mutex nand_mtx;
95
static struct wakeup nand_wakeup;
96
static struct mutex ecc_mtx;
97
static struct wakeup ecc_wakeup;
2 theseven 98
 
54 theseven 99
static uint8_t nand_ctrl[0x200] CACHEALIGN_ATTR;
100
static uint8_t nand_spare[0x40] CACHEALIGN_ATTR;
101
static uint8_t nand_ecc[0x30] CACHEALIGN_ATTR;
2 theseven 102
 
54 theseven 103
 
66 theseven 104
static uint32_t nand_unlock(uint32_t rc)
54 theseven 105
{
106
    nand_last_activity_value = USEC_TIMER;
107
    mutex_unlock(&nand_mtx);
108
    return rc;
109
}
110
 
66 theseven 111
static uint32_t ecc_unlock(uint32_t rc)
54 theseven 112
{
113
    mutex_unlock(&ecc_mtx);
114
    return rc;
115
}
116
 
66 theseven 117
static uint32_t nand_timeout(long timeout)
54 theseven 118
{
119
    if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
120
    else
121
    {
122
        yield();
123
        return 0;
124
    }
125
}
126
 
66 theseven 127
static uint32_t nand_wait_rbbdone(void)
2 theseven 128
{
54 theseven 129
    uint32_t timeout = USEC_TIMER + 20000;
130
    while (!(FMCSTAT & FMCSTAT_RBBDONE))
131
        if (nand_timeout(timeout)) return 1;
2 theseven 132
    FMCSTAT = FMCSTAT_RBBDONE;
133
    return 0;
134
}
135
 
66 theseven 136
static uint32_t nand_wait_cmddone(void)
2 theseven 137
{
54 theseven 138
    uint32_t timeout = USEC_TIMER + 20000;
139
    while (!(FMCSTAT & FMCSTAT_CMDDONE))
140
        if (nand_timeout(timeout)) return 1;
2 theseven 141
    FMCSTAT = FMCSTAT_CMDDONE;
142
    return 0;
143
}
144
 
66 theseven 145
static uint32_t nand_wait_addrdone(void)
2 theseven 146
{
54 theseven 147
    uint32_t timeout = USEC_TIMER + 20000;
148
    while (!(FMCSTAT & FMCSTAT_ADDRDONE))
149
        if (nand_timeout(timeout)) return 1;
2 theseven 150
    FMCSTAT = FMCSTAT_ADDRDONE;
151
    return 0;
152
}
153
 
66 theseven 154
static uint32_t nand_wait_chip_ready(uint32_t bank)
2 theseven 155
{
54 theseven 156
    uint32_t timeout = USEC_TIMER + 20000;
157
    while (!(FMCSTAT & (FMCSTAT_BANK0READY << bank)))
158
        if (nand_timeout(timeout)) return 1;
2 theseven 159
    FMCSTAT = (FMCSTAT_BANK0READY << bank);
160
    return 0;
161
}
162
 
66 theseven 163
static void nand_set_fmctrl0(uint32_t bank, uint32_t flags)
2 theseven 164
{
165
    FMCTRL0 = (nand_tunk1[bank] << 16) | (nand_twp[bank] << 12)
166
            | (1 << 11) | 1 | (1 << (bank + 1)) | flags;
167
}
168
 
66 theseven 169
static uint32_t nand_send_cmd(uint32_t cmd)
2 theseven 170
{
171
    FMCMD = cmd;
172
    return nand_wait_rbbdone();
173
}
174
 
66 theseven 175
static uint32_t nand_send_address(uint32_t page, uint32_t offset)
2 theseven 176
{
177
    FMANUM = 4;
178
    FMADDR0 = (page << 16) | offset;
179
    FMADDR1 = (page >> 16) & 0xFF;
180
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
181
    return nand_wait_cmddone();
182
}
183
 
184
uint32_t nand_reset(uint32_t bank)
185
{
186
    nand_set_fmctrl0(bank, 0);
187
    if (nand_send_cmd(NAND_CMD_RESET)) return 1;
188
    if (nand_wait_chip_ready(bank)) return 1;
189
    FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
190
    sleep(1000);
191
    return 0;
192
}
193
 
66 theseven 194
static uint32_t nand_wait_status_ready(uint32_t bank)
2 theseven 195
{
54 theseven 196
    uint32_t timeout = USEC_TIMER + 20000;
2 theseven 197
    nand_set_fmctrl0(bank, 0);
198
    if ((FMCSTAT & (FMCSTAT_BANK0READY << bank)))
199
        FMCSTAT = (FMCSTAT_BANK0READY << bank);
200
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
201
    if (nand_send_cmd(NAND_CMD_GET_STATUS)) return 1;
202
    while (1)
203
    {
54 theseven 204
        if (nand_timeout(timeout)) return 1;
2 theseven 205
        FMDNUM = 0;
206
        FMCTRL1 = FMCTRL1_DOREADDATA;
207
        if (nand_wait_addrdone()) return 1;
208
        if ((FMFIFO & NAND_STATUS_READY)) break;
209
        FMCTRL1 = FMCTRL1_CLEARRFIFO;
210
    }
211
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
212
    return nand_send_cmd(NAND_CMD_READ);
213
}
214
 
66 theseven 215
static void nand_transfer_data_start(uint32_t bank, uint32_t direction,
216
                                     void* buffer, uint32_t size)
2 theseven 217
{
218
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
219
    FMDNUM = size - 1;
220
    FMCTRL1 = FMCTRL1_DOREADDATA << direction;
221
    DMACON3 = (2 << DMACON_DEVICE_SHIFT)
222
            | (direction << DMACON_DIRECTION_SHIFT)
223
            | (2 << DMACON_DATA_SIZE_SHIFT)
224
            | (3 << DMACON_BURST_LEN_SHIFT);
225
    while ((DMAALLST & DMAALLST_CHAN3_MASK))
226
        DMACOM3 = DMACOM_CLEARBOTHDONE;
265 theseven 227
    DMABASE3 = buffer;
2 theseven 228
    DMATCNT3 = (size >> 4) - 1;
636 theseven 229
    if (direction) clean_dcache();
230
    else invalidate_dcache();
2 theseven 231
    DMACOM3 = 4;
54 theseven 232
}
233
 
66 theseven 234
static uint32_t nand_transfer_data_collect(uint32_t direction)
54 theseven 235
{
236
    uint32_t timeout = USEC_TIMER + 20000;
251 theseven 237
    while (DMAALLST & DMAALLST_DMABUSY3)
54 theseven 238
        if (nand_timeout(timeout)) return 1;
2 theseven 239
    if (nand_wait_addrdone()) return 1;
240
    if (!direction) FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
241
    else FMCTRL1 = FMCTRL1_CLEARRFIFO;
242
    return 0;
243
}
244
 
66 theseven 245
static uint32_t nand_transfer_data(uint32_t bank, uint32_t direction,
246
                                   void* buffer, uint32_t size)
2 theseven 247
{
54 theseven 248
    nand_transfer_data_start(bank, direction, buffer, size);
249
    uint32_t rc = nand_transfer_data_collect(direction);
250
    return rc;
251
}
252
 
66 theseven 253
static void ecc_start(uint32_t size, void* databuffer, void* sparebuffer,
254
                      uint32_t type)
54 theseven 255
{
256
    mutex_lock(&ecc_mtx, TIMEOUT_BLOCK);
2 theseven 257
    ECC_INT_CLR = 1;
85 theseven 258
    SRCPND = (1 << IRQ_ECC);
2 theseven 259
    ECC_UNK1 = size;
265 theseven 260
    ECC_DATA_PTR = databuffer;
261
    ECC_SPARE_PTR = sparebuffer;
636 theseven 262
    invalidate_dcache();
54 theseven 263
    ECC_CTRL = type;
264
}
265
 
66 theseven 266
static uint32_t ecc_collect(void)
54 theseven 267
{
268
    uint32_t timeout = USEC_TIMER + 20000;
85 theseven 269
    while (!(SRCPND & (1 << IRQ_ECC)))
54 theseven 270
        if (nand_timeout(timeout)) return ecc_unlock(1);
2 theseven 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))
249 theseven 349
				panicf(PANIC_KILLTHREAD, "nand_power_up: nand_reset(bank=%d) failed.", (unsigned int)i);
54 theseven 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))
249 theseven 385
		panicf(PANIC_KILLTHREAD,
68 theseven 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))
249 theseven 389
		panicf(PANIC_KILLTHREAD,
68 theseven 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);
2 theseven 412
            if (checkempty)
54 theseven 413
                rc = nand_check_empty((uint8_t*)sparebuffer) << 1;
2 theseven 414
        }
54 theseven 415
        return nand_unlock(rc);
2 theseven 416
    }
54 theseven 417
    if (nand_transfer_data(bank, 0, spare, 0x40)) return nand_unlock(1);
418
    if (databuffer)
2 theseven 419
    {
54 theseven 420
        memcpy(nand_ecc, &spare[0xC], 0x28);
2 theseven 421
        rc |= (ecc_decode(3, data, nand_ecc) & 0xF) << 4;
422
    }
423
    memset(nand_ctrl, 0xFF, 0x200);
424
    memcpy(nand_ctrl, spare, 0xC);
54 theseven 425
    memcpy(nand_ecc, &spare[0x34], 0xC);
2 theseven 426
    eccresult = ecc_decode(0, nand_ctrl, nand_ecc);
427
    rc |= (eccresult & 0xF) << 8;
54 theseven 428
    if (sparebuffer)
2 theseven 429
    {
54 theseven 430
        if (eccresult & 1) memset(sparebuffer, 0xFF, 0xC);
431
        else memcpy(sparebuffer, nand_ctrl, 0xC);
2 theseven 432
    }
433
    if (checkempty) rc |= nand_check_empty(spare) << 1;
224 theseven 434
#ifdef NAND_DEBUG
435
    if ((rc & 2) == 0)
436
    {
437
        if ((rc & 0x10) != 0)
438
            DEBUGF("NAND: ECC failed to correct bank %d page %d user data!", bank, page);
439
        if ((rc & 0xE0) != 0)
440
            DEBUGF("NAND: ECC corrected %d errors in bank %d page %d user data!", rc >> 5, bank, page);
441
        if ((rc & 0x100) != 0)
442
            DEBUGF("NAND: ECC failed to correct bank %d page %d control data!", bank, page);
443
        if ((rc & 0xE00) != 0)
444
            DEBUGF("NAND: ECC corrected %d errors in bank %d page %d control data!", (rc >> 9) & 7, bank, page);
445
    }
446
#ifdef NAND_TRACE
447
    else DEBUGF("NAND: Bank %d page %d: Erased page!", bank, page);
448
#endif
449
#endif
54 theseven 450
 
224 theseven 451
#ifdef NAND_TRACE
452
    DEBUGF("NAND: Read success, RC=%X", rc);
453
#endif
54 theseven 454
    return nand_unlock(rc);
2 theseven 455
}
456
 
66 theseven 457
static uint32_t nand_write_page_int(uint32_t bank, uint32_t page,
458
                                    void* databuffer, void* sparebuffer,
459
                                    uint32_t doecc, uint32_t wait)
2 theseven 460
{
224 theseven 461
#ifdef NAND_TRACE
462
    DEBUGF("NAND: Write bank %d, page %d", bank, page);
463
#endif
68 theseven 464
    uint8_t* data = (uint8_t*)databuffer;
2 theseven 465
    uint8_t* spare = nand_spare;
68 theseven 466
    if (sparebuffer) spare = (uint8_t*)sparebuffer;
467
	if ((uint32_t)databuffer & 0xf)
249 theseven 468
		panicf(PANIC_KILLTHREAD,
68 theseven 469
	           "nand_write_page: Misaligned data buffer at %08X (bank %lu, page %lu)",
470
			   (unsigned int)databuffer, bank, page);
471
	if ((uint32_t)sparebuffer & 0xf)
249 theseven 472
		panicf(PANIC_KILLTHREAD,
68 theseven 473
	           "nand_write_page: Misaligned spare buffer at %08X (bank %lu, page %lu)",
474
			   (unsigned int)sparebuffer, bank, page);
54 theseven 475
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
476
    nand_last_activity_value = USEC_TIMER;
477
    if (!nand_powered) nand_power_up();
68 theseven 478
    if (!sparebuffer) memset(spare, 0xFF, 0x40);
54 theseven 479
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
480
    if (nand_send_cmd(NAND_CMD_PROGRAM)) return nand_unlock(1);
68 theseven 481
    if (nand_send_address(page, data ? 0 : 0x800))
54 theseven 482
        return nand_unlock(1);
68 theseven 483
    if (data) nand_transfer_data_start(bank, 1, data, 0x800);
2 theseven 484
    if (doecc)
485
    {
54 theseven 486
        if (ecc_encode(3, data, nand_ecc)) return nand_unlock(1);
2 theseven 487
        memcpy(&spare[0xC], nand_ecc, 0x28);
488
        memset(nand_ctrl, 0xFF, 0x200);
489
        memcpy(nand_ctrl, spare, 0xC);
54 theseven 490
        if (ecc_encode(0, nand_ctrl, nand_ecc)) return nand_unlock(1);
2 theseven 491
        memcpy(&spare[0x34], nand_ecc, 0xC);
492
    }
68 theseven 493
    if (data)
54 theseven 494
        if (nand_transfer_data_collect(1))
495
            return nand_unlock(1);
2 theseven 496
    if (sparebuffer || doecc)
54 theseven 497
        if (nand_transfer_data(bank, 1, spare, 0x40))
498
            return nand_unlock(1);
499
    if (nand_send_cmd(NAND_CMD_PROGCNFRM)) return nand_unlock(1);
500
    if (wait) if (nand_wait_status_ready(bank)) return nand_unlock(1);
501
    return nand_unlock(0);
2 theseven 502
}
503
 
504
uint32_t nand_block_erase(uint32_t bank, uint32_t page)
505
{
224 theseven 506
#ifdef NAND_TRACE
507
    DEBUGF("NAND: Block erase starting at bank %d, page %d", bank, page);
508
#endif
54 theseven 509
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
510
    nand_last_activity_value = USEC_TIMER;
511
    if (!nand_powered) nand_power_up();
2 theseven 512
    nand_set_fmctrl0(bank, 0);
54 theseven 513
    if (nand_send_cmd(NAND_CMD_BLOCKERASE)) return nand_unlock(1);
2 theseven 514
    FMANUM = 2;
515
    FMADDR0 = page;
516
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
54 theseven 517
    if (nand_wait_cmddone()) return nand_unlock(1);
518
    if (nand_send_cmd(NAND_CMD_ERASECNFRM)) return nand_unlock(1);
519
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
520
    return nand_unlock(0);
521
}
522
 
523
uint32_t nand_read_page_fast(uint32_t page, void* databuffer,
524
                             void* sparebuffer, uint32_t doecc,
525
                             uint32_t checkempty)
526
{
224 theseven 527
#ifdef NAND_TRACE
528
    DEBUGF("NAND: Read all banks, page %d", page);
529
#endif
54 theseven 530
    uint32_t i, rc = 0;
531
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
532
     || !databuffer || !sparebuffer || !doecc)
533
    {
534
        for (i = 0; i < 4; i++)
535
        {
61 theseven 536
            if (nand_type[i] < 0) continue;
54 theseven 537
            void* databuf = (void*)0;
538
            void* sparebuf = (void*)0;
539
            if (databuffer) databuf = (void*)((uint32_t)databuffer + 0x800 * i);
540
            if (sparebuffer) sparebuf = (void*)((uint32_t)sparebuffer + 0x40 * i);
541
            uint32_t ret = nand_read_page(i, page, databuf, sparebuf, doecc, checkempty);
542
            if (ret & 1) rc |= 1 << (i << 2);
543
            if (ret & 2) rc |= 2 << (i << 2);
544
            if (ret & 0x10) rc |= 4 << (i << 2);
545
            if (ret & 0x100) rc |= 8 << (i << 2);
546
        }
547
        return rc;
548
    }
549
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
550
    nand_last_activity_value = USEC_TIMER;
551
    if (!nand_powered) nand_power_up();
552
    uint8_t status[4];
61 theseven 553
    for (i = 0; i < 4; i++) status[i] = (nand_type[i] < 0);
54 theseven 554
    for (i = 0; i < 4; i++)
555
    {
556
        if (!status[i])
557
        {
558
            nand_set_fmctrl0(i, FMCTRL0_ENABLEDMA);
559
            if (nand_send_cmd(NAND_CMD_READ))
560
                status[i] = 1;
561
        }
562
        if (!status[i])
563
            if (nand_send_address(page, 0))
564
                status[i] = 1;
565
        if (!status[i])
566
            if (nand_send_cmd(NAND_CMD_READ2))
567
                status[i] = 1;
568
    }
569
    if (!status[0])
570
        if (nand_wait_status_ready(0))
571
            status[0] = 1;
572
    if (!status[0])
573
        if (nand_transfer_data(0, 0, databuffer, 0x800))
574
            status[0] = 1;
575
    if (!status[0])
576
        if (nand_transfer_data(0, 0, sparebuffer, 0x40))
577
            status[0] = 1;
578
    for (i = 1; i < 4; i++)
579
    {
580
        if (!status[i])
581
            if (nand_wait_status_ready(i))
582
                status[i] = 1;
583
        if (!status[i])
584
            nand_transfer_data_start(i, 0, (void*)((uint32_t)databuffer
585
                                                 + 0x800 * i), 0x800);
586
        if (!status[i - 1])
587
        {
588
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
589
            ecc_start(3, (void*)((uint32_t)databuffer
590
                               + 0x800 * (i - 1)), nand_ecc, ECCCTRL_STARTDECODING);
591
        }
592
        if (!status[i])
593
            if (nand_transfer_data_collect(0))
594
                status[i] = 1;
595
        if (!status[i])
596
            nand_transfer_data_start(i, 0, (void*)((uint32_t)sparebuffer
597
                                                 + 0x40 * i), 0x40);
598
        if (!status[i - 1])
599
            if (ecc_collect() & 1)
600
                status[i - 1] = 4;
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
            ecc_start(0, nand_ctrl, nand_ecc, ECCCTRL_STARTDECODING);
607
        }
608
        if (!status[i])
609
            if (nand_transfer_data_collect(0))
610
                status[i] = 1;
611
        if (!status[i - 1])
612
        {
613
            if (ecc_collect() & 1)
614
            {
615
                status[i - 1] |= 8;
616
                memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
617
            }
618
            else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
619
            if (checkempty)
620
                status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
621
                                                        + 0x40 * (i - 1))) << 1;
622
        }
623
    }
624
    if (!status[i - 1])
625
    {
626
        memcpy(nand_ecc,(void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
627
        if (ecc_decode(3, (void*)((uint32_t)databuffer
628
                                + 0x800 * (i - 1)), nand_ecc) & 1)
629
            status[i - 1] = 4;
630
    }
631
    if (!status[i - 1])
632
    {
633
        memset(nand_ctrl, 0xFF, 0x200);
634
        memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
635
        memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
636
        if (ecc_decode(0, nand_ctrl, nand_ecc) & 1)
637
        {
638
            status[i - 1] |= 8;
639
            memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
640
        }
641
        else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
642
        if (checkempty)
643
            status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
644
                                                    + 0x40 * (i - 1))) << 1;
645
    }
646
    for (i = 0; i < 4; i++)
61 theseven 647
        if (nand_type[i] < 0)
54 theseven 648
            rc |= status[i] << (i << 2);
649
    return nand_unlock(rc);
650
}
651
 
652
uint32_t nand_write_page(uint32_t bank, uint32_t page, void* databuffer,
653
                         void* sparebuffer, uint32_t doecc)
654
{
655
    return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, 1);
656
}
657
 
658
uint32_t nand_write_page_start(uint32_t bank, uint32_t page, void* databuffer,
659
                               void* sparebuffer, uint32_t doecc)
660
{
224 theseven 661
#ifdef NAND_TRACE
662
    DEBUGF("NAND: Write all banks, page %d", page);
663
#endif
54 theseven 664
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
665
     || !databuffer || !sparebuffer || !doecc || !nand_interleaved)
666
        return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, !nand_interleaved);
667
 
668
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
669
    nand_last_activity_value = USEC_TIMER;
670
    if (!nand_powered) nand_power_up();
671
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
672
    if (nand_send_cmd(NAND_CMD_PROGRAM))
673
        return nand_unlock(1);
674
    if (nand_send_address(page, 0))
675
        return nand_unlock(1);
676
    nand_transfer_data_start(bank, 1, databuffer, 0x800);
677
    if (ecc_encode(3, databuffer, nand_ecc))
678
        return nand_unlock(1);
679
    memcpy((void*)((uint32_t)sparebuffer + 0xC), nand_ecc, 0x28);
680
    memset(nand_ctrl, 0xFF, 0x200);
681
    memcpy(nand_ctrl, sparebuffer, 0xC);
682
    if (ecc_encode(0, nand_ctrl, nand_ecc))
683
        return nand_unlock(1);
684
    memcpy((void*)((uint32_t)sparebuffer + 0x34), nand_ecc, 0xC);
685
    if (nand_transfer_data_collect(0))
686
        return nand_unlock(1);
687
    if (nand_transfer_data(bank, 1, sparebuffer, 0x40))
688
        return nand_unlock(1);
689
    return nand_unlock(nand_send_cmd(NAND_CMD_PROGCNFRM));
690
}
691
 
692
uint32_t nand_write_page_collect(uint32_t bank)
693
{
2 theseven 694
    return nand_wait_status_ready(bank);
695
}
696
 
66 theseven 697
static uint32_t nand_block_erase_fast(uint32_t page)
2 theseven 698
{
699
    uint32_t i, rc = 0;
54 theseven 700
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
701
    nand_last_activity_value = USEC_TIMER;
702
    if (!nand_powered) nand_power_up();
2 theseven 703
    for (i = 0; i < 4; i++)
704
    {
61 theseven 705
        if (nand_type[i] < 0) continue;
2 theseven 706
        nand_set_fmctrl0(i, 0);
707
        if (nand_send_cmd(NAND_CMD_BLOCKERASE))
708
        {
709
            rc |= 1 << i;
710
            continue;
711
        }
712
        FMANUM = 2;
713
        FMADDR0 = page;
714
        FMCTRL1 = FMCTRL1_DOTRANSADDR;
715
        if (nand_wait_cmddone())
716
        {
717
            rc |= 1 << i;
718
            continue;
719
        }
720
        if (nand_send_cmd(NAND_CMD_ERASECNFRM)) rc |= 1 << i;
721
    }
722
    for (i = 0; i < 4; i++)
723
    {
61 theseven 724
        if (nand_type[i] < 0) continue;
2 theseven 725
        if (rc & (1 << i)) continue;
726
        if (nand_wait_status_ready(i)) rc |= 1 << i;
727
    }
54 theseven 728
    return nand_unlock(rc);
2 theseven 729
}
730
 
731
const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
732
{
61 theseven 733
    if (nand_type[bank] < 0)
2 theseven 734
        return (struct nand_device_info_type*)0;
735
    return &nand_deviceinfotable[nand_type[bank]];
736
}
737
 
835 theseven 738
static void nand_thread(void* arg0, void* arg1, void* arg2, void* arg3)
2 theseven 739
{
54 theseven 740
    while (1)
741
    {
300 theseven 742
        mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
54 theseven 743
        if (TIME_AFTER(USEC_TIMER, nand_last_activity_value + 200000) && nand_powered)
744
            nand_power_down();
300 theseven 745
        mutex_unlock(&nand_mtx);
54 theseven 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;
429 theseven 798
    thread_create(&nand_thread_handle, "NAND idle monitor", nand_thread, nand_stack,
835 theseven 799
                  sizeof(nand_stack), OS_THREAD, 1, true, NULL, NULL, NULL, NULL);
54 theseven 800
 
2 theseven 801
    return 0;
802
}