/*
 *               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 <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(struct 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; 

#if OH0
  struct ohci_ed *intrEDList =  (struct ohci_ed *) OH0_INTRED_START;
  memset((u8 *)OH0_INTRED_START, 0, OH0_INTRED_SPACE_SIZE);
#elif OH1
  struct ohci_ed *intrEDList =  (struct ohci_ed *) OH1_INTRED_START;
  memset((u8 *)OH1_INTRED_START, 0, OH1_INTRED_SPACE_SIZE);
#endif

  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 inline u8 
__lg(u32 p)
{
  u8 i = 0;
  while ((p>>= 1) != 0) ++i;
  return i;
}

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

struct ohci_ed *
allocIntrED(struct ohci_hcd *hcd, u8 interval)
{
  struct ohci_ed *rv = NULL;
  u8 i = 0;
  u32 flags;
  struct 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;
      }
  }
  /* XXX horti: If you get here no empty slots were found. Could
   * allocate a new ED and link it to the node. But don't bother for 
   * now.
   */
done:
  return rv;
}

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