/*
 * (C) Copyright 2001
 * Denis Peter, MPL AG Switzerland, d.peter@mpl.ch
 *
 * 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 <command.h> 
#include "common_util.h"
#include <asm/processor.h>
 
extern int  gunzip (void *, int, unsigned char *, int *);
extern int mem_test(unsigned long start, unsigned long ramsize, int quiet);
 
#define I2C_BACKUP_ADDR 0x7C00 /* 0x200 bytes for backup */
#define IMAGE_SIZE 0x80000 

extern flash_info_t flash_info[];	/* info for FLASH chips */

image_header_t header;

 

int mpl_prg(unsigned long src,unsigned long size)
{
	unsigned long start;
	flash_info_t *info;
	int i,rc;
	unsigned long *magic = (unsigned long *)src;
	
	info = &flash_info[0];
  	start = 0 - size;
	for(i=info->sector_count-1;i>0;i--)
	{
		info->protect[i] = 0; /* unprotect this sector */
		if(start>=info->start[i])
		break;
	}
	/* set-up flash location */
	/* now erase flash */
	if(magic[0]!=IH_MAGIC) {
		printf("Bad Magic number\n");
		return -1;
	}
	printf("Erasing at %lx (sector %d) (start %lx)\n",
				start,i,info->start[i]);
	flash_erase (info, i, info->sector_count-1);
	printf("flash erased, programming from 0x%lx 0x%lx Bytes\n",src,size);
	switch (rc = flash_write ((uchar *)src, start, size)) 
	{
		case 0:	printf ("OK programming done\n"); return 0;
		case 1: printf ("ERROR Timeout writing to Flash\n"); return 1;
		case 2:	printf ("ERROR Flash not Erased\n"); return 1;
		case 4: printf ("ERROR Can't write to protected Flash sectors\n"); return 1;
		case 8: printf ("ERROR Outside available Flash\n"); return 1;
		default:	printf ("ERROR %s[%d] FIXME: rc=%d\n",__FILE__,__LINE__,rc); return 1;
	}
  return 1;
}


int mpl_prg_image(unsigned long load_addr)
{
	unsigned long data,len,checksum;
	image_header_t *hdr=&header;  
	/* Copy header so we can blank CRC field for re-calculation */
	memcpy (&header, (char *)load_addr, sizeof(image_header_t));
	if (hdr->ih_magic  != IH_MAGIC) {
		printf ("Bad Magic Number\n");
		return 1;
	}
	print_image_hdr(hdr);
	if (hdr->ih_os  != IH_OS_PPCBOOT) {
		printf ("No PPCBOOT Image\n");
		return 1;
	}
	if (hdr->ih_type  != IH_TYPE_FIRMWARE) {
		printf ("No Firmware Image\n");
		return 1;
	}
	data = (ulong)&header;
	len  = sizeof(image_header_t);
	checksum = hdr->ih_hcrc;
	hdr->ih_hcrc = 0;
	if (crc32 (0, (char *)data, len) != checksum) {
		printf ("Bad Header Checksum\n");
		return 1;
	}
	data = load_addr + sizeof(image_header_t);
	len  = hdr->ih_size;
	printf ("Verifying Checksum ... ");
	if (crc32 (0, (char *)data, len) != hdr->ih_dcrc) {
		printf ("Bad Data CRC\n");
		return 1;
	}
	switch (hdr->ih_comp) {
	case IH_COMP_NONE:
		break;
	case IH_COMP_GZIP:
		printf ("  Uncompressing  ... ");
		if (gunzip ((void *)(data+0x100000), 0x400000,
			    (uchar *)data, (int *)&len) != 0) {
			printf ("GUNZIP ERROR\n");
			return 1;
		}
		data+=0x100000;
		break;
	default:
		printf ("   Unimplemented compression type %d\n", hdr->ih_comp);
		return 1;
	}

	printf ("  OK\n");
	return(mpl_prg(data,len));
} 


void get_backup_values(backup_t *buf)
{
	eeprom_read(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,(void *)buf,sizeof(backup_t));
}

void set_backup_values(int overwrite)
{
	backup_t back;
	int i;

	get_backup_values(&back);
	if(!overwrite) {
		if(strncmp(back.signature,"MPL\0",4)==0) {
			printf("Not possible to write Backup\n");
			return;
		}
	}
	memcpy(back.signature,"MPL\0",4);
	i=getenv_r("serial#",back.serial_name,16);
	if(i==0) {
		printf("Not possible to write Backup\n");
		return;
	}
	back.serial_name[16]=0;
	i=getenv_r("ethaddr",back.eth_addr,20);
	if(i==0) {
		printf("Not possible to write Backup\n");
		return;
	}
	back.eth_addr[20]=0;
	eeprom_write(CFG_DEF_EEPROM_ADDR, I2C_BACKUP_ADDR,(void *)&back,sizeof(backup_t));
}

void clear_env_values(void)
{
	backup_t back;
	unsigned char env_crc[4];
	
	memset(&back,0xff,sizeof(backup_t));
	memset(env_crc,0x00,4);
	eeprom_write(CFG_DEF_EEPROM_ADDR,I2C_BACKUP_ADDR,(void *)&back,sizeof(backup_t));
	eeprom_write(CFG_DEF_EEPROM_ADDR,CFG_ENV_OFFSET,(void *)env_crc,4);
}
	
/* ------------------------------------------------------------------------- */

	/* switches the cs0 and the cs1 to the locations. 
	   When boot is TRUE, the the mapping is switched 
	   to the boot configuration, If it is FALSE, the
	   flash will be switched in the boot area */

#undef SW_CS_DBG
#ifdef SW_CS_DBG
#define	SW_CS_PRINTF(fmt,args...)	printf (fmt ,##args)
#else
#define SW_CS_PRINTF(fmt,args...)
#endif


int switch_cs(unsigned char boot)
{
  	unsigned long pbcr;
	mtdcr(ebccfga, pb0cr); /* get cs0 config reg */
	pbcr = mfdcr(ebccfgd);
	if((pbcr&0x00002000)==0) {  
		/* we need only to switch if boot from MPS */
		/*printf(" MPS boot mode detected. ");*/ 
		/* printf("cs0 cfg: %lx\n",pbcr); */
		if(boot) { 
			/* switch to boot configuration */
			/* this is a 8bit boot, switch cs0 to flash location */
			SW_CS_PRINTF("switch to boot mode (MPS on High address\n");  
			pbcr&=0x000FFFFF; /*mask base address of the cs0 */
			pbcr|=(FLASH_BASE0_PRELIM & 0xFFF00000);
			mtdcr(ebccfga, pb0cr);
			mtdcr(ebccfgd, pbcr);
			SW_CS_PRINTF("  new cs0 cfg: %lx\n",pbcr); 
			mtdcr(ebccfga, pb1cr); /* get cs1 config reg (flash) */
			pbcr = mfdcr(ebccfgd);
			SW_CS_PRINTF(" old cs1 cfg: %lx\n",pbcr); 
			pbcr&=0x000FFFFF; /*mask base address of the cs1 */
			pbcr|=(MULTI_PURPOSE_SOCKET_ADDR & 0xFFF00000);
			mtdcr(ebccfga, pb1cr);
			mtdcr(ebccfgd, pbcr);
			SW_CS_PRINTF("  new cs1 cfg: %lx, MPS is on High Address\n",pbcr); 
		}
		else 
		{ 
			/* map flash to boot area, */
			SW_CS_PRINTF("map Flash to boot area\n"); 
			pbcr&=0x000FFFFF; /*mask base address of the cs0 */
			pbcr|=(MULTI_PURPOSE_SOCKET_ADDR & 0xFFF00000);
			mtdcr(ebccfga, pb0cr);
			mtdcr(ebccfgd, pbcr);
			SW_CS_PRINTF("  new cs0 cfg: %lx\n",pbcr); 
			mtdcr(ebccfga, pb1cr); /* get cs1 config reg (flash) */
			pbcr = mfdcr(ebccfgd);
			SW_CS_PRINTF("  cs1 cfg: %lx\n",pbcr); 
			pbcr&=0x000FFFFF; /*mask base address of the cs1 */
			pbcr|=(FLASH_BASE0_PRELIM & 0xFFF00000);
			mtdcr(ebccfga, pb1cr);
			mtdcr(ebccfgd, pbcr);
			SW_CS_PRINTF("  new cs1 cfg: %lx Flash is on High Address\n",pbcr); 
		}
		return 1;
	}
	else {
		SW_CS_PRINTF("Normal boot, no switching necessary\n"); 
		return 0;
	}
}


int do_mplcommon(cmd_tbl_t *cmdtp, bd_t *bd, int flag, int argc, char *argv[])
{
 	ulong size,src,load_addr;
	int result;	
	backup_t back;
	src = MULTI_PURPOSE_SOCKET_ADDR;
	size = IMAGE_SIZE;

	if (strcmp(argv[1], "flash") == 0) 
	{
#if (CONFIG_COMMANDS & CFG_CMD_FDC)
		if (strcmp(argv[2], "floppy") == 0) {
 			char *local_args[3];
			extern int do_fdcboot (cmd_tbl_t *, bd_t *, int, int, char *[]);
			printf ("\nupdating bootloader image from floppy\n");
			local_args[0] = argv[0];
	    		if(argc==4) {
				local_args[1] = argv[3];
				local_args[2] = NULL;
				load_addr=simple_strtoul(argv[3], NULL, 16);
				result=do_fdcboot(cmdtp, bd, 0, 2, local_args);
			}
			else {
				local_args[1] = NULL;
				load_addr=CFG_LOAD_ADDR;
				result=do_fdcboot(cmdtp, bd, 0, 1, local_args);
			}
			result=mpl_prg_image(load_addr); 
			return result;
		}
#endif /* (CONFIG_COMMANDS & CFG_CMD_FDC) */
		if (strcmp(argv[2], "mem") == 0) {
	    		if(argc==4) {
				load_addr=simple_strtoul(argv[3], NULL, 16);
			}
			else {
				load_addr=CFG_LOAD_ADDR;
			}
			printf ("\nupdating bootloader image from memory at %lX\n",load_addr);
			result=mpl_prg_image(load_addr); 
			return result;
		}
		if (strcmp(argv[2], "mps") == 0) {
			printf ("\nupdating bootloader image from MSP\n");
			result=mpl_prg(src,size);
			return result;
		}
		
	}
	if (strcmp(argv[1], "mem") == 0) 
	{
		result=0;
		if(argc==3)
		{
			result = (int)simple_strtol(argv[2], NULL, 16);
	    }
	    src=(unsigned long)&result;
	    src-=CFG_MEMTEST_START;
	    src-=(100*1024); /* - 100k */
	    src&=0xfff00000;
	    size=0;
	    do {
	    	size++;
			printf("\n\nPass %ld\n",size);
			mem_test(CFG_MEMTEST_START,src,1);
			if(ctrlc())
				break;
			if(result>0)
				result--;
			
		}while(result);
		return 0;
	}
 	if (strcmp(argv[1], "clearenvvalues") == 0) 
	{
 		if (strcmp(argv[2], "yes") == 0)
		{
			clear_env_values();
			return 0;
		}
	}
	if (strcmp(argv[1], "getback") == 0) {
		get_backup_values(&back);
		back.signature[3]=0;
		back.serial_name[16]=0;
		back.eth_addr[20]=0;
		printf("GetBackUp: signature: %s\n",back.signature);
		printf("           serial#:   %s\n",back.serial_name);
		printf("           ethaddr:   %s\n",back.eth_addr);
		return 0;
	} 
	if (strcmp(argv[1], "setback") == 0) {
		set_backup_values(1);
		return 0;
	}
#if 0
	if (strcmp(argv[1], "test") == 0) {
		char *local_args[6];
		int i;
		extern int do_doc(cmd_tbl_t *, bd_t *, int, int, char *[]);
		local_args[0] = argv[0];
	    for(i=1;i<5;i++) {
	    	if(i+1<argc) {
				local_args[i]=argv[i+1];
			}
			else {
				local_args[i]=NULL;
			}
		}
		local_args[5]=NULL;
		result=do_doc(cmdtp, bd, 0, argc-1, local_args);
		return 0;
	}
	if (strcmp(argv[1], "test1") == 0) {
		char *local_args[6];
		int i;
		extern int do_docboot(cmd_tbl_t *, bd_t *, int, int, char *[]);
		local_args[0] = argv[0];
	    for(i=1;i<5;i++) {
	    	if(i+1<argc) {
				local_args[i]=argv[i+1];
			}
			else {
				local_args[i]=NULL;
			}
		}
		local_args[5]=NULL;
		result=do_docboot(cmdtp, bd, 0, argc-1, local_args);
		return 0;
	}
#endif
	printf("Usage:\n%s\n", cmdtp->usage);
	return 1;
}


#if (CONFIG_COMMANDS & CFG_CMD_DOC)
extern void doc_probe(ulong physadr);
void doc_init (void)
{
  doc_probe(MULTI_PURPOSE_SOCKET_ADDR);
}
#endif
