/*
 *               Copyright (C) 2006, 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: mmu.c,v 1.1 2006/09/29 22:37:48 achan Exp $
 */


#include <types.h>
#include <ioslibc.h>
#include <iop.h>
#include <sys.h>
#include <mmu.h>
#include <diag/proto.h>
#include <diag/aes_sw.h>
#include <addr_map.h>


/* Hardcoded addresses for the start of AHB, AHB protected, SRAM and DDR */
#define AHB_START           0x0d000000
#define AHB_PROT_START      0x0d800000
#define SRAM_START          0x0dc00000
#define SRAM_ALT_START      0xfff00000
#define DDR_START           IOP_GDDR3_START

#define TTB_START           (SRAM_START + (32 * 1024))
#define TTB_ENTRIES         4096

#define DCACHE_SIZE         (16 * 1024)
#define MEMCPY_SIZE         (64 * 1024)
#define AES_SIZE            (64 * 1024)

/* Tests to run */
#define VALUE_00            1
#define VALUE_FF            2
#define VALUE_MEM           3
static u32 tests[] = {VALUE_00, VALUE_FF, VALUE_MEM};

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


extern void mmuSetTTB(u32 ttbAddr);
extern void mmuSetDACR(u32 access);
extern void drainWriteBuffer(void);
extern void mmuEnable(void);
extern void mmuDisable(void);
extern void enableDCache(void);
extern void disableDCache(void);
extern void flushDCache(void);

/*
 * Initialize the MMU and D-cache
 */
void
enableMMUDCache(void)
{
    u32	*ttbAddr = (u32 *) TTB_START;

    /* Initialize the TTB region */
    memset(ttbAddr, 0, TTB_ENTRIES * sizeof (u32));

    /*
     * Set up the MMU regions, one for the SRAM and the other for
     * DDR, both with D-cache enabled. Only the first MB is set in
     * both cases
     */
    setupSectionEntry(ttbAddr, SRAM_START >> 20, WRITEBACK_SECTION);
    setupSectionEntry(ttbAddr, SRAM_ALT_START >> 20, WRITEBACK_SECTION);
    setupSectionEntry(ttbAddr, DDR_START >> 20, WRITEBACK_SECTION);

    /*
     * Other than the actual memory regions, also set up access to
     * the register space at AHB_START and AHB_PROT_START. 1MB is
     * also enough in both cases
     */
    setupSectionEntry(ttbAddr, AHB_START >> 20, NONBUFFERED_SECTION);
    setupSectionEntry(ttbAddr, AHB_PROT_START >> 20, NONBUFFERED_SECTION);

    /* Set the TTB base register */
    mmuSetTTB((u32) ttbAddr);

    /* Set up the domain access */
    mmuSetDACR(DOMAIN_MANAGER_ACCESS);

    /* Enable the MMU */
    mmuEnable();

    /* Enable the D-cache */
    enableDCache();
}

/*
 * Disable the D-cache and MMU
 */
void
disableMMUDCache(void)
{
    /* Flush the D-cache */
    flushDCache();

    /* Disable the D-cache */
    disableDCache();

    /* Drain the write buffer */
    drainWriteBuffer();

    /* Disable the MMU */
    mmuDisable();
}


void
writeToMem32(u32 test)
{
    u32     count = 0;
    u32     value, *ptr;

    for (count = 0; count < (DCACHE_SIZE / 4); count++) {
        if (test == VALUE_00) {
            value = 0;
        } else if (test == VALUE_FF) {
            value = 0xffffffff;
        } else {
            value = DDR_START + (count * 4);
        }

        ptr = (u32 *) (DDR_START + (count * 4));
        *ptr = value;
    }   
}

void
writeToMem16(u32 test)
{
    u32     count = 0;
    u16     value, *ptr;

    for (count = 0; count < (DCACHE_SIZE / 2); count++) {
        if (test == VALUE_00) {
            value = 0;
        } else if (test == VALUE_FF) {
            value = 0xffff;
        } else {
            value = (u16) (DDR_START + (count * 2));
        }

        ptr = (u16 *) (DDR_START + (count * 2));
        *ptr = value;
    }   
}

void
writeToMem8(u32 test)
{
    u32     count = 0;
    u8      value, *ptr;

    for (count = 0; count < DCACHE_SIZE; count++) {
        if (test == VALUE_00) {
            value = 0;
        } else if (test == VALUE_FF) {
            value = 0xff;
        } else {
            value = (u8) (DDR_START + count);
        }

        ptr = (u8 *) (DDR_START + count);
        *ptr = value;
    }   
}


void
readFromMem32(u32 test)
{
    u32     count = 0;
    u32     *ptr, check, value;

    for (count = 0; count < (DCACHE_SIZE / 4); count++) {
        if (test == VALUE_00) {
            value = 0;
        } else if (test == VALUE_FF) {
            value = 0xffffffff;
        } else {
            value = DDR_START + (count * 4);
        }

        ptr = (u32 *) (DDR_START + (count * 4));
        check = *ptr;
        if (check != value) {
            printf("Test %d: read back check failed with addr=0x%x,"
                " 0x%x:0x%x\n", test, DDR_START + (count * 4), check, value);
            while (1);
        }
    }   
}

void
readFromMem16(u32 test)
{
    u32     count = 0;
    u16     *ptr, check, value;

    for (count = 0; count < (DCACHE_SIZE / 2); count++) {
        if (test == VALUE_00) {
            value = 0;
        } else if (test == VALUE_FF) {
            value = 0xffff;
        } else {
            value = (u16) (DDR_START + (count * 2));
        }

        ptr = (u16 *) (DDR_START + (count * 2));
        check = *ptr;
        if (check != value) {
            printf("Test %d: read back check failed with addr=0x%x,"
                " 0x%x:0x%x\n", test, DDR_START + (count * 2), check, value);
            while (1);
        }
    }   
}

void
readFromMem8(u32 test)
{
    u32     count = 0;
    u8      *ptr, check, value;

    for (count = 0; count < DCACHE_SIZE; count++) {
        if (test == VALUE_00) {
            value = 0;
        } else if (test == VALUE_FF) {
            value = 0xff;
        } else {
            value = (u8) (DDR_START + count);
        }

        ptr = (u8 *) (DDR_START + count);
        check = *ptr;
        if (check != value) {
            printf("Test %d: read back check failed with addr=0x%x,"
                " 0x%x:0x%x\n", test, DDR_START + count, check, value);
            while (1);
        }
    }   
}

void
doMemcpyTest(void)
{
    u8      *src = (u8 *) DDR_START;
    u8      *dst = (u8 *) (DDR_START + MEMCPY_SIZE);
    u32     count;

    /* Assign some random data to the buffer */
    for (count = 0; count < MEMCPY_SIZE; count++) {
        *src++ = (u8) ((count * 3372) + 198);
    }
    src = (u8 *) DDR_START;

    /* Perform the memcpy */
    memcpy(dst, src, MEMCPY_SIZE);

    /* Compare the source and destination data */
    for (count = 0; count < MEMCPY_SIZE; count++) {
        if (src[count] != dst[count]) {
            printf("Test MEMCPY: comparison check failed with addr=0x%x,"
                " 0x%x:0x%x\n", DDR_START + count, src[count], dst[count]);
            while (1);
        }
    }
}

void
doAesTest(void)
{
    u8      *data = (u8 *) DDR_START;
    u8      *enc = (u8 *) (DDR_START + AES_SIZE);
    u8      *dec = (u8 *) (DDR_START + (2 * AES_SIZE));
    u32     swKey[4], swIv[4];
    u32     count;

    /* Assign some random data to the buffer */
    for (count = 0; count < AES_SIZE; count++) {
        *data++ = (u8) ((count * 2193) + 442);
    }
    data = (u8 *) DDR_START;

    /* Perform encryption using the software AES engine */
    memset(swKey, 0, sizeof (swKey));
    memset(swIv, 0, sizeof (swIv));
    aes_sw_encrypt((u8 *) swKey, (u8 *) swIv, data, AES_SIZE * 8, enc);

    /* Perform decryption using the software AES engine */
    memset(swKey, 0, sizeof (swKey));
    memset(swIv, 0, sizeof (swIv));
    aes_sw_decrypt((u8 *) swKey, (u8 *) swIv, enc, AES_SIZE * 8, dec);

    /* Compare the original data and decrypted text */
    for (count = 0; count < AES_SIZE; count++) {
        if (data[count] != dec[count]) {
            printf("Test AES: comparison check failed with addr=0x%x,"
                " 0x%x:0x%x\n", DDR_START + count, data[count], dec[count]);
            while (1);
        }
    }
}


s32
main(void)
{
    s32     rc = 0;
    u32     cycles = 0, count;

#ifndef DISABLE_CACHE
    /* Enable the MMU and D-cache */
    enableMMUDCache();
#endif

    while (1) {
        /* Simple memory test */
        for (count = 0; count < (sizeof(tests) / sizeof(tests[0])); count++) {
            /* 32-bit test */
            writeToMem32(tests[count]);
            readFromMem32(tests[count]);

            /* 16-bit test */
            writeToMem16(tests[count]);
            readFromMem16(tests[count]);

            /* 8-bit test */
            writeToMem8(tests[count]);
            readFromMem8(tests[count]);
        }

        /* Memory test with memcpy */
        doMemcpyTest();

        /* Memory test with AES software engine */
        doAesTest();
 
        cycles++;
        printf("Completed %d cycles\n", cycles);
    }

#ifndef DISABLE_CACHE
    /* Disable the MMU and D-cache */
    disableMMUDCache();
#endif

    return (rc);
}
