/*
 *               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 <ios.h>
#include <iostypes.h>
#include <ioslibc.h>
#include "ohci.h"
#include "ohci_regs.h"
#include "ohci_core.h"

/* Memory management for transfer descriptors,
 * endpoint descriptors and buffers for driver use.
 */
IOSHeapId __ohcdHeapId = -1;

IOSError 
freeTD(struct ohci_td *td)
{
  return IOS_Free(__ohcdHeapId, td);
}

struct ohci_td *
allocTD(void)
{
    struct ohci_td *td = IOS_AllocAligned(__ohcdHeapId, sizeof(struct ohci_td), OHCI_TD_ALIGN);
    if (NULL != td) memset(td, 0, sizeof(struct ohci_td));
    return td;
}

/* 0 if invalid
 * 1 if valid
 */
u8 
isValidTD(struct ohci_td *td)
{
    u8 rv = 0;

    if (NULL == td) {
        OHCI_DBG("invalid TD: NULL\n");
        goto done;
    }
    if (0 != ((u32)td % OHCI_TD_ALIGN)) {
        OHCI_DBG("invalid TD: %p alignment\n", td);
        goto done;
    }
#if OH0
    if (((u32)td <  OH0_BUF_START) || (u32)td >= (OH0_BUF_START + OH0_BUF_SPACE_SIZE )) 
#elif OH1
        if (((u32)td <  OH1_BUF_START) || (u32)td >= (OH1_BUF_START + OH1_BUF_SPACE_SIZE ))
#endif 
        {
            OHCI_DBG("invalid TD: %p address \n", td);
            goto done;
        }
    OHCI_DBG("TD %p valid\n", td);
    rv = 1;

done:
    return rv;
}

struct ohci_itd *
allocITD(void)
{
    struct ohci_itd *itd = IOS_AllocAligned(__ohcdHeapId, sizeof(struct ohci_itd), OHCI_ITD_ALIGN);
    if (NULL != itd) memset(itd, 0, sizeof(struct ohci_itd));
    return itd;
}

IOSError
freeITD(struct ohci_itd *itd)
{
  return IOS_Free(__ohcdHeapId, itd);
}

struct ohci_ed *
allocED(void)
{
    struct ohci_ed *ed = IOS_AllocAligned(__ohcdHeapId, sizeof(struct ohci_ed), OHCI_ED_ALIGN);
    if (NULL != ed) memset(ed, 0, sizeof(struct ohci_ed));
    return ed;
}

IOSError
freeED(struct ohci_ed *ed)
{
    return IOS_Free(__ohcdHeapId, ed);
}

IOSError
initOHCDHeap(void)
{ 
#if OH0
    memset((u8 *)OH0_BUF_START, 0, OH0_BUF_SPACE_SIZE);
    __ohcdHeapId = IOS_CreateHeap((u8 *) OH0_BUF_START, OH0_BUF_SPACE_SIZE);
#elif OH1
    memset((u8 *)OH1_BUF_START, 0, OH1_BUF_SPACE_SIZE);
    __ohcdHeapId = IOS_CreateHeap((u8 *) OH1_BUF_START, OH1_BUF_SPACE_SIZE);
#endif
    if (__ohcdHeapId < 0) 
        OHCI_DBG("Failed to create buf heap: %d\n", __ohcdHeapId);
    return __ohcdHeapId;
}

IOSError
initOHCDMem(void)
{
    IOSError rv;
    if ((rv = initOHCDHeap()) < 0)
        goto err;
     rv = IOS_ERROR_OK;
     goto done;
err:
   if (__ohcdHeapId > 0)  IOS_DestroyHeap(__ohcdHeapId); 
   rv = IOS_ERROR_INVALID; /* XXX value */

done:
    return rv;
}

/* Utils */
#if OH1
/* SRAM address range after swap for ES1.x */
#ifndef ES_20
#define SWAPPED_SRAM_START 0xffff0000
#define SWAPPED_SRAM_END   0xffffffff
#endif

/* SRAM address range after swap for ES1.x */
#ifdef ES_20
#define SWAPPED_SRAM_START 0xfff00000
#define SWAPPED_SRAM_END   0xffff8000
#endif /* ES_20 */

/* Unswapped SRAM address range is the same 
 * for ES1.x and ES2.0
 */
#define SRAM_START 0x0d400000
#define SRAM_END   0x0d418000

u32 
cpu2dmaSRAMAddr(u32 addr)
{
    u32 addr1 = addr;
    if ((addr >= SWAPPED_SRAM_START) && (addr < SWAPPED_SRAM_END)) {
        addr1 = SRAM_START + (addr - SWAPPED_SRAM_START);
        OHCI_DBG("translated CPU SRAM address to DMA address: 0x%08x--> 0x%08x", addr, addr1);
    }
    return addr1;
}

/* Nobody needs this yet */
#if 0 
u32 
dma2cpuSRAMAddr(u32 addr)
{
    u32 addr1 = addr;
    if ((addr >= SRAM_START) && (addr < SRAM_END)) {
        addr1 = SWAPPED_SRAM_START + (addr - SRAM_START);
        OHCI_DBG("translated DMA SRAM address to CPU address: 0x%08x--> 0x%08x", addr, addr1);
    }
    return addr1;
}
#endif /* 0 */

#endif /* OH1 */

#if OH0
/* No access to SRAM */
inline u32 cpu2dmaSRAMAddr(u32 addr) {return addr;}
//inline u32 dma2cpuSRAMAddr(u32 addr) {return addr;}
#endif /* OH0 */
