Kill some D2 yellows with a simplistic Rolo implementation and NAND driver cleanup...
[kugel-rb.git] / firmware / target / arm / tcc780x / ata-nand-tcc780x.c
blob4cf55c0c3ce8f0983a3a835022f1c23b8866a085
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
19 #include "ata.h"
20 #include "ata-target.h"
21 #include "system.h"
22 #include <string.h>
23 #include "led.h"
24 #include "panic.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 */
31 extern int line;
32 #endif
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 struct mutex ata_mtx SHAREDBSS_ATTR;
44 #define SECTOR_SIZE 512
46 /* TCC780x NAND Flash Controller */
48 #define NFC_CMD (*(volatile unsigned long *)0xF0053000)
49 #define NFC_SADDR (*(volatile unsigned long *)0xF005300C)
50 #define NFC_SDATA (*(volatile unsigned long *)0xF0053040)
51 #define NFC_WDATA (*(volatile unsigned long *)0xF0053010)
52 #define NFC_CTRL (*(volatile unsigned long *)0xF0053050)
53 #define NFC_IREQ (*(volatile unsigned long *)0xF0053060)
54 #define NFC_RST (*(volatile unsigned long *)0xF0053064)
56 /* NFC_CTRL flags */
57 #define NFC_16BIT (1<<26)
58 #define NFC_CS0 (1<<23)
59 #define NFC_CS1 (1<<22)
60 #define NFC_READY (1<<20)
62 #define ECC_CTRL (*(volatile unsigned long *)0xF005B000)
63 #define ECC_BASE (*(volatile unsigned long *)0xF005B004)
64 #define ECC_CLR (*(volatile unsigned long *)0xF005B00C)
65 #define ECC_MLC0W (*(volatile unsigned long *)0xF005B030)
66 #define ECC_MLC1W (*(volatile unsigned long *)0xF005B034)
67 #define ECC_MLC2W (*(volatile unsigned long *)0xF005B038)
68 #define ECC_ERR (*(volatile unsigned long *)0xF005B070)
69 #define ECC_ERRADDR (*(volatile unsigned long *)0xF005B050)
70 #define ECC_ERRDATA (*(volatile unsigned long *)0xF005B060)
72 /* ECC_CTRL flags */
73 #define ECC_M4EN (1<<6)
74 #define ECC_ENC (1<<27)
75 #define ECC_READY (1<<26)
77 /* Chip characteristics, initialised by nand_get_chip_info() */
79 static int page_size = 0;
80 static int spare_size = 0;
81 static int pages_per_block = 0;
82 static int blocks_per_bank = 0;
83 static int pages_per_bank = 0;
84 static int row_cycles = 0;
85 static int col_cycles = 0;
86 static int total_banks = 0;
87 static int sectors_per_page = 0;
88 static int bytes_per_segment = 0;
89 static int sectors_per_segment = 0;
90 static int segments_per_bank = 0;
92 /* Maximum values for static buffers */
94 #define MAX_PAGE_SIZE 4096
95 #define MAX_SPARE_SIZE 128
96 #define MAX_BLOCKS_PER_BANK 8192
97 #define MAX_PAGES_PER_BLOCK 128
99 /* In theory we can support 4 banks, but only 2 have been seen on 2/4/8Gb D2s. */
100 #ifdef COWON_D2
101 #define MAX_BANKS 2
102 #else
103 #define MAX_BANKS 4
104 #endif
106 #define MAX_SEGMENTS (MAX_BLOCKS_PER_BANK * MAX_BANKS / 4)
108 /* Logical/Physical translation table */
110 struct lpt_entry
112 short chip;
113 short phys_segment;
114 //short segment_flag;
116 static struct lpt_entry lpt_lookup[MAX_SEGMENTS];
118 /* Write Caches */
120 #define MAX_WRITE_CACHES 8
122 struct write_cache
124 short chip;
125 short phys_segment;
126 short log_segment;
127 short page_map[MAX_PAGES_PER_BLOCK * 4];
129 static struct write_cache write_caches[MAX_WRITE_CACHES];
131 static int write_caches_in_use = 0;
133 /* Read buffer */
135 unsigned int page_buf[(MAX_PAGE_SIZE + MAX_SPARE_SIZE) / 4];
138 /* Conversion functions */
140 static inline int phys_segment_to_page_addr(int phys_segment, int page_in_seg)
142 int page_addr = phys_segment * pages_per_block * 2;
144 if (page_in_seg & 1)
146 /* Data is located in block+1 */
147 page_addr += pages_per_block;
150 if (page_in_seg & 2)
152 /* Data is located in second plane */
153 page_addr += (blocks_per_bank/2) * pages_per_block;
156 page_addr += page_in_seg/4;
158 return page_addr;
162 /* NAND physical access functions */
164 static void nand_chip_select(int chip)
166 if (chip == -1)
168 /* Disable both chip selects */
169 GPIOB_CLEAR = (1<<21);
170 NFC_CTRL |= NFC_CS0 | NFC_CS1;
172 else
174 /* NFC chip select */
175 if (chip & 1)
177 NFC_CTRL &= ~NFC_CS0;
178 NFC_CTRL |= NFC_CS1;
180 else
182 NFC_CTRL |= NFC_CS0;
183 NFC_CTRL &= ~NFC_CS1;
186 /* Secondary chip select */
187 if (chip & 2)
189 GPIOB_SET = (1<<21);
191 else
193 GPIOB_CLEAR = (1<<21);
199 static void nand_read_id(int chip, unsigned char* id_buf)
201 int i;
203 /* Enable NFC bus clock */
204 BCLKCTR |= DEV_NAND;
206 /* Reset NAND controller */
207 NFC_RST = 0;
209 /* Set slow cycle timings since the chip is as yet unidentified */
210 NFC_CTRL = (NFC_CTRL &~0xFFF) | 0x353;
212 nand_chip_select(chip);
214 /* Set write protect */
215 GPIOB_CLEAR = (1<<19);
217 /* Reset command */
218 NFC_CMD = 0xFF;
220 /* Set 8-bit data width */
221 NFC_CTRL &= ~NFC_16BIT;
223 /* Read ID command, single address cycle */
224 NFC_CMD = 0x90;
225 NFC_SADDR = 0x00;
227 /* Read the 5 chip ID bytes */
228 for (i = 0; i < 5; i++)
230 id_buf[i] = NFC_SDATA & 0xFF;
233 nand_chip_select(-1);
235 /* Disable NFC bus clock */
236 BCLKCTR &= ~DEV_NAND;
240 static void nand_read_uid(int chip, unsigned int* uid_buf)
242 int i;
244 /* Enable NFC bus clock */
245 BCLKCTR |= DEV_NAND;
247 /* Set cycle timing (stp = 1, pw = 3, hold = 1) */
248 NFC_CTRL = (NFC_CTRL &~0xFFF) | 0x131;
250 nand_chip_select(chip);
252 /* Set write protect */
253 GPIOB_CLEAR = 1<<19;
255 /* Set 8-bit data width */
256 NFC_CTRL &= ~NFC_16BIT;
258 /* Undocumented (SAMSUNG specific?) commands set the chip into a
259 special mode allowing a normally-hidden UID block to be read. */
260 NFC_CMD = 0x30;
261 NFC_CMD = 0x65;
263 /* Read command */
264 NFC_CMD = 0x00;
266 /* Write row/column address */
267 for (i = 0; i < col_cycles; i++) NFC_SADDR = 0;
268 for (i = 0; i < row_cycles; i++) NFC_SADDR = 0;
270 /* End of read */
271 NFC_CMD = 0x30;
273 /* Wait until complete */
274 while (!(NFC_CTRL & NFC_READY)) {};
276 /* Copy data to buffer (data repeats after 8 words) */
277 for (i = 0; i < 8; i++)
279 uid_buf[i] = NFC_WDATA;
282 /* Reset the chip back to normal mode */
283 NFC_CMD = 0xFF;
285 nand_chip_select(-1);
287 /* Disable NFC bus clock */
288 BCLKCTR &= ~DEV_NAND;
292 static void nand_read_raw(int chip, int row, int column, int size, void* buf)
294 int i;
296 /* Enable NFC bus clock */
297 BCLKCTR |= DEV_NAND;
299 /* Set cycle timing (stp = 1, pw = 3, hold = 1) */
300 NFC_CTRL = (NFC_CTRL &~0xFFF) | 0x131;
302 nand_chip_select(chip);
304 /* Set write protect */
305 GPIOB_CLEAR = (1<<19);
307 /* Set 8-bit data width */
308 NFC_CTRL &= ~NFC_16BIT;
310 /* Read command */
311 NFC_CMD = 0x00;
313 /* Write column address */
314 for (i = 0; i < col_cycles; i++)
316 NFC_SADDR = column & 0xFF;
317 column = column >> 8;
320 /* Write row address */
321 for (i = 0; i < row_cycles; i++)
323 NFC_SADDR = row & 0xFF;
324 row = row >> 8;
327 /* End of read command */
328 NFC_CMD = 0x30;
330 /* Wait until complete */
331 while (!(NFC_CTRL & NFC_READY)) {};
333 /* Read data into page buffer */
334 if (((unsigned int)buf & 3) || (size & 3))
336 /* Use byte copy since either the buffer or size are not word-aligned */
337 /* TODO: Byte copy only where necessary (use words for mid-section) */
338 for (i = 0; i < size; i++)
340 ((unsigned char*)buf)[i] = NFC_SDATA;
343 else
345 /* Use 4-byte copy as buffer and size are both word-aligned */
346 for (i = 0; i < (size/4); i++)
348 ((unsigned int*)buf)[i] = NFC_WDATA;
352 nand_chip_select(-1);
354 /* Disable NFC bus clock */
355 BCLKCTR &= ~DEV_NAND;
359 static void nand_get_chip_info(void)
361 bool found = false;
362 unsigned char manuf_id;
363 unsigned char id_buf[8];
365 /* Read chip id from bank 0 */
366 nand_read_id(0, id_buf);
368 manuf_id = id_buf[0];
370 switch (manuf_id)
372 case 0xEC: /* SAMSUNG */
374 switch(id_buf[1]) /* Chip Id */
376 case 0xD5: /* K9LAG08UOM */
378 page_size = 2048;
379 spare_size = 64;
380 pages_per_block = 128;
381 blocks_per_bank = 8192;
382 col_cycles = 2;
383 row_cycles = 3;
385 found = true;
386 break;
388 case 0xD7: /* K9LBG08UOM */
390 page_size = 4096;
391 spare_size = 128;
392 pages_per_block = 128;
393 blocks_per_bank = 8192;
394 col_cycles = 2;
395 row_cycles = 3;
397 found = true;
398 break;
400 break;
403 if (!found)
405 panicf("Unknown NAND: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
406 id_buf[0],id_buf[1],id_buf[2],id_buf[3],id_buf[4]);
409 pages_per_bank = blocks_per_bank * pages_per_block;
410 segments_per_bank = blocks_per_bank / 4;
411 bytes_per_segment = page_size * pages_per_block * 4;
412 sectors_per_page = page_size / SECTOR_SIZE;
413 sectors_per_segment = bytes_per_segment / SECTOR_SIZE;
415 /* Establish how many banks are present */
416 nand_read_id(1, id_buf);
418 if (id_buf[0] == manuf_id)
420 /* Bank 1 is populated, now check if banks 2/3 are valid */
421 nand_read_id(2, id_buf);
423 if (id_buf[0] == manuf_id)
425 /* Bank 2 returned matching id - check if 2/3 are shadowing 0/1 */
426 unsigned int uid_buf0[8];
427 unsigned int uid_buf2[8];
429 nand_read_uid(0, uid_buf0);
430 nand_read_uid(2, uid_buf2);
432 if (memcmp(uid_buf0, uid_buf2, 32) == 0)
434 /* UIDs match, assume banks 2/3 are shadowing 0/1 */
435 total_banks = 2;
437 else
439 /* UIDs differ, assume banks 2/3 are valid */
440 total_banks = 4;
443 else
445 /* Bank 2 returned differing id - assume 2/3 are junk */
446 total_banks = 2;
449 else
451 /* Bank 1 returned differing id - assume it is junk */
452 total_banks = 1;
456 Sanity checks:
458 1. "BMP" tag at block 0, page 0, offset <page_size> [always present]
459 2. Byte at <page_size>+4 contains number of banks [or 0xff if 1 bank]
461 If this is confirmed for all D2s we can simplify the above code and
462 also remove the icky nand_read_uid() function.
465 nand_read_raw(0, /* bank */
466 0, /* page */
467 page_size, /* offset */
468 8, id_buf);
470 if (strncmp(id_buf, "BMP", 3)) panicf("BMP tag not present");
472 if (total_banks > 1)
474 if (id_buf[4] != total_banks) panicf("BMPM total_banks mismatch");
479 static bool nand_read_sector_of_phys_page(int chip, int page,
480 int sector, void* buf)
482 nand_read_raw(chip, page,
483 sector * (SECTOR_SIZE+16),
484 SECTOR_SIZE, buf);
486 /* TODO: Read the 16 spare bytes, perform ECC correction */
488 return true;
492 static bool nand_read_sector_of_phys_segment(int chip, int phys_segment,
493 int page_in_seg, int sector,
494 void* buf)
496 int page_addr = phys_segment_to_page_addr(phys_segment,
497 page_in_seg);
499 return nand_read_sector_of_phys_page(chip, page_addr, sector, buf);
503 static bool nand_read_sector_of_logical_segment(int log_segment, int sector,
504 void* buf)
506 int page_in_segment = sector / sectors_per_page;
507 int sector_in_page = sector % sectors_per_page;
509 int chip = lpt_lookup[log_segment].chip;
510 int phys_segment = lpt_lookup[log_segment].phys_segment;
512 /* Check if any of the write caches refer to this segment/page.
513 If present we need to read the cached page instead. */
515 int cache_num = 0;
516 bool found = false;
518 while (!found && cache_num < write_caches_in_use)
520 if (write_caches[cache_num].log_segment == log_segment
521 && write_caches[cache_num].page_map[page_in_segment] != -1)
523 found = true;
524 chip = write_caches[cache_num].chip;
525 phys_segment = write_caches[cache_num].phys_segment;
526 page_in_segment = write_caches[cache_num].page_map[page_in_segment];
528 else
530 cache_num++;
534 return nand_read_sector_of_phys_segment(chip, phys_segment,
535 page_in_segment,
536 sector_in_page, buf);
539 #if 0 // LPT table is work-in-progress
541 static void read_lpt_block(int chip, int phys_segment)
543 int page = 1; /* table starts at page 1 of segment */
544 bool cont = true;
546 struct lpt_entry* lpt_ptr = NULL;
548 while (cont && page < pages_per_block)
550 int i = 0;
552 nand_read_sector_of_phys_segment(chip, phys_segment,
553 page, 0, /* only sector 0 is used */
554 page_buf);
556 /* Find out which chunk of the LPT table this section contains.
557 Do this by reading the logical segment number of entry 0 */
558 if (lpt_ptr == NULL)
560 int first_chip = page_buf[0] / segments_per_bank;
561 int first_phys_segment = page_buf[0] % segments_per_bank;
563 unsigned char spare_buf[16];
565 nand_read_raw(first_chip,
566 phys_segment_to_page_addr(first_phys_segment, 0),
567 SECTOR_SIZE, /* offset */
568 16, spare_buf);
570 int first_log_segment = (spare_buf[6] << 8) | spare_buf[7];
572 lpt_ptr = &lpt_lookup[first_log_segment];
574 #if defined(BOOTLOADER) && 1
575 printf("lpt @ %lx:%lx (ls:%lx)",
576 first_chip, first_phys_segment, first_log_segment);
577 #endif
580 while (cont && (i < SECTOR_SIZE/4))
582 if (page_buf[i] != 0xFFFFFFFF)
584 lpt_ptr->chip = page_buf[i] / segments_per_bank;
585 lpt_ptr->phys_segment = page_buf[i] % segments_per_bank;
587 lpt_ptr++;
588 i++;
590 else cont = false;
592 page++;
596 #endif
599 static void read_write_cache_segment(int chip, int phys_segment)
601 int page;
602 unsigned char spare_buf[16];
604 if (write_caches_in_use == MAX_WRITE_CACHES)
605 panicf("Max NAND write caches reached");
607 write_caches[write_caches_in_use].chip = chip;
608 write_caches[write_caches_in_use].phys_segment = phys_segment;
610 /* Loop over each page in the phys segment (from page 1 onwards).
611 Read spare for 1st sector, store location of page in array. */
612 for (page = 1; page < pages_per_block * 4; page++)
614 unsigned short cached_page;
615 unsigned short log_segment;
617 nand_read_raw(chip, phys_segment_to_page_addr(phys_segment, page),
618 SECTOR_SIZE, /* offset to first sector's spare */
619 16, spare_buf);
621 cached_page = (spare_buf[3] << 8) | spare_buf[2]; /* why does endian */
622 log_segment = (spare_buf[6] << 8) | spare_buf[7]; /* -ness differ? */
624 if (cached_page != 0xFFFF)
626 write_caches[write_caches_in_use].log_segment = log_segment;
627 write_caches[write_caches_in_use].page_map[cached_page] = page;
630 write_caches_in_use++;
634 /* TEMP testing functions */
636 #ifdef BOOTLOADER
638 #if 0
639 static void display_page(int chip, int page)
641 int i;
642 nand_read_raw(chip, page, 0, page_size+spare_size, page_buf);
644 for (i = 0; i < (page_size+spare_size)/4; i += 132)
646 int j,interesting = 0;
647 line = 1;
648 printf("c:%d p:%lx s:%d", chip, page, i/128);
650 for (j=i; j<(i+131); j++)
652 if (page_buf[j] != 0xffffffff) interesting = 1;
655 if (interesting)
657 for (j=i; j<(i+131); j+=8)
659 printf("%lx %lx %lx %lx %lx %lx %lx %lx",
660 page_buf[j],page_buf[j+1],page_buf[j+2],page_buf[j+3],
661 page_buf[j+4],page_buf[j+5],page_buf[j+6],page_buf[j+7]);
663 while (!button_read_device()) {};
664 while (button_read_device()) {};
665 reset_screen();
669 #endif
671 static void nand_test(void)
673 int segment = 0;
675 printf("%d banks", total_banks);
676 printf("* %d pages", pages_per_bank);
677 printf("* %d bytes per page", page_size);
679 while (lpt_lookup[segment].chip != -1
680 && segment < segments_per_bank * total_banks)
682 segment++;
684 printf("%d sequential segments found (%dMb)",
685 segment, (unsigned)(segment*bytes_per_segment)>>20);
687 #endif
690 /* API Functions */
692 void ata_led(bool onoff)
694 led(onoff);
697 int ata_read_sectors(IF_MV2(int drive,) unsigned long start, int incount,
698 void* inbuf)
700 #ifdef HAVE_MULTIVOLUME
701 (void)drive; /* unused for now */
702 #endif
703 mutex_lock(&ata_mtx);
705 while (incount > 0)
707 int done = 0;
708 int segment = start / sectors_per_segment;
709 int secmod = start % sectors_per_segment;
711 while (incount > 0 && secmod < sectors_per_segment)
713 if (!nand_read_sector_of_logical_segment(segment, secmod, inbuf))
715 mutex_unlock(&ata_mtx);
716 return -1;
719 inbuf += SECTOR_SIZE;
720 incount--;
721 secmod++;
722 done++;
725 if (done < 0)
727 mutex_unlock(&ata_mtx);
728 return -1;
730 start += done;
733 mutex_unlock(&ata_mtx);
734 return 0;
737 int ata_write_sectors(IF_MV2(int drive,) unsigned long start, int count,
738 const void* outbuf)
740 #ifdef HAVE_MULTIVOLUME
741 (void)drive; /* unused for now */
742 #endif
744 /* TODO: Learn more about TNFTL and implement this one day... */
745 (void)start;
746 (void)count;
747 (void)outbuf;
748 return -1;
751 void ata_spindown(int seconds)
753 /* null */
754 (void)seconds;
757 bool ata_disk_is_active(void)
759 /* null */
760 return 0;
763 void ata_sleep(void)
765 /* null */
768 void ata_spin(void)
770 /* null */
773 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
774 int ata_hard_reset(void)
776 /* null */
777 return 0;
780 int ata_soft_reset(void)
782 /* null */
783 return 0;
786 void ata_enable(bool on)
788 /* null - flash controller is enabled/disabled as needed. */
789 (void)on;
792 int ata_init(void)
794 int i, bank, phys_segment;
795 unsigned char spare_buf[16];
797 if (initialized) return 0;
799 /* Get chip characteristics and number of banks */
800 nand_get_chip_info();
802 for (i = 0; i < MAX_SEGMENTS; i++)
804 lpt_lookup[i].chip = -1;
805 lpt_lookup[i].phys_segment = -1;
806 //lpt_lookup[i].segment_flag = -1;
809 write_caches_in_use = 0;
811 for (i = 0; i < MAX_WRITE_CACHES; i++)
813 int page;
815 write_caches[i].log_segment = -1;
816 write_caches[i].chip = -1;
817 write_caches[i].phys_segment = -1;
819 for (page = 0; page < MAX_PAGES_PER_BLOCK * 4; page++)
821 write_caches[i].page_map[page] = -1;
825 /* Scan banks to build up block translation table */
826 for (bank = 0; bank < total_banks; bank++)
828 for (phys_segment = 0; phys_segment < segments_per_bank; phys_segment++)
830 /* Read spare bytes from first sector of each segment */
831 nand_read_raw(bank, phys_segment_to_page_addr(phys_segment, 0),
832 SECTOR_SIZE, /* offset */
833 16, spare_buf);
835 switch (spare_buf[4]) /* block type */
837 case 0x12:
839 /* Log->Phys Translation table (for Main data area) */
840 //read_lpt_block(bank, phys_segment);
841 break;
844 case 0x13:
845 case 0x17:
847 /* Main data area segment */
848 int segment = (spare_buf[6] << 8) | spare_buf[7];
850 if (segment < MAX_SEGMENTS)
852 /* Store in LPT if not present or 0x17 overrides 0x13 */
853 //if (lpt_lookup[segment].segment_flag == -1 ||
854 // lpt_lookup[segment].segment_flag == 0x13)
856 lpt_lookup[segment].chip = bank;
857 lpt_lookup[segment].phys_segment = phys_segment;
858 //lpt_lookup[segment].segment_flag = spare_buf[4];
861 break;
864 case 0x15:
866 /* Recently-written page data (for Main data area) */
867 read_write_cache_segment(bank, phys_segment);
868 break;
874 initialized = true;
876 #ifdef BOOTLOADER
877 /* TEMP - print out some diagnostics */
878 nand_test();
879 #endif
881 return 0;
885 /* TEMP: This will return junk, it's here for compilation only */
886 unsigned short* ata_get_identify(void)
888 return (unsigned short*)0x21000000; /* Unused DRAM */