/*
 * (C) Copyright 2001
 * Josh Huber <huber@mclx.com>, Mission Critical Linux, Inc.
 *
 * 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
 */

/*
 * evb64260.c - main board support/init for the Galileo Eval board.
 */

#include <ppcboot.h>
#include <mpc74xx.h>
#include <galileo/memory.h>
#include <galileo/pci.h>
#include <net.h>

#include "eth.h"
#include "mpsc.h"

#undef DEBUG

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

static long int dram_size (long int *, long int);

/* this is the GT register space location where we want it to be */
unsigned int INTERNAL_REG_BASE_ADDR = 0xf0000000;

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

/*
 * This is a version of the GT register space remapping function that
 * doesn't touch globals (meaning, it's ok to run from flash.)
 */

void
my_remap_gt_regs(u32 cur_loc, u32 new_loc)
{
	u32 temp;

	/* check and see if it's already moved */
	temp = in_le32((u32 *)(new_loc + INTERNAL_SPACE_DECODE));
	if ((temp & 0xffff) == new_loc >> 20)
		return;

	temp = (in_le32((u32 *)(cur_loc + INTERNAL_SPACE_DECODE)) &
		0xffff0000) | (new_loc >> 20);

	out_le32((u32 *)(cur_loc + INTERNAL_SPACE_DECODE), temp);

	while (GTREGREAD(INTERNAL_SPACE_DECODE) != temp);
}

/*
 * board_pre_init.
 *
 * set up gal. device mappings, etc.
 */
int
board_pre_init(void)
{
	/*
	 * 0 - SRAM
	 * 1 - RTC
	 * 2 - UART
	 * 3 - Flash
	 * boot - BootCS
	 */

	/*
	 * set up the GT the way the kernel wants it
	 * the call to move the GT register space will obviously
	 * fail if it has already been done, but we're going to assume
	 * that if it's not at the power-on location, it's where we put
	 * it last time. (huber)
	 */
	my_remap_gt_regs(CFG_DFL_GT_REGS, CFG_GT_REGS);

	memoryMapDevice0Space(CFG_DEV0_SPACE, CFG_DEV0_SIZE);
	memoryMapDevice1Space(CFG_DEV1_SPACE, CFG_DEV1_SIZE);
	memoryMapDevice2Space(CFG_DEV2_SPACE, CFG_DEV2_SIZE);
	memoryMapDevice3Space(CFG_DEV3_SPACE, CFG_DEV3_SIZE);

	/* mask all external interrupt sources */
	GT_REG_WRITE(CPU_INTERRUPT_MASK_REGISTER_LOW, 0);
	GT_REG_WRITE(CPU_INTERRUPT_MASK_REGISTER_HIGH, 0);
	GT_REG_WRITE(PCI_0INTERRUPT_CAUSE_MASK_REGISTER_LOW, 0);
	GT_REG_WRITE(PCI_0INTERRUPT_CAUSE_MASK_REGISTER_HIGH, 0);
	GT_REG_WRITE(PCI_1INTERRUPT_CAUSE_MASK_REGISTER_LOW, 0);
	GT_REG_WRITE(PCI_1INTERRUPT_CAUSE_MASK_REGISTER_HIGH, 0);
	GT_REG_WRITE(CPU_INT_0_MASK, 0);
	GT_REG_WRITE(CPU_INT_1_MASK, 0);
	GT_REG_WRITE(CPU_INT_2_MASK, 0);
	GT_REG_WRITE(CPU_INT_3_MASK, 0);

	return 0;
}

/* various things to do after relocation */

void
misc_init_r(bd_t *bd)
{
#ifdef CONFIG_MPSC
	mpsc_init2();
#endif
}

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

/*
 * Check Board Identity:
 *
 * right now, assume borad type. (there is just one...after all)
 */

int
checkboard (void)
{
	int l_type = 0;

	printf ("EVB64260\n");

	return (l_type);
}

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

/* automatic memory sizing */

int
memory_map_bank(unsigned int bankNo,
		unsigned int bankBase,
		unsigned int bankLength) 
{
#ifdef DEBUG
	if (bankLength > 0) {
		printf("\nmapping bank %d at %08x - %08x",
		       bankNo, bankBase, bankBase + bankLength - 1);
	} else {
		printf("\nunmapping bank %d", bankNo);
	}
#endif

        switch(bankNo) {
        case 0:
                memoryMapBank0(bankBase, bankLength);
		pci0MapMemoryBank0(bankBase, bankLength);
		pci1MapMemoryBank0(bankBase, bankLength);
                return(true);
                break;
        case 1:
                memoryMapBank1(bankBase, bankLength);
		pci0MapMemoryBank1(bankBase, bankLength);
		pci1MapMemoryBank1(bankBase, bankLength);
                return(true);
                break;
        case 2:
                memoryMapBank2(bankBase, bankLength);
		pci0MapMemoryBank2(bankBase, bankLength);
		pci1MapMemoryBank2(bankBase, bankLength);
                return(true);
                break;
        case 3:
                memoryMapBank3(bankBase, bankLength);
		pci0MapMemoryBank3(bankBase, bankLength);
		pci1MapMemoryBank3(bankBase, bankLength);
                return(true);
                break;
        default:
                return(0);
        }
}

#define GB         (1 << 29)

long int
initdram(int board_type)
{
        ulong bank_no, realsize, total, check;

#ifdef DEBUG
	int type;

	type = GTREGREAD(0x810);
	printf("\n\nBank 0/1 width: %d\n", type);
	type = GTREGREAD(0x814);
	printf("Bank 1/2 width: %d\n", type);
#endif
	memory_map_bank(0, 0, GB);
	memory_map_bank(1, 0, 0);
	memory_map_bank(2, 0, 0);
	memory_map_bank(3, 0, 0);

	realsize = total = 0;
	check = GB;
	for (bank_no = 0; bank_no < CFG_DRAM_BANKS; bank_no++) {
		if ((total + check) > CFG_GT_REGS)
			check -= CFG_GT_REGS - (total+check);

		memory_map_bank(bank_no, total, check);
		realsize = dram_size((long int *)total, check);

		/* XXX UGLY HACK XXX */
		/* todo: figure out why dram_size is returning >0
		   when there is no memory there [josh]*/
		if (realsize < 10)
			realsize = 0;

		memory_map_bank(bank_no, total, realsize);

		total += realsize;
	}

#ifdef DEBUG
	puts("\n");
#endif

        return(total);
}

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

/*
 * Check memory range for valid RAM. A simple memory test determines
 * the actually available RAM size between addresses `base' and
 * `base + maxsize'. Some (not all) hardware errors are detected:
 * - short between address lines
 * - short between data lines
 */
static long int
dram_size(long int *base, long int maxsize)
{
	volatile long int	 *addr;
	long int		  cnt, val;

	for (cnt = maxsize/sizeof(long); cnt > 0; cnt >>= 1) {
		addr = base + cnt;	/* pointer arith! */

		*addr = ~cnt;
	}

	/* write 0 to base address */
	addr = base;
	*addr = 0;

	/* check at base address */
	if ((val = *addr) != 0) {
		return (0);
	}

	for (cnt = 1; ; cnt <<= 1) {
		addr = base + cnt;	/* pointer arith! */

		val = *addr;

		if (val != (~cnt)) {
			return (cnt * sizeof(long));
		}
	}
	/* NOTREACHED */

	return 0;
}

void
debug_led(int led, int mode)
{
	volatile int *addr;

	if (mode == 1) {
		switch (led) {
		case 0:
			addr = (int *)0x1c808000;
			break;

		case 1:
			addr = (int *)0x1c80c000;
			break;

		case 2:
			addr = (int *)0x1c810000;
			break;
		}
	} else if (mode == 0) {
		switch (led) {
		case 0:
			addr = (int *)0x1c814000;
			break;

		case 1:
			addr = (int *)0x1c818000;
			break;

		case 2:
			addr = (int *)0x1c81c000;
			break;
		}
	}

	*addr = 0;
}

void
perform_soft_reset()
{
    unsigned int CS1baseAddress;
    CS1baseAddress = memoryGetDeviceBaseAddress(DEVICE1);
    WRITE_CHAR((CS1baseAddress | 0x20000),0);
}

#if (CONFIG_COMMANDS & CFG_CMD_NET)

/* 
 * ethernet handlers.  mostly just pass off to routines in
 * eth.c
 */

int
eth_init(bd_t *bis)
{
	return evb64260_eth0_probe(bis);
}

int
eth_send(volatile void *packet, int length)
{
	return evb64260_eth0_transmit(length, packet);
}

int
eth_rx(void)
{
	int len;

	for (;;) {
		len = evb64260_eth0_poll();

		if (len) {
			NetReceive(eth_data, len);
		} else {
			len = -1;
			break;
		}
	}

	return len;
}

void
eth_halt(void)
{
	evb64260_eth0_disable();
}

#endif	/* CFG_CMD_NET */

/* we're not using this right now...btw, it was lifted from the
 * galileo code in Dink32 [josh] */

#if 0

void
display_mem_map(void)
{
	unsigned int base,size,width;
	/* SDRAM */
	printf("SDRAM\n");
	base = memoryGetBankBaseAddress(BANK0);
	size = memoryGetBankSize(BANK0);
	if(size !=0)
	{
		printf("BANK0: base - 0x%x\tsize - %d bytes\n",base,size);
	}
	base = memoryGetBankBaseAddress(BANK1);
	size = memoryGetBankSize(BANK1);
	if(size !=0)
	{
		printf("BANK1: base - 0x%x\tsize - %d bytes\n",base,size);
	}
	base = memoryGetBankBaseAddress(BANK2);
	size = memoryGetBankSize(BANK2);
	if(size !=0)
	{
		printf("BANK2: base - 0x%x\tsize - %d bytes\n",base,size);
	}
	base = memoryGetBankBaseAddress(BANK3);
	size = memoryGetBankSize(BANK3);
	if(size !=0)
	{
		printf("BANK3: base - 0x%x\tsize - %d bytes\n",base,size);
	}
	/* Devices */
	printf("\nDEVICES\n");
	base = memoryGetDeviceBaseAddress(DEVICE0); /* SRAM */
	size = memoryGetDeviceSize(DEVICE0);
	width= memoryGetDeviceWidth(DEVICE0) * 8;
	printf("SRAM:  base - 0x%x\tsize - %d bytes\twidth - %d bits\n",
	       base, size, width);
	base = memoryGetDeviceBaseAddress(DEVICE1); /* RTC */
	size = memoryGetDeviceSize(DEVICE1);
	width= memoryGetDeviceWidth(DEVICE1) * 8;
	printf("RTC:   base - 0x%x\tsize - %d bytes\twidth - %d bits\n",
	       base, size, width);
	base = memoryGetDeviceBaseAddress(DEVICE2); /* UART */
	size = memoryGetDeviceSize(DEVICE2);
	width= memoryGetDeviceWidth(DEVICE2) * 8;
	printf("UART:  base - 0x%x\tsize - %d bytes\twidth - %d bits\n",
	       base, size, width);
	base = memoryGetDeviceBaseAddress(DEVICE3); /* Flash */
	size = memoryGetDeviceSize(DEVICE3);
	width= memoryGetDeviceWidth(DEVICE3) * 8;
	printf("FLASH: base - 0x%x\tsize - %d bytes\twidth - %d bits\n",
	       base, size, width);
	base = memoryGetDeviceBaseAddress(BOOT_DEVICE); /* Boot */
	size = memoryGetDeviceSize(BOOT_DEVICE);
	width= memoryGetDeviceWidth(BOOT_DEVICE) * 8;
	printf("BOOT:  base - 0x%x\tsize - %d bytes\twidth - %d bits\n",
	       base, size, width);
	/* CPU's PCI windows */
	printf("\nCPU's PCI 0 windows\n");
	base = pci0GetMemory0Base();
	size = pci0GetMemory0Size();
	printf("MEMORY 0: base - 0x%x\tsize - %d bytes\n",base,size);
	base = pci0GetMemory1Base();
	size = pci0GetMemory1Size();
	printf("MEMORY 1: base - 0x%x\tsize - %d bytes\n",base,size);
	base = pci0GetMemory2Base();
	size = pci0GetMemory2Size();
	printf("MEMORY 2: base - 0x%x\tsize - %d bytes\n",base,size);
	base = pci0GetMemory3Base();
	size = pci0GetMemory3Size();
	printf("MEMORY 3: base - 0x%x\tsize - %d bytes\n",base,size);
    
	printf("\nCPU's PCI 1 windows\n");
	base = pci1GetMemory0Base();
	size = pci1GetMemory0Size();
	printf("MEMORY 0: base - 0x%x\tsize - %d bytes\n",base,size);
	base = pci1GetMemory1Base();
	size = pci1GetMemory1Size();
	printf("MEMORY 1: base - 0x%x\tsize - %d bytes\n",base,size);
	base = pci1GetMemory2Base();
	size = pci1GetMemory2Size();
	printf("MEMORY 2: base - 0x%x\tsize - %d bytes\n",base,size);
	base = pci1GetMemory3Base();
	size = pci1GetMemory3Size();
	printf("MEMORY 3: base - 0x%x\tsize - %d bytes\n",base,size);
}

#endif
