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 ****************************************************************************/
25 //#include "ata-nand-target.h" /* TODO */
32 /*#define LOGF_ENABLE*/
39 * Standard NAND flash commands
41 #define NAND_CMD_READ0 0
42 #define NAND_CMD_READ1 1
43 #define NAND_CMD_RNDOUT 5
44 #define NAND_CMD_PAGEPROG 0x10
45 #define NAND_CMD_READOOB 0x50
46 #define NAND_CMD_ERASE1 0x60
47 #define NAND_CMD_STATUS 0x70
48 #define NAND_CMD_STATUS_MULTI 0x71
49 #define NAND_CMD_SEQIN 0x80
50 #define NAND_CMD_RNDIN 0x85
51 #define NAND_CMD_READID 0x90
52 #define NAND_CMD_ERASE2 0xd0
53 #define NAND_CMD_RESET 0xff
55 /* Extended commands for large page devices */
56 #define NAND_CMD_READSTART 0x30
57 #define NAND_CMD_RNDOUTSTART 0xE0
58 #define NAND_CMD_CACHEDPROG 0x15
61 #define NAND_STATUS_FAIL 0x01
62 #define NAND_STATUS_FAIL_N1 0x02
63 #define NAND_STATUS_TRUE_READY 0x20
64 #define NAND_STATUS_READY 0x40
65 #define NAND_STATUS_WP 0x80
68 * NAND parameter struct
72 unsigned int bus_width
; /* data bus width: 8-bit/16-bit */
73 unsigned int row_cycle
; /* row address cycles: 2/3 */
74 unsigned int page_size
; /* page size in bytes: 512/2048/4096 */
75 unsigned int oob_size
; /* oob size in bytes: 16/64/128 */
76 unsigned int page_per_block
; /* pages per block: 32/64/128 */
82 * NAND read routine for JZ4740
84 * Copyright (c) 2005-2008 Ingenic Semiconductor Inc.
88 static volatile unsigned long nand_address
;
89 #define NAND_DATAPORT (nand_address)
90 #define NAND_ADDRPORT (nand_address+0x10000)
91 #define NAND_COMMPORT (nand_address+0x08000)
97 #define __nand_cmd(n) (REG8(NAND_COMMPORT) = (n))
98 #define __nand_addr(n) (REG8(NAND_ADDRPORT) = (n))
99 #define __nand_data8() (REG8(NAND_DATAPORT))
100 #define __nand_data16() (REG16(NAND_DATAPORT))
102 #define __nand_ecc_rs_encoding() \
103 (REG_EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_ERST | EMC_NFECR_RS | EMC_NFECR_RS_ENCODING)
104 #define __nand_ecc_rs_decoding() \
105 (REG_EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_ERST | EMC_NFECR_RS | EMC_NFECR_RS_DECODING)
106 #define __nand_ecc_disable() (REG_EMC_NFECR &= ~EMC_NFECR_ECCE)
107 #define __nand_ecc_encode_sync() while (!(REG_EMC_NFINTS & EMC_NFINTS_ENCF))
108 #define __nand_ecc_decode_sync() while (!(REG_EMC_NFINTS & EMC_NFINTS_DECF))
110 /*--------------------------------------------------------------*/
112 static struct nand_info
* chip_info
= NULL
;
113 static struct nand_info
* banks
[4];
114 static unsigned int nr_banks
= 1;
115 static unsigned long bank_size
;
116 static struct nand_param internal_param
;
117 static struct mutex nand_mtx
;
119 static struct mutex nand_dma_mtx
;
120 static struct wakeup nand_wkup
;
122 static unsigned char temp_page
[4096]; /* Max page size */
124 static inline void jz_nand_wait_ready(void)
126 register unsigned int timeout
= 1000;
127 while ((REG_GPIO_PXPIN(2) & 0x40000000) && timeout
--);
128 while (!(REG_GPIO_PXPIN(2) & 0x40000000));
132 static inline void jz_nand_read_buf16(void *buf
, int count
)
135 register unsigned short *p
= (unsigned short *)buf
;
137 for (i
= 0; i
< count
; i
+= 2)
138 *p
++ = __nand_data16();
141 static inline void jz_nand_read_buf8(void *buf
, int count
)
144 register unsigned char *p
= (unsigned char *)buf
;
146 for (i
= 0; i
< count
; i
++)
147 *p
++ = __nand_data8();
150 static void jz_nand_write_dma(void *source
, unsigned int len
, int bw
)
152 mutex_lock(&nand_dma_mtx
);
154 if(((unsigned int)source
< 0xa0000000) && len
)
155 dma_cache_wback_inv((unsigned long)source
, len
);
159 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) = DMAC_DCCSR_NDES
;
160 REG_DMAC_DSAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)source
);
161 REG_DMAC_DTAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)NAND_DATAPORT
);
162 REG_DMAC_DTCR(DMA_NAND_CHANNEL
) = len
/ 16;
163 REG_DMAC_DRSR(DMA_NAND_CHANNEL
) = DMAC_DRSR_RS_AUTO
;
164 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) = (DMAC_DCMD_SAI
| DMAC_DCMD_DAI
| DMAC_DCMD_SWDH_32
| DMAC_DCMD_DS_16BYTE
|
165 (bw
== 8 ? DMAC_DCMD_DWDH_8
: DMAC_DCMD_DWDH_16
));
167 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) |= DMAC_DCCSR_EN
; /* Enable DMA channel */
169 while( REG_DMAC_DTCR(DMA_NAND_CHANNEL
) )
172 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) |= DMAC_DCMD_TIE
; /* Enable DMA interrupt */
173 wakeup_wait(&nand_wkup
, TIMEOUT_BLOCK
);
176 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_EN
; /* Disable DMA channel */
180 mutex_unlock(&nand_dma_mtx
);
183 static void jz_nand_read_dma(void *target
, unsigned int len
, int bw
)
185 mutex_lock(&nand_dma_mtx
);
187 if(((unsigned int)target
< 0xa0000000) && len
)
188 dma_cache_wback_inv((unsigned long)target
, len
);
192 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) = DMAC_DCCSR_NDES
;
193 REG_DMAC_DSAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)NAND_DATAPORT
);
194 REG_DMAC_DTAR(DMA_NAND_CHANNEL
) = PHYSADDR((unsigned long)target
);
195 REG_DMAC_DTCR(DMA_NAND_CHANNEL
) = len
/ 4;
196 REG_DMAC_DRSR(DMA_NAND_CHANNEL
) = DMAC_DRSR_RS_AUTO
;
197 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) = (DMAC_DCMD_SAI
| DMAC_DCMD_DAI
| DMAC_DCMD_DWDH_32
| DMAC_DCMD_DS_32BIT
|
198 (bw
== 8 ? DMAC_DCMD_SWDH_8
: DMAC_DCMD_SWDH_16
));
199 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) |= DMAC_DCCSR_EN
; /* Enable DMA channel */
201 while( REG_DMAC_DTCR(DMA_NAND_CHANNEL
) )
204 REG_DMAC_DCMD(DMA_NAND_CHANNEL
) |= DMAC_DCMD_TIE
; /* Enable DMA interrupt */
205 wakeup_wait(&nand_wkup
, TIMEOUT_BLOCK
);
208 //REG_DMAC_DCCSR(DMA_NAND_CHANNEL) &= ~DMAC_DCCSR_EN; /* Disable DMA channel */
212 mutex_unlock(&nand_dma_mtx
);
215 void DMA_CALLBACK(DMA_NAND_CHANNEL
)(void)
217 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_HLT
)
218 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_HLT
;
220 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_AR
)
221 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_AR
;
223 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_CT
)
224 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_CT
;
226 if (REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) & DMAC_DCCSR_TT
)
227 REG_DMAC_DCCSR(DMA_NAND_CHANNEL
) &= ~DMAC_DCCSR_TT
;
229 wakeup_signal(&nand_wkup
);
233 static inline void jz_nand_read_buf(void *buf
, int count
, int bw
)
237 jz_nand_read_dma(buf
, count
, 8);
239 jz_nand_read_dma(buf
, count
, 16);
242 jz_nand_read_buf8(buf
, count
);
244 jz_nand_read_buf16(buf
, count
);
250 * Correct 1~9-bit errors in 512-bytes data
252 static void jz_rs_correct(unsigned char *dat
, int idx
, int mask
)
255 unsigned short d
, d1
, dm
;
260 i
= (j
== 0) ? (i
- 1) : i
;
261 j
= (j
== 0) ? 7 : (j
- 1);
269 d
= (dat
[i
] << 8) | dat
[i
- 1];
271 d1
= (d
>> j
) & 0x1ff;
275 d
= (d
& dm
) | (d1
<< j
);
277 dat
[i
- 1] = d
& 0xff;
279 dat
[i
] = (d
>> 8) & 0xff;
286 static int jz_nand_read_oob(unsigned long page_addr
, unsigned char *buf
, int size
)
288 struct nand_param
*nandp
= &internal_param
;
289 int page_size
, row_cycle
, bus_width
;
292 page_size
= nandp
->page_size
;
293 row_cycle
= nandp
->row_cycle
;
294 bus_width
= nandp
->bus_width
;
296 if (page_size
>= 2048)
297 col_addr
= page_size
;
301 if (page_size
>= 2048)
302 /* Send READ0 command */
303 __nand_cmd(NAND_CMD_READ0
);
305 /* Send READOOB command */
306 __nand_cmd(NAND_CMD_READOOB
);
308 /* Send column address */
309 __nand_addr(col_addr
& 0xff);
310 if (page_size
>= 2048)
311 __nand_addr((col_addr
>> 8) & 0xff);
313 /* Send page address */
314 __nand_addr(page_addr
& 0xff);
315 __nand_addr((page_addr
>> 8) & 0xff);
317 __nand_addr((page_addr
>> 16) & 0xff);
319 /* Send READSTART command for 2048 ps NAND */
320 if (page_size
>= 2048)
321 __nand_cmd(NAND_CMD_READSTART
);
323 /* Wait for device ready */
324 jz_nand_wait_ready();
327 jz_nand_read_buf(buf
, size
, bus_width
);
338 * block - block number: 0, 1, 2, ...
339 * page - page number within a block: 0, 1, 2, ...
340 * dst - pointer to target buffer
342 static int jz_nand_read_page(unsigned long page_addr
, unsigned char *dst
)
344 struct nand_param
*nandp
= &internal_param
;
345 int page_size
, oob_size
, page_per_block
;
346 int row_cycle
, bus_width
, ecc_count
;
351 unsigned char *data_buf
;
352 unsigned char oob_buf
[nandp
->oob_size
];
354 if(nand_address
== 0)
357 page_size
= nandp
->page_size
;
358 oob_size
= nandp
->oob_size
;
359 page_per_block
= nandp
->page_per_block
;
360 row_cycle
= nandp
->row_cycle
;
361 bus_width
= nandp
->bus_width
;
366 jz_nand_read_oob(page_addr
, oob_buf
, oob_size
);
372 /* Send READ0 command */
373 __nand_cmd(NAND_CMD_READ0
);
375 /* Send column address */
377 if (page_size
>= 2048)
380 /* Send page address */
381 __nand_addr(page_addr
& 0xff);
382 __nand_addr((page_addr
>> 8) & 0xff);
384 __nand_addr((page_addr
>> 16) & 0xff);
386 /* Send READSTART command for 2048 ps NAND */
387 if (page_size
>= 2048)
388 __nand_cmd(NAND_CMD_READSTART
);
390 /* Wait for device ready */
391 jz_nand_wait_ready();
396 ecc_count
= page_size
/ ECC_BLOCK
;
398 for (i
= 0; i
< ecc_count
; i
++)
401 volatile unsigned char *paraddr
= (volatile unsigned char *)EMC_NFPAR0
;
404 /* Enable RS decoding */
405 REG_EMC_NFINTS
= 0x0;
406 __nand_ecc_rs_decoding();
410 jz_nand_read_buf((void *)data_buf
, ECC_BLOCK
, bus_width
);
414 for (j
= 0; j
< PAR_SIZE
; j
++)
415 *paraddr
++ = oob_buf
[ECC_POS
+ i
*PAR_SIZE
+ j
];
418 REG_EMC_NFECR
|= EMC_NFECR_PRDY
;
420 /* Wait for completion */
421 __nand_ecc_decode_sync();
423 /* Disable decoding */
424 __nand_ecc_disable();
426 /* Check result of decoding */
427 stat
= REG_EMC_NFINTS
;
428 if (stat
& EMC_NFINTS_ERR
)
431 if (stat
& EMC_NFINTS_UNCOR
)
433 /* Uncorrectable error occurred */
434 logf("Uncorrectable ECC error at NAND page address 0x%lx", page_addr
);
439 unsigned int errcnt
, index
, mask
;
441 errcnt
= (stat
& EMC_NFINTS_ERRCNT_MASK
) >> EMC_NFINTS_ERRCNT_BIT
;
445 index
= (REG_EMC_NFERR3
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
446 mask
= (REG_EMC_NFERR3
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
447 jz_rs_correct(data_buf
, index
, mask
);
449 index
= (REG_EMC_NFERR2
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
450 mask
= (REG_EMC_NFERR2
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
451 jz_rs_correct(data_buf
, index
, mask
);
453 index
= (REG_EMC_NFERR1
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
454 mask
= (REG_EMC_NFERR1
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
455 jz_rs_correct(data_buf
, index
, mask
);
457 index
= (REG_EMC_NFERR0
& EMC_NFERR_INDEX_MASK
) >> EMC_NFERR_INDEX_BIT
;
458 mask
= (REG_EMC_NFERR0
& EMC_NFERR_MASK_MASK
) >> EMC_NFERR_MASK_BIT
;
459 jz_rs_correct(data_buf
, index
, mask
);
468 data_buf
+= ECC_BLOCK
;
475 * Disable NAND controller
477 static void jz_nand_disable(void)
479 REG_EMC_NFCSR
&= ~(EMC_NFCSR_NFCE1
| EMC_NFCSR_NFCE2
|
480 EMC_NFCSR_NFCE3
| EMC_NFCSR_NFCE4
|
481 EMC_NFCSR_NFE1
| EMC_NFCSR_NFE2
|
482 EMC_NFCSR_NFE3
| EMC_NFCSR_NFE4
);
486 * Enable NAND controller
488 static void jz_nand_enable(void)
492 REG_GPIO_PXFUNS(1) = 0x1E018000; // __gpio_as_func0() start
493 REG_GPIO_PXSELC(1) = 0x1E018000; // __gpio_as_func0() end
495 REG_GPIO_PXFUNS(2) = 0x3000<<16; // __gpio_as_func0() start
496 REG_GPIO_PXSELC(2) = 0x3000<<16; // __gpio_as_func0() end
498 REG_GPIO_PXFUNC(2) = 0x4000<<16; // __gpio_port_as_input() start
499 REG_GPIO_PXSELC(2) = 0x4000<<16;
500 REG_GPIO_PXDIRC(2) = 0x4000<<16; // __gpio_port_as_input() end
501 REG_GPIO_PXPES(2) = 0x4000<<16; // __gpio_disable_pull()
503 REG_GPIO_PXFUNS(1) = 0x40<<16; // __gpio_as_func0() start
504 REG_GPIO_PXSELC(1) = 0x40<<16; // __gpio_as_func0() end
506 REG_EMC_SMCR1
= (REG_EMC_SMCR1
& 0xFF) | 0x4621200;
507 REG_EMC_SMCR2
= (REG_EMC_SMCR2
& 0xFF) | 0x4621200;
508 REG_EMC_SMCR3
= (REG_EMC_SMCR3
& 0xFF) | 0x4621200;
509 REG_EMC_SMCR4
= (REG_EMC_SMCR4
& 0xFF) | 0x4621200;
511 REG_EMC_SMCR1
= REG_EMC_SMCR2
= REG_EMC_SMCR3
= REG_EMC_SMCR4
= 0x6621200;
513 REG_EMC_SMCR1
= REG_EMC_SMCR2
= REG_EMC_SMCR3
= REG_EMC_SMCR4
= 0x04444400;
517 static void jz_nand_select(int bank
)
519 REG_EMC_NFCSR
|= (EMC_NFCSR_NFE(bank
+1) | EMC_NFCSR_NFCE(bank
+1));
524 nand_address
= 0xB8000000;
527 nand_address
= 0xB4000000;
530 nand_address
= 0xAC000000;
533 nand_address
= 0xA8000000;
538 static void jz_nand_deselect(int bank
)
540 REG_EMC_NFCSR
&= ~(EMC_NFCSR_NFE(bank
+1) | EMC_NFCSR_NFCE(bank
+1));
544 static int jz_nand_init(void)
546 unsigned char cData
[5];
555 __nand_cmd(NAND_CMD_READID
);
556 __nand_addr(NAND_CMD_READ0
);
557 cData
[0] = __nand_data8();
558 cData
[1] = __nand_data8();
559 cData
[2] = __nand_data8();
560 cData
[3] = __nand_data8();
561 cData
[4] = __nand_data8();
565 logf("NAND chip %d: 0x%x 0x%x 0x%x 0x%x 0x%x", i
+1, cData
[0], cData
[1],
566 cData
[2], cData
[3], cData
[4]);
568 banks
[i
] = nand_identify(cData
);
573 if(i
== 0 && banks
[i
] == NULL
)
575 panicf("Unknown NAND flash chip: 0x%x 0x%x 0x%x 0x%x 0x%x", cData
[0],
576 cData
[1], cData
[2], cData
[3], cData
[4]);
577 return -1; /* panicf() doesn't return though */
580 chip_info
= banks
[0];
582 internal_param
.bus_width
= 8;
583 internal_param
.row_cycle
= chip_info
->row_cycles
;
584 internal_param
.page_size
= chip_info
->page_size
;
585 internal_param
.oob_size
= chip_info
->spare_size
;
586 internal_param
.page_per_block
= chip_info
->pages_per_block
;
588 bank_size
= chip_info
->page_size
* chip_info
->blocks_per_bank
/ 512 * chip_info
->pages_per_block
;
598 static bool inited
= false;
602 res
= jz_nand_init();
603 mutex_init(&nand_mtx
);
605 mutex_init(&nand_dma_mtx
);
606 wakeup_init(&nand_wkup
);
607 system_enable_irq(DMA_IRQ(DMA_NAND_CHANNEL
));
616 static inline int read_sector(unsigned long start
, unsigned int count
,
617 void* buf
, unsigned int chip_size
)
621 if(UNLIKELY(start
% chip_size
== 0 && count
== chip_size
))
622 ret
= jz_nand_read_page(start
/ chip_size
, buf
);
625 ret
= jz_nand_read_page(start
/ chip_size
, temp_page
);
626 memcpy(buf
, temp_page
+ (start
% chip_size
), count
);
632 int nand_read_sectors(IF_MV2(int drive
,) unsigned long start
, int count
, void* buf
)
634 #ifdef HAVE_MULTIVOLUME
638 unsigned int i
, _count
, chip_size
= chip_info
->page_size
;
639 unsigned long _start
;
642 mutex_lock(&nand_mtx
);
647 if(_count
<= chip_size
)
649 jz_nand_select(start
/ bank_size
);
650 ret
= read_sector(_start
, _count
, buf
, chip_size
);
651 jz_nand_deselect(start
/ bank_size
);
655 for(i
=0; i
<_count
&& ret
==0; i
+=chip_size
)
657 jz_nand_select((start
+(i
>>9)) / bank_size
);
659 ret
= read_sector(_start
+i
, (_count
-i
< chip_size
?
660 _count
-i
: chip_size
),
663 jz_nand_deselect((start
+(i
>>9)) / bank_size
);
667 mutex_unlock(&nand_mtx
);
669 logf("nand_read_sectors(%ld, %d, 0x%x): %d", start
, count
, (int)buf
, ret
);
675 int nand_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
, const void* buf
)
680 #ifdef HAVE_MULTIVOLUME
687 #ifdef HAVE_STORAGE_FLUSH
694 void nand_spindown(int seconds
)
700 void nand_sleep(void)
710 void nand_enable(bool on
)
712 /* null - flash controller is enabled/disabled as needed. */
717 long nand_last_disk_activity(void)
722 int nand_spinup_time(void)
727 void nand_sleepnow(void)
731 #ifdef STORAGE_GET_INFO
732 void nand_get_info(IF_MV2(int drive
,) struct storage_info
*info
)
734 #ifdef HAVE_MULTIVOLUME
738 /* firmware version */
739 info
->revision
="0.00";
741 info
->vendor
="Rockbox";
742 info
->product
="NAND Storage";
745 info
->num_sectors
= bank_size
* nr_banks
;
746 info
->sector_size
= 512;
750 #ifdef CONFIG_STORAGE_MULTI
751 int nand_num_drives(int first_drive
)
753 /* We don't care which logical drive number(s) we have been assigned */