1
0
mirror of git://projects.qi-hardware.com/iris.git synced 2025-04-21 12:27:27 +03:00

booting from nand works

This commit is contained in:
Bas Wijnen
2010-08-22 22:03:06 +02:00
parent e4151e737b
commit 04ed743042
13 changed files with 564 additions and 481 deletions

View File

@@ -15,50 +15,54 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
start_load = 0xa0600000
start_load = 0x80600000
load = 0x80000000
UDC_BOOT = comment this out for nand boot
ifdef UDC_BOOT
boot_threads = $(standard_boot_programs) $(udc_boot_programs)
threadlist = mips/nanonote/threadlist-udc
ARCH_CXXFLAGS = -DNUM_THREADS=2
all: mips/nanonote/nand-boot.raw test
else
boot_threads = $(standard_boot_programs) $(sd_boot_programs)
threadlist = mips/nanonote/threadlist-sd
ARCH_CXXFLAGS = -DNUM_THREADS=4
all: mips/nanonote/nand-boot.raw iris-sd.tar
iris-sd.tar: $(addprefix fs/,$(addsuffix .elf,$(programs))) mips/start.raw.gz fs/init.config
mkimage -A mips -T kernel -a $(start_load) -e a$(shell /bin/sh -c '$(OBJDUMP) -t mips/start.elf | grep __start$$ | cut -b2-8') -n Iris -d mips/start.raw.gz fs/uimage | sed -e 's/:/;/g'
cd fs && tar cvf ../$@ uimage init.config $(addsuffix .elf,$(programs)) --dereference
endif
ARCH_CPPFLAGS = -I. -Imips -Imips/nanonote -Wa,-mips32 -DNANONOTE -DUSE_SERIAL
CROSS = mipsel-linux-gnu-
OBJDUMP = $(CROSS)objdump
junk = mdebug.abi32 reginfo comment pdr
OBJCOPYFLAGS = $(addprefix --remove-section=.,$(junk))
iris.elf: LDFLAGS = --omagic -Ttext $(load)
mips/start.elf: LDFLAGS = --omagic -Ttext $(start_load)
mips/nanonote/nand-boot.elf: LDFLAGS = --omagic -Ttext 0x80000000
source/nand.o: mips/nanonote/nand-boot.raw
UDC_BOOT = comment this out for sd boot
arch_iris_sources = mips/interrupts.cc mips/arch.cc
boot_sources = mips/init.cc mips/nanonote/board.cc
arch_headers = mips/arch.hh mips/nanonote/jz4740.hh mips/nanonote/board.hh
arch_headers = mips/arch.hh mips/nanonote/jz4740.hh mips/nanonote/board.hh mips/nand.hh
udc_boot_programs = udc
sd_boot_programs = sd+mmc partition fat
standard_boot_programs = bootinit
programs = init gpio lcd bsquare ball buzzer metronome elfrun alarm rtc gui nand test $(udc_boot_programs) $(sd_boot_programs) $(standard_boot_programs)
ARCH_CPPFLAGS = -I. -Imips -Imips/nanonote -Wa,-mips32 -DNANONOTE -DUSE_SERIAL
CROSS = mipsel-linux-gnu-
OBJDUMP = $(CROSS)objdump
junk = mdebug.abi32 reginfo comment pdr
OBJCOPYFLAGS = $(addprefix --remove-section=.,$(junk))
ifdef UDC_BOOT
boot_threads = $(standard_boot_programs) $(udc_boot_programs)
threadlist = mips/nanonote/threadlist-udc
ARCH_CXXFLAGS = -DNUM_THREADS=2
all: mips/nanonote/nand-boot.raw test
mips/start.o: TARGET =
else
boot_threads = $(standard_boot_programs) $(sd_boot_programs)
threadlist = mips/nanonote/threadlist-sd
ARCH_CXXFLAGS = -DNUM_THREADS=4
all: mips/nanonote/nand-boot.raw iris-sd.tar
mips/start.o: TARGET = -DWRAPPED
iris-sd.tar: $(addprefix fs/,$(addsuffix .elf,$(programs))) mips/start.raw.gz fs/init.config
mkimage -A mips -T kernel -a $(start_load) -e $(shell /bin/sh -c '$(OBJDUMP) -t mips/start.elf | grep __start$$ | cut -b1-8') -n Iris -d mips/start.raw.gz fs/uimage | sed -e 's/:/;/g'
cd fs && tar cvf ../$@ uimage init.config $(addsuffix .elf,$(programs)) --dereference
endif
iris.elf: LDFLAGS = --omagic -Ttext $(load)
mips/start.elf: LDFLAGS = --omagic -Ttext $(start_load)
mips/nanonote/nand-boot.elf: LDFLAGS = --omagic -Ttext 0x80000000
mips/nanonote/nand-boot.o: mips/nand.hh
source/nand.o: mips/nand.hh mips/nanonote/nand-boot.raw iris-sd.raw
mips/start.o:mips/start.S Makefile Makefile.arch iris.raw
$(CC) $(CPPFLAGS) -DSTART=0xa$(shell /bin/sh -c '$(OBJDUMP) -t iris.elf | grep __start$$ | cut -b2-8') -c $< -o $@
$(CC) $(CPPFLAGS) $(TARGET) -DSTART=0x$(shell /bin/sh -c '$(OBJDUMP) -t iris.elf | grep __start$$ | cut -b1-8') -c $< -o $@
test: mips/start.raw mips/start.elf mips/nanonote/server/usb-server mips/nanonote/sdram-setup.raw $(addsuffix .elf,$(addprefix fs/,$(programs))) fs/init.config
echo "reboot $(start_load) 0xa$(shell /bin/sh -c '$(OBJDUMP) -t mips/start.elf | grep __start$$ | cut -b2-8') $<" | nc localhost 5050
echo "reboot $(start_load) 0x$(shell /bin/sh -c '$(OBJDUMP) -t mips/start.elf | grep __start$$ | cut -b1-8') $<" | nc localhost 5050
mips/nanonote/server/usb-server: mips/nanonote/server/usb-server.ccp mips/nanonote/server/Makefile.am mips/nanonote/server/configure.ac devices.hh
$(MAKE) -C mips/nanonote/server

View File

@@ -72,10 +72,10 @@ void board_init ():
UART0_MCR = 0
UART0_SIRCR = 0
UART0_UACR = 0
UART0_UMR = 1
UART0_UMR = 16
UART0_LCR = UARTLCR_WLEN_8 | UARTLCR_STOP1 | UARTLCR_DLAB
unsigned const baud = 57600
unsigned uart_div = 12000000 / baud
unsigned uart_div = 12000000 / 16 / baud
UART0_DLHR = (uart_div >> 8) & 0xff
UART0_DLLR = uart_div & 0xff
UART0_LCR = UARTLCR_WLEN_8 | UARTLCR_STOP1
@@ -83,9 +83,14 @@ void board_init ():
kdebug ("\n\nSerial port initialized\n")
#endif
void arch_reboot ():
static void sync_serial ():
#ifndef NDEBUG
// Wait for serial port to be done.
while !(UART0_LSR & UARTLSR_TEMT):
#endif
void arch_reboot ():
sync_serial ()
// Reboot.
wdt_select_extalclk ()
wdt_select_clk_div1 ()
@@ -96,8 +101,7 @@ void arch_reboot ():
while true:
void arch_poweroff ():
// Wait for serial port to be done.
while !(UART0_LSR & UARTLSR_TEMT):
sync_serial ()
// Power off.
// Make sure the rtc is running.
cpm_start_rtc ()
@@ -113,3 +117,9 @@ void arch_poweroff ():
// Fall back to reboot.
kdebug ("Power down failed! Rebooting instead.\n")
arch_reboot ()
void arch_boot (unsigned address):
sync_serial ()
// Boot into another kernel.
arch_flush_cache ()
return ((void (*)())address) ()

View File

@@ -16,8 +16,6 @@
// 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.
asm volatile ("\t.set noreorder\n"
"\t.text\n"
"\t.globl __start\n"
@@ -28,6 +26,8 @@ asm volatile ("\t.set noreorder\n"
"\tnop\n"
"\t.word _gp\n"
"1:\n"
"\tori $t0, $zero, 0x0006\n"
"\tmtc0 $t0, $12\n"
"\tlw $gp, 0($ra)\n"
"\tla $sp, stack + 0x1000\n"
"\tla $t9, nandboot_start\n"
@@ -38,160 +38,89 @@ asm volatile ("\t.set noreorder\n"
#define __KERNEL__
#include <jz4740.hh>
// Standard NAND flash commands
#define CMD_READ0 0
#define CMD_READ1 1
#define CMD_RNDOUT 5
#define CMD_PAGEPROG 0x10
#define CMD_READOOB 0x50
#define CMD_ERASE1 0x60
#define CMD_STATUS 0x70
#define CMD_STATUS_MULTI 0x71
#define CMD_SEQIN 0x80
#define CMD_RNDIN 0x85
#define CMD_READID 0x90
#define CMD_ERASE2 0xd0
#define CMD_RESET 0xff
#define DELAY()
// Extended commands for large page devices
#define CMD_READSTART 0x30
#define CMD_RNDOUTSTART 0xE0
#define CMD_CACHEDPROG 0x15
static void debug (unsigned ch):
while !(UART0_LSR & UARTLSR_TDRQ):
UART0_TDR = ch
// Status bits
#define STATUS_FAIL 0x01
#define STATUS_FAIL_N1 0x02
#define STATUS_TRUE_READY 0x20
#define STATUS_READY 0x40
#define STATUS_WP 0x80
static void debug_num (unsigned num, unsigned base, unsigned min_digits = 1):
char const *encode = "0123456789abcdef"
unsigned digits = 1
unsigned power = 1
while power <= num / base || min_digits > digits:
power *= base
++digits
for unsigned i = 0; i < digits; ++i:
unsigned d = num / power
debug (encode[d])
num -= d * power
power /= base
static volatile char *command
static volatile char *address
static volatile char *data
static void debug (char const *f, ...):
unsigned *last = (unsigned *)&f
while *f:
if *f == '%':
++f
switch *f:
case '%':
debug ('%')
break
case 'd':
++last
debug_num (*last, 10)
break
case 'x':
++last
debug_num (*last, 0x10)
break
case 's':
++last
char *str = (char*)*last
while *str:
debug (*str++)
break
default:
debug ("warning: invalid character in dbg format string\n")
break
else:
debug (*f)
++f
static unsigned page_bits
static unsigned redundant_bits
static unsigned block_bits
static unsigned word_size
#define debug_line() debug ("nand-boot line %d\n", __LINE__)
static void unbusy ():
while !(gpio_get_port (2) & (1 << 30)):
// Do nothing.
// Delay.
for unsigned i = 0; i < 1000; ++i:
gpio_set (0, 0)
#include "nand.hh"
static void addr (unsigned d):
unbusy ()
*address = d
unbusy ()
static void cmd (unsigned d):
unbusy ()
*command = d
unbusy ()
static void wdata (unsigned d):
unbusy ()
*data = d
unbusy ()
static unsigned rdata ():
unbusy ()
unsigned ret = *data
unbusy ()
return ret
// Reset nand controller.
static void reset ():
unsigned base = 0xa0000000 + 0x18000000
data = (volatile char *)base
command = (volatile char *)(base + 0x8000)
address = (volatile char *)(base + 0x10000)
// Set up.
gpio_as_nand ()
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)
redundant_bits = (d & 4 ? 4 : 3)
block_bits = 16 + ((d >> 4) & 3)
word_size = (d & 0x40 ? 16 : 8)
//unsigned serial_access_minimum = (d & 0x80 ? 25 : 50)
d = rdata ()
//unsigned num_planes = 1 << ((d >> 2) & 3)
//unsigned plane_bits = 26 + ((d >> 4) & 7)
// Read 512 bytes from nand and store them into buffer. a must be aligned.
// Return value is true if the oob claims there is valid data.
static bool read (unsigned a, char *buffer):
unsigned column = a & ((1 << page_bits) - 1)
unsigned row = a >> page_bits
cmd (CMD_READ0)
addr (column)
addr (column >> 8)
addr (row)
addr (row >> 8)
addr (row >> 16)
cmd (CMD_READSTART)
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 ()
EMC_NFECR = EMC_NFECR_RS | EMC_NFECR_RS_DECODING
char error[9]
// 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 validcol = (1 << page_bits) + 2
bool valid = false
cmd (CMD_RNDOUT)
addr (validcol)
addr (validcol >> 8)
cmd (CMD_RNDOUTSTART)
for unsigned t = 0; t < 3; ++t:
valid = rdata () == 0 || valid
unsigned errcol = (1 << page_bits) + (column >> 9) * 9 + 6
cmd (CMD_RNDOUT)
addr (errcol)
addr (errcol >> 8)
cmd (CMD_RNDOUTSTART)
for unsigned t = 0; t < 9; ++t:
error[t] = rdata ()
EMC_NFPAR (0) = ((unsigned *)error)[0]
EMC_NFPAR (1) = ((unsigned *)error)[1]
EMC_NFPAR (2) = error[9]
EMC_NFECR = EMC_NFECR_ECCE | EMC_NFECR_RS | EMC_NFECR_RS_DECODING | EMC_NFECR_PRDY
while !(EMC_NFINTS & EMC_NFINTS_DECF):
// Do nothing.
unsigned errs = (EMC_NFINTS & EMC_NFINTS_ERRCNT_MASK) >> EMC_NFINTS_ERRCNT_BIT
for unsigned i = 0; i < errs; ++i:
buffer[EMC_NFERR (i) >> 16] ^= EMC_NFERR (i) & 0xff
static void setup_uart ():
cpm_start_uart0 ()
gpio_as_uart0 ()
UART0_IER = 0
UART0_FCR = 0
UART0_MCR = 0
UART0_SIRCR = 0
UART0_UACR = 0
UART0_UMR = 16
UART0_LCR = UARTLCR_WLEN_8 | UARTLCR_STOP1 | UARTLCR_DLAB
unsigned const baud = 57600
unsigned uart_div = 12000000 / 16 / baud
UART0_DLHR = (uart_div >> 8) & 0xff
UART0_DLLR = uart_div & 0xff
UART0_LCR = UARTLCR_WLEN_8 | UARTLCR_STOP1
UART0_FCR = UARTFCR_UUE | UARTFCR_FE | UARTFCR_RFLS | UARTFCR_TFLS
debug ("\n\nNand-boot: serial port initialized\n")
extern "C":
void nandboot_start ():
unsigned base = 0x18000000 + 0xa0000000
data = (volatile char *)base
command = (volatile char *)(base + 0x8000)
address = (volatile char *)(base + 0x10000)
pll_init ()
cpm_start_all ()
gpio_as_sdram_16bit ()
gpio_as_nand ()
setup_sdram ()
setup_uart ()
reset ()
// Load contents of nand flash (from 0x4000) into 0xa0600000;
unsigned a = 0x4000
@@ -199,8 +128,16 @@ extern "C":
while read (a, (char *)target):
a += 0x200
target += 0x200
// Tell about what'll happen.
debug ("Jumping to a0600000:\n")
for unsigned d = 0; d < 0x40; ++d:
debug (" ")
debug_num (((char *)0xa0600000)[d] & 0xff, 16, 2)
debug ('\n')
// Wait for the serial port to finish.
while !(UART0_LSR & UARTLSR_TEMT):
// Then jump to 0xa0600000.
return ((void (*)())0xa0600000) ()
((void (*)())0xa0600000) ()
unsigned char stack[0x1000]