1
0
mirror of git://projects.qi-hardware.com/iris.git synced 2024-11-17 06:12:48 +02:00
iris/mips/nand.hhp

324 lines
8.8 KiB
Plaintext
Raw Normal View History

2010-08-22 23:03:06 +03:00
#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 <wijnen@debian.org>
//
// 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 <http://www.gnu.org/licenses/>.
// 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()
2010-08-22 23:03:06 +03:00
// 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
2010-09-02 00:27:14 +03:00
static unsigned size_bits
2010-08-22 23:03:06 +03:00
static unsigned word_size
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 ()
2010-09-09 22:48:35 +03:00
gpio_as_gpio (2, 1 << 30)
gpio_as_input (2, 1 << 30)
gpio_enable_pull (2, 1 << 30)
2010-08-22 23:03:06 +03:00
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 ()
2010-09-02 00:27:14 +03:00
unsigned num_planes_bits = d >> 2 & 3
unsigned plane_bits = 26 + (d >> 4 & 7)
size_bits = plane_bits + num_planes_bits - 3
2010-08-22 23:03:06 +03:00
static bool read (unsigned a, char *buffer):
unsigned column = a & ((1 << page_bits) - 1)
unsigned row = a >> page_bits
//debug ("reading %x:", a)
2010-08-22 23:03:06 +03:00
// 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
unsigned col = (1 << page_bits) + 2
cmd (CMD_READ0)
addr (col)
addr (col >> 8)
addr (row)
addr (row >> 8)
addr (row >> 16)
cmd (CMD_READSTART)
bool valid = false
for unsigned t = 0; t < 3; ++t:
if rdata () == 0:
valid = true
break
if !valid:
//debug ("invalid page for nand read: %x\n", a)
for unsigned i = 0; i < 1 << (9 - 2); ++i:
((unsigned *)buffer)[i] = ~0
2010-08-22 23:03:06 +03:00
return false
col = (1 << page_bits) + 6 + 9 * (column >> 9)
cmd (CMD_RNDOUT)
addr (col)
addr (col >> 8)
cmd (CMD_RNDOUTSTART)
//debug ("parity data:")
2010-08-22 23:03:06 +03:00
for unsigned t = 0; t < 9; ++t:
error[t] = rdata ()
//debug (" %x", error[t] & 0xff)
//debug ("\n")
2010-08-22 23:03:06 +03:00
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
for unsigned t = 0; t < 0x200; ++t:
buffer[t] = rdata ()
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
for unsigned i = 0; i < 0x10; ++i:
if (buffer[i] & 0xff) < 0x10:
//debug (" 0")
2010-08-22 23:03:06 +03:00
else:
//debug (" ")
//debug ("%x", buffer[i] & 0xff)
//debug ("\n")
2010-08-22 23:03:06 +03:00
return true
static void write (unsigned a, char *buffer):
dbgl ()
2010-08-22 23:03:06 +03:00
unsigned row = a >> page_bits
//debug ("writing: %x/%x\n", a, row)
2010-08-22 23:03:06 +03:00
cmd (CMD_SEQIN)
addr (0)
addr (0)
addr (row)
addr (row >> 8)
addr (row >> 16)
char ecc[4][12]
dbgl ()
2010-08-22 23:03:06 +03:00
for unsigned i = 0; i < 0x4; ++i:
dbgl ()
2010-08-22 23:03:06 +03:00
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 ()
dbgl ()
2010-08-22 23:03:06 +03:00
for unsigned t = 0; t < 9; ++t:
ecc[i][t] = ((volatile char *)&EMC_NFPAR (0))[t]
dbgl ()
2010-08-22 23:03:06 +03:00
//debug ("parity for %x:", i * 0x200 + a)
//for unsigned t = 0; t < 9; ++t:
//debug (" %x", ecc[i][t] & 0xff)
//kdebug ("\n")
else:
dbgl ()
2010-08-22 23:03:06 +03:00
for unsigned t = 0; t < 9; ++t:
ecc[i][t] = 0xff
dbgl ()
2010-08-22 23:03:06 +03:00
// 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 unsigned i = 0; i < 6; ++i:
wdata (0)
for unsigned i = 0; i < 4; ++i:
for unsigned j = 0; j < 9; ++j:
wdata (ecc[i][j])
dbgl ()
2010-08-22 23:03:06 +03:00
cmd (CMD_PAGEPROG)
// Wait at least 100 ns.
DELAY ()
dbgl ()
2010-08-22 23:03:06 +03:00
cmd (CMD_READ0)
addr (0)
addr (0)
addr (row)
addr (row >> 8)
addr (row >> 16)
cmd (CMD_READSTART)
dbgl ()
2010-08-22 23:03:06 +03:00
for unsigned i = 0; i < 4; ++i:
dbgl ()
2010-08-22 23:03:06 +03:00
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)
dbgl ()
2010-08-22 23:03:06 +03:00
for unsigned i = 0; i < 6; ++i:
if rdata () != 0:
debug ("extra data not 0 at byte %d\n", i)
dbgl ()
2010-08-22 23:03:06 +03:00
for unsigned i = 0; i < 4; ++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)
dbgl ()
//debug ("nand program %x:", a)
2010-08-22 23:03:06 +03:00
for unsigned i = 0; i < 0x10; ++i:
if (buffer[i] & 0xff) < 0x10:
//debug (" 0")
2010-08-22 23:03:06 +03:00
else:
//debug (" ")
//debug ("%x", buffer[i] & 0xff)
dbgl ()
//debug ("\n")
2010-08-22 23:03:06 +03:00
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)