2 * -------------------------------------------------------------------------
3 * i2c-ibm_iic.c Support for the IIC peripheral on PPC 4xx
4 * -------------------------------------------------------------------------
6 * Cleaned and unified by Matt Porter <mporter@mvista.com>
8 * Original driver by Ian DaSilva <idasilva@mvista.com> and Armin Kuster
11 * Copyright 2000-2003 MontaVista Software Inc.
13 * ----------------------------------------------------------------------------
14 * This file was highly leveraged from i2c-elektor.c, which was created
15 * by Simon G. Vogl and Hans Berglund:
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> and even
22 * Frodo Looijaard <frodol@dds.nl>
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
37 * ----------------------------------------------------------------------------
40 #include <linux/kernel.h>
41 #include <linux/ioport.h>
42 #include <linux/module.h>
43 #include <linux/delay.h>
44 #include <linux/slab.h>
45 #include <linux/version.h>
46 #include <linux/init.h>
49 #include <linux/i2c.h>
50 #include <linux/i2c-id.h>
54 * This next section is configurable, and it is used to set the number
55 * of i2c controllers in the system. The default number of instances is 1,
56 * however, this should be changed to reflect your system's configuration.
59 static int i2c_debug = 0;
60 static int iic_scan = 0; /* have a look at what's hanging 'round */
61 static wait_queue_head_t iic_wait[2];
62 static int iic_pending;
64 #define DEB(x) if (i2c_debug>=1) x
65 #define DEB2(x) if (i2c_debug>=2) x
66 #define DEB3(x) if (i2c_debug>=3) x /* print several statistical values */
67 #define DEBPROTO(x) if (i2c_debug>=9) x;
68 /* debug the protocol by showing transferred bits */
71 #define IIC_DEV(x) ((struct ocp_dev *)adap->data)->x
73 #define iic_outb( reg, val) writeb(val,&(reg))
74 #define iic_inb( reg) readb(&(reg))
76 #define IIC_SINGLE_XFER 0
77 #define IIC_COMBINED_XFER 1
79 #define IIC_ERR_LOST_ARB -2
80 #define IIC_ERR_INCOMPLETE_XFR -3
81 #define IIC_ERR_NACK -1
82 #define IIC_TIMEOUT 100
85 #define IIC_DEV(x) ((struct ocp_dev *)adap->data)->x
88 * Determine if there was an error in the requested transaction.
89 * If so, determine the cause.
92 iic_analyze_status(struct i2c_adapter *adap, int *error_code)
97 iic = (struct iic_regs *) IIC_DEV(vaddr);
99 ret = iic_inb(iic->sts);
102 ret = iic_inb(iic->extsts);
104 /* Lost arbitration */
105 *error_code = IIC_ERR_LOST_ARB;
108 /* Incomplete transfer */
109 *error_code = IIC_ERR_INCOMPLETE_XFR;
112 /* Master transfer aborted by a NACK during the transfer of the address byte */
113 *error_code = IIC_ERR_NACK;
121 * Generate I2C compliant 7-bit address.
123 * Note: 10 bit addresses are not supported in this driver, although they are
124 * supported by the hardware. This functionality needs to be implemented.
127 iic_do_address(struct i2c_adapter *adap, struct i2c_msg *msg, int retries)
129 struct iic_regs *iic;
130 unsigned short flags = msg->flags;
133 iic = (struct iic_regs *) IIC_DEV(vaddr);
136 * The following segment for 10 bit addresses needs to be ported
139 /* Ten bit addresses not supported right now */
140 if ((flags & I2C_M_TEN)) {
141 /* a ten bit address */
142 addr = 0xf0 | ((msg->addr >> 7) & 0x03);
143 DEB2(printk(KERN_DEBUG "addr0: %d\n", addr));
144 /* try extended address code... */
145 ret = try_address(adap, addr, retries);
148 "iic_do_address: died at extended address code.\n");
151 /* the remaining 8 bit address */
152 iic_outb(msg->addr & 0x7f);
153 /* Status check comes here */
156 "iic_do_address: died at 2nd address code.\n");
159 if (flags & I2C_M_RD) {
161 /* okay, now switch into reading mode */
163 ret = try_address(adap, addr, retries);
166 "iic_do_address: died at extended address code.\n");
171 /* ----------> normal 7 bit address */
172 /*Ten bit addresses not supported yet */
174 addr = (msg->addr << 1);
175 if (flags & I2C_M_RD)
177 if (flags & I2C_M_REV_DIR_ADDR)
180 * Write to the low slave address
182 iic_outb(iic->lmadr, addr);
184 * Write zero to the high slave register since we are
185 * only using 7 bit addresses
187 iic_outb(iic->hmadr, 0);
193 * Initialize IBM IIC.
196 iic_adap_init(struct i2c_adapter *adap)
198 struct iic_regs *iic;
199 unsigned short retval;
201 iic = (struct iic_regs *) ((struct ocp_dev *) adap->data)->vaddr;
203 /* Clear master low master address */
204 iic_outb(iic->lmadr, 0);
206 /* Clear high master address */
207 iic_outb(iic->hmadr, 0);
209 /* Clear low slave address */
210 iic_outb(iic->lsadr, 0);
212 /* Clear high slave address */
213 iic_outb(iic->hsadr, 0);
216 iic_outb(iic->sts, 0x0a);
218 /* Clear extended status */
219 iic_outb(iic->extsts, 0x8f);
221 /* Set clock division */
222 iic_outb(iic->clkdiv, 0x04);
224 retval = iic_inb(iic->clkdiv);
225 DEB(printk("iic_adap_init: CLKDIV register = %x\n", retval));
227 /* Enable interrupts on Requested Master Transfer Complete */
228 iic_outb(iic->intmsk, 0x01);
230 /* Clear transfer count */
231 iic_outb(iic->xfrcnt, 0x0);
233 /* Clear extended control and status */
234 iic_outb(iic->xtcntlss, 0xf0);
236 /* Set mode control (flush master data buf, enable hold SCL, exit */
238 iic_outb(iic->mdcntl, 0x47);
240 /* Clear control register */
241 iic_outb(iic->cntl, 0x0);
243 DEB2(printk(KERN_DEBUG "iic_adap_init: IBM IIC initialized.\n"));
248 * Description: After we issue a transaction on the IIC bus, this function
249 * is called. It puts this process to sleep until we get an interrupt from
250 * from the controller telling us that the transaction we requested in complete.
253 iic_wait_for_irq(struct i2c_adapter *adap, int *status)
256 int timeout = DEF_TIMEOUT;
258 struct iic_regs *iic;
260 iic = (struct iic_regs *) IIC_DEV(vaddr);
262 *status = iic_inb(iic->sts);
264 while (timeout-- && (*status & 0x01)) {
266 struct ocp_dev *iic_dev;
268 iic_dev = (struct ocp_dev *) adap->data;
271 * If interrupts are enabled (which they are),
272 * then put the process to sleep. This process
273 * will be awakened by two events -- either the
274 * the IIC peripheral interrupts or the timeout
277 if (iic_dev->irq > 0) {
279 if (iic_pending == 0) {
280 interruptible_sleep_on_timeout(&
289 * If interrupts are not enabled then
290 * delay for a reasonable amount of
291 * time and return. We expect that by
292 * time we return to the calling function
293 * that the IIC has finished our requested
294 * transaction and the status bit reflects
297 * udelay is probably not the best choice
298 * for this since it is the equivalent of
303 *status = iic_inb(iic->sts);
307 /* Issue stop signal on the bus, and force an interrupt */
308 retval = iic_inb(iic->cntl);
309 iic_outb(iic->cntl, retval | 0x80);
310 /* Clear status register */
311 iic_outb(iic->sts, 0x0a);
312 /* Exit unknown bus state */
313 retval = iic_inb(iic->mdcntl);
314 iic_outb(iic->mdcntl, (retval | 0x02));
316 /* Pending transfer bit cleared? */
317 retval = iic_inb(iic->sts);
318 retval = retval & 0x01;
321 * IIC has locked up. Perform a soft reset
322 * to regain control of the IIC and then
325 iic_outb(iic->xtcntlss, 0x01);
328 /* Pending transfer bit cleared? */
329 retval = iic_inb(iic->sts);
330 retval = retval & 0x01;
333 "IIC has locked up. Processor reset is required.\n");
336 * IIC sometimes needs the interrupt bit
337 * written again. Sometimes it does not
338 * stick when written in iic_adap_init.
340 iic_outb(iic->intmsk, 0x01);
348 * Low level master send transaction.
352 iic_sendbytes(struct i2c_adapter *adap, const char *buf,
353 int count, int xfer_flag)
355 struct iic_regs *iic;
356 int wrcount, status, timeout;
357 int loops, remainder, i, j;
360 iic = (struct iic_regs *) IIC_DEV(vaddr);
366 remainder = count % 4;
368 if ((loops > 1) && (remainder == 0)) {
369 for (i = 0; i < (loops - 1); i++) {
371 /* Write four bytes to master data buffer */
372 for (j = 0; j < 4; j++) {
373 iic_outb(iic->mdbuf, buf[wrcount++]);
376 /* Issue IIC command to begin transmission */
377 iic_outb(iic->cntl, 0x35);
380 * Wait for transmission to complete. When
381 * complete, continue next loop iteration.
383 timeout = iic_wait_for_irq(adap, &status);
390 ret = iic_analyze_status(adap, &error_code);
393 if (error_code == IIC_ERR_INCOMPLETE_XFR) {
394 /* Return bytes transferred */
395 ret = iic_inb(iic->xfrcnt);
397 return (wrcount - 4 + ret);
402 } else if ((loops >= 1) && (remainder > 0)) {
403 for (i = 0; i < loops; i++) {
404 /* Write four bytes to master data buffer */
405 for (j = 0; j < 4; j++) {
406 iic_outb(iic->mdbuf, buf[wrcount++]);
409 /* Issue IIC command to begin transmission */
410 iic_outb(iic->cntl, 0x35);
413 * Wait for transmission to complete. When
414 * complete, continue next loop iteration.
416 timeout = iic_wait_for_irq(adap, &status);
420 ret = iic_analyze_status(adap, &error_code);
422 if (error_code == IIC_ERR_INCOMPLETE_XFR) {
423 /* Return bytes transferred */
424 ret = iic_inb(iic->xfrcnt);
426 return (wrcount - 4 + ret);
436 /* Write the remaining bytes (<=4) */
437 for (i = 0; i < remainder; i++)
438 iic_outb(iic->mdbuf, buf[wrcount++]);
440 if (xfer_flag == IIC_COMBINED_XFER) {
441 iic_outb(iic->cntl, (0x09 | ((remainder - 1) << 4)));
443 iic_outb(iic->cntl, (0x01 | ((remainder - 1) << 4)));
445 DEB2(printk(KERN_DEBUG "iic_sendbytes: Waiting for interrupt\n"));
446 timeout = iic_wait_for_irq(adap, &status);
450 ret = iic_analyze_status(adap, &error_code);
453 if (error_code == IIC_ERR_INCOMPLETE_XFR) {
454 /* Return bytes transferred */
455 ret = iic_inb(iic->xfrcnt);
457 return (wrcount - 4 + ret);
462 DEB2(printk(KERN_DEBUG "iic_sendbytes: Got interrupt\n"));
467 * Low level master read transaction.
470 iic_readbytes(struct i2c_adapter *adap, char *buf, int count, int xfer_type)
472 struct iic_regs *iic;
473 int rdcount = 0, i, status, timeout;
474 int loops, remainder, j;
477 iic = (struct iic_regs *) IIC_DEV(vaddr);
482 remainder = count % 4;
484 if ((loops > 1) && (remainder == 0)) {
485 for (i = 0; i < (loops - 1); i++) {
486 /* Issue IIC master read command (<=4 bytes) */
487 iic_outb(iic->cntl, 0x37);
490 * Wait for transmission to complete. When
491 * complete, continue next loop iteration.
493 timeout = iic_wait_for_irq(adap, &status);
497 ret = iic_analyze_status(adap, &error_code);
500 if (error_code == IIC_ERR_INCOMPLETE_XFR)
506 for (j = 0; j < 4; j++) {
508 * Wait for data to shuffle to top of
509 * data buffer. This value needs to be
513 buf[rdcount] = iic_inb(iic->mdbuf);
519 else if ((loops >= 1) && (remainder > 0)) {
520 for (i = 0; i < loops; i++) {
521 /* Issue IIC master read (<=4 bytes) */
522 iic_outb(iic->cntl, 0x37);
525 * Wait for transmission to complete. When
526 * complete, continue next loop iteration.
528 timeout = iic_wait_for_irq(adap, &status);
532 ret = iic_analyze_status(adap, &error_code);
535 if (error_code == IIC_ERR_INCOMPLETE_XFR)
541 for (j = 0; j < 4; j++) {
543 * Wait for data to shuffle to top of
544 * data buffer. This value needs to be
548 buf[rdcount] = iic_inb(iic->mdbuf);
557 (KERN_DEBUG "iic_readbytes: writing %x to IIC0CNTL\n",
558 (0x03 | ((remainder - 1) << 4))));
560 if (xfer_type == IIC_COMBINED_XFER) {
561 iic_outb(iic->cntl, (0x0b | ((remainder - 1) << 4)));
563 iic_outb(iic->cntl, (0x03 | ((remainder - 1) << 4)));
565 DEB2(printk(KERN_DEBUG "iic_readbytes: Wait for pin\n"));
566 timeout = iic_wait_for_irq(adap, &status);
567 DEB2(printk(KERN_DEBUG "iic_readbytes: Got the interrupt\n"));
571 ret = iic_analyze_status(adap, &error_code);
574 if (error_code == IIC_ERR_INCOMPLETE_XFR)
580 for (i = 0; i < remainder; i++) {
581 buf[rdcount] = iic_inb(iic->mdbuf);
589 * Execute combined transations. Combined transactions consist
590 * of combinations of reading and writing blocks of data. Each
591 * transfer (i.e. a read or a write) is separated by a repeated
595 iic_combined_transaction(struct i2c_adapter *i2c_adap, struct i2c_msg msgs[],
599 struct i2c_msg *pmsg;
602 DEB2(printk(KERN_DEBUG "Beginning combined transaction\n"));
603 for (i = 0; i < num; i++) {
605 if (pmsg->flags & I2C_M_RD) {
607 /* Last segment needs to be terminated with a stop */
609 DEB2(printk(KERN_DEBUG "This one is a read\n"));
612 (KERN_DEBUG "Doing the last read\n"));
615 iic_readbytes(i2c_adap, pmsg->buf, pmsg->len,
618 1) ? IIC_COMBINED_XFER :
621 if (ret != pmsg->len) {
622 DEB2(printk("iic_combined_transation: fail: "
623 "only read %d bytes.\n", ret));
627 ("iic_combined_transaction: read %d bytes.\n",
630 } else if (!(pmsg->flags & I2C_M_RD)) {
632 /* Last segment needs to be terminated with a stop */
635 (KERN_DEBUG "This one is a write\n"));
638 (KERN_DEBUG "Doing the last write\n"));
641 iic_sendbytes(i2c_adap, pmsg->buf, pmsg->len,
644 1) ? IIC_COMBINED_XFER :
647 if (ret != pmsg->len) {
648 DEB2(printk("iic_combined_transaction: fail: "
649 "only wrote %d bytes.\n", ret));
653 ("iic_combined_transaction: wrote %d bytes.\n",
663 * Prepare controller for a transaction and call iic_sendbytes or
664 * iic_readbytes to do the work.
667 iic_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[], int num)
669 struct iic_regs *iic;
670 struct i2c_msg *pmsg;
674 iic = (struct iic_regs *) IIC_DEV(vaddr);
679 * Clear status register
682 (KERN_DEBUG "iic_xfer: iic_xfer: Clearing status register\n"));
683 iic_outb(iic->sts, 0x0a);
685 /* Wait for any pending transfers to complete */
688 "iic_xfer: Waiting for any pending transfers to complete\n"));
689 while ((ret = iic_inb(iic->sts)) == 0x01) {
693 /* Flush master data buffer */
694 DEB2(printk(KERN_DEBUG "iic_xfer: Clearing master data buffer\n"));
695 ret = iic_inb(iic->mdcntl);
696 iic_outb(iic->mdcntl, ret | 0x40);
698 /* Load slave address */
699 DEB2(printk(KERN_DEBUG "iic_xfer: Loading slave address\n"));
700 ret = iic_do_address(adap, pmsg, adap->retries);
702 /* Check to see if the bus is busy */
703 ret = iic_inb(iic->extsts);
705 /* Mask off the irrelevent bits */
708 /* Bus is free when BCS bits in the EXTSTS register are 0b100 */
710 return IIC_ERR_LOST_ARB;
712 /* Combined transaction (read/write) */
715 (KERN_DEBUG "iic_xfer: Call combined transaction\n"));
716 ret = iic_combined_transaction(adap, msgs, num);
719 else if ((num == 1) && (pmsg->flags & I2C_M_RD)) {
720 /* Tell device to begin reading data from the master data */
721 DEB2(printk(KERN_DEBUG "iic_xfer: Call adapter's read\n"));
723 iic_readbytes(adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
726 else if ((num == 1) && (!(pmsg->flags & I2C_M_RD))) {
728 * Write data to master data buffers and tell our device
729 * to begin transmitting
731 DEB2(printk(KERN_DEBUG "iic_xfer: Call adapter's write\n"));
733 iic_sendbytes(adap, pmsg->buf, pmsg->len, IIC_SINGLE_XFER);
740 iic_func(struct i2c_adapter *adap)
742 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
745 static struct i2c_algorithm iic_algo = {
750 NULL, /* slave_xmit */
751 NULL, /* slave_recv */
753 iic_func, /* functionality */
757 * Register bus structure
760 iic_add_bus(struct i2c_adapter *adap)
764 (KERN_DEBUG "iic_add_bus: hw routines for %s registered.\n",
766 adap->id |= iic_algo.id;
767 adap->algo = &iic_algo;
768 adap->timeout = IIC_TIMEOUT;
769 adap->retries = IIC_RETRY;
772 i2c_add_adapter(adap);
775 * Scan the I2C bus for valid 7 bit addresses
776 * (ie things that ACK on 1byte read)
777 * if i2c_debug is off we print everything on one line.
778 * if i2c_debug is on we do a newline per print so we don't
779 * clash too much with printk's in the other functions.
780 * TODO: check for 10-bit mode and never run as a slave.
786 printk(KERN_INFO " iic_add_bus: scanning bus %s. Found ",
790 for (i = 0; i < 0x7f; i++) {
797 msg.flags = I2C_M_RD;
798 if ((ret = iic_xfer(adap, &msg, 1)) == 1) {
801 ("I2C Found 0x%02x ret %d Data 0x%02x\n",
804 printk("0x%02x ", i);
810 printk("I2C Found Nothing\n");
823 iic_del_bus(struct i2c_adapter *adap)
826 if ((res = i2c_del_adapter(adap)) < 0)
829 (KERN_DEBUG "iic_del_bus: adapter unregistered: %s\n",
836 * IIC interrupt handler
839 iic_handler(int this_irq, void *dev_id, struct pt_regs *regs)
842 struct iic_regs *iic;
843 struct ocp_dev *iic_dev;
845 iic_dev = (struct ocp_dev *) dev_id;
846 iic = (struct iic_regs *) iic_dev->vaddr;
850 DEB2(printk("iic_handler: in interrupt handler\n"));
851 /* Read status register */
852 ret = readb((int) &(iic->sts));
853 DEB2(printk("iic_handler: status = %x\n", ret));
854 /* Clear status register */
855 writeb(0x0a, (int) &(iic->sts));
856 wake_up_interruptible(&(iic_wait[iic_dev->num]));
860 * Release irq and memory
866 struct ocp_dev *iic_drv;
868 for (i = 0; i < ocp_get_max(IIC); i++) {
869 iic_drv = ocp_get_dev(IIC, i);
870 if (iic_drv->irq > 0) {
871 disable_irq(iic_drv->irq);
872 free_irq(iic_drv->irq, 0);
879 iic_reg(struct i2c_client *client)
885 iic_unreg(struct i2c_client *client)
891 * Increment module usage count
894 iic_inc_use(struct i2c_adapter *adap)
902 * Decrement module usage count
905 iic_dec_use(struct i2c_adapter *adap)
913 * Called when the module is loaded. This function starts the
914 * cascade of calls up through the heirarchy of i2c modules
915 * (i.e. up to the algorithm layer and into to the core layer)
920 struct ocp_dev *iic_drv;
921 struct i2c_adapter *adap;
924 printk(KERN_INFO "IBM IIC driver\n");
926 while (curr_iic != -ENXIO) {
927 if (!(iic_drv = ocp_alloc_dev(sizeof (struct i2c_adapter))))
930 if ((curr_iic = ocp_register(iic_drv)) != -ENXIO) {
931 adap = (struct i2c_adapter *) iic_drv->ocpdev;
932 iic_drv->vaddr = ioremap(iic_drv->paddr, 17);
933 strcpy(adap->name, "IBM IIC adapter");
934 adap->data = (void *) iic_drv;
935 adap->id = I2C_HW_OCP;
937 adap->inc_use = iic_inc_use;
938 adap->dec_use = iic_dec_use;
939 adap->client_register = iic_reg;
940 adap->client_unregister = iic_unreg;
942 init_waitqueue_head(&(iic_wait[curr_iic]));
944 if (iic_drv->irq > 0) {
947 iic_handler, 0, "IBM IIC", iic_drv)) {
949 "iic_init: Request irq %d failed\n",
954 ("iic_init: Enabled interrupt\n"));
958 if (iic_add_bus(adap) < 0)
962 ocp_free_dev(iic_drv);
966 return (curr_iic == -ENXIO) ? 0 : curr_iic;
973 struct ocp_dev *iic_drv;
974 struct i2c_adapter *adap;
976 for (i = 0; i < ocp_get_max(IIC); i++) {
977 iic_drv = ocp_get_dev(IIC, i);
978 adap = (struct i2c_adapter *) iic_drv->ocpdev;
986 * Put the process to sleep for a period equal to timeout
989 iic_sleep(unsigned long timeout)
991 schedule_timeout(timeout * HZ);
994 MODULE_AUTHOR("MontaVista Software <www.mvista.com>");
995 MODULE_DESCRIPTION("IBM IIC driver");
996 MODULE_PARM(iic_scan, "i");
997 MODULE_PARM(i2c_debug, "i");
998 MODULE_PARM_DESC(iic_scan, "Scan for active chips on the bus");
999 MODULE_PARM_DESC(i2c_debug,
1000 "debug level - 0 off; 1 normal; 2,3 more verbose; 9 iic-protocol");
1001 MODULE_LICENSE("GPL");
1003 module_init(iic_init);
1004 module_exit(iic_exit);