2 * i2c-algo-8xx.c i2x driver algorithms for MPC8XX CPM
3 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * moved into proper i2c interface; separated out platform specific
20 * parts into i2c-rpx.c
21 * Brad Parker (brad@heeltoe.com)
27 /* $Id: i2c-algo-8xx.c,v 1.1.1.1 2005/04/11 02:50:21 jack Exp $ */
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/version.h>
34 #include <linux/init.h>
35 #include <asm/uaccess.h>
36 #include <linux/ioport.h>
37 #include <linux/errno.h>
38 #include <linux/sched.h>
40 #include <asm/mpc8xx.h>
41 #include <asm/commproc.h>
43 #include <linux/i2c.h>
44 #include <linux/i2c-algo-8xx.h>
46 #define CPM_MAX_READ 513
47 /* #define I2C_CHIP_ERRATA */ /* Try uncomment this if you have an older CPU(earlier than rev D4) */
48 static wait_queue_head_t iic_wait;
49 static ushort r_tbase, r_rbase;
55 cpm_iic_interrupt(void *dev_id, struct pt_regs *regs)
57 volatile i2c8xx_t *i2c = (i2c8xx_t *)dev_id;
59 printk("cpm_iic_interrupt(dev_id=%p)\n", dev_id);
61 /* Chip errata, clear enable. This is not needed on rev D4 CPUs */
62 /* This should probably be removed and replaced by I2C_CHIP_ERRATA stuff */
63 /* Someone with a buggy CPU needs to confirm that */
68 i2c->i2c_i2cer = 0xff;
70 /* Get 'me going again.
72 wake_up_interruptible(&iic_wait);
76 cpm_iic_init(struct i2c_algo_8xx_data *cpm_adap)
78 volatile iic_t *iip = cpm_adap->iip;
79 volatile i2c8xx_t *i2c = cpm_adap->i2c;
81 bd_t *bd = (bd_t *)__res;
83 if (cpm_debug) printk(KERN_DEBUG "cpm_iic_init()\n");
85 /* Initialize the parameter ram.
86 * We need to make sure many things are initialized to zero,
87 * especially in the case of a microcode patch.
100 /* Set up the IIC parameters in the parameter ram.
102 iip->iic_tbase = r_tbase = cpm_adap->dp_addr;
103 iip->iic_rbase = r_rbase = cpm_adap->dp_addr + sizeof(cbd_t)*2;
105 iip->iic_tfcr = SMC_EB;
106 iip->iic_rfcr = SMC_EB;
108 /* Set maximum receive size.
110 iip->iic_mrblr = CPM_MAX_READ;
112 /* Initialize Tx/Rx parameters.
114 if (cpm_adap->reloc == 0) {
115 volatile cpm8xx_t *cp = cpm_adap->cp;
118 mk_cr_cmd(CPM_CR_CH_I2C, CPM_CR_INIT_TRX) | CPM_CR_FLG;
119 while (cp->cp_cpcr & CPM_CR_FLG);
121 iip->iic_rbptr = iip->iic_rbase;
122 iip->iic_tbptr = iip->iic_tbase;
127 /* Select an arbitrary address. Just make sure it is unique.
129 i2c->i2c_i2add = 0xfe;
131 /* Make clock run at 60 KHz.
133 brg = (unsigned char) (bd->bi_intfreq/(32*2*60000) -3);
134 i2c->i2c_i2brg = brg;
136 i2c->i2c_i2mod = 0x00;
137 i2c->i2c_i2com = 0x01; /* Master mode */
139 /* Disable interrupts.
142 i2c->i2c_i2cer = 0xff;
144 init_waitqueue_head(&iic_wait);
146 /* Install interrupt handler.
149 printk ("%s[%d] Install ISR for IRQ %d\n",
150 __func__,__LINE__, CPMVEC_I2C);
152 (*cpm_adap->setisr)(CPMVEC_I2C, cpm_iic_interrupt, (void *)i2c);
157 cpm_iic_shutdown(struct i2c_algo_8xx_data *cpm_adap)
159 volatile i2c8xx_t *i2c = cpm_adap->i2c;
163 i2c->i2c_i2mod &= ~1;
165 i2c->i2c_i2cer = 0xff;
171 cpm_reset_iic_params(volatile iic_t *iip)
173 iip->iic_tbase = r_tbase;
174 iip->iic_rbase = r_rbase;
176 iip->iic_tfcr = SMC_EB;
177 iip->iic_rfcr = SMC_EB;
179 iip->iic_mrblr = CPM_MAX_READ;
183 iip->iic_rbptr = iip->iic_rbase;
188 iip->iic_tbptr = iip->iic_tbase;
193 #define BD_SC_NAK ((ushort)0x0004) /* NAK - did not respond */
194 #define BD_SC_OV ((ushort)0x0002) /* OV - receive overrun */
195 #define CPM_CR_CLOSE_RXBD ((ushort)0x0007)
197 static void force_close(struct i2c_algo_8xx_data *cpm)
199 volatile i2c8xx_t *i2c = cpm->i2c;
200 if (cpm->reloc == 0) { /* micro code disabled */
201 volatile cpm8xx_t *cp = cpm->cp;
203 if (cpm_debug) printk("force_close()\n");
205 mk_cr_cmd(CPM_CR_CH_I2C, CPM_CR_CLOSE_RXBD) |
208 while (cp->cp_cpcr & CPM_CR_FLG);
210 i2c->i2c_i2cmr = 0x00; /* Disable all interrupts */
211 i2c->i2c_i2cer = 0xff;
216 * abyte = address byte, with r/w flag already set
219 cpm_iic_read(struct i2c_algo_8xx_data *cpm, u_char abyte, char *buf, int count)
221 volatile iic_t *iip = cpm->iip;
222 volatile i2c8xx_t *i2c = cpm->i2c;
223 volatile cpm8xx_t *cp = cpm->cp;
224 volatile cbd_t *tbdf, *rbdf;
226 unsigned long flags, tmo;
228 if (count >= CPM_MAX_READ)
231 /* check for and use a microcode relocation patch */
233 cpm_reset_iic_params(iip);
236 tbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_tbase];
237 rbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_rbase];
239 /* To read, we need an empty buffer of the proper length.
240 * All that is used is the first byte for address, the remainder
241 * is just used for timing (and doesn't really have to exist).
244 tb = (u_char *)(((uint)tb + 15) & ~15);
245 tb[0] = abyte; /* Device address byte w/rw flag */
247 flush_dcache_range((unsigned long) tb, (unsigned long) (tb+1));
249 if (cpm_debug) printk("cpm_iic_read(abyte=0x%x)\n", abyte);
251 tbdf->cbd_bufaddr = __pa(tb);
252 tbdf->cbd_datlen = count + 1;
254 BD_SC_READY | BD_SC_LAST |
255 BD_SC_WRAP | BD_IIC_START;
257 iip->iic_mrblr = count +1; /* prevent excessive read, +1
258 is needed otherwise will the
259 RXB interrupt come too early */
261 /* flush will invalidate too. */
262 flush_dcache_range((unsigned long) buf, (unsigned long) (buf+count));
264 rbdf->cbd_datlen = 0;
265 rbdf->cbd_bufaddr = __pa(buf);
266 rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP| BD_SC_INTRPT;
268 /* Chip bug, set enable here */
269 local_irq_save(flags);
270 i2c->i2c_i2cmr = 0x13; /* Enable some interupts */
271 i2c->i2c_i2cer = 0xff;
272 i2c->i2c_i2mod |= 1; /* Enable */
273 i2c->i2c_i2com |= 0x80; /* Begin transmission */
275 /* Wait for IIC transfer */
276 tmo = interruptible_sleep_on_timeout(&iic_wait,1*HZ);
277 local_irq_restore(flags);
278 } else { /* busy wait for small transfers, its faster */
279 i2c->i2c_i2cmr = 0x00; /* Disable I2C interupts */
280 i2c->i2c_i2cer = 0xff;
281 i2c->i2c_i2mod |= 1; /* Enable */
282 i2c->i2c_i2com |= 0x80; /* Begin transmission */
283 tmo = jiffies + 1*HZ;
284 while(!(i2c->i2c_i2cer & 0x11 || time_after(jiffies, tmo))); /* Busy wait, with a timeout */
287 if (signal_pending(current) || !tmo){
290 printk("IIC read: timeout!\n");
293 #ifdef I2C_CHIP_ERRATA
294 /* Chip errata, clear enable. This is not needed on rev D4 CPUs.
295 Disabling I2C too early may cause too short stop condition */
297 i2c->i2c_i2mod &= ~1;
300 printk("tx sc %04x, rx sc %04x\n",
301 tbdf->cbd_sc, rbdf->cbd_sc);
304 if (tbdf->cbd_sc & BD_SC_READY) {
305 printk("IIC read; complete but tbuf ready\n");
307 printk("tx sc %04x, rx sc %04x\n",
308 tbdf->cbd_sc, rbdf->cbd_sc);
311 if (tbdf->cbd_sc & BD_SC_NAK) {
313 printk("IIC read; no ack\n");
317 if (rbdf->cbd_sc & BD_SC_EMPTY) {
318 /* force_close(cpm); */
320 printk("IIC read; complete but rbuf empty\n");
321 printk("tx sc %04x, rx sc %04x\n",
322 tbdf->cbd_sc, rbdf->cbd_sc);
327 if (rbdf->cbd_sc & BD_SC_OV) {
329 printk("IIC read; Overrun\n");
333 if (cpm_debug) printk("read %d bytes\n", rbdf->cbd_datlen);
335 if (rbdf->cbd_datlen < count) {
337 printk("IIC read; short, wanted %d got %d\n",
338 count, rbdf->cbd_datlen);
346 * addr = address byte, with r/w flag already set
349 cpm_iic_write(struct i2c_algo_8xx_data *cpm, u_char abyte, char *buf,int count)
351 volatile iic_t *iip = cpm->iip;
352 volatile i2c8xx_t *i2c = cpm->i2c;
353 volatile cpm8xx_t *cp = cpm->cp;
354 volatile cbd_t *tbdf;
356 unsigned long flags, tmo;
358 /* check for and use a microcode relocation patch */
360 cpm_reset_iic_params(iip);
363 tb = (u_char *)(((uint)tb + 15) & ~15);
364 *tb = abyte; /* Device address byte w/rw flag */
366 flush_dcache_range((unsigned long) tb, (unsigned long) (tb+1));
367 flush_dcache_range((unsigned long) buf, (unsigned long) (buf+count));
369 if (cpm_debug) printk("cpm_iic_write(abyte=0x%x)\n", abyte);
371 /* set up 2 descriptors */
372 tbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_tbase];
374 tbdf[0].cbd_bufaddr = __pa(tb);
375 tbdf[0].cbd_datlen = 1;
376 tbdf[0].cbd_sc = BD_SC_READY | BD_IIC_START;
378 tbdf[1].cbd_bufaddr = __pa(buf);
379 tbdf[1].cbd_datlen = count;
380 tbdf[1].cbd_sc = BD_SC_READY | BD_SC_INTRPT | BD_SC_LAST | BD_SC_WRAP;
383 /* Chip bug, set enable here */
384 local_irq_save(flags);
385 i2c->i2c_i2cmr = 0x13; /* Enable some interupts */
386 i2c->i2c_i2cer = 0xff;
387 i2c->i2c_i2mod |= 1; /* Enable */
388 i2c->i2c_i2com |= 0x80; /* Begin transmission */
390 /* Wait for IIC transfer */
391 tmo = interruptible_sleep_on_timeout(&iic_wait,1*HZ);
392 local_irq_restore(flags);
393 } else { /* busy wait for small transfers, its faster */
394 i2c->i2c_i2cmr = 0x00; /* Disable I2C interupts */
395 i2c->i2c_i2cer = 0xff;
396 i2c->i2c_i2mod |= 1; /* Enable */
397 i2c->i2c_i2com |= 0x80; /* Begin transmission */
398 tmo = jiffies + 1*HZ;
399 while(!(i2c->i2c_i2cer & 0x12 || time_after(jiffies, tmo))); /* Busy wait, with a timeout */
402 if (signal_pending(current) || !tmo){
404 if(cpm_debug && !tmo)
405 printk("IIC write: timeout!\n");
410 /* Chip errata, clear enable. This is not needed on rev D4 CPUs.
411 Disabling I2C too early may cause too short stop condition */
413 i2c->i2c_i2mod &= ~1;
416 printk("tx0 sc %04x, tx1 sc %04x\n",
417 tbdf[0].cbd_sc, tbdf[1].cbd_sc);
420 if (tbdf->cbd_sc & BD_SC_NAK) {
422 printk("IIC write; no ack\n");
426 if (tbdf->cbd_sc & BD_SC_READY) {
428 printk("IIC write; complete but tbuf ready\n");
435 /* See if an IIC address exists..
436 * addr = 7 bit address, unshifted
439 cpm_iic_tryaddress(struct i2c_algo_8xx_data *cpm, int addr)
441 volatile iic_t *iip = cpm->iip;
442 volatile i2c8xx_t *i2c = cpm->i2c;
443 volatile cpm8xx_t *cp = cpm->cp;
444 volatile cbd_t *tbdf, *rbdf;
446 unsigned long flags, len, tmo;
449 printk("cpm_iic_tryaddress(cpm=%p,addr=%d)\n", cpm, addr);
451 /* check for and use a microcode relocation patch */
453 cpm_reset_iic_params(iip);
456 if (cpm_debug && addr == 0) {
457 printk("iip %p, dp_addr 0x%x\n", cpm->iip, cpm->dp_addr);
458 printk("iic_tbase %d, r_tbase %d\n", iip->iic_tbase, r_tbase);
461 tbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_tbase];
462 rbdf = (cbd_t *)&cp->cp_dpmem[iip->iic_rbase];
465 tb = (u_char *)(((uint)tb + 15) & ~15);
467 /* do a simple read */
468 tb[0] = (addr << 1) | 1; /* device address (+ read) */
471 flush_dcache_range((unsigned long) tb, (unsigned long) (tb+2));
473 tbdf->cbd_bufaddr = __pa(tb);
474 tbdf->cbd_datlen = len;
476 BD_SC_READY | BD_SC_LAST |
477 BD_SC_WRAP | BD_IIC_START;
479 rbdf->cbd_datlen = 0;
480 rbdf->cbd_bufaddr = __pa(tb+2);
481 rbdf->cbd_sc = BD_SC_EMPTY | BD_SC_WRAP | BD_SC_INTRPT;
483 local_irq_save(flags);
484 i2c->i2c_i2cmr = 0x13; /* Enable some interupts */
485 i2c->i2c_i2cer = 0xff;
486 i2c->i2c_i2mod |= 1; /* Enable */
487 i2c->i2c_i2com |= 0x80; /* Begin transmission */
489 if (cpm_debug > 1) printk("about to sleep\n");
491 /* wait for IIC transfer */
492 tmo = interruptible_sleep_on_timeout(&iic_wait,1*HZ);
493 local_irq_restore(flags);
495 #ifdef I2C_CHIP_ERRATA
496 /* Chip errata, clear enable. This is not needed on rev D4 CPUs.
497 Disabling I2C too early may cause too short stop condition */
499 i2c->i2c_i2mod &= ~1;
502 if (signal_pending(current) || !tmo){
504 if(cpm_debug && !tmo)
505 printk("IIC tryaddress: timeout!\n");
509 if (cpm_debug > 1) printk("back from sleep\n");
511 if (tbdf->cbd_sc & BD_SC_NAK) {
512 if (cpm_debug > 1) printk("IIC try; no ack\n");
516 if (tbdf->cbd_sc & BD_SC_READY) {
517 printk("IIC try; complete but tbuf ready\n");
523 static int cpm_xfer(struct i2c_adapter *i2c_adap,
524 struct i2c_msg msgs[],
527 struct i2c_algo_8xx_data *adap = i2c_adap->algo_data;
528 struct i2c_msg *pmsg;
532 for (i = 0; i < num; i++) {
536 printk("i2c-algo-8xx.o: "
537 "#%d addr=0x%x flags=0x%x len=%d\n buf=%lx\n",
538 i, pmsg->addr, pmsg->flags, pmsg->len, (unsigned long)pmsg->buf);
540 addr = pmsg->addr << 1;
541 if (pmsg->flags & I2C_M_RD )
543 if (pmsg->flags & I2C_M_REV_DIR_ADDR )
546 if (!(pmsg->flags & I2C_M_NOSTART)) {
548 if (pmsg->flags & I2C_M_RD ) {
549 /* read bytes into buffer*/
550 ret = cpm_iic_read(adap, addr, pmsg->buf, pmsg->len);
552 printk("i2c-algo-8xx.o: read %d bytes\n", ret);
553 if (ret < pmsg->len ) {
554 return (ret<0)? ret : -EREMOTEIO;
557 /* write bytes from buffer */
558 ret = cpm_iic_write(adap, addr, pmsg->buf, pmsg->len);
560 printk("i2c-algo-8xx.o: wrote %d\n", ret);
561 if (ret < pmsg->len ) {
562 return (ret<0) ? ret : -EREMOTEIO;
569 static int algo_control(struct i2c_adapter *adapter,
570 unsigned int cmd, unsigned long arg)
575 static u32 cpm_func(struct i2c_adapter *adap)
577 return I2C_FUNC_SMBUS_EMUL | I2C_FUNC_10BIT_ADDR |
578 I2C_FUNC_PROTOCOL_MANGLING;
581 /* -----exported algorithm data: ------------------------------------- */
583 static struct i2c_algorithm cpm_algo = {
584 "MPC8xx CPM algorithm",
588 NULL, /* slave_xmit */
589 NULL, /* slave_recv */
590 algo_control, /* ioctl */
591 cpm_func, /* functionality */
595 * registering functions to load algorithms at runtime
597 int i2c_8xx_add_bus(struct i2c_adapter *adap)
600 struct i2c_algo_8xx_data *cpm_adap = adap->algo_data;
603 printk("i2c-algo-8xx.o: hw routines for %s registered.\n",
606 /* register new adapter to i2c module... */
608 adap->id |= cpm_algo.id;
609 adap->algo = &cpm_algo;
615 i2c_add_adapter(adap);
616 cpm_iic_init(cpm_adap);
620 printk(KERN_INFO " i2c-algo-8xx.o: scanning bus %s...\n",
622 for (i = 0; i < 128; i++) {
623 if (cpm_iic_tryaddress(cpm_adap, i)) {
624 printk("(%02x)",i<<1);
633 int i2c_8xx_del_bus(struct i2c_adapter *adap)
636 struct i2c_algo_8xx_data *cpm_adap = adap->algo_data;
638 cpm_iic_shutdown(cpm_adap);
640 if ((res = i2c_del_adapter(adap)) < 0)
643 printk("i2c-algo-8xx.o: adapter unregistered: %s\n",adap->name);
651 EXPORT_SYMBOL(i2c_8xx_add_bus);
652 EXPORT_SYMBOL(i2c_8xx_del_bus);
654 int __init i2c_algo_8xx_init (void)
656 printk("i2c-algo-8xx.o: i2c mpc8xx algorithm module version %s (%s)\n", I2C_VERSION, I2C_DATE);
662 MODULE_AUTHOR("Brad Parker <brad@heeltoe.com>");
663 MODULE_DESCRIPTION("I2C-Bus MPC8XX algorithm");
664 #ifdef MODULE_LICENSE
665 MODULE_LICENSE("GPL");
668 int init_module(void)
670 return i2c_algo_8xx_init();
673 void cleanup_module(void)