freemyipod r61 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r60‎ | r61 | r62 >
Date:00:41, 9 August 2010
Author:theseven
Status:new
Tags:
Comment:
Fix some bugs... Mounting NAND on Nano2G succeeds now.
Modified paths:
  • /embios/trunk/target/ipodnano2g/config.h (modified) (history)
  • /embios/trunk/target/ipodnano2g/ftl.c (modified) (history)
  • /embios/trunk/target/ipodnano2g/ls.x (modified) (history)
  • /embios/trunk/target/ipodnano2g/nand.c (modified) (history)
  • /embios/trunk/target/ipodnano2g/nand.h (modified) (history)

Diff [purge]

Index: embios/trunk/target/ipodnano2g/ls.x
@@ -56,7 +56,7 @@
5757 *(.initbss*)
5858 . = ALIGN(0x4);
5959 _initstackstart = .;
60 - . += 0x4000;
 60+ . += 0x400;
6161 _initstackend = .;
6262 _initbssend = .;
6363 } > INIT
Index: embios/trunk/target/ipodnano2g/config.h
@@ -27,7 +27,9 @@
2828
2929 //#define NAND_DEBUG
3030 //#define NAND_TRACE
 31+//#define VFL_DEBUG
3132 //#define VFL_TRACE
 33+//#define FTL_DEBUG
3234 //#define FTL_TRACE
3335
3436
Index: embios/trunk/target/ipodnano2g/nand.c
@@ -82,7 +82,7 @@
8383 uint8_t nand_twp[4];
8484 uint8_t nand_tunk2[4];
8585 uint8_t nand_tunk3[4];
86 -uint32_t nand_type[4];
 86+int nand_type[4];
8787 int nand_powered = 0;
8888 int nand_interleaved = 0;
8989 int nand_cached = 0;
@@ -299,15 +299,15 @@
300300 {
301301 mutex_lock(&nand_mtx, TIMEOUT_BLOCK);
302302 uint32_t result;
303 - if (nand_reset(bank)) return nand_unlock(0xFFFFFFFF);
304 - if (nand_send_cmd(0x90)) return nand_unlock(0xFFFFFFFF);
 303+ if (nand_reset(bank)) return nand_unlock(0xFFFFFFFE);
 304+ if (nand_send_cmd(0x90)) return nand_unlock(0xFFFFFFFD);
305305 FMANUM = 0;
306306 FMADDR0 = 0;
307307 FMCTRL1 = FMCTRL1_DOTRANSADDR;
308 - if (nand_wait_cmddone()) return nand_unlock(0xFFFFFFFF);
 308+ if (nand_wait_cmddone()) return nand_unlock(0xFFFFFFFC);
309309 FMDNUM = 4;
310310 FMCTRL1 = FMCTRL1_DOREADDATA;
311 - if (nand_wait_addrdone()) return nand_unlock(0xFFFFFFFF);
 311+ if (nand_wait_addrdone()) return nand_unlock(0xFFFFFFFB);
312312 result = FMFIFO;
313313 FMCTRL1 = FMCTRL1_CLEARRFIFO;
314314 return nand_unlock(result);
@@ -343,14 +343,9 @@
344344 sleep(50000);
345345 nand_last_activity_value = USEC_TIMER;
346346 for (i = 0; i < 4; i++)
347 - {
348 - if(nand_type[i] != 0xFFFFFFFF)
349 - {
350 - if(nand_reset(i))
351 -
 347+ if (nand_type[i] >= 0)
 348+ if (nand_reset(i))
352349 panicf(PANIC_FATAL, "nand_power_up: nand_reset(bank=%d) failed.", (unsigned int)i);
353 - }
354 - }
355350 nand_powered = 1;
356351 nand_last_activity_value = USEC_TIMER;
357352 mutex_unlock(&nand_mtx);
@@ -505,7 +500,7 @@
506501 {
507502 for (i = 0; i < 4; i++)
508503 {
509 - if (nand_type[i] == 0xFFFFFFFF) continue;
 504+ if (nand_type[i] < 0) continue;
510505 void* databuf = (void*)0;
511506 void* sparebuf = (void*)0;
512507 if (databuffer) databuf = (void*)((uint32_t)databuffer + 0x800 * i);
@@ -522,7 +517,7 @@
523518 nand_last_activity_value = USEC_TIMER;
524519 if (!nand_powered) nand_power_up();
525520 uint8_t status[4];
526 - for (i = 0; i < 4; i++) status[i] = (nand_type[i] == 0xFFFFFFFF);
 521+ for (i = 0; i < 4; i++) status[i] = (nand_type[i] < 0);
527522 for (i = 0; i < 4; i++)
528523 {
529524 if (!status[i])
@@ -616,7 +611,7 @@
617612 + 0x40 * (i - 1))) << 1;
618613 }
619614 for (i = 0; i < 4; i++)
620 - if (nand_type[i] != 0xFFFFFFFF)
 615+ if (nand_type[i] < 0)
621616 rc |= status[i] << (i << 2);
622617 return nand_unlock(rc);
623618 }
@@ -671,7 +666,7 @@
672667 if (!nand_powered) nand_power_up();
673668 for (i = 0; i < 4; i++)
674669 {
675 - if (nand_type[i] == 0xFFFFFFFF) continue;
 670+ if (nand_type[i] < 0) continue;
676671 nand_set_fmctrl0(i, 0);
677672 if (nand_send_cmd(NAND_CMD_BLOCKERASE))
678673 {
@@ -690,7 +685,7 @@
691686 }
692687 for (i = 0; i < 4; i++)
693688 {
694 - if (nand_type[i] == 0xFFFFFFFF) continue;
 689+ if (nand_type[i] < 0) continue;
695690 if (rc & (1 << i)) continue;
696691 if (nand_wait_status_ready(i)) rc |= 1 << i;
697692 }
@@ -699,7 +694,7 @@
700695
701696 const struct nand_device_info_type* nand_get_device_type(uint32_t bank)
702697 {
703 - if (nand_type[bank] == 0xFFFFFFFF)
 698+ if (nand_type[bank] < 0)
704699 return (struct nand_device_info_type*)0;
705700 return &nand_deviceinfotable[nand_type[bank]];
706701 }
@@ -714,7 +709,7 @@
715710 }
716711 }
717712
718 -uint32_t nand_device_init(void)
 713+int nand_device_init(void)
719714 {
720715 mutex_init(&nand_mtx);
721716 wakeup_init(&nand_wakeup);
@@ -726,7 +721,7 @@
727722
728723 /* Assume there are 0 banks, to prevent
729724 nand_power_up from talking with them yet. */
730 - for(i = 0; i < 4; i++) nand_type[i] = 0xFFFFFFFF;
 725+ for (i = 0; i < 4; i++) nand_type[i] = -1;
731726 nand_power_up();
732727
733728 /* Now that the flash is powered on, detect how
@@ -738,7 +733,11 @@
739734 nand_tunk2[i] = 7;
740735 nand_tunk3[i] = 7;
741736 type = nand_get_chip_type(i);
742 - if (type == 0xFFFFFFFF) continue;
 737+ if (type >= 0xFFFFFFF0)
 738+ {
 739+ nand_type[i] = (int)type;
 740+ continue;
 741+ }
743742 for (j = 0; ; j++)
744743 {
745744 if (j == ARRAYLEN(nand_deviceinfotable)) break;
@@ -753,7 +752,7 @@
754753 nand_tunk2[i] = nand_deviceinfotable[nand_type[i]].tunk2;
755754 nand_tunk3[i] = nand_deviceinfotable[nand_type[i]].tunk3;
756755 }
757 - if (nand_type[0] == 0xFFFFFFFF) return 1;
 756+ if (nand_type[0] < 0) return nand_type[0];
758757 nand_interleaved = ((nand_type[0] >> 22) & 1);
759758 nand_cached = ((nand_type[0] >> 23) & 1);
760759
Index: embios/trunk/target/ipodnano2g/ftl.c
@@ -576,7 +576,7 @@
577577 retries until it works or all available pages have been tried */
578578 uint32_t ftl_vfl_commit_cxt(uint32_t bank)
579579 {
580 - DEBUGF("FTL: VFL: Committing context on bank %d\n", bank);
 580+ DEBUGF("FTL: VFL: Committing context on bank %d", bank);
581581 if (ftl_vfl_cxt[bank].nextcxtpage + 8 <= ftl_nand_type->pagesperblock)
582582 if (ftl_vfl_store_cxt(bank) == 0) return 0;
583583 uint32_t current = ftl_vfl_cxt[bank].activecxtblock;
@@ -595,7 +595,7 @@
596596 ftl_vfl_cxt[bank].nextcxtpage = 0;
597597 if (ftl_vfl_store_cxt(bank) == 0) return 0;
598598 }
599 - panicf(PANIC_FATAL, "VFL: Failed to commit VFL CXT!\n");
 599+ panicf(PANIC_FATAL, "VFL: Failed to commit VFL CXT!");
600600 return 1;
601601 }
602602 #endif
@@ -679,7 +679,7 @@
680680 for (spareindex = 0; spareindex < spareused; spareindex++)
681681 if (ftl_vfl_cxt[bank].remaptable[spareindex] == block)
682682 {
683 - DEBUGF("FTL: VFL: Following remapped block: %d => %d\n",
 683+ DEBUGF("FTL: VFL: Following remapped block: %d => %d",
684684 block, ftl_vfl_cxt[bank].firstspare + spareindex);
685685 return ftl_vfl_cxt[bank].firstspare + spareindex;
686686 }
@@ -822,7 +822,7 @@
823823 uint32_t checkempty, uint32_t remaponfail)
824824 {
825825 #ifdef VFL_TRACE
826 - DEBUGF("FTL: VFL: Reading page %d\n", vpage);
 826+ DEBUGF("FTL: VFL: Reading page %d", vpage);
827827 #endif
828828
829829 uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
@@ -854,7 +854,7 @@
855855 #else
856856 if (remaponfail == 1 &&(ret & 0x11D) != 0 && (ret & 2) == 0)
857857 {
858 - DEBUGF("FTL: VFL: Scheduling vBlock %d for remapping!\n", block);
 858+ DEBUGF("FTL: VFL: Scheduling vBlock %d for remapping!", block);
859859 ftl_vfl_schedule_block_for_remap(bank, block);
860860 }
861861 #endif
@@ -870,7 +870,7 @@
871871 uint32_t checkempty, uint32_t remaponfail)
872872 {
873873 #ifdef VFL_TRACE
874 - DEBUGF("FTL: VFL: Fast reading page %d on all banks\n", vpage);
 874+ DEBUGF("FTL: VFL: Fast reading page %d on all banks", vpage);
875875 #endif
876876
877877 uint32_t i, rc = 0;
@@ -947,7 +947,7 @@
948948 {
949949 uint32_t i, j;
950950 #ifdef VFL_TRACE
951 - DEBUGF("FTL: VFL: Writing page %d\n", vpage);
 951+ DEBUGF("FTL: VFL: Writing page %d", vpage);
952952 #endif
953953
954954 uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
@@ -1139,7 +1139,7 @@
11401140 return 1;
11411141 }
11421142
1143 - DEBUGF("FTL: Found FTL context block: vBlock %d\n", ftlcxtblock);
 1143+ DEBUGF("FTL: Found FTL context block: vBlock %d", ftlcxtblock);
11441144 uint32_t ftlcxtfound = 0;
11451145 for (i = ftl_nand_type->pagesperblock * ftl_banks - 1; i > 0; i--)
11461146 {
@@ -1155,9 +1155,9 @@
11561156 else
11571157 {
11581158 /* This will trip if there was an unclean unmount before. */
1159 - DEBUGF("FTL: Unclean shutdown before!\n");
 1159+ DEBUGF("FTL: Unclean shutdown before!");
11601160 #ifdef FTL_FORCEMOUNT
1161 - DEBUGF("FTL: Forcing mount nevertheless...\n");
 1161+ DEBUGF("FTL: Forcing mount nevertheless...");
11621162 #else
11631163 break;
11641164 #endif
@@ -1170,7 +1170,7 @@
11711171 return 1;
11721172 }
11731173
1174 - DEBUGF("FTL: Successfully read FTL context block\n");
 1174+ DEBUGF("FTL: Successfully read FTL context block");
11751175 uint32_t pagestoread = ftl_nand_type->userblocks >> 10;
11761176 if ((ftl_nand_type->userblocks & 0x1FF) != 0) pagestoread++;
11771177
@@ -1232,28 +1232,28 @@
12331233 uint8_t bbtentry = ftl_bbt[i][j];
12341234 for (k = 0; k < 8; k++) if ((bbtentry & (1 << k)) == 0) badblocks++;
12351235 }
1236 - DEBUGF("FTL: BBT for bank %d: %d bad blocks\n", i, badblocks);
 1236+ DEBUGF("FTL: BBT for bank %d: %d bad blocks", i, badblocks);
12371237 badblocks = 0;
12381238 #endif
12391239 for (j = 0; j < ftl_vfl_cxt[i].sparecount; j++)
12401240 if (ftl_vfl_cxt[i].remaptable[j] == 0xFFFF) badblocks++;
1241 - DEBUGF("FTL: VFL: Bank %d: %d of %d spare blocks are bad\n",
 1241+ DEBUGF("FTL: VFL: Bank %d: %d of %d spare blocks are bad",
12421242 i, badblocks, ftl_vfl_cxt[i].sparecount);
1243 - DEBUGF("FTL: VFL: Bank %d: %d blocks remapped\n",
 1243+ DEBUGF("FTL: VFL: Bank %d: %d blocks remapped",
12441244 i, ftl_vfl_cxt[i].spareused);
1245 - DEBUGF("FTL: VFL: Bank %d: %d blocks scheduled for remapping\n",
 1245+ DEBUGF("FTL: VFL: Bank %d: %d blocks scheduled for remapping",
12461246 i, 0x334 - ftl_vfl_cxt[i].scheduledstart);
12471247 }
12481248 #ifndef FTL_READONLY
12491249 uint32_t min = 0xFFFFFFFF, max = 0, total = 0;
1250 - for (i = 0; i < (*ftl_nand_type).userBlocks + 23; i++)
 1250+ for (i = 0; i < (*ftl_nand_type).userblocks + 23; i++)
12511251 {
12521252 if (ftl_erasectr[i] > max) max = ftl_erasectr[i];
12531253 if (ftl_erasectr[i] < min) min = ftl_erasectr[i];
12541254 total += ftl_erasectr[i];
12551255 }
1256 - DEBUGF("FTL: Erase counters: Minimum: %d, maximum %d, average: %d, total: %d\n",
1257 - min, max, total / ((*ftl_nand_type).userBlocks + 23), total);
 1256+ DEBUGF("FTL: Erase counters: Minimum: %d, maximum %d, average: %d, total: %d",
 1257+ min, max, total / ((*ftl_nand_type).userblocks + 23), total);
12581258 #endif
12591259 #endif
12601260
@@ -1284,12 +1284,12 @@
12851285 uint32_t error = 0;
12861286
12871287 #ifdef FTL_TRACE
1288 - DEBUGF("FTL: Reading %d sectors starting at %d\n", count, sector);
 1288+ DEBUGF("FTL: Reading %d sectors starting at %d", count, sector);
12891289 #endif
12901290
12911291 if (sector + count > ftl_nand_type->userblocks * ppb)
12921292 {
1293 - DEBUGF("FTL: Sector %d is out of range!\n", sector + count - 1);
 1293+ DEBUGF("FTL: Sector %d is out of range!", sector + count - 1);
12941294 return 1;
12951295 }
12961296 if (count == 0) return 0;
@@ -1307,13 +1307,13 @@
13081308 if (logentry != (struct ftl_log_type*)0)
13091309 {
13101310 #ifdef FTL_TRACE
1311 - DEBUGF("FTL: Block %d has a log entry\n", block);
 1311+ DEBUGF("FTL: Block %d has a log entry", block);
13121312 #endif
13131313 if (logentry->scatteredvblock != 0xFFFF
13141314 && logentry->pageoffsets[page] != 0xFFFF)
13151315 {
13161316 #ifdef FTL_TRACE
1317 - DEBUGF("FTL: Found page %d at block %d, page %d\n", page,
 1317+ DEBUGF("FTL: Found page %d at block %d, page %d", page,
13181318 (*logentry).scatteredvblock, (*logentry).pageoffsets[page]);
13191319 #endif
13201320 abspage = logentry->scatteredvblock * ppb
@@ -1336,7 +1336,7 @@
13371337 memset(&((uint8_t*)buffer)[(i + j) << 11], 0, 0x800);
13381338 else if ((ret & (0xd << (j << 2))) || ftl_sparebuffer[j].user.eccmark != 0xFF)
13391339 {
1340 - DEBUGF("FTL: Error while reading sector %d!\n", (sector + i));
 1340+ DEBUGF("FTL: Error while reading sector %d!", (sector + i));
13411341 error = 1;
13421342 memset(&((uint8_t*)buffer)[(i + j) << 11], 0, 0x800);
13431343 }
@@ -1349,7 +1349,7 @@
13501350 if (ret & 2) memset(&((uint8_t*)buffer)[i << 11], 0, 0x800);
13511351 else if ((ret & 0x11D) != 0 || ftl_sparebuffer[0].user.eccmark != 0xFF)
13521352 {
1353 - DEBUGF("FTL: Error while reading sector %d!\n", (sector + i));
 1353+ DEBUGF("FTL: Error while reading sector %d!", (sector + i));
13541354 error = 1;
13551355 memset(&((uint8_t*)buffer)[i << 11], 0, 0x800);
13561356 }
@@ -1523,7 +1523,7 @@
15241524 if (newblock == 0xFFFFFFFF) return 1;
15251525 ftl_cxt.ftlctrlblocks[i] = newblock;
15261526 ftl_cxt.ftlctrlpage = newblock * ppb;
1527 - DEBUGF("Starting new FTL control block at %d\n", ftl_cxt.ftlctrlpage);
 1527+ DEBUGF("Starting new FTL control block at %d", ftl_cxt.ftlctrlpage);
15281528 uint32_t pagestoread = (ftl_nand_type->userblocks + 23) >> 10;
15291529 if (((ftl_nand_type->userblocks + 23) & 0x1FF) != 0) pagestoread++;
15301530 for (i = 0; i < pagestoread; i++)
@@ -1855,7 +1855,7 @@
18561856 uint32_t mappages = (ftl_nand_type->userblocks + 0x3ff) >> 10;
18571857 uint32_t ctrpages = (ftl_nand_type->userblocks + 23 + 0x3ff) >> 10;
18581858 uint32_t endpage = ftl_cxt.ftlctrlpage + mappages + ctrpages + 1;
1859 - DEBUGF("FTL: Committing context\n");
 1859+ DEBUGF("FTL: Committing context");
18601860 if (endpage >= (ftl_cxt.ftlctrlpage / ppb + 1) * ppb)
18611861 ftl_cxt.ftlctrlpage |= ppb - 1;
18621862 for (i = 0; i < ctrpages; i++)
@@ -1882,7 +1882,7 @@
18831883 ftl_sparebuffer[0].meta.type = 0x43;
18841884 if (ftl_vfl_write(ftl_cxt.ftlctrlpage, 1, &ftl_cxt, &ftl_sparebuffer[0]) != 0)
18851885 return 1;
1886 - DEBUGF("FTL: Wrote context to page %d\n", ftl_cxt.ftlctrlpage);
 1886+ DEBUGF("FTL: Wrote context to page %d", ftl_cxt.ftlctrlpage);
18871887 return 0;
18881888 }
18891889 #endif
@@ -1939,12 +1939,12 @@
19401940 uint32_t ppb = ftl_nand_type->pagesperblock * ftl_banks;
19411941
19421942 #ifdef FTL_TRACE
1943 - DEBUGF("FTL: Writing %d sectors starting at %d\n", count, sector);
 1943+ DEBUGF("FTL: Writing %d sectors starting at %d", count, sector);
19441944 #endif
19451945
19461946 if (sector + count > ftl_nand_type->userblocks * ppb)
19471947 {
1948 - DEBUGF("FTL: Sector %d is out of range!\n", sector + count - 1);
 1948+ DEBUGF("FTL: Sector %d is out of range!", sector + count - 1);
19491949 return 1;
19501950 }
19511951 if (count == 0) return 0;
@@ -1955,7 +1955,7 @@
19561956 {
19571957 for (i = 0; i < 3; i++)
19581958 {
1959 - DEBUGF("FTL: Marking dirty, try %d\n", i);
 1959+ DEBUGF("FTL: Marking dirty, try %d", i);
19601960 if (ftl_next_ctrl_pool_page() != 0)
19611961 {
19621962 mutex_unlock(&ftl_mtx);
@@ -1974,7 +1974,7 @@
19751975 mutex_unlock(&ftl_mtx);
19761976 return 1;
19771977 }
1978 - DEBUGF("FTL: Wrote dirty mark to %d\n", ftl_cxt.ftlctrlpage);
 1978+ DEBUGF("FTL: Wrote dirty mark to %d", ftl_cxt.ftlctrlpage);
19791979 ftl_cxt.clean_flag = 0;
19801980 }
19811981
@@ -1992,7 +1992,7 @@
19931993 if (page == 0 && count - i >= ppb)
19941994 {
19951995 #ifdef FTL_TRACE
1996 - DEBUGF("FTL: Going to write a full hyperblock in one shot\n");
 1996+ DEBUGF("FTL: Going to write a full hyperblock in one shot");
19971997 #endif
19981998 uint32_t vblock = logentry->scatteredvblock;
19991999 logentry->scatteredvblock = 0xFFFF;
@@ -1999,7 +1999,7 @@
20002000 if (logentry->pagesused != 0)
20012001 {
20022002 #ifdef FTL_TRACE
2003 - DEBUGF("FTL: Scattered block had some pages already used, committing\n");
 2003+ DEBUGF("FTL: Scattered block had some pages already used, committing");
20042004 #endif
20052005 ftl_release_pool_block(vblock);
20062006 vblock = ftl_allocate_pool_block();
@@ -2041,7 +2041,7 @@
20422042 if (logentry->pagesused == ppb)
20432043 {
20442044 #ifdef FTL_TRACE
2045 - DEBUGF("FTL: Scattered block is full, committing\n");
 2045+ DEBUGF("FTL: Scattered block is full, committing");
20462046 #endif
20472047 ftl_remove_scattered_block(logentry);
20482048 logentry = ftl_allocate_log_entry(block);
@@ -2119,7 +2119,7 @@
21202120 if (ftl_cxt.clean_flag == 1) return 0;
21212121
21222122 #ifdef FTL_TRACE
2123 - DEBUGF("FTL: Syncing\n");
 2123+ DEBUGF("FTL: Syncing");
21242124 #endif
21252125
21262126 if (ftl_cxt.swapcounter >= 20)
@@ -2165,8 +2165,9 @@
21662166 uint32_t i;
21672167 uint32_t result = 0;
21682168 uint32_t foundsignature, founddevinfo, blockwiped, repaired, skip;
2169 - if (nand_device_init() != 0) //return 1;
2170 - panicf(PANIC_FATAL, "FTL: Lowlevel NAND driver init failed!");
 2169+ int rc;
 2170+ if ((rc = nand_device_init()) != 0) //return 1;
 2171+ panicf(PANIC_FATAL, "FTL: Lowlevel NAND driver init failed: %d", rc);
21712172 ftl_banks = 0;
21722173 for (i = 0; i < 4; i++)
21732174 if (nand_get_device_type(i) != 0) ftl_banks = i + 1;
Index: embios/trunk/target/ipodnano2g/nand.h
@@ -56,7 +56,7 @@
5757
5858 const struct nand_device_info_type* nand_get_device_type(uint32_t bank);
5959 uint32_t nand_reset(uint32_t bank);
60 -uint32_t nand_device_init(void);
 60+int nand_device_init(void);
6161 void nand_set_active(void);
6262 long nand_last_activity(void);
6363 void nand_power_up(void);