Subversion Repositories freemyipod

Rev

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

Rev Author Line No. Line
2 theseven 1
//
2
//
3
//    Copyright 2010 TheSeven
4
//
5
//
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;
229
    clean_dcache();
230
    DMACOM3 = 4;
54 theseven 231
}
232
 
66 theseven 233
static uint32_t nand_transfer_data_collect(uint32_t direction)
54 theseven 234
{
235
    uint32_t timeout = USEC_TIMER + 20000;
251 theseven 236
    while (DMAALLST & DMAALLST_DMABUSY3)
54 theseven 237
        if (nand_timeout(timeout)) return 1;
2 theseven 238
    if (!direction) invalidate_dcache();
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;
2 theseven 262
    clean_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
    invalidate_dcache();
272
    ECC_INT_CLR = 1;
85 theseven 273
    SRCPND = (1 << IRQ_ECC);
54 theseven 274
    return ecc_unlock(ECC_RESULT);
2 theseven 275
}
276
 
66 theseven 277
static uint32_t ecc_decode(uint32_t size, void* databuffer, void* sparebuffer)
54 theseven 278
{
279
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTDECODING);
280
    uint32_t rc = ecc_collect();
281
    return rc;
282
}
283
 
66 theseven 284
static uint32_t ecc_encode(uint32_t size, void* databuffer, void* sparebuffer)
2 theseven 285
{
54 theseven 286
    ecc_start(size, databuffer, sparebuffer, ECCCTRL_STARTENCODING);
287
    ecc_collect();
2 theseven 288
    return 0;
289
}
290
 
66 theseven 291
static uint32_t nand_check_empty(uint8_t* buffer)
2 theseven 292
{
293
    uint32_t i, count;
294
    count = 0;
295
    for (i = 0; i < 0x40; i++) if (buffer[i] != 0xFF) count++;
296
    if (count < 2) return 1;
297
    return 0;
298
}
299
 
66 theseven 300
static uint32_t nand_get_chip_type(uint32_t bank)
2 theseven 301
{
54 theseven 302
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
2 theseven 303
    uint32_t result;
61 theseven 304
    if (nand_reset(bank)) return nand_unlock(0xFFFFFFFE);
305
    if (nand_send_cmd(0x90)) return nand_unlock(0xFFFFFFFD);
2 theseven 306
    FMANUM = 0;
307
    FMADDR0 = 0;
308
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
61 theseven 309
    if (nand_wait_cmddone()) return nand_unlock(0xFFFFFFFC);
2 theseven 310
    FMDNUM = 4;
311
    FMCTRL1 = FMCTRL1_DOREADDATA;
61 theseven 312
    if (nand_wait_addrdone()) return nand_unlock(0xFFFFFFFB);
2 theseven 313
    result = FMFIFO;
314
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
54 theseven 315
    return nand_unlock(result);
2 theseven 316
}
317
 
54 theseven 318
void nand_set_active(void)
2 theseven 319
{
54 theseven 320
    nand_last_activity_value = USEC_TIMER;
321
}
322
 
323
long nand_last_activity(void)
324
{
325
    return nand_last_activity_value;
326
}
327
 
328
void nand_power_up(void)
329
{
330
    uint32_t i;
331
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
332
    nand_last_activity_value = USEC_TIMER;
88 theseven 333
    PWRCON(1) &= ~0x40;
334
    PWRCON(0) &= ~0x100000;
54 theseven 335
    PCON2 = 0x33333333;
336
    PDAT2 = 0;
337
    PCON3 = 0x11113333;
338
    PDAT3 = 0;
339
    PCON4 = 0x33333333;
340
    PDAT4 = 0;
341
    PCON5 = (PCON5 & ~0xF) | 3;
342
    PUNK5 = 1;
343
    pmu_ldo_set_voltage(4, 0x15);
344
    pmu_ldo_power_on(4);
345
    sleep(50000);
346
    nand_last_activity_value = USEC_TIMER;
347
    for (i = 0; i < 4; i++)
61 theseven 348
        if (nand_type[i] >= 0)
349
            if (nand_reset(i))
249 theseven 350
				panicf(PANIC_KILLTHREAD, "nand_power_up: nand_reset(bank=%d) failed.", (unsigned int)i);
54 theseven 351
    nand_powered = 1;
352
    nand_last_activity_value = USEC_TIMER;
353
    mutex_unlock(&nand_mtx);
354
}
355
 
356
void nand_power_down(void)
357
{
358
    if (!nand_powered) return;
359
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
360
    pmu_ldo_power_off(4);
361
    PCON2 = 0x11111111;
362
    PDAT2 = 0;
363
    PCON3 = 0x11111111;
364
    PDAT3 = 0;
365
    PCON4 = 0x11111111;
366
    PDAT4 = 0;
367
    PCON5 = (PCON5 & ~0xF) | 1;
368
    PUNK5 = 1;
88 theseven 369
    PWRCON(1) |= 0x40;
370
    PWRCON(0) |= 0x100000;
54 theseven 371
    nand_powered = 0;
372
    mutex_unlock(&nand_mtx);
373
}
374
 
375
uint32_t nand_read_page(uint32_t bank, uint32_t page, void* databuffer,
376
                        void* sparebuffer, uint32_t doecc,
377
                        uint32_t checkempty)
378
{
224 theseven 379
#ifdef NAND_TRACE
380
    DEBUGF("NAND: Read bank %d, page %d", bank, page);
381
#endif
68 theseven 382
    uint8_t* data = (uint8_t*)databuffer;
54 theseven 383
    uint8_t* spare = nand_spare;
68 theseven 384
    if (sparebuffer) spare = (uint8_t*)sparebuffer;
69 theseven 385
	if ((uint32_t)databuffer & (CACHEALIGN_SIZE - 1))
249 theseven 386
		panicf(PANIC_KILLTHREAD,
68 theseven 387
	           "nand_read_page: Misaligned data buffer at %08X (bank %lu, page %lu)",
388
			   (unsigned int)databuffer, bank, page);
69 theseven 389
	if ((uint32_t)sparebuffer & (CACHEALIGN_SIZE - 1))
249 theseven 390
		panicf(PANIC_KILLTHREAD,
68 theseven 391
	           "nand_read_page: Misaligned spare buffer at %08X (bank %lu, page %lu)",
392
			   (unsigned int)sparebuffer, bank, page);
54 theseven 393
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
394
    nand_last_activity_value = USEC_TIMER;
395
    if (!nand_powered) nand_power_up();
2 theseven 396
    uint32_t rc, eccresult;
397
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
54 theseven 398
    if (nand_send_cmd(NAND_CMD_READ)) return nand_unlock(1);
68 theseven 399
    if (nand_send_address(page, data ? 0 : 0x800))
54 theseven 400
        return nand_unlock(1);
401
    if (nand_send_cmd(NAND_CMD_READ2)) return nand_unlock(1);
402
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
68 theseven 403
    if (data)
54 theseven 404
        if (nand_transfer_data(bank, 0, data, 0x800))
405
            return nand_unlock(1);
2 theseven 406
    rc = 0;
407
    if (!doecc)
408
    {
409
        if (sparebuffer)
410
        {
54 theseven 411
            if (nand_transfer_data(bank, 0, spare, 0x40))
412
                return nand_unlock(1);
2 theseven 413
            if (checkempty)
54 theseven 414
                rc = nand_check_empty((uint8_t*)sparebuffer) << 1;
2 theseven 415
        }
54 theseven 416
        return nand_unlock(rc);
2 theseven 417
    }
54 theseven 418
    if (nand_transfer_data(bank, 0, spare, 0x40)) return nand_unlock(1);
419
    if (databuffer)
2 theseven 420
    {
54 theseven 421
        memcpy(nand_ecc, &spare[0xC], 0x28);
2 theseven 422
        rc |= (ecc_decode(3, data, nand_ecc) & 0xF) << 4;
423
    }
424
    memset(nand_ctrl, 0xFF, 0x200);
425
    memcpy(nand_ctrl, spare, 0xC);
54 theseven 426
    memcpy(nand_ecc, &spare[0x34], 0xC);
2 theseven 427
    eccresult = ecc_decode(0, nand_ctrl, nand_ecc);
428
    rc |= (eccresult & 0xF) << 8;
54 theseven 429
    if (sparebuffer)
2 theseven 430
    {
54 theseven 431
        if (eccresult & 1) memset(sparebuffer, 0xFF, 0xC);
432
        else memcpy(sparebuffer, nand_ctrl, 0xC);
2 theseven 433
    }
434
    if (checkempty) rc |= nand_check_empty(spare) << 1;
224 theseven 435
#ifdef NAND_DEBUG
436
    if ((rc & 2) == 0)
437
    {
438
        if ((rc & 0x10) != 0)
439
            DEBUGF("NAND: ECC failed to correct bank %d page %d user data!", bank, page);
440
        if ((rc & 0xE0) != 0)
441
            DEBUGF("NAND: ECC corrected %d errors in bank %d page %d user data!", rc >> 5, bank, page);
442
        if ((rc & 0x100) != 0)
443
            DEBUGF("NAND: ECC failed to correct bank %d page %d control data!", bank, page);
444
        if ((rc & 0xE00) != 0)
445
            DEBUGF("NAND: ECC corrected %d errors in bank %d page %d control data!", (rc >> 9) & 7, bank, page);
446
    }
447
#ifdef NAND_TRACE
448
    else DEBUGF("NAND: Bank %d page %d: Erased page!", bank, page);
449
#endif
450
#endif
54 theseven 451
 
224 theseven 452
#ifdef NAND_TRACE
453
    DEBUGF("NAND: Read success, RC=%X", rc);
454
#endif
54 theseven 455
    return nand_unlock(rc);
2 theseven 456
}
457
 
66 theseven 458
static uint32_t nand_write_page_int(uint32_t bank, uint32_t page,
459
                                    void* databuffer, void* sparebuffer,
460
                                    uint32_t doecc, uint32_t wait)
2 theseven 461
{
224 theseven 462
#ifdef NAND_TRACE
463
    DEBUGF("NAND: Write bank %d, page %d", bank, page);
464
#endif
68 theseven 465
    uint8_t* data = (uint8_t*)databuffer;
2 theseven 466
    uint8_t* spare = nand_spare;
68 theseven 467
    if (sparebuffer) spare = (uint8_t*)sparebuffer;
468
	if ((uint32_t)databuffer & 0xf)
249 theseven 469
		panicf(PANIC_KILLTHREAD,
68 theseven 470
	           "nand_write_page: Misaligned data buffer at %08X (bank %lu, page %lu)",
471
			   (unsigned int)databuffer, bank, page);
472
	if ((uint32_t)sparebuffer & 0xf)
249 theseven 473
		panicf(PANIC_KILLTHREAD,
68 theseven 474
	           "nand_write_page: Misaligned spare buffer at %08X (bank %lu, page %lu)",
475
			   (unsigned int)sparebuffer, bank, page);
54 theseven 476
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
477
    nand_last_activity_value = USEC_TIMER;
478
    if (!nand_powered) nand_power_up();
68 theseven 479
    if (!sparebuffer) memset(spare, 0xFF, 0x40);
54 theseven 480
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
481
    if (nand_send_cmd(NAND_CMD_PROGRAM)) return nand_unlock(1);
68 theseven 482
    if (nand_send_address(page, data ? 0 : 0x800))
54 theseven 483
        return nand_unlock(1);
68 theseven 484
    if (data) nand_transfer_data_start(bank, 1, data, 0x800);
2 theseven 485
    if (doecc)
486
    {
54 theseven 487
        if (ecc_encode(3, data, nand_ecc)) return nand_unlock(1);
2 theseven 488
        memcpy(&spare[0xC], nand_ecc, 0x28);
489
        memset(nand_ctrl, 0xFF, 0x200);
490
        memcpy(nand_ctrl, spare, 0xC);
54 theseven 491
        if (ecc_encode(0, nand_ctrl, nand_ecc)) return nand_unlock(1);
2 theseven 492
        memcpy(&spare[0x34], nand_ecc, 0xC);
493
    }
68 theseven 494
    if (data)
54 theseven 495
        if (nand_transfer_data_collect(1))
496
            return nand_unlock(1);
2 theseven 497
    if (sparebuffer || doecc)
54 theseven 498
        if (nand_transfer_data(bank, 1, spare, 0x40))
499
            return nand_unlock(1);
500
    if (nand_send_cmd(NAND_CMD_PROGCNFRM)) return nand_unlock(1);
501
    if (wait) if (nand_wait_status_ready(bank)) return nand_unlock(1);
502
    return nand_unlock(0);
2 theseven 503
}
504
 
505
uint32_t nand_block_erase(uint32_t bank, uint32_t page)
506
{
224 theseven 507
#ifdef NAND_TRACE
508
    DEBUGF("NAND: Block erase starting at bank %d, page %d", bank, page);
509
#endif
54 theseven 510
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
511
    nand_last_activity_value = USEC_TIMER;
512
    if (!nand_powered) nand_power_up();
2 theseven 513
    nand_set_fmctrl0(bank, 0);
54 theseven 514
    if (nand_send_cmd(NAND_CMD_BLOCKERASE)) return nand_unlock(1);
2 theseven 515
    FMANUM = 2;
516
    FMADDR0 = page;
517
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
54 theseven 518
    if (nand_wait_cmddone()) return nand_unlock(1);
519
    if (nand_send_cmd(NAND_CMD_ERASECNFRM)) return nand_unlock(1);
520
    if (nand_wait_status_ready(bank)) return nand_unlock(1);
521
    return nand_unlock(0);
522
}
523
 
524
uint32_t nand_read_page_fast(uint32_t page, void* databuffer,
525
                             void* sparebuffer, uint32_t doecc,
526
                             uint32_t checkempty)
527
{
224 theseven 528
#ifdef NAND_TRACE
529
    DEBUGF("NAND: Read all banks, page %d", page);
530
#endif
54 theseven 531
    uint32_t i, rc = 0;
532
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
533
     || !databuffer || !sparebuffer || !doecc)
534
    {
535
        for (i = 0; i < 4; i++)
536
        {
61 theseven 537
            if (nand_type[i] < 0) continue;
54 theseven 538
            void* databuf = (void*)0;
539
            void* sparebuf = (void*)0;
540
            if (databuffer) databuf = (void*)((uint32_t)databuffer + 0x800 * i);
541
            if (sparebuffer) sparebuf = (void*)((uint32_t)sparebuffer + 0x40 * i);
542
            uint32_t ret = nand_read_page(i, page, databuf, sparebuf, doecc, checkempty);
543
            if (ret & 1) rc |= 1 << (i << 2);
544
            if (ret & 2) rc |= 2 << (i << 2);
545
            if (ret & 0x10) rc |= 4 << (i << 2);
546
            if (ret & 0x100) rc |= 8 << (i << 2);
547
        }
548
        return rc;
549
    }
550
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
551
    nand_last_activity_value = USEC_TIMER;
552
    if (!nand_powered) nand_power_up();
553
    uint8_t status[4];
61 theseven 554
    for (i = 0; i < 4; i++) status[i] = (nand_type[i] < 0);
54 theseven 555
    for (i = 0; i < 4; i++)
556
    {
557
        if (!status[i])
558
        {
559
            nand_set_fmctrl0(i, FMCTRL0_ENABLEDMA);
560
            if (nand_send_cmd(NAND_CMD_READ))
561
                status[i] = 1;
562
        }
563
        if (!status[i])
564
            if (nand_send_address(page, 0))
565
                status[i] = 1;
566
        if (!status[i])
567
            if (nand_send_cmd(NAND_CMD_READ2))
568
                status[i] = 1;
569
    }
570
    if (!status[0])
571
        if (nand_wait_status_ready(0))
572
            status[0] = 1;
573
    if (!status[0])
574
        if (nand_transfer_data(0, 0, databuffer, 0x800))
575
            status[0] = 1;
576
    if (!status[0])
577
        if (nand_transfer_data(0, 0, sparebuffer, 0x40))
578
            status[0] = 1;
579
    for (i = 1; i < 4; i++)
580
    {
581
        if (!status[i])
582
            if (nand_wait_status_ready(i))
583
                status[i] = 1;
584
        if (!status[i])
585
            nand_transfer_data_start(i, 0, (void*)((uint32_t)databuffer
586
                                                 + 0x800 * i), 0x800);
587
        if (!status[i - 1])
588
        {
589
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
590
            ecc_start(3, (void*)((uint32_t)databuffer
591
                               + 0x800 * (i - 1)), nand_ecc, ECCCTRL_STARTDECODING);
592
        }
593
        if (!status[i])
594
            if (nand_transfer_data_collect(0))
595
                status[i] = 1;
596
        if (!status[i])
597
            nand_transfer_data_start(i, 0, (void*)((uint32_t)sparebuffer
598
                                                 + 0x40 * i), 0x40);
599
        if (!status[i - 1])
600
            if (ecc_collect() & 1)
601
                status[i - 1] = 4;
602
        if (!status[i - 1])
603
        {
604
            memset(nand_ctrl, 0xFF, 0x200);
605
            memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
606
            memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
607
            ecc_start(0, nand_ctrl, nand_ecc, ECCCTRL_STARTDECODING);
608
        }
609
        if (!status[i])
610
            if (nand_transfer_data_collect(0))
611
                status[i] = 1;
612
        if (!status[i - 1])
613
        {
614
            if (ecc_collect() & 1)
615
            {
616
                status[i - 1] |= 8;
617
                memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
618
            }
619
            else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
620
            if (checkempty)
621
                status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
622
                                                        + 0x40 * (i - 1))) << 1;
623
        }
624
    }
625
    if (!status[i - 1])
626
    {
627
        memcpy(nand_ecc,(void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0xC), 0x28);
628
        if (ecc_decode(3, (void*)((uint32_t)databuffer
629
                                + 0x800 * (i - 1)), nand_ecc) & 1)
630
            status[i - 1] = 4;
631
    }
632
    if (!status[i - 1])
633
    {
634
        memset(nand_ctrl, 0xFF, 0x200);
635
        memcpy(nand_ctrl, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xC);
636
        memcpy(nand_ecc, (void*)((uint32_t)sparebuffer + 0x40 * (i - 1) + 0x34), 0xC);
637
        if (ecc_decode(0, nand_ctrl, nand_ecc) & 1)
638
        {
639
            status[i - 1] |= 8;
640
            memset((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), 0xFF, 0xC);
641
        }
642
        else memcpy((void*)((uint32_t)sparebuffer + 0x40 * (i - 1)), nand_ctrl, 0xC);
643
        if (checkempty)
644
            status[i - 1] |= nand_check_empty((void*)((uint32_t)sparebuffer
645
                                                    + 0x40 * (i - 1))) << 1;
646
    }
647
    for (i = 0; i < 4; i++)
61 theseven 648
        if (nand_type[i] < 0)
54 theseven 649
            rc |= status[i] << (i << 2);
650
    return nand_unlock(rc);
651
}
652
 
653
uint32_t nand_write_page(uint32_t bank, uint32_t page, void* databuffer,
654
                         void* sparebuffer, uint32_t doecc)
655
{
656
    return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, 1);
657
}
658
 
659
uint32_t nand_write_page_start(uint32_t bank, uint32_t page, void* databuffer,
660
                               void* sparebuffer, uint32_t doecc)
661
{
224 theseven 662
#ifdef NAND_TRACE
663
    DEBUGF("NAND: Write all banks, page %d", page);
664
#endif
54 theseven 665
    if (((uint32_t)databuffer & 0xf) || ((uint32_t)sparebuffer & 0xf)
666
     || !databuffer || !sparebuffer || !doecc || !nand_interleaved)
667
        return nand_write_page_int(bank, page, databuffer, sparebuffer, doecc, !nand_interleaved);
668
 
669
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
670
    nand_last_activity_value = USEC_TIMER;
671
    if (!nand_powered) nand_power_up();
672
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
673
    if (nand_send_cmd(NAND_CMD_PROGRAM))
674
        return nand_unlock(1);
675
    if (nand_send_address(page, 0))
676
        return nand_unlock(1);
677
    nand_transfer_data_start(bank, 1, databuffer, 0x800);
678
    if (ecc_encode(3, databuffer, nand_ecc))
679
        return nand_unlock(1);
680
    memcpy((void*)((uint32_t)sparebuffer + 0xC), nand_ecc, 0x28);
681
    memset(nand_ctrl, 0xFF, 0x200);
682
    memcpy(nand_ctrl, sparebuffer, 0xC);
683
    if (ecc_encode(0, nand_ctrl, nand_ecc))
684
        return nand_unlock(1);
685
    memcpy((void*)((uint32_t)sparebuffer + 0x34), nand_ecc, 0xC);
686
    if (nand_transfer_data_collect(0))
687
        return nand_unlock(1);
688
    if (nand_transfer_data(bank, 1, sparebuffer, 0x40))
689
        return nand_unlock(1);
690
    return nand_unlock(nand_send_cmd(NAND_CMD_PROGCNFRM));
691
}
692
 
693
uint32_t nand_write_page_collect(uint32_t bank)
694
{
2 theseven 695
    return nand_wait_status_ready(bank);
696
}
697
 
66 theseven 698
static uint32_t nand_block_erase_fast(uint32_t page)
2 theseven 699
{
700
    uint32_t i, rc = 0;
54 theseven 701
    mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
702
    nand_last_activity_value = USEC_TIMER;
703
    if (!nand_powered) nand_power_up();
2 theseven 704
    for (i = 0; i < 4; i++)
705
    {
61 theseven 706
        if (nand_type[i] < 0) continue;
2 theseven 707
        nand_set_fmctrl0(i, 0);
708
        if (nand_send_cmd(NAND_CMD_BLOCKERASE))
709
        {
710
            rc |= 1 << i;
711
            continue;
712
        }
713
        FMANUM = 2;
714
        FMADDR0 = page;
715
        FMCTRL1 = FMCTRL1_DOTRANSADDR;
716
        if (nand_wait_cmddone())
717
        {
718
            rc |= 1 << i;
719
            continue;
720
        }
721
        if (nand_send_cmd(NAND_CMD_ERASECNFRM)) rc |= 1 << i;
722
    }
723
    for (i = 0; i < 4; i++)
724
    {
61 theseven 725
        if (nand_type[i] < 0) continue;
2 theseven 726
        if (rc & (1 << i)) continue;
727
        if (nand_wait_status_ready(i)) rc |= 1 << i;
728
    }
54 theseven 729
    return nand_unlock(rc);
2 theseven 730
}
731
 
732
const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
733
{
61 theseven 734
    if (nand_type[bank] < 0)
2 theseven 735
        return (struct nand_device_info_type*)0;
736
    return &nand_deviceinfotable[nand_type[bank]];
737
}
738
 
54 theseven 739
static void nand_thread(void)
2 theseven 740
{
54 theseven 741
    while (1)
742
    {
300 theseven 743
        mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
54 theseven 744
        if (TIME_AFTER(USEC_TIMER, nand_last_activity_value + 200000) && nand_powered)
745
            nand_power_down();
300 theseven 746
        mutex_unlock(&nand_mtx);
54 theseven 747
        sleep(100000);
748
    }
749
}
750
 
61 theseven 751
int nand_device_init(void)
54 theseven 752
{
753
    mutex_init(&nand_mtx);
754
    wakeup_init(&nand_wakeup);
755
    mutex_init(&ecc_mtx);
756
    wakeup_init(&ecc_wakeup);
757
 
2 theseven 758
    uint32_t type;
759
    uint32_t i, j;
54 theseven 760
 
761
    /* Assume there are 0 banks, to prevent
762
       nand_power_up from talking with them yet. */
61 theseven 763
    for (i = 0; i < 4; i++) nand_type[i] = -1;
54 theseven 764
    nand_power_up();
765
 
766
    /* Now that the flash is powered on, detect how
767
       many banks we really have and initialize them. */
2 theseven 768
    for (i = 0; i < 4; i++)
769
    {
770
        nand_tunk1[i] = 7;
771
        nand_twp[i] = 7;
772
        nand_tunk2[i] = 7;
773
        nand_tunk3[i] = 7;
774
        type = nand_get_chip_type(i);
61 theseven 775
        if (type >= 0xFFFFFFF0)
776
        {
777
            nand_type[i] = (int)type;
778
            continue;
779
        }
2 theseven 780
        for (j = 0; ; j++)
781
        {
58 theseven 782
            if (j == ARRAYLEN(nand_deviceinfotable)) break;
2 theseven 783
            else if (nand_deviceinfotable[j].id == type)
784
            {
785
                nand_type[i] = j;
786
                break;
787
            }
788
        }
789
        nand_tunk1[i] = nand_deviceinfotable[nand_type[i]].tunk1;
790
        nand_twp[i] = nand_deviceinfotable[nand_type[i]].twp;
791
        nand_tunk2[i] = nand_deviceinfotable[nand_type[i]].tunk2;
792
        nand_tunk3[i] = nand_deviceinfotable[nand_type[i]].tunk3;
793
    }
61 theseven 794
    if (nand_type[0] < 0) return nand_type[0];
54 theseven 795
    nand_interleaved = ((nand_type[0] >> 22) & 1);
796
    nand_cached = ((nand_type[0] >> 23) & 1);
797
 
798
    nand_last_activity_value = USEC_TIMER;
429 theseven 799
    thread_create(&nand_thread_handle, "NAND idle monitor", nand_thread, nand_stack,
543 theseven 800
                  sizeof(nand_stack), OS_THREAD, 1, true);
54 theseven 801
 
2 theseven 802
    return 0;
803
}