1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale CPM1/CPM2 I2C interface.
4 * Copyright (c) 1999 Dan Malek (dmalek@jlc.net).
6 * moved into proper i2c interface;
7 * Brad Parker (brad@heeltoe.com)
9 * Parts from dbox2_i2c.c (cvs.tuxbox.org)
10 * (C) 2000-2001 Felix Domke (tmbinc@gmx.net), Gillem (htoa@gmx.net)
12 * (C) 2007 Montavista Software, Inc.
13 * Vitaly Bordug <vitb@kernel.crashing.org>
15 * Converted to of_platform_device. Renamed to i2c-cpm.c.
16 * (C) 2007,2008 Jochen Friedrich <jochen@scram.de>
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/delay.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/errno.h>
25 #include <linux/stddef.h>
26 #include <linux/i2c.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/of_address.h>
30 #include <linux/of_device.h>
31 #include <linux/of_irq.h>
32 #include <linux/of_platform.h>
33 #include <sysdev/fsl_soc.h>
36 /* Try to define this if you have an older CPU (earlier than rev D4) */
37 /* However, better use a GPIO based bitbang driver in this case :/ */
38 #undef I2C_CHIP_ERRATA
40 #define CPM_MAX_READ 513
43 #define I2C_EB (0x10) /* Big endian mode */
44 #define I2C_EB_CPM2 (0x30) /* Big endian mode, memory snoop */
46 #define DPRAM_BASE ((u8 __iomem __force *)cpm_muram_addr(0))
48 /* I2C parameter RAM. */
50 ushort rbase
; /* Rx Buffer descriptor base address */
51 ushort tbase
; /* Tx Buffer descriptor base address */
52 u_char rfcr
; /* Rx function code */
53 u_char tfcr
; /* Tx function code */
54 ushort mrblr
; /* Max receive buffer length */
55 uint rstate
; /* Internal */
56 uint rdp
; /* Internal */
57 ushort rbptr
; /* Rx Buffer descriptor pointer */
58 ushort rbc
; /* Internal */
59 uint rxtmp
; /* Internal */
60 uint tstate
; /* Internal */
61 uint tdp
; /* Internal */
62 ushort tbptr
; /* Tx Buffer descriptor pointer */
63 ushort tbc
; /* Internal */
64 uint txtmp
; /* Internal */
65 char res1
[4]; /* Reserved */
66 ushort rpbase
; /* Relocation pointer */
67 char res2
[2]; /* Reserved */
68 /* The following elements are only for CPM2 */
69 char res3
[4]; /* Reserved */
70 uint sdmatmp
; /* Internal */
73 #define I2COM_START 0x80
74 #define I2COM_MASTER 0x01
75 #define I2CER_TXE 0x10
76 #define I2CER_BUSY 0x04
77 #define I2CER_TXB 0x02
78 #define I2CER_RXB 0x01
98 struct platform_device
*ofdev
;
99 struct i2c_adapter adap
;
101 int version
; /* CPM1=1, CPM2=2 */
105 struct i2c_reg __iomem
*i2c_reg
;
106 struct i2c_ram __iomem
*i2c_ram
;
108 wait_queue_head_t i2c_wait
;
109 cbd_t __iomem
*tbase
;
110 cbd_t __iomem
*rbase
;
111 u_char
*txbuf
[CPM_MAXBD
];
112 u_char
*rxbuf
[CPM_MAXBD
];
113 dma_addr_t txdma
[CPM_MAXBD
];
114 dma_addr_t rxdma
[CPM_MAXBD
];
117 static irqreturn_t
cpm_i2c_interrupt(int irq
, void *dev_id
)
120 struct i2c_reg __iomem
*i2c_reg
;
121 struct i2c_adapter
*adap
= dev_id
;
124 cpm
= i2c_get_adapdata(dev_id
);
125 i2c_reg
= cpm
->i2c_reg
;
127 /* Clear interrupt. */
128 i
= in_8(&i2c_reg
->i2cer
);
129 out_8(&i2c_reg
->i2cer
, i
);
131 dev_dbg(&adap
->dev
, "Interrupt: %x\n", i
);
133 wake_up(&cpm
->i2c_wait
);
135 return i
? IRQ_HANDLED
: IRQ_NONE
;
138 static void cpm_reset_i2c_params(struct cpm_i2c
*cpm
)
140 struct i2c_ram __iomem
*i2c_ram
= cpm
->i2c_ram
;
142 /* Set up the I2C parameters in the parameter ram. */
143 out_be16(&i2c_ram
->tbase
, (u8 __iomem
*)cpm
->tbase
- DPRAM_BASE
);
144 out_be16(&i2c_ram
->rbase
, (u8 __iomem
*)cpm
->rbase
- DPRAM_BASE
);
146 if (cpm
->version
== 1) {
147 out_8(&i2c_ram
->tfcr
, I2C_EB
);
148 out_8(&i2c_ram
->rfcr
, I2C_EB
);
150 out_8(&i2c_ram
->tfcr
, I2C_EB_CPM2
);
151 out_8(&i2c_ram
->rfcr
, I2C_EB_CPM2
);
154 out_be16(&i2c_ram
->mrblr
, CPM_MAX_READ
);
156 out_be32(&i2c_ram
->rstate
, 0);
157 out_be32(&i2c_ram
->rdp
, 0);
158 out_be16(&i2c_ram
->rbptr
, 0);
159 out_be16(&i2c_ram
->rbc
, 0);
160 out_be32(&i2c_ram
->rxtmp
, 0);
161 out_be32(&i2c_ram
->tstate
, 0);
162 out_be32(&i2c_ram
->tdp
, 0);
163 out_be16(&i2c_ram
->tbptr
, 0);
164 out_be16(&i2c_ram
->tbc
, 0);
165 out_be32(&i2c_ram
->txtmp
, 0);
168 static void cpm_i2c_force_close(struct i2c_adapter
*adap
)
170 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
171 struct i2c_reg __iomem
*i2c_reg
= cpm
->i2c_reg
;
173 dev_dbg(&adap
->dev
, "cpm_i2c_force_close()\n");
175 cpm_command(cpm
->cp_command
, CPM_CR_CLOSE_RX_BD
);
177 out_8(&i2c_reg
->i2cmr
, 0x00); /* Disable all interrupts */
178 out_8(&i2c_reg
->i2cer
, 0xff);
181 static void cpm_i2c_parse_message(struct i2c_adapter
*adap
,
182 struct i2c_msg
*pmsg
, int num
, int tx
, int rx
)
189 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
191 tbdf
= cpm
->tbase
+ tx
;
192 rbdf
= cpm
->rbase
+ rx
;
194 addr
= i2c_8bit_addr_from_msg(pmsg
);
199 /* Align read buffer */
200 rb
= (u_char
*) (((ulong
) rb
+ 1) & ~1);
202 tb
[0] = addr
; /* Device address byte w/rw flag */
204 out_be16(&tbdf
->cbd_datlen
, pmsg
->len
+ 1);
205 out_be16(&tbdf
->cbd_sc
, 0);
207 if (!(pmsg
->flags
& I2C_M_NOSTART
))
208 setbits16(&tbdf
->cbd_sc
, BD_I2C_START
);
211 setbits16(&tbdf
->cbd_sc
, BD_SC_LAST
| BD_SC_WRAP
);
213 if (pmsg
->flags
& I2C_M_RD
) {
215 * To read, we need an empty buffer of the proper length.
216 * All that is used is the first byte for address, the remainder
217 * is just used for timing (and doesn't really have to exist).
220 dev_dbg(&adap
->dev
, "cpm_i2c_read(abyte=0x%x)\n", addr
);
222 out_be16(&rbdf
->cbd_datlen
, 0);
223 out_be16(&rbdf
->cbd_sc
, BD_SC_EMPTY
| BD_SC_INTRPT
);
225 if (rx
+ 1 == CPM_MAXBD
)
226 setbits16(&rbdf
->cbd_sc
, BD_SC_WRAP
);
229 setbits16(&tbdf
->cbd_sc
, BD_SC_READY
);
231 dev_dbg(&adap
->dev
, "cpm_i2c_write(abyte=0x%x)\n", addr
);
233 memcpy(tb
+1, pmsg
->buf
, pmsg
->len
);
236 setbits16(&tbdf
->cbd_sc
, BD_SC_READY
| BD_SC_INTRPT
);
240 static int cpm_i2c_check_message(struct i2c_adapter
*adap
,
241 struct i2c_msg
*pmsg
, int tx
, int rx
)
247 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
249 tbdf
= cpm
->tbase
+ tx
;
250 rbdf
= cpm
->rbase
+ rx
;
255 /* Align read buffer */
256 rb
= (u_char
*) (((uint
) rb
+ 1) & ~1);
259 if (pmsg
->flags
& I2C_M_RD
) {
260 dev_dbg(&adap
->dev
, "tx sc 0x%04x, rx sc 0x%04x\n",
261 in_be16(&tbdf
->cbd_sc
), in_be16(&rbdf
->cbd_sc
));
263 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_NAK
) {
264 dev_dbg(&adap
->dev
, "I2C read; No ack\n");
267 if (in_be16(&rbdf
->cbd_sc
) & BD_SC_EMPTY
) {
269 "I2C read; complete but rbuf empty\n");
272 if (in_be16(&rbdf
->cbd_sc
) & BD_SC_OV
) {
273 dev_err(&adap
->dev
, "I2C read; Overrun\n");
276 memcpy(pmsg
->buf
, rb
, pmsg
->len
);
278 dev_dbg(&adap
->dev
, "tx sc %d 0x%04x\n", tx
,
279 in_be16(&tbdf
->cbd_sc
));
281 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_NAK
) {
282 dev_dbg(&adap
->dev
, "I2C write; No ack\n");
285 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_UN
) {
286 dev_err(&adap
->dev
, "I2C write; Underrun\n");
289 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_CL
) {
290 dev_err(&adap
->dev
, "I2C write; Collision\n");
297 static int cpm_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
299 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
300 struct i2c_reg __iomem
*i2c_reg
= cpm
->i2c_reg
;
301 struct i2c_ram __iomem
*i2c_ram
= cpm
->i2c_ram
;
302 struct i2c_msg
*pmsg
;
309 /* Reset to use first buffer */
310 out_be16(&i2c_ram
->rbptr
, in_be16(&i2c_ram
->rbase
));
311 out_be16(&i2c_ram
->tbptr
, in_be16(&i2c_ram
->tbase
));
320 * If there was a collision in the last i2c transaction,
321 * Set I2COM_MASTER as it was cleared during collision.
323 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_CL
) {
324 out_8(&cpm
->i2c_reg
->i2com
, I2COM_MASTER
);
329 dev_dbg(&adap
->dev
, "R: %d T: %d\n", rptr
, tptr
);
331 cpm_i2c_parse_message(adap
, pmsg
, num
, tptr
, rptr
);
332 if (pmsg
->flags
& I2C_M_RD
)
336 /* Start transfer now */
337 /* Enable RX/TX/Error interupts */
338 out_8(&i2c_reg
->i2cmr
, I2CER_TXE
| I2CER_TXB
| I2CER_RXB
);
339 out_8(&i2c_reg
->i2cer
, 0xff); /* Clear interrupt status */
340 /* Chip bug, set enable here */
341 setbits8(&i2c_reg
->i2mod
, I2MOD_EN
); /* Enable */
342 /* Begin transmission */
343 setbits8(&i2c_reg
->i2com
, I2COM_START
);
349 /* Check for outstanding messages */
350 dev_dbg(&adap
->dev
, "test ready.\n");
352 if (pmsg
->flags
& I2C_M_RD
)
353 ret
= wait_event_timeout(cpm
->i2c_wait
,
354 (in_be16(&tbdf
[tptr
].cbd_sc
) & BD_SC_NAK
) ||
355 !(in_be16(&rbdf
[rptr
].cbd_sc
) & BD_SC_EMPTY
),
358 ret
= wait_event_timeout(cpm
->i2c_wait
,
359 !(in_be16(&tbdf
[tptr
].cbd_sc
) & BD_SC_READY
),
363 dev_err(&adap
->dev
, "I2C transfer: timeout\n");
367 dev_dbg(&adap
->dev
, "ready.\n");
368 ret
= cpm_i2c_check_message(adap
, pmsg
, tptr
, rptr
);
370 if (pmsg
->flags
& I2C_M_RD
)
376 #ifdef I2C_CHIP_ERRATA
378 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
379 * Disabling I2C too early may cause too short stop condition
382 clrbits8(&i2c_reg
->i2mod
, I2MOD_EN
);
387 cpm_i2c_force_close(adap
);
388 #ifdef I2C_CHIP_ERRATA
390 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
392 clrbits8(&i2c_reg
->i2mod
, I2MOD_EN
);
397 static u32
cpm_i2c_func(struct i2c_adapter
*adap
)
399 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
402 /* -----exported algorithm data: ------------------------------------- */
404 static const struct i2c_algorithm cpm_i2c_algo
= {
405 .master_xfer
= cpm_i2c_xfer
,
406 .functionality
= cpm_i2c_func
,
409 /* CPM_MAX_READ is also limiting writes according to the code! */
410 static const struct i2c_adapter_quirks cpm_i2c_quirks
= {
411 .max_num_msgs
= CPM_MAXBD
,
412 .max_read_len
= CPM_MAX_READ
,
413 .max_write_len
= CPM_MAX_READ
,
416 static const struct i2c_adapter cpm_ops
= {
417 .owner
= THIS_MODULE
,
419 .algo
= &cpm_i2c_algo
,
420 .quirks
= &cpm_i2c_quirks
,
423 static int cpm_i2c_setup(struct cpm_i2c
*cpm
)
425 struct platform_device
*ofdev
= cpm
->ofdev
;
428 void __iomem
*i2c_base
;
433 dev_dbg(&cpm
->ofdev
->dev
, "cpm_i2c_setup()\n");
435 init_waitqueue_head(&cpm
->i2c_wait
);
437 cpm
->irq
= irq_of_parse_and_map(ofdev
->dev
.of_node
, 0);
441 /* Install interrupt handler. */
442 ret
= request_irq(cpm
->irq
, cpm_i2c_interrupt
, 0, "cpm_i2c",
447 /* I2C parameter RAM */
448 i2c_base
= of_iomap(ofdev
->dev
.of_node
, 1);
449 if (i2c_base
== NULL
) {
454 if (of_device_is_compatible(ofdev
->dev
.of_node
, "fsl,cpm1-i2c")) {
456 /* Check for and use a microcode relocation patch. */
457 cpm
->i2c_ram
= i2c_base
;
458 cpm
->i2c_addr
= in_be16(&cpm
->i2c_ram
->rpbase
);
461 * Maybe should use cpm_muram_alloc instead of hardcoding
462 * this in micropatch.c
465 cpm
->i2c_ram
= cpm_muram_addr(cpm
->i2c_addr
);
471 } else if (of_device_is_compatible(ofdev
->dev
.of_node
, "fsl,cpm2-i2c")) {
472 cpm
->i2c_addr
= cpm_muram_alloc(sizeof(struct i2c_ram
), 64);
473 cpm
->i2c_ram
= cpm_muram_addr(cpm
->i2c_addr
);
474 out_be16(i2c_base
, cpm
->i2c_addr
);
485 /* I2C control/status registers */
486 cpm
->i2c_reg
= of_iomap(ofdev
->dev
.of_node
, 0);
487 if (cpm
->i2c_reg
== NULL
) {
492 data
= of_get_property(ofdev
->dev
.of_node
, "fsl,cpm-command", &len
);
493 if (!data
|| len
!= 4) {
497 cpm
->cp_command
= *data
;
499 data
= of_get_property(ofdev
->dev
.of_node
, "linux,i2c-class", &len
);
500 if (data
&& len
== 4)
501 cpm
->adap
.class = *data
;
503 data
= of_get_property(ofdev
->dev
.of_node
, "clock-frequency", &len
);
504 if (data
&& len
== 4)
507 cpm
->freq
= 60000; /* use 60kHz i2c clock by default */
510 * Allocate space for CPM_MAXBD transmit and receive buffer
511 * descriptors in the DP ram.
513 cpm
->dp_addr
= cpm_muram_alloc(sizeof(cbd_t
) * 2 * CPM_MAXBD
, 8);
519 cpm
->tbase
= cpm_muram_addr(cpm
->dp_addr
);
520 cpm
->rbase
= cpm_muram_addr(cpm
->dp_addr
+ sizeof(cbd_t
) * CPM_MAXBD
);
522 /* Allocate TX and RX buffers */
527 for (i
= 0; i
< CPM_MAXBD
; i
++) {
528 cpm
->rxbuf
[i
] = dma_alloc_coherent(&cpm
->ofdev
->dev
,
530 &cpm
->rxdma
[i
], GFP_KERNEL
);
531 if (!cpm
->rxbuf
[i
]) {
535 out_be32(&rbdf
[i
].cbd_bufaddr
, ((cpm
->rxdma
[i
] + 1) & ~1));
537 cpm
->txbuf
[i
] = dma_alloc_coherent(&cpm
->ofdev
->dev
,
539 &cpm
->txdma
[i
], GFP_KERNEL
);
540 if (!cpm
->txbuf
[i
]) {
544 out_be32(&tbdf
[i
].cbd_bufaddr
, cpm
->txdma
[i
]);
547 /* Initialize Tx/Rx parameters. */
549 cpm_reset_i2c_params(cpm
);
551 dev_dbg(&cpm
->ofdev
->dev
, "i2c_ram 0x%p, i2c_addr 0x%04x, freq %d\n",
552 cpm
->i2c_ram
, cpm
->i2c_addr
, cpm
->freq
);
553 dev_dbg(&cpm
->ofdev
->dev
, "tbase 0x%04x, rbase 0x%04x\n",
554 (u8 __iomem
*)cpm
->tbase
- DPRAM_BASE
,
555 (u8 __iomem
*)cpm
->rbase
- DPRAM_BASE
);
557 cpm_command(cpm
->cp_command
, CPM_CR_INIT_TRX
);
560 * Select an invalid address. Just make sure we don't use loopback mode
562 out_8(&cpm
->i2c_reg
->i2add
, 0x7f << 1);
565 * PDIV is set to 00 in i2mod, so brgclk/32 is used as input to the
566 * i2c baud rate generator. This is divided by 2 x (DIV + 3) to get
567 * the actual i2c bus frequency.
569 brg
= get_brgfreq() / (32 * 2 * cpm
->freq
) - 3;
570 out_8(&cpm
->i2c_reg
->i2brg
, brg
);
572 out_8(&cpm
->i2c_reg
->i2mod
, 0x00);
573 out_8(&cpm
->i2c_reg
->i2com
, I2COM_MASTER
); /* Master mode */
575 /* Disable interrupts. */
576 out_8(&cpm
->i2c_reg
->i2cmr
, 0);
577 out_8(&cpm
->i2c_reg
->i2cer
, 0xff);
582 for (i
= 0; i
< CPM_MAXBD
; i
++) {
584 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
585 cpm
->rxbuf
[i
], cpm
->rxdma
[i
]);
587 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
588 cpm
->txbuf
[i
], cpm
->txdma
[i
]);
590 cpm_muram_free(cpm
->dp_addr
);
592 iounmap(cpm
->i2c_reg
);
594 if ((cpm
->version
== 1) && (!cpm
->i2c_addr
))
595 iounmap(cpm
->i2c_ram
);
596 if (cpm
->version
== 2)
597 cpm_muram_free(cpm
->i2c_addr
);
599 free_irq(cpm
->irq
, &cpm
->adap
);
603 static void cpm_i2c_shutdown(struct cpm_i2c
*cpm
)
608 clrbits8(&cpm
->i2c_reg
->i2mod
, I2MOD_EN
);
610 /* Disable interrupts */
611 out_8(&cpm
->i2c_reg
->i2cmr
, 0);
612 out_8(&cpm
->i2c_reg
->i2cer
, 0xff);
614 free_irq(cpm
->irq
, &cpm
->adap
);
616 /* Free all memory */
617 for (i
= 0; i
< CPM_MAXBD
; i
++) {
618 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
619 cpm
->rxbuf
[i
], cpm
->rxdma
[i
]);
620 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
621 cpm
->txbuf
[i
], cpm
->txdma
[i
]);
624 cpm_muram_free(cpm
->dp_addr
);
625 iounmap(cpm
->i2c_reg
);
627 if ((cpm
->version
== 1) && (!cpm
->i2c_addr
))
628 iounmap(cpm
->i2c_ram
);
629 if (cpm
->version
== 2)
630 cpm_muram_free(cpm
->i2c_addr
);
633 static int cpm_i2c_probe(struct platform_device
*ofdev
)
639 cpm
= kzalloc(sizeof(struct cpm_i2c
), GFP_KERNEL
);
645 platform_set_drvdata(ofdev
, cpm
);
648 i2c_set_adapdata(&cpm
->adap
, cpm
);
649 cpm
->adap
.dev
.parent
= &ofdev
->dev
;
650 cpm
->adap
.dev
.of_node
= of_node_get(ofdev
->dev
.of_node
);
652 result
= cpm_i2c_setup(cpm
);
654 dev_err(&ofdev
->dev
, "Unable to init hardware\n");
658 /* register new adapter to i2c module... */
660 data
= of_get_property(ofdev
->dev
.of_node
, "linux,i2c-index", &len
);
661 cpm
->adap
.nr
= (data
&& len
== 4) ? be32_to_cpup(data
) : -1;
662 result
= i2c_add_numbered_adapter(&cpm
->adap
);
667 dev_dbg(&ofdev
->dev
, "hw routines for %s registered.\n",
672 cpm_i2c_shutdown(cpm
);
679 static int cpm_i2c_remove(struct platform_device
*ofdev
)
681 struct cpm_i2c
*cpm
= platform_get_drvdata(ofdev
);
683 i2c_del_adapter(&cpm
->adap
);
685 cpm_i2c_shutdown(cpm
);
692 static const struct of_device_id cpm_i2c_match
[] = {
694 .compatible
= "fsl,cpm1-i2c",
697 .compatible
= "fsl,cpm2-i2c",
702 MODULE_DEVICE_TABLE(of
, cpm_i2c_match
);
704 static struct platform_driver cpm_i2c_driver
= {
705 .probe
= cpm_i2c_probe
,
706 .remove
= cpm_i2c_remove
,
708 .name
= "fsl-i2c-cpm",
709 .of_match_table
= cpm_i2c_match
,
713 module_platform_driver(cpm_i2c_driver
);
715 MODULE_AUTHOR("Jochen Friedrich <jochen@scram.de>");
716 MODULE_DESCRIPTION("I2C-Bus adapter routines for CPM boards");
717 MODULE_LICENSE("GPL");