/*---------------------------------------------------------------------------*
  Project:  IOP-OS State Transition Manager
  File:     stm.h

  Copyright 2006 Nintendo.  All rights reserved.

  These coded instructions, statements, and computer programs contain
  proprietary information of Nintendo of America Inc. and/or Nintendo
  Company Ltd., and are protected by Federal copyright law.  They may
  not be disclosed to third parties or copied or duplicated in any form,
  in whole or in part, without the prior written consent of Nintendo.
  
  $Log: stm.h,v $
  Revision 1.2  2006/07/07 18:15:33  wheeler
  Remove PWR and RSW interrupt routines.

  Revision 1.1  2006/07/03 21:22:25  wheeler
  STM reference from Nintendo.

  Revision 1.3  2006/06/28 09:44:40  hirose_kazuki
  Some static functions became public.

  Revision 1.2  2006/06/20 08:32:05  hirose_kazuki
  Removed VIO debug messages. More workarounds for chattering effects.

  Revision 1.1  2006/06/16 04:20:58  hirose_kazuki
  Initial check in.

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

#ifndef __STM_H__
#define __STM_H__

//#define VIO_DEBUG
#define ENABLE_LED_HEARTBEAT

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

#include <ios.h>
#include <hw.h>
#include <ioslibc.h>

#ifdef VIO_DEBUG
// For debug output
#define ISTMReport              printf
#else
#define ISTMReport(...)         (void)0
#endif

/*---------------------------------------------------------------------------------------*
    Process/Thread control
 *---------------------------------------------------------------------------------------*/
#define STM_MAIN_STACK_SIZE     1024
#define STM_MAIN_PRIORITY       10
#define STM_SEVP_MSGQ_SIZE      8

#define STM_IMRM_STACK_SIZE     1024
#define STM_IMRM_PRIORITY       104
#define STM_IMRM_MSGQ_SIZE      8

#define STM_EHRM_STACK_SIZE     1024
#define STM_EHRM_PRIORITY       96
#define STM_EHRM_MSGQ_SIZE      8

// For debug purpose only
#ifdef ENABLE_LED_HEARTBEAT
#define STM_SLED_STACK_SIZE     1024
#define STM_SLED_PRIORITY       8
#define STM_SLED_MSGQ_SIZE      4
#endif // ENABLE_LED_HEARTBEAT

// for chattering removal
#define STM_NUM_FOR_DECHATTER   40      // miliseconds

/*---------------------------------------------------------------------------------------*
    Commands
 *---------------------------------------------------------------------------------------*/

#define ISTM_CMD_REGISTER       0x00001000
#define ISTM_CMD_HOTRESET       0x00002001
#define ISTM_CMD_PDNRESET       0x00002002
#define ISTM_CMD_SHUTDOWN       0x00002003
#define ISTM_CMD_IDLEMODE       0x00002004
#define ISTM_CMD_GETSTATE       0x00003001
#define ISTM_CMD_READDDRREG     0x00004001
#define ISTM_CMD_READDDRREG2    0x00004002

#define ISTM_EVT_RESET          0x00020000
#define ISTM_EVT_GPIO           0x00000800

#define ISTM_MODE_NORMAL        0x00000000
#define ISTM_MODE_IDLE          0x00000001

#define ISTM_IMRM_PATH          "/dev/stm/immediate"
#define ISTM_EHRM_PATH          "/dev/stm/eventhook"


#define IDLE_DSKPLL_IFB         0x80000000
#define IDLE_STOP_FAN           0x40000000
#define IDLE_PWRDWN_DCDC115     0x20000000
#define IDLE_USE_FXCLK          0x10000000
#define IDLE_LED_HEARTBEAT      0x08000000
#define IDLE_SOLIDCOLOR         0x04000000
#define IDLE_PWRDWN             0x00800000
#define IDLE_PWRDWN_PI_IE       0x00400000
#define IDLE_PWRDWN_MEM         0x00200000
#define IDLE_PWRDWN_VI          0x00100000
#define IDLE_PWRDWN_AI          0x00080000
#define IDLE_PWRDWN_SI          0x00040000
#define IDLE_PWRDWN_SDIO        0x00020000
#define IDLE_PWRDWN_GPIOLV      0x00010000
#define IDLE_STOP_PLLS          0x00008000
#define IDLE_CLKGATE            0x00000080
#define IDLE_GFXPIRST           0x00000040
#define IDLE_GATE_CPUCLK        0x00000020
#define IDLE_GATE_USB           0x00000010
#define IDLE_GATE_SD            0x00000008
#define IDLE_GATE_VIVH          0x00000004


/*---------------------------------------------------------------------------------------*
    Management Structure
 *---------------------------------------------------------------------------------------*/
typedef struct STMControl
{
    IOSResourceRequest* req;
    u32                 mode;

} STMControl;

extern STMControl   Control;

/*---------------------------------------------------------------------------------------*
    Function Prototypes
 *---------------------------------------------------------------------------------------*/
// Commands
void    ISTM_HotReset       ( IOSResourceRequest* req );
void    ISTM_PdnReset       ( IOSResourceRequest* req );
void    ISTM_Shutdown       ( IOSResourceRequest* req );
void    ISTM_IdleMode       ( IOSResourceRequest* req );
void    ISTM_GetState       ( IOSResourceRequest* req );
void    ISTM_ReadDDRReg     ( IOSResourceRequest* req );
void    ISTM_ReadDDRReg2    ( IOSResourceRequest* req );
void    ISTM_Register       ( IOSResourceRequest* req );


// HW register access command for reset.
void    ISTM_TriggerHotReset        ( void );
void    ISTM_TriggerHotResetPdn     ( void );

// HW register access commands for idle/stand-by.
void    ISTM_ChangeDSKPLL2Internal  ( u32 flags );
void    ISTM_ResetSubsystems        ( u32 flags );
void    ISTM_SetClockGating         ( u32 flags );
void    ISTM_PowerDownHWInterface   ( u32 flags );
void    ISTM_StopPLLs               ( void );
void    ISTM_PowerDownFAN           ( void );
void    ISTM_PowerDownDCDC115       ( void );
void    ISTM_PowerDownAllSystem     ( void );
void    ISTM_SolidColor             ( void );
void    ISTM_StartLEDHeartBeat      ( void );

// Other HW register access commands
#if 0
void    ISTM_EnablePWRInt          ( void );
void    ISTM_DisablePWRInt          ( void );
void    ISTM_EnableRSWInt           ( void );
void    ISTM_DisableRSWInt          ( void );
#endif
void    ISTM_Init                   ( void );
void    ISTM_ReadDDRRegInternal     ( u16* idList, u16* dataList );
void    ISTM_ReadDDRReg2Internal    ( u16* idList, u16* dataList );


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

#endif // __STM_H__

