/*
 * (C) Copyright 2001
 * Matthias Fuchs, esd gmbh germany, matthias.fuchs@esd-electronics.com
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <ppcboot.h>
#include <asm/processor.h>
#include <command.h>

/* ------------------------------------------------------------------------- */

#define MAX_ONES               226
#define FPGA_PRG_SLEEP         32        /* fpga program sleep-time */

#define IBM405GP_GPIO0_OR      0xef600700  /* GPIO Output */
#define IBM405GP_GPIO0_TCR     0xef600704  /* GPIO Three-State Control */
#define IBM405GP_GPIO0_ODR     0xef600718  /* GPIO Open Drain */
#define IBM405GP_GPIO0_IR      0xef60071c  /* GPIO Input */


void write_1(void)
{
  out32(IBM405GP_GPIO0_OR, 0x05000000);  /* set clock to 0 */
  out32(IBM405GP_GPIO0_OR, 0x05000000);  /* set data to 1 */
  out32(IBM405GP_GPIO0_OR, 0x07000000);  /* set clock to 1 */
  out32(IBM405GP_GPIO0_OR, 0x07000000);  /* set data to 1 */
}

void write_0(void)
{
  out32(IBM405GP_GPIO0_OR, 0x05000000);  /* set clock to 0 */
  out32(IBM405GP_GPIO0_OR, 0x04000000);  /* set data to 0 */
  out32(IBM405GP_GPIO0_OR, 0x06000000);  /* set clock to 1 */
  out32(IBM405GP_GPIO0_OR, 0x07000000);  /* set data to 1 */
}

static int fpga_boot_compressed(void)
{
  int i,index,len;
  unsigned char b;
  int size = sizeof(fpgadata);
  int bit;

  /* display infos on fpgaimage */
  index = 15;
  for (i=0; i<4; i++)
    {
      len = fpgadata[index];
#ifdef FPGA_DEBUG
      printf("FPGA: %s\n", &(fpgadata[index+1]));
#endif
      index += len+3;
    }

  /* search for preamble 0xFF2X */
  for (index = 0; index < size-1 ; index++)
    {
      if ((fpgadata[index] == 0xff) && ((fpgadata[index+1] & 0xf0) == 0x30))
	break;
    }
  index += 2;

#ifdef FPGA_DEBUG
  printf("FPGA: COMPRESSED configdata starts at position 0x%x\n",index);
  printf("FPGA: length of fpga-data %d\n", size-index);
#endif

  /*
   * Setup port pins for fpga programming
   */
  out32(IBM405GP_GPIO0_ODR, 0x00000000);  /* no open drain pins */
  out32(IBM405GP_GPIO0_TCR, 0x07000000);  /* setup for output */
  out32(IBM405GP_GPIO0_OR, 0x07000000);   /* set output pins to high (default) */

#ifdef FPGA_DEBUG
  printf("%s\n",((in32(IBM405GP_GPIO0_IR) & 0x00800000) == 0) ? "NOT DONE" : "DONE" );
#endif

  /*
   * Init fpga by asserting and deasserting PROGRAM*
   */
  out32(IBM405GP_GPIO0_OR, 0x03000000);
  udelay(FPGA_PRG_SLEEP*1000);
#ifdef FPGA_DEBUG
  printf("%s\n",((in32(IBM405GP_GPIO0_IR) & 0x00800000) == 0) ? "NOT DONE" : "DONE" );
#endif
  out32(IBM405GP_GPIO0_OR, 0x07000000);
  udelay(FPGA_PRG_SLEEP*1000);
#ifdef FPGA_DEBUG
  printf("%s\n",((in32(IBM405GP_GPIO0_IR) & 0x00800000) == 0) ? "NOT DONE" : "DONE" );
#endif

#ifdef FPGA_DEBUG
  printf("write configuration data into fpga\n");
#endif
  /* write configuration-data into fpga... */

  /* send 0xff 0x20 */
  write_1();  write_1();  write_1();  write_1();  write_1();  write_1();  write_1();  write_1();
  write_0();  write_0();  write_1();  write_0();  write_0();  write_0();  write_0();  write_0();

  /*
  ** Bit_DeCompression
  **   Code 1           .. maxOnes     : n                 '1's followed by '0'     
  **        maxOnes + 1 .. maxOnes + 1 : n - 1             '1's no '0'
  **        maxOnes + 2 .. 254         : n - (maxOnes + 2) '0's followed by '1'
  **        255                        :                   '1'
  */

  for (i=index; i<size; i++)
    {
      b = fpgadata[i];
      if ((b >= 1) && (b <= MAX_ONES))
	{
	  for(bit=0; bit<b; bit++)
	    write_1();
	  write_0();
	}
      else if (b == (MAX_ONES+1))
	{
	  for(bit=1; bit<b; bit++)
	    write_1();
	}
      else if ((b >= (MAX_ONES+2)) && (b <= 254))
	{
	  for(bit=0; bit<(b-(MAX_ONES+2)); bit++)
	    write_0();	  
	  write_1();
	}
      else if (b == 255)
	write_1();
    }
  udelay(FPGA_PRG_SLEEP*1000);
#ifdef FPGA_DEBUG
  printf("%s\n",((in32(IBM405GP_GPIO0_IR) & 0x00800000) == 0) ? "NOT DONE" : "DONE" );
#endif

  /*
   * Check if fpga's DONE signal - correctly booted ?
   */
  if ((in32(IBM405GP_GPIO0_IR) & 0x00800000) == 0)
    {
#ifdef FPGA_DEBUG
      printf("FPGA: Booting failed!\n");
#endif
      return -1;
    }
  else
    {
#ifdef FPGA_DEBUG
      printf("FPGA: Booting successful!\n");
#endif
      return 0;
    }
}
