Subversion Repositories freemyipod

Rev

Go to most recent revision | Details | 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
 
96
static void nand_wait_cmddone(void)
97
{
98
    while ((FMCSTAT & FMCSTAT_CMDDONE) == 0);
99
    FMCSTAT = FMCSTAT_CMDDONE;
100
}
101
 
102
static void nand_wait_addrdone(void)
103
{
104
    while ((FMCSTAT & FMCSTAT_ADDRDONE) == 0);
105
    FMCSTAT = FMCSTAT_ADDRDONE;
106
}
107
 
108
static void nand_wait_chip_ready(uint32_t bank)
109
{
110
    while ((FMCSTAT & (FMCSTAT_BANK0READY << bank)) == 0);
111
    FMCSTAT = (FMCSTAT_BANK0READY << bank);
112
}
113
 
114
static void nand_set_fmctrl0(uint32_t bank, uint32_t flags)
115
{
116
    FMCTRL0 = (nand_tunk1 << 16) | (nand_twp << 12)
117
            | (1 << 11) | 1 | (1 << (bank + 1)) | flags;
118
}
119
 
120
static uint32_t nand_send_cmd(uint32_t cmd)
121
{
122
    FMCMD = cmd;
123
    return nand_wait_rbbdone();
124
}
125
 
126
static void nand_send_address(uint32_t page, uint32_t offset)
127
{
128
    FMANUM = 4;
129
    FMADDR0 = (page << 16) | offset;
130
    FMADDR1 = (page >> 16) & 0xFF;
131
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
132
    nand_wait_cmddone();
133
}
134
 
135
uint32_t nand_reset(uint32_t bank)
136
{
137
    nand_set_fmctrl0(bank, 0);
138
    if (nand_send_cmd(NAND_CMD_RESET)) return 1;
139
    nand_wait_chip_ready(bank);
140
    FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
141
    sleep(1000);
142
    return 0;
143
}
144
 
145
static uint32_t nand_wait_status_ready(uint32_t bank)
146
{
147
    nand_set_fmctrl0(bank, 0);
148
    if ((FMCSTAT & (FMCSTAT_BANK0READY << bank)))
149
        FMCSTAT = (FMCSTAT_BANK0READY << bank);
150
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
151
    nand_send_cmd(NAND_CMD_GET_STATUS);
152
    while (1)
153
    {
154
        FMDNUM = 0;
155
        FMCTRL1 = FMCTRL1_DOREADDATA;
156
        nand_wait_addrdone();
157
        if (FMFIFO & NAND_STATUS_READY) break;
158
        FMCTRL1 = FMCTRL1_CLEARRFIFO;
159
    }
160
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
161
    nand_send_cmd(NAND_CMD_READ);
162
}
163
 
164
static void nand_transfer_data(uint32_t bank, uint32_t direction, void* buffer, uint32_t size)
165
{
166
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
167
    FMDNUM = size - 1;
168
    FMCTRL1 = FMCTRL1_DOREADDATA << direction;
169
    DMACON3 = (2 << DMACON_DEVICE_SHIFT)
170
            | (direction << DMACON_DIRECTION_SHIFT)
171
            | (2 << DMACON_DATA_SIZE_SHIFT)
172
            | (3 << DMACON_BURST_LEN_SHIFT);
173
    while ((DMAALLST & DMAALLST_CHAN3_MASK))
174
        DMACOM3 = DMACOM_CLEARBOTHDONE;
175
    DMABASE3 = (uint32_t)buffer;
176
    DMATCNT3 = (size >> 4) - 1;
177
    DMACOM3 = 4;
178
    while (DMAALLST & DMAALLST_DMABUSY3);
179
    nand_wait_addrdone();
180
    if (!direction) FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO;
181
    else FMCTRL1 = FMCTRL1_CLEARRFIFO;
182
}
183
 
184
static uint32_t ecc_decode(uint32_t size, void* databuffer, void* sparebuffer)
185
{
186
    ECC_INT_CLR = 1;
187
    SRCPND = INTMSK_ECC;
188
    ECC_UNK1 = size;
189
    ECC_DATA_PTR = (uint32_t)databuffer;
190
    ECC_SPARE_PTR = (uint32_t)sparebuffer;
191
    ECC_CTRL = ECCCTRL_STARTDECODING;
192
    while (!(SRCPND & INTMSK_ECC));
193
    ECC_INT_CLR = 1;
194
    SRCPND = INTMSK_ECC;
195
    return ECC_RESULT;
196
}
197
 
198
uint32_t nand_check_empty(uint8_t* buffer)
199
{
200
    uint32_t i, count;
201
    count = 0;
202
    for (i = 0; i < 0x40; i++) if (buffer[i] != 0xFF) count++;
203
    if (count < 2) return 1;
204
    return 0;
205
}
206
 
207
uint32_t nand_get_chip_type(uint32_t bank)
208
{
209
    uint32_t result;
210
    if (nand_reset(bank)) return 0xFFFFFFFF;
211
    if (nand_send_cmd(0x90)) return 0xFFFFFFFF;
212
    FMANUM = 0;
213
    FMADDR0 = 0;
214
    FMCTRL1 = FMCTRL1_DOTRANSADDR;
215
    nand_wait_cmddone();
216
    FMDNUM = 4;
217
    FMCTRL1 = FMCTRL1_DOREADDATA;
218
    nand_wait_addrdone();
219
    result = FMFIFO;
220
    FMCTRL1 = FMCTRL1_CLEARRFIFO;
221
    return result;
222
}
223
 
224
uint32_t nand_read_page(uint32_t bank, uint32_t page, void* databuffer,
225
                        void* sparebuffer, uint32_t checkempty)
226
{
227
    uint32_t rc, eccresult;
228
    uint8_t* data = (uint8_t*)databuffer;
229
    uint8_t* spare = (uint8_t*)sparebuffer;
230
    nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA);
231
    nand_send_cmd(NAND_CMD_READ);
232
    nand_send_address(page, 0);
233
    nand_send_cmd(NAND_CMD_READ2);
234
    nand_wait_status_ready(bank);
235
    nand_transfer_data(bank, 0, data, 0x800);
236
    nand_transfer_data(bank, 0, spare, 0x40);
237
    memcpy(nand_ecc, &spare[0xC], 0x28);
238
    rc = (ecc_decode(3, data, nand_ecc) & 0xF) << 4;
239
    memset(nand_ctrl, 0xFF, 0x200);
240
    memcpy(nand_ctrl, spare, 0xC);
241
    memcpy(nand_ecc, &spare[0x34], 0xC);
242
    eccresult = ecc_decode(0, nand_ctrl, nand_ecc);
243
    rc |= (eccresult & 0xF) << 8;
244
    if (eccresult & 1) memset(spare, 0xFF, 0xC);
245
    else memcpy(spare, nand_ctrl, 0xC);
246
    if (checkempty) rc |= nand_check_empty(spare) << 1;
247
    return rc;
248
}
249
 
250
const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
251
{
252
    if (nand_type[bank] < 0)
253
        return (struct nand_device_info_type*)0;
254
    return &nand_deviceinfotable[nand_type[bank]];
255
}
256
 
257
uint32_t nand_init()
258
{
259
    uint32_t type;
260
    uint32_t i, j;
261
    PWRCONEXT &= ~0x40;
262
    PWRCON &= ~0x100000;
263
    PCON2 = 0x33333333;
264
    PDAT2 = 0;
265
    PCON3 = 0x11113333;
266
    PDAT3 = 0;
267
    PCON4 = 0x33333333;
268
    PDAT4 = 0;
269
    PCON5 = (PCON5 & ~0xF) | 3;
270
    PUNK5 = 1;
271
    sleep(10000);
272
    for (i = 0; i < 4; i++) nand_type[i] = -1;
273
    for (i = 0; i < 4; i++)
274
    {
275
        nand_tunk1 = 7;
276
        nand_twp = 7;
277
        type = nand_get_chip_type(i);
278
        if (type >= 0xFFFFFFF0)
279
        {
280
            nand_type[i] = (int)type;
281
            continue;
282
        }
283
        for (j = 0; ; j++)
284
        {
285
            if (j == NAND_DEVICEINFOTABLE_ENTRIES) break;
286
            else if (nand_deviceinfotable[j].id == type)
287
            {
288
                nand_type[i] = j;
289
                break;
290
            }
291
        }
292
    }
293
    nand_tunk1 = nand_deviceinfotable[nand_type[0]].tunk1;
294
    nand_twp = nand_deviceinfotable[nand_type[0]].twp;
295
    return 0;
296
}