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

📄 i2c-ibm_iic.c

📁 《linux驱动程序设计从入门到精通》一书中所有的程序代码含驱动和相应的应用程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * drivers/i2c/i2c-ibm_iic.c * * Support for the IIC peripheral on IBM PPC 4xx * * Copyright (c) 2003, 2004 Zultys Technologies. * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net> * * Based on original work by  * 	Ian DaSilva  <idasilva@mvista.com> *      Armin Kuster <akuster@mvista.com> * 	Matt Porter  <mporter@mvista.com> * *      Copyright 2000-2003 MontaVista Software Inc. * * Original driver version was highly leveraged from i2c-elektor.c * *   	Copyright 1995-97 Simon G. Vogl *                1998-99 Hans Berglund * *   	With some changes from Ky鰏ti M鋖kki <kmalkki@cc.hut.fi>  *	and even Frodo Looijaard <frodol@dds.nl> * * 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. * */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/ioport.h>#include <linux/delay.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/interrupt.h>#include <asm/irq.h>#include <asm/io.h>#include <linux/i2c.h>#include <linux/i2c-id.h>#include <asm/ocp.h>#include <asm/ibm4xx.h>#include "i2c-ibm_iic.h"#define DRIVER_VERSION "2.1"MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);MODULE_LICENSE("GPL");static int iic_force_poll = 0;MODULE_PARM(iic_force_poll, "i");MODULE_PARM_DESC(iic_force_poll, "Force polling mode");static int iic_force_fast = 0;MODULE_PARM(iic_force_fast, "i");MODULE_PARM_DESC(iic_fast_poll, "Force fast mode (400 kHz)");#define DBG_LEVEL 0#ifdef DBG#undef DBG#endif#ifdef DBG2#undef DBG2#endif#if DBG_LEVEL > 0#  define DBG(f,x...)	printk(KERN_DEBUG "ibm-iic" f, ##x)#else#  define DBG(f,x...)	((void)0)#endif#if DBG_LEVEL > 1#  define DBG2(f,x...) 	DBG(f, ##x)#else#  define DBG2(f,x...) 	((void)0)#endif#if DBG_LEVEL > 2static void dump_iic_regs(const char* header, struct ibm_iic_private* dev){	volatile struct iic_regs *iic = dev->vaddr;	printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);	printk(KERN_DEBUG "  cntl     = 0x%02x, mdcntl = 0x%02x\n"	       KERN_DEBUG "  sts      = 0x%02x, extsts = 0x%02x\n"	       KERN_DEBUG "  clkdiv   = 0x%02x, xfrcnt = 0x%02x\n"	       KERN_DEBUG "  xtcntlss = 0x%02x, directcntl = 0x%02x\n",		in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts), 		in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt), 		in_8(&iic->xtcntlss), in_8(&iic->directcntl));}#  define DUMP_REGS(h,dev)	dump_iic_regs((h),(dev))#else#  define DUMP_REGS(h,dev)	((void)0)#endif/* Bus timings (in ns) for bit-banging */static struct i2c_timings {	unsigned int hd_sta;	unsigned int su_sto;	unsigned int low;	unsigned int high;	unsigned int buf;} timings [] = {/* Standard mode (100 KHz) */{	.hd_sta	= 4000,	.su_sto	= 4000,	.low	= 4700,	.high	= 4000,	.buf	= 4700,},/* Fast mode (400 KHz) */{	.hd_sta = 600,	.su_sto	= 600,	.low 	= 1300,	.high 	= 600,	.buf	= 1300,}};/* Enable/disable interrupt generation */static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable){	out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);} /* * Initialize IIC interface. */static void iic_dev_init(struct ibm_iic_private* dev){	volatile struct iic_regs *iic = dev->vaddr;	DBG("%d: init\n", dev->idx);		/* Clear master address */	out_8(&iic->lmadr, 0);	out_8(&iic->hmadr, 0);	/* Clear slave address */	out_8(&iic->lsadr, 0);	out_8(&iic->hsadr, 0);	/* Clear status & extended status */	out_8(&iic->sts, STS_SCMP | STS_IRQA);	out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA			    | EXTSTS_ICT | EXTSTS_XFRA);	/* Set clock divider */	out_8(&iic->clkdiv, dev->clckdiv);	/* Clear transfer count */	out_8(&iic->xfrcnt, 0);	/* Clear extended control and status */	out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC			    | XTCNTLSS_SWS);	/* Clear control register */	out_8(&iic->cntl, 0);		/* Enable interrupts if possible */	iic_interrupt_mode(dev, dev->irq >= 0);	/* Set mode control */	out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS			    | (dev->fast_mode ? MDCNTL_FSM : 0));	DUMP_REGS("iic_init", dev);}/*  * Reset IIC interface */static void iic_dev_reset(struct ibm_iic_private* dev){	volatile struct iic_regs *iic = dev->vaddr;	int i;	u8 dc;		DBG("%d: soft reset\n", dev->idx);	DUMP_REGS("reset", dev);	    	/* Place chip in the reset state */	out_8(&iic->xtcntlss, XTCNTLSS_SRST);		/* Check if bus is free */	dc = in_8(&iic->directcntl);		if (!DIRCTNL_FREE(dc)){		DBG("%d: trying to regain bus control\n", dev->idx);			/* Try to set bus free state */		out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);				/* Wait until we regain bus control */		for (i = 0; i < 100; ++i){			dc = in_8(&iic->directcntl);			if (DIRCTNL_FREE(dc))				break;						/* Toggle SCL line */			dc ^= DIRCNTL_SCC;			out_8(&iic->directcntl, dc);			udelay(10);			dc ^= DIRCNTL_SCC;			out_8(&iic->directcntl, dc);						/* be nice */			cond_resched();		}	}		/* Remove reset */	out_8(&iic->xtcntlss, 0);		/* Reinitialize interface */	iic_dev_init(dev);}/* * Do 0-length transaction using bit-banging through IIC_DIRECTCNTL register. *//* Wait for SCL and/or SDA to be high */static int iic_dc_wait(volatile struct iic_regs *iic, u8 mask){	unsigned long x = jiffies + HZ / 28 + 2;	while ((in_8(&iic->directcntl) & mask) != mask){		if (unlikely(time_after(jiffies, x)))			return -1;		cond_resched();	}	return 0;}static int iic_smbus_quick(struct ibm_iic_private* dev, const struct i2c_msg* p){	volatile struct iic_regs* iic = dev->vaddr;	const struct i2c_timings* t = &timings[dev->fast_mode ? 1 : 0];	u8 mask, v, sda;	int i, res;	/* Only 7-bit addresses are supported */	if (unlikely(p->flags & I2C_M_TEN)){		DBG("%d: smbus_quick - 10 bit addresses are not supported\n",			dev->idx);		return -EINVAL;	}	DBG("%d: smbus_quick(0x%02x)\n", dev->idx, p->addr);	/* Reset IIC interface */	out_8(&iic->xtcntlss, XTCNTLSS_SRST);	/* Wait for bus to become free */	out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);	if (unlikely(iic_dc_wait(iic, DIRCNTL_MSDA | DIRCNTL_MSC)))		goto err;	ndelay(t->buf);	/* START */	out_8(&iic->directcntl, DIRCNTL_SCC);	sda = 0;	ndelay(t->hd_sta);	/* Send address */	v = (u8)((p->addr << 1) | ((p->flags & I2C_M_RD) ? 1 : 0));	for (i = 0, mask = 0x80; i < 8; ++i, mask >>= 1){		out_8(&iic->directcntl, sda);		ndelay(t->low / 2);		sda = (v & mask) ? DIRCNTL_SDAC : 0;		out_8(&iic->directcntl, sda);		ndelay(t->low / 2);		out_8(&iic->directcntl, DIRCNTL_SCC | sda);		if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))			goto err;		ndelay(t->high);	}	/* ACK */	out_8(&iic->directcntl, sda);	ndelay(t->low / 2);	out_8(&iic->directcntl, DIRCNTL_SDAC);	ndelay(t->low / 2);	out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);	if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))		goto err;	res = (in_8(&iic->directcntl) & DIRCNTL_MSDA) ? -EREMOTEIO : 1;	ndelay(t->high);	/* STOP */	out_8(&iic->directcntl, 0);	ndelay(t->low);	out_8(&iic->directcntl, DIRCNTL_SCC);	if (unlikely(iic_dc_wait(iic, DIRCNTL_MSC)))		goto err;	ndelay(t->su_sto);	out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);	ndelay(t->buf);	DBG("%d: smbus_quick -> %s\n", dev->idx, res ? "NACK" : "ACK");out:	/* Remove reset */	out_8(&iic->xtcntlss, 0);	/* Reinitialize interface */	iic_dev_init(dev);	return res;err:	DBG("%d: smbus_quick - bus is stuck\n", dev->idx);	res = -EREMOTEIO;	goto out;}/* * IIC interrupt handler */static irqreturn_t iic_handler(int irq, void *dev_id, struct pt_regs *regs){	struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;	volatile struct iic_regs* iic = dev->vaddr;		DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n", 	     dev->idx, in_8(&iic->sts), in_8(&iic->extsts));		/* Acknowledge IRQ and wakeup iic_wait_for_tc */	out_8(&iic->sts, STS_IRQA | STS_SCMP);	wake_up_interruptible(&dev->wq);		return IRQ_HANDLED;}/* * Get master transfer result and clear errors if any. * Returns the number of actually transferred bytes or error (<0) */static int iic_xfer_result(struct ibm_iic_private* dev){	volatile struct iic_regs *iic = dev->vaddr;			if (unlikely(in_8(&iic->sts) & STS_ERR)){		DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx, 			in_8(&iic->extsts));						/* Clear errors and possible pending IRQs */		out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | 			EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);					/* Flush master data buffer */		out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);				/* Is bus free?		 * If error happened during combined xfer		 * IIC interface is usually stuck in some strange		 * state, the only way out - soft reset.		 */		if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){			DBG("%d: bus is stuck, resetting\n", dev->idx);			iic_dev_reset(dev);		}		return -EREMOTEIO;	}	else		return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;}/* * Try to abort active transfer. */static void iic_abort_xfer(struct ibm_iic_private* dev){	volatile struct iic_regs *iic = dev->vaddr;	unsigned long x;		DBG("%d: iic_abort_xfer\n", dev->idx);		out_8(&iic->cntl, CNTL_HMT);		/*	 * Wait for the abort command to complete.	 * It's not worth to be optimized, just poll (timeout >= 1 tick)	 */	x = jiffies + 2;	while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){		if (time_after(jiffies, x)){			DBG("%d: abort timeout, resetting...\n", dev->idx);			iic_dev_reset(dev);			return;		}		schedule();	}	/* Just to clear errors */	iic_xfer_result(dev);}/* * Wait for master transfer to complete. * It puts current process to sleep until we get interrupt or timeout expires. * Returns the number of transferred bytes or error (<0) */static int iic_wait_for_tc(struct ibm_iic_private* dev){		volatile struct iic_regs *iic = dev->vaddr;	int ret = 0;		if (dev->irq >= 0){		/* Interrupt mode */		wait_queue_t wait;    		init_waitqueue_entry(&wait, current);

⌨️ 快捷键说明

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