/*
 *               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.
 *
 * $Id: cache.c,v 1.2 2008/04/08 18:33:20 cnguyen Exp $
 */

#include <stdlib.h>
#include <iostypes.h>
#include <ios.h>
#include <ioslibc.h>
#include <apputils.h>
#include <elf.h>
#include <iostypes.h>
#include <iop.h>

extern int lockIcache(int address, int length);
extern int lockDcache(int address, int length);
extern int cacheDbgRd (void);
extern int cacheDbgWr (unsigned int);
extern int mmuReadTTB (void);
extern int mmuReadDACR (void);
extern int mmuReadDFSR (void);
extern int mmuReadIFSR (void);
extern int mmuReadFAR (void);

extern void setupMmu (void);
extern void setupMmuNoAccess (void);
extern void setupMmuCltAccess (void);
extern void setupMmuMgrAccess (void);
extern void flushDCache (void);
extern void flushInvalidDCache (void);
extern void drainWriteBuffer (void);
extern void enableDCache (void);
extern void enableICache (void);
extern void invalICache (void);
extern void invalICacheMVA (unsigned int);
extern void invalICacheSW (unsigned int);
extern void invalICacheLine (unsigned int);
extern void invalDCache (void);
extern void invalIDCache (void);
extern void mmuSetDACR (unsigned int);
extern void mmuSetTTB (unsigned int);
extern void mmuSetDFSR (unsigned int);
extern void mmuSetIFSR (unsigned int);
extern void mmuSetFAR (unsigned int);


char *
fault_status (int status)
{
  switch (status) {
  case 0x1:
  case 0x3:
    return "alignment";
  case 0x5:
    return "translation section";
  case 0x7:
    return "translation page";
  case 0x8:
  case 0xa:
    return "external abort";
  case 0x9:
    return "domain section";
  case 0xb:
    return "comain page";
  case 0xc:
    return "ext abort high";
  case 0xd:
    return "perms section";
  case 0xe:
    return "ext abort low";
  case 0xf:
    return "perms page";
  }

  return "unknown";
}

/*
 * domain_valid
 *
 * Given a fault status, is the associated domain value valid?
 *
 *    0: invalid
 *    1: valid
 */
int
domain_valid (int status)
{
  switch (status) {
  case 0x1:
  case 0x3:
  case 0x5:
  case 0x8:
  case 0xa:
  case 0xc:
    return 0;
  case 0x7:
  case 0x9:
  case 0xb:
  case 0xd:
  case 0xe:
  case 0xf:
    return 1;
  }

  return 0;
}

void
mmu_usage (void)
{
  printf ("Mmu operations:\n");
  printf ("\tmmu setup            - Set up/enable MMU\n");
  printf ("\tmmu setupNoAccess    - Set up/enable MMU with no access\n");
  printf ("\tmmu setupCltAccess   - Set up/enable MMU with clt access\n");
  printf ("\tmmu setupMgrAccess   - Set up/enable MMU with MGR access\n");
  printf ("\tmmu SetTTB           - Set up the Translation table base address\n");
  printf ("\tmmu ReadTTB          - Read the Translation Table base address\n");
  printf ("\tmmu SetDACR          - Program the DACR register\n");
  printf ("\tmmu ReadDACR [field] - Read the DACR register\n");
  printf ("\tmmu SetDFSR          - Program the DFSR register\n");
  printf ("\tmmu ReadDFSR         - Read the DFSR register\n");
  printf ("\tmmu SetIFSR          - Program the IFSR register\n");
  printf ("\tmmu ReadIFSR         - Read the IFSR register\n");
  printf ("\tmmu SetIFSR          - Program the IFSR register\n");
  printf ("\tmmu ReadIFSR         - Read the IFSR register\n");

  return;
}

/*
 * mmu
 *
 * Various mmu operations
 */
int
mmu (char *args, char *d)
{
  s32 rc = 0;
  char *ptr;
  int valid = 0, value = 0, field = -1;

  if (getNextToken(&ptr, &args) == 0) {
    mmu_usage ();
    goto mmu_out;
  }

  if (strncmp(ptr, "setup", 5) == 0) {
    setupMmu ();
    valid = 1;
  }
  if (strncmp(ptr, "setupNoAccess", 13) == 0) {
    setupMmuNoAccess ();
    valid = 1;
  }
  if (strncmp(ptr, "setupCLTAccess", 14) == 0) {
    setupMmuCltAccess ();
    valid = 1;
  }
  if (strncmp(ptr, "setupMgAccess", 13) == 0) {
    setupMmuMgrAccess ();
    valid = 1;
  }
  if (strncmp(ptr, "SetTTB", 6) == 0) {
    if (getNextToken(&ptr, &args) == 0) {
      printf ("mmu SetTTB value\n");
      goto mmu_out;
    } else {
      value = asciiToInt(ptr);
    }
    mmuSetTTB (value);
    valid = 1;
  }
  if (strncmp(ptr, "ReadTTB", 7) == 0) {
    value = mmuReadTTB ();
    printf ("TTB base address 0x%08x\n",
	    value);
    valid = 1;
  }
  if (strncmp(ptr, "SetDACR", 7) == 0) {
    if (getNextToken(&ptr, &args) == -1) {
      printf ("mmu SetDACR value\n");
      goto mmu_out;
    } else {
      value = asciiToInt(ptr);
    }
    mmuSetDACR (value);
    valid = 1;
  }
  if (strncmp(ptr, "ReadDACR", 8) == 0) {
    if (getNextToken(&ptr, &args) == 0) {
      /* No field selected */
    } else {
      field = asciiToInt(ptr);
    }
    value = mmuReadDACR ();
    if (field == -1) {
      printf ("DACR = 0x%08x\n", value);
    } else {
      value = (value & (0x3 << (field << 1))) >> (field << 1);
      printf ("value 0x%08x %s\n",
	      value,
	      (value == 0) ? "no access" :
	      ((value == 1) ? "client access" :
	       ((value == 2) ? "reserved" :
		((value == 3) ? "manager" : "unknown"))));
    }
    valid = 1;
  }
  if (strncmp(ptr, "SetDFSR", 7) == 0) {
    if (getNextToken(&ptr, &args) == -1) {
      printf ("mmu SetDFSR value\n");
      goto mmu_out;
    } else {
      value = asciiToInt(ptr);
    }
    mmuSetDFSR (value);
    valid = 1;
  }
  if (strncmp(ptr, "ReadDFSR", 8) == 0) {
    value = mmuReadDFSR ();
    printf ("DFSR = 0x%08x UNP 0x%07x domain 0x%02x (%s) status 0x%02x (%s)\n", 
	    value, (value & 0xffffffe) >> 9, 
	    (value & 0xf0) >> 4, 
	    (domain_valid(value & 0xf) == 1) ? "valid" : "invalid",
	    value & 0xf, fault_status(value & 0xf));
    valid = 1;
  }
  if (strncmp(ptr, "SetIFSR", 7) == 0) {
    if (getNextToken(&ptr, &args) == -1) {
      printf ("mmu SetIFSR value\n");
      goto mmu_out;
    } else {
      value = asciiToInt(ptr);
    }
    mmuSetIFSR (value);
    valid = 1;
  }
  if (strncmp(ptr, "ReadIFSR", 8) == 0) {
    value = mmuReadIFSR ();
    printf ("IFSR = 0x%08x UNP 0x%07x domain 0x%02x (%s) status 0x%02x (%s)\n", 
	    value, (value & 0xffffffe) >> 9,
	    (value & 0xf0) >> 4, 
	    (domain_valid(value & 0xf) == 1) ? "valid" : "invalid",
	    value & 0xf, fault_status(value & 0xf));
    valid = 1;
  }
  if (strncmp(ptr, "SetFAR", 6) == 0) {
    if (getNextToken(&ptr, &args) == -1) {
      printf ("mmu SetFAR value\n");
      goto mmu_out;
    } else {
      value = asciiToInt(ptr);
    }
    mmuSetFAR (value);
    valid = 1;
  }
  if (strncmp(ptr, "ReadFAR", 7) == 0) {
    value = mmuReadFAR ();
    printf ("IFSR address = 0x%08x\n", 
	    value);
    valid = 1;
  }

  if (valid == 0) {
    printf ("mmu %s: not a valid command\n", ptr);
    mmu_usage ();
  }

 mmu_out:

  return rc;
}

void
cache_usage (void)
{
  printf ("Cache operations:\n");
  printf ("\tcache RDbg       - Read the cache debug register\n");
  printf ("\tcache WDbg       - Flip bits in the cache debug register\n");
  printf ("\tcache Dflush     - Flush D cache\n");
  printf ("\tcache flushInval - Flush invalid cache lines\n");
  printf ("\tcache Drain      - Drain write buffer\n");
  printf ("\tcache Denable    - Enable D cache\n");
  printf ("\tcache Ienable    - Enable I cache\n");
  printf ("\tcache Iinval     - Invalidate I cache\n");
  printf ("\tcache IinvalMVA  - Invalidate I cache entry by MVA\n");
  printf ("\tcache IinvalSW   - Invalidate I cache entry by Set/Way\n");
  printf ("\tcache IinvalLine - Invalidate I cache line by MVA\n");
  printf ("\tcache Dinval     - Invalidate D cache\n");
  printf ("\tcache IDinval    - Invalidate I and D cache\n");

  return;
}

/*
 * cache
 *
 * Various cache operations
 */
int
cache (char *args, char *d)
{
  s32 rc = 0;
  char *ptr;
  int valid = 0, value = 0;
  int tag = 0, way = 0, set = 0;

  if (getNextToken(&ptr, &args) == 0) {
    cache_usage ();
    goto cache_out;
  }

  if (strncmp(ptr, "RDbg", 4) == 0) {
    printf ("cache debug register 0x%08x\n",
	    (value = cacheDbgRd()));
    printf ("write-back %s Icache line-fill %s Dcache line-fill %s\n",
	    (value & 0x4) ? "force" : "enabled",
	    (value & 0x2) ? "disabled" : "enabled",
	    (value & 0x1) ? "disabled" : "enabled");
    valid = 1;
  }
  if (strncmp(ptr, "WDbg", 4) == 0) {
    if (getNextToken(&ptr, &args) == 0) {
      printf ("cache WDbg value\n");
      goto cache_out;
    } else {
      value = asciiToInt(ptr);
      if (value & ~0x7) {
	value = value & 0x7;
	printf ("Bits truncated to 0x%08x\n", value);
      }
      value = cacheDbgWr(value);
      printf ("Final debug register 0x%08x\n", value);
    }
    valid = 1;
  }
  if (strncmp(ptr, "Dflush", 6) == 0) {
    flushDCache ();
    valid = 1;
  }
  if (strncmp(ptr, "flushInval", 10) == 0) {
    flushInvalidDCache ();
    valid = 1;
  }
  if (strncmp(ptr, "Drain", 5) == 0) {
    drainWriteBuffer ();
    valid = 1;
  }
  if (strncmp(ptr, "Denable", 7) == 0) {
    enableDCache ();
    valid = 1;
  }
  if (strncmp(ptr, "Ienable", 7) == 0) {
    enableICache ();
    valid = 1;
  }
  if (strncmp(ptr, "Iinval", 6) == 0) {
    invalICache ();
    valid = 1;
  }
  if (strncmp(ptr, "IinvalMVA", 9) == 0) {
    if (getNextToken(&ptr, &args) == 0) {
      printf ("cache IinvalMVA tag set\n");
      goto cache_out;
    } else {
      tag = asciiToInt(ptr);
    }
    if (tag & ~0xfffff) {
      tag = tag & 0xfffff;
      printf ("tag truncated to 0x%05x\n", tag);
    }
    if (getNextToken(&ptr, &args) == 0) {
      printf ("cache IinvalMVA tag set\n");
      goto cache_out;
    } else {
      set = asciiToInt(ptr);
    }
    if (set & ~0x7f) {
      set = set & 0x7f;
      printf ("set truncated to 0x%02x\n", set);
    }
    value = (tag << 12) | (set << 5);
    invalICacheMVA (value);
    valid = 1;
  }
  if (strncmp(ptr, "IinvalSW", 8) == 0) {
    if (getNextToken(&ptr, &args) == 0) {
      printf ("cache IinvalSW way set\n");
      goto cache_out;
    } else {
      way = asciiToInt(ptr);
    }
    if (way & ~0x3) {
      way = way & 0x3;
      printf ("way truncated to 0x%x\n", way);
    }
    if (getNextToken(&ptr, &args) == 0) {
      printf ("cache IinvalSW way set\n");
      goto cache_out;
    } else {
      set = asciiToInt(ptr);
    }
    if (set & ~0x7f) {
      set = set & 0x7f;
      printf ("set truncated to 0x%02x\n", set);
    }
    value = (way << 30) | (set << 5);
    invalICacheSW (value);
    valid = 1;
  }
  if (strncmp(ptr, "IinvalLIne", 10) == 0) {
    if (getNextToken(&ptr, &args) == 0) {
      printf ("cache IinvalLine tag set\n");
      goto cache_out;
    } else {
      tag = asciiToInt(ptr);
    }
    if (tag & ~0xfffff) {
      tag = tag & 0xfffff;
      printf ("tag truncated to 0x%05x\n", tag);
    }
    if (getNextToken(&ptr, &args) == 0) {
      printf ("cache IinvalLine tag set\n");
      goto cache_out;
    } else {
      set = asciiToInt(ptr);
    }
    if (set & ~0x7f) {
      set = set & 0x7f;
      printf ("set truncated to 0x%02x\n", set);
    }
    value = (tag << 12) | (set << 5);
    invalICacheLine (value);
    valid = 1;
  }
  if (strncmp(ptr, "Dinval", 6) == 0) {
    invalDCache ();
    valid = 1;
  }
  if (strncmp(ptr, "IDinval", 7) == 0) {
    invalIDCache ();
    valid = 1;
  }

  if (valid == 0) {
    printf ("cache %s: not a valid command\n", ptr);
    cache_usage ();
  }

 cache_out:

  return rc;
}

/*
 * cli
 *
 * I Cache lock test.
 *
 *   % cli address length
 */
int
cli (char *args, char *d)
{
  s32 rc = 0;
  char *ptr;
  u32 address, length;
  int status;

  if (getNextToken(&ptr, &args) == 0) {
    printf ("%s address length\n",
	    __FUNCTION__);
    goto cl_out;
  }
  /*
   * I probably want an unsigned version of this.
   */
  address = asciiToInt(ptr);

  if (getNextToken(&ptr, &args) == 0) {
    printf ("%s address length\n",
	    __FUNCTION__);
    goto cl_out;
  }
  length = asciiToInt(ptr);

  printf ("%s: calling lockIcache(0x%x, %d)\n",
	  __FUNCTION__, address, length);

  status = lockIcache(address, length);
  
  printf ("%s: lockIcache = 0x%x\n", 
	  __FUNCTION__, status);

 cl_out:

  return rc;
}

/*
 * cld
 *
 * I Cache lock test.
 *
 *   % cld address length
 */
int
cld (char *args, char *d)
{
  s32 rc = 0;
  char *ptr;
  u32 address, length;
  int status;

  if (getNextToken(&ptr, &args) == 0) {
    printf ("%s address length\n",
	    __FUNCTION__);
    goto cl_out;
  }
  /*
   * I probably want an unsigned version of this.
   */
  address = asciiToInt(ptr);

  if (getNextToken(&ptr, &args) == 0) {
    printf ("%s address length\n",
	    __FUNCTION__);
    goto cl_out;
  }
  length = asciiToInt(ptr);

  printf ("%s: calling lockIcache(0x%x, %d)\n",
	  __FUNCTION__, address, length);

  status = lockDcache(address, length);
  
  printf ("%s: lockIcache = 0x%x\n", 
	  __FUNCTION__, status);

 cl_out:

  return rc;
}

/* eof */
