/**     diag_mon_npa_56x4.c
 **
 ** Author: Ted Schmitz
 ** Date of original creation December 16, 2008
 **
 **     This file includes the unique command functions fr the NPA-56x4
 ** platform.
 **
 **/

void disp_seeprom(void)
{
  printf("\n");

  /* read data into global array */
  (void) read_entire_seeprom_timed((unsigned char * ) &seeprom_data[0]);
  disp_seeprom_data();
}

void disp_sdram_spd_data(void)
{
  printf("\n");
  dump_all_sdram_spd_data();

}

/****************************************************************************
 **              Set Unique Defaults For This Platform
 **
 **	This routine sets any unique defaults for this particulat platform.
 ** All common defaults are set in "set_defaults()" function in the
 ** "diag_mon.c" module.
 **
 ** INPUT - Nothing
 **
 ** OUTPUT - In this case certain tests selection bits are enabled or disabled
 **          depending on which hardware options are present on this
 **          particular board.
 **
 ** RETURN - None
 **
 *****************************************************************************/
void set_defaults_for_this_platform(void)
{

  nv_test_sel_bits = (unsigned long) DEFAULT_LL_TESTS;

}

/****************************************************************************
 **              Clear Diagnostic Area Of SEEPROM
 **
 **	This routine sets the entire diagnostic area of the SEEPROM to all
 ** FF's to simulate what the SEEPROM contains when a brand new board is 
 ** powered up.
 **
 ** NOTE:  The operator is prompted to verify the destruction of the
 **        information in the SEEPROM.
 **
 ** INPUT - Nothing
 **
 ** OUTPUT - If the operatoe answers "Y" to the verify question, the entire 
 **          diagnostic area of the SEEPROM will be set to FF's.
 **
 ** RETURN - None
 **
 *****************************************************************************/
void clear_seeprom_space(void)
{
  printf("\nSet diagnostic data in SEEPROM to FF's(Y/N)? N\b");

  gen_fgets(in_line, MAX_INPUT_LINE_SIZE);/* get a line from operator */

  /* Separate command and all parameters into separate ASCII strings. */
  param_count = separate_parms(in_line, args);

  /* init SEPROM only if operator answers "Y[es]" */
  if ((param_count == 1) && ((in_line[0] | 0x20) == 'y'))
    {
    printf("\n\nSetting diagnostic area in SEEPROM to FF's...");
    clear_diag_seeprom_space();
    printf("done.\n");
    }
  else
    {
    printf("\n\nDiagnostic data in SEEPROM is unchanged.\n");
    }
}

/****************************************************************************
 **             External Execute Selected Tests Function
 **
 **   This function is called when the diagnostic monitor is not currently 
 ** active but it is time to execute all the selected phase two tests from
 ** an external call.  This function sets up all the necessary parameters
 ** then calls the "start_testing()" function to execute all the tests.
 **
 ** INPUT - Nothing
 **
 ** OUTPUT - See "start_testing()" function for test execution details.
 **
 ** RETURN - Nothing
 **
 ****************************************************************************/
void external_start_testing(void)
{
  char bwork;                           /* 8-bit working register */
  int result;                           /* result of various func calls */
  int i;

 /* get the saved execution count */
  if (get_exec_count(&exec_count, ES1084) == PROBLEM_DETECTED)
    return;

  param_count = 0;                      /* no params on cmd line */

  start_cmd_flag = ST_CMD_FROM_EXT_SRC; /* start cmd entered from ext src */
  start_testing();                      /* exec the selected phase two tests */

  /* if any failures, blink fault LED, else blink Power good LED */
  bwork = SYS_LED_REG;                  /* read cur contents of LED reg */
  bwork &= (~ALL_SYS_LEDS_MASK & 0xFF); /* clear all sys led bits */
  SYS_LED_REG = bwork;                  /* turn off all sys LEDs */

  /* If unit passes, turn "Power Good" LED on & off predefined num of times */
  if (error_flag == PASS)
    {
    tbl_delay(MS_500);
    for (i = 0; i < LL_POST_END_BLINK_VAL; i++)
      {
      bwork = SYS_LED_REG;              /* read cur contets of LED reg */
      bwork |= GOOD_LED;                /* set LED bit */
      SYS_LED_REG = bwork;              /* turn on LED (Good) */

      tbl_delay(MS_500);

      bwork = SYS_LED_REG;              /* read cur contets of LED reg */
      bwork &= ~GOOD_LED;               /* clear LED bit */
      SYS_LED_REG = bwork;              /* turn off  LED (Good) */

      tbl_delay(MS_500);
      }
    }
  else
    {
    /**   On failure, flash fault LED three times then start up 
     ** normal firmware.
     **/
    tbl_delay(MS_500);
    for (i = 0; i < LL_POST_END_BLINK_VAL; i++)
      {
      bwork = SYS_LED_REG;                /* read cur contets of LED reg */
      bwork |= FAULT_LED;                 /* set LED bit */
      SYS_LED_REG = bwork;                /* turn on LED (Fault) */

      tbl_delay(MS_500);

      bwork = SYS_LED_REG;                /* read cur contets of LED reg */
      bwork &= ~FAULT_LED;                /* clear LED bit */
      SYS_LED_REG = bwork;                /* turn off LED (Fault) */

      tbl_delay(MS_500);
      }

    /* get the error mode from non-volatile storage */
    result = read_diag_data_from_nvram(&error_mode,ERROR_MODE_OS, 
                                       TOT_EMODE_BYTES);
    if (result == PROBLEM_DETECTED)
      {
      report_seeprom_problem(ES1086);     /* log the error & exit */
      return;
      }

    if (error_mode == STOP_ON_ERROR)
      {
      printf("\n\nFatal failure detected by Low-level POST!\n");
      printf("Processor has been halted!\n\n");
      while(1);       /* hang here on fatal error in Stop-On_Error mode */
      }

    /* all other error modes exit now */

    } /* end 'if (error_flag == PASS)...' */

}

/*****************************************************************************
 **                     Return Default Error Mode
 **
 **   This function returns the default Error mode for this platform.
 **
 ** INPUT - Nothing
 **
 ** OUTPUT - Nothing
 **
 ** RETURN - The default test mode for this platform will be returned
 **          to the calling code.
 **
 ****************************************************************************/
unsigned char get_def_error_mode(void)
{

  return GTNF_ON_ERROR;		/* goto NF on first fail */

}

/****************************************************************************
 **              Check The Default Tests For This Platform
 **
 **	This routine checks to make sure all the default tests for the
 ** current platform are selected and returns the appropriate status.
 **
 ** INPUT - "test_select_bits" contains a bit for each test currently selected
 **         for exexcution.
 **
 ** OUTPUT - Nothing
 **
 ** RETURN - Return "TRUE" if all default tests (for the current platform)
 **          are selected.  Return "FALSE" otherwise.
 **
 *****************************************************************************/
int all_def_tests_for_this_platform_are_sel(unsigned long test_select_bits)
{
  unsigned long cal_test_sel_bits;      /* calculated test select bits */

  cal_test_sel_bits = (unsigned long) DEFAULT_LL_TESTS;

  /**   Now modify which tests are selected according to the simulated
   ** hardware options register, if needed.
   **/

  if (test_select_bits == cal_test_sel_bits)
    return TRUE;
  else
    return FALSE;
}

/****************************************************************************
 **                 Write SEEPROM Debug Function
 **
 **	This is a debug routine to write data to the SEEPROM at any address specified
 ** by the operator.
 **
   - - - TBD - - -

 ** INPUT - "test_select_bits" contains a bit for each test currently selected
 **         for exexcution.
 **
 ** OUTPUT - Nothing
 **
 ** RETURN - Return "TRUE" if all default tests (for the current platform)
 **          are selected.  Return "FALSE" otherwise.
 **
 *****************************************************************************/
void debug_write_seeprom(void)
{
#if 0
  int ret_status;             /* status returned from the Uboot call */
  unsigned char buffer[5];
#if 0
  /* call the Uboot driver to write data to the SEEPROM (this call hangs) */
  ret_status = eeprom_write((unsigned) BOARD_EEPROM_TWSI_ADDR, 
                            (unsigned) offset,
                            buffer,
                            (unsigned) length);
#else
  //extern void i2c_init (int speed, int slaveaddr);


  buffer[0] = 0x30;
  buffer[1] = 0x31;
  buffer[2] = 0x32;
  //i2c_init(50000, 0xFE);
  ret_status = eeprom_write((unsigned) BOARD_EEPROM_TWSI_ADDR, 
                            //(unsigned) 0x1000, /* works */
                            //(unsigned) 0x7000, /* works */
                            (unsigned) 0x8000, /* does NOT work */
                            buffer,
                            (unsigned) 2);
#endif

#else
  printf("\nWrite SEEPROM BIST command is not yet implemented!\n");
#endif
}
