#ifndef PHSTBBOOTDSP_INTERNAL_H    /* Multi-include protection.   <FILENAME>_H */
#define PHSTBBOOTDSP_INTERNAL_H

/*****************************************************************************/
/* Copyright (c) 2008 NXP B.V.                                               */
/*                                                                           */
/* 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, using version 2 of the License.             */
/*                                                                           */
/* 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, write to the Free Software               */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307       */
/* USA.                                                                      */
/*                                                                           */
/*****************************************************************************/
/**
    All Rights Reserved.
*//**
    @file   phStbBootDsp_Internal.h
    @brief  Internal header file for this component.

@b  Component:  phStbBootDsp

This file should not need changing when porting to a different OS.

The OS-specific .h file should implement some macros
such that the common C file compile (see Linux definitions of these):
- UNCAC_ADDR
- ALIGN
- PAGE_SIZE
- PAGE_OFFSET

The OS-specific .c file(s) should implement phStbBootDsp_ReadImage()
that the common C file calls.

The OS-specific .c file should initiate a call to phStbBootDsp_BootDspFile().

*//*

Rev Date        Author      Comments
--------------------------------------------------------------------------------
  1 20061106   batelaan    Initial
  2 20061130   batelaan    Beautify
  3 20061130   batelaan    New base-to-info offset
  5 20070921   batelaan    Correct/simplify offsets
  6 20081111   welzenbach  added typedef pphStbBootDsp_Img_t used for phStbBootDsp_ImageTmpLoad
6.1.1 20080422 batelaan    Update file header
  7 20080327   laird       Added #define and changed types.
  8 20080407   batelaan    phStbBootDsp_DspReset changed to phStbBootDsp_DspRun
  9 20080423   batelaan    Merge 6.1.1
--------------------------------------------------------------------------------
    For consistency and standardisation retain the Section Separators.
*/

/*******************
* INCLUDE FILES    *
********************/

#ifndef __ASSEMBLER__
#include <tmNxTypes.h>
#include <uboot_image.h>
#include <phStbNetDev.h>
#if TMFL_OS_IS_LINUX
  #include <linux/phStbBootDsp_Linux_Internal.h>
#endif
#if TMFL_OS_IS_CE
  #include <ce/phStbBootDsp_CE_Internal.h>
#endif
#endif

/**************************************************************************
* EXPORTED MACROS                          PH_STBTEMPLATE_<_WORD>+        *
***************************************************************************/

/** Offset from base address (below which kernel should not access memory)
    to uImage load address.

    WARNING File $KSRC/include/asm-mips/mach-pnx833x/kernel-entry-init.h
    contains a hardcoded value which should match
    #PH_STBBOOTDSP_BASE_TO_INFO_OFFSET
*/
#define PH_STBBOOTDSP_BASE_TO_LOAD_OFFSET (0x1000)

/** Size of are where info is stored that the DSP will use to boot with.
    Should be at least sizeof(::phStbBootDsp_BootKernelInfo_t).

    WARNING File $KSRC/include/asm-mips/mach-pnx833x/kernel-entry-init.h
    contains a hardcoded value which should match
    #PH_STBBOOTDSP_BASE_TO_INFO_OFFSET
*/
#define PH_STBBOOTDSP_DSP_INFO_SIZE 0x0020

/** Address where info is stored that the DSP will use to boot with.
    It is #PH_STBBOOTDSP_DSP_INFO_SIZE below the load address
    (::phStbBootDsp_UImageHeader, field ih_load)..
    @see ::phStbBootDsp_BootKernelInfo_t
*/
#define PH_STBBOOTDSP_DSP_INFO_ADDR \
    ((UInt32)(UNCAC_ADDR(phStbBootDsp_UImageHeader.ih_load - \
     PH_STBBOOTDSP_DSP_INFO_SIZE)))

#ifndef __ASSEMBLER__

/** Command line (debug part) for the kernel. */
#if PH_STBBOOTDSP_VERBOSE
  #define PH_STBBOOTDSP_DFLT_CMDLINE_DEBUG " debug"
#else
  #define PH_STBBOOTDSP_DFLT_CMDLINE_DEBUG ""
  #define DBGPRINT(_expr) do { } while (0)
#endif

/** Command line for the kernel.
   Includes #PH_STBBOOTDSP_DFLT_CMDLINE_DEBUG. */
#define PH_STBBOOTDSP_DFLT_CMDLINE "console=ttyS1,115200" PH_STBBOOTDSP_DFLT_CMDLINE_DEBUG

/** Offset in bytes from phStbBootDsp_UImageHeader.ih_load where we
    temporarily load the uImage, so we can decompresses it into the
    proper phStbBootDsp_UImageHeader.ih_load address. */
#define PH_STBBOOTDSP_TMP_LOAD_OFFSET 0x400000UL

/*********************************************************************
* EXPORTED TYPEDEFS                      [p]phStbTemplate_<Word>+_t  *
**********************************************************************/
/** Stores all info to get the DSP to boot the Linux kernel.
    Set up by the main MIPS, and then used by phStbBootDsp_BootKernel()
    running on the DSP itself to find out all necessary info to boot the kernel.
    This is stored at #PH_STBBOOTDSP_DSP_INFO_ADDR address.
*/
typedef struct phStbBootDsp_BootKernelInfo {
    /** Kernel argc argument */
    Int argc;
    /** Kernel argv argument */
    Char **argv;
    /** Kernel env array argument */
    Char **env;
    /** Address from which the kernel can use any memory.
        Uncached address. */
    ulong baseUncached;
    /** Set to 1 by the DSP very early during booting. */
    ulong booting;
    /** Kernel stack ptr to use. */
    ulong sp;
    /** Uncached address for tmp operation, before booting kernel.
        Used amongst others to do cache clearing. */
    ulong tmp;
} phStbBootDsp_BootKernelInfo_t;

/**************************************************************************
* EXPORTED DATA                     g[k|p|kp|pk|kpk]phStbTemplate_<Word>+ *
***************************************************************************/

/** Uboot image header. */
extern image_header_t phStbBootDsp_UImageHeader;

/** Uboot image length. */
extern uint32_t phStbBootDsp_ImageLen;

/** Pointer to the kernel image. */
typedef unsigned char *pphStbBootDsp_Img_t;

/** Tmp ram area where we load the kernel image,
    before uncompressing it into the final location.
    @see phStbBootDsp_ProcessImageHeader()
*/
extern pphStbBootDsp_Img_t phStbBootDsp_ImageTmpLoad;

#if PH_STBBOOTDSP_VERBOSE
/** Set true if the driver should print verbose output. */
extern int gphStbBootDsp_Verbose;
#endif

/****************************************************************************
* EXPORTED FUNCTION PROTOTYPES phStbTemplate[_<Word>+] *
*****************************************************************************/

#ifdef __cplusplus
extern "C"
{
#endif

/**********************************************************/
/*       C L I E N T     F U N C T I O N S     */
/**********************************************************/

/** Boot the DSP with the supplied image file.
    @param @b IN file Filename containing the image.
    @param @b IN cmdline Command line to pass to the kernel image.
    The default is normally taken from #PH_STBBOOTDSP_DFLT_CMDLINE,
    but can be modified for debugging purposes.
    @return 0 on success, non-zero on error.
*/
Int phStbBootDsp_BootDspFile(const char *file, const char *cmdline);

/**********************************************************/
/* I N T E R N A L   C O M M O N    F U N C T I O N S     */
/**********************************************************/

/** Validates and processes the kernel image header.
    Main purpose is to determine ::phStbBootDsp_ImageTmpLoad.
    @return 0 on success, non-zero on error.
*/
Int phStbBootDsp_ProcessImageHeader(void);

/** Assembler function to boot Linux kernel.
    Uses ::phStbBootDsp_BootKernelInfo_t to find out all info.
    This is stored at #PH_STBBOOTDSP_DSP_INFO_ADDR address.
*/
void phStbBootDsp_BootKernel(void);

/** Function following phStbBootDsp_BootKernel(), used only to determine length
    of phStbBootDsp_BootKernel().
*/
void phStbBootDsp_BootKernelEnd(void);

/** Ensure the DSP is run or reset.
    @param run @b IN If TM_TRUE, then start it, otherwise stop it.
*/
void phStbBootDsp_DspRun(Bool run);

/** Uncompress the kernel image from ::phStbBootDsp_ImageTmpLoad into
    ::phStbBootDsp_UImageHeader.ih_load area.
    @return -1 on error, 0 on success.
*/
Int StbBootDsp_UncompressImage(void);

/** Decompress a Linux kernel using GNU unzip. */
int phStbBootDsp_Gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp);

#if PH_STBBOOTDSP_VERBOSE
void phStbBootDsp_UImageHeaderDump(void);
#endif

/**********************************************************/
/* O S - S P E C I F I C     F U N C T I O N S */
/**********************************************************/

/** Read an image into memory, ready for booting.
    To be implemented for each different OS.
    @param @b IN file Filename containing the image.
    @return 0 on success, non-zero on error.
*/
int phStbBootDsp_ReadImage(const char *file);

/******************************************************************************/
#ifdef __cplusplus
}
#endif
#endif /* #ifndef __ASSEMBLER__ */

#endif /* PHSTBBOOTDSP_INTERNAL_H      Do not add any thing below this line */
