i2c-ibm_iic.c 20.1 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * drivers/i2c/busses/i2c-ibm_iic.c
L
Linus Torvalds 已提交
3 4 5 6 7 8
 *
 * 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>
 *
S
Stefan Roese 已提交
9
 * Based on original work by
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20
 * 	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
 *
21
 *   	With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
 *	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/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;
module_param(iic_force_poll, bool, 0);
MODULE_PARM_DESC(iic_force_poll, "Force polling mode");

static int iic_force_fast;
module_param(iic_force_fast, bool, 0);
58
MODULE_PARM_DESC(iic_force_fast, "Force fast mode (400 kHz)");
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88

#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 > 2
static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
{
	volatile struct iic_regs __iomem *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",
S
Stefan Roese 已提交
89 90
		in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
		in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
L
Linus Torvalds 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
		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);
}
S
Stefan Roese 已提交
128

L
Linus Torvalds 已提交
129 130 131 132 133 134 135 136
/*
 * Initialize IIC interface.
 */
static void iic_dev_init(struct ibm_iic_private* dev)
{
	volatile struct iic_regs __iomem *iic = dev->vaddr;

	DBG("%d: init\n", dev->idx);
S
Stefan Roese 已提交
137

L
Linus Torvalds 已提交
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	/* 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);
S
Stefan Roese 已提交
163

L
Linus Torvalds 已提交
164 165 166 167 168 169 170 171 172 173
	/* 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);
}

S
Stefan Roese 已提交
174
/*
L
Linus Torvalds 已提交
175 176 177 178 179 180 181
 * Reset IIC interface
 */
static void iic_dev_reset(struct ibm_iic_private* dev)
{
	volatile struct iic_regs __iomem *iic = dev->vaddr;
	int i;
	u8 dc;
S
Stefan Roese 已提交
182

L
Linus Torvalds 已提交
183 184
	DBG("%d: soft reset\n", dev->idx);
	DUMP_REGS("reset", dev);
S
Stefan Roese 已提交
185

L
Linus Torvalds 已提交
186 187
    	/* Place chip in the reset state */
	out_8(&iic->xtcntlss, XTCNTLSS_SRST);
S
Stefan Roese 已提交
188

L
Linus Torvalds 已提交
189
	/* Check if bus is free */
S
Stefan Roese 已提交
190
	dc = in_8(&iic->directcntl);
L
Linus Torvalds 已提交
191 192
	if (!DIRCTNL_FREE(dc)){
		DBG("%d: trying to regain bus control\n", dev->idx);
S
Stefan Roese 已提交
193

L
Linus Torvalds 已提交
194
		/* Try to set bus free state */
S
Stefan Roese 已提交
195 196
		out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);

L
Linus Torvalds 已提交
197 198 199 200 201
		/* Wait until we regain bus control */
		for (i = 0; i < 100; ++i){
			dc = in_8(&iic->directcntl);
			if (DIRCTNL_FREE(dc))
				break;
S
Stefan Roese 已提交
202

L
Linus Torvalds 已提交
203 204 205 206 207 208
			/* Toggle SCL line */
			dc ^= DIRCNTL_SCC;
			out_8(&iic->directcntl, dc);
			udelay(10);
			dc ^= DIRCNTL_SCC;
			out_8(&iic->directcntl, dc);
S
Stefan Roese 已提交
209

L
Linus Torvalds 已提交
210 211 212 213
			/* be nice */
			cond_resched();
		}
	}
S
Stefan Roese 已提交
214

L
Linus Torvalds 已提交
215 216
	/* Remove reset */
	out_8(&iic->xtcntlss, 0);
S
Stefan Roese 已提交
217

L
Linus Torvalds 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
	/* 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 __iomem *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 __iomem *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
 */
323
static irqreturn_t iic_handler(int irq, void *dev_id)
L
Linus Torvalds 已提交
324 325 326
{
	struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
	volatile struct iic_regs __iomem *iic = dev->vaddr;
S
Stefan Roese 已提交
327 328

	DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
L
Linus Torvalds 已提交
329
	     dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
S
Stefan Roese 已提交
330

L
Linus Torvalds 已提交
331 332 333
	/* Acknowledge IRQ and wakeup iic_wait_for_tc */
	out_8(&iic->sts, STS_IRQA | STS_SCMP);
	wake_up_interruptible(&dev->wq);
S
Stefan Roese 已提交
334

L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343
	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)
{
S
Stefan Roese 已提交
344 345
	volatile struct iic_regs __iomem *iic = dev->vaddr;

L
Linus Torvalds 已提交
346
	if (unlikely(in_8(&iic->sts) & STS_ERR)){
S
Stefan Roese 已提交
347
		DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
L
Linus Torvalds 已提交
348
			in_8(&iic->extsts));
S
Stefan Roese 已提交
349

L
Linus Torvalds 已提交
350
		/* Clear errors and possible pending IRQs */
S
Stefan Roese 已提交
351
		out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
L
Linus Torvalds 已提交
352
			EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
S
Stefan Roese 已提交
353

L
Linus Torvalds 已提交
354 355
		/* Flush master data buffer */
		out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
S
Stefan Roese 已提交
356

L
Linus Torvalds 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
		/* 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 __iomem *iic = dev->vaddr;
	unsigned long x;
S
Stefan Roese 已提交
379

L
Linus Torvalds 已提交
380
	DBG("%d: iic_abort_xfer\n", dev->idx);
S
Stefan Roese 已提交
381

L
Linus Torvalds 已提交
382
	out_8(&iic->cntl, CNTL_HMT);
S
Stefan Roese 已提交
383

L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	/*
	 * 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){
S
Stefan Roese 已提交
408

L
Linus Torvalds 已提交
409 410
	volatile struct iic_regs __iomem *iic = dev->vaddr;
	int ret = 0;
S
Stefan Roese 已提交
411

L
Linus Torvalds 已提交
412 413
	if (dev->irq >= 0){
		/* Interrupt mode */
S
Stefan Roese 已提交
414
		ret = wait_event_interruptible_timeout(dev->wq,
L
Linus Torvalds 已提交
415 416 417 418 419 420 421 422 423 424 425 426
			!(in_8(&iic->sts) & STS_PT), dev->adap.timeout * HZ);

		if (unlikely(ret < 0))
			DBG("%d: wait interrupted\n", dev->idx);
		else if (unlikely(in_8(&iic->sts) & STS_PT)){
			DBG("%d: wait timeout\n", dev->idx);
			ret = -ETIMEDOUT;
		}
	}
	else {
		/* Polling mode */
		unsigned long x = jiffies + dev->adap.timeout * HZ;
S
Stefan Roese 已提交
427

L
Linus Torvalds 已提交
428 429 430 431 432 433
		while (in_8(&iic->sts) & STS_PT){
			if (unlikely(time_after(jiffies, x))){
				DBG("%d: poll timeout\n", dev->idx);
				ret = -ETIMEDOUT;
				break;
			}
S
Stefan Roese 已提交
434

L
Linus Torvalds 已提交
435 436 437 438 439 440
			if (unlikely(signal_pending(current))){
				DBG("%d: poll interrupted\n", dev->idx);
				ret = -ERESTARTSYS;
				break;
			}
			schedule();
S
Stefan Roese 已提交
441
		}
L
Linus Torvalds 已提交
442
	}
S
Stefan Roese 已提交
443

L
Linus Torvalds 已提交
444 445 446 447
	if (unlikely(ret < 0))
		iic_abort_xfer(dev);
	else
		ret = iic_xfer_result(dev);
S
Stefan Roese 已提交
448

L
Linus Torvalds 已提交
449
	DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
S
Stefan Roese 已提交
450

L
Linus Torvalds 已提交
451 452 453 454 455 456
	return ret;
}

/*
 * Low level master transfer routine
 */
S
Stefan Roese 已提交
457
static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
L
Linus Torvalds 已提交
458 459 460 461 462 463 464 465 466 467
			  int combined_xfer)
{
	volatile struct iic_regs __iomem *iic = dev->vaddr;
	char* buf = pm->buf;
	int i, j, loops, ret = 0;
	int len = pm->len;

	u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
	if (pm->flags & I2C_M_RD)
		cntl |= CNTL_RW;
S
Stefan Roese 已提交
468

L
Linus Torvalds 已提交
469 470 471 472
	loops = (len + 3) / 4;
	for (i = 0; i < loops; ++i, len -= 4){
		int count = len > 4 ? 4 : len;
		u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
S
Stefan Roese 已提交
473

L
Linus Torvalds 已提交
474 475 476
		if (!(cntl & CNTL_RW))
			for (j = 0; j < count; ++j)
				out_8((void __iomem *)&iic->mdbuf, *buf++);
S
Stefan Roese 已提交
477

L
Linus Torvalds 已提交
478 479 480 481
		if (i < loops - 1)
			cmd |= CNTL_CHT;
		else if (combined_xfer)
			cmd |= CNTL_RPST;
S
Stefan Roese 已提交
482

L
Linus Torvalds 已提交
483
		DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
S
Stefan Roese 已提交
484

L
Linus Torvalds 已提交
485 486
		/* Start transfer */
		out_8(&iic->cntl, cmd);
S
Stefan Roese 已提交
487

L
Linus Torvalds 已提交
488 489 490 491 492 493
		/* Wait for completion */
		ret = iic_wait_for_tc(dev);

		if (unlikely(ret < 0))
			break;
		else if (unlikely(ret != count)){
S
Stefan Roese 已提交
494
			DBG("%d: xfer_bytes, requested %d, transfered %d\n",
L
Linus Torvalds 已提交
495
				dev->idx, count, ret);
S
Stefan Roese 已提交
496

L
Linus Torvalds 已提交
497 498 499
			/* If it's not a last part of xfer, abort it */
			if (combined_xfer || (i < loops - 1))
    				iic_abort_xfer(dev);
S
Stefan Roese 已提交
500

L
Linus Torvalds 已提交
501
			ret = -EREMOTEIO;
S
Stefan Roese 已提交
502
			break;
L
Linus Torvalds 已提交
503
		}
S
Stefan Roese 已提交
504

L
Linus Torvalds 已提交
505 506 507 508
		if (cntl & CNTL_RW)
			for (j = 0; j < count; ++j)
				*buf++ = in_8((void __iomem *)&iic->mdbuf);
	}
S
Stefan Roese 已提交
509

L
Linus Torvalds 已提交
510 511 512 513 514 515 516 517 518 519
	return ret > 0 ? 0 : ret;
}

/*
 * Set target slave address for master transfer
 */
static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
{
	volatile struct iic_regs __iomem *iic = dev->vaddr;
	u16 addr = msg->addr;
S
Stefan Roese 已提交
520 521

	DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
L
Linus Torvalds 已提交
522
		addr, msg->flags & I2C_M_TEN ? 10 : 7);
S
Stefan Roese 已提交
523

L
Linus Torvalds 已提交
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	if (msg->flags & I2C_M_TEN){
	    out_8(&iic->cntl, CNTL_AMD);
	    out_8(&iic->lmadr, addr);
	    out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
	}
	else {
	    out_8(&iic->cntl, 0);
	    out_8(&iic->lmadr, addr << 1);
	}
}

static inline int iic_invalid_address(const struct i2c_msg* p)
{
	return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
}

S
Stefan Roese 已提交
540
static inline int iic_address_neq(const struct i2c_msg* p1,
L
Linus Torvalds 已提交
541 542
				  const struct i2c_msg* p2)
{
S
Stefan Roese 已提交
543
	return (p1->addr != p2->addr)
L
Linus Torvalds 已提交
544
		|| ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
S
Stefan Roese 已提交
545
}
L
Linus Torvalds 已提交
546 547

/*
S
Stefan Roese 已提交
548
 * Generic master transfer entrypoint.
L
Linus Torvalds 已提交
549 550 551 552 553 554 555
 * Returns the number of processed messages or error (<0)
 */
static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
{
    	struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
	volatile struct iic_regs __iomem *iic = dev->vaddr;
	int i, ret = 0;
S
Stefan Roese 已提交
556

L
Linus Torvalds 已提交
557
	DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
S
Stefan Roese 已提交
558

L
Linus Torvalds 已提交
559 560
	if (!num)
		return 0;
S
Stefan Roese 已提交
561

L
Linus Torvalds 已提交
562 563 564 565
	/* Check the sanity of the passed messages.
	 * Uhh, generic i2c layer is more suitable place for such code...
	 */
	if (unlikely(iic_invalid_address(&msgs[0]))){
S
Stefan Roese 已提交
566
		DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
L
Linus Torvalds 已提交
567 568
			msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
		return -EINVAL;
S
Stefan Roese 已提交
569
	}
L
Linus Torvalds 已提交
570 571 572 573 574 575 576 577 578
	for (i = 0; i < num; ++i){
		if (unlikely(msgs[i].len <= 0)){
			if (num == 1 && !msgs[0].len){
				/* Special case for I2C_SMBUS_QUICK emulation.
				 * IBM IIC doesn't support 0-length transactions
				 * so we have to emulate them using bit-banging.
				 */
				return iic_smbus_quick(dev, &msgs[0]);
			}
S
Stefan Roese 已提交
579
			DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587
				msgs[i].len, i);
			return -EINVAL;
		}
		if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
			DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
			return -EINVAL;
		}
	}
S
Stefan Roese 已提交
588

L
Linus Torvalds 已提交
589 590 591
	/* Check bus state */
	if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
		DBG("%d: iic_xfer, bus is not free\n", dev->idx);
S
Stefan Roese 已提交
592

L
Linus Torvalds 已提交
593 594 595
		/* Usually it means something serious has happend.
		 * We *cannot* have unfinished previous transfer
		 * so it doesn't make any sense to try to stop it.
S
Stefan Roese 已提交
596
		 * Probably we were not able to recover from the
L
Linus Torvalds 已提交
597 598 599 600 601
		 * previous error.
		 * The only *reasonable* thing I can think of here
		 * is soft reset.  --ebs
		 */
		iic_dev_reset(dev);
S
Stefan Roese 已提交
602

L
Linus Torvalds 已提交
603 604 605 606
		if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
			DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
			return -EREMOTEIO;
		}
S
Stefan Roese 已提交
607
	}
L
Linus Torvalds 已提交
608 609 610 611
	else {
		/* Flush master data buffer (just in case) */
		out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
	}
S
Stefan Roese 已提交
612

L
Linus Torvalds 已提交
613 614
	/* Load slave address */
	iic_address(dev, &msgs[0]);
S
Stefan Roese 已提交
615

L
Linus Torvalds 已提交
616 617 618 619 620 621 622 623 624 625 626 627
	/* Do real transfer */
    	for (i = 0; i < num && !ret; ++i)
		ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);

	return ret < 0 ? ret : num;
}

static u32 iic_func(struct i2c_adapter *adap)
{
	return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
}

628
static const struct i2c_algorithm iic_algo = {
L
Linus Torvalds 已提交
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
	.master_xfer 	= iic_xfer,
	.functionality	= iic_func
};

/*
 * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
 */
static inline u8 iic_clckdiv(unsigned int opb)
{
	/* Compatibility kludge, should go away after all cards
	 * are fixed to fill correct value for opbfreq.
	 * Previous driver version used hardcoded divider value 4,
	 * it corresponds to OPB frequency from the range (40, 50] MHz
	 */
	if (!opb){
		printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
			" fix your board specific setup\n");
		opb = 50000000;
	}

	/* Convert to MHz */
	opb /= 1000000;
S
Stefan Roese 已提交
651

L
Linus Torvalds 已提交
652
	if (opb < 20 || opb > 150){
653
		printk(KERN_WARNING "ibm-iic: invalid OPB clock frequency %u MHz\n",
L
Linus Torvalds 已提交
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
			opb);
		opb = opb < 20 ? 20 : 150;
	}
	return (u8)((opb + 9) / 10 - 1);
}

/*
 * Register single IIC interface
 */
static int __devinit iic_probe(struct ocp_device *ocp){

	struct ibm_iic_private* dev;
	struct i2c_adapter* adap;
	struct ocp_func_iic_data* iic_data = ocp->def->additions;
	int ret;
S
Stefan Roese 已提交
669

L
Linus Torvalds 已提交
670 671 672 673
	if (!iic_data)
		printk(KERN_WARNING"ibm-iic%d: missing additional data!\n",
			ocp->def->index);

674
	if (!(dev = kzalloc(sizeof(*dev), GFP_KERNEL))) {
675
		printk(KERN_ERR "ibm-iic%d: failed to allocate device data\n",
L
Linus Torvalds 已提交
676 677 678 679 680 681
			ocp->def->index);
		return -ENOMEM;
	}

	dev->idx = ocp->def->index;
	ocp_set_drvdata(ocp, dev);
S
Stefan Roese 已提交
682

683 684 685 686 687 688
	if (!request_mem_region(ocp->def->paddr, sizeof(struct iic_regs),
				"ibm_iic")) {
		ret = -EBUSY;
		goto fail1;
	}

L
Linus Torvalds 已提交
689
	if (!(dev->vaddr = ioremap(ocp->def->paddr, sizeof(struct iic_regs)))){
690
		printk(KERN_ERR "ibm-iic%d: failed to ioremap device registers\n",
L
Linus Torvalds 已提交
691 692 693 694
			dev->idx);
		ret = -ENXIO;
		goto fail2;
	}
S
Stefan Roese 已提交
695

L
Linus Torvalds 已提交
696 697 698 699
	init_waitqueue_head(&dev->wq);

	dev->irq = iic_force_poll ? -1 : ocp->def->irq;
	if (dev->irq >= 0){
700
		/* Disable interrupts until we finish initialization,
L
Linus Torvalds 已提交
701 702 703 704
		   assumes level-sensitive IRQ setup...
		 */
		iic_interrupt_mode(dev, 0);
		if (request_irq(dev->irq, iic_handler, 0, "IBM IIC", dev)){
S
Stefan Roese 已提交
705
			printk(KERN_ERR "ibm-iic%d: request_irq %d failed\n",
L
Linus Torvalds 已提交
706
				dev->idx, dev->irq);
S
Stefan Roese 已提交
707
			/* Fallback to the polling mode */
L
Linus Torvalds 已提交
708 709 710
			dev->irq = -1;
		}
	}
S
Stefan Roese 已提交
711

L
Linus Torvalds 已提交
712
	if (dev->irq < 0)
S
Stefan Roese 已提交
713
		printk(KERN_WARNING "ibm-iic%d: using polling mode\n",
L
Linus Torvalds 已提交
714
			dev->idx);
S
Stefan Roese 已提交
715

L
Linus Torvalds 已提交
716 717
	/* Board specific settings */
	dev->fast_mode = iic_force_fast ? 1 : (iic_data ? iic_data->fast_mode : 0);
S
Stefan Roese 已提交
718 719

	/* clckdiv is the same for *all* IIC interfaces,
L
Linus Torvalds 已提交
720 721 722 723
	 * but I'd rather make a copy than introduce another global. --ebs
	 */
	dev->clckdiv = iic_clckdiv(ocp_sys_info.opb_bus_freq);
	DBG("%d: clckdiv = %d\n", dev->idx, dev->clckdiv);
S
Stefan Roese 已提交
724

L
Linus Torvalds 已提交
725 726
	/* Initialize IIC interface */
	iic_dev_init(dev);
S
Stefan Roese 已提交
727

L
Linus Torvalds 已提交
728 729
	/* Register it with i2c layer */
	adap = &dev->adap;
730
	adap->dev.parent = &ocp->dev;
L
Linus Torvalds 已提交
731 732
	strcpy(adap->name, "IBM IIC");
	i2c_set_adapdata(adap, dev);
733
	adap->id = I2C_HW_OCP;
734
	adap->class = I2C_CLASS_HWMON;
L
Linus Torvalds 已提交
735 736 737 738 739
	adap->algo = &iic_algo;
	adap->client_register = NULL;
	adap->client_unregister = NULL;
	adap->timeout = 1;

740 741 742 743 744 745 746 747
	/*
	 * If "dev->idx" is negative we consider it as zero.
	 * The reason to do so is to avoid sysfs names that only make
	 * sense when there are multiple adapters.
	 */
	adap->nr = dev->idx >= 0 ? dev->idx : 0;

	if ((ret = i2c_add_numbered_adapter(adap)) < 0) {
748
		printk(KERN_ERR "ibm-iic%d: failed to register i2c adapter\n",
L
Linus Torvalds 已提交
749 750 751
			dev->idx);
		goto fail;
	}
S
Stefan Roese 已提交
752

L
Linus Torvalds 已提交
753 754 755 756 757
	printk(KERN_INFO "ibm-iic%d: using %s mode\n", dev->idx,
		dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");

	return 0;

S
Stefan Roese 已提交
758
fail:
L
Linus Torvalds 已提交
759 760 761
	if (dev->irq >= 0){
		iic_interrupt_mode(dev, 0);
		free_irq(dev->irq, dev);
S
Stefan Roese 已提交
762
	}
L
Linus Torvalds 已提交
763 764

	iounmap(dev->vaddr);
S
Stefan Roese 已提交
765
fail2:
766 767
	release_mem_region(ocp->def->paddr, sizeof(struct iic_regs));
fail1:
L
Linus Torvalds 已提交
768
	ocp_set_drvdata(ocp, NULL);
S
Stefan Roese 已提交
769
	kfree(dev);
L
Linus Torvalds 已提交
770 771 772 773 774 775 776 777 778 779 780
	return ret;
}

/*
 * Cleanup initialized IIC interface
 */
static void __devexit iic_remove(struct ocp_device *ocp)
{
	struct ibm_iic_private* dev = (struct ibm_iic_private*)ocp_get_drvdata(ocp);
	BUG_ON(dev == NULL);
	if (i2c_del_adapter(&dev->adap)){
781
		printk(KERN_ERR "ibm-iic%d: failed to delete i2c adapter :(\n",
L
Linus Torvalds 已提交
782 783 784
			dev->idx);
		/* That's *very* bad, just shutdown IRQ ... */
		if (dev->irq >= 0){
S
Stefan Roese 已提交
785
		    iic_interrupt_mode(dev, 0);
L
Linus Torvalds 已提交
786 787 788 789 790
		    free_irq(dev->irq, dev);
		    dev->irq = -1;
		}
	} else {
		if (dev->irq >= 0){
S
Stefan Roese 已提交
791
		    iic_interrupt_mode(dev, 0);
L
Linus Torvalds 已提交
792 793 794
		    free_irq(dev->irq, dev);
		}
		iounmap(dev->vaddr);
795
		release_mem_region(ocp->def->paddr, sizeof(struct iic_regs));
L
Linus Torvalds 已提交
796 797 798 799
		kfree(dev);
	}
}

S
Stefan Roese 已提交
800
static struct ocp_device_id ibm_iic_ids[] __devinitdata =
L
Linus Torvalds 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
{
	{ .vendor = OCP_VENDOR_IBM, .function = OCP_FUNC_IIC },
	{ .vendor = OCP_VENDOR_INVALID }
};

MODULE_DEVICE_TABLE(ocp, ibm_iic_ids);

static struct ocp_driver ibm_iic_driver =
{
	.name 		= "iic",
	.id_table	= ibm_iic_ids,
	.probe		= iic_probe,
	.remove		= __devexit_p(iic_remove),
#if defined(CONFIG_PM)
	.suspend	= NULL,
	.resume		= NULL,
#endif
};

static int __init iic_init(void)
{
	printk(KERN_INFO "IBM IIC driver v" DRIVER_VERSION "\n");
	return ocp_register_driver(&ibm_iic_driver);
}

static void __exit iic_exit(void)
{
	ocp_unregister_driver(&ibm_iic_driver);
}

module_init(iic_init);
module_exit(iic_exit);