Subversion Repositories freemyipod

Rev

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

Rev Author Line No. Line
253 theseven 1
//
2
//
3
//    Copyright 2009 TheSeven
4
//
5
//
6
//    This file is part of the Linux4Nano toolkit.
7
//
8
//    TheSeven's iBugger 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
//    TheSeven's iBugger 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 the Linux4Nano toolkit.  If not, see <http://www.gnu.org/licenses/>.
20
//
21
//
22
 
23
 
24
#include <toolkit.h>
25
#include <util.h>
26
#include <timer.h>
27
#include <nand.h>
28
 
29
#define NAND_CMD_READ       0x00
30
#define NAND_CMD_PROGCNFRM  0x10
31
#define NAND_CMD_READ2      0x30
32
#define NAND_CMD_BLOCKERASE 0x60
33
#define NAND_CMD_GET_STATUS 0x70
34
#define NAND_CMD_PROGRAM    0x80
35
#define NAND_CMD_ERASECNFRM 0xD0
36
#define NAND_CMD_RESET      0xFF
37
 
38
#define NAND_STATUS_READY   0x40
39
 
40
#define NAND_DEVICEINFOTABLE_ENTRIES 33
41
 
42
static const struct nand_device_info_type nand_deviceinfotable[] =
43
{
44
    {0x1580F1EC, 1024, 968, 6, 2, 1, 0},
45
    {0x1580DAEC, 2048, 1936, 6, 2, 1, 0},
46
    {0x15C1DAEC, 2048, 1936, 6, 2, 1, 0},
47
    {0x1510DCEC, 4096, 3872, 6, 2, 1, 0},
48
    {0x95C1DCEC, 4096, 3872, 6, 2, 1, 0},
49
    {0x2514DCEC, 2048, 1936, 7, 2, 1, 0},
50
    {0x2514D3EC, 4096, 3872, 7, 2, 1, 0},
51
    {0x2555D3EC, 4096, 3872, 7, 2, 1, 0},
52
    {0x2555D5EC, 8192, 7744, 7, 2, 1, 0},
53
    {0x2585D3AD, 4096, 3872, 7, 3, 2, 0},
54
    {0x9580DCAD, 4096, 3872, 6, 3, 2, 0},
55
    {0xA514D3AD, 4096, 3872, 7, 3, 2, 0},
56
    {0xA550D3AD, 4096, 3872, 7, 3, 2, 0},
57
    {0xA560D5AD, 4096, 3872, 7, 3, 2, 0},
58
    {0xA555D5AD, 8192, 7744, 7, 3, 2, 0},
59
    {0xA585D598, 8320, 7744, 7, 3, 1, 0},
60
    {0xA584D398, 4160, 3872, 7, 3, 1, 0},
61
    {0x95D1D32C, 8192, 7744, 6, 2, 1, 0},
62
    {0x1580DC2C, 4096, 3872, 6, 2, 1, 0},
63
    {0x15C1D32C, 8192, 7744, 6, 2, 1, 0},
64
    {0x9590DC2C, 4096, 3872, 6, 2, 1, 0},
65
    {0xA594D32C, 4096, 3872, 7, 2, 1, 0},
66
    {0x2584DC2C, 2048, 1936, 7, 2, 1, 0},
67
    {0xA5D5D52C, 8192, 7744, 7, 3, 2, 0},
68
    {0x95D1D389, 8192, 7744, 6, 2, 1, 0},
69
    {0x1580DC89, 4096, 3872, 6, 2, 1, 0},
70
    {0x15C1D389, 8192, 7744, 6, 2, 1, 0},
71
    {0x9590DC89, 4096, 3872, 6, 2, 1, 0},
72
    {0xA594D389, 4096, 3872, 7, 2, 1, 0},
73
    {0x2584DC89, 2048, 1936, 7, 2, 1, 0},
74
    {0xA5D5D589, 8192, 7744, 7, 2, 1, 0},
75
    {0xA514D320, 4096, 3872, 7, 2, 1, 0},
76
    {0xA555D520, 8192, 3872, 7, 2, 1, 0}
77
};
78
 
79
static uint8_t nand_tunk1;
80
static uint8_t nand_twp;
81
static int nand_type[4];
82
 
83
static uint8_t nand_ctrl[0x200] __attribute__((aligned(16)));
84
static uint8_t nand_ecc[0x30] __attribute__((aligned(16)));
85
 
86
 
87
static uint32_t nand_wait_rbbdone(void)
88
{
89
    uint32_t timeout = USEC_TIMER + 20000;
90
    while (!(FMCSTAT & FMCSTAT_RBBDONE))
91
        if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
92
    FMCSTAT = FMCSTAT_RBBDONE;
93
    return 0;
94
}
95
 
255 theseven 96
static uint32_t nand_wait_cmddone(void)
253 theseven 97
{
255 theseven 98
    uint32_t timeout = USEC_TIMER + 20000;
99
    while ((FMCSTAT & FMCSTAT_CMDDONE) == 0)
100
        if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
253 theseven 101
    FMCSTAT = FMCSTAT_CMDDONE;
255 theseven 102
    return 0;
253 theseven 103
}
104
 
255 theseven 105
static uint32_t nand_wait_addrdone(void)
253 theseven 106
{
255 theseven 107
    uint32_t timeout = USEC_TIMER + 20000;
108
    while ((FMCSTAT & FMCSTAT_ADDRDONE) == 0)
109
        if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
253 theseven 110
    FMCSTAT = FMCSTAT_ADDRDONE;
255 theseven 111
    return 0;
253 theseven 112
}
113
 
255 theseven 114
static uint32_t nand_wait_chip_ready(uint32_t bank)
253 theseven 115
{
255 theseven 116
    uint32_t timeout = USEC_TIMER + 20000;
117
    while ((FMCSTAT & (FMCSTAT_BANK0READY << bank)) == 0)
118
        if (TIME_AFTER(USEC_TIMER, timeout)) return 1;
253 theseven 119
    FMCSTAT = (FMCSTAT_BANK0READY << bank);
255 theseven 120
    return 0;
253 theseven 121
}
122
 
123
static void nand_set_fmctrl0(uint32_t bank, uint32_t flags)
124
{
125
    FMCTRL0 = (nand_tunk1 << 16) | (nand_twp << 12)
126
            | (1 << 11) | 1 | (1 << (bank + 1)) | flags;
127
}
128
 
129
static uint32_t nand_send_cmd(uint32_t cmd)
130
{
131
    FMCMD = cmd;
132
    return nand_wait_rbbdone();
133
}
134
 
135
static void nand_send_address(uint32_t page, uint32_t offset)
136
{
137
    FMANUM = 4;
138
    FMADDR0 = (page << 16) | offset;
139
    FMADDR1 = (page >> 16) & 0xFF;
140
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
141
    nand_wait_cmddone();
142
}
143
 
144
uint32_t nand_reset(uint32_t bank)
145
{
146
    nand_set_fmctrl0(bank, 0);
147
    if (nand_send_cmd(NAND_CMD_RESET)) return 1;
255 theseven 148
    if (nand_wait_chip_ready(bank)) return 1;
253 theseven 149
    FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
150
    sleep(1000);
151
    return 0;
152
}
153
 
154
static uint32_t nand_wait_status_ready(uint32_t bank)
155
{
156
    nand_set_fmctrl0(bank, 0);
157
    if ((FMCSTAT & (FMCSTAT_BANK0READY << bank)))
158
        FMCSTAT = (FMCSTAT_BANK0READY << bank);
159
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
160
    nand_send_cmd(NAND_CMD_GET_STATUS);
161
    while (1)
162
    {
163
        FMDNUM = 0;
164
        FMCTRL1 = FMCTRL1_DOREADDATA;
165
        nand_wait_addrdone();
166
        if (FMFIFO & NAND_STATUS_READY) break;
167
        FMCTRL1 = FMCTRL1_CLEARRFIFO;
168
    }
169
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
170
    nand_send_cmd(NAND_CMD_READ);
171
}
172
 
173
static void nand_transfer_data(uint32_t bank, uint32_t direction, void* buffer, uint32_t size)
174
{
175
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
176
    FMDNUM = size - 1;
177
    FMCTRL1 = FMCTRL1_DOREADDATA << direction;
178
    DMACON3 = (2 << DMACON_DEVICE_SHIFT)
179
            | (direction << DMACON_DIRECTION_SHIFT)
180
            | (2 << DMACON_DATA_SIZE_SHIFT)
181
            | (3 << DMACON_BURST_LEN_SHIFT);
182
    while ((DMAALLST & DMAALLST_CHAN3_MASK))
183
        DMACOM3 = DMACOM_CLEARBOTHDONE;
184
    DMABASE3 = (uint32_t)buffer;
185
    DMATCNT3 = (size >> 4) - 1;
186
    DMACOM3 = 4;
187
    while (DMAALLST & DMAALLST_DMABUSY3);
188
    nand_wait_addrdone();
189
    if (!direction) FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
190
    else FMCTRL1 = FMCTRL1_CLEARRFIFO;
191
}
192
 
193
static uint32_t ecc_decode(uint32_t size, void* databuffer, void* sparebuffer)
194
{
195
    ECC_INT_CLR = 1;
196
    SRCPND = INTMSK_ECC;
197
    ECC_UNK1 = size;
198
    ECC_DATA_PTR = (uint32_t)databuffer;
199
    ECC_SPARE_PTR = (uint32_t)sparebuffer;
200
    ECC_CTRL = ECCCTRL_STARTDECODING;
201
    while (!(SRCPND & INTMSK_ECC));
202
    ECC_INT_CLR = 1;
203
    SRCPND = INTMSK_ECC;
204
    return ECC_RESULT;
205
}
206
 
207
uint32_t nand_check_empty(uint8_t* buffer)
208
{
209
    uint32_t i, count;
210
    count = 0;
211
    for (i = 0; i < 0x40; i++) if (buffer[i] != 0xFF) count++;
212
    if (count < 2) return 1;
213
    return 0;
214
}
215
 
216
uint32_t nand_get_chip_type(uint32_t bank)
217
{
218
    uint32_t result;
219
    if (nand_reset(bank)) return 0xFFFFFFFF;
220
    if (nand_send_cmd(0x90)) return 0xFFFFFFFF;
221
    FMANUM = 0;
222
    FMADDR0 = 0;
223
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
255 theseven 224
    if (nand_wait_cmddone()) return 0xFFFFFFFF;
253 theseven 225
    FMDNUM = 4;
226
    FMCTRL1 = FMCTRL1_DOREADDATA;
255 theseven 227
    if (nand_wait_addrdone()) return 0xFFFFFFFF;
253 theseven 228
    result = FMFIFO;
229
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
230
    return result;
231
}
232
 
233
uint32_t nand_read_page(uint32_t bank, uint32_t page, void* databuffer,
234
                        void* sparebuffer, uint32_t checkempty)
235
{
236
    uint32_t rc, eccresult;
237
    uint8_t* data = (uint8_t*)databuffer;
238
    uint8_t* spare = (uint8_t*)sparebuffer;
239
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
240
    nand_send_cmd(NAND_CMD_READ);
241
    nand_send_address(page, 0);
242
    nand_send_cmd(NAND_CMD_READ2);
243
    nand_wait_status_ready(bank);
244
    nand_transfer_data(bank, 0, data, 0x800);
245
    nand_transfer_data(bank, 0, spare, 0x40);
246
    memcpy(nand_ecc, &spare[0xC], 0x28);
247
    rc = (ecc_decode(3, data, nand_ecc) & 0xF) << 4;
248
    memset(nand_ctrl, 0xFF, 0x200);
249
    memcpy(nand_ctrl, spare, 0xC);
250
    memcpy(nand_ecc, &spare[0x34], 0xC);
251
    eccresult = ecc_decode(0, nand_ctrl, nand_ecc);
252
    rc |= (eccresult & 0xF) << 8;
253
    if (eccresult & 1) memset(spare, 0xFF, 0xC);
254
    else memcpy(spare, nand_ctrl, 0xC);
255
    if (checkempty) rc |= nand_check_empty(spare) << 1;
256
    return rc;
257
}
258
 
259
const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
260
{
261
    if (nand_type[bank] < 0)
262
        return (struct nand_device_info_type*)0;
263
    return &nand_deviceinfotable[nand_type[bank]];
264
}
265
 
266
uint32_t nand_init()
267
{
268
    uint32_t type;
269
    uint32_t i, j;
270
    PWRCONEXT &= ~0x40;
271
    PWRCON &= ~0x100000;
272
    PCON2 = 0x33333333;
273
    PDAT2 = 0;
274
    PCON3 = 0x11113333;
275
    PDAT3 = 0;
276
    PCON4 = 0x33333333;
277
    PDAT4 = 0;
278
    PCON5 = (PCON5 & ~0xF) | 3;
279
    PUNK5 = 1;
280
    sleep(10000);
281
    for (i = 0; i < 4; i++) nand_type[i] = -1;
282
    for (i = 0; i < 4; i++)
283
    {
284
        nand_tunk1 = 7;
285
        nand_twp = 7;
286
        type = nand_get_chip_type(i);
287
        if (type >= 0xFFFFFFF0)
288
        {
289
            nand_type[i] = (int)type;
290
            continue;
291
        }
292
        for (j = 0; ; j++)
293
        {
294
            if (j == NAND_DEVICEINFOTABLE_ENTRIES) break;
295
            else if (nand_deviceinfotable[j].id == type)
296
            {
255 theseven 297
                nand_tunk1 = nand_deviceinfotable[j].tunk1;
298
                nand_twp = nand_deviceinfotable[j].twp;
253 theseven 299
                nand_type[i] = j;
300
                break;
301
            }
302
        }
303
    }
304
    return 0;
305
}