#pypp 0 // Iris: micro-kernel for a capability-based operating system. // mips/nand.hhp: NAND driver functions, split off to be used in two places. // The functions are not inline, so this file must be included exactly once per executable that needs it. // Copyright 2009 Bas Wijnen // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // The following defines are taken from mtd/nand.h in the Linux source. // Everything not in the nand datasheet is thrown out. //#define dbgl() debug ("%s:%d\n", __PRETTY_FUNCTION__, __LINE__) #define dbgl() // Standard NAND flash commands #define CMD_READ0 0 #define CMD_READSTART 0x30 #define CMD_READID 0x90 #define CMD_RESET 0xff #define CMD_SEQIN 0x80 #define CMD_PAGEPROG 0x10 #define CMD_ERASE1 0x60 #define CMD_ERASE2 0xd0 #define CMD_RNDIN 0x85 #define CMD_RNDOUT 5 #define CMD_RNDOUTSTART 0xe0 #define CMD_STATUS 0x70 // Status bits #define STATUS_FAIL 0x01 #define STATUS_READY 0x40 #define STATUS_WRITABLE 0x80 static volatile char *command static volatile char *address static volatile char *data static unsigned page_bits static unsigned redundant_bits static unsigned block_bits static unsigned size_bits static unsigned word_size static unsigned ecc_start static void unbusy (): while !(gpio_get_port (2) & (1 << 30)): DELAY () static void addr (unsigned d): unbusy () *address = d static void cmd (unsigned d): unbusy () *command = d static void wdata (unsigned d): unbusy () *data = d static unsigned rdata (): unbusy () unsigned ret = *data return ret static void reset (): // Set up. gpio_as_nand () gpio_as_gpio (2, 1 << 30) gpio_as_input (2, 1 << 30) gpio_enable_pull (2, 1 << 30) EMC_NFCSR = EMC_NFCSR_NFE1 | EMC_NFCSR_NFCE1 // Reset nand. cmd (CMD_RESET) cmd (CMD_READID) addr (0) unsigned d = rdata () //unsigned maker = d d = rdata () //unsigned device = d d = rdata () //unsigned internal_chip_number = 1 << (d & 0x3) //unsigned cell_type = 2 << ((d >> 2) & 0x3) //unsigned simultaneously_programmed_pages = 1 << ((d >> 4) & 0x3) //bool can_interleave_program_between_chips = d & 0x40 //bool can_cache_program = d & 0x80 d = rdata () page_bits = 10 + (d & 3) debug ("page bits: %d\n", page_bits) redundant_bits = (d & 4 ? 4 : 3) debug ("redundant bits: %d\n", redundant_bits) block_bits = 16 + ((d >> 4) & 3) debug ("block bits: %d\n", block_bits) word_size = (d & 0x40 ? 16 : 8) debug ("word size: %d\n", word_size) //unsigned serial_access_minimum = (d & 0x80 ? 25 : 50) d = rdata () unsigned num_planes_bits = d >> 2 & 3 unsigned plane_bits = 26 + (d >> 4 & 7) size_bits = plane_bits + num_planes_bits - 3 if page_bits == 11: ecc_start = 6 else if page_bits == 12: ecc_start = 12 else: debug ("unknown nand type; assuming ecc offset to be 6. This is probably wrong!\n") ecc_start = 6 static bool read (unsigned a, char *buffer): unsigned column = a & ((1 << page_bits) - 1) unsigned row = a >> page_bits //debug ("reading %x:", a) // Read oob information first. char error[12] // Spare space (starts at 1 << page_bits) // 0: unused // 2: detect valid data (at least 1 byte == 0 means valid) // 5: unused // 6: 9-byte ecc of 1st 512 bytes // 15: 9-byte ecc of 2nd 512 bytes // 24: 9-byte ecc of 3rd 512 bytes // 33: 9-byte ecc of 4th 512 bytes // 42: unused // 64: end of space // For 2 GB nand: // 0-11: unused/bad block markers // 12-89: 8 times 9-byte ecc // 90-127: unused // 128: end of space unsigned col = (1 << page_bits) + ecc_start + 9 * (column >> 9) cmd (CMD_READ0) addr (col) addr (col >> 8) addr (row) addr (row >> 8) addr (row >> 16) cmd (CMD_READSTART) //debug ("parity data:") bool parity_all_ff = true for unsigned t = 0; t < 9; ++t: error[t] = rdata () if parity_all_ff && (error[t] & 0xff) != 0xff: parity_all_ff = false //debug (" %x", error[t] & 0xff) //debug ("\n") cmd (CMD_RNDOUT) addr (column) addr (column >> 8) cmd (CMD_RNDOUTSTART) EMC_NFINTS = 0 EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_ERST bool all_ff = true for unsigned t = 0; t < 0x200; ++t: buffer[t] = rdata () if all_ff && (buffer[t] & 0xff) != 0xff: all_ff = false if !all_ff || !parity_all_ff: for unsigned t = 0; t < 9; ++t: ((volatile char *)&EMC_NFPAR (0))[t] = error[t] EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_PRDY while !(EMC_NFINTS & EMC_NFINTS_DECF): DELAY () unsigned ints = EMC_NFINTS if ints & EMC_NFINTS_UNCOR: debug ("uncorrectable error in nand at %x\n", a) return false unsigned errs = (ints & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT for unsigned i = 0; i < errs; ++i: unsigned err = EMC_NFERR (i) unsigned index = (err >> 16) - 1 unsigned mask = err & 0x1ff unsigned bit = index * 9 unsigned offset= bit & 7 unsigned byte = bit / 8 debug ("correcting %x on %x+%d\n", mask, byte, offset) unsigned data = buffer[byte] | buffer[byte + 1] << 8 data ^= mask << offset buffer[byte] = data buffer[byte + 1] = data >> 8 #if 0 for unsigned i = 0; i < 0x10; ++i: if (buffer[i] & 0xff) < 0x10: debug (" 0") else: debug (" ") debug ("%x", buffer[i] & 0xff) debug ("\n") #endif return true static void write (unsigned a, char *buffer): unsigned row = a >> page_bits //debug ("writing: %x/%x\n", a, row) cmd (CMD_SEQIN) addr (0) addr (0) addr (row) addr (row >> 8) addr (row >> 16) char ecc[8][12] for unsigned i = 0; i < 1 << (page_bits - 9); ++i: bool all_ff = true EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_ENCODING | EMC_NFECR_ERST //debug ("writing data from %x\n", (unsigned)buffer + i * 0x200) for unsigned j = 0; j < 0x200; ++j: wdata (buffer[i * 0x200 + j]) if all_ff && (buffer[i * 0x200 + j] & 0xff) != 0xff: all_ff = false if !all_ff: while !(EMC_NFINTS & EMC_NFINTS_ENCF): DELAY () for unsigned t = 0; t < 9; ++t: ecc[i][t] = ((volatile char *)&EMC_NFPAR (0))[t] //debug ("parity for %x:", i * 0x200 + a) //for unsigned t = 0; t < 9; ++t: //debug (" %x", ecc[i][t] & 0xff) //kdebug ("\n") else: for unsigned t = 0; t < 9; ++t: ecc[i][t] = 0xff // Spare space (starts at 1 << page_bits) // 0: unused // 2: detect valid data (at least 1 byte == 0 means valid) // 5: unused // 6: 9-byte ecc of 1st 512 bytes // 15: 9-byte ecc of 2nd 512 bytes // 24: 9-byte ecc of 3rd 512 bytes // 33: 9-byte ecc of 4th 512 bytes // 42: unused // 64: end of space // For 2 GB nand: // 0-11: unused/bad block markers // 12-89: 8 times 9-byte ecc // 90-127: unused // 128: end of space for unsigned i = 0; i < ecc_start; ++i: wdata (i == 4 ? 0 : 0xff) for unsigned i = 0; i < 1 << (page_bits - 9); ++i: for unsigned j = 0; j < 9; ++j: wdata (ecc[i][j]) cmd (CMD_PAGEPROG) // Wait at least 100 ns. DELAY () cmd (CMD_READ0) addr (0) addr (0) addr (row) addr (row >> 8) addr (row >> 16) cmd (CMD_READSTART) for unsigned i = 0; i < 1 << (page_bits - 9); ++i: EMC_NFINTS = 0 EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_ERST for unsigned t = 0; t < 0x200; ++t: unsigned r = rdata () & 0xff if r != (buffer[i * 0x200 + t] & 0xff): debug ("program error at %x: %x != %x\n", i * 0x200 + t, buffer[i * 0x200 + t] & 0xff, r) for unsigned t = 0; t < 9; ++t: ((volatile char *)&EMC_NFPAR (0))[t] = ecc[i][t] EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_PRDY while !(EMC_NFINTS & EMC_NFINTS_DECF): DELAY () unsigned ints = EMC_NFINTS if ints & EMC_NFINTS_UNCOR: debug ("uncorrectable error during verify\n") continue unsigned errs = (ints & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT for unsigned i = 0; i < errs; ++i: unsigned err = EMC_NFERR (i) unsigned index = (err >> 16) - 1 unsigned mask = err & 0x1ff unsigned bit = index * 9 unsigned offset= bit & 7 unsigned byte = bit / 8 debug ("error detected by parity: %x on %x+%d\n", mask, byte, offset) for unsigned i = 0; i < ecc_start; ++i: if rdata () != (i == 4 ? 0 : 0xff): debug ("incorrect extra data at byte %d\n", i) for unsigned i = 0; i < 1 << (page_bits - 9); ++i: for unsigned j = 0; j < 9; ++j: unsigned r = rdata () & 0xff if r != (ecc[i][j] & 0xff): debug ("ecc doesn't match: %x != %x\n", r, ecc[i][j] & 0xff) //debug ("nand program %x:", a) #if 0 for unsigned i = 0; i < 0x10; ++i: if (buffer[i] & 0xff) < 0x10: debug (" 0") else: debug (" ") debug ("%x", buffer[i] & 0xff) debug ("\n") #endif static void erase (unsigned a): unsigned row = a >> page_bits cmd (CMD_ERASE1) addr (row) addr (row >> 8) addr (row >> 16) cmd (CMD_ERASE2) //debug ("nand erase %d done\n", a)