Updated TCC780x NAND driver. Still work-in-progress but lots better than the previous...
[kugel-rb.git] / firmware / target / arm / tcc780x / ata-nand-tcc780x.c
blobf6d1df96ce931f0cbd444cf425d1ce0ce5712195
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 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)
59 /* NFC_CTRL flags */
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)
75 /* ECC_CTRL flags */
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. */
103 #ifdef COWON_D2
104 #define MAX_BANKS 2
105 #else
106 #define MAX_BANKS 4
107 #endif
109 #define MAX_SEGMENTS (MAX_BLOCKS_PER_BANK * MAX_BANKS / 4)
111 /* Logical/Physical translation table */
113 struct lpt_entry
115 short chip;
116 short phys_segment;
117 //short segment_flag;
119 static struct lpt_entry lpt_lookup[MAX_SEGMENTS];
121 /* Write Caches */
123 #define MAX_WRITE_CACHES 8
125 struct write_cache
127 short chip;
128 short phys_segment;
129 short log_segment;
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;
136 /* Read buffer */
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;
147 if (page_in_seg & 1)
149 /* Data is located in block+1 */
150 page_addr += pages_per_block;
153 if (page_in_seg & 2)
155 /* Data is located in second plane */
156 page_addr += (blocks_per_bank/2) * pages_per_block;
159 page_addr += page_in_seg/4;
161 return page_addr;
165 /* NAND physical access functions */
167 static void nand_chip_select(int chip)
169 if (chip == -1)
171 /* Disable both chip selects */
172 GPIOB_CLEAR = (1<<21);
173 NFC_CTRL |= NFC_CS0 | NFC_CS1;
175 else
177 /* NFC chip select */
178 if (chip & 1)
180 NFC_CTRL &= ~NFC_CS0;
181 NFC_CTRL |= NFC_CS1;
183 else
185 NFC_CTRL |= NFC_CS0;
186 NFC_CTRL &= ~NFC_CS1;
189 /* Secondary chip select */
190 if (chip & 2)
192 GPIOB_SET = (1<<21);
194 else
196 GPIOB_CLEAR = (1<<21);
202 static void nand_read_id(int chip, unsigned char* id_buf)
204 int i;
206 /* Enable NFC bus clock */
207 BCLKCTR |= DEV_NAND;
209 /* Reset NAND controller */
210 NFC_RST = 0;
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);
220 /* Reset command */
221 NFC_CMD = 0xFF;
223 /* Set 8-bit data width */
224 NFC_CTRL &= ~NFC_16BIT;
226 /* Read ID command, single address cycle */
227 NFC_CMD = 0x90;
228 NFC_SADDR = 0x00;
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)
245 int i;
247 /* Enable NFC bus clock */
248 BCLKCTR |= DEV_NAND;
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 */
256 GPIOB_CLEAR = 1<<19;
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. */
263 NFC_CMD = 0x30;
264 NFC_CMD = 0x65;
266 /* Read command */
267 NFC_CMD = 0x00;
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;
273 /* End of read */
274 NFC_CMD = 0x30;
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 */
286 NFC_CMD = 0xFF;
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)
297 int i;
299 /* Enable NFC bus clock */
300 BCLKCTR |= DEV_NAND;
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;
313 /* Read command */
314 NFC_CMD = 0x00;
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;
327 row = row >> 8;
330 /* End of read command */
331 NFC_CMD = 0x30;
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;
346 else
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)
364 bool found = false;
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];
373 switch (manuf_id)
375 case 0xEC: /* SAMSUNG */
377 switch(id_buf[1]) /* Chip Id */
379 case 0xD5: /* K9LAG08UOM */
381 page_size = 2048;
382 spare_size = 64;
383 pages_per_block = 128;
384 blocks_per_bank = 8192;
385 col_cycles = 2;
386 row_cycles = 3;
388 found = true;
389 break;
391 case 0xD7: /* K9LBG08UOM */
393 page_size = 4096;
394 spare_size = 128;
395 pages_per_block = 128;
396 blocks_per_bank = 8192;
397 col_cycles = 2;
398 row_cycles = 3;
400 found = true;
401 break;
403 break;
406 if (!found)
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 */
438 total_banks = 2;
440 else
442 /* UIDs differ, assume banks 2/3 are valid */
443 total_banks = 4;
446 else
448 /* Bank 2 returned differing id - assume 2/3 are junk */
449 total_banks = 2;
452 else
454 /* Bank 1 returned differing id - assume it is junk */
455 total_banks = 1;
459 Sanity checks:
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 */
469 0, /* page */
470 page_size, /* offset */
471 8, id_buf);
473 if (strncmp(id_buf, "BMP", 3)) panicf("BMP tag not present");
475 if (total_banks > 1)
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),
487 SECTOR_SIZE, buf);
489 /* TODO: Read the 16 spare bytes, perform ECC correction */
491 return true;
495 static bool nand_read_sector_of_phys_segment(int chip, int phys_segment,
496 int page_in_seg, int sector,
497 void* buf)
499 int page_addr = phys_segment_to_page_addr(phys_segment,
500 page_in_seg);
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,
507 void* buf)
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. */
518 int cache_num = 0;
519 bool found = false;
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)
526 found = true;
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];
531 else
533 cache_num++;
537 return nand_read_sector_of_phys_segment(chip, phys_segment,
538 page_in_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 */
547 bool cont = true;
549 struct lpt_entry* lpt_ptr = NULL;
551 while (cont && page < pages_per_block)
553 int i = 0;
555 nand_read_sector_of_phys_segment(chip, phys_segment,
556 page, 0, /* only sector 0 is used */
557 page_buf);
559 /* Find out which chunk of the LPT table this section contains.
560 Do this by reading the logical segment number of entry 0 */
561 if (lpt_ptr == NULL)
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 */
571 16, spare_buf);
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);
580 #endif
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;
590 lpt_ptr++;
591 i++;
593 else cont = false;
595 page++;
599 #endif
602 static void read_write_cache_segment(int chip, int phys_segment)
604 int page;
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 */
622 16, spare_buf);
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 */
639 #ifdef BOOTLOADER
641 #if 0
642 static void display_page(int chip, int page)
644 int i;
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;
650 line = 1;
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;
658 if (interesting)
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()) {};
668 reset_screen();
672 #endif
674 static void nand_test(void)
676 int segment = 0;
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)
685 segment++;
687 printf("%d sequential segments found (%dMb)",
688 segment, (unsigned)(segment*bytes_per_segment)>>20);
690 #endif
693 /* API Functions */
695 void ata_led(bool onoff)
697 led(onoff);
700 int ata_read_sectors(IF_MV2(int drive,) unsigned long start, int incount,
701 void* inbuf)
703 #ifdef HAVE_MULTIVOLUME
704 (void)drive; /* unused for now */
705 #endif
706 mutex_lock(&ata_mtx);
708 while (incount > 0)
710 int done = 0;
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);
719 return -1;
722 inbuf += SECTOR_SIZE;
723 incount--;
724 secmod++;
725 done++;
728 if (done < 0)
730 mutex_unlock(&ata_mtx);
731 return -1;
733 start += done;
736 mutex_unlock(&ata_mtx);
737 return 0;
740 int ata_write_sectors(IF_MV2(int drive,) unsigned long start, int count,
741 const void* outbuf)
743 #warning function not implemented
744 (void)start;
745 (void)count;
746 (void)outbuf;
747 return -1;
750 void ata_spindown(int seconds)
752 /* null */
753 (void)seconds;
756 bool ata_disk_is_active(void)
758 /* null */
759 return 0;
762 void ata_sleep(void)
764 /* null */
767 void ata_spin(void)
769 /* null */
772 /* Hardware reset protocol as specified in chapter 9.1, ATA spec draft v5 */
773 int ata_hard_reset(void)
775 /* null */
776 return 0;
779 int ata_soft_reset(void)
781 /* null */
782 return 0;
785 void ata_enable(bool on)
787 /* null - flash controller is enabled/disabled as needed. */
788 (void)on;
791 int ata_init(void)
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++)
812 int page;
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 */
832 16, spare_buf);
834 switch (spare_buf[4]) /* block type */
836 case 0x12:
838 /* Log->Phys Translation table (for Main data area) */
839 //read_lpt_block(bank, phys_segment);
840 break;
843 case 0x13:
844 case 0x17:
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];
860 break;
863 case 0x15:
865 /* Recently-written page data (for Main data area) */
866 read_write_cache_segment(bank, phys_segment);
867 break;
873 initialized = true;
875 #ifdef BOOTLOADER
876 /* TEMP - print out some diagnostics */
877 nand_test();
878 #endif
880 return 0;
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 */