#include "archinc.h"
#include "cli.h"
#include "pm.h"

#include <stdio.h>
#include <stdlib.h>

typedef enum {run, idle, sleep} pmState_t;

static pmState_t pmState;

void pmSleep(void);

static int sleepHandler(int argc, char * argv[], int flags)
{
  pmSleep();
}


int pmInit(void)
{
  pmState = run;

  PCONP = (unsigned long)0x0000018A;
  cliRegisterCommand("sleep", sleepHandler, "sleep - put processor to sleep");
  return 0;
} 

void pmSleep(void)
{
  int i;

  pmState = sleep;
  PCON = (unsigned long)0x02;

  for(i=0;i<100;i++)
    ; // processor should spin down here

  // if we got here, we must be awake
  pmState = run;
  return;
}

void pmIdle(void)
{
  int i;

  pmState = idle;
  PCON = (unsigned long)0x01;

  for(i=0;i<100;i++)
    ; // processor should spin down here

  // if we got here, we must be awake
  pmState = run;
  return;

}
