1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Maurus Cuelenaere
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.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
31 /*#define LOGF_ENABLE*/
38 * Standard NAND flash commands
40 #define NAND_CMD_READ0 0
41 #define NAND_CMD_READ1 1
42 #define NAND_CMD_RNDOUT 5
43 #define NAND_CMD_PAGEPROG 0x10
44 #define NAND_CMD_READOOB 0x50
45 #define NAND_CMD_ERASE1 0x60
46 #define NAND_CMD_STATUS 0x70
47 #define NAND_CMD_STATUS_MULTI 0x71
48 #define NAND_CMD_SEQIN 0x80
49 #define NAND_CMD_RNDIN 0x85
50 #define NAND_CMD_READID 0x90
51 #define NAND_CMD_ERASE2 0xd0
52 #define NAND_CMD_RESET 0xff
54 /* Extended commands for large page devices */
55 #define NAND_CMD_READSTART 0x30
56 #define NAND_CMD_RNDOUTSTART 0xE0
57 #define NAND_CMD_CACHEDPROG 0x15
60 #define NAND_STATUS_FAIL 0x01
61 #define NAND_STATUS_FAIL_N1 0x02
62 #define NAND_STATUS_TRUE_READY 0x20
63 #define NAND_STATUS_READY 0x40
64 #define NAND_STATUS_WP 0x80
67 * NAND parameter struct
71 unsigned int bus_width
; /* data bus width: 8-bit/16-bit */
72 unsigned int row_cycle
; /* row address cycles: 2/3 */
73 unsigned int page_size
; /* page size in bytes: 512/2048/4096 */
74 unsigned int oob_size
; /* oob size in bytes: 16/64/128 */
75 unsigned int page_per_block
; /* pages per block: 32/64/128 */
81 * NAND read routine for JZ4740
83 * Copyright (c) 2005-2008 Ingenic Semiconductor Inc.
87 static volatile unsigned long nand_address
;
88 #define NAND_DATAPORT (nand_address)
89 #define NAND_ADDRPORT (nand_address+0x10000)
90 #define NAND_COMMPORT (nand_address+0x08000)
96 #define __nand_cmd(n) (REG8(NAND_COMMPORT) = (n))
97 #define __nand_addr(n) (REG8(NAND_ADDRPORT) = (n))
98 #define __nand_data8() (REG8(NAND_DATAPORT))
99 #define __nand_data16() (REG16(NAND_DATAPORT))
101 #define __nand_ecc_rs_encoding() \
102 (REG_EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_ERST | EMC_NFECR_RS | EMC_NFECR_RS_ENCODING)
103 #define __nand_ecc_rs_decoding() \
104 (REG_EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_ERST | EMC_NFECR_RS | EMC_NFECR_RS_DECODING)
105 #define __nand_ecc_disable() (REG_EMC_NFECR &= ~EMC_NFECR_ECCE)
106 #define __nand_ecc_encode_sync() while (!(REG_EMC_NFINTS & EMC_NFINTS_ENCF))
107 #define __nand_ecc_decode_sync() while (!(REG_EMC_NFINTS & EMC_NFINTS_DECF))
109 /*--------------------------------------------------------------*/
111 static struct nand_info
* chip_info
= NULL
;
112 static struct nand_info
* banks
[4];
113 static unsigned int nr_banks
= 1;
114 static unsigned long bank_size
;
115 static struct nand_param internal_param
;
116 static struct mutex nand_mtx
;
118 static struct mutex nand_dma_mtx
;
119 static struct semaphore nand_dma_complete
;
121 static unsigned char temp_page
[4096]; /* Max page size */
123 static inline void jz_nand_wait_ready(void)
125 register unsigned int timeout
= 1000;
126 while ((REG_GPIO_PXPIN(2) & 0x40000000) && timeout
--);
127 while (!(REG_GPIO_PXPIN(2) & 0x40000000));
131 static inline void jz_nand_read_buf16(void *buf
, int count
)
134 register unsigned short *p
= (unsigned short *)buf
;
136 for (i
= 0; i
< count
; i
+= 2)
137 *p
++ = __nand_data16();
140 static inline void jz_nand_read_buf8(void *buf
, int count
)
143 register unsigned char *p
= (unsigned char *)buf
;
145 for (i
= 0; i
< count
; i
++)
146 *p
++ = __nand_data8();
149 static void jz_nand_write_dma(void *source
, unsigned int len
, int bw
)
151 mutex_lock(&nand_dma_mtx
);
153 if(((unsigned int)source
< 0xa0000000) && len
)
154 dma_cache_wback_inv((unsigned long)source
, len
);
158 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) = DMAC_DCCSR_NDES
;
159 REG_DMAC_DSAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)source
);
160 REG_DMAC_DTAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)NAND_DATAPORT
);
161 REG_DMAC_DTCR(DMA_NAND_CHANNEL
) = len
/ 16;
162 REG_DMAC_DRSR(DMA_NAND_CHANNEL
) = DMAC_DRSR_RS_AUTO
;
163 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) = (DMAC_DCMD_SAI
| DMAC_DCMD_DAI
| DMAC_DCMD_SWDH_32
| DMAC_DCMD_DS_16BYTE
|
164 (bw
== 8 ? DMAC_DCMD_DWDH_8
: DMAC_DCMD_DWDH_16
));
166 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) |= DMAC_DCCSR_EN
; /* Enable DMA channel */
168 while( REG_DMAC_DTCR(DMA_NAND_CHANNEL
) )
171 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) |= DMAC_DCMD_TIE
; /* Enable DMA interrupt */
172 semaphore_wait(&nand_dma_complete
, TIMEOUT_BLOCK
);
175 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_EN
; /* Disable DMA channel */
179 mutex_unlock(&nand_dma_mtx
);
182 static void jz_nand_read_dma(void *target
, unsigned int len
, int bw
)
184 mutex_lock(&nand_dma_mtx
);
186 if(((unsigned int)target
< 0xa0000000) && len
)
187 dma_cache_wback_inv((unsigned long)target
, len
);
191 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) = DMAC_DCCSR_NDES
;
192 REG_DMAC_DSAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)NAND_DATAPORT
);
193 REG_DMAC_DTAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)target
);
194 REG_DMAC_DTCR(DMA_NAND_CHANNEL
) = len
/ 4;
195 REG_DMAC_DRSR(DMA_NAND_CHANNEL
) = DMAC_DRSR_RS_AUTO
;
196 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) = (DMAC_DCMD_SAI
| DMAC_DCMD_DAI
| DMAC_DCMD_DWDH_32
| DMAC_DCMD_DS_32BIT
|
197 (bw
== 8 ? DMAC_DCMD_SWDH_8
: DMAC_DCMD_SWDH_16
));
198 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) |= DMAC_DCCSR_EN
; /* Enable DMA channel */
200 while( REG_DMAC_DTCR(DMA_NAND_CHANNEL
) )
203 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) |= DMAC_DCMD_TIE
; /* Enable DMA interrupt */
204 semaphore_wait(&nand_dma_complete
, TIMEOUT_BLOCK
);
207 //REG_DMAC_DCCSR(DMA_NAND_CHANNEL) &= ~DMAC_DCCSR_EN; /* Disable DMA channel */
211 mutex_unlock(&nand_dma_mtx
);
214 void DMA_CALLBACK(DMA_NAND_CHANNEL
)(void)
216 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_HLT
)
217 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_HLT
;
219 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_AR
)
220 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_AR
;
222 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_CT
)
223 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_CT
;
225 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_TT
)
226 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_TT
;
228 semaphore_release(&nand_dma_complete
);
232 static inline void jz_nand_read_buf(void *buf
, int count
, int bw
)
236 jz_nand_read_dma(buf
, count
, 8);
238 jz_nand_read_dma(buf
, count
, 16);
241 jz_nand_read_buf8(buf
, count
);
243 jz_nand_read_buf16(buf
, count
);
249 * Correct 1~9-bit errors in 512-bytes data
251 static void jz_rs_correct(unsigned char *dat
, int idx
, int mask
)
254 unsigned short d
, d1
, dm
;
259 i
= (j
== 0) ? (i
- 1) : i
;
260 j
= (j
== 0) ? 7 : (j
- 1);
268 d
= (dat
[i
] << 8) | dat
[i
- 1];
270 d1
= (d
>> j
) & 0x1ff;
274 d
= (d
& dm
) | (d1
<< j
);
276 dat
[i
- 1] = d
& 0xff;
278 dat
[i
] = (d
>> 8) & 0xff;
285 static int jz_nand_read_oob(unsigned long page_addr
, unsigned char *buf
, int size
)
287 struct nand_param
*nandp
= &internal_param
;
288 int page_size
, row_cycle
, bus_width
;
291 page_size
= nandp
->page_size
;
292 row_cycle
= nandp
->row_cycle
;
293 bus_width
= nandp
->bus_width
;
295 if (page_size
>= 2048)
296 col_addr
= page_size
;
300 if (page_size
>= 2048)
301 /* Send READ0 command */
302 __nand_cmd(NAND_CMD_READ0
);
304 /* Send READOOB command */
305 __nand_cmd(NAND_CMD_READOOB
);
307 /* Send column address */
308 __nand_addr(col_addr
& 0xff);
309 if (page_size
>= 2048)
310 __nand_addr((col_addr
>> 8) & 0xff);
312 /* Send page address */
313 __nand_addr(page_addr
& 0xff);
314 __nand_addr((page_addr
>> 8) & 0xff);
316 __nand_addr((page_addr
>> 16) & 0xff);
318 /* Send READSTART command for 2048 ps NAND */
319 if (page_size
>= 2048)
320 __nand_cmd(NAND_CMD_READSTART
);
322 /* Wait for device ready */
323 jz_nand_wait_ready();
326 jz_nand_read_buf(buf
, size
, bus_width
);
337 * block - block number: 0, 1, 2, ...
338 * page - page number within a block: 0, 1, 2, ...
339 * dst - pointer to target buffer
341 static int jz_nand_read_page(unsigned long page_addr
, unsigned char *dst
)
343 struct nand_param
*nandp
= &internal_param
;
344 int page_size
, oob_size
, page_per_block
;
345 int row_cycle
, bus_width
, ecc_count
;
350 unsigned char *data_buf
;
351 unsigned char oob_buf
[nandp
->oob_size
];
353 if(nand_address
== 0)
356 page_size
= nandp
->page_size
;
357 oob_size
= nandp
->oob_size
;
358 page_per_block
= nandp
->page_per_block
;
359 row_cycle
= nandp
->row_cycle
;
360 bus_width
= nandp
->bus_width
;
365 jz_nand_read_oob(page_addr
, oob_buf
, oob_size
);
371 /* Send READ0 command */
372 __nand_cmd(NAND_CMD_READ0
);
374 /* Send column address */
376 if (page_size
>= 2048)
379 /* Send page address */
380 __nand_addr(page_addr
& 0xff);
381 __nand_addr((page_addr
>> 8) & 0xff);
383 __nand_addr((page_addr
>> 16) & 0xff);
385 /* Send READSTART command for 2048 ps NAND */
386 if (page_size
>= 2048)
387 __nand_cmd(NAND_CMD_READSTART
);
389 /* Wait for device ready */
390 jz_nand_wait_ready();
395 ecc_count
= page_size
/ ECC_BLOCK
;
397 for (i
= 0; i
< ecc_count
; i
++)
400 volatile unsigned char *paraddr
= (volatile unsigned char *)EMC_NFPAR0
;
403 /* Enable RS decoding */
404 REG_EMC_NFINTS
= 0x0;
405 __nand_ecc_rs_decoding();
409 jz_nand_read_buf((void *)data_buf
, ECC_BLOCK
, bus_width
);
413 for (j
= 0; j
< PAR_SIZE
; j
++)
414 *paraddr
++ = oob_buf
[ECC_POS
+ i
*PAR_SIZE
+ j
];
417 REG_EMC_NFECR
|= EMC_NFECR_PRDY
;
419 /* Wait for completion */
420 __nand_ecc_decode_sync();
422 /* Disable decoding */
423 __nand_ecc_disable();
425 /* Check result of decoding */
426 stat
= REG_EMC_NFINTS
;
427 if (stat
& EMC_NFINTS_ERR
)
430 if (stat
& EMC_NFINTS_UNCOR
)
432 /* Uncorrectable error occurred */
433 logf("Uncorrectable ECC error at NAND page address 0x%lx", page_addr
);
438 unsigned int errcnt
, index
, mask
;
440 errcnt
= (stat
& EMC_NFINTS_ERRCNT_MASK
) >> EMC_NFINTS_ERRCNT_BIT
;
444 index
= (REG_EMC_NFERR3
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
445 mask
= (REG_EMC_NFERR3
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
446 jz_rs_correct(data_buf
, index
, mask
);
448 index
= (REG_EMC_NFERR2
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
449 mask
= (REG_EMC_NFERR2
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
450 jz_rs_correct(data_buf
, index
, mask
);
452 index
= (REG_EMC_NFERR1
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
453 mask
= (REG_EMC_NFERR1
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
454 jz_rs_correct(data_buf
, index
, mask
);
456 index
= (REG_EMC_NFERR0
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
457 mask
= (REG_EMC_NFERR0
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
458 jz_rs_correct(data_buf
, index
, mask
);
467 data_buf
+= ECC_BLOCK
;
474 * Disable NAND controller
476 static void jz_nand_disable(void)
478 REG_EMC_NFCSR
&= ~(EMC_NFCSR_NFCE1
| EMC_NFCSR_NFCE2
|
479 EMC_NFCSR_NFCE3
| EMC_NFCSR_NFCE4
|
480 EMC_NFCSR_NFE1
| EMC_NFCSR_NFE2
|
481 EMC_NFCSR_NFE3
| EMC_NFCSR_NFE4
);
485 * Enable NAND controller
487 static void jz_nand_enable(void)
491 REG_GPIO_PXFUNS(1) = 0x1E018000; // __gpio_as_func0() start
492 REG_GPIO_PXSELC(1) = 0x1E018000; // __gpio_as_func0() end
494 REG_GPIO_PXFUNS(2) = 0x3000<<16; // __gpio_as_func0() start
495 REG_GPIO_PXSELC(2) = 0x3000<<16; // __gpio_as_func0() end
497 REG_GPIO_PXFUNC(2) = 0x4000<<16; // __gpio_port_as_input() start
498 REG_GPIO_PXSELC(2) = 0x4000<<16;
499 REG_GPIO_PXDIRC(2) = 0x4000<<16; // __gpio_port_as_input() end
500 REG_GPIO_PXPES(2) = 0x4000<<16; // __gpio_disable_pull()
502 REG_GPIO_PXFUNS(1) = 0x40<<16; // __gpio_as_func0() start
503 REG_GPIO_PXSELC(1) = 0x40<<16; // __gpio_as_func0() end
505 REG_EMC_SMCR1
= (REG_EMC_SMCR1
& 0xFF) | 0x4621200;
506 REG_EMC_SMCR2
= (REG_EMC_SMCR2
& 0xFF) | 0x4621200;
507 REG_EMC_SMCR3
= (REG_EMC_SMCR3
& 0xFF) | 0x4621200;
508 REG_EMC_SMCR4
= (REG_EMC_SMCR4
& 0xFF) | 0x4621200;
510 REG_EMC_SMCR1
= REG_EMC_SMCR2
= REG_EMC_SMCR3
= REG_EMC_SMCR4
= 0x6621200;
512 REG_EMC_SMCR1
= REG_EMC_SMCR2
= REG_EMC_SMCR3
= REG_EMC_SMCR4
= 0x04444400;
516 static void jz_nand_select(int bank
)
518 REG_EMC_NFCSR
|= (EMC_NFCSR_NFE(bank
+1) | EMC_NFCSR_NFCE(bank
+1));
523 nand_address
= 0xB8000000;
526 nand_address
= 0xB4000000;
529 nand_address
= 0xAC000000;
532 nand_address
= 0xA8000000;
537 static void jz_nand_deselect(int bank
)
539 REG_EMC_NFCSR
&= ~(EMC_NFCSR_NFE(bank
+1) | EMC_NFCSR_NFCE(bank
+1));
543 static int jz_nand_init(void)
545 unsigned char cData
[5];
554 __nand_cmd(NAND_CMD_READID
);
555 __nand_addr(NAND_CMD_READ0
);
556 cData
[0] = __nand_data8();
557 cData
[1] = __nand_data8();
558 cData
[2] = __nand_data8();
559 cData
[3] = __nand_data8();
560 cData
[4] = __nand_data8();
564 logf("NAND chip %d: 0x%x 0x%x 0x%x 0x%x 0x%x", i
+1, cData
[0], cData
[1],
565 cData
[2], cData
[3], cData
[4]);
567 banks
[i
] = nand_identify(cData
);
572 if(i
== 0 && banks
[i
] == NULL
)
574 panicf("Unknown NAND flash chip: 0x%x 0x%x 0x%x 0x%x 0x%x", cData
[0],
575 cData
[1], cData
[2], cData
[3], cData
[4]);
576 return -1; /* panicf() doesn't return though */
579 chip_info
= banks
[0];
581 internal_param
.bus_width
= 8;
582 internal_param
.row_cycle
= chip_info
->row_cycles
;
583 internal_param
.page_size
= chip_info
->page_size
;
584 internal_param
.oob_size
= chip_info
->spare_size
;
585 internal_param
.page_per_block
= chip_info
->pages_per_block
;
587 bank_size
= chip_info
->page_size
* chip_info
->blocks_per_bank
/ 512 * chip_info
->pages_per_block
;
597 static bool inited
= false;
601 res
= jz_nand_init();
602 mutex_init(&nand_mtx
);
604 mutex_init(&nand_dma_mtx
);
605 semaphore_init(&nand_dma_complete
, 1, 0);
606 system_enable_irq(DMA_IRQ(DMA_NAND_CHANNEL
));
615 static inline int read_sector(unsigned long start
, unsigned int count
,
616 void* buf
, unsigned int chip_size
)
620 if(UNLIKELY(start
% chip_size
== 0 && count
== chip_size
))
621 ret
= jz_nand_read_page(start
/ chip_size
, buf
);
624 ret
= jz_nand_read_page(start
/ chip_size
, temp_page
);
625 memcpy(buf
, temp_page
+ (start
% chip_size
), count
);
631 int nand_read_sectors(IF_MV2(int drive
,) unsigned long start
, int count
, void* buf
)
633 #ifdef HAVE_MULTIVOLUME
637 unsigned int i
, _count
, chip_size
= chip_info
->page_size
;
638 unsigned long _start
;
641 mutex_lock(&nand_mtx
);
646 if(_count
<= chip_size
)
648 jz_nand_select(start
/ bank_size
);
649 ret
= read_sector(_start
, _count
, buf
, chip_size
);
650 jz_nand_deselect(start
/ bank_size
);
654 for(i
=0; i
<_count
&& ret
==0; i
+=chip_size
)
656 jz_nand_select((start
+(i
>>9)) / bank_size
);
658 ret
= read_sector(_start
+i
, (_count
-i
< chip_size
?
659 _count
-i
: chip_size
),
662 jz_nand_deselect((start
+(i
>>9)) / bank_size
);
666 mutex_unlock(&nand_mtx
);
668 logf("nand_read_sectors(%ld, %d, 0x%x): %d", start
, count
, (int)buf
, ret
);
674 int nand_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
, const void* buf
)
679 #ifdef HAVE_MULTIVOLUME
686 #ifdef HAVE_STORAGE_FLUSH
693 void nand_spindown(int seconds
)
699 void nand_sleep(void)
709 void nand_enable(bool on
)
711 /* null - flash controller is enabled/disabled as needed. */
716 long nand_last_disk_activity(void)
721 int nand_spinup_time(void)
726 void nand_sleepnow(void)
730 #ifdef STORAGE_GET_INFO
731 void nand_get_info(IF_MV2(int drive
,) struct storage_info
*info
)
733 #ifdef HAVE_MULTIVOLUME
737 /* firmware version */
738 info
->revision
="0.00";
740 info
->vendor
="Rockbox";
741 info
->product
="NAND Storage";
744 info
->num_sectors
= bank_size
* nr_banks
;
745 info
->sector_size
= 512;
749 #ifdef CONFIG_STORAGE_MULTI
750 int nand_num_drives(int first_drive
)
752 /* We don't care which logical drive number(s) we have been assigned */