diff options
Diffstat (limited to 'firmware/target/arm/s5l8700/ipodnano2g/nand-nano2g.c')
-rw-r--r-- | firmware/target/arm/s5l8700/ipodnano2g/nand-nano2g.c | 399 |
1 files changed, 399 insertions, 0 deletions
diff --git a/firmware/target/arm/s5l8700/ipodnano2g/nand-nano2g.c b/firmware/target/arm/s5l8700/ipodnano2g/nand-nano2g.c new file mode 100644 index 0000000000..ba83ab6df2 --- /dev/null +++ b/firmware/target/arm/s5l8700/ipodnano2g/nand-nano2g.c | |||
@@ -0,0 +1,399 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2009 by Michael Sparmann | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | |||
23 | #include "config.h" | ||
24 | #include "system.h" | ||
25 | #include "cpu.h" | ||
26 | #include "inttypes.h" | ||
27 | #include "nand-target.h" | ||
28 | #include <string.h> | ||
29 | |||
30 | |||
31 | #define NAND_CMD_READ 0x00 | ||
32 | #define NAND_CMD_PROGCNFRM 0x10 | ||
33 | #define NAND_CMD_READ2 0x30 | ||
34 | #define NAND_CMD_BLOCKERASE 0x60 | ||
35 | #define NAND_CMD_GET_STATUS 0x70 | ||
36 | #define NAND_CMD_PROGRAM 0x80 | ||
37 | #define NAND_CMD_ERASECNFRM 0xD0 | ||
38 | #define NAND_CMD_RESET 0xFF | ||
39 | |||
40 | #define NAND_STATUS_READY 0x40 | ||
41 | |||
42 | #define NAND_DEVICEINFOTABLE_ENTRIES 33 | ||
43 | |||
44 | static const struct nand_device_info_type nand_deviceinfotable[] = | ||
45 | { | ||
46 | {0x1580F1EC, 1024, 968, 0x40, 6, 2, 1, 2, 1}, | ||
47 | {0x1580DAEC, 2048, 1936, 0x40, 6, 2, 1, 2, 1}, | ||
48 | {0x15C1DAEC, 2048, 1936, 0x40, 6, 2, 1, 2, 1}, | ||
49 | {0x1510DCEC, 4096, 3872, 0x40, 6, 2, 1, 2, 1}, | ||
50 | {0x95C1DCEC, 4096, 3872, 0x40, 6, 2, 1, 2, 1}, | ||
51 | {0x2514DCEC, 2048, 1936, 0x80, 7, 2, 1, 2, 1}, | ||
52 | {0x2514D3EC, 4096, 3872, 0x80, 7, 2, 1, 2, 1}, | ||
53 | {0x2555D3EC, 4096, 3872, 0x80, 7, 2, 1, 2, 1}, | ||
54 | {0x2555D5EC, 8192, 7744, 0x80, 7, 2, 1, 2, 1}, | ||
55 | {0x2585D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2}, | ||
56 | {0x9580DCAD, 4096, 3872, 0x40, 6, 3, 2, 3, 2}, | ||
57 | {0xA514D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2}, | ||
58 | {0xA550D3AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2}, | ||
59 | {0xA560D5AD, 4096, 3872, 0x80, 7, 3, 2, 3, 2}, | ||
60 | {0xA555D5AD, 8192, 7744, 0x80, 7, 3, 2, 3, 2}, | ||
61 | {0xA585D598, 8320, 7744, 0x80, 7, 3, 1, 2, 1}, | ||
62 | {0xA584D398, 4160, 3872, 0x80, 7, 3, 1, 2, 1}, | ||
63 | {0x95D1D32C, 8192, 7744, 0x40, 6, 2, 1, 2, 1}, | ||
64 | {0x1580DC2C, 4096, 3872, 0x40, 6, 2, 1, 2, 1}, | ||
65 | {0x15C1D32C, 8192, 7744, 0x40, 6, 2, 1, 2, 1}, | ||
66 | {0x9590DC2C, 4096, 3872, 0x40, 6, 2, 1, 2, 1}, | ||
67 | {0xA594D32C, 4096, 3872, 0x80, 7, 2, 1, 2, 1}, | ||
68 | {0x2584DC2C, 2048, 1936, 0x80, 7, 2, 1, 2, 1}, | ||
69 | {0xA5D5D52C, 8192, 7744, 0x80, 7, 3, 2, 2, 1}, | ||
70 | {0x95D1D389, 8192, 7744, 0x40, 6, 2, 1, 2, 1}, | ||
71 | {0x1580DC89, 4096, 3872, 0x40, 6, 2, 1, 2, 1}, | ||
72 | {0x15C1D389, 8192, 7744, 0x40, 6, 2, 1, 2, 1}, | ||
73 | {0x9590DC89, 4096, 3872, 0x40, 6, 2, 1, 2, 1}, | ||
74 | {0xA594D389, 4096, 3872, 0x80, 7, 2, 1, 2, 1}, | ||
75 | {0x2584DC89, 2048, 1936, 0x80, 7, 2, 1, 2, 1}, | ||
76 | {0xA5D5D589, 8192, 7744, 0x80, 7, 2, 1, 2, 1}, | ||
77 | {0xA514D320, 4096, 3872, 0x80, 7, 2, 1, 2, 1}, | ||
78 | {0xA555D520, 8192, 3872, 0x80, 7, 2, 1, 2, 1} | ||
79 | }; | ||
80 | |||
81 | uint8_t nand_tunk1[4]; | ||
82 | uint8_t nand_twp[4]; | ||
83 | uint8_t nand_tunk2[4]; | ||
84 | uint8_t nand_tunk3[4]; | ||
85 | uint32_t nand_type[4]; | ||
86 | |||
87 | static uint8_t nand_aligned_data[0x800] __attribute__((aligned(32))); | ||
88 | static uint8_t nand_aligned_ctrl[0x200] __attribute__((aligned(32))); | ||
89 | static uint8_t nand_aligned_spare[0x40] __attribute__((aligned(32))); | ||
90 | static uint8_t nand_aligned_ecc[0x28] __attribute__((aligned(32))); | ||
91 | #define nand_uncached_data \ | ||
92 | ((uint8_t*)(((uint32_t)nand_aligned_data) | 0x40000000)) | ||
93 | #define nand_uncached_ctrl \ | ||
94 | ((uint8_t*)(((uint32_t)nand_aligned_ctrl) | 0x40000000)) | ||
95 | #define nand_uncached_spare \ | ||
96 | ((uint8_t*)(((uint32_t)nand_aligned_spare) | 0x40000000)) | ||
97 | #define nand_uncached_ecc \ | ||
98 | ((uint8_t*)(((uint32_t)nand_aligned_ecc) | 0x40000000)) | ||
99 | |||
100 | |||
101 | uint32_t nand_wait_rbbdone(void) | ||
102 | { | ||
103 | uint32_t timeout = 0x40000; | ||
104 | while ((FMCSTAT & FMCSTAT_RBBDONE) == 0) if (timeout-- == 0) return 1; | ||
105 | FMCSTAT = FMCSTAT_RBBDONE; | ||
106 | return 0; | ||
107 | } | ||
108 | |||
109 | uint32_t nand_wait_cmddone(void) | ||
110 | { | ||
111 | uint32_t timeout = 0x40000; | ||
112 | while ((FMCSTAT & FMCSTAT_CMDDONE) == 0) if (timeout-- == 0) return 1; | ||
113 | FMCSTAT = FMCSTAT_CMDDONE; | ||
114 | return 0; | ||
115 | } | ||
116 | |||
117 | uint32_t nand_wait_addrdone(void) | ||
118 | { | ||
119 | uint32_t timeout = 0x40000; | ||
120 | while ((FMCSTAT & FMCSTAT_ADDRDONE) == 0) if (timeout-- == 0) return 1; | ||
121 | FMCSTAT = FMCSTAT_ADDRDONE; | ||
122 | return 0; | ||
123 | } | ||
124 | |||
125 | uint32_t nand_wait_chip_ready(uint32_t bank) | ||
126 | { | ||
127 | uint32_t timeout = 0x40000; | ||
128 | while ((FMCSTAT & (FMCSTAT_BANK0READY << bank)) == 0) | ||
129 | if (timeout-- == 0) return 1; | ||
130 | FMCSTAT = (FMCSTAT_BANK0READY << bank); | ||
131 | return 0; | ||
132 | } | ||
133 | |||
134 | void nand_set_fmctrl0(uint32_t bank, uint32_t flags) | ||
135 | { | ||
136 | FMCTRL0 = (nand_tunk1[bank] << 16) | (nand_twp[bank] << 12) | ||
137 | | (1 << 11) | 1 | (1 << (bank + 1)) | flags; | ||
138 | } | ||
139 | |||
140 | uint32_t nand_send_cmd(uint32_t cmd) | ||
141 | { | ||
142 | FMCMD = cmd; | ||
143 | return nand_wait_rbbdone(); | ||
144 | } | ||
145 | |||
146 | uint32_t nand_send_address(uint32_t page, uint32_t offset) | ||
147 | { | ||
148 | FMANUM = 4; | ||
149 | FMADDR0 = (page << 16) | offset; | ||
150 | FMADDR1 = (page >> 16) & 0xFF; | ||
151 | FMCTRL1 = FMCTRL1_DOTRANSADDR; | ||
152 | return nand_wait_cmddone(); | ||
153 | } | ||
154 | |||
155 | uint32_t nand_reset(uint32_t bank) | ||
156 | { | ||
157 | nand_set_fmctrl0(bank, 0); | ||
158 | if (nand_send_cmd(NAND_CMD_RESET) != 0) return 1; | ||
159 | if (nand_wait_chip_ready(bank) != 0) return 1; | ||
160 | FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO; | ||
161 | return 0; | ||
162 | } | ||
163 | |||
164 | uint32_t nand_wait_status_ready(uint32_t bank) | ||
165 | { | ||
166 | uint32_t timeout = 0x4000; | ||
167 | nand_set_fmctrl0(bank, 0); | ||
168 | if ((FMCSTAT & (FMCSTAT_BANK0READY << bank)) != 0) | ||
169 | FMCSTAT = (FMCSTAT_BANK0READY << bank); | ||
170 | FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO; | ||
171 | if (nand_send_cmd(NAND_CMD_GET_STATUS) != 0) return 1; | ||
172 | while (1) | ||
173 | { | ||
174 | if (timeout-- == 0) return 1; | ||
175 | FMDNUM = 0; | ||
176 | FMCTRL1 = FMCTRL1_DOREADDATA; | ||
177 | if (nand_wait_addrdone() != 0) return 1; | ||
178 | if ((FMFIFO & NAND_STATUS_READY) != 0) break; | ||
179 | FMCTRL1 = FMCTRL1_CLEARRFIFO; | ||
180 | } | ||
181 | FMCTRL1 = FMCTRL1_CLEARRFIFO; | ||
182 | return nand_send_cmd(NAND_CMD_READ); | ||
183 | } | ||
184 | |||
185 | uint32_t nand_transfer_data(uint32_t bank, uint32_t direction, | ||
186 | void* buffer, uint32_t size) | ||
187 | { | ||
188 | uint32_t timeout = 0x40000; | ||
189 | nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA); | ||
190 | FMDNUM = size - 1; | ||
191 | FMCTRL1 = FMCTRL1_DOREADDATA << direction; | ||
192 | DMACON3 = (2 << DMACON_DEVICE_SHIFT) | ||
193 | | (direction << DMACON_DIRECTION_SHIFT) | ||
194 | | (2 << DMACON_DATA_SIZE_SHIFT) | ||
195 | | (3 << DMACON_BURST_LEN_SHIFT); | ||
196 | while ((DMAALLST & DMAALLST_CHAN3_MASK) != 0) | ||
197 | DMACOM3 = DMACOM_CLEARBOTHDONE; | ||
198 | DMABASE3 = (uint32_t)buffer; | ||
199 | DMATCNT3 = (size >> 4) - 1; | ||
200 | DMACOM3 = 4; | ||
201 | while ((DMAALLST & DMAALLST_DMABUSY3) != 0) | ||
202 | if (timeout-- == 0) return 1; | ||
203 | if (nand_wait_addrdone() != 0) return 1; | ||
204 | if (direction == 0) FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO; | ||
205 | return 0; | ||
206 | } | ||
207 | |||
208 | uint32_t ecc_decode(uint32_t size, void* databuffer, void* sparebuffer) | ||
209 | { | ||
210 | uint32_t timeout = 0x40000; | ||
211 | ECC_INT_CLR = 1; | ||
212 | SRCPND = INTMSK_ECC; | ||
213 | ECC_UNK1 = size; | ||
214 | ECC_DATA_PTR = (uint32_t)databuffer; | ||
215 | ECC_SPARE_PTR = (uint32_t)sparebuffer; | ||
216 | ECC_CTRL = ECCCTRL_STARTDECODING; | ||
217 | while ((SRCPND & INTMSK_ECC) == 0) if (timeout-- == 0) return 1; | ||
218 | ECC_INT_CLR = 1; | ||
219 | SRCPND = INTMSK_ECC; | ||
220 | return ECC_RESULT; | ||
221 | } | ||
222 | |||
223 | uint32_t ecc_encode(uint32_t size, void* databuffer, void* sparebuffer) | ||
224 | { | ||
225 | uint32_t timeout = 0x40000; | ||
226 | ECC_INT_CLR = 1; | ||
227 | SRCPND = INTMSK_ECC; | ||
228 | ECC_UNK1 = size; | ||
229 | ECC_DATA_PTR = (uint32_t)databuffer; | ||
230 | ECC_SPARE_PTR = (uint32_t)sparebuffer; | ||
231 | ECC_CTRL = ECCCTRL_STARTENCODING; | ||
232 | while ((SRCPND & INTMSK_ECC) == 0) if (timeout-- == 0) return 1; | ||
233 | ECC_INT_CLR = 1; | ||
234 | SRCPND = INTMSK_ECC; | ||
235 | return 0; | ||
236 | } | ||
237 | |||
238 | uint32_t nand_check_empty(uint8_t* buffer) | ||
239 | { | ||
240 | uint32_t i, count; | ||
241 | count = 0; | ||
242 | for (i = 0; i < 0x40; i++) if (buffer[i] != 0xFF) count++; | ||
243 | if (count < 2) return 1; | ||
244 | return 0; | ||
245 | } | ||
246 | |||
247 | uint32_t nand_get_chip_type(uint32_t bank) | ||
248 | { | ||
249 | uint32_t result; | ||
250 | if (nand_reset(bank) != 0) return 0xFFFFFFFF; | ||
251 | if (nand_send_cmd(0x90) != 0) return 0xFFFFFFFF; | ||
252 | FMANUM = 0; | ||
253 | FMADDR0 = 0; | ||
254 | FMCTRL1 = FMCTRL1_DOTRANSADDR; | ||
255 | if (nand_wait_cmddone() != 0) return 0xFFFFFFFF; | ||
256 | FMDNUM = 4; | ||
257 | FMCTRL1 = FMCTRL1_DOREADDATA; | ||
258 | if (nand_wait_addrdone() != 0) return 0xFFFFFFFF; | ||
259 | result = FMFIFO; | ||
260 | FMCTRL1 = FMCTRL1_CLEARRFIFO | FMCTRL1_CLEARWFIFO; | ||
261 | return result; | ||
262 | } | ||
263 | |||
264 | uint32_t nand_read_page(uint32_t bank, uint32_t page, void* databuffer, | ||
265 | void* sparebuffer, uint32_t doecc, | ||
266 | uint32_t checkempty) | ||
267 | { | ||
268 | uint32_t rc, eccresult; | ||
269 | nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA); | ||
270 | if (nand_send_cmd(NAND_CMD_READ) != 0) return 1; | ||
271 | if (nand_send_address(page, (databuffer == 0) ? 0x800 : 0) != 0) | ||
272 | return 1; | ||
273 | if (nand_send_cmd(NAND_CMD_READ2) != 0) return 1; | ||
274 | if (nand_wait_status_ready(bank) != 0) return 1; | ||
275 | if (databuffer != 0) | ||
276 | if (nand_transfer_data(bank, 0, nand_uncached_data, 0x800) != 0) | ||
277 | return 1; | ||
278 | if (doecc == 0) | ||
279 | { | ||
280 | memcpy(databuffer, nand_uncached_data, 0x800); | ||
281 | if (sparebuffer != 0) | ||
282 | { | ||
283 | if (nand_transfer_data(bank, 0, nand_uncached_spare, 0x40) != 0) | ||
284 | return 1; | ||
285 | memcpy(sparebuffer, nand_uncached_spare, 0x800); | ||
286 | if (checkempty != 0) | ||
287 | return nand_check_empty((uint8_t*)sparebuffer) << 1; | ||
288 | } | ||
289 | return 0; | ||
290 | } | ||
291 | rc = 0; | ||
292 | if (nand_transfer_data(bank, 0, nand_uncached_spare, 0x40) != 0) | ||
293 | return 1; | ||
294 | memcpy(nand_uncached_ecc, &nand_uncached_spare[0xC], 0x28); | ||
295 | rc |= (ecc_decode(3, nand_uncached_data, nand_uncached_ecc) & 0xF) << 4; | ||
296 | if (databuffer != 0) memcpy(databuffer, nand_uncached_data, 0x800); | ||
297 | memset(nand_uncached_ctrl, 0xFF, 0x200); | ||
298 | memcpy(nand_uncached_ctrl, nand_uncached_spare, 0xC); | ||
299 | memcpy(nand_uncached_ecc, &nand_uncached_spare[0x34], 0xC); | ||
300 | eccresult = ecc_decode(0, nand_uncached_ctrl, nand_uncached_ecc); | ||
301 | rc |= (eccresult & 0xF) << 8; | ||
302 | if (sparebuffer != 0) | ||
303 | { | ||
304 | memcpy(sparebuffer, nand_uncached_spare, 0x40); | ||
305 | if ((eccresult & 1) != 0) memset(sparebuffer, 0xFF, 0xC); | ||
306 | else memcpy(sparebuffer, nand_uncached_ctrl, 0xC); | ||
307 | } | ||
308 | if (checkempty != 0) rc |= nand_check_empty(nand_uncached_spare) << 1; | ||
309 | |||
310 | return rc; | ||
311 | } | ||
312 | |||
313 | uint32_t nand_write_page(uint32_t bank, uint32_t page, void* databuffer, | ||
314 | void* sparebuffer, uint32_t doecc) | ||
315 | { | ||
316 | if (sparebuffer != 0) memcpy(nand_uncached_spare, sparebuffer, 0x40); | ||
317 | else memset(nand_uncached_spare, 0xFF, 0x40); | ||
318 | if (doecc != 0) | ||
319 | { | ||
320 | memcpy(nand_uncached_data, databuffer, 0x800); | ||
321 | if (ecc_encode(3, nand_uncached_data, nand_uncached_ecc) != 0) | ||
322 | return 1; | ||
323 | memcpy(&nand_uncached_spare[0xC], nand_uncached_ecc, 0x28); | ||
324 | memset(nand_uncached_ctrl, 0xFF, 0x200); | ||
325 | memcpy(nand_uncached_ctrl, nand_uncached_spare, 0xC); | ||
326 | if (ecc_encode(0, nand_uncached_ctrl, nand_uncached_ecc) != 0) | ||
327 | return 1; | ||
328 | memcpy(&nand_uncached_spare[0x34], nand_uncached_ecc, 0xC); | ||
329 | } | ||
330 | nand_set_fmctrl0(bank, FMCTRL0_ENABLEDMA); | ||
331 | if (nand_send_cmd(NAND_CMD_PROGRAM) != 0) | ||
332 | return 1; | ||
333 | if (nand_send_address(page, (databuffer == 0) ? 0x800 : 0) != 0) | ||
334 | return 1; | ||
335 | if (databuffer != 0) | ||
336 | if (nand_transfer_data(bank, 1, nand_uncached_data, 0x800) != 0) | ||
337 | return 1; | ||
338 | if (sparebuffer != 0 || doecc != 0) | ||
339 | if (nand_transfer_data(bank, 1, nand_uncached_spare, 0x40) != 0) | ||
340 | return 1; | ||
341 | if (nand_send_cmd(NAND_CMD_PROGCNFRM) != 0) return 1; | ||
342 | return nand_wait_status_ready(bank); | ||
343 | } | ||
344 | |||
345 | uint32_t nand_block_erase(uint32_t bank, uint32_t page) | ||
346 | { | ||
347 | nand_set_fmctrl0(bank, 0); | ||
348 | if (nand_send_cmd(NAND_CMD_BLOCKERASE) != 0) return 1; | ||
349 | FMANUM = 2; | ||
350 | FMADDR0 = page; | ||
351 | FMCTRL1 = FMCTRL1_DOTRANSADDR; | ||
352 | if (nand_wait_cmddone() != 0) return 1; | ||
353 | if (nand_send_cmd(NAND_CMD_ERASECNFRM) != 0) return 1; | ||
354 | return nand_wait_status_ready(bank); | ||
355 | } | ||
356 | |||
357 | const struct nand_device_info_type* nand_get_device_type(uint32_t bank) | ||
358 | { | ||
359 | if (nand_type[bank] == 0xFFFFFFFF) | ||
360 | return (struct nand_device_info_type*)0; | ||
361 | return &nand_deviceinfotable[nand_type[bank]]; | ||
362 | } | ||
363 | |||
364 | uint32_t nand_device_init(void) | ||
365 | { | ||
366 | uint32_t type; | ||
367 | uint32_t i, j; | ||
368 | PCON2 = 0x33333333; | ||
369 | PDAT2 = 0; | ||
370 | PCON3 = 0x11113333; | ||
371 | PDAT3 = 0; | ||
372 | PCON4 = 0x33333333; | ||
373 | PDAT4 = 0; | ||
374 | for (i = 0; i < 4; i++) | ||
375 | { | ||
376 | nand_tunk1[i] = 7; | ||
377 | nand_twp[i] = 7; | ||
378 | nand_tunk2[i] = 7; | ||
379 | nand_tunk3[i] = 7; | ||
380 | type = nand_get_chip_type(i); | ||
381 | nand_type[i] = 0xFFFFFFFF; | ||
382 | if (type == 0xFFFFFFFF) continue; | ||
383 | for (j = 0; ; j++) | ||
384 | { | ||
385 | if (j == ARRAYLEN(nand_deviceinfotable)) break; | ||
386 | else if (nand_deviceinfotable[j].id == type) | ||
387 | { | ||
388 | nand_type[i] = j; | ||
389 | break; | ||
390 | } | ||
391 | } | ||
392 | nand_tunk1[i] = nand_deviceinfotable[nand_type[i]].tunk1; | ||
393 | nand_twp[i] = nand_deviceinfotable[nand_type[i]].twp; | ||
394 | nand_tunk2[i] = nand_deviceinfotable[nand_type[i]].tunk2; | ||
395 | nand_tunk3[i] = nand_deviceinfotable[nand_type[i]].tunk3; | ||
396 | } | ||
397 | if (nand_type[0] == 0xFFFFFFFF) return 1; | ||
398 | return 0; | ||
399 | } | ||