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

📄 powerspan.c

📁 gumstiz u-boot loader in linux
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * @file powerspan.c Source file for PowerSpan II code. *//* * (C) Copyright 2005 * AMIRIX Systems 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 */#include <common.h>#include <command.h>#include <asm/processor.h>#include "powerspan.h"#define tolower(x) x#include "ap1000.h"#ifdef INCLUDE_PCI/** Write one byte with byte swapping.  * @param  addr [IN] the address to write to  * @param  val  [IN] the value to write  */void write1 (unsigned long addr, unsigned char val){	volatile unsigned char *p = (volatile unsigned char *) addr;#ifdef VERBOSITY	if (gVerbosityLevel > 1) {		printf ("write1: addr=%08x val=%02x\n", addr, val);	}#endif	*p = val;	PSII_SYNC ();}/** Read one byte with byte swapping.  * @param  addr  [IN] the address to read from  * @return the value at addr  */unsigned char read1 (unsigned long addr){	unsigned char val;	volatile unsigned char *p = (volatile unsigned char *) addr;	val = *p;	PSII_SYNC ();#ifdef VERBOSITY	if (gVerbosityLevel > 1) {		printf ("read1: addr=%08x val=%02x\n", addr, val);	}#endif	return val;}/** Write one 2-byte word with byte swapping.  * @param  addr  [IN] the address to write to  * @param  val   [IN] the value to write  */void write2 (unsigned long addr, unsigned short val){	volatile unsigned short *p = (volatile unsigned short *) addr;#ifdef VERBOSITY	if (gVerbosityLevel > 1) {		printf ("write2: addr=%08x val=%04x -> *p=%04x\n", addr, val,			((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8));	}#endif	*p = ((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8);	PSII_SYNC ();}/** Read one 2-byte word with byte swapping.  * @param  addr  [IN] the address to read from  * @return the value at addr  */unsigned short read2 (unsigned long addr){	unsigned short val;	volatile unsigned short *p = (volatile unsigned short *) addr;	val = *p;	val = ((val & 0xFF00) >> 8) | ((val & 0x00FF) << 8);	PSII_SYNC ();#ifdef VERBOSITY	if (gVerbosityLevel > 1) {		printf ("read2: addr=%08x *p=%04x -> val=%04x\n", addr, *p,			val);	}#endif	return val;}/** Write one 4-byte word with byte swapping.  * @param  addr  [IN] the address to write to  * @param  val   [IN] the value to write  */void write4 (unsigned long addr, unsigned long val){	volatile unsigned long *p = (volatile unsigned long *) addr;#ifdef VERBOSITY	if (gVerbosityLevel > 1) {		printf ("write4: addr=%08x val=%08x -> *p=%08x\n", addr, val,			((val & 0xFF000000) >> 24) |			((val & 0x000000FF) << 24) |			((val & 0x00FF0000) >>  8) |			((val & 0x0000FF00) <<  8));	}#endif	*p = ((val & 0xFF000000) >> 24) | ((val & 0x000000FF) << 24) |		((val & 0x00FF0000) >> 8) | ((val & 0x0000FF00) << 8);	PSII_SYNC ();}/** Read one 4-byte word with byte swapping.  * @param  addr  [IN] the address to read from  * @return the value at addr  */unsigned long read4 (unsigned long addr){	unsigned long val;	volatile unsigned long *p = (volatile unsigned long *) addr;	val = *p;	val = ((val & 0xFF000000) >> 24) | ((val & 0x000000FF) << 24) |		((val & 0x00FF0000) >> 8) | ((val & 0x0000FF00) << 8);	PSII_SYNC ();#ifdef VERBOSITY	if (gVerbosityLevel > 1) {		printf ("read4: addr=%08x *p=%08x -> val=%08x\n", addr, *p,			val);	}#endif	return val;}int PCIReadConfig (int bus, int dev, int fn, int reg, int width,		   unsigned long *val){	unsigned int conAdrVal;	unsigned int conDataReg = REG_CONFIG_DATA;	unsigned int status;	int ret_val = 0;	/* DEST bit hardcoded to 1: local pci is PCI-2 */	/* TYPE bit is hardcoded to 1: all config cycles are local */	conAdrVal = (1 << 24)		| ((bus & 0xFF) << 16)		| ((dev & 0xFF) << 11)		| ((fn & 0x07) << 8)		| (reg & 0xFC);	/* clear any pending master aborts */	write4 (REG_P1_CSR, CLEAR_MASTER_ABORT);	/* Load the conAdrVal value first, then read from pb_conf_data */	write4 (REG_CONFIG_ADDRESS, conAdrVal);	PSII_SYNC ();	/* Note: documentation does not match the pspan library code */	/* Note: *pData comes back as -1 if device is not present */	switch (width) {	case 4:		*(unsigned int *) val = read4 (conDataReg);		break;	case 2:		*(unsigned short *) val = read2 (conDataReg);		break;	case 1:		*(unsigned char *) val = read1 (conDataReg);		break;	default:		ret_val = ILLEGAL_REG_OFFSET;		break;	}	PSII_SYNC ();	/* clear any pending master aborts */	status = read4 (REG_P1_CSR);	if (status & CLEAR_MASTER_ABORT) {		ret_val = NO_DEVICE_FOUND;		write4 (REG_P1_CSR, CLEAR_MASTER_ABORT);	}	return ret_val;}int PCIWriteConfig (int bus, int dev, int fn, int reg, int width,		    unsigned long val){	unsigned int conAdrVal;	unsigned int conDataReg = REG_CONFIG_DATA;	unsigned int status;	int ret_val = 0;	/* DEST bit hardcoded to 1: local pci is PCI-2 */	/* TYPE bit is hardcoded to 1: all config cycles are local */	conAdrVal = (1 << 24)		| ((bus & 0xFF) << 16)		| ((dev & 0xFF) << 11)		| ((fn & 0x07) << 8)		| (reg & 0xFC);	/* clear any pending master aborts */	write4 (REG_P1_CSR, CLEAR_MASTER_ABORT);	/* Load the conAdrVal value first, then read from pb_conf_data */	write4 (REG_CONFIG_ADDRESS, conAdrVal);	PSII_SYNC ();	/* Note: documentation does not match the pspan library code */	/* Note: *pData comes back as -1 if device is not present */	switch (width) {	case 4:		write4 (conDataReg, val);		break;	case 2:		write2 (conDataReg, val);		break;	case 1:		write1 (conDataReg, val);		break;	default:		ret_val = ILLEGAL_REG_OFFSET;		break;	}	PSII_SYNC ();	/* clear any pending master aborts */	status = read4 (REG_P1_CSR);	if (status & CLEAR_MASTER_ABORT) {		ret_val = NO_DEVICE_FOUND;		write4 (REG_P1_CSR, CLEAR_MASTER_ABORT);	}	return ret_val;}int pci_read_config_byte (int bus, int dev, int fn, int reg,			  unsigned char *val){	unsigned long read_val;	int ret_val;	ret_val = PCIReadConfig (bus, dev, fn, reg, 1, &read_val);	*val = read_val & 0xFF;	return ret_val;}int pci_write_config_byte (int bus, int dev, int fn, int reg,			   unsigned char val){	return PCIWriteConfig (bus, dev, fn, reg, 1, val);}int pci_read_config_word (int bus, int dev, int fn, int reg,			  unsigned short *val){	unsigned long read_val;	int ret_val;	ret_val = PCIReadConfig (bus, dev, fn, reg, 2, &read_val);	*val = read_val & 0xFFFF;	return ret_val;}int pci_write_config_word (int bus, int dev, int fn, int reg,			   unsigned short val){	return PCIWriteConfig (bus, dev, fn, reg, 2, val);}int pci_read_config_dword (int bus, int dev, int fn, int reg,			   unsigned long *val){	return PCIReadConfig (bus, dev, fn, reg, 4, val);}int pci_write_config_dword (int bus, int dev, int fn, int reg,			    unsigned long val){	return PCIWriteConfig (bus, dev, fn, reg, 4, val);}#endif /* INCLUDE_PCI */int I2CAccess (unsigned char theI2CAddress, unsigned char theDevCode,	       unsigned char theChipSel, unsigned char *theValue, int RWFlag){	int ret_val = 0;	unsigned int reg_value;	reg_value = PowerSpanRead (REG_I2C_CSR);	if (reg_value & I2C_CSR_ACT) {		printf ("Error: I2C busy\n");		ret_val = I2C_BUSY;	} else {		reg_value = ((theI2CAddress & 0xFF) << 24)			| ((theDevCode & 0x0F) << 12)			| ((theChipSel & 0x07) << 9)			| I2C_CSR_ERR;		if (RWFlag == I2C_WRITE) {			reg_value |= I2C_CSR_RW | ((*theValue & 0xFF) << 16);		}		PowerSpanWrite (REG_I2C_CSR, reg_value);		udelay (1);		do {			reg_value = PowerSpanRead (REG_I2C_CSR);			if ((reg_value & I2C_CSR_ACT) == 0) {				if (reg_value & I2C_CSR_ERR) {					ret_val = I2C_ERR;				} else {					*theValue =						(reg_value & I2C_CSR_DATA) >>						16;				}			}		} while (reg_value & I2C_CSR_ACT);	}	return ret_val;}int EEPROMRead (unsigned char theI2CAddress, unsigned char *theValue){	return I2CAccess (theI2CAddress, I2C_EEPROM_DEV, I2C_EEPROM_CHIP_SEL,			  theValue, I2C_READ);}int EEPROMWrite (unsigned char theI2CAddress, unsigned char theValue){	return I2CAccess (theI2CAddress, I2C_EEPROM_DEV, I2C_EEPROM_CHIP_SEL,			  &theValue, I2C_WRITE);}int do_eeprom (cmd_tbl_t * cmdtp, int flag, int argc, char *argv[]){	char cmd;	int ret_val = 0;	unsigned int address = 0;	unsigned char value = 1;	unsigned char read_value;	int ii;	int error = 0;	unsigned char *mem_ptr;

⌨️ 快捷键说明

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