2 * drivers/i2c/i2c-ibm_iic.c
4 * Support for the IIC peripheral on IBM PPC 4xx
6 * Copyright (c) 2003 Zultys Technologies.
7 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
9 * Based on original work by
10 * Ian DaSilva <idasilva@mvista.com>
11 * Armin Kuster <akuster@mvista.com>
12 * Matt Porter <mporter@mvista.com>
14 * Copyright 2000-2003 MontaVista Software Inc.
16 * Original driver version was highly leveraged from i2c-elektor.c
18 * Copyright 1995-97 Simon G. Vogl
19 * 1998-99 Hans Berglund
21 * With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi>
22 * and even Frodo Looijaard <frodol@dds.nl>
24 * This program is free software; you can redistribute it and/or modify it
25 * under the terms of the GNU General Public License as published by the
26 * Free Software Foundation; either version 2 of the License, or (at your
27 * option) any later version.
31 #include <linux/config.h>
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/ioport.h>
35 #include <linux/delay.h>
36 #include <linux/slab.h>
37 #include <linux/init.h>
38 #include <linux/interrupt.h>
41 #include <linux/i2c.h>
42 #include <linux/i2c-id.h>
44 #include <asm/ibm4xx.h>
46 #include "i2c-ibm_iic.h"
48 #define DRIVER_VERSION "2.0"
50 MODULE_DESCRIPTION("IBM IIC driver v" DRIVER_VERSION);
51 MODULE_LICENSE("GPL");
53 static int iic_force_poll = 0;
54 MODULE_PARM(iic_force_poll, "i");
55 MODULE_PARM_DESC(iic_force_poll, "Force polling mode");
57 static int iic_force_fast = 0;
58 MODULE_PARM(iic_force_fast, "i");
59 MODULE_PARM_DESC(iic_fast_poll, "Force fast mode (400 kHz)");
72 # define DBG(x...) printk(KERN_DEBUG "ibm-iic" ##x)
74 # define DBG(x...) ((void)0)
77 # define DBG2(x...) DBG( ##x )
79 # define DBG2(x...) ((void)0)
82 static void dump_iic_regs(const char* header, struct ibm_iic_private* dev)
84 volatile struct iic_regs *iic = dev->vaddr;
85 printk(KERN_DEBUG "ibm-iic%d: %s\n", dev->idx, header);
86 printk(KERN_DEBUG " cntl = 0x%02x, mdcntl = 0x%02x\n"
87 KERN_DEBUG " sts = 0x%02x, extsts = 0x%02x\n"
88 KERN_DEBUG " clkdiv = 0x%02x, xfrcnt = 0x%02x\n"
89 KERN_DEBUG " xtcntlss = 0x%02x, directcntl = 0x%02x\n",
90 in_8(&iic->cntl), in_8(&iic->mdcntl), in_8(&iic->sts),
91 in_8(&iic->extsts), in_8(&iic->clkdiv), in_8(&iic->xfrcnt),
92 in_8(&iic->xtcntlss), in_8(&iic->directcntl));
94 # define DUMP_REGS(h,dev) dump_iic_regs((h),(dev))
96 # define DUMP_REGS(h,dev) ((void)0)
99 /* Enable/disable interrupt generation */
100 static inline void iic_interrupt_mode(struct ibm_iic_private* dev, int enable)
102 out_8(&dev->vaddr->intmsk, enable ? INTRMSK_EIMTC : 0);
106 * Initialize IIC interface.
108 static void iic_dev_init(struct ibm_iic_private* dev)
110 volatile struct iic_regs *iic = dev->vaddr;
112 DBG("%d: init\n", dev->idx);
114 /* Clear master address */
115 out_8(&iic->lmadr, 0);
116 out_8(&iic->hmadr, 0);
118 /* Clear slave address */
119 out_8(&iic->lsadr, 0);
120 out_8(&iic->hsadr, 0);
122 /* Clear status & extended status */
123 out_8(&iic->sts, STS_SCMP | STS_IRQA);
124 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD | EXTSTS_LA
125 | EXTSTS_ICT | EXTSTS_XFRA);
127 /* Set clock divider */
128 out_8(&iic->clkdiv, dev->clckdiv);
130 /* Clear transfer count */
131 out_8(&iic->xfrcnt, 0);
133 /* Clear extended control and status */
134 out_8(&iic->xtcntlss, XTCNTLSS_SRC | XTCNTLSS_SRS | XTCNTLSS_SWC
137 /* Clear control register */
138 out_8(&iic->cntl, 0);
140 /* Enable interrupts if possible */
141 iic_interrupt_mode(dev, dev->irq >= 0);
143 /* Set mode control */
144 out_8(&iic->mdcntl, MDCNTL_FMDB | MDCNTL_EINT | MDCNTL_EUBS
145 | (dev->fast_mode ? MDCNTL_FSM : 0));
147 DUMP_REGS("iic_init", dev);
151 * Reset IIC interface
153 static void iic_dev_reset(struct ibm_iic_private* dev)
155 volatile struct iic_regs *iic = dev->vaddr;
159 DBG("%d: soft reset\n", dev->idx);
160 DUMP_REGS("reset", dev);
162 /* Place chip in the reset state */
163 out_8(&iic->xtcntlss, XTCNTLSS_SRST);
165 /* Check if bus is free */
166 dc = in_8(&iic->directcntl);
167 if (!DIRCTNL_FREE(dc)){
168 DBG("%d: trying to regain bus control\n", dev->idx);
170 /* Try to set bus free state */
171 out_8(&iic->directcntl, DIRCNTL_SDAC | DIRCNTL_SCC);
173 /* Wait until we regain bus control */
174 for (i = 0; i < 100; ++i){
175 dc = in_8(&iic->directcntl);
176 if (DIRCTNL_FREE(dc))
179 /* Toggle SCL line */
181 out_8(&iic->directcntl, dc);
184 out_8(&iic->directcntl, dc);
192 out_8(&iic->xtcntlss, 0);
194 /* Reinitialize interface */
199 * IIC interrupt handler
201 static irqreturn_t iic_handler(int irq, void *dev_id, struct pt_regs *regs)
203 struct ibm_iic_private* dev = (struct ibm_iic_private*)dev_id;
204 volatile struct iic_regs* iic = dev->vaddr;
206 DBG2("%d: irq handler, STS = 0x%02x, EXTSTS = 0x%02x\n",
207 dev->idx, in_8(&iic->sts), in_8(&iic->extsts));
209 /* Acknowledge IRQ and wakeup iic_wait_for_tc */
210 out_8(&iic->sts, STS_IRQA | STS_SCMP);
211 wake_up_interruptible(&dev->wq);
217 * Get master transfer result and clear errors if any.
218 * Returns the number of actually transferred bytes or error (<0)
220 static int iic_xfer_result(struct ibm_iic_private* dev)
222 volatile struct iic_regs *iic = dev->vaddr;
224 if (unlikely(in_8(&iic->sts) & STS_ERR)){
225 DBG("%d: xfer error, EXTSTS = 0x%02x\n", dev->idx,
228 /* Clear errors and possible pending IRQs */
229 out_8(&iic->extsts, EXTSTS_IRQP | EXTSTS_IRQD |
230 EXTSTS_LA | EXTSTS_ICT | EXTSTS_XFRA);
232 /* Flush master data buffer */
233 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
236 * If error happened during combined xfer
237 * IIC interface is usually stuck in some strange
238 * state, the only way out - soft reset.
240 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
241 DBG("%d: bus is stuck, resetting\n", dev->idx);
247 return in_8(&iic->xfrcnt) & XFRCNT_MTC_MASK;
251 * Try to abort active transfer.
253 static void iic_abort_xfer(struct ibm_iic_private* dev)
255 volatile struct iic_regs *iic = dev->vaddr;
258 DBG("%d: iic_abort_xfer\n", dev->idx);
260 out_8(&iic->cntl, CNTL_HMT);
263 * Wait for the abort command to complete.
264 * It's not worth to be optimized, just poll (timeout >= 1 tick)
267 while ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
268 if (time_after(jiffies, x)){
269 DBG("%d: abort timeout, resetting...\n", dev->idx);
276 /* Just to clear errors */
277 iic_xfer_result(dev);
281 * Wait for master transfer to complete.
282 * It puts current process to sleep until we get interrupt or timeout expires.
283 * Returns the number of transferred bytes or error (<0)
285 static int iic_wait_for_tc(struct ibm_iic_private* dev){
287 volatile struct iic_regs *iic = dev->vaddr;
293 init_waitqueue_entry(&wait, current);
295 add_wait_queue(&dev->wq, &wait);
296 set_current_state(TASK_INTERRUPTIBLE);
297 if (in_8(&iic->sts) & STS_PT)
298 schedule_timeout(dev->adap.timeout * HZ);
299 set_current_state(TASK_RUNNING);
300 remove_wait_queue(&dev->wq, &wait);
302 if (unlikely(signal_pending(current))){
303 DBG("%d: wait interrupted\n", dev->idx);
305 } else if (unlikely(in_8(&iic->sts) & STS_PT)){
306 DBG("%d: wait timeout\n", dev->idx);
312 unsigned long x = jiffies + dev->adap.timeout * HZ;
314 while (in_8(&iic->sts) & STS_PT){
315 if (unlikely(time_after(jiffies, x))){
316 DBG("%d: poll timeout\n", dev->idx);
321 if (unlikely(signal_pending(current))){
322 DBG("%d: poll interrupted\n", dev->idx);
330 if (unlikely(ret < 0))
333 ret = iic_xfer_result(dev);
335 DBG2("%d: iic_wait_for_tc -> %d\n", dev->idx, ret);
341 * Low level master transfer routine
343 static int iic_xfer_bytes(struct ibm_iic_private* dev, struct i2c_msg* pm,
346 volatile struct iic_regs *iic = dev->vaddr;
348 int i, j, loops, ret = 0;
351 u8 cntl = (in_8(&iic->cntl) & CNTL_AMD) | CNTL_PT;
352 if (pm->flags & I2C_M_RD)
355 loops = (len + 3) / 4;
356 for (i = 0; i < loops; ++i, len -= 4){
357 int count = len > 4 ? 4 : len;
358 u8 cmd = cntl | ((count - 1) << CNTL_TCT_SHIFT);
360 if (!(cntl & CNTL_RW))
361 for (j = 0; j < count; ++j)
362 out_8((volatile u8*)&iic->mdbuf, *buf++);
366 else if (combined_xfer)
369 DBG2("%d: xfer_bytes, %d, CNTL = 0x%02x\n", dev->idx, count, cmd);
372 out_8(&iic->cntl, cmd);
374 /* Wait for completion */
375 ret = iic_wait_for_tc(dev);
377 if (unlikely(ret < 0))
379 else if (unlikely(ret != count)){
380 DBG("%d: xfer_bytes, requested %d, transfered %d\n",
381 dev->idx, count, ret);
383 /* If it's not a last part of xfer, abort it */
384 if (combined_xfer || (i < loops - 1))
392 for (j = 0; j < count; ++j)
393 *buf++ = in_8((volatile u8*)&iic->mdbuf);
396 return ret > 0 ? 0 : ret;
400 * Set target slave address for master transfer
402 static inline void iic_address(struct ibm_iic_private* dev, struct i2c_msg* msg)
404 volatile struct iic_regs *iic = dev->vaddr;
405 u16 addr = msg->addr;
407 DBG2("%d: iic_address, 0x%03x (%d-bit)\n", dev->idx,
408 addr, msg->flags & I2C_M_TEN ? 10 : 7);
410 if (msg->flags & I2C_M_TEN){
411 out_8(&iic->cntl, CNTL_AMD);
412 out_8(&iic->lmadr, addr);
413 out_8(&iic->hmadr, 0xf0 | ((addr >> 7) & 0x06));
416 out_8(&iic->cntl, 0);
417 out_8(&iic->lmadr, addr << 1);
421 static inline int iic_invalid_address(const struct i2c_msg* p)
423 return (p->addr > 0x3ff) || (!(p->flags & I2C_M_TEN) && (p->addr > 0x7f));
426 static inline int iic_address_neq(const struct i2c_msg* p1,
427 const struct i2c_msg* p2)
429 return (p1->addr != p2->addr)
430 || ((p1->flags & I2C_M_TEN) != (p2->flags & I2C_M_TEN));
434 * Generic master transfer entrypoint.
435 * Returns the number of processed messages or error (<0)
437 static int iic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
439 struct ibm_iic_private* dev = (struct ibm_iic_private*)(i2c_get_adapdata(adap));
440 volatile struct iic_regs *iic = dev->vaddr;
443 DBG2("%d: iic_xfer, %d msg(s)\n", dev->idx, num);
448 /* Check the sanity of the passed messages.
449 * Uhh, generic i2c layer is more suitable place for such code...
451 if (unlikely(iic_invalid_address(&msgs[0]))){
452 DBG("%d: invalid address 0x%03x (%d-bit)\n", dev->idx,
453 msgs[0].addr, msgs[0].flags & I2C_M_TEN ? 10 : 7);
456 for (i = 0; i < num; ++i){
457 if (unlikely(msgs[i].len <= 0)){
458 DBG("%d: invalid len %d in msg[%d]\n", dev->idx,
462 if (unlikely(iic_address_neq(&msgs[0], &msgs[i]))){
463 DBG("%d: invalid addr in msg[%d]\n", dev->idx, i);
468 /* Check bus state */
469 if (unlikely((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE)){
470 DBG("%d: iic_xfer, bus is not free\n", dev->idx);
472 /* Usually it means something serious has happend.
473 * We *cannot* have unfinished previous transfer
474 * so it doesn't make any sense to try to stop it.
475 * Probably we were not able to recover from the
477 * The only *reasonable* thing I can think of here
478 * is soft reset. --ebs
482 if ((in_8(&iic->extsts) & EXTSTS_BCS_MASK) != EXTSTS_BCS_FREE){
483 DBG("%d: iic_xfer, bus is still not free\n", dev->idx);
488 /* Flush master data buffer (just in case) */
489 out_8(&iic->mdcntl, in_8(&iic->mdcntl) | MDCNTL_FMDB);
492 /* Load slave address */
493 iic_address(dev, &msgs[0]);
495 /* Do real transfer */
496 for (i = 0; i < num && !ret; ++i)
497 ret = iic_xfer_bytes(dev, &msgs[i], i < num - 1);
499 return ret < 0 ? ret : num;
502 static u32 iic_func(struct i2c_adapter *adap)
504 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR;
507 static struct i2c_algorithm iic_algo = {
508 .name = "IBM IIC algorithm",
510 .master_xfer = iic_xfer,
514 .algo_control = NULL,
515 .functionality = iic_func
519 * Calculates IICx_CLCKDIV value for a specific OPB clock frequency
521 static inline u8 iic_clckdiv(unsigned int opb)
523 /* Compatibility kludge, should go away after all cards
524 * are fixed to fill correct value for opbfreq.
525 * Previous driver version used hardcoded divider value 4,
526 * it corresponds to OPB frequency from the range (40, 50] MHz
529 printk(KERN_WARNING "ibm-iic: using compatibility value for OPB freq,"
530 " fix your board specific setup\n");
537 if (opb < 20 || opb > 150){
538 printk(KERN_CRIT "ibm-iic: invalid OPB clock frequency %u MHz\n",
540 opb = opb < 20 ? 20 : 150;
542 return (u8)((opb + 9) / 10 - 1);
546 * Register single IIC interface
548 static int __devinit iic_probe(struct ocp_device *ocp){
550 struct ibm_iic_private* dev;
551 struct i2c_adapter* adap;
553 bd_t* bd = (bd_t*)&__res;
555 if (!(dev = kmalloc(sizeof(*dev), GFP_KERNEL))){
556 printk(KERN_CRIT "ibm-iic: failed to allocate device data\n");
560 memset(dev, 0, sizeof(*dev));
562 ocp_set_drvdata(ocp, dev);
564 if (!(dev->vaddr = ioremap(ocp->paddr, sizeof(struct iic_regs)))){
565 printk(KERN_CRIT "ibm-iic%d: failed to ioremap device registers\n",
571 init_waitqueue_head(&dev->wq);
573 dev->irq = iic_force_poll ? -1 : ocp->irq;
575 /* Disable interrupts until we finish intialization,
576 assumes level-sensitive IRQ setup...
578 iic_interrupt_mode(dev, 0);
579 if (request_irq(dev->irq, iic_handler, 0, "IBM IIC", dev)){
580 printk(KERN_ERR "ibm-iic%d: request_irq %d failed\n",
582 /* Fallback to the polling mode */
588 printk(KERN_WARNING "ibm-iic%d: using polling mode\n",
591 /* Board specific settings */
592 BUG_ON(dev->idx >= sizeof(bd->bi_iic_fast) / sizeof(bd->bi_iic_fast[0]));
593 dev->fast_mode = iic_force_fast ? 1 : bd->bi_iic_fast[dev->idx];
595 /* clckdiv is the same for *all* IIC interfaces,
596 * but I'd rather make a copy than introduce another global. --ebs
598 dev->clckdiv = iic_clckdiv(bd->bi_opb_busfreq);
599 DBG("%d: clckdiv = %d\n", dev->idx, dev->clckdiv);
601 /* Initialize IIC interface */
604 /* Register it with i2c layer */
606 strcpy(adap->name, "IBM IIC");
607 i2c_set_adapdata(adap, dev);
608 adap->id = I2C_HW_OCP | iic_algo.id;
609 adap->algo = &iic_algo;
610 adap->client_register = NULL;
611 adap->client_unregister = NULL;
615 if ((ret = i2c_add_adapter(adap)) != 0){
616 printk(KERN_CRIT "ibm-iic%d: failed to register i2c adapter\n",
621 printk(KERN_INFO "ibm-iic%d: using %s mode\n", dev->idx,
622 dev->fast_mode ? "fast (400 kHz)" : "standard (100 kHz)");
628 iic_interrupt_mode(dev, 0);
629 free_irq(dev->irq, dev);
632 iounmap((void*)dev->vaddr);
634 ocp_set_drvdata(ocp, 0);
640 * Cleanup initialized IIC interface
642 static void __devexit iic_remove(struct ocp_device *ocp)
644 struct ibm_iic_private* dev = (struct ibm_iic_private*)ocp_get_drvdata(ocp);
646 if (i2c_del_adapter(&dev->adap)){
647 printk(KERN_CRIT "ibm-iic%d: failed to delete i2c adapter :(\n",
649 /* That's *very* bad, just shutdown IRQ ... */
651 iic_interrupt_mode(dev, 0);
652 free_irq(dev->irq, dev);
657 iic_interrupt_mode(dev, 0);
658 free_irq(dev->irq, dev);
660 iounmap((void*)dev->vaddr);
665 static struct ocp_device_id ibm_iic_ids[] __devinitdata =
667 { .vendor = OCP_VENDOR_IBM, .device = OCP_FUNC_IIC },
668 { .vendor = OCP_VENDOR_INVALID }
671 MODULE_DEVICE_TABLE(ocp, ibm_iic_ids);
673 static struct ocp_driver ibm_iic_driver =
676 .id_table = ibm_iic_ids,
678 .remove = __devexit_p(iic_remove),
679 #if defined(CONFIG_PM)
685 static int __init iic_init(void)
687 printk(KERN_INFO "IBM IIC driver v" DRIVER_VERSION "\n");
688 return ocp_module_init(&ibm_iic_driver);
691 static void __exit iic_exit(void)
693 ocp_unregister_driver(&ibm_iic_driver);
696 module_init(iic_init);
697 module_exit(iic_exit);