/*
 * BCM947XX Self-Booting Linux
 *
 * Code should be position-independent until it copies itself to SDRAM.
 *
 * Copyright 2001-2003, Broadcom Corporation
 * All Rights Reserved.
 * 
 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
 *
 * $Id: head.S,v 1.1.1.5 2003/10/29 03:04:03 honor Exp $
 */

#include <asm/asm.h>
#include <asm/regdef.h>
#include <asm/addrspace.h>	
#include <asm/mipsregs.h>
#include <bcm4710.h>

	.text
	LEAF(startup)
	.set	noreorder

	/* Check if we booted from SDRAM */
	bal	1f
	nop
1:	li	t0, 0x1fffffff
	and	t0, t0, ra
	li	t1, BCM4710_FLASH
	blt	t0,t1,setsp
	li	v0,8			/* If we are in ram just assume 8MB of memory */

	/* Initialize SDRAM */
	li	t0, KSEG1ADDR(BCM4710_FLASH)
	la	t1, text_start
	la	t2, board_draminit
	sub	t2, t2, t1
	add	t2, t2, t0
	jalr	t2
	nop

	/* v0 now contains memory size in MB */

	/* Copy self to SDRAM */
	li	a0, BCM4710_FLASH
	la	a1, text_start
	la	a2, input_data
1:	lw	t0, 0(a0)
	sw	t0, 0(a1)
	add	a0, 4
	add	a1, 4
	blt	a1, a2, 1b
	nop

setsp:
	/* Set up stack pointer */
	sll	v0, 20
	or	v0, KSEG0
	sub	sp, v0, 4

	/* Clear BSS */	
	la	a0, bss_start
	la	a1, bss_end
1:	sw	zero, 0(a0)
	addi	a0, a0, 4
	blt	a0, a1, 1b
	nop

	/* Jump to C */
	la	t0, c_main
	jal	t0
	move	a0, ra

	/* Embedded NVRAM */
	.balign	0x400	
	.globl  embedded_nvram
embedded_nvram:
	.fill   0x100,4,~(0x48534c46)
	
	.set reorder
	END(startup)
