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/interrupt.h>
37 #include <linux/errno.h>
38 #include <linux/stddef.h>
39 #include <linux/i2c.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/of_device.h>
43 #include <linux/of_platform.h>
44 #include <sysdev/fsl_soc.h>
47 /* Try to define this if you have an older CPU (earlier than rev D4) */
48 /* However, better use a GPIO based bitbang driver in this case :/ */
49 #undef I2C_CHIP_ERRATA
51 #define CPM_MAX_READ 513
54 #define I2C_EB (0x10) /* Big endian mode */
55 #define I2C_EB_CPM2 (0x30) /* Big endian mode, memory snoop */
57 #define DPRAM_BASE ((u8 __iomem __force *)cpm_muram_addr(0))
59 /* I2C parameter RAM. */
61 ushort rbase
; /* Rx Buffer descriptor base address */
62 ushort tbase
; /* Tx Buffer descriptor base address */
63 u_char rfcr
; /* Rx function code */
64 u_char tfcr
; /* Tx function code */
65 ushort mrblr
; /* Max receive buffer length */
66 uint rstate
; /* Internal */
67 uint rdp
; /* Internal */
68 ushort rbptr
; /* Rx Buffer descriptor pointer */
69 ushort rbc
; /* Internal */
70 uint rxtmp
; /* Internal */
71 uint tstate
; /* Internal */
72 uint tdp
; /* Internal */
73 ushort tbptr
; /* Tx Buffer descriptor pointer */
74 ushort tbc
; /* Internal */
75 uint txtmp
; /* Internal */
76 char res1
[4]; /* Reserved */
77 ushort rpbase
; /* Relocation pointer */
78 char res2
[2]; /* Reserved */
81 #define I2COM_START 0x80
82 #define I2COM_MASTER 0x01
83 #define I2CER_TXE 0x10
84 #define I2CER_BUSY 0x04
85 #define I2CER_TXB 0x02
86 #define I2CER_RXB 0x01
106 struct platform_device
*ofdev
;
107 struct i2c_adapter adap
;
109 int version
; /* CPM1=1, CPM2=2 */
113 struct i2c_reg __iomem
*i2c_reg
;
114 struct i2c_ram __iomem
*i2c_ram
;
116 wait_queue_head_t i2c_wait
;
117 cbd_t __iomem
*tbase
;
118 cbd_t __iomem
*rbase
;
119 u_char
*txbuf
[CPM_MAXBD
];
120 u_char
*rxbuf
[CPM_MAXBD
];
121 u32 txdma
[CPM_MAXBD
];
122 u32 rxdma
[CPM_MAXBD
];
125 static irqreturn_t
cpm_i2c_interrupt(int irq
, void *dev_id
)
128 struct i2c_reg __iomem
*i2c_reg
;
129 struct i2c_adapter
*adap
= dev_id
;
132 cpm
= i2c_get_adapdata(dev_id
);
133 i2c_reg
= cpm
->i2c_reg
;
135 /* Clear interrupt. */
136 i
= in_8(&i2c_reg
->i2cer
);
137 out_8(&i2c_reg
->i2cer
, i
);
139 dev_dbg(&adap
->dev
, "Interrupt: %x\n", i
);
141 wake_up(&cpm
->i2c_wait
);
143 return i
? IRQ_HANDLED
: IRQ_NONE
;
146 static void cpm_reset_i2c_params(struct cpm_i2c
*cpm
)
148 struct i2c_ram __iomem
*i2c_ram
= cpm
->i2c_ram
;
150 /* Set up the I2C parameters in the parameter ram. */
151 out_be16(&i2c_ram
->tbase
, (u8 __iomem
*)cpm
->tbase
- DPRAM_BASE
);
152 out_be16(&i2c_ram
->rbase
, (u8 __iomem
*)cpm
->rbase
- DPRAM_BASE
);
154 if (cpm
->version
== 1) {
155 out_8(&i2c_ram
->tfcr
, I2C_EB
);
156 out_8(&i2c_ram
->rfcr
, I2C_EB
);
158 out_8(&i2c_ram
->tfcr
, I2C_EB_CPM2
);
159 out_8(&i2c_ram
->rfcr
, I2C_EB_CPM2
);
162 out_be16(&i2c_ram
->mrblr
, CPM_MAX_READ
);
164 out_be32(&i2c_ram
->rstate
, 0);
165 out_be32(&i2c_ram
->rdp
, 0);
166 out_be16(&i2c_ram
->rbptr
, 0);
167 out_be16(&i2c_ram
->rbc
, 0);
168 out_be32(&i2c_ram
->rxtmp
, 0);
169 out_be32(&i2c_ram
->tstate
, 0);
170 out_be32(&i2c_ram
->tdp
, 0);
171 out_be16(&i2c_ram
->tbptr
, 0);
172 out_be16(&i2c_ram
->tbc
, 0);
173 out_be32(&i2c_ram
->txtmp
, 0);
176 static void cpm_i2c_force_close(struct i2c_adapter
*adap
)
178 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
179 struct i2c_reg __iomem
*i2c_reg
= cpm
->i2c_reg
;
181 dev_dbg(&adap
->dev
, "cpm_i2c_force_close()\n");
183 cpm_command(cpm
->cp_command
, CPM_CR_CLOSE_RX_BD
);
185 out_8(&i2c_reg
->i2cmr
, 0x00); /* Disable all interrupts */
186 out_8(&i2c_reg
->i2cer
, 0xff);
189 static void cpm_i2c_parse_message(struct i2c_adapter
*adap
,
190 struct i2c_msg
*pmsg
, int num
, int tx
, int rx
)
197 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
199 tbdf
= cpm
->tbase
+ tx
;
200 rbdf
= cpm
->rbase
+ rx
;
202 addr
= pmsg
->addr
<< 1;
203 if (pmsg
->flags
& I2C_M_RD
)
209 /* Align read buffer */
210 rb
= (u_char
*) (((ulong
) rb
+ 1) & ~1);
212 tb
[0] = addr
; /* Device address byte w/rw flag */
214 out_be16(&tbdf
->cbd_datlen
, pmsg
->len
+ 1);
215 out_be16(&tbdf
->cbd_sc
, 0);
217 if (!(pmsg
->flags
& I2C_M_NOSTART
))
218 setbits16(&tbdf
->cbd_sc
, BD_I2C_START
);
221 setbits16(&tbdf
->cbd_sc
, BD_SC_LAST
| BD_SC_WRAP
);
223 if (pmsg
->flags
& I2C_M_RD
) {
225 * To read, we need an empty buffer of the proper length.
226 * All that is used is the first byte for address, the remainder
227 * is just used for timing (and doesn't really have to exist).
230 dev_dbg(&adap
->dev
, "cpm_i2c_read(abyte=0x%x)\n", addr
);
232 out_be16(&rbdf
->cbd_datlen
, 0);
233 out_be16(&rbdf
->cbd_sc
, BD_SC_EMPTY
| BD_SC_INTRPT
);
235 if (rx
+ 1 == CPM_MAXBD
)
236 setbits16(&rbdf
->cbd_sc
, BD_SC_WRAP
);
239 setbits16(&tbdf
->cbd_sc
, BD_SC_READY
);
241 dev_dbg(&adap
->dev
, "cpm_i2c_write(abyte=0x%x)\n", addr
);
243 memcpy(tb
+1, pmsg
->buf
, pmsg
->len
);
246 setbits16(&tbdf
->cbd_sc
, BD_SC_READY
| BD_SC_INTRPT
);
250 static int cpm_i2c_check_message(struct i2c_adapter
*adap
,
251 struct i2c_msg
*pmsg
, int tx
, int rx
)
257 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
259 tbdf
= cpm
->tbase
+ tx
;
260 rbdf
= cpm
->rbase
+ rx
;
265 /* Align read buffer */
266 rb
= (u_char
*) (((uint
) rb
+ 1) & ~1);
269 if (pmsg
->flags
& I2C_M_RD
) {
270 dev_dbg(&adap
->dev
, "tx sc 0x%04x, rx sc 0x%04x\n",
271 in_be16(&tbdf
->cbd_sc
), in_be16(&rbdf
->cbd_sc
));
273 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_NAK
) {
274 dev_dbg(&adap
->dev
, "I2C read; No ack\n");
277 if (in_be16(&rbdf
->cbd_sc
) & BD_SC_EMPTY
) {
279 "I2C read; complete but rbuf empty\n");
282 if (in_be16(&rbdf
->cbd_sc
) & BD_SC_OV
) {
283 dev_err(&adap
->dev
, "I2C read; Overrun\n");
286 memcpy(pmsg
->buf
, rb
, pmsg
->len
);
288 dev_dbg(&adap
->dev
, "tx sc %d 0x%04x\n", tx
,
289 in_be16(&tbdf
->cbd_sc
));
291 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_NAK
) {
292 dev_dbg(&adap
->dev
, "I2C write; No ack\n");
295 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_UN
) {
296 dev_err(&adap
->dev
, "I2C write; Underrun\n");
299 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_CL
) {
300 dev_err(&adap
->dev
, "I2C write; Collision\n");
307 static int cpm_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
309 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
310 struct i2c_reg __iomem
*i2c_reg
= cpm
->i2c_reg
;
311 struct i2c_ram __iomem
*i2c_ram
= cpm
->i2c_ram
;
312 struct i2c_msg
*pmsg
;
322 /* Check if we have any oversized READ requests */
323 for (i
= 0; i
< num
; i
++) {
325 if (pmsg
->len
>= CPM_MAX_READ
)
329 /* Reset to use first buffer */
330 out_be16(&i2c_ram
->rbptr
, in_be16(&i2c_ram
->rbase
));
331 out_be16(&i2c_ram
->tbptr
, in_be16(&i2c_ram
->tbase
));
340 * If there was a collision in the last i2c transaction,
341 * Set I2COM_MASTER as it was cleared during collision.
343 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_CL
) {
344 out_8(&cpm
->i2c_reg
->i2com
, I2COM_MASTER
);
349 dev_dbg(&adap
->dev
, "R: %d T: %d\n", rptr
, tptr
);
351 cpm_i2c_parse_message(adap
, pmsg
, num
, tptr
, rptr
);
352 if (pmsg
->flags
& I2C_M_RD
)
356 /* Start transfer now */
357 /* Enable RX/TX/Error interupts */
358 out_8(&i2c_reg
->i2cmr
, I2CER_TXE
| I2CER_TXB
| I2CER_RXB
);
359 out_8(&i2c_reg
->i2cer
, 0xff); /* Clear interrupt status */
360 /* Chip bug, set enable here */
361 setbits8(&i2c_reg
->i2mod
, I2MOD_EN
); /* Enable */
362 /* Begin transmission */
363 setbits8(&i2c_reg
->i2com
, I2COM_START
);
369 /* Check for outstanding messages */
370 dev_dbg(&adap
->dev
, "test ready.\n");
372 if (pmsg
->flags
& I2C_M_RD
)
373 ret
= wait_event_timeout(cpm
->i2c_wait
,
374 (in_be16(&tbdf
[tptr
].cbd_sc
) & BD_SC_NAK
) ||
375 !(in_be16(&rbdf
[rptr
].cbd_sc
) & BD_SC_EMPTY
),
378 ret
= wait_event_timeout(cpm
->i2c_wait
,
379 !(in_be16(&tbdf
[tptr
].cbd_sc
) & BD_SC_READY
),
383 dev_err(&adap
->dev
, "I2C transfer: timeout\n");
387 dev_dbg(&adap
->dev
, "ready.\n");
388 ret
= cpm_i2c_check_message(adap
, pmsg
, tptr
, rptr
);
390 if (pmsg
->flags
& I2C_M_RD
)
396 #ifdef I2C_CHIP_ERRATA
398 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
399 * Disabling I2C too early may cause too short stop condition
402 clrbits8(&i2c_reg
->i2mod
, I2MOD_EN
);
407 cpm_i2c_force_close(adap
);
408 #ifdef I2C_CHIP_ERRATA
410 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
412 clrbits8(&i2c_reg
->i2mod
, I2MOD_EN
);
417 static u32
cpm_i2c_func(struct i2c_adapter
*adap
)
419 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
422 /* -----exported algorithm data: ------------------------------------- */
424 static const struct i2c_algorithm cpm_i2c_algo
= {
425 .master_xfer
= cpm_i2c_xfer
,
426 .functionality
= cpm_i2c_func
,
429 static const struct i2c_adapter cpm_ops
= {
430 .owner
= THIS_MODULE
,
432 .algo
= &cpm_i2c_algo
,
435 static int cpm_i2c_setup(struct cpm_i2c
*cpm
)
437 struct platform_device
*ofdev
= cpm
->ofdev
;
440 void __iomem
*i2c_base
;
445 dev_dbg(&cpm
->ofdev
->dev
, "cpm_i2c_setup()\n");
447 init_waitqueue_head(&cpm
->i2c_wait
);
449 cpm
->irq
= irq_of_parse_and_map(ofdev
->dev
.of_node
, 0);
453 /* Install interrupt handler. */
454 ret
= request_irq(cpm
->irq
, cpm_i2c_interrupt
, 0, "cpm_i2c",
459 /* I2C parameter RAM */
460 i2c_base
= of_iomap(ofdev
->dev
.of_node
, 1);
461 if (i2c_base
== NULL
) {
466 if (of_device_is_compatible(ofdev
->dev
.of_node
, "fsl,cpm1-i2c")) {
468 /* Check for and use a microcode relocation patch. */
469 cpm
->i2c_ram
= i2c_base
;
470 cpm
->i2c_addr
= in_be16(&cpm
->i2c_ram
->rpbase
);
473 * Maybe should use cpm_muram_alloc instead of hardcoding
474 * this in micropatch.c
477 cpm
->i2c_ram
= cpm_muram_addr(cpm
->i2c_addr
);
483 } else if (of_device_is_compatible(ofdev
->dev
.of_node
, "fsl,cpm2-i2c")) {
484 cpm
->i2c_addr
= cpm_muram_alloc(sizeof(struct i2c_ram
), 64);
485 cpm
->i2c_ram
= cpm_muram_addr(cpm
->i2c_addr
);
486 out_be16(i2c_base
, cpm
->i2c_addr
);
497 /* I2C control/status registers */
498 cpm
->i2c_reg
= of_iomap(ofdev
->dev
.of_node
, 0);
499 if (cpm
->i2c_reg
== NULL
) {
504 data
= of_get_property(ofdev
->dev
.of_node
, "fsl,cpm-command", &len
);
505 if (!data
|| len
!= 4) {
509 cpm
->cp_command
= *data
;
511 data
= of_get_property(ofdev
->dev
.of_node
, "linux,i2c-class", &len
);
512 if (data
&& len
== 4)
513 cpm
->adap
.class = *data
;
515 data
= of_get_property(ofdev
->dev
.of_node
, "clock-frequency", &len
);
516 if (data
&& len
== 4)
519 cpm
->freq
= 60000; /* use 60kHz i2c clock by default */
522 * Allocate space for CPM_MAXBD transmit and receive buffer
523 * descriptors in the DP ram.
525 cpm
->dp_addr
= cpm_muram_alloc(sizeof(cbd_t
) * 2 * CPM_MAXBD
, 8);
531 cpm
->tbase
= cpm_muram_addr(cpm
->dp_addr
);
532 cpm
->rbase
= cpm_muram_addr(cpm
->dp_addr
+ sizeof(cbd_t
) * CPM_MAXBD
);
534 /* Allocate TX and RX buffers */
539 for (i
= 0; i
< CPM_MAXBD
; i
++) {
540 cpm
->rxbuf
[i
] = dma_alloc_coherent(&cpm
->ofdev
->dev
,
542 &cpm
->rxdma
[i
], GFP_KERNEL
);
543 if (!cpm
->rxbuf
[i
]) {
547 out_be32(&rbdf
[i
].cbd_bufaddr
, ((cpm
->rxdma
[i
] + 1) & ~1));
549 cpm
->txbuf
[i
] = (unsigned char *)dma_alloc_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1, &cpm
->txdma
[i
], GFP_KERNEL
);
550 if (!cpm
->txbuf
[i
]) {
554 out_be32(&tbdf
[i
].cbd_bufaddr
, cpm
->txdma
[i
]);
557 /* Initialize Tx/Rx parameters. */
559 cpm_reset_i2c_params(cpm
);
561 dev_dbg(&cpm
->ofdev
->dev
, "i2c_ram 0x%p, i2c_addr 0x%04x, freq %d\n",
562 cpm
->i2c_ram
, cpm
->i2c_addr
, cpm
->freq
);
563 dev_dbg(&cpm
->ofdev
->dev
, "tbase 0x%04x, rbase 0x%04x\n",
564 (u8 __iomem
*)cpm
->tbase
- DPRAM_BASE
,
565 (u8 __iomem
*)cpm
->rbase
- DPRAM_BASE
);
567 cpm_command(cpm
->cp_command
, CPM_CR_INIT_TRX
);
570 * Select an invalid address. Just make sure we don't use loopback mode
572 out_8(&cpm
->i2c_reg
->i2add
, 0x7f << 1);
575 * PDIV is set to 00 in i2mod, so brgclk/32 is used as input to the
576 * i2c baud rate generator. This is divided by 2 x (DIV + 3) to get
577 * the actual i2c bus frequency.
579 brg
= get_brgfreq() / (32 * 2 * cpm
->freq
) - 3;
580 out_8(&cpm
->i2c_reg
->i2brg
, brg
);
582 out_8(&cpm
->i2c_reg
->i2mod
, 0x00);
583 out_8(&cpm
->i2c_reg
->i2com
, I2COM_MASTER
); /* Master mode */
585 /* Disable interrupts. */
586 out_8(&cpm
->i2c_reg
->i2cmr
, 0);
587 out_8(&cpm
->i2c_reg
->i2cer
, 0xff);
592 for (i
= 0; i
< CPM_MAXBD
; i
++) {
594 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
595 cpm
->rxbuf
[i
], cpm
->rxdma
[i
]);
597 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
598 cpm
->txbuf
[i
], cpm
->txdma
[i
]);
600 cpm_muram_free(cpm
->dp_addr
);
602 iounmap(cpm
->i2c_reg
);
604 if ((cpm
->version
== 1) && (!cpm
->i2c_addr
))
605 iounmap(cpm
->i2c_ram
);
606 if (cpm
->version
== 2)
607 cpm_muram_free(cpm
->i2c_addr
);
609 free_irq(cpm
->irq
, &cpm
->adap
);
613 static void cpm_i2c_shutdown(struct cpm_i2c
*cpm
)
618 clrbits8(&cpm
->i2c_reg
->i2mod
, I2MOD_EN
);
620 /* Disable interrupts */
621 out_8(&cpm
->i2c_reg
->i2cmr
, 0);
622 out_8(&cpm
->i2c_reg
->i2cer
, 0xff);
624 free_irq(cpm
->irq
, &cpm
->adap
);
626 /* Free all memory */
627 for (i
= 0; i
< CPM_MAXBD
; i
++) {
628 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
629 cpm
->rxbuf
[i
], cpm
->rxdma
[i
]);
630 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
631 cpm
->txbuf
[i
], cpm
->txdma
[i
]);
634 cpm_muram_free(cpm
->dp_addr
);
635 iounmap(cpm
->i2c_reg
);
637 if ((cpm
->version
== 1) && (!cpm
->i2c_addr
))
638 iounmap(cpm
->i2c_ram
);
639 if (cpm
->version
== 2)
640 cpm_muram_free(cpm
->i2c_addr
);
643 static int cpm_i2c_probe(struct platform_device
*ofdev
)
649 cpm
= kzalloc(sizeof(struct cpm_i2c
), GFP_KERNEL
);
655 platform_set_drvdata(ofdev
, cpm
);
658 i2c_set_adapdata(&cpm
->adap
, cpm
);
659 cpm
->adap
.dev
.parent
= &ofdev
->dev
;
660 cpm
->adap
.dev
.of_node
= of_node_get(ofdev
->dev
.of_node
);
662 result
= cpm_i2c_setup(cpm
);
664 dev_err(&ofdev
->dev
, "Unable to init hardware\n");
668 /* register new adapter to i2c module... */
670 data
= of_get_property(ofdev
->dev
.of_node
, "linux,i2c-index", &len
);
671 cpm
->adap
.nr
= (data
&& len
== 4) ? be32_to_cpup(data
) : -1;
672 result
= i2c_add_numbered_adapter(&cpm
->adap
);
675 dev_err(&ofdev
->dev
, "Unable to register with I2C\n");
679 dev_dbg(&ofdev
->dev
, "hw routines for %s registered.\n",
684 cpm_i2c_shutdown(cpm
);
691 static int cpm_i2c_remove(struct platform_device
*ofdev
)
693 struct cpm_i2c
*cpm
= platform_get_drvdata(ofdev
);
695 i2c_del_adapter(&cpm
->adap
);
697 cpm_i2c_shutdown(cpm
);
704 static const struct of_device_id cpm_i2c_match
[] = {
706 .compatible
= "fsl,cpm1-i2c",
709 .compatible
= "fsl,cpm2-i2c",
714 MODULE_DEVICE_TABLE(of
, cpm_i2c_match
);
716 static struct platform_driver cpm_i2c_driver
= {
717 .probe
= cpm_i2c_probe
,
718 .remove
= cpm_i2c_remove
,
720 .name
= "fsl-i2c-cpm",
721 .owner
= THIS_MODULE
,
722 .of_match_table
= cpm_i2c_match
,
726 module_platform_driver(cpm_i2c_driver
);
728 MODULE_AUTHOR("Jochen Friedrich <jochen@scram.de>");
729 MODULE_DESCRIPTION("I2C-Bus adapter routines for CPM boards");
730 MODULE_LICENSE("GPL");