/* Adapted from ATI's initialization code */
/* All non-standard initializations for the USB block. See
 * init_usbpll.c for code to initalize USB (& SDIO) PLLs.
 * HCDs do the stuff described in the standards.
 */
#include <types.h>
#include <iop.h>
#include <ioslibc.h>
#include "sys.h"
#include "sys_int.h"

void
init_USB(u32 chipid_major)
{

  /* Assert all USB resets */
  busWrt32(ACR_USBFRCRST_REG, 0xfe);
  udelay(2);  /* XXX horti: delay not really needed */
  
  /* Initial USB PLL --> good 48MHz clock */
  init_USB_PLL(); 

  /* Deassert USB2COMMON_RESET & wait 50us.
   * --> good clk480 & clk60 outputs from COMMON
   */
  busWrt32(ACR_USBFRCRST_REG, 0xf6); 
  udelay(50);

  /* Deassert EARLYRST_P* & wait 1us --> divided clks propagate 
   * internal to PHY digital
   */
  busWrt32(ACR_USBFRCRST_REG, 0xf4); 
  udelay(1);

  /* Deassert PORTRST_P* & wait 1us  --> PHY digital logic reset &
   * port clk60 output good.
   */
  busWrt32(ACR_USBFRCRST_REG, 0xf0); 
  udelay(1);


  /* Deassert USB HC resets. Waits needed because reset synchronizers
   * need time to cleanly deassert resets in clk domains and stop
   * gating clocks.
   */
  busWrt32(ACR_USBFRCRST_REG, 0x70);  
  udelay(1);
  busWrt32(ACR_USBFRCRST_REG, 0x60); 
  udelay(1);
  busWrt32(ACR_USBFRCRST_REG, 0x40);
  udelay(1);
  busWrt32(ACR_USBFRCRST_REG, 0x00);
  udelay(1);

  /* PHY calibration */
  busWrt32((EHC_BASE + 0x10 + PHYADJ_CTRL_OFFSET), 0x2214); 
  if (0 == chipid_major) { /* ES 1.x */
      busWrt32((EHC_BASE + 0x10 + USB_PHYCMN_CAL_OFFSET), 0x20400);
  }
  else { /* ES 2.x and up */
      busWrt32((EHC_BASE + 0x10 + USB_PHYCMN_CAL_OFFSET), 0x20600);
  }
  busWrt32((EHC_BASE + 0x10 + UTMI_CTRL_OFFSET), 0x00000026);
  udelay(1);
  busWrt32((EHC_BASE + 0x10 + UTMI_CTRL_OFFSET), 0x00002026);
  udelay(1);
  busWrt32((EHC_BASE + 0x10 + UTMI_CTRL_OFFSET), 0x00004026);
  
  /* Wait 20us before using USB, because PHY has internal timer
   * before it will respond to stimulus from HC.
   */
  udelay(20);
  

  /* Initialize other non-standard regs.  */
  /* Set chicken bits. (same as POR values, but set here to be sure) */
  busWrt32((EHC_BASE + 0x10 + USB_CHICKENBITS_OFFSET),
          USB_CHICKENBITS_SET_RECLAMATION_FIX_EN_MASK |
          USB_CHICKENBITS_NO_EHC_RETRYLIMIT_MASK |
          USB_CHICKENBITS_PCI_FRC_NO_BABBLE_MASK);

  /* Set chicken bit to enable USB interrupts in the host controller 
   * driver. 
   */
  /* Ready for drivers to do standard initialization */
}
