/*
 *               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.
 *
 */
/* Interrupt endpoints & transfers. */
#include <iostypes.h>
#include <ios.h>
#include <iop.h>
#include <ioslibc.h>
#include "ohci.h"
#include "ohci_regs.h"

#include "ohci_core.h"

#define MAX_INTR_INTVL  32 
#define NUM_INTR_NODES  63
IOSError 
initIntrEDs(ohci_hcd *hcd)
{
  IOSError rv = IOS_ERROR_OK;
  u8 i;
  u8 base0, base1;
  u32 flags = CPU_TO_OHCI32(OHCI_ED_SKIP_MASK);
  u8 intvl = MAX_INTR_INTVL; 

  ohci_ed *intrEDList =  (ohci_ed *) OH0_INTRED_START;
  memset((u8 *)OH0_INTRED_START, 0, OH0_INTRED_SPACE_SIZE);

  base1 = intvl;
  base0 = 0;
  /* Disable ED's with intvl < 32. Don't disable the first ED's on the
   * list. Nothing will get processed if you do that.
   */
  while (intvl > 1) {
      for (i = 0; i < intvl; ++i) {
          if (0 != base0) {
              intrEDList[base0 + i].flags = flags;
          } else {
              intrEDList[base0 + i].flags = 0;
          }
          intrEDList[base0 + i].nextED = 
              CPU_TO_OHCI32((intrEDList + base1 + i%(intvl/2)));
      }
      base0 += intvl;
      base1 += intvl/2;
      intvl /=2;
  }
  hcd->intr_list = intrEDList;
  return rv;
}

static u8 
__lg(u32 p)
{
  u8 i = 0;
  while ((p>>= 1) != 0) ++i;
  return i;
}

static u8 intrOffsets[] = {62, 60, 56, 48, 32, 0};

/* Dequeue ED and free dummy TD
 * XXX Reuse this for ISO EDs.
 */
static IOSError
__dequeueED(ohci_hcd *hcd, ohci_ed *list, ohci_ed *ed)
{
    IOSError rv = IOS_ERROR_INVALID;
    ohci_ed *prev = NULL;
    ohci_ed *cur = list;
    
    OHCI_DBG("Dequeuing ed %p, list = %p\n", ed, list);
    do {
        if (ed == cur) {
            if (NULL != prev) {
                prev->nextED = cur->nextED;
            }
            freeTD((ohci_td *) (OHCI_TO_CPU32(cur->headP) & 0xfffffff0));
            rv = IOS_ERROR_OK;
            goto done;
        }
        prev = cur;
        cur = (ohci_ed *)OHCI_TO_CPU32(cur->nextED);
    } while (NULL != cur);
done:
    return rv;
}

static IOSError
__dequeuePeriodicED(ohci_hcd *hcd, ohci_ed *ed)
{
    IOSError rv = IOS_ERROR_INVALID;
    u32 i;
    ohci_ed *tmp = &hcd->intr_list[intrOffsets[0]];
    
    OHCI_DBG("Dequeuing ed %p, tail = %p\n", ed, tmp);
    if (NULL == ed) {
        goto done;
    }
    for (i = 0; i < 32; ++i) {
       if (IOS_ERROR_OK == (rv = __dequeueED(hcd, tmp, ed))) {
           goto done;
       }
    }
done:
    return rv;
}

IOSError
freeIntrED(ohci_hcd *hcd, ohci_ed *ed)
{
  IOSError rv = IOS_ERROR_OK;
  u32 flags = OHCI_TO_CPU32(ed->flags);
  ohci_ed *intrEDList =  hcd->intr_list;
  if ((0 == (flags & OHCI_ED_NEW_MASK)) && 
       ((ed > (intrEDList + NUM_INTR_NODES)) || (ed < intrEDList))) {
      rv = IOS_ERROR_INVALID;
      OHCI_ERR("freeIntrEd got invalid ed to free: %p\n", ed);
      goto done;
  }
  IOS_FlushMem(hcd->hc_wb);
  IOS_InvalidateRdb(IOS_RB_IOD);
  
  stopList(hcd, OHCI_CTRL_PLE_MASK);
  if (flags & OHCI_ED_NEW_MASK) {
      __dequeuePeriodicED(hcd, ed);
      freeED(ed);
  } else {
      flags = 0; 
      if (ed  >= (intrEDList + 32)) {
          flags |= OHCI_ED_SKIP_MASK;
      }
      ed->flags = CPU_TO_OHCI32(flags);
  }
  restartList(hcd, OHCI_CTRL_PLE_MASK);

  IOS_FlushMem(IOS_WB_IOD);
  IOS_InvalidateRdb(hcd->hc_rdb);
done:
  return rv;
}

ohci_ed *
allocIntrED(ohci_hcd *hcd, u8 interval)
{
  ohci_ed *rv = NULL;
  u8 i = 0;
  u32 flags;
  ohci_ed *intrEDList =  hcd->intr_list;
  u8 idx = __lg(interval);

  if (idx > 5) idx = 5;

  /* Allocate ED with interval <= requested interval */
  for (i = intrOffsets[idx]; i < NUM_INTR_NODES ; ++i) {
      flags = OHCI_TO_CPU32(intrEDList[i].flags);
      if ( 0 == (flags & OHCI_ED_USED_MASK) ) {
          flags |= OHCI_ED_USED_MASK;
          intrEDList[i].flags = CPU_TO_OHCI32(flags);
          rv = intrEDList + i;
          OHCI_DBG("int = %u : allocated node = %u\n", interval, i);
          goto done;
      }
  }
  /* If you get here no empty slots were found. Allocate a new ED 
   * and link it to the node.
   */
  OHCI_DBG("No pre-allocated ED free for interval: %u\n", interval);

  /* Allocate new ed and enqueue */
  {
      rv = allocED();
      if (rv == NULL) {
          OHCI_ERR("Failed to allocate ED for interrupt ep\n");
          goto done;
      }
      rv->flags = CPU_TO_OHCI32(OHCI_ED_USED_MASK | OHCI_ED_NEW_MASK);
      OHCI_DBG("%s:new ED %08x flags = %08x\n", __FUNCTION__, rv,
              OHCI_TO_CPU32(rv->flags));
      IOS_FlushMem(hcd->hc_wb);
      IOS_InvalidateRdb(IOS_RB_IOD);
      stopList(hcd, OHCI_CTRL_PLE_MASK);
      rv->nextED = intrEDList[intrOffsets[idx]].nextED;
      intrEDList[intrOffsets[idx]].nextED = CPU_TO_OHCI32(rv);
      restartList(hcd, OHCI_CTRL_PLE_MASK);
      IOS_FlushMem(IOS_WB_IOD);
      IOS_InvalidateRdb(hcd->hc_rdb);
  }
  
done:
  return rv;
}

IOSError
enqueueISOED(ohci_hcd *hcd, ohci_ed *ed)
{
    IOSError rv = IOS_ERROR_OK;
    ohci_ed *tmp = &hcd->intr_list[intrOffsets[0]];
    
    OHCI_DBG("Enqueuing ed %p, tail = %p\n", ed, tmp);
    while (NULL != tmp->nextED) {
        tmp = (ohci_ed *)(OHCI_TO_CPU32(tmp->nextED) & 0xfffffff0);
    } 
    tmp->nextED = CPU_TO_OHCI32((u32)ed);
    OHCI_DBG("Enqueued ed %p itd = 0x%08x\n", ed, ed->headP);
    return rv;
}

IOSError
dequeueISOED(ohci_hcd *hcd, ohci_ed *ed)
{
    IOSError rv = IOS_ERROR_OK;
    ohci_ed *tmp = &hcd->intr_list[intrOffsets[0]];
    ohci_ed *prev;
    
    OHCI_DBG("Dequeuing ed %p, tail = %p\n", ed, tmp);
    while (NULL != tmp->nextED) {
        prev = tmp;
        tmp = (ohci_ed *)OHCI_TO_CPU32(tmp->nextED);
        if (ed == tmp) {
            prev->nextED = ed->nextED;
            freeTD((ohci_td *) (OHCI_TO_CPU32(ed->headP) & 0xfffffff0));
            OHCI_DBG("freed td 0x%08x\n", ed->headP);
            freeED(ed);
            OHCI_DBG("freed iso ed %p\n", ed);
            break;
        }
    } 
    OHCI_DBG("Dequeued ed %p\n", ed);
    return rv;
}
