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

📄 envctrl.c

📁 linux-2.6.15.6
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Id: envctrl.c,v 1.25 2002/01/15 09:01:26 davem Exp $ * envctrl.c: Temperature and Fan monitoring on Machines providing it. * * Copyright (C) 1998  Eddie C. Dost  (ecd@skynet.be) * Copyright (C) 2000  Vinh Truong    (vinh.truong@eng.sun.com) * VT - The implementation is to support Sun Microelectronics (SME) platform *      environment monitoring.  SME platforms use pcf8584 as the i2c bus  *      controller to access pcf8591 (8-bit A/D and D/A converter) and  *      pcf8571 (256 x 8-bit static low-voltage RAM with I2C-bus interface). *      At board level, it follows SME Firmware I2C Specification. Reference: * 	http://www-eu2.semiconductors.com/pip/PCF8584P * 	http://www-eu2.semiconductors.com/pip/PCF8574AP * 	http://www-eu2.semiconductors.com/pip/PCF8591P * * EB - Added support for CP1500 Global Address and PS/Voltage monitoring. * 		Eric Brower <ebrower@usa.net> * * DB - Audit every copy_to_user in envctrl_read. *              Daniele Bellucci <bellucda@tiscali.it> */#define __KERNEL_SYSCALLS__static int errno;#include <linux/config.h>#include <linux/module.h>#include <linux/sched.h>#include <linux/kthread.h>#include <linux/errno.h>#include <linux/delay.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/miscdevice.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/kernel.h>#include <asm/ebus.h>#include <asm/uaccess.h>#include <asm/envctrl.h>#define ENVCTRL_MINOR	162#define PCF8584_ADDRESS	0x55#define CONTROL_PIN	0x80#define CONTROL_ES0	0x40#define CONTROL_ES1	0x20#define CONTROL_ES2	0x10#define CONTROL_ENI	0x08#define CONTROL_STA	0x04#define CONTROL_STO	0x02#define CONTROL_ACK	0x01#define STATUS_PIN	0x80#define STATUS_STS	0x20#define STATUS_BER	0x10#define STATUS_LRB	0x08#define STATUS_AD0	0x08#define STATUS_AAB	0x04#define STATUS_LAB	0x02#define STATUS_BB	0x01/* * CLK Mode Register. */#define BUS_CLK_90	0x00#define BUS_CLK_45	0x01#define BUS_CLK_11	0x02#define BUS_CLK_1_5	0x03#define CLK_3		0x00#define CLK_4_43	0x10#define CLK_6		0x14#define CLK_8		0x18#define CLK_12		0x1c#define OBD_SEND_START	0xc5    /* value to generate I2c_bus START condition */#define OBD_SEND_STOP 	0xc3    /* value to generate I2c_bus STOP condition *//* Monitor type of i2c child device. * Firmware definitions. */#define PCF8584_MAX_CHANNELS            8#define PCF8584_GLOBALADDR_TYPE			6  /* global address monitor */#define PCF8584_FANSTAT_TYPE            3  /* fan status monitor */#define PCF8584_VOLTAGE_TYPE            2  /* voltage monitor    */#define PCF8584_TEMP_TYPE	        	1  /* temperature monitor*//* Monitor type of i2c child device. * Driver definitions. */#define ENVCTRL_NOMON				0#define ENVCTRL_CPUTEMP_MON			1    /* cpu temperature monitor */#define ENVCTRL_CPUVOLTAGE_MON	  	2    /* voltage monitor         */#define ENVCTRL_FANSTAT_MON  		3    /* fan status monitor      */#define ENVCTRL_ETHERTEMP_MON		4    /* ethernet temperarture */					     /* monitor                     */#define ENVCTRL_VOLTAGESTAT_MON	  	5    /* voltage status monitor  */#define ENVCTRL_MTHRBDTEMP_MON		6    /* motherboard temperature */#define ENVCTRL_SCSITEMP_MON		7    /* scsi temperarture */#define ENVCTRL_GLOBALADDR_MON		8    /* global address *//* Child device type. * Driver definitions. */#define I2C_ADC				0    /* pcf8591 */#define I2C_GPIO			1    /* pcf8571 *//* Data read from child device may need to decode * through a data table and a scale. * Translation type as defined by firmware. */#define ENVCTRL_TRANSLATE_NO		0#define ENVCTRL_TRANSLATE_PARTIAL	1#define ENVCTRL_TRANSLATE_COMBINED	2#define ENVCTRL_TRANSLATE_FULL		3     /* table[data] */#define ENVCTRL_TRANSLATE_SCALE		4     /* table[data]/scale *//* Driver miscellaneous definitions. */#define ENVCTRL_MAX_CPU			4#define CHANNEL_DESC_SZ			256/* Mask values for combined GlobalAddress/PowerStatus node */#define ENVCTRL_GLOBALADDR_ADDR_MASK 	0x1F#define ENVCTRL_GLOBALADDR_PSTAT_MASK	0x60/* Node 0x70 ignored on CompactPCI CP1400/1500 platforms  * (see envctrl_init_i2c_child) */#define ENVCTRL_CPCI_IGNORED_NODE		0x70#define PCF8584_DATA	0x00#define PCF8584_CSR	0x01/* Each child device can be monitored by up to PCF8584_MAX_CHANNELS. * Property of a port or channel as defined by the firmware. */struct pcf8584_channel {        unsigned char chnl_no;        unsigned char io_direction;        unsigned char type;        unsigned char last;};/* Each child device may have one or more tables of bytes to help decode * data. Table property as defined by the firmware. */ struct pcf8584_tblprop {        unsigned int type;        unsigned int scale;          unsigned int offset; /* offset from the beginning of the table */        unsigned int size;};/* i2c child */struct i2c_child_t {	/* Either ADC or GPIO. */	unsigned char i2ctype;        unsigned long addr;            struct pcf8584_channel chnl_array[PCF8584_MAX_CHANNELS];	/* Channel info. */ 	unsigned int total_chnls;	/* Number of monitor channels. */	unsigned char fan_mask;		/* Byte mask for fan status channels. */	unsigned char voltage_mask;	/* Byte mask for voltage status channels. */        struct pcf8584_tblprop tblprop_array[PCF8584_MAX_CHANNELS];	/* Properties of all monitor channels. */	unsigned int total_tbls;	/* Number of monitor tables. */        char *tables;			/* Pointer to table(s). */	char chnls_desc[CHANNEL_DESC_SZ]; /* Channel description. */	char mon_type[PCF8584_MAX_CHANNELS];};static void __iomem *i2c;static struct i2c_child_t i2c_childlist[ENVCTRL_MAX_CPU*2];static unsigned char chnls_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };static unsigned int warning_temperature = 0;static unsigned int shutdown_temperature = 0;static char read_cpu;/* Forward declarations. */static struct i2c_child_t *envctrl_get_i2c_child(unsigned char);/* Function Description: Test the PIN bit (Pending Interrupt Not)  * 			 to test when serial transmission is completed . * Return : None. */static void envtrl_i2c_test_pin(void){	int limit = 1000000;	while (--limit > 0) {		if (!(readb(i2c + PCF8584_CSR) & STATUS_PIN)) 			break;		udelay(1);	} 	if (limit <= 0)		printk(KERN_INFO "envctrl: Pin status will not clear.\n");}/* Function Description: Test busy bit. * Return : None. */static void envctrl_i2c_test_bb(void){	int limit = 1000000;	while (--limit > 0) {		/* Busy bit 0 means busy. */		if (readb(i2c + PCF8584_CSR) & STATUS_BB)			break;		udelay(1);	} 	if (limit <= 0)		printk(KERN_INFO "envctrl: Busy bit will not clear.\n");}/* Function Description: Send the address for a read access. * Return : 0 if not acknowledged, otherwise acknowledged. */static int envctrl_i2c_read_addr(unsigned char addr){	envctrl_i2c_test_bb();	/* Load address. */	writeb(addr + 1, i2c + PCF8584_DATA);	envctrl_i2c_test_bb();	writeb(OBD_SEND_START, i2c + PCF8584_CSR);	/* Wait for PIN. */	envtrl_i2c_test_pin();	/* CSR 0 means acknowledged. */	if (!(readb(i2c + PCF8584_CSR) & STATUS_LRB)) {		return readb(i2c + PCF8584_DATA);	} else {		writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);		return 0;	}}/* Function Description: Send the address for write mode.   * Return : None. */static void envctrl_i2c_write_addr(unsigned char addr){	envctrl_i2c_test_bb();	writeb(addr, i2c + PCF8584_DATA);	/* Generate Start condition. */	writeb(OBD_SEND_START, i2c + PCF8584_CSR);}/* Function Description: Read 1 byte of data from addr  *			 set by envctrl_i2c_read_addr()  * Return : Data from address set by envctrl_i2c_read_addr(). */static unsigned char envctrl_i2c_read_data(void){	envtrl_i2c_test_pin();	writeb(CONTROL_ES0, i2c + PCF8584_CSR);  /* Send neg ack. */	return readb(i2c + PCF8584_DATA);}/* Function Description: Instruct the device which port to read data from.   * Return : None. */static void envctrl_i2c_write_data(unsigned char port){	envtrl_i2c_test_pin();	writeb(port, i2c + PCF8584_DATA);}/* Function Description: Generate Stop condition after last byte is sent. * Return : None. */static void envctrl_i2c_stop(void){	envtrl_i2c_test_pin();	writeb(OBD_SEND_STOP, i2c + PCF8584_CSR);}/* Function Description: Read adc device. * Return : Data at address and port. */static unsigned char envctrl_i2c_read_8591(unsigned char addr, unsigned char port){	/* Send address. */	envctrl_i2c_write_addr(addr);	/* Setup port to read. */	envctrl_i2c_write_data(port);	envctrl_i2c_stop();	/* Read port. */	envctrl_i2c_read_addr(addr);	/* Do a single byte read and send stop. */	envctrl_i2c_read_data();	envctrl_i2c_stop();	return readb(i2c + PCF8584_DATA);}/* Function Description: Read gpio device. * Return : Data at address. */static unsigned char envctrl_i2c_read_8574(unsigned char addr){	unsigned char rd;	envctrl_i2c_read_addr(addr);	/* Do a single byte read and send stop. */	rd = envctrl_i2c_read_data();	envctrl_i2c_stop();	return rd;}/* Function Description: Decode data read from an adc device using firmware *                       table. * Return: Number of read bytes. Data is stored in bufdata in ascii format. */static int envctrl_i2c_data_translate(unsigned char data, int translate_type,				      int scale, char *tbl, char *bufdata){	int len = 0;	switch (translate_type) {	case ENVCTRL_TRANSLATE_NO:		/* No decode necessary. */		len = 1;		bufdata[0] = data;		break;	case ENVCTRL_TRANSLATE_FULL:		/* Decode this way: data = table[data]. */		len = 1;		bufdata[0] = tbl[data];		break;	case ENVCTRL_TRANSLATE_SCALE:		/* Decode this way: data = table[data]/scale */		sprintf(bufdata,"%d ", (tbl[data] * 10) / (scale));		len = strlen(bufdata);		bufdata[len - 1] = bufdata[len - 2];		bufdata[len - 2] = '.';		break;	default:		break;	};	return len;}/* Function Description: Read cpu-related data such as cpu temperature, voltage. * Return: Number of read bytes. Data is stored in bufdata in ascii format. */static int envctrl_read_cpu_info(int cpu, struct i2c_child_t *pchild,				 char mon_type, unsigned char *bufdata){	unsigned char data;	int i;	char *tbl, j = -1;	/* Find the right monitor type and channel. */	for (i = 0; i < PCF8584_MAX_CHANNELS; i++) {		if (pchild->mon_type[i] == mon_type) {			if (++j == cpu) {				break;			}		}	}	if (j != cpu)		return 0;

⌨️ 快捷键说明

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