/*
 *----------------------------------------------------------------------
 *    micro T-Kernel
 *
 *    Copyright (C) 2006-2011 by Ken Sakamura. All rights reserved.
 *    micro T-Kernel is distributed under the micro T-License.
 *----------------------------------------------------------------------
 *
 *    Version:   1.01.01
 *    Released by T-Engine Forum(http://www.t-engine.org) at 2011/07/29.
 *
 *----------------------------------------------------------------------
 */

/*
 *	devinit.c (TWRK60N512)
 *	Device-Dependent Initialization
 */

/*
 *  for MB9BF618T on FRK-FM3
 * 
 *   Copyright (C) by Y.Ishioka.
 */

#include "sysinit.h"
#include "kernel.h"
#include <tk/sysdef.h>
#include <sys/sysinfo.h>
#include <libstr.h>
#if 0 /* Y.Ishioka */
#include "MK60N512MD100.h"
#include "core_cm4.h"
#else
#include "mb9bf618t.h"
#include "core_cm3.h"
#endif

/* NVIC Init Priority */
#define INTPRI_INIT_MEMMANAGE       0x00
#define INTPRI_INIT_BUSFAULT        0x00
#define INTPRI_INIT_USAGEFAULT      0x00
#define INTPRI_INIT_SVC             0x1f
#define INTPRI_INIT_DEBUGMON        0x10
#define INTPRI_INIT_PENDSV          0x1f
#define INTPRI_INIT_SYSTICK         0x08

#if 0 /* Y.Ishioka */
Noinit(EXPORT	FP	knl_intvec[N_SVCVEC]);
#else
Noinit(EXPORT	FP	knl_intvec[N_INTVEC + N_SVCVEC]);
#endif
EXPORT	W	knl_taskindp = 0;
Noinit(EXPORT	UW	knl_taskmode);

/* ------------------------------------------------------------------------ */

/* Y.Ishioka */
EXPORT  void  Int_Dummy_Handler( void )
{
    return ;
}


/*
 * Initialization before micro T-Kernel starts
 */

EXPORT ER knl_init_device( void )
{
#if 0
    NVIC_SetPriority(MemoryManagement_IRQn, INTPRI_INIT_MEMMANAGE);
#else
    NVIC_SetPriority(MemManage_IRQn, INTPRI_INIT_MEMMANAGE);
#endif
    NVIC_SetPriority(BusFault_IRQn,         INTPRI_INIT_BUSFAULT);
    NVIC_SetPriority(UsageFault_IRQn,       INTPRI_INIT_USAGEFAULT);
#if 0
    NVIC_SetPriority(SVCall_IRQn,           INTPRI_INIT_SVC);
#else
    NVIC_SetPriority(SVC_IRQn,              INTPRI_INIT_SVC);
#endif
    NVIC_SetPriority(DebugMonitor_IRQn,     INTPRI_INIT_DEBUGMON);
    NVIC_SetPriority(PendSV_IRQn,           INTPRI_INIT_PENDSV);
    NVIC_SetPriority(SysTick_IRQn,          INTPRI_INIT_SYSTICK);

/* Y.Ishioka */
    {
        int  loop ;

        for( loop=0 ; loop<N_INTVEC + N_SVCVEC ; loop++ ) {
            knl_intvec[loop] = Int_Dummy_Handler ;
        }
    }

	return E_OK;
}

/* ------------------------------------------------------------------------ */
/*
 * Start processing after T-Kernel starts
 *	Called from the initial task contexts.
 */
EXPORT ER knl_start_device( void )
{
	return E_OK;
}

#if USE_CLEANUP
/* ------------------------------------------------------------------------ */
/*
 * System finalization
 *	Called just before system shutdown.
 *	Execute finalization that must be done before system shutdown.
 */
EXPORT ER knl_finish_device( void )
{
	return E_OK;
}

/* ------------------------------------------------------------------------ */
/*
 *	Re-starting processing
 */

/*
 * Re-starting processing
 *	mode = -1		Reset and re-start	(cold boot)
 *	mode = -2		Re-start		(warm boot)
 *	mode = -3		Reboot			(normal boot)
 *	mode = 0xFFhhmmss	Re-start at hh:mm:ss
 *				0 <= hh < 24, 0 <= mm,ss < 60
 */
EXPORT ER knl_restart_device( W mode )
{
	if ( mode == -1 ) {
		/* Reset and re-start (cold boot) */
#if USE_KERNEL_MESSAGE
		tm_putstring((UB*)"\n<< SYSTEM RESTART >>\n");
#endif
		tm_exit(-1);  /* no return */
		return E_OBJ;
	}

	if ( mode == -3 ) {
		/* Reboot (normal boot) */
#if USE_KERNEL_MESSAGE
		tm_putstring((UB*)"\n<< SYSTEM REBOOT >>\n");
#endif
		return E_NOSPT;
	}

	if ( mode == -2 ) {
		return E_NOSPT; /* Unsupported */
	}

	if ( (mode & 0xff000000U) == 0xff000000U ) {
		/* Re-start at specified time */
		return E_NOSPT;	/* Unsupported */
	}

	return E_PAR;
}
#endif /* USE_CLEANUP */
