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 */
70 #define I2COM_START 0x80
71 #define I2COM_MASTER 0x01
72 #define I2CER_TXE 0x10
73 #define I2CER_BUSY 0x04
74 #define I2CER_TXB 0x02
75 #define I2CER_RXB 0x01
95 struct platform_device
*ofdev
;
96 struct i2c_adapter adap
;
98 int version
; /* CPM1=1, CPM2=2 */
102 struct i2c_reg __iomem
*i2c_reg
;
103 struct i2c_ram __iomem
*i2c_ram
;
105 wait_queue_head_t i2c_wait
;
106 cbd_t __iomem
*tbase
;
107 cbd_t __iomem
*rbase
;
108 u_char
*txbuf
[CPM_MAXBD
];
109 u_char
*rxbuf
[CPM_MAXBD
];
110 dma_addr_t txdma
[CPM_MAXBD
];
111 dma_addr_t rxdma
[CPM_MAXBD
];
114 static irqreturn_t
cpm_i2c_interrupt(int irq
, void *dev_id
)
117 struct i2c_reg __iomem
*i2c_reg
;
118 struct i2c_adapter
*adap
= dev_id
;
121 cpm
= i2c_get_adapdata(dev_id
);
122 i2c_reg
= cpm
->i2c_reg
;
124 /* Clear interrupt. */
125 i
= in_8(&i2c_reg
->i2cer
);
126 out_8(&i2c_reg
->i2cer
, i
);
128 dev_dbg(&adap
->dev
, "Interrupt: %x\n", i
);
130 wake_up(&cpm
->i2c_wait
);
132 return i
? IRQ_HANDLED
: IRQ_NONE
;
135 static void cpm_reset_i2c_params(struct cpm_i2c
*cpm
)
137 struct i2c_ram __iomem
*i2c_ram
= cpm
->i2c_ram
;
139 /* Set up the I2C parameters in the parameter ram. */
140 out_be16(&i2c_ram
->tbase
, (u8 __iomem
*)cpm
->tbase
- DPRAM_BASE
);
141 out_be16(&i2c_ram
->rbase
, (u8 __iomem
*)cpm
->rbase
- DPRAM_BASE
);
143 if (cpm
->version
== 1) {
144 out_8(&i2c_ram
->tfcr
, I2C_EB
);
145 out_8(&i2c_ram
->rfcr
, I2C_EB
);
147 out_8(&i2c_ram
->tfcr
, I2C_EB_CPM2
);
148 out_8(&i2c_ram
->rfcr
, I2C_EB_CPM2
);
151 out_be16(&i2c_ram
->mrblr
, CPM_MAX_READ
);
153 out_be32(&i2c_ram
->rstate
, 0);
154 out_be32(&i2c_ram
->rdp
, 0);
155 out_be16(&i2c_ram
->rbptr
, 0);
156 out_be16(&i2c_ram
->rbc
, 0);
157 out_be32(&i2c_ram
->rxtmp
, 0);
158 out_be32(&i2c_ram
->tstate
, 0);
159 out_be32(&i2c_ram
->tdp
, 0);
160 out_be16(&i2c_ram
->tbptr
, 0);
161 out_be16(&i2c_ram
->tbc
, 0);
162 out_be32(&i2c_ram
->txtmp
, 0);
165 static void cpm_i2c_force_close(struct i2c_adapter
*adap
)
167 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
168 struct i2c_reg __iomem
*i2c_reg
= cpm
->i2c_reg
;
170 dev_dbg(&adap
->dev
, "cpm_i2c_force_close()\n");
172 cpm_command(cpm
->cp_command
, CPM_CR_CLOSE_RX_BD
);
174 out_8(&i2c_reg
->i2cmr
, 0x00); /* Disable all interrupts */
175 out_8(&i2c_reg
->i2cer
, 0xff);
178 static void cpm_i2c_parse_message(struct i2c_adapter
*adap
,
179 struct i2c_msg
*pmsg
, int num
, int tx
, int rx
)
186 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
188 tbdf
= cpm
->tbase
+ tx
;
189 rbdf
= cpm
->rbase
+ rx
;
191 addr
= i2c_8bit_addr_from_msg(pmsg
);
196 /* Align read buffer */
197 rb
= (u_char
*) (((ulong
) rb
+ 1) & ~1);
199 tb
[0] = addr
; /* Device address byte w/rw flag */
201 out_be16(&tbdf
->cbd_datlen
, pmsg
->len
+ 1);
202 out_be16(&tbdf
->cbd_sc
, 0);
204 if (!(pmsg
->flags
& I2C_M_NOSTART
))
205 setbits16(&tbdf
->cbd_sc
, BD_I2C_START
);
208 setbits16(&tbdf
->cbd_sc
, BD_SC_LAST
| BD_SC_WRAP
);
210 if (pmsg
->flags
& I2C_M_RD
) {
212 * To read, we need an empty buffer of the proper length.
213 * All that is used is the first byte for address, the remainder
214 * is just used for timing (and doesn't really have to exist).
217 dev_dbg(&adap
->dev
, "cpm_i2c_read(abyte=0x%x)\n", addr
);
219 out_be16(&rbdf
->cbd_datlen
, 0);
220 out_be16(&rbdf
->cbd_sc
, BD_SC_EMPTY
| BD_SC_INTRPT
);
222 if (rx
+ 1 == CPM_MAXBD
)
223 setbits16(&rbdf
->cbd_sc
, BD_SC_WRAP
);
226 setbits16(&tbdf
->cbd_sc
, BD_SC_READY
);
228 dev_dbg(&adap
->dev
, "cpm_i2c_write(abyte=0x%x)\n", addr
);
230 memcpy(tb
+1, pmsg
->buf
, pmsg
->len
);
233 setbits16(&tbdf
->cbd_sc
, BD_SC_READY
| BD_SC_INTRPT
);
237 static int cpm_i2c_check_message(struct i2c_adapter
*adap
,
238 struct i2c_msg
*pmsg
, int tx
, int rx
)
244 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
246 tbdf
= cpm
->tbase
+ tx
;
247 rbdf
= cpm
->rbase
+ rx
;
252 /* Align read buffer */
253 rb
= (u_char
*) (((uint
) rb
+ 1) & ~1);
256 if (pmsg
->flags
& I2C_M_RD
) {
257 dev_dbg(&adap
->dev
, "tx sc 0x%04x, rx sc 0x%04x\n",
258 in_be16(&tbdf
->cbd_sc
), in_be16(&rbdf
->cbd_sc
));
260 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_NAK
) {
261 dev_dbg(&adap
->dev
, "I2C read; No ack\n");
264 if (in_be16(&rbdf
->cbd_sc
) & BD_SC_EMPTY
) {
266 "I2C read; complete but rbuf empty\n");
269 if (in_be16(&rbdf
->cbd_sc
) & BD_SC_OV
) {
270 dev_err(&adap
->dev
, "I2C read; Overrun\n");
273 memcpy(pmsg
->buf
, rb
, pmsg
->len
);
275 dev_dbg(&adap
->dev
, "tx sc %d 0x%04x\n", tx
,
276 in_be16(&tbdf
->cbd_sc
));
278 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_NAK
) {
279 dev_dbg(&adap
->dev
, "I2C write; No ack\n");
282 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_UN
) {
283 dev_err(&adap
->dev
, "I2C write; Underrun\n");
286 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_CL
) {
287 dev_err(&adap
->dev
, "I2C write; Collision\n");
294 static int cpm_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
296 struct cpm_i2c
*cpm
= i2c_get_adapdata(adap
);
297 struct i2c_reg __iomem
*i2c_reg
= cpm
->i2c_reg
;
298 struct i2c_ram __iomem
*i2c_ram
= cpm
->i2c_ram
;
299 struct i2c_msg
*pmsg
;
306 /* Reset to use first buffer */
307 out_be16(&i2c_ram
->rbptr
, in_be16(&i2c_ram
->rbase
));
308 out_be16(&i2c_ram
->tbptr
, in_be16(&i2c_ram
->tbase
));
317 * If there was a collision in the last i2c transaction,
318 * Set I2COM_MASTER as it was cleared during collision.
320 if (in_be16(&tbdf
->cbd_sc
) & BD_SC_CL
) {
321 out_8(&cpm
->i2c_reg
->i2com
, I2COM_MASTER
);
326 dev_dbg(&adap
->dev
, "R: %d T: %d\n", rptr
, tptr
);
328 cpm_i2c_parse_message(adap
, pmsg
, num
, tptr
, rptr
);
329 if (pmsg
->flags
& I2C_M_RD
)
333 /* Start transfer now */
334 /* Enable RX/TX/Error interupts */
335 out_8(&i2c_reg
->i2cmr
, I2CER_TXE
| I2CER_TXB
| I2CER_RXB
);
336 out_8(&i2c_reg
->i2cer
, 0xff); /* Clear interrupt status */
337 /* Chip bug, set enable here */
338 setbits8(&i2c_reg
->i2mod
, I2MOD_EN
); /* Enable */
339 /* Begin transmission */
340 setbits8(&i2c_reg
->i2com
, I2COM_START
);
346 /* Check for outstanding messages */
347 dev_dbg(&adap
->dev
, "test ready.\n");
349 if (pmsg
->flags
& I2C_M_RD
)
350 ret
= wait_event_timeout(cpm
->i2c_wait
,
351 (in_be16(&tbdf
[tptr
].cbd_sc
) & BD_SC_NAK
) ||
352 !(in_be16(&rbdf
[rptr
].cbd_sc
) & BD_SC_EMPTY
),
355 ret
= wait_event_timeout(cpm
->i2c_wait
,
356 !(in_be16(&tbdf
[tptr
].cbd_sc
) & BD_SC_READY
),
360 dev_err(&adap
->dev
, "I2C transfer: timeout\n");
364 dev_dbg(&adap
->dev
, "ready.\n");
365 ret
= cpm_i2c_check_message(adap
, pmsg
, tptr
, rptr
);
367 if (pmsg
->flags
& I2C_M_RD
)
373 #ifdef I2C_CHIP_ERRATA
375 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
376 * Disabling I2C too early may cause too short stop condition
379 clrbits8(&i2c_reg
->i2mod
, I2MOD_EN
);
384 cpm_i2c_force_close(adap
);
385 #ifdef I2C_CHIP_ERRATA
387 * Chip errata, clear enable. This is not needed on rev D4 CPUs.
389 clrbits8(&i2c_reg
->i2mod
, I2MOD_EN
);
394 static u32
cpm_i2c_func(struct i2c_adapter
*adap
)
396 return I2C_FUNC_I2C
| (I2C_FUNC_SMBUS_EMUL
& ~I2C_FUNC_SMBUS_QUICK
);
399 /* -----exported algorithm data: ------------------------------------- */
401 static const struct i2c_algorithm cpm_i2c_algo
= {
402 .master_xfer
= cpm_i2c_xfer
,
403 .functionality
= cpm_i2c_func
,
406 /* CPM_MAX_READ is also limiting writes according to the code! */
407 static const struct i2c_adapter_quirks cpm_i2c_quirks
= {
408 .max_num_msgs
= CPM_MAXBD
,
409 .max_read_len
= CPM_MAX_READ
,
410 .max_write_len
= CPM_MAX_READ
,
413 static const struct i2c_adapter cpm_ops
= {
414 .owner
= THIS_MODULE
,
416 .algo
= &cpm_i2c_algo
,
417 .quirks
= &cpm_i2c_quirks
,
420 static int cpm_i2c_setup(struct cpm_i2c
*cpm
)
422 struct platform_device
*ofdev
= cpm
->ofdev
;
425 void __iomem
*i2c_base
;
430 dev_dbg(&cpm
->ofdev
->dev
, "cpm_i2c_setup()\n");
432 init_waitqueue_head(&cpm
->i2c_wait
);
434 cpm
->irq
= irq_of_parse_and_map(ofdev
->dev
.of_node
, 0);
438 /* Install interrupt handler. */
439 ret
= request_irq(cpm
->irq
, cpm_i2c_interrupt
, 0, "cpm_i2c",
444 /* I2C parameter RAM */
445 i2c_base
= of_iomap(ofdev
->dev
.of_node
, 1);
446 if (i2c_base
== NULL
) {
451 if (of_device_is_compatible(ofdev
->dev
.of_node
, "fsl,cpm1-i2c")) {
453 /* Check for and use a microcode relocation patch. */
454 cpm
->i2c_ram
= i2c_base
;
455 cpm
->i2c_addr
= in_be16(&cpm
->i2c_ram
->rpbase
);
458 * Maybe should use cpm_muram_alloc instead of hardcoding
459 * this in micropatch.c
462 cpm
->i2c_ram
= cpm_muram_addr(cpm
->i2c_addr
);
468 } else if (of_device_is_compatible(ofdev
->dev
.of_node
, "fsl,cpm2-i2c")) {
469 cpm
->i2c_addr
= cpm_muram_alloc(sizeof(struct i2c_ram
), 64);
470 cpm
->i2c_ram
= cpm_muram_addr(cpm
->i2c_addr
);
471 out_be16(i2c_base
, cpm
->i2c_addr
);
482 /* I2C control/status registers */
483 cpm
->i2c_reg
= of_iomap(ofdev
->dev
.of_node
, 0);
484 if (cpm
->i2c_reg
== NULL
) {
489 data
= of_get_property(ofdev
->dev
.of_node
, "fsl,cpm-command", &len
);
490 if (!data
|| len
!= 4) {
494 cpm
->cp_command
= *data
;
496 data
= of_get_property(ofdev
->dev
.of_node
, "linux,i2c-class", &len
);
497 if (data
&& len
== 4)
498 cpm
->adap
.class = *data
;
500 data
= of_get_property(ofdev
->dev
.of_node
, "clock-frequency", &len
);
501 if (data
&& len
== 4)
504 cpm
->freq
= 60000; /* use 60kHz i2c clock by default */
507 * Allocate space for CPM_MAXBD transmit and receive buffer
508 * descriptors in the DP ram.
510 cpm
->dp_addr
= cpm_muram_alloc(sizeof(cbd_t
) * 2 * CPM_MAXBD
, 8);
516 cpm
->tbase
= cpm_muram_addr(cpm
->dp_addr
);
517 cpm
->rbase
= cpm_muram_addr(cpm
->dp_addr
+ sizeof(cbd_t
) * CPM_MAXBD
);
519 /* Allocate TX and RX buffers */
524 for (i
= 0; i
< CPM_MAXBD
; i
++) {
525 cpm
->rxbuf
[i
] = dma_alloc_coherent(&cpm
->ofdev
->dev
,
527 &cpm
->rxdma
[i
], GFP_KERNEL
);
528 if (!cpm
->rxbuf
[i
]) {
532 out_be32(&rbdf
[i
].cbd_bufaddr
, ((cpm
->rxdma
[i
] + 1) & ~1));
534 cpm
->txbuf
[i
] = dma_alloc_coherent(&cpm
->ofdev
->dev
,
536 &cpm
->txdma
[i
], GFP_KERNEL
);
537 if (!cpm
->txbuf
[i
]) {
541 out_be32(&tbdf
[i
].cbd_bufaddr
, cpm
->txdma
[i
]);
544 /* Initialize Tx/Rx parameters. */
546 cpm_reset_i2c_params(cpm
);
548 dev_dbg(&cpm
->ofdev
->dev
, "i2c_ram 0x%p, i2c_addr 0x%04x, freq %d\n",
549 cpm
->i2c_ram
, cpm
->i2c_addr
, cpm
->freq
);
550 dev_dbg(&cpm
->ofdev
->dev
, "tbase 0x%04x, rbase 0x%04x\n",
551 (u8 __iomem
*)cpm
->tbase
- DPRAM_BASE
,
552 (u8 __iomem
*)cpm
->rbase
- DPRAM_BASE
);
554 cpm_command(cpm
->cp_command
, CPM_CR_INIT_TRX
);
557 * Select an invalid address. Just make sure we don't use loopback mode
559 out_8(&cpm
->i2c_reg
->i2add
, 0x7f << 1);
562 * PDIV is set to 00 in i2mod, so brgclk/32 is used as input to the
563 * i2c baud rate generator. This is divided by 2 x (DIV + 3) to get
564 * the actual i2c bus frequency.
566 brg
= get_brgfreq() / (32 * 2 * cpm
->freq
) - 3;
567 out_8(&cpm
->i2c_reg
->i2brg
, brg
);
569 out_8(&cpm
->i2c_reg
->i2mod
, 0x00);
570 out_8(&cpm
->i2c_reg
->i2com
, I2COM_MASTER
); /* Master mode */
572 /* Disable interrupts. */
573 out_8(&cpm
->i2c_reg
->i2cmr
, 0);
574 out_8(&cpm
->i2c_reg
->i2cer
, 0xff);
579 for (i
= 0; i
< CPM_MAXBD
; i
++) {
581 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
582 cpm
->rxbuf
[i
], cpm
->rxdma
[i
]);
584 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
585 cpm
->txbuf
[i
], cpm
->txdma
[i
]);
587 cpm_muram_free(cpm
->dp_addr
);
589 iounmap(cpm
->i2c_reg
);
591 if ((cpm
->version
== 1) && (!cpm
->i2c_addr
))
592 iounmap(cpm
->i2c_ram
);
593 if (cpm
->version
== 2)
594 cpm_muram_free(cpm
->i2c_addr
);
596 free_irq(cpm
->irq
, &cpm
->adap
);
600 static void cpm_i2c_shutdown(struct cpm_i2c
*cpm
)
605 clrbits8(&cpm
->i2c_reg
->i2mod
, I2MOD_EN
);
607 /* Disable interrupts */
608 out_8(&cpm
->i2c_reg
->i2cmr
, 0);
609 out_8(&cpm
->i2c_reg
->i2cer
, 0xff);
611 free_irq(cpm
->irq
, &cpm
->adap
);
613 /* Free all memory */
614 for (i
= 0; i
< CPM_MAXBD
; i
++) {
615 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
616 cpm
->rxbuf
[i
], cpm
->rxdma
[i
]);
617 dma_free_coherent(&cpm
->ofdev
->dev
, CPM_MAX_READ
+ 1,
618 cpm
->txbuf
[i
], cpm
->txdma
[i
]);
621 cpm_muram_free(cpm
->dp_addr
);
622 iounmap(cpm
->i2c_reg
);
624 if ((cpm
->version
== 1) && (!cpm
->i2c_addr
))
625 iounmap(cpm
->i2c_ram
);
626 if (cpm
->version
== 2)
627 cpm_muram_free(cpm
->i2c_addr
);
630 static int cpm_i2c_probe(struct platform_device
*ofdev
)
636 cpm
= kzalloc(sizeof(struct cpm_i2c
), GFP_KERNEL
);
642 platform_set_drvdata(ofdev
, cpm
);
645 i2c_set_adapdata(&cpm
->adap
, cpm
);
646 cpm
->adap
.dev
.parent
= &ofdev
->dev
;
647 cpm
->adap
.dev
.of_node
= of_node_get(ofdev
->dev
.of_node
);
649 result
= cpm_i2c_setup(cpm
);
651 dev_err(&ofdev
->dev
, "Unable to init hardware\n");
655 /* register new adapter to i2c module... */
657 data
= of_get_property(ofdev
->dev
.of_node
, "linux,i2c-index", &len
);
658 cpm
->adap
.nr
= (data
&& len
== 4) ? be32_to_cpup(data
) : -1;
659 result
= i2c_add_numbered_adapter(&cpm
->adap
);
664 dev_dbg(&ofdev
->dev
, "hw routines for %s registered.\n",
669 cpm_i2c_shutdown(cpm
);
676 static int cpm_i2c_remove(struct platform_device
*ofdev
)
678 struct cpm_i2c
*cpm
= platform_get_drvdata(ofdev
);
680 i2c_del_adapter(&cpm
->adap
);
682 cpm_i2c_shutdown(cpm
);
689 static const struct of_device_id cpm_i2c_match
[] = {
691 .compatible
= "fsl,cpm1-i2c",
694 .compatible
= "fsl,cpm2-i2c",
699 MODULE_DEVICE_TABLE(of
, cpm_i2c_match
);
701 static struct platform_driver cpm_i2c_driver
= {
702 .probe
= cpm_i2c_probe
,
703 .remove
= cpm_i2c_remove
,
705 .name
= "fsl-i2c-cpm",
706 .of_match_table
= cpm_i2c_match
,
710 module_platform_driver(cpm_i2c_driver
);
712 MODULE_AUTHOR("Jochen Friedrich <jochen@scram.de>");
713 MODULE_DESCRIPTION("I2C-Bus adapter routines for CPM boards");
714 MODULE_LICENSE("GPL");