2 * (C) Copyright 2003-2004
3 * Humboldt Solutions Ltd, adrian@humboldt.co.uk.
5 * This is a combined i2c adapter and algorithm driver for the
6 * MPC107/Tsi107 PowerPC northbridge and processors that include
7 * the same I2C unit (8240, 8245, 85xx).
11 * This file is licensed under the terms of the GNU General Public
12 * License version 2. This program is licensed "as is" without any
13 * warranty of any kind, whether express or implied.
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/sched.h>
19 #include <linux/init.h>
20 #include <linux/of_platform.h>
21 #include <linux/of_i2c.h>
24 #include <linux/fsl_devices.h>
25 #include <linux/i2c.h>
26 #include <linux/interrupt.h>
27 #include <linux/delay.h>
29 #define DRV_NAME "mpc-i2c"
31 #define MPC_I2C_FDR 0x04
32 #define MPC_I2C_CR 0x08
33 #define MPC_I2C_SR 0x0c
34 #define MPC_I2C_DR 0x10
35 #define MPC_I2C_DFSRR 0x14
56 wait_queue_head_t queue
;
57 struct i2c_adapter adap
;
62 static inline void writeccr(struct mpc_i2c
*i2c
, u32 x
)
64 writeb(x
, i2c
->base
+ MPC_I2C_CR
);
67 static irqreturn_t
mpc_i2c_isr(int irq
, void *dev_id
)
69 struct mpc_i2c
*i2c
= dev_id
;
70 if (readb(i2c
->base
+ MPC_I2C_SR
) & CSR_MIF
) {
71 /* Read again to allow register to stabilise */
72 i2c
->interrupt
= readb(i2c
->base
+ MPC_I2C_SR
);
73 writeb(0, i2c
->base
+ MPC_I2C_SR
);
79 /* Sometimes 9th clock pulse isn't generated, and slave doesn't release
80 * the bus, because it wants to send ACK.
81 * Following sequence of enabling/disabling and sending start/stop generates
82 * the pulse, so it's all OK.
84 static void mpc_i2c_fixup(struct mpc_i2c
*i2c
)
88 writeccr(i2c
, CCR_MEN
);
90 writeccr(i2c
, CCR_MSTA
| CCR_MTX
);
92 writeccr(i2c
, CCR_MSTA
| CCR_MTX
| CCR_MEN
);
94 writeccr(i2c
, CCR_MEN
);
98 static int i2c_wait(struct mpc_i2c
*i2c
, unsigned timeout
, int writing
)
100 unsigned long orig_jiffies
= jiffies
;
104 if (i2c
->irq
== NO_IRQ
) {
105 while (!(readb(i2c
->base
+ MPC_I2C_SR
) & CSR_MIF
)) {
107 if (time_after(jiffies
, orig_jiffies
+ timeout
)) {
108 dev_dbg(i2c
->dev
, "timeout\n");
114 x
= readb(i2c
->base
+ MPC_I2C_SR
);
115 writeb(0, i2c
->base
+ MPC_I2C_SR
);
118 result
= wait_event_timeout(i2c
->queue
,
119 (i2c
->interrupt
& CSR_MIF
), timeout
);
121 if (unlikely(!(i2c
->interrupt
& CSR_MIF
))) {
122 dev_dbg(i2c
->dev
, "wait timeout\n");
134 if (!(x
& CSR_MCF
)) {
135 dev_dbg(i2c
->dev
, "unfinished\n");
140 dev_dbg(i2c
->dev
, "MAL\n");
144 if (writing
&& (x
& CSR_RXAK
)) {
145 dev_dbg(i2c
->dev
, "No RXAK\n");
147 writeccr(i2c
, CCR_MEN
);
153 static void mpc_i2c_setclock(struct mpc_i2c
*i2c
)
155 /* Set clock and filters */
156 if (i2c
->flags
& FSL_I2C_DEV_SEPARATE_DFSRR
) {
157 writeb(0x31, i2c
->base
+ MPC_I2C_FDR
);
158 writeb(0x10, i2c
->base
+ MPC_I2C_DFSRR
);
159 } else if (i2c
->flags
& FSL_I2C_DEV_CLOCK_5200
)
160 writeb(0x3f, i2c
->base
+ MPC_I2C_FDR
);
162 writel(0x1031, i2c
->base
+ MPC_I2C_FDR
);
165 static void mpc_i2c_start(struct mpc_i2c
*i2c
)
167 /* Clear arbitration */
168 writeb(0, i2c
->base
+ MPC_I2C_SR
);
170 writeccr(i2c
, CCR_MEN
);
173 static void mpc_i2c_stop(struct mpc_i2c
*i2c
)
175 writeccr(i2c
, CCR_MEN
);
178 static int mpc_write(struct mpc_i2c
*i2c
, int target
,
179 const u8
*data
, int length
, int restart
)
182 unsigned timeout
= i2c
->adap
.timeout
;
183 u32 flags
= restart
? CCR_RSTA
: 0;
187 writeccr(i2c
, CCR_MEN
);
188 /* Start as master */
189 writeccr(i2c
, CCR_MIEN
| CCR_MEN
| CCR_MSTA
| CCR_MTX
| flags
);
190 /* Write target byte */
191 writeb((target
<< 1), i2c
->base
+ MPC_I2C_DR
);
193 result
= i2c_wait(i2c
, timeout
, 1);
197 for (i
= 0; i
< length
; i
++) {
198 /* Write data byte */
199 writeb(data
[i
], i2c
->base
+ MPC_I2C_DR
);
201 result
= i2c_wait(i2c
, timeout
, 1);
209 static int mpc_read(struct mpc_i2c
*i2c
, int target
,
210 u8
*data
, int length
, int restart
)
212 unsigned timeout
= i2c
->adap
.timeout
;
214 u32 flags
= restart
? CCR_RSTA
: 0;
218 writeccr(i2c
, CCR_MEN
);
219 /* Switch to read - restart */
220 writeccr(i2c
, CCR_MIEN
| CCR_MEN
| CCR_MSTA
| CCR_MTX
| flags
);
221 /* Write target address byte - this time with the read flag set */
222 writeb((target
<< 1) | 1, i2c
->base
+ MPC_I2C_DR
);
224 result
= i2c_wait(i2c
, timeout
, 1);
230 writeccr(i2c
, CCR_MIEN
| CCR_MEN
| CCR_MSTA
| CCR_TXAK
);
232 writeccr(i2c
, CCR_MIEN
| CCR_MEN
| CCR_MSTA
);
234 readb(i2c
->base
+ MPC_I2C_DR
);
237 for (i
= 0; i
< length
; i
++) {
238 result
= i2c_wait(i2c
, timeout
, 0);
242 /* Generate txack on next to last byte */
244 writeccr(i2c
, CCR_MIEN
| CCR_MEN
| CCR_MSTA
| CCR_TXAK
);
245 /* Generate stop on last byte */
247 writeccr(i2c
, CCR_MIEN
| CCR_MEN
| CCR_TXAK
);
248 data
[i
] = readb(i2c
->base
+ MPC_I2C_DR
);
254 static int mpc_xfer(struct i2c_adapter
*adap
, struct i2c_msg
*msgs
, int num
)
256 struct i2c_msg
*pmsg
;
259 unsigned long orig_jiffies
= jiffies
;
260 struct mpc_i2c
*i2c
= i2c_get_adapdata(adap
);
264 /* Allow bus up to 1s to become not busy */
265 while (readb(i2c
->base
+ MPC_I2C_SR
) & CSR_MBB
) {
266 if (signal_pending(current
)) {
267 dev_dbg(i2c
->dev
, "Interrupted\n");
271 if (time_after(jiffies
, orig_jiffies
+ HZ
)) {
272 dev_dbg(i2c
->dev
, "timeout\n");
273 if (readb(i2c
->base
+ MPC_I2C_SR
) ==
274 (CSR_MCF
| CSR_MBB
| CSR_RXAK
))
281 for (i
= 0; ret
>= 0 && i
< num
; i
++) {
284 "Doing %s %d bytes to 0x%02x - %d of %d messages\n",
285 pmsg
->flags
& I2C_M_RD
? "read" : "write",
286 pmsg
->len
, pmsg
->addr
, i
+ 1, num
);
287 if (pmsg
->flags
& I2C_M_RD
)
289 mpc_read(i2c
, pmsg
->addr
, pmsg
->buf
, pmsg
->len
, i
);
292 mpc_write(i2c
, pmsg
->addr
, pmsg
->buf
, pmsg
->len
, i
);
295 return (ret
< 0) ? ret
: num
;
298 static u32
mpc_functionality(struct i2c_adapter
*adap
)
300 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
303 static const struct i2c_algorithm mpc_algo
= {
304 .master_xfer
= mpc_xfer
,
305 .functionality
= mpc_functionality
,
308 static struct i2c_adapter mpc_ops
= {
309 .owner
= THIS_MODULE
,
310 .name
= "MPC adapter",
315 static int __devinit
fsl_i2c_probe(struct of_device
*op
,
316 const struct of_device_id
*match
)
321 i2c
= kzalloc(sizeof(*i2c
), GFP_KERNEL
);
325 i2c
->dev
= &op
->dev
; /* for debug and error output */
327 if (of_get_property(op
->node
, "dfsrr", NULL
))
328 i2c
->flags
|= FSL_I2C_DEV_SEPARATE_DFSRR
;
330 if (of_device_is_compatible(op
->node
, "fsl,mpc5200-i2c") ||
331 of_device_is_compatible(op
->node
, "mpc5200-i2c"))
332 i2c
->flags
|= FSL_I2C_DEV_CLOCK_5200
;
334 init_waitqueue_head(&i2c
->queue
);
336 i2c
->base
= of_iomap(op
->node
, 0);
338 dev_err(i2c
->dev
, "failed to map controller\n");
343 i2c
->irq
= irq_of_parse_and_map(op
->node
, 0);
344 if (i2c
->irq
!= NO_IRQ
) { /* i2c->irq = NO_IRQ implies polling */
345 result
= request_irq(i2c
->irq
, mpc_i2c_isr
,
346 IRQF_SHARED
, "i2c-mpc", i2c
);
348 dev_err(i2c
->dev
, "failed to attach interrupt\n");
353 mpc_i2c_setclock(i2c
);
355 dev_set_drvdata(&op
->dev
, i2c
);
358 i2c_set_adapdata(&i2c
->adap
, i2c
);
359 i2c
->adap
.dev
.parent
= &op
->dev
;
361 result
= i2c_add_adapter(&i2c
->adap
);
363 dev_err(i2c
->dev
, "failed to add adapter\n");
366 of_register_i2c_devices(&i2c
->adap
, op
->node
);
371 dev_set_drvdata(&op
->dev
, NULL
);
372 free_irq(i2c
->irq
, i2c
);
374 irq_dispose_mapping(i2c
->irq
);
381 static int __devexit
fsl_i2c_remove(struct of_device
*op
)
383 struct mpc_i2c
*i2c
= dev_get_drvdata(&op
->dev
);
385 i2c_del_adapter(&i2c
->adap
);
386 dev_set_drvdata(&op
->dev
, NULL
);
388 if (i2c
->irq
!= NO_IRQ
)
389 free_irq(i2c
->irq
, i2c
);
391 irq_dispose_mapping(i2c
->irq
);
397 static const struct of_device_id mpc_i2c_of_match
[] = {
398 {.compatible
= "fsl-i2c",},
401 MODULE_DEVICE_TABLE(of
, mpc_i2c_of_match
);
404 /* Structure for a device driver */
405 static struct of_platform_driver mpc_i2c_driver
= {
406 .match_table
= mpc_i2c_of_match
,
407 .probe
= fsl_i2c_probe
,
408 .remove
= __devexit_p(fsl_i2c_remove
),
410 .owner
= THIS_MODULE
,
415 static int __init
fsl_i2c_init(void)
419 rv
= of_register_platform_driver(&mpc_i2c_driver
);
421 printk(KERN_ERR DRV_NAME
422 " of_register_platform_driver failed (%i)\n", rv
);
426 static void __exit
fsl_i2c_exit(void)
428 of_unregister_platform_driver(&mpc_i2c_driver
);
431 module_init(fsl_i2c_init
);
432 module_exit(fsl_i2c_exit
);
434 MODULE_AUTHOR("Adrian Cox <adrian@humboldt.co.uk>");
435 MODULE_DESCRIPTION("I2C-Bus adapter for MPC107 bridge and "
436 "MPC824x/85xx/52xx processors");
437 MODULE_LICENSE("GPL");