2 * Freescale CPM1/CPM2 I2C interface.
3 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
5 * moved into proper i2c interface;
6 * Brad Parker (brad@heeltoe.com)
8 * Parts from dbox2_i2c.c (cvs.tuxbox.org)
9 * (C) 2000-2001 Felix Domke (tmbinc@gmx.net), Gillem (htoa@gmx.net)
11 * (C) 2007 Montavista Software, Inc.
12 * Vitaly Bordug <vitb@kernel.crashing.org>
14 * Converted to of_platform_device. Renamed to i2c-cpm.c.
15 * (C) 2007,2008 Jochen Friedrich <jochen@scram.de>
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/delay.h>
35 #include <linux/slab.h>
36 #include <linux/init.h>
37 #include <linux/interrupt.h>
38 #include <linux/errno.h>
39 #include <linux/stddef.h>
40 #include <linux/i2c.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/of_device.h>
44 #include <linux/of_platform.h>
45 #include <sysdev/fsl_soc.h>
48 /* Try to define this if you have an older CPU (earlier than rev D4) */
49 /* However, better use a GPIO based bitbang driver in this case :/ */
50 #undef I2C_CHIP_ERRATA
52 #define CPM_MAX_READ 513
55 #define I2C_EB (0x10) /* Big endian mode */
56 #define I2C_EB_CPM2 (0x30) /* Big endian mode, memory snoop */
58 #define DPRAM_BASE ((u8 __iomem __force *)cpm_muram_addr(0))
60 /* I2C parameter RAM. */
62 ushort rbase; /* Rx Buffer descriptor base address */
63 ushort tbase; /* Tx Buffer descriptor base address */
64 u_char rfcr; /* Rx function code */
65 u_char tfcr; /* Tx function code */
66 ushort mrblr; /* Max receive buffer length */
67 uint rstate; /* Internal */
68 uint rdp; /* Internal */
69 ushort rbptr; /* Rx Buffer descriptor pointer */
70 ushort rbc; /* Internal */
71 uint rxtmp; /* Internal */
72 uint tstate; /* Internal */
73 uint tdp; /* Internal */
74 ushort tbptr; /* Tx Buffer descriptor pointer */
75 ushort tbc; /* Internal */
76 uint txtmp; /* Internal */
77 char res1[4]; /* Reserved */
78 ushort rpbase; /* Relocation pointer */
79 char res2[2]; /* Reserved */
82 #define I2COM_START 0x80
83 #define I2COM_MASTER 0x01
84 #define I2CER_TXE 0x10
85 #define I2CER_BUSY 0x04
86 #define I2CER_TXB 0x02
87 #define I2CER_RXB 0x01
107 struct platform_device *ofdev;
108 struct i2c_adapter adap;
110 int version; /* CPM1=1, CPM2=2 */
114 struct i2c_reg __iomem *i2c_reg;
115 struct i2c_ram __iomem *i2c_ram;
117 wait_queue_head_t i2c_wait;
118 cbd_t __iomem *tbase;
119 cbd_t __iomem *rbase;
120 u_char *txbuf[CPM_MAXBD];
121 u_char *rxbuf[CPM_MAXBD];
122 u32 txdma[CPM_MAXBD];
123 u32 rxdma[CPM_MAXBD];
126 static irqreturn_t cpm_i2c_interrupt(int irq, void *dev_id)
129 struct i2c_reg __iomem *i2c_reg;
130 struct i2c_adapter *adap = dev_id;
133 cpm = i2c_get_adapdata(dev_id);
134 i2c_reg = cpm->i2c_reg;
136 /* Clear interrupt. */
137 i = in_8(&i2c_reg->i2cer);
138 out_8(&i2c_reg->i2cer, i);
140 dev_dbg(&adap->dev, "Interrupt: %x\n", i);
142 wake_up(&cpm->i2c_wait);
144 return i ? IRQ_HANDLED : IRQ_NONE;
147 static void cpm_reset_i2c_params(struct cpm_i2c *cpm)
149 struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
151 /* Set up the I2C parameters in the parameter ram. */
152 out_be16(&i2c_ram->tbase, (u8 __iomem *)cpm->tbase - DPRAM_BASE);
153 out_be16(&i2c_ram->rbase, (u8 __iomem *)cpm->rbase - DPRAM_BASE);
155 if (cpm->version == 1) {
156 out_8(&i2c_ram->tfcr, I2C_EB);
157 out_8(&i2c_ram->rfcr, I2C_EB);
159 out_8(&i2c_ram->tfcr, I2C_EB_CPM2);
160 out_8(&i2c_ram->rfcr, I2C_EB_CPM2);
163 out_be16(&i2c_ram->mrblr, CPM_MAX_READ);
165 out_be32(&i2c_ram->rstate, 0);
166 out_be32(&i2c_ram->rdp, 0);
167 out_be16(&i2c_ram->rbptr, 0);
168 out_be16(&i2c_ram->rbc, 0);
169 out_be32(&i2c_ram->rxtmp, 0);
170 out_be32(&i2c_ram->tstate, 0);
171 out_be32(&i2c_ram->tdp, 0);
172 out_be16(&i2c_ram->tbptr, 0);
173 out_be16(&i2c_ram->tbc, 0);
174 out_be32(&i2c_ram->txtmp, 0);
177 static void cpm_i2c_force_close(struct i2c_adapter *adap)
179 struct cpm_i2c *cpm = i2c_get_adapdata(adap);
180 struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
182 dev_dbg(&adap->dev, "cpm_i2c_force_close()\n");
184 cpm_command(cpm->cp_command, CPM_CR_CLOSE_RX_BD);
186 out_8(&i2c_reg->i2cmr, 0x00); /* Disable all interrupts */
187 out_8(&i2c_reg->i2cer, 0xff);
190 static void cpm_i2c_parse_message(struct i2c_adapter *adap,
191 struct i2c_msg *pmsg, int num, int tx, int rx)
198 struct cpm_i2c *cpm = i2c_get_adapdata(adap);
200 tbdf = cpm->tbase + tx;
201 rbdf = cpm->rbase + rx;
203 addr = pmsg->addr << 1;
204 if (pmsg->flags & I2C_M_RD)
210 /* Align read buffer */
211 rb = (u_char *) (((ulong) rb + 1) & ~1);
213 tb[0] = addr; /* Device address byte w/rw flag */
215 out_be16(&tbdf->cbd_datlen, pmsg->len + 1);
216 out_be16(&tbdf->cbd_sc, 0);
218 if (!(pmsg->flags & I2C_M_NOSTART))
219 setbits16(&tbdf->cbd_sc, BD_I2C_START);
222 setbits16(&tbdf->cbd_sc, BD_SC_LAST | BD_SC_WRAP);
224 if (pmsg->flags & I2C_M_RD) {
226 * To read, we need an empty buffer of the proper length.
227 * All that is used is the first byte for address, the remainder
228 * is just used for timing (and doesn't really have to exist).
231 dev_dbg(&adap->dev, "cpm_i2c_read(abyte=0x%x)\n", addr);
233 out_be16(&rbdf->cbd_datlen, 0);
234 out_be16(&rbdf->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT);
236 if (rx + 1 == CPM_MAXBD)
237 setbits16(&rbdf->cbd_sc, BD_SC_WRAP);
240 setbits16(&tbdf->cbd_sc, BD_SC_READY);
242 dev_dbg(&adap->dev, "cpm_i2c_write(abyte=0x%x)\n", addr);
244 memcpy(tb+1, pmsg->buf, pmsg->len);
247 setbits16(&tbdf->cbd_sc, BD_SC_READY | BD_SC_INTRPT);
251 static int cpm_i2c_check_message(struct i2c_adapter *adap,
252 struct i2c_msg *pmsg, int tx, int rx)
258 struct cpm_i2c *cpm = i2c_get_adapdata(adap);
260 tbdf = cpm->tbase + tx;
261 rbdf = cpm->rbase + rx;
266 /* Align read buffer */
267 rb = (u_char *) (((uint) rb + 1) & ~1);
270 if (pmsg->flags & I2C_M_RD) {
271 dev_dbg(&adap->dev, "tx sc 0x%04x, rx sc 0x%04x\n",
272 in_be16(&tbdf->cbd_sc), in_be16(&rbdf->cbd_sc));
274 if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
275 dev_dbg(&adap->dev, "I2C read; No ack\n");
278 if (in_be16(&rbdf->cbd_sc) & BD_SC_EMPTY) {
280 "I2C read; complete but rbuf empty\n");
283 if (in_be16(&rbdf->cbd_sc) & BD_SC_OV) {
284 dev_err(&adap->dev, "I2C read; Overrun\n");
287 memcpy(pmsg->buf, rb, pmsg->len);
289 dev_dbg(&adap->dev, "tx sc %d 0x%04x\n", tx,
290 in_be16(&tbdf->cbd_sc));
292 if (in_be16(&tbdf->cbd_sc) & BD_SC_NAK) {
293 dev_dbg(&adap->dev, "I2C write; No ack\n");
296 if (in_be16(&tbdf->cbd_sc) & BD_SC_UN) {
297 dev_err(&adap->dev, "I2C write; Underrun\n");
300 if (in_be16(&tbdf->cbd_sc) & BD_SC_CL) {
301 dev_err(&adap->dev, "I2C write; Collision\n");
308 static int cpm_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
310 struct cpm_i2c *cpm = i2c_get_adapdata(adap);
311 struct i2c_reg __iomem *i2c_reg = cpm->i2c_reg;
312 struct i2c_ram __iomem *i2c_ram = cpm->i2c_ram;
313 struct i2c_msg *pmsg;
323 /* Check if we have any oversized READ requests */
324 for (i = 0; i < num; i++) {
326 if (pmsg->len >= CPM_MAX_READ)
330 /* Reset to use first buffer */
331 out_be16(&i2c_ram->rbptr, in_be16(&i2c_ram->rbase));
332 out_be16(&i2c_ram->tbptr, in_be16(&i2c_ram->tbase));
341 * If there was a collision in the last i2c transaction,
342 * Set I2COM_MASTER as it was cleared during collision.
344 if (in_be16(&tbdf->cbd_sc) & BD_SC_CL) {
345 out_8(&cpm->i2c_reg->i2com, I2COM_MASTER);
350 dev_dbg(&adap->dev, "R: %d T: %d\n", rptr, tptr);
352 cpm_i2c_parse_message(adap, pmsg, num, tptr, rptr);
353 if (pmsg->flags & I2C_M_RD)
357 /* Start transfer now */
358 /* Enable RX/TX/Error interupts */
359 out_8(&i2c_reg->i2cmr, I2CER_TXE | I2CER_TXB | I2CER_RXB);
360 out_8(&i2c_reg->i2cer, 0xff); /* Clear interrupt status */
361 /* Chip bug, set enable here */
362 setbits8(&i2c_reg->i2mod, I2MOD_EN); /* Enable */
363 /* Begin transmission */
364 setbits8(&i2c_reg->i2com, I2COM_START);
370 /* Check for outstanding messages */
371 dev_dbg(&adap->dev, "test ready.\n");
373 if (pmsg->flags & I2C_M_RD)
374 ret = wait_event_timeout(cpm->i2c_wait,
375 (in_be16(&tbdf[tptr].cbd_sc) & BD_SC_NAK) ||
376 !(in_be16(&rbdf[rptr].cbd_sc) & BD_SC_EMPTY),
379 ret = wait_event_timeout(cpm->i2c_wait,
380 !(in_be16(&tbdf[tptr].cbd_sc) & BD_SC_READY),
384 dev_err(&adap->dev, "I2C transfer: timeout\n");
388 dev_dbg(&adap->dev, "ready.\n");
389 ret = cpm_i2c_check_message(adap, pmsg, tptr, rptr);
391 if (pmsg->flags & I2C_M_RD)
397 #ifdef I2C_CHIP_ERRATA
399 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
400 * Disabling I2C too early may cause too short stop condition
403 clrbits8(&i2c_reg->i2mod, I2MOD_EN);
408 cpm_i2c_force_close(adap);
409 #ifdef I2C_CHIP_ERRATA
411 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
413 clrbits8(&i2c_reg->i2mod, I2MOD_EN);
418 static u32 cpm_i2c_func(struct i2c_adapter *adap)
420 return I2C_FUNC_I2C | (I2C_FUNC_SMBUS_EMUL & ~I2C_FUNC_SMBUS_QUICK);
423 /* -----exported algorithm data: ------------------------------------- */
425 static const struct i2c_algorithm cpm_i2c_algo = {
426 .master_xfer = cpm_i2c_xfer,
427 .functionality = cpm_i2c_func,
430 static const struct i2c_adapter cpm_ops = {
431 .owner = THIS_MODULE,
433 .algo = &cpm_i2c_algo,
436 static int cpm_i2c_setup(struct cpm_i2c *cpm)
438 struct platform_device *ofdev = cpm->ofdev;
441 void __iomem *i2c_base;
446 dev_dbg(&cpm->ofdev->dev, "cpm_i2c_setup()\n");
448 init_waitqueue_head(&cpm->i2c_wait);
450 cpm->irq = of_irq_to_resource(ofdev->dev.of_node, 0, NULL);
454 /* Install interrupt handler. */
455 ret = request_irq(cpm->irq, cpm_i2c_interrupt, 0, "cpm_i2c",
460 /* I2C parameter RAM */
461 i2c_base = of_iomap(ofdev->dev.of_node, 1);
462 if (i2c_base == NULL) {
467 if (of_device_is_compatible(ofdev->dev.of_node, "fsl,cpm1-i2c")) {
469 /* Check for and use a microcode relocation patch. */
470 cpm->i2c_ram = i2c_base;
471 cpm->i2c_addr = in_be16(&cpm->i2c_ram->rpbase);
474 * Maybe should use cpm_muram_alloc instead of hardcoding
475 * this in micropatch.c
478 cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
484 } else if (of_device_is_compatible(ofdev->dev.of_node, "fsl,cpm2-i2c")) {
485 cpm->i2c_addr = cpm_muram_alloc(sizeof(struct i2c_ram), 64);
486 cpm->i2c_ram = cpm_muram_addr(cpm->i2c_addr);
487 out_be16(i2c_base, cpm->i2c_addr);
498 /* I2C control/status registers */
499 cpm->i2c_reg = of_iomap(ofdev->dev.of_node, 0);
500 if (cpm->i2c_reg == NULL) {
505 data = of_get_property(ofdev->dev.of_node, "fsl,cpm-command", &len);
506 if (!data || len != 4) {
510 cpm->cp_command = *data;
512 data = of_get_property(ofdev->dev.of_node, "linux,i2c-class", &len);
513 if (data && len == 4)
514 cpm->adap.class = *data;
516 data = of_get_property(ofdev->dev.of_node, "clock-frequency", &len);
517 if (data && len == 4)
520 cpm->freq = 60000; /* use 60kHz i2c clock by default */
523 * Allocate space for CPM_MAXBD transmit and receive buffer
524 * descriptors in the DP ram.
526 cpm->dp_addr = cpm_muram_alloc(sizeof(cbd_t) * 2 * CPM_MAXBD, 8);
532 cpm->tbase = cpm_muram_addr(cpm->dp_addr);
533 cpm->rbase = cpm_muram_addr(cpm->dp_addr + sizeof(cbd_t) * CPM_MAXBD);
535 /* Allocate TX and RX buffers */
540 for (i = 0; i < CPM_MAXBD; i++) {
541 cpm->rxbuf[i] = dma_alloc_coherent(&cpm->ofdev->dev,
543 &cpm->rxdma[i], GFP_KERNEL);
544 if (!cpm->rxbuf[i]) {
548 out_be32(&rbdf[i].cbd_bufaddr, ((cpm->rxdma[i] + 1) & ~1));
550 cpm->txbuf[i] = (unsigned char *)dma_alloc_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1, &cpm->txdma[i], GFP_KERNEL);
551 if (!cpm->txbuf[i]) {
555 out_be32(&tbdf[i].cbd_bufaddr, cpm->txdma[i]);
558 /* Initialize Tx/Rx parameters. */
560 cpm_reset_i2c_params(cpm);
562 dev_dbg(&cpm->ofdev->dev, "i2c_ram 0x%p, i2c_addr 0x%04x, freq %d\n",
563 cpm->i2c_ram, cpm->i2c_addr, cpm->freq);
564 dev_dbg(&cpm->ofdev->dev, "tbase 0x%04x, rbase 0x%04x\n",
565 (u8 __iomem *)cpm->tbase - DPRAM_BASE,
566 (u8 __iomem *)cpm->rbase - DPRAM_BASE);
568 cpm_command(cpm->cp_command, CPM_CR_INIT_TRX);
571 * Select an invalid address. Just make sure we don't use loopback mode
573 out_8(&cpm->i2c_reg->i2add, 0x7f << 1);
576 * PDIV is set to 00 in i2mod, so brgclk/32 is used as input to the
577 * i2c baud rate generator. This is divided by 2 x (DIV + 3) to get
578 * the actual i2c bus frequency.
580 brg = get_brgfreq() / (32 * 2 * cpm->freq) - 3;
581 out_8(&cpm->i2c_reg->i2brg, brg);
583 out_8(&cpm->i2c_reg->i2mod, 0x00);
584 out_8(&cpm->i2c_reg->i2com, I2COM_MASTER); /* Master mode */
586 /* Disable interrupts. */
587 out_8(&cpm->i2c_reg->i2cmr, 0);
588 out_8(&cpm->i2c_reg->i2cer, 0xff);
593 for (i = 0; i < CPM_MAXBD; i++) {
595 dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
596 cpm->rxbuf[i], cpm->rxdma[i]);
598 dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
599 cpm->txbuf[i], cpm->txdma[i]);
601 cpm_muram_free(cpm->dp_addr);
603 iounmap(cpm->i2c_reg);
605 if ((cpm->version == 1) && (!cpm->i2c_addr))
606 iounmap(cpm->i2c_ram);
607 if (cpm->version == 2)
608 cpm_muram_free(cpm->i2c_addr);
610 free_irq(cpm->irq, &cpm->adap);
614 static void cpm_i2c_shutdown(struct cpm_i2c *cpm)
619 clrbits8(&cpm->i2c_reg->i2mod, I2MOD_EN);
621 /* Disable interrupts */
622 out_8(&cpm->i2c_reg->i2cmr, 0);
623 out_8(&cpm->i2c_reg->i2cer, 0xff);
625 free_irq(cpm->irq, &cpm->adap);
627 /* Free all memory */
628 for (i = 0; i < CPM_MAXBD; i++) {
629 dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
630 cpm->rxbuf[i], cpm->rxdma[i]);
631 dma_free_coherent(&cpm->ofdev->dev, CPM_MAX_READ + 1,
632 cpm->txbuf[i], cpm->txdma[i]);
635 cpm_muram_free(cpm->dp_addr);
636 iounmap(cpm->i2c_reg);
638 if ((cpm->version == 1) && (!cpm->i2c_addr))
639 iounmap(cpm->i2c_ram);
640 if (cpm->version == 2)
641 cpm_muram_free(cpm->i2c_addr);
644 static int cpm_i2c_probe(struct platform_device *ofdev)
650 cpm = kzalloc(sizeof(struct cpm_i2c), GFP_KERNEL);
656 platform_set_drvdata(ofdev, cpm);
659 i2c_set_adapdata(&cpm->adap, cpm);
660 cpm->adap.dev.parent = &ofdev->dev;
661 cpm->adap.dev.of_node = of_node_get(ofdev->dev.of_node);
663 result = cpm_i2c_setup(cpm);
665 dev_err(&ofdev->dev, "Unable to init hardware\n");
669 /* register new adapter to i2c module... */
671 data = of_get_property(ofdev->dev.of_node, "linux,i2c-index", &len);
672 cpm->adap.nr = (data && len == 4) ? be32_to_cpup(data) : -1;
673 result = i2c_add_numbered_adapter(&cpm->adap);
676 dev_err(&ofdev->dev, "Unable to register with I2C\n");
680 dev_dbg(&ofdev->dev, "hw routines for %s registered.\n",
685 cpm_i2c_shutdown(cpm);
692 static int cpm_i2c_remove(struct platform_device *ofdev)
694 struct cpm_i2c *cpm = platform_get_drvdata(ofdev);
696 i2c_del_adapter(&cpm->adap);
698 cpm_i2c_shutdown(cpm);
705 static const struct of_device_id cpm_i2c_match[] = {
707 .compatible = "fsl,cpm1-i2c",
710 .compatible = "fsl,cpm2-i2c",
715 MODULE_DEVICE_TABLE(of, cpm_i2c_match);
717 static struct platform_driver cpm_i2c_driver = {
718 .probe = cpm_i2c_probe,
719 .remove = cpm_i2c_remove,
721 .name = "fsl-i2c-cpm",
722 .owner = THIS_MODULE,
723 .of_match_table = cpm_i2c_match,
727 module_platform_driver(cpm_i2c_driver);
729 MODULE_AUTHOR("Jochen Friedrich <jochen@scram.de>");
730 MODULE_DESCRIPTION("I2C-Bus adapter routines for CPM boards");
731 MODULE_LICENSE("GPL");