1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 Rob Purchase
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include "ata-target.h"
26 /* The NAND driver is currently work-in-progress and as such contains
27 some dead code and debug stuff, such as the next few lines. */
29 #if defined(BOOTLOADER)
30 #include "../../../../bootloader/common.h" /* for printf */
34 /* for compatibility */
35 int ata_spinup_time
= 0;
37 long last_disk_activity
= -1;
39 /** static, private data **/
40 static bool initialized
= false;
42 static long next_yield
= 0;
43 #define MIN_YIELD_PERIOD 2000
45 static struct mutex ata_mtx NOCACHEBSS_ATTR
;
47 #define SECTOR_SIZE 512
49 /* TCC780x NAND Flash Controller */
51 #define NFC_CMD (*(volatile unsigned long *)0xF0053000)
52 #define NFC_SADDR (*(volatile unsigned long *)0xF005300C)
53 #define NFC_SDATA (*(volatile unsigned long *)0xF0053040)
54 #define NFC_WDATA (*(volatile unsigned long *)0xF0053010)
55 #define NFC_CTRL (*(volatile unsigned long *)0xF0053050)
56 #define NFC_IREQ (*(volatile unsigned long *)0xF0053060)
57 #define NFC_RST (*(volatile unsigned long *)0xF0053064)
60 #define NFC_16BIT (1<<26)
61 #define NFC_CS0 (1<<23)
62 #define NFC_CS1 (1<<22)
63 #define NFC_READY (1<<20)
65 #define ECC_CTRL (*(volatile unsigned long *)0xF005B000)
66 #define ECC_BASE (*(volatile unsigned long *)0xF005B004)
67 #define ECC_CLR (*(volatile unsigned long *)0xF005B00C)
68 #define ECC_MLC0W (*(volatile unsigned long *)0xF005B030)
69 #define ECC_MLC1W (*(volatile unsigned long *)0xF005B034)
70 #define ECC_MLC2W (*(volatile unsigned long *)0xF005B038)
71 #define ECC_ERR (*(volatile unsigned long *)0xF005B070)
72 #define ECC_ERRADDR (*(volatile unsigned long *)0xF005B050)
73 #define ECC_ERRDATA (*(volatile unsigned long *)0xF005B060)
76 #define ECC_M4EN (1<<6)
77 #define ECC_ENC (1<<27)
78 #define ECC_READY (1<<26)
80 /* Chip characteristics, initialised by nand_get_chip_info() */
82 static int page_size
= 0;
83 static int spare_size
= 0;
84 static int pages_per_block
= 0;
85 static int blocks_per_bank
= 0;
86 static int pages_per_bank
= 0;
87 static int row_cycles
= 0;
88 static int col_cycles
= 0;
89 static int total_banks
= 0;
90 static int sectors_per_page
= 0;
91 static int bytes_per_segment
= 0;
92 static int sectors_per_segment
= 0;
93 static int segments_per_bank
= 0;
95 /* Maximum values for static buffers */
97 #define MAX_PAGE_SIZE 4096
98 #define MAX_SPARE_SIZE 128
99 #define MAX_BLOCKS_PER_BANK 8192
100 #define MAX_PAGES_PER_BLOCK 128
102 /* In theory we can support 4 banks, but only 2 have been seen on 2/4/8Gb D2s. */
109 #define MAX_SEGMENTS (MAX_BLOCKS_PER_BANK * MAX_BANKS / 4)
111 /* Logical/Physical translation table */
117 //short segment_flag;
119 static struct lpt_entry lpt_lookup
[MAX_SEGMENTS
];
123 #define MAX_WRITE_CACHES 8
130 short page_map
[MAX_PAGES_PER_BLOCK
* 4];
132 static struct write_cache write_caches
[MAX_WRITE_CACHES
];
134 static int write_caches_in_use
= 0;
138 unsigned int page_buf
[(MAX_PAGE_SIZE
+ MAX_SPARE_SIZE
) / 4];
141 /* Conversion functions */
143 static inline int phys_segment_to_page_addr(int phys_segment
, int page_in_seg
)
145 int page_addr
= phys_segment
* pages_per_block
* 2;
149 /* Data is located in block+1 */
150 page_addr
+= pages_per_block
;
155 /* Data is located in second plane */
156 page_addr
+= (blocks_per_bank
/2) * pages_per_block
;
159 page_addr
+= page_in_seg
/4;
165 /* NAND physical access functions */
167 static void nand_chip_select(int chip
)
171 /* Disable both chip selects */
172 GPIOB_CLEAR
= (1<<21);
173 NFC_CTRL
|= NFC_CS0
| NFC_CS1
;
177 /* NFC chip select */
180 NFC_CTRL
&= ~NFC_CS0
;
186 NFC_CTRL
&= ~NFC_CS1
;
189 /* Secondary chip select */
196 GPIOB_CLEAR
= (1<<21);
202 static void nand_read_id(int chip
, unsigned char* id_buf
)
206 /* Enable NFC bus clock */
209 /* Reset NAND controller */
212 /* Set slow cycle timings since the chip is as yet unidentified */
213 NFC_CTRL
= (NFC_CTRL
&~0xFFF) | 0x353;
215 nand_chip_select(chip
);
217 /* Set write protect */
218 GPIOB_CLEAR
= (1<<19);
223 /* Set 8-bit data width */
224 NFC_CTRL
&= ~NFC_16BIT
;
226 /* Read ID command, single address cycle */
230 /* Read the 5 chip ID bytes */
231 for (i
= 0; i
< 5; i
++)
233 id_buf
[i
] = NFC_SDATA
& 0xFF;
236 nand_chip_select(-1);
238 /* Disable NFC bus clock */
239 BCLKCTR
&= ~DEV_NAND
;
243 static void nand_read_uid(int chip
, unsigned int* uid_buf
)
247 /* Enable NFC bus clock */
250 /* Set cycle timing (stp = 1, pw = 3, hold = 1) */
251 NFC_CTRL
= (NFC_CTRL
&~0xFFF) | 0x131;
253 nand_chip_select(chip
);
255 /* Set write protect */
258 /* Set 8-bit data width */
259 NFC_CTRL
&= ~NFC_16BIT
;
261 /* Undocumented (SAMSUNG specific?) commands set the chip into a
262 special mode allowing a normally-hidden UID block to be read. */
269 /* Write row/column address */
270 for (i
= 0; i
< col_cycles
; i
++) NFC_SADDR
= 0;
271 for (i
= 0; i
< row_cycles
; i
++) NFC_SADDR
= 0;
276 /* Wait until complete */
277 while (!(NFC_CTRL
& NFC_READY
)) {};
279 /* Copy data to buffer (data repeats after 8 words) */
280 for (i
= 0; i
< 8; i
++)
282 uid_buf
[i
] = NFC_WDATA
;
285 /* Reset the chip back to normal mode */
288 nand_chip_select(-1);
290 /* Disable NFC bus clock */
291 BCLKCTR
&= ~DEV_NAND
;
295 static void nand_read_raw(int chip
, int row
, int column
, int size
, void* buf
)
299 /* Enable NFC bus clock */
302 /* Set cycle timing (stp = 1, pw = 3, hold = 1) */
303 NFC_CTRL
= (NFC_CTRL
&~0xFFF) | 0x131;
305 nand_chip_select(chip
);
307 /* Set write protect */
308 GPIOB_CLEAR
= (1<<19);
310 /* Set 8-bit data width */
311 NFC_CTRL
&= ~NFC_16BIT
;
316 /* Write column address */
317 for (i
= 0; i
< col_cycles
; i
++)
319 NFC_SADDR
= column
& 0xFF;
320 column
= column
>> 8;
323 /* Write row address */
324 for (i
= 0; i
< row_cycles
; i
++)
326 NFC_SADDR
= row
& 0xFF;
330 /* End of read command */
333 /* Wait until complete */
334 while (!(NFC_CTRL
& NFC_READY
)) {};
336 /* Read data into page buffer */
337 if (((unsigned int)buf
& 3) || (size
& 3))
339 /* Use byte copy since either the buffer or size are not word-aligned */
340 /* TODO: Byte copy only where necessary (use words for mid-section) */
341 for (i
= 0; i
< size
; i
++)
343 ((unsigned char*)buf
)[i
] = NFC_SDATA
;
348 /* Use 4-byte copy as buffer and size are both word-aligned */
349 for (i
= 0; i
< (size
/4); i
++)
351 ((unsigned int*)buf
)[i
] = NFC_WDATA
;
355 nand_chip_select(-1);
357 /* Disable NFC bus clock */
358 BCLKCTR
&= ~DEV_NAND
;
362 static void nand_get_chip_info(void)
365 unsigned char manuf_id
;
366 unsigned char id_buf
[8];
368 /* Read chip id from bank 0 */
369 nand_read_id(0, id_buf
);
371 manuf_id
= id_buf
[0];
375 case 0xEC: /* SAMSUNG */
377 switch(id_buf
[1]) /* Chip Id */
379 case 0xD5: /* K9LAG08UOM */
383 pages_per_block
= 128;
384 blocks_per_bank
= 8192;
391 case 0xD7: /* K9LBG08UOM */
395 pages_per_block
= 128;
396 blocks_per_bank
= 8192;
408 panicf("Unknown NAND: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
409 id_buf
[0],id_buf
[1],id_buf
[2],id_buf
[3],id_buf
[4]);
412 pages_per_bank
= blocks_per_bank
* pages_per_block
;
413 segments_per_bank
= blocks_per_bank
/ 4;
414 bytes_per_segment
= page_size
* pages_per_block
* 4;
415 sectors_per_page
= page_size
/ SECTOR_SIZE
;
416 sectors_per_segment
= bytes_per_segment
/ SECTOR_SIZE
;
418 /* Establish how many banks are present */
419 nand_read_id(1, id_buf
);
421 if (id_buf
[0] == manuf_id
)
423 /* Bank 1 is populated, now check if banks 2/3 are valid */
424 nand_read_id(2, id_buf
);
426 if (id_buf
[0] == manuf_id
)
428 /* Bank 2 returned matching id - check if 2/3 are shadowing 0/1 */
429 unsigned int uid_buf0
[8];
430 unsigned int uid_buf2
[8];
432 nand_read_uid(0, uid_buf0
);
433 nand_read_uid(2, uid_buf2
);
435 if (memcmp(uid_buf0
, uid_buf2
, 32) == 0)
437 /* UIDs match, assume banks 2/3 are shadowing 0/1 */
442 /* UIDs differ, assume banks 2/3 are valid */
448 /* Bank 2 returned differing id - assume 2/3 are junk */
454 /* Bank 1 returned differing id - assume it is junk */
461 1. "BMP" tag at block 0, page 0, offset <page_size> [always present]
462 2. Byte at <page_size>+4 contains number of banks [or 0xff if 1 bank]
464 If this is confirmed for all D2s we can simplify the above code and
465 also remove the icky nand_read_uid() function.
468 nand_read_raw(0, /* bank */
470 page_size
, /* offset */
473 if (strncmp(id_buf
, "BMP", 3)) panicf("BMP tag not present");
477 if (id_buf
[4] != total_banks
) panicf("BMPM total_banks mismatch");
482 static bool nand_read_sector_of_phys_page(int chip
, int page
,
483 int sector
, void* buf
)
485 nand_read_raw(chip
, page
,
486 sector
* (SECTOR_SIZE
+16),
489 /* TODO: Read the 16 spare bytes, perform ECC correction */
495 static bool nand_read_sector_of_phys_segment(int chip
, int phys_segment
,
496 int page_in_seg
, int sector
,
499 int page_addr
= phys_segment_to_page_addr(phys_segment
,
502 return nand_read_sector_of_phys_page(chip
, page_addr
, sector
, buf
);
506 static bool nand_read_sector_of_logical_segment(int log_segment
, int sector
,
509 int page_in_segment
= sector
/ sectors_per_page
;
510 int sector_in_page
= sector
% sectors_per_page
;
512 int chip
= lpt_lookup
[log_segment
].chip
;
513 int phys_segment
= lpt_lookup
[log_segment
].phys_segment
;
515 /* Check if any of the write caches refer to this segment/page.
516 If present we need to read the cached page instead. */
521 while (!found
&& cache_num
< write_caches_in_use
)
523 if (write_caches
[cache_num
].log_segment
== log_segment
524 && write_caches
[cache_num
].page_map
[page_in_segment
] != -1)
527 chip
= write_caches
[cache_num
].chip
;
528 phys_segment
= write_caches
[cache_num
].phys_segment
;
529 page_in_segment
= write_caches
[cache_num
].page_map
[page_in_segment
];
537 return nand_read_sector_of_phys_segment(chip
, phys_segment
,
539 sector_in_page
, buf
);
542 #if 0 // LPT table is work-in-progress
544 static void read_lpt_block(int chip
, int phys_segment
)
546 int page
= 1; /* table starts at page 1 of segment */
549 struct lpt_entry
* lpt_ptr
= NULL
;
551 while (cont
&& page
< pages_per_block
)
555 nand_read_sector_of_phys_segment(chip
, phys_segment
,
556 page
, 0, /* only sector 0 is used */
559 /* Find out which chunk of the LPT table this section contains.
560 Do this by reading the logical segment number of entry 0 */
563 int first_chip
= page_buf
[0] / segments_per_bank
;
564 int first_phys_segment
= page_buf
[0] % segments_per_bank
;
566 unsigned char spare_buf
[16];
568 nand_read_raw(first_chip
,
569 phys_segment_to_page_addr(first_phys_segment
, 0),
570 SECTOR_SIZE
, /* offset */
573 int first_log_segment
= (spare_buf
[6] << 8) | spare_buf
[7];
575 lpt_ptr
= &lpt_lookup
[first_log_segment
];
577 #if defined(BOOTLOADER) && 1
578 printf("lpt @ %lx:%lx (ls:%lx)",
579 first_chip
, first_phys_segment
, first_log_segment
);
583 while (cont
&& (i
< SECTOR_SIZE
/4))
585 if (page_buf
[i
] != 0xFFFFFFFF)
587 lpt_ptr
->chip
= page_buf
[i
] / segments_per_bank
;
588 lpt_ptr
->phys_segment
= page_buf
[i
] % segments_per_bank
;
602 static void read_write_cache_segment(int chip
, int phys_segment
)
605 unsigned char spare_buf
[16];
607 if (write_caches_in_use
== MAX_WRITE_CACHES
)
608 panicf("Max NAND write caches reached");
610 write_caches
[write_caches_in_use
].chip
= chip
;
611 write_caches
[write_caches_in_use
].phys_segment
= phys_segment
;
613 /* Loop over each page in the phys segment (from page 1 onwards).
614 Read spare for 1st sector, store location of page in array. */
615 for (page
= 1; page
< pages_per_block
* 4; page
++)
617 unsigned short cached_page
;
618 unsigned short log_segment
;
620 nand_read_raw(chip
, phys_segment_to_page_addr(phys_segment
, page
),
621 SECTOR_SIZE
, /* offset to first sector's spare */
624 cached_page
= (spare_buf
[3] << 8) | spare_buf
[2]; /* why does endian */
625 log_segment
= (spare_buf
[6] << 8) | spare_buf
[7]; /* -ness differ? */
627 if (cached_page
!= 0xFFFF)
629 write_caches
[write_caches_in_use
].log_segment
= log_segment
;
630 write_caches
[write_caches_in_use
].page_map
[cached_page
] = page
;
633 write_caches_in_use
++;
637 /* TEMP testing functions */
642 static void display_page(int chip
, int page
)
645 nand_read_raw(chip
, page
, 0, page_size
+spare_size
, page_buf
);
647 for (i
= 0; i
< (page_size
+spare_size
)/4; i
+= 132)
649 int j
,interesting
= 0;
651 printf("c:%d p:%lx s:%d", chip
, page
, i
/128);
653 for (j
=i
; j
<(i
+131); j
++)
655 if (page_buf
[j
] != 0xffffffff) interesting
= 1;
660 for (j
=i
; j
<(i
+131); j
+=8)
662 printf("%lx %lx %lx %lx %lx %lx %lx %lx",
663 page_buf
[j
],page_buf
[j
+1],page_buf
[j
+2],page_buf
[j
+3],
664 page_buf
[j
+4],page_buf
[j
+5],page_buf
[j
+6],page_buf
[j
+7]);
666 while (!button_read_device()) {};
667 while (button_read_device()) {};
674 static void nand_test(void)
678 printf("%d banks", total_banks
);
679 printf("* %d pages", pages_per_bank
);
680 printf("* %d bytes per page", page_size
);
682 while (lpt_lookup
[segment
].chip
!= -1
683 && segment
< segments_per_bank
* total_banks
)
687 printf("%d sequential segments found (%dMb)",
688 segment
, (unsigned)(segment
*bytes_per_segment
)>>20);
695 void ata_led(bool onoff
)
700 int ata_read_sectors(IF_MV2(int drive
,) unsigned long start
, int incount
,
703 #ifdef HAVE_MULTIVOLUME
704 (void)drive
; /* unused for now */
706 mutex_lock(&ata_mtx
);
711 int segment
= start
/ sectors_per_segment
;
712 int secmod
= start
% sectors_per_segment
;
714 while (incount
> 0 && secmod
< sectors_per_segment
)
716 if (!nand_read_sector_of_logical_segment(segment
, secmod
, inbuf
))
718 mutex_unlock(&ata_mtx
);
722 inbuf
+= SECTOR_SIZE
;
730 mutex_unlock(&ata_mtx
);
736 mutex_unlock(&ata_mtx
);
740 int ata_write_sectors(IF_MV2(int drive
,) unsigned long start
, int count
,
743 #warning function not implemented
750 void ata_spindown(int seconds
)
756 bool ata_disk_is_active(void)
772 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
773 int ata_hard_reset(void)
779 int ata_soft_reset(void)
785 void ata_enable(bool on
)
787 /* null - flash controller is enabled/disabled as needed. */
793 int i
, bank
, phys_segment
;
794 unsigned char spare_buf
[16];
796 if (initialized
) return 0;
798 /* Get chip characteristics and number of banks */
799 nand_get_chip_info();
801 for (i
= 0; i
< MAX_SEGMENTS
; i
++)
803 lpt_lookup
[i
].chip
= -1;
804 lpt_lookup
[i
].phys_segment
= -1;
805 //lpt_lookup[i].segment_flag = -1;
808 write_caches_in_use
= 0;
810 for (i
= 0; i
< MAX_WRITE_CACHES
; i
++)
814 write_caches
[i
].log_segment
= -1;
815 write_caches
[i
].chip
= -1;
816 write_caches
[i
].phys_segment
= -1;
818 for (page
= 0; page
< MAX_PAGES_PER_BLOCK
* 4; page
++)
820 write_caches
[i
].page_map
[page
] = -1;
824 /* Scan banks to build up block translation table */
825 for (bank
= 0; bank
< total_banks
; bank
++)
827 for (phys_segment
= 0; phys_segment
< segments_per_bank
; phys_segment
++)
829 /* Read spare bytes from first sector of each segment */
830 nand_read_raw(bank
, phys_segment_to_page_addr(phys_segment
, 0),
831 SECTOR_SIZE
, /* offset */
834 switch (spare_buf
[4]) /* block type */
838 /* Log->Phys Translation table (for Main data area) */
839 //read_lpt_block(bank, phys_segment);
846 /* Main data area segment */
847 int segment
= (spare_buf
[6] << 8) | spare_buf
[7];
849 if (segment
< MAX_SEGMENTS
)
851 /* Store in LPT if not present or 0x17 overrides 0x13 */
852 //if (lpt_lookup[segment].segment_flag == -1 ||
853 // lpt_lookup[segment].segment_flag == 0x13)
855 lpt_lookup
[segment
].chip
= bank
;
856 lpt_lookup
[segment
].phys_segment
= phys_segment
;
857 //lpt_lookup[segment].segment_flag = spare_buf[4];
865 /* Recently-written page data (for Main data area) */
866 read_write_cache_segment(bank
, phys_segment
);
876 /* TEMP - print out some diagnostics */
884 /* TEMP: This will return junk, it's here for compilation only */
885 unsigned short* ata_get_identify(void)
887 return (unsigned short*)0x21000000; /* Unused DRAM */