/*	$Id: pci_machdep.c,v 1.1.1.1 2006/08/23 17:03:03 pefo Exp $ */

/*
 * Copyright (c) 2002 Opsycon AB  (www.opsycon.se)
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by Opsycon AB, Sweden.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/param.h>
#include <sys/device.h>
#include <sys/systm.h>

#include <sys/malloc.h>

#include <dev/pci/pcivar.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/nppbreg.h>

#include <machine/bus.h>

#include "target/cp7000.h"
#include "pmon/dev/gt64120reg.h"

#include <pmon.h>

extern void *pmalloc __P((size_t ));

#define PCI_IO_SPACE_PCI_BASE	0x00000000	/* PCI i/o in PCI space */
#define PCI_LOCAL_MEM_PCI_BASE	0x00000000	/* CPU Mem accessed from PCI */

/* soft versions of above */
static pcireg_t pci_local_mem_pci_base;

static pcireg_t _pci_conf_readn __P((pcitag_t, int, int));
static void _pci_conf_writen __P((pcitag_t, int, pcireg_t, int));
extern int _pciverbose;

struct pci_bus *_pci_bus[16];
int _max_pci_bus = 0;


/*
 * Called to initialise the bridge at the beginning of time
 */
int
_pci_hwinit (initialise, iot, memt)
	int initialise;
	bus_space_tag_t iot;
	bus_space_tag_t memt;
{
	pcireg_t stat;
	struct pci_device *pcidev;
	struct pci_bus *pcibus;
	u_int32_t b0Base, b0Size, b1Base, b1Size;

	/*
	 *  PCI and local bus maps 1-1
	 */
	iot->bus_base = 0;
	iot->bus_reverse = 1;
	memt->bus_base = 0;
	memt->bus_reverse = 1;

	pci_local_mem_pci_base = PCI_LOCAL_MEM_PCI_BASE;

	/*
	 *  Allocate and initialize PCI bus heads.
	 */

	/*
	 * PCI Bus 0
	 */
	pcidev = pmalloc(sizeof(struct pci_device));
	pcibus = pmalloc(sizeof(struct pci_bus));
	if(pcidev == NULL || pcibus == NULL) {
		printf("pci: can't alloc memory. pci not initialized\n");
		return(-1);
	}

	pcidev->pa.pa_flags = PCI_FLAGS_IO_ENABLED | PCI_FLAGS_MEM_ENABLED;
	pcidev->pa.pa_iot = iot;
	pcidev->pa.pa_memt = memt;
	pcidev->pa.pa_dmat = &bus_dmamap_tag;
	pcidev->bridge.secbus = pcibus;
	_pci_head = pcidev;

	pcibus->minpcimemaddr  = PCI0_MEM_SPACE_BASE;
	pcibus->nextpcimemaddr = PCI0_MEM_SPACE_BASE + PCI0_MEM_SPACE_SIZE;
	pcibus->minpciioaddr  = PCI0_IO_SPACE_BASE;
	pcibus->nextpciioaddr = PCI0_IO_SPACE_BASE + PCI0_IO_SPACE_SIZE;
	pcibus->pci_mem_base   = PCI0_MEM_SPACE_BASE; /* Maps 1-1 */
	pcibus->pci_io_base    = PCI0_IO_SPACE_BASE; /* Maps 1-1 */
	pcibus->max_lat = 255;
	pcibus->fast_b2b = 1;
	pcibus->prefetch = 1;
	pcibus->bandwidth = 4000000;
	pcibus->ndev = 1;
	_pci_bushead = pcibus;
	_pci_bus[_max_pci_bus++] = pcibus; 

	register_mem((void *)PCI0_MEM_SPACE_BASE,
			(void *)PCI0_MEM_SPACE_BASE+PCI0_MEM_SPACE_SIZE-1,
			MEM_IO, "PCI memory");
	register_mem((void *)PCI0_IO_SPACE_BASE,
			(void *)PCI0_IO_SPACE_BASE+PCI0_IO_SPACE_SIZE-1,
			MEM_IO, "PCI I/O");
	/*
	 *  Enable PCI 0 as master to do config cycles.
	 */
	stat = _pci_conf_read(_pci_make_tag(0, 0, 0), PCI_COMMAND_STATUS_REG);
	stat |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;
	_pci_conf_write(_pci_make_tag(0, 0, 0), PCI_COMMAND_STATUS_REG, stat);

	/*
	 *  Set up CPU to PCI mapping.
	 */
	GT_PAS_PCI0IOLO = htole32((PCI0_IO_SPACE_BASE >> 21) & 0x7ff);
	GT_PAS_PCI0IOHI = htole32(((PCI0_IO_SPACE_BASE + PCI0_IO_SPACE_SIZE - 1) >> 21) & 0x7f);
	GT_PAS_PCI0MEM0LO = htole32((PCI0_MEM_SPACE_BASE >> 21) & 0x7ff);
	GT_PAS_PCI0MEM0HI = htole32(((PCI0_MEM_SPACE_BASE + PCI0_MEM_SPACE_SIZE - 1) >> 21) & 0x7f);

	/*
	 *  Set up mapping for PCI to localmem accesses.
	 */
	b0Base = letoh32(GT_PAS_RAS10LO) << 21;
	b0Size = (letoh32(GT_PAS_RAS10HI) + 1) << 21;
	if(b0Size < b0Base) {
		b0Size = 0;
		b0Base = 0;
	}
	else {
		b0Size -= b0Base + 1;
	}
	b1Base = letoh32(GT_PAS_RAS32LO) << 21;
	b1Size = b1Base + ((letoh32(GT_PAS_RAS32HI) + 1) << 21);
	if(b1Size < b1Base) {
		b1Size = 0;
		b1Base = 0;
	}
	else {
		b1Size -= b1Base + 1;
	}

	stat = _pci_conf_read(_pci_make_tag(0, 0, 0), GT_PCI0_MAP10) & 0xffff;
	_pci_conf_write(_pci_make_tag(0, 0, 0), GT_PCI0_MAP10, b0Base | stat);
	stat = _pci_conf_read(_pci_make_tag(0, 0, 0), GT_PCI0_MAP14) & 0xffff;
	_pci_conf_write(_pci_make_tag(0, 0, 0), GT_PCI0_MAP14, b1Base | stat);
	stat = _pci_conf_read(_pci_make_tag(0, 0, 0), GT_PCI1_MAP10) & 0xffff;
	_pci_conf_write(_pci_make_tag(0, 0, 0), GT_PCI1_MAP10, b0Base | stat);
	stat = _pci_conf_read(_pci_make_tag(0, 0, 0), GT_PCI1_MAP14) & 0xffff;
	_pci_conf_write(_pci_make_tag(0, 0, 0), GT_PCI1_MAP14, b1Base | stat);

	/* This BAR appears to be impossible to turn off. Remap it */
	stat = GT64120_BASE;
	stat |= _pci_conf_read(_pci_make_tag(0, 0, 0), GT_PCI0_MAP20) & 0xffff;
	_pci_conf_write(_pci_make_tag(0, 0, 0), GT_PCI0_MAP20, stat);
	stat = GT64120_BASE;
	stat |= _pci_conf_read(_pci_make_tag(0, 0, 0), GT_PCI1_MAP20) & 0xffff;
	_pci_conf_write(_pci_make_tag(0, 0, 0), GT_PCI1_MAP20, stat);


	GT_PCI0_RAS10SIZE = htole32(b0Size);
	GT_PCI0_RAS32SIZE = htole32(b1Size);
	GT_PCI1_RAS10SIZE = htole32(b0Size);
	GT_PCI1_RAS32SIZE = htole32(b1Size);
	

	if (!initialise) {
		return(0);
	}

	return(1);
}


/*
 * Called to reinitialise the bridge after we've scanned each PCI device
 * and know what is possible. We also set up the interrupt controller
 * routing and level control registers.
 */
void
_pci_hwreinit (void)
{
}

void
_pci_flush (void)
{
}


/*
 *  Map the CPU virtual address of an area of local memory to a PCI
 *  address that can be used by a PCI bus master to access it.
 */
vm_offset_t
_pci_dmamap(va, len)
	vm_offset_t va;
	unsigned int len;
{
	return(pci_local_mem_pci_base + VA_TO_PA (va));
}

/*
 *  Map the PCI address of an area of local memory to a CPU physical
 *  address.
 */
vm_offset_t
_pci_cpumap(pcia, len)
	vm_offset_t pcia;
	unsigned int len;
{
	return PA_TO_VA(pcia - pci_local_mem_pci_base);
}


/*
 *  Make pci tag from bus, device and function data.
 */
pcitag_t
_pci_make_tag(bus, device, function)
	int bus;
	int device;
	int function;
{
	pcitag_t tag;

	tag = (bus << 16) | (device << 11) | (function << 8);
	return(tag);
}

/*
 *  Break up a pci tag to bus, device function components.
 */
void
_pci_break_tag(tag, busp, devicep, functionp)
	pcitag_t tag;
	int *busp;
	int *devicep;
	int *functionp;
{
	if (busp) {
		*busp = (tag >> 16) & 255;
	}
	if (devicep) {
		*devicep = (tag >> 11) & 31;
	}
	if (functionp) {
		*functionp = (tag >> 8) & 7;
	}
}

int
_pci_canscan (pcitag_t tag)
{
	int bus, device, function;

	_pci_break_tag (tag, &bus, &device, &function); 

	/* Ignore bridge itself and DEC-ethernet chip */
	if((bus == 0 && device == 0) ||
	   (bus == 0 && device == 2) ||
	   (bus == 0 && device == 3)) {
		return(0);
	}
	return (1);
}

/*
 *  Read a value form PCI configuration space. Support for
 *  all three data sizes (byte, halfword and word) is provided.
 */
static pcireg_t
_pci_conf_readn(tag, reg, width)
	pcitag_t tag;
	int reg;
	int width;
{
	pcireg_t data;
	u_int32_t addr;
	int bus, device, function;

	if (reg & (width-1) || reg < 0 || reg >= 0x100) {
		if (_pciverbose >= 1) {
			_pci_tagprintf (tag, "_pci_conf_read: bad reg 0x%x\r\n", reg);
		}
		return ~0;
	}

	_pci_break_tag (tag, &bus, &device, &function); 
	if (bus == 0) {
		/* Type 0 configuration on onboard PCI bus */
		if (device > 29) {
			return ~0;		/* device out of range */
		}
		addr = (device << 11) | (function << 8) | reg;
	}
	else {
		return ~0;		/* bus out of range */
	}

	if(bus == 0) {
		GT_PCI0_CFGADDR = htole32(GT_IPCI_CFGADDR_ConfigEn | tag | reg);
		data = (pcireg_t)GT_PCI0_CFGDATA;
	}
	else {
		GT_PCI1_CFGADDR = htole32(GT_IPCI_CFGADDR_ConfigEn | tag | reg);
		data = (pcireg_t)GT_PCI1_CFGDATA;
	}

	if(device == 0) {
		data = letoh32(data);
	}

	return data;
}


pcireg_t
_pci_conf_read(pcitag_t tag, int reg)
{
	pcireg_t dat, stat;

	/* Due to an errata in the GT64120A, reads to non-existent config */
	/* locations does not guarantee a return of 0xffffffff.  The only */
	/* way to check is to look at the Master Abort bit. Clear bit     */
	stat = _pci_conf_readn(_pci_make_tag(0, 0, 0), PCI_COMMAND_STATUS_REG, 4);
	stat |= PCI_STATUS_MASTER_ABORT;
	_pci_conf_writen(_pci_make_tag(0, 0, 0), PCI_COMMAND_STATUS_REG, stat, 4);

    	dat = _pci_conf_readn (tag, reg, 4);

	/* Check Master Abort for failure condition                       */
	stat = _pci_conf_readn(_pci_make_tag(0, 0, 0), PCI_COMMAND_STATUS_REG, 4);
	if (stat & PCI_STATUS_MASTER_ABORT) {
		 return ((pcireg_t)0xffffffff);
	}

	return (dat);
}

/*
 *  Write a value to PCI configuration space. Support for
 *  all three data sizes (byte, halfword and word) is provided.
 */
static void
_pci_conf_writen(tag, reg, data, width)
	pcitag_t tag;
	int reg;
	pcireg_t data;
	int width;
{
    u_int32_t addr;
    int bus, device, function;

	if (reg & (width-1) || reg < 0 || reg >= 0x100) {
		if (_pciverbose >= 1) { 
			_pci_tagprintf(tag, "_pci_conf_write: bad reg 0x%x\r\n", reg);
		}
		return;
	}

	_pci_break_tag (tag, &bus, &device, &function);

	if (bus == 0 || bus == 1) {
		/* Type 0 configuration on onboard PCI buses */
		if (device > 29) {
			return;		/* device out of range */
		}
		addr = (device << 11) | (function << 8) | reg;
	}
	else {
		_pci_tagprintf(tag, "_pci_conf_read: bus %d not supported!\r\n", bus);
		return;			/* bus out of range */
	}

	if(device == 0) {
		data = letoh32(data);
	}

	if(bus == 0) {
		GT_PCI0_CFGADDR = htole32(GT_IPCI_CFGADDR_ConfigEn | tag | reg);
		GT_PCI0_CFGDATA = data;
	}
	else {
		GT_PCI1_CFGADDR = htole32(GT_IPCI_CFGADDR_ConfigEn | tag | reg);
		GT_PCI1_CFGDATA = data;
	}
}

void
_pci_conf_write(pcitag_t tag, int reg, pcireg_t data)
{
    _pci_conf_writen (tag, reg, data, 4);
}


/*
 *  Get contents of PCI Mapping register and do any machine
 *  dependent mapping setup.
 */
int
_pci_map_port(tag, reg, port)
	pcitag_t tag;
	int reg;
	unsigned int *port;
{
	pcireg_t address;
    
	if (reg < PCI_MAPREG_START || reg >= PCI_MAPREG_END || (reg & 3)) {
		if (_pciverbose >= 1) {
			_pci_tagprintf(tag, "_pci_map_port: bad request\r\n");
		}
		return -1;
	}
    
	address = _pci_conf_read(tag, reg);
    
	if (PCI_MAPREG_TYPE(address) != PCI_MAPREG_TYPE_IO) {
		if (_pciverbose >= 1) {
			_pci_tagprintf (tag, "_pci_map_port: attempt to i/o map a memory region\r\n");
		}
		return(-1);
	}

	*port = (address & PCI_MAPREG_IO_ADDR_MASK) - PCI_IO_SPACE_PCI_BASE;
	return(0);
}

void *
_pci_map_int(tag, level, func, arg)
	pcitag_t tag;
	int level;
	int (*func) __P((void *));
	void *arg;
{
	pcireg_t data;
	int pin, bus, device;

	data = _pci_conf_read(tag, PCI_INTERRUPT_REG);

	pin = PCI_INTERRUPT_PIN(data);

	if (pin == 0) {
		/* No IRQ used. */
		return NULL;
	}

	if (pin > 4) {
		if (_pciverbose >= 1) {
			_pci_tagprintf (tag, "_pci_map_int: bad interrupt pin %d\r\n", pin);
		}
		return(NULL);
	}


	_pci_break_tag (tag, &bus, &device, NULL);

	if (bus != 0 || device > 5) {
		return(NULL);
	}

	/* XXX need to work this out based on device number etc. */
	_pci_tagprintf(tag, "_pci_map_int: attempt to map device %d pin %c\n", 
		   device, '@' + pin);
	return(NULL);
}

void
pci_sync_cache(p, adr, size, rw)
	void *p;
	vm_offset_t adr;
	size_t size;
	int rw;
{
	CPU_IOFlushDCache(adr, size, rw);
}

