/*
 * Copyright (c) 1996-2003 MIPS Technologies, Inc.  All rights reserved.
 * 
 * Unpublished rights (if any) reserved under the copyright laws of the
 * United States of America and other countries.
 * 
 * This code is proprietary to MIPS Technologies, Inc. ("MIPS
 * Technologies").  Any copying, reproducing, modifying or use of this
 * code (in whole or in part) that is not expressly permitted in writing
 * by MIPS Technologies or an authorized third party is strictly
 * prohibited.  At a minimum, this code is protected under unfair
 * competition and copyright laws.  Violations thereof may result in
 * criminal penalties and fines.
 * 
 * MIPS Technologies reserves the right to change this code to improve
 * function, design or otherwise.  MIPS Technologies does not assume any
 * liability arising out of the application or use of this code, or of
 * any error or omission in such code.  Any warranties, whether express,
 * statutory, implied or otherwise, including but not limited to the
 * implied warranties of merchantability or fitness for a particular
 * purpose, are excluded.  Except as expressly provided in any written
 * license agreement from MIPS Technologies or an authorized third party,
 * the furnishing of this code does not give recipient any license to any
 * intellectual property rights, including any patent rights, that cover
 * this code.
 * 
 * This code shall not be exported or transferred for the purpose of
 * reexporting in violation of any U.S. or non-U.S. regulation, treaty,
 * Executive Order, law, statute, amendment or supplement thereto.
 * 
 * This code constitutes one or more of the following: commercial
 * computer software, commercial computer software documentation or other
 * commercial items.  If the user of this code, or any related
 * documentation of any kind, including related technical data or
 * manuals, is an agency, department, or other entity of the United
 * States government ("Government"), the use, duplication, reproduction,
 * release, modification, disclosure, or transfer of this code, or any
 * related documentation of any kind, is restricted in accordance with
 * Federal Acquisition Regulation 12.212 for civilian agencies and
 * Defense Federal Acquisition Regulation Supplement 227.7202 for
 * military agencies.  The use of this code by the Government is further
 * restricted in accordance with the terms of the license agreement(s)
 * and/or applicable contract terms and conditions covering this code
 * from MIPS Technologies or an authorized third party.
 */

/*
 * SDE-MIPS kit implementation specific defines.
 */


#ifndef __KIT_IMPL_H__
#define __KIT_IMPL_H__

#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/clock.h>
#include <mips/xcpt.h>

#define __need___va_list
#include <stdarg.h>

extern void	_scandevs (void);
extern void	_sig_raise (pid_t, int, struct xcptcontext *);
extern void	_sig_deliver (struct xcptcontext *);

extern void 	_xcpt_sigcatch (int, int);
extern void 	_xcpt_catch (unsigned int, int);
extern int	_xcpt_deliver (struct xcptcontext *);

extern int 	_debug_handler (int, struct xcptcontext *);
extern void	_debug_breakpoint (void);
extern int	_debug_sighook (int);
extern int	_debug_enabled (void);
extern int	_debug_connected (void);

/* called from crt0 */
extern void	_cop1_init (int);
extern void	_debug_init (int);
extern void	_debug_exit (int);

/* sbrk query interfaces */
long		_sbrk_totalpages (void);
long		_sbrk_availpages (void);
void		_sbrk_init (void);

/* monitor specific functions */
extern void	_mon_printf (const char *, ...);
extern void	_mon_vprintf (const char *, __va_list);
extern void	_mon_puts (const char *);
extern void	_mon_putc (char);
extern int	_mon_getc (int);
extern int	_gmoncreat (const char *);
extern ssize_t	_gmonwrite (int, const void *, size_t);
extern int	_gmonclose (int);


/* system board specific functions */
extern time_t	_sbd_gettod (void);
extern void	_sbd_settod (time_t);
extern long	_sbd_cpufreq (void);
extern long	_sbd_pipefreq (void);

/* old timer */
extern clock_t	_sbd_inittimer (clock_t (*)(long, struct xcptcontext *));
extern clock_t	_sbd_gettimer (void);
extern void	_sbd_settimer (clock_t);

/* new timer / interrupts */
unsigned int	_sbd_tickspersec (void);
unsigned int	_sbd_gettickval (void);
void		_sbd_settickcmp (unsigned int);

/* polled delays */
extern void	(_sbd_nsdelay) (unsigned long);
extern void	(_sbd_usdelay) (unsigned long);
extern void	(_sbd_msdelay) (unsigned long);

#ifdef INTR_SETMASK
/* new interrupts */
void		_sbd_intrinit (void);
int		_sbd_intrpoll (int);
void		_sbd_intrpending (intrset_t *);
void		_sbd_intrenable (int);
void		_sbd_intrprocmask (int, const intrset_t *, intrset_t *);
int		_sbd_intrnext (int);
void		(_sbd_intrenter) (void);
void		(_sbd_intrleave) (void);
int		_sbd_intrhandler (unsigned int, struct xcptcontext *);
int		_sbd_intrtype (int, const intrtype_t *, intrtype_t *);
int		_sbd_probable_ipl (unsigned int);
int		_sbd_intr_howmany (void);
#endif

/* interface to fairly dumb alphanumeric display panel */
struct panelmode;
struct panelinfo;
extern void 	_sbdpanelmessage (int, int, const char *, size_t nb, int);
extern void	_sbdpanelprogress (int);
extern void	_sbdpanelflash (int);		/* deprecated */
extern void	_sbdpanelmode (const struct panelmode *, struct panelmode *);
extern void	_sbdpanelclear (void);
extern void	_sbdpanelsize (int *, int *);	/* deprecated */
extern void	_sbdpanelinfo (struct panelinfo *);
extern int 	_sbdpanelinit (void);

/* low-level nvram access */
extern unsigned int 	_sbd_nvram_size (unsigned int *, unsigned int *);
extern void		_sbd_nvram_flush (int);
extern int		_sbd_nvram_hwtest (void);
extern unsigned char	_sbd_nvram_getbyte (unsigned int);
extern void		_sbd_nvram_setbyte (unsigned int, unsigned char);
extern unsigned short	_sbd_nvram_getshort (unsigned int);
extern void		_sbd_nvram_setshort (unsigned int, unsigned short);

/* low-level environment access */
extern unsigned int	_sbd_env_size (unsigned int *, unsigned int *);
extern int		_sbd_env_erase (unsigned int, unsigned int);
extern unsigned char 	_sbd_env_getbyte (unsigned int);
extern void		_sbd_env_getbytes (unsigned int, unsigned char *,
					   unsigned int);
extern void		_sbd_env_setbyte (unsigned int offset, unsigned char);
extern void		_sbd_env_setbytes (unsigned int, const unsigned char *,
					   unsigned int);
extern void		_sbd_env_flush (unsigned int);
extern int 		_sbd_env_force_reset (void);


/* equivalent for flash environment */
struct flashcookie;
extern const struct flashcookie *_sbd_flashenv_open (void);

/* high-level non-volatile environment */
extern char *		_sbd_getenv (const char *);
extern int		_sbd_setenv (const char *, const char *, int);
extern void		_sbd_unsetenv (const char *);
extern void		_sbd_mapenv (int (*)(const char *, const char *));
extern const char * 	_sbd_envinit (void);

/* memory layout */
struct sbd_region {
    paddr_t	base;
    size_t	size;
    int		type;
};

#define SBD_MEM_END	0
#define SBD_MEM_RAM	1

extern const struct sbd_region * _sbd_memlayout (void);

/* debug support routines */
extern void	_dbg_panic (const char *, ...);
extern void	_dbg_printf (const char *, ...);
extern int	_dbg_init (void);
extern int	_dbg_inch (void);
extern int	_dbg_inpoll (void);
extern void	_dbg_outch (int);
extern void	_dbg_drain (void);

extern void	_dbg_signals (void);
extern void	_dbg_enterhook (void);
extern int	_dbg_exithook (void);
extern void	_dbg_attachhook (void);
extern void	_dbg_detachhook (void);
extern int	_dbg_put_byte (vaddr_t va, unsigned char v);
extern unsigned int _dbg_get_byte (vaddr_t va, int *err);
extern int	_dbg_put_half (vaddr_t va, unsigned short v);
extern unsigned int _dbg_get_half (vaddr_t va, int *err);
extern int	_dbg_put_word (vaddr_t va, unsigned int v);
extern unsigned int _dbg_get_word (vaddr_t va, int *err);
extern int	_dbg_accessible (vaddr_t va, size_t len, int rw);
extern int	_dbg_cacheable (vaddr_t va);
extern paddr_t	_dbg_vtop (vaddr_t va);
 
struct fpactx;

/* debug thread support routines */
extern int	_dbg_getthread (struct xcptcontext *);
extern int	_dbg_newthread (void);
extern struct xcptcontext *
		_dbg_setthread (struct xcptcontext *, int, int);
extern struct xcptcontext *
		_dbg_contthread (struct xcptcontext *);
extern int	_dbg_querythread (int);
extern int	_dbg_restart (struct xcptcontext *, int);
extern int	_dbg_getfpactx (struct xcptcontext *, struct fpactx *);
extern void	_dbg_putfpactx (struct xcptcontext *, struct fpactx *);
extern void	_dbg_write (const char *, size_t);

/* used at start/_exit time */
extern void	_call_init_funcs (int);
extern void	_call_fini_funcs (void);

extern void	_muldiv_emu_init (void);
extern void	_mips16_entry_init (void);
extern void	_mips_unaligned_init (void);
extern void	_mips_ecchandler_init (void);

extern int	_mips_bremul (struct xcptcontext *, struct fpactx *,
			      unsigned int, vaddr_t *, int *);

extern int	_mips16_bremul (struct xcptcontext *, 
				unsigned short, vaddr_t *, vaddr_t *);

extern unsigned int _mips_clz (unsigned int);
extern unsigned int _mips_dclz (unsigned long long);

#endif /* __KIT_IMPL_H__ */
