/*---------------------------------------------------------------------------*
  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.5  2008/05/28 18:15:41  horti
  Some updates to stm test.

  Revision 1.4  2008/05/23 18:08:22  horti
  dos2unix a bunch of STM files.

  Revision 1.3  2008/05/23 17:54:48  horti
  Update to STM reference from Nintendo (with minor hacks to build in our tree).
  Initial version of changes to create an STM test thread to call STM ioctls.
  Can use it to test STM ioctl commands to transition to idle mode, etc.

  Revision 1.28  2007/05/29 00:57:28  hirose_kazuki
  Added definition of ISTM_DICV_INITIAL_WAIT.

  Revision 1.27  2007/05/08 12:31:42  hirose_kazuki
  Added LOWER_CLOCK_ENABLED flag (disabled by default.)

  Revision 1.26  2007/05/07 06:56:05  hirose_kazuki
  Added macro definitions for system clock rate change.

  Revision 1.25  2007/05/04 07:49:05  seiki_masashi
  added investigator thread for debug

  Revision 1.24  2007/05/02 12:05:13  hirose_kazuki
  Changed the value of STM_DICV_PRIORITY_LOW.

  Revision 1.23  2007/05/01 10:52:30  hirose_kazuki
  Redefined thread priorities, DI polling interval and LED command.

  Revision 1.22  2007/04/28 10:14:11  hirose_kazuki
  Added support for LED illuminations on disc drive loadings/ejects.

  Revision 1.21  2006/12/26 01:03:50  hirose_kazuki
  Added macro definitions for AI clock controls.

  Revision 1.20  2006/11/05 14:32:41  seiki_masashi
  added some flags.

  Revision 1.19  2006/11/01 04:08:49  seiki_masashi
  enable LED_BY_RESET

  Revision 1.18  2006/10/25 14:16:13  hirose_kazuki
  Changed priority.

  Revision 1.17  2006/10/25 13:13:04  hirose_kazuki
  Changed function prototype of ISTM_StartLEDFlashLoop().

  Revision 1.16  2006/10/25 08:41:33  hirose_kazuki
  Disabled any LED activity for debug purpose.

  Revision 1.15  2006/10/21 07:43:05  adachi_hiroaki
  Added wake-up command.

  Revision 1.14  2006/09/26 08:39:06  hirose_kazuki
  Added some functions necessary for wake-up setting.

  Revision 1.13  2006/09/19 12:41:39  terui
  Add ISTM_CMD_READVER definition.
  Add ISTM_ReadVer() proto-type.

  Revision 1.12  2006/09/18 07:32:18  hirose_kazuki
  Added printserver client code (disabled by default.)

  Revision 1.11  2006/09/15 00:47:55  hirose_kazuki
  Added ISTM_PowerUpDCDC115().

  Revision 1.10  2006/09/14 11:54:06  hirose_kazuki
  Added DI cover state polling during IDLE mode.
  Some workarounds for wakeup failure on RVT-Rs.

  Revision 1.9  2006/09/01 12:52:34  hirose_kazuki
  Fixed a value of ISTM_LED_DEBUG.

  Revision 1.8  2006/09/01 11:43:14  hirose_kazuki
  Added supports for release-hook and LED flashing.

  Revision 1.7  2006/08/11 07:22:20  hirose_kazuki
  Changed framweork of Idle -> wakeup.

  Revision 1.6  2006/08/11 04:09:26  hirose_kazuki
  Added VI dimming register access.

  Revision 1.5  2006/07/19 09:05:49  hirose_kazuki
  Updated thread priorities.

  Revision 1.4  2006/07/19 06:44:17  hirose_kazuki
  Integrated BroadOn's modifications for MMU protection support.

  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 ENABLE_LED_HEARTBEAT
#define LED_BY_RESET
//#define REPORT_ENABLED
//#define INVESTIGATOR_ENABLED
#define LOWER_CLOCK_ENABLED

#ifdef REPORT_ENABLED
//#define REPORT_DICOVER_STATE
//#define REPORT_GETSTATE_CMD
#endif // REPORT_ENABLED

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

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


/*---------------------------------------------------------------------------------------*
    Process/Thread control
 *---------------------------------------------------------------------------------------*/
#define STM_MAIN_STACK_SIZE     1024
#define STM_MAIN_PRIORITY       120     // Defined as IOS_PRIORITY_STM
#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

#define STM_DICV_STACK_SIZE     1024
#define STM_DICV_PRIORITY       91
#define STM_DICV_PRIORITY_LOW   20
#define STM_DICV_MSGQ_SIZE      4

#define STM_SLED_STACK_SIZE     1024
#define STM_SLED_PRIORITY       90
#define STM_SLED_MSGQ_SIZE      4

#ifdef INVESTIGATOR_ENABLED
#define STM_INVESTIGATOR_STACK_SIZE 1024
#define STM_INVESTIGATOR_PRIORITY   120
#define STM_INVESTIGATOR_MSGQ_SIZE  8
#endif

#define STM_TEST_STACK_SIZE     1024
#define STM_TEST_PRIORITY       91

// 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_WAKEUP         0x00002005
#define ISTM_CMD_GETSTATE       0x00003001
#define ISTM_CMD_RELEASEHOOK    0x00003002
#define ISTM_CMD_READDDRREG     0x00004001
#define ISTM_CMD_READDDRREG2    0x00004002
#define ISTM_CMD_VIDIMMING      0x00005001
#define ISTM_CMD_LEDFLASH       0x00006001
#define ISTM_CMD_LEDMODE        0x00006002

#define ISTM_CMD_READVER        0x00007001

#define ISTM_EVT_VOID           0x00000000
#define ISTM_EVT_RESET          0x00020000
#define ISTM_EVT_GPIO           0x00000800

#define ISTM_MSG_TIMER          0x40000000

#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_STOP_AIPLL         0x00004000
#define IDLE_SYSFREQ_MASK       0x00000300
#define IDLE_SYSFREQ_162MHZ     0x00000000
#define IDLE_SYSFREQ_108MHZ     0x00000100
#define IDLE_SYSFREQ_81MHZ      0x00000200
#define IDLE_SYSFREQ_54MHZ      0x00000300
#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
#define IDLE_GATE_AIAH          0x00000002

#define ISTM_LED_FLAG_OVERWRITE     0x00010000
#define ISTM_LED_FLAG_NOT_EXEC      0x00020000

#define ISTM_LED_CMD_DRIVE_OPEN     0x00008003
#define ISTM_LED_CMD_DRIVE_CLOSE    0x00008004

#define ISTM_DICV_INITIAL_WAIT      1500000
#define ISTM_DICV_INTERVAL          250000

/*---------------------------------------------------------------------------------------*
    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_ReleaseHook    ( IOSResourceRequest* req );
void    ISTM_ReadDDRReg     ( IOSResourceRequest* req );
void    ISTM_ReadDDRReg2    ( IOSResourceRequest* req );
void    ISTM_VIDimming      ( IOSResourceRequest* req );
void    ISTM_LEDFlash       ( IOSResourceRequest* req );
void    ISTM_LEDMode        ( IOSResourceRequest* req );
void    ISTM_Register       ( IOSResourceRequest* req );

void    ISTM_ReadVer        ( 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_ChangeDSKPLL2External  ( void );
void    ISTM_ResetSubsystems        ( u32 flags );
void    ISTM_SetClockGating         ( u32 flags );
void    ISTM_ResumeClockGating      ( void );
void    ISTM_PowerDownHWInterface   ( u32 flags );
void    ISTM_PowerUpHWInterface     ( void );
void    ISTM_StopPLLs               ( u32 flags );
void    ISTM_PowerDownFAN           ( void );
void    ISTM_PowerUpFAN             ( void );
void    ISTM_PowerDownDCDC115       ( void );
void    ISTM_PowerUpDCDC115         ( void );
void    ISTM_PowerDownAllSystem     ( void );
void    ISTM_SolidColor             ( void );
void    ISTM_Wakeup                 ( void );

// LED control
void    ISTM_StartLEDFlashLoop      ( u32* command, u32 length );
void    ISTM_SetLEDMode             ( u32 mode );
void    ISTM_SetLEDSpeed            ( u32 speed );

// DI Cover state polling
u32     ISTM_ReadDICoverState       ( void );

// Other HW register access commands
void    ISTM_Init                   ( void );
void    ISTM_ReadDDRRegInternal     ( u16* idList, u16* dataList );
void    ISTM_ReadDDRReg2Internal    ( u16* idList, u16* dataList );
void    ISTM_VIDimmingInternal      ( u32* inData, u32* outData );

// Debug output
void    ISTM_PrintInit              ( void );
//void    ISTM_Report                 ( const char* fmt, ... );
//void    ISTM_Printf                 ( const char *fmt, ... );

#define ISTM_Report printf
#define ISTM_Printf printf
/*---------------------------------------------------------------------------------------*/

#endif // __STM_H__

