/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  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 BroadOn Communications Corp.
 *
 */

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

#define STACK_SIZE 1024
const u8 _initStack[STACK_SIZE];
const u32 _initStackSize = sizeof(_initStack);
const u32 _initPriority = 10;

extern u32 timerIntrCnt;


#define ACRRegRead(offset)          IO_READ(ACR_PROT_REGS_BASE+(offset))
#define ACRRegWrite(offset, val)    IOS_ACRRegWrite((offset), val)

/* Definitions for Hollywood */
#define SYSPLL_CLKO_CFG_243MHZ      0x10
#define SYSPLL_CLKO_CFG_162MHZ      0x18
#define SYSPLL_CLKO_CFG_108MHZ      0x24
#define SYSPLL_CLKO_CFG_081MHZ      0x30
#define SYSPLL_CLKO_CFG_054MHZ      0x48

/* Definitions for Bollywood */
#define SYSPLL_CLKO_CFG_B_243MHZ    0x08
#define SYSPLL_CLKO_CFG_B_162MHZ    0x0c
#define SYSPLL_CLKO_CFG_B_108MHZ    0x12
#define SYSPLL_CLKO_CFG_B_081MHZ    0x18
#define SYSPLL_CLKO_CFG_B_054MHZ    0x24

#define PLL_DELAY                   2
#define LED_INTERVAL                500000

#define WAIT_LIMIT                  25
#define ITER_LIMIT                  WAIT_LIMIT


int ctr = 0;

u32 timeStart[WAIT_LIMIT];
u32 timeStop[WAIT_LIMIT];
IOSMessage message[WAIT_LIMIT];

/* 
 * NOTE:
 *   This test must be run with STM process ID (14) (i.e., re tmr2.elf 14)
 *   to have the proper permission to write ACR registers.
 *   In addition, the JTAG clock needs to be set to 1MHz or lower for the
 *   test to run correctly. Otherwise, when switching to lower frequencies
 *   (i.e., 162MHz, 108MHz, 81MHz, 54MHz), JTAGJet will appear to hang.
 *   To change the JTAG clock in JTAGJet, go to menu
 *       View->Target Configuration->Arm Emulation Paramters
 */
	
void
main(u32 arg)
{
    IOSMessageQueueId mq;
    IOSMessage array[4];
    IOSMessage m;
    int i = 0;
    IOSTimerId t1;
    u32 reg, fx, clko;
    IOSError rv = IOS_ERROR_OK;

    u32 chipid_major, chipid_minor;
    getChipId(&chipid_major, &chipid_minor);

    printf("Creating message queue\n");
    mq = IOS_CreateMessageQueue(array, 4);

    /* Resetting to normal clock of 243 MHz */
    printf("Resetting Clock Speed to 243 MHz\n");

    fx = IO_READ(ACR_SYSCTRL_TRUSTED);
    ACRSYSCTRL_SET_USE_FXCLK(fx, 0);
    printf("-> Setting FXCLK to 0...\n");
    rv = IOS_ACRRegWrite(ACRSYSCTRL_OFFSET, fx);
    if (rv != IOS_ERROR_OK)
        printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                    ACRSYSCTRL_OFFSET, rv);

    /* Need to wait at least 1 microsecond */
    // udelay(PLL_DELAY);

    /* Restore SYSCLK settings */
    reg  = IO_READ(ACR_PLLSYS_TRUSTED);
    clko = (chipid_major >= 2) ? 
            SYSPLL_CLKO_CFG_B_243MHZ : SYSPLL_CLKO_CFG_243MHZ;
    ACRPLLSYS_SET_SYSPLL_CLKO_0(reg, clko);
    printf("-> Setting CLKO_0 to 0x%x ...\n", clko);
    rv = IOS_ACRRegWrite(ACRPLLSYS_OFFSET, reg);
    if (rv != IOS_ERROR_OK)
        printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                    ACRPLLSYS_OFFSET, rv);

    reg  = IO_READ(ACR_PLLSYSEXT_TRUSTED);
    clko = (chipid_major >= 2) ? 
             SYSPLL_CLKO_CFG_B_162MHZ : SYSPLL_CLKO_CFG_162MHZ;
    ACRPLLSYSEXT_SET_SYSPLL_CLKO_1(reg, clko);
    printf("-> Setting CLKO_1 to 0x%x ...\n", clko);
    rv = IOS_ACRRegWrite(ACRPLLSYSEXT_OFFSET, reg);
    if (rv != IOS_ERROR_OK)
        printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                    ACRPLLSYSEXT_OFFSET, rv);

    /* Wait (longer time) */
    // udelay(PLL_DELAY*1000);

    /* 
     * Timer values must be stored in an array, because the 'printf' statements
     *   take much longer than the timer intervals. 
     *
     * Each 'printf' statement is on the scale of 300,000 ticks.
     */

    i = 0;
    printf("Set periodic timer of 2 secs [0xdeadbeef]\n");
    t1 = IOS_CreateTimer(0, 2000000, mq, 0xdeadbeef);
    do {
      timeStart[i] = IOS_GetTimer();
      IOS_ReceiveMessage(mq, &m, IOS_MESSAGE_BLOCK);
      timeStop[i] = IOS_GetTimer();
      message[i] = m;
      printf("[%2d] Current Clock Speed (MHz) = %d\n", i, IOS_GetCoreClk()); 

      fx = IO_READ(ACR_SYSCTRL_TRUSTED);
      ACRSYSCTRL_SET_USE_FXCLK(fx, 1);

      /* Change the clock speed to 162, 108, 81 MHz */
      if (i == 5) {
          /* Change to 162 MHz */
          printf("[%2d] Changing Clock Speed to 162 MHz\n", i);
 
          reg  = IO_READ(ACR_PLLSYSEXT_TRUSTED);
          clko = (chipid_major >= 2) ? 
                  SYSPLL_CLKO_CFG_B_162MHZ : SYSPLL_CLKO_CFG_162MHZ;
          ACRPLLSYSEXT_SET_SYSPLL_CLKO_1(reg, clko);
          rv = IOS_ACRRegWrite(ACRPLLSYSEXT_OFFSET, reg);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRSYSCTRL_OFFSET, rv);

          /* Wait (longer time) */
          // udelay(PLL_DELAY*1000);

          /* Use FXCLK temporary */
          rv = IOS_ACRRegWrite(ACRSYSCTRL_OFFSET, fx);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRSYSCTRL_OFFSET, rv);

          /* Wait (at least 1 usec) */
          // udelay(PLL_DELAY);

      }
      else if (i == 10) {
          /* Change to 108 MHz */
          printf("[%2d] Changing Clock Speed to 108 MHz\n", i);

          clko = (chipid_major >= 2) ? 
                  SYSPLL_CLKO_CFG_B_108MHZ : SYSPLL_CLKO_CFG_108MHZ;
          reg = IO_READ(ACR_PLLSYSEXT_TRUSTED);
          ACRPLLSYSEXT_SET_SYSPLL_CLKO_1(reg, clko);
          rv = IOS_ACRRegWrite(ACRPLLSYSEXT_OFFSET, reg);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRPLLSYSEXT_OFFSET, rv);

          /* Wait (longer time) */
          // udelay(PLL_DELAY*1000);

          /* Use FXCLK temporary */
          rv = IOS_ACRRegWrite(ACRSYSCTRL_OFFSET, fx);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRSYSCTRL_OFFSET, rv);

          /* Wait (at least 1 usec) */
          // udelay(PLL_DELAY);

      }
      else if (i == 15) {
          /* Change to 81 MHz */
          printf("[%2d] Changing Clock Speed to 81 MHz\n", i);

          clko = (chipid_major >= 2) ? 
                  SYSPLL_CLKO_CFG_B_081MHZ : SYSPLL_CLKO_CFG_081MHZ;
          reg = IO_READ(ACR_PLLSYSEXT_TRUSTED);
          ACRPLLSYSEXT_SET_SYSPLL_CLKO_1(reg, clko);
          rv = IOS_ACRRegWrite(ACRPLLSYSEXT_OFFSET, reg);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRPLLSYSEXT_OFFSET, rv);

          /* Wait (longer time) */
          // udelay(PLL_DELAY*1000);

          /* Use FXCLK temporary */
          rv = IOS_ACRRegWrite(ACRSYSCTRL_OFFSET, fx);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRSYSCTRL_OFFSET, rv);

          /* Wait (at least 1 usec) */
          // udelay(PLL_DELAY);

      }
      else if (i == 20) {
          /* Change to 54 MHz */
          printf("[%2d] Changing Clock Speed to 54 MHz\n", i);

          clko = (chipid_major >= 2) ? 
                  SYSPLL_CLKO_CFG_B_054MHZ : SYSPLL_CLKO_CFG_054MHZ;
          reg = IO_READ(ACR_PLLSYSEXT_TRUSTED);
          ACRPLLSYSEXT_SET_SYSPLL_CLKO_1(reg, clko);
          rv = IOS_ACRRegWrite(ACRPLLSYSEXT_OFFSET, reg);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRPLLSYSEXT_OFFSET, rv);

          /* Wait (longer time) */
          // udelay(PLL_DELAY*1000);

          /* Use FXCLK temporary */
          rv = IOS_ACRRegWrite(ACRSYSCTRL_OFFSET, fx);
          if (rv != IOS_ERROR_OK)
              printf("**** ERROR in writing ACR reg (ofs=0x%x), rv=%d\n",
                          ACRSYSCTRL_OFFSET, rv);

          /* Wait (at least 1 usec) */
          // udelay(PLL_DELAY);

      }
    
      i ++;
    } while (i < ITER_LIMIT);

    for(i = 0; i < WAIT_LIMIT; i ++) {
      printf("waiting for timer: %d @ %u\n", i, timeStart[i]);
      printf("Received timer message 0x%x @ %u\n", message[i], timeStop[i]);
    }

    while (1) {}
    
}

