⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pci_machdep.c

📁 MIPS处理器的bootloader,龙芯就是用的修改过的PMON2
💻 C
字号:
/*	$Id: pci_machdep.c,v 1.4 2003/08/30 15:07:32 pefo Exp $ *//* * Copyright (c) 2000-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 <dev/pci/pcivar.h>#include <dev/pci/pcireg.h>#include <machine/bus.h>#include <machine/pio.h>#include "target/db64360.h"#include "pmon/dev/mv64360reg.h"#include <pmon.h>/* PCI i/o regions in PCI space */#define PCI_IO_SPACE_PCI_BASE		0x00000000/* PCI mem regions 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;extern int _pciverbose;extern void *pmalloc __P((size_t ));static struct pci_bus *_pci_bus[16];static int _max_pci_bus = 0;struct bartab {	int	csbase,	cssize;	int	pci0size, pci1size;	int	barh, barl;} barlist[] = {{ CS_0_BASE_ADDR, CS_0_SIZE,  PCI_0_CS_0_BANK_SIZE, PCI_1_CS_0_BANK_SIZE,  PCI_SCS_0_BASE_ADDR_HIGH, PCI_SCS_0_BASE_ADDR_LOW },{ CS_1_BASE_ADDR, CS_1_SIZE,  PCI_0_CS_1_BANK_SIZE, PCI_1_CS_1_BANK_SIZE,  PCI_SCS_1_BASE_ADDR_HIGH, PCI_SCS_1_BASE_ADDR_LOW },{ CS_2_BASE_ADDR, CS_2_SIZE,  PCI_0_CS_2_BANK_SIZE, PCI_1_CS_2_BANK_SIZE,  PCI_SCS_2_BASE_ADDR_HIGH, PCI_SCS_2_BASE_ADDR_LOW },{ CS_3_BASE_ADDR, CS_3_SIZE,  PCI_0_CS_3_BANK_SIZE, PCI_1_CS_3_BANK_SIZE,  PCI_SCS_3_BASE_ADDR_HIGH, PCI_SCS_3_BASE_ADDR_LOW },};#define NBARS (sizeof(barlist) / sizeof(struct bartab))/* * 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;	pcitag_t tag;	int i;	struct pci_device *pcidev;	struct pci_bus *pcibus;        /*         *  PCI and local bus maps 1-1         */        iot->bus_base = PA_TO_VA(0);        iot->bus_reverse = 1;        memt->bus_base = PA_TO_VA(0);        memt->bus_reverse = 1;	/*	 *  Where PCI finds our RAM memory in the PCI map	 */        pci_local_mem_pci_base = PCI_LOCAL_MEM_PCI_BASE;	if (!initialise) {		return(0);	}	/*	 *  Allocate and initialize PCI bus heads.	 */	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;	/*	 * PCI Bus 1	 */	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 = *_pci_head;	pcidev->bridge.secbus = pcibus;	_pci_head->next = pcidev;	*pcibus = *_pci_bushead;	pcibus->minpcimemaddr  = PCI1_MEM_SPACE_BASE;	pcibus->nextpcimemaddr = PCI1_MEM_SPACE_BASE + PCI1_MEM_SPACE_SIZE;	pcibus->minpciioaddr  = PCI1_IO_SPACE_BASE;	pcibus->nextpciioaddr = PCI1_IO_SPACE_BASE + PCI1_IO_SPACE_SIZE;	pcibus->pci_mem_base   = PCI1_MEM_SPACE_BASE; /* Maps 1-1 */	pcibus->pci_io_base    = PCI1_IO_SPACE_BASE; /* Maps 1-1 */	_pci_bushead->next = pcibus;	_pci_bus[_max_pci_bus++] = pcibus;        /*         *  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);        /*         *  Enable PCI 1 as master to do config cycles.         */        stat = _pci_conf_read(_pci_make_tag(1, 0, 0), PCI_COMMAND_STATUS_REG);        stat |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE;        _pci_conf_write(_pci_make_tag(1, 0, 0), PCI_COMMAND_STATUS_REG, stat);	/*	 *  Set up CPU to PCI mappings. Disable all but PCI_0_IO and	 *  PCI_0_MEMORY0 as well as PCI_1_IO and PCI_1_MEMORY0.	 */	stat = GT_READ(BASE_ADDR_ENABLE);	stat |= (1<<11) | (1<<12) | (1<<13) | (1<<16) | (1<<17) | (1<<18);	stat &= ~((1<<9) | (1<<10) | (1<<14) | (1<<15));	GT_WRITE(BASE_ADDR_ENABLE, stat);	GT_WRITE(PCI_0_IO_BASE_ADDR, PCI0_IO_SPACE_BASE >> 16);        GT_WRITE(PCI_0_IO_SIZE, (PCI0_IO_SPACE_SIZE - 1) >> 16);	GT_WRITE(PCI_0_MEMORY0_BASE_ADDR, PCI0_MEM_SPACE_BASE >> 16);        GT_WRITE(PCI_0_MEMORY0_SIZE, (PCI0_MEM_SPACE_SIZE - 1) >> 16);	GT_WRITE(PCI_1_IO_BASE_ADDR, PCI1_IO_SPACE_BASE >> 16);        GT_WRITE(PCI_1_IO_SIZE, (PCI1_IO_SPACE_SIZE - 1) >> 16);	GT_WRITE(PCI_1_MEMORY0_BASE_ADDR, PCI1_MEM_SPACE_BASE >> 16);        GT_WRITE(PCI_1_MEMORY0_SIZE, (PCI1_MEM_SPACE_SIZE - 1) >> 16);	        /*         *  Set up mapping for PCI to localmem accesses.	 *  config regs to find mapping and size. BAR and	 *  size register should be set to match SDRAM CS.         */	stat = 0xfffffef0;	stat |= GT_READ(BASE_ADDR_ENABLE) & 0x000f;	/* Disable inactive */	GT_WRITE(PCI_0_BASE_ADDR_REG_ENABLE, stat);	stat = 0xfffffef0;	stat |= GT_READ(BASE_ADDR_ENABLE) & 0x000f;	/* Disable inactive */	GT_WRITE(PCI_1_BASE_ADDR_REG_ENABLE, stat);	for(i = 0; i < NBARS; i++) {		u_int32_t csbase, cssize;		csbase = GT_READ(barlist[i].csbase) << 16;		cssize = (GT_READ(barlist[i].cssize) + 1) << 16;		cssize -= 1;		tag = _pci_make_tag(0, 0, (barlist[i].barl & 0x700) >> 8);		stat = _pci_conf_read(tag, barlist[i].barl & 0xff) & 0xfff;		stat |= csbase & 0xfffff000;		_pci_conf_write(tag, barlist[i].barl & 0xff, stat);		_pci_conf_write(tag, barlist[i].barh & 0xff, 0);		tag = _pci_make_tag(1, 0, (barlist[i].barl & 0x700) >> 8);		stat = _pci_conf_read(tag, barlist[i].barl & 0xff) & 0xfff;		stat |= csbase & 0xfffff000;		_pci_conf_write(tag, barlist[i].barl & 0xff, stat);		_pci_conf_write(tag, barlist[i].barh & 0xff, 0);		GT_WRITE(barlist[i].pci0size, cssize & 0xfffff000);		GT_WRITE(barlist[i].pci1size, cssize & 0xfffff000);	}	stat = GT_READ(INTERNAL_SPACE_BASE_ADDR) << 16;	tag = _pci_make_tag(0, 0, 0);	_pci_conf_write(tag, PCI_INTERNAL_REG_MEM_MAPPED_BASE_ADDR_LO, stat);	_pci_conf_write(tag, PCI_INTERNAL_REG_MEM_MAPPED_BASE_ADDR_HIGH, 0x0);	tag = _pci_make_tag(1, 0, 0);	_pci_conf_write(tag, PCI_INTERNAL_REG_MEM_MAPPED_BASE_ADDR_LO, stat);	_pci_conf_write(tag, PCI_INTERNAL_REG_MEM_MAPPED_BASE_ADDR_HIGH, 0x0);	/*	 *  This target uses the internal arbiter	 */	stat = GT_READ(PCI_0_ARBITER_CONTROL);	stat |= 0x80000000;	GT_WRITE(PCI_0_ARBITER_CONTROL, stat);	stat = GT_READ(PCI_1_ARBITER_CONTROL);	stat |= 0x80000000;	GT_WRITE(PCI_1_ARBITER_CONTROL, stat);        return(2);}/* * Called to reinitialise the bridge after we've scanned each PCI device * and know what is possible. */void_pci_hwreinit (void){/* XXX We should probably set up the bus limits here... */}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);	if((bus == 0 || bus == 1) && (device == 0 || device == 31)) {		return(0);		/* Ignore the Discovery itself */	}	return (1);}/* *  Read a value from PCI configuration space. */pcireg_t_pci_conf_read(tag, reg)	pcitag_t tag;	int reg;{	pcireg_t data;	u_int32_t adr;	int bus, device, function;	int tmp;	if (reg < 0 || reg >= 0x100) {		if (_pciverbose >= 1) {			_pci_tagprintf (tag, "_pci_conf_readn: bad reg 0x%x\r\n", reg);		}		return ~0;	}        _pci_break_tag (tag, &bus, &device, &function);        /* Type 0 configuration on onboard PCI bus */        if (device > 31 || function > 7) {                return ~0;              /* device out of range */        }        adr = (device << 11) | (function << 8) | reg | GT_IPCI_CFGADDR_ConfigEn;        if(bus == 0) {		tmp = GT_READ(PCI_0_P2P_CONFIG);		adr |= tmp & 0xff0000;		/* Use the bus we are on */		if (device == 0) { 		/* 0 is always the Disco chip */			adr |= (tmp >> (24-11)) & (0x1f << 11);		}                GT_WRITE(PCI_0_CONFIG_ADDR, adr);                data = GT_READ(PCI_0_CONFIG_DATA_VIRTUAL_REG);        }        else {		tmp = GT_READ(PCI_1_P2P_CONFIG);		adr |= tmp & 0xff0000;		/* Use the bus we are on */		if (device == 0) { 		/* 0 is always the Disco chip */			adr |= (tmp >> (24-11)) & (0x1f << 11);		}                GT_WRITE(PCI_1_CONFIG_ADDR, adr);                data = GT_READ(PCI_1_CONFIG_DATA_VIRTUAL_REG);        }        return data;}/* *  Write a value to PCI configuration space. */void_pci_conf_write(tag, reg, data)	pcitag_t tag;	int reg;	pcireg_t data;{	u_int32_t adr;	int bus, device, function;	int tmp;	if (reg < 0 || reg >= 0x100) {		if (_pciverbose >= 1) { 			_pci_tagprintf(tag, "_pci_conf_writen: bad reg %x\r\n", reg);		}		return;	}	_pci_break_tag (tag, &bus, &device, &function);	/* Type 0 configuration on onboard PCI buses */	if (device > 31 || function > 7) {		return;         /* device out of range */	}	adr = (device << 11) | (function << 8) | reg | GT_IPCI_CFGADDR_ConfigEn;        if(bus == 0) {		tmp = GT_READ(PCI_0_P2P_CONFIG);		adr |= tmp & 0xff0000;		/* Use the bus we are on */		if (device == 0) { 		/* 0 is always the Disco chip */			adr |= (tmp >> (24-11)) & (0x1f << 11);		}                GT_WRITE(PCI_0_CONFIG_ADDR, adr);                GT_WRITE(PCI_0_CONFIG_DATA_VIRTUAL_REG, data);        }        else {		tmp = GT_READ(PCI_1_P2P_CONFIG);		if (bus == 1 && device == 0) {	/* Disco chip */			adr |= tmp & 0xff0000;	/* Use the bus we are on */			adr |= (tmp >> (24-11)) & (0x1f << 11);		}                GT_WRITE(PCI_1_CONFIG_ADDR, adr);                GT_WRITE(PCI_1_CONFIG_DATA_VIRTUAL_REG, data);        }}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);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -