2 * Driver for the i2c controller on the Marvell line of host bridges
3 * (e.g, gt642[46]0, mv643[46]0, mv644[46]0, and Orion SoC family).
5 * Author: Mark A. Greer <mgreer@mvista.com>
7 * 2005 (c) MontaVista, Software, Inc. This file is licensed under
8 * the terms of the GNU General Public License version 2. This program
9 * is licensed "as is" without any warranty of any kind, whether express
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/module.h>
15 #include <linux/spinlock.h>
16 #include <linux/i2c.h>
17 #include <linux/interrupt.h>
18 #include <linux/mv643xx_i2c.h>
19 #include <linux/platform_device.h>
23 /* Register defines */
24 #define MV64XXX_I2C_REG_SLAVE_ADDR 0x00
25 #define MV64XXX_I2C_REG_DATA 0x04
26 #define MV64XXX_I2C_REG_CONTROL 0x08
27 #define MV64XXX_I2C_REG_STATUS 0x0c
28 #define MV64XXX_I2C_REG_BAUD 0x0c
29 #define MV64XXX_I2C_REG_EXT_SLAVE_ADDR 0x10
30 #define MV64XXX_I2C_REG_SOFT_RESET 0x1c
32 #define MV64XXX_I2C_REG_CONTROL_ACK 0x00000004
33 #define MV64XXX_I2C_REG_CONTROL_IFLG 0x00000008
34 #define MV64XXX_I2C_REG_CONTROL_STOP 0x00000010
35 #define MV64XXX_I2C_REG_CONTROL_START 0x00000020
36 #define MV64XXX_I2C_REG_CONTROL_TWSIEN 0x00000040
37 #define MV64XXX_I2C_REG_CONTROL_INTEN 0x00000080
39 /* Ctlr status values */
40 #define MV64XXX_I2C_STATUS_BUS_ERR 0x00
41 #define MV64XXX_I2C_STATUS_MAST_START 0x08
42 #define MV64XXX_I2C_STATUS_MAST_REPEAT_START 0x10
43 #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK 0x18
44 #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK 0x20
45 #define MV64XXX_I2C_STATUS_MAST_WR_ACK 0x28
46 #define MV64XXX_I2C_STATUS_MAST_WR_NO_ACK 0x30
47 #define MV64XXX_I2C_STATUS_MAST_LOST_ARB 0x38
48 #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK 0x40
49 #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK 0x48
50 #define MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK 0x50
51 #define MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK 0x58
52 #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK 0xd0
53 #define MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_NO_ACK 0xd8
54 #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK 0xe0
55 #define MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_NO_ACK 0xe8
56 #define MV64XXX_I2C_STATUS_NO_STATUS 0xf8
60 MV64XXX_I2C_STATE_INVALID
,
61 MV64XXX_I2C_STATE_IDLE
,
62 MV64XXX_I2C_STATE_WAITING_FOR_START_COND
,
63 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK
,
64 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK
,
65 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK
,
66 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA
,
71 MV64XXX_I2C_ACTION_INVALID
,
72 MV64XXX_I2C_ACTION_CONTINUE
,
73 MV64XXX_I2C_ACTION_SEND_START
,
74 MV64XXX_I2C_ACTION_SEND_ADDR_1
,
75 MV64XXX_I2C_ACTION_SEND_ADDR_2
,
76 MV64XXX_I2C_ACTION_SEND_DATA
,
77 MV64XXX_I2C_ACTION_RCV_DATA
,
78 MV64XXX_I2C_ACTION_RCV_DATA_STOP
,
79 MV64XXX_I2C_ACTION_SEND_STOP
,
82 struct mv64xxx_i2c_data
{
88 void __iomem
*reg_base
;
99 wait_queue_head_t waitq
;
102 struct i2c_adapter adapter
;
106 *****************************************************************************
108 * Finite State Machine & Interrupt Routines
110 *****************************************************************************
113 /* Reset hardware and initialize FSM */
115 mv64xxx_i2c_hw_init(struct mv64xxx_i2c_data
*drv_data
)
117 writel(0, drv_data
->reg_base
+ MV64XXX_I2C_REG_SOFT_RESET
);
118 writel((((drv_data
->freq_m
& 0xf) << 3) | (drv_data
->freq_n
& 0x7)),
119 drv_data
->reg_base
+ MV64XXX_I2C_REG_BAUD
);
120 writel(0, drv_data
->reg_base
+ MV64XXX_I2C_REG_SLAVE_ADDR
);
121 writel(0, drv_data
->reg_base
+ MV64XXX_I2C_REG_EXT_SLAVE_ADDR
);
122 writel(MV64XXX_I2C_REG_CONTROL_TWSIEN
| MV64XXX_I2C_REG_CONTROL_STOP
,
123 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
124 drv_data
->state
= MV64XXX_I2C_STATE_IDLE
;
128 mv64xxx_i2c_fsm(struct mv64xxx_i2c_data
*drv_data
, u32 status
)
131 * If state is idle, then this is likely the remnants of an old
132 * operation that driver has given up on or the user has killed.
133 * If so, issue the stop condition and go to idle.
135 if (drv_data
->state
== MV64XXX_I2C_STATE_IDLE
) {
136 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_STOP
;
140 /* The status from the ctlr [mostly] tells us what to do next */
142 /* Start condition interrupt */
143 case MV64XXX_I2C_STATUS_MAST_START
: /* 0x08 */
144 case MV64XXX_I2C_STATUS_MAST_REPEAT_START
: /* 0x10 */
145 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_ADDR_1
;
146 drv_data
->state
= MV64XXX_I2C_STATE_WAITING_FOR_ADDR_1_ACK
;
149 /* Performing a write */
150 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_ACK
: /* 0x18 */
151 if (drv_data
->msg
->flags
& I2C_M_TEN
) {
152 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_ADDR_2
;
154 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK
;
158 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_2_ACK
: /* 0xd0 */
159 case MV64XXX_I2C_STATUS_MAST_WR_ACK
: /* 0x28 */
160 if ((drv_data
->bytes_left
== 0)
161 || (drv_data
->aborting
162 && (drv_data
->byte_posn
!= 0))) {
163 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_STOP
;
164 drv_data
->state
= MV64XXX_I2C_STATE_IDLE
;
166 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_DATA
;
168 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK
;
169 drv_data
->bytes_left
--;
173 /* Performing a read */
174 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_ACK
: /* 40 */
175 if (drv_data
->msg
->flags
& I2C_M_TEN
) {
176 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_ADDR_2
;
178 MV64XXX_I2C_STATE_WAITING_FOR_ADDR_2_ACK
;
182 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_2_ACK
: /* 0xe0 */
183 if (drv_data
->bytes_left
== 0) {
184 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_STOP
;
185 drv_data
->state
= MV64XXX_I2C_STATE_IDLE
;
189 case MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK
: /* 0x50 */
190 if (status
!= MV64XXX_I2C_STATUS_MAST_RD_DATA_ACK
)
191 drv_data
->action
= MV64XXX_I2C_ACTION_CONTINUE
;
193 drv_data
->action
= MV64XXX_I2C_ACTION_RCV_DATA
;
194 drv_data
->bytes_left
--;
196 drv_data
->state
= MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA
;
198 if ((drv_data
->bytes_left
== 1) || drv_data
->aborting
)
199 drv_data
->cntl_bits
&= ~MV64XXX_I2C_REG_CONTROL_ACK
;
202 case MV64XXX_I2C_STATUS_MAST_RD_DATA_NO_ACK
: /* 0x58 */
203 drv_data
->action
= MV64XXX_I2C_ACTION_RCV_DATA_STOP
;
204 drv_data
->state
= MV64XXX_I2C_STATE_IDLE
;
207 case MV64XXX_I2C_STATUS_MAST_WR_ADDR_NO_ACK
: /* 0x20 */
208 case MV64XXX_I2C_STATUS_MAST_WR_NO_ACK
: /* 30 */
209 case MV64XXX_I2C_STATUS_MAST_RD_ADDR_NO_ACK
: /* 48 */
210 /* Doesn't seem to be a device at other end */
211 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_STOP
;
212 drv_data
->state
= MV64XXX_I2C_STATE_IDLE
;
213 drv_data
->rc
= -ENODEV
;
217 dev_err(&drv_data
->adapter
.dev
,
218 "mv64xxx_i2c_fsm: Ctlr Error -- state: 0x%x, "
219 "status: 0x%x, addr: 0x%x, flags: 0x%x\n",
220 drv_data
->state
, status
, drv_data
->msg
->addr
,
221 drv_data
->msg
->flags
);
222 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_STOP
;
223 mv64xxx_i2c_hw_init(drv_data
);
229 mv64xxx_i2c_do_action(struct mv64xxx_i2c_data
*drv_data
)
231 switch(drv_data
->action
) {
232 case MV64XXX_I2C_ACTION_CONTINUE
:
233 writel(drv_data
->cntl_bits
,
234 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
237 case MV64XXX_I2C_ACTION_SEND_START
:
238 writel(drv_data
->cntl_bits
| MV64XXX_I2C_REG_CONTROL_START
,
239 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
242 case MV64XXX_I2C_ACTION_SEND_ADDR_1
:
243 writel(drv_data
->addr1
,
244 drv_data
->reg_base
+ MV64XXX_I2C_REG_DATA
);
245 writel(drv_data
->cntl_bits
,
246 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
249 case MV64XXX_I2C_ACTION_SEND_ADDR_2
:
250 writel(drv_data
->addr2
,
251 drv_data
->reg_base
+ MV64XXX_I2C_REG_DATA
);
252 writel(drv_data
->cntl_bits
,
253 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
256 case MV64XXX_I2C_ACTION_SEND_DATA
:
257 writel(drv_data
->msg
->buf
[drv_data
->byte_posn
++],
258 drv_data
->reg_base
+ MV64XXX_I2C_REG_DATA
);
259 writel(drv_data
->cntl_bits
,
260 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
263 case MV64XXX_I2C_ACTION_RCV_DATA
:
264 drv_data
->msg
->buf
[drv_data
->byte_posn
++] =
265 readl(drv_data
->reg_base
+ MV64XXX_I2C_REG_DATA
);
266 writel(drv_data
->cntl_bits
,
267 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
270 case MV64XXX_I2C_ACTION_RCV_DATA_STOP
:
271 drv_data
->msg
->buf
[drv_data
->byte_posn
++] =
272 readl(drv_data
->reg_base
+ MV64XXX_I2C_REG_DATA
);
273 drv_data
->cntl_bits
&= ~MV64XXX_I2C_REG_CONTROL_INTEN
;
274 writel(drv_data
->cntl_bits
| MV64XXX_I2C_REG_CONTROL_STOP
,
275 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
277 wake_up_interruptible(&drv_data
->waitq
);
280 case MV64XXX_I2C_ACTION_INVALID
:
282 dev_err(&drv_data
->adapter
.dev
,
283 "mv64xxx_i2c_do_action: Invalid action: %d\n",
287 case MV64XXX_I2C_ACTION_SEND_STOP
:
288 drv_data
->cntl_bits
&= ~MV64XXX_I2C_REG_CONTROL_INTEN
;
289 writel(drv_data
->cntl_bits
| MV64XXX_I2C_REG_CONTROL_STOP
,
290 drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
);
292 wake_up_interruptible(&drv_data
->waitq
);
298 mv64xxx_i2c_intr(int irq
, void *dev_id
)
300 struct mv64xxx_i2c_data
*drv_data
= dev_id
;
303 irqreturn_t rc
= IRQ_NONE
;
305 spin_lock_irqsave(&drv_data
->lock
, flags
);
306 while (readl(drv_data
->reg_base
+ MV64XXX_I2C_REG_CONTROL
) &
307 MV64XXX_I2C_REG_CONTROL_IFLG
) {
308 status
= readl(drv_data
->reg_base
+ MV64XXX_I2C_REG_STATUS
);
309 mv64xxx_i2c_fsm(drv_data
, status
);
310 mv64xxx_i2c_do_action(drv_data
);
313 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
319 *****************************************************************************
321 * I2C Msg Execution Routines
323 *****************************************************************************
326 mv64xxx_i2c_prepare_for_io(struct mv64xxx_i2c_data
*drv_data
,
332 drv_data
->byte_posn
= 0;
333 drv_data
->bytes_left
= msg
->len
;
334 drv_data
->aborting
= 0;
336 drv_data
->cntl_bits
= MV64XXX_I2C_REG_CONTROL_ACK
|
337 MV64XXX_I2C_REG_CONTROL_INTEN
| MV64XXX_I2C_REG_CONTROL_TWSIEN
;
339 if (msg
->flags
& I2C_M_RD
)
342 if (msg
->flags
& I2C_M_TEN
) {
343 drv_data
->addr1
= 0xf0 | (((u32
)msg
->addr
& 0x300) >> 7) | dir
;
344 drv_data
->addr2
= (u32
)msg
->addr
& 0xff;
346 drv_data
->addr1
= ((u32
)msg
->addr
& 0x7f) << 1 | dir
;
352 mv64xxx_i2c_wait_for_completion(struct mv64xxx_i2c_data
*drv_data
)
358 time_left
= wait_event_interruptible_timeout(drv_data
->waitq
,
359 !drv_data
->block
, drv_data
->adapter
.timeout
);
361 spin_lock_irqsave(&drv_data
->lock
, flags
);
362 if (!time_left
) { /* Timed out */
363 drv_data
->rc
= -ETIMEDOUT
;
365 } else if (time_left
< 0) { /* Interrupted/Error */
366 drv_data
->rc
= time_left
; /* errno value */
370 if (abort
&& drv_data
->block
) {
371 drv_data
->aborting
= 1;
372 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
374 time_left
= wait_event_timeout(drv_data
->waitq
,
375 !drv_data
->block
, drv_data
->adapter
.timeout
);
377 if ((time_left
<= 0) && drv_data
->block
) {
378 drv_data
->state
= MV64XXX_I2C_STATE_IDLE
;
379 dev_err(&drv_data
->adapter
.dev
,
380 "mv64xxx: I2C bus locked, block: %d, "
381 "time_left: %d\n", drv_data
->block
,
383 mv64xxx_i2c_hw_init(drv_data
);
386 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
390 mv64xxx_i2c_execute_msg(struct mv64xxx_i2c_data
*drv_data
, struct i2c_msg
*msg
)
394 spin_lock_irqsave(&drv_data
->lock
, flags
);
395 mv64xxx_i2c_prepare_for_io(drv_data
, msg
);
397 if (unlikely(msg
->flags
& I2C_M_NOSTART
)) { /* Skip start/addr phases */
398 if (drv_data
->msg
->flags
& I2C_M_RD
) {
399 /* No action to do, wait for slave to send a byte */
400 drv_data
->action
= MV64XXX_I2C_ACTION_CONTINUE
;
402 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_DATA
;
404 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_DATA
;
406 MV64XXX_I2C_STATE_WAITING_FOR_SLAVE_ACK
;
407 drv_data
->bytes_left
--;
410 drv_data
->action
= MV64XXX_I2C_ACTION_SEND_START
;
411 drv_data
->state
= MV64XXX_I2C_STATE_WAITING_FOR_START_COND
;
415 mv64xxx_i2c_do_action(drv_data
);
416 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
418 mv64xxx_i2c_wait_for_completion(drv_data
);
423 *****************************************************************************
425 * I2C Core Support Routines (Interface to higher level I2C code)
427 *****************************************************************************
430 mv64xxx_i2c_functionality(struct i2c_adapter
*adap
)
432 return I2C_FUNC_I2C
| I2C_FUNC_10BIT_ADDR
| I2C_FUNC_SMBUS_EMUL
;
436 mv64xxx_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msgs
[], int num
)
438 struct mv64xxx_i2c_data
*drv_data
= i2c_get_adapdata(adap
);
441 for (i
=0; i
<num
; i
++)
442 if ((rc
= mv64xxx_i2c_execute_msg(drv_data
, &msgs
[i
])) < 0)
448 static const struct i2c_algorithm mv64xxx_i2c_algo
= {
449 .master_xfer
= mv64xxx_i2c_xfer
,
450 .functionality
= mv64xxx_i2c_functionality
,
454 *****************************************************************************
456 * Driver Interface & Early Init Routines
458 *****************************************************************************
461 mv64xxx_i2c_map_regs(struct platform_device
*pd
,
462 struct mv64xxx_i2c_data
*drv_data
)
465 struct resource
*r
= platform_get_resource(pd
, IORESOURCE_MEM
, 0);
470 size
= resource_size(r
);
472 if (!request_mem_region(r
->start
, size
, drv_data
->adapter
.name
))
475 drv_data
->reg_base
= ioremap(r
->start
, size
);
476 drv_data
->reg_base_p
= r
->start
;
477 drv_data
->reg_size
= size
;
483 mv64xxx_i2c_unmap_regs(struct mv64xxx_i2c_data
*drv_data
)
485 if (drv_data
->reg_base
) {
486 iounmap(drv_data
->reg_base
);
487 release_mem_region(drv_data
->reg_base_p
, drv_data
->reg_size
);
490 drv_data
->reg_base
= NULL
;
491 drv_data
->reg_base_p
= 0;
495 mv64xxx_i2c_probe(struct platform_device
*pd
)
497 struct mv64xxx_i2c_data
*drv_data
;
498 struct mv64xxx_i2c_pdata
*pdata
= pd
->dev
.platform_data
;
501 if ((pd
->id
!= 0) || !pdata
)
504 drv_data
= kzalloc(sizeof(struct mv64xxx_i2c_data
), GFP_KERNEL
);
508 if (mv64xxx_i2c_map_regs(pd
, drv_data
)) {
513 strlcpy(drv_data
->adapter
.name
, MV64XXX_I2C_CTLR_NAME
" adapter",
514 sizeof(drv_data
->adapter
.name
));
516 init_waitqueue_head(&drv_data
->waitq
);
517 spin_lock_init(&drv_data
->lock
);
519 drv_data
->freq_m
= pdata
->freq_m
;
520 drv_data
->freq_n
= pdata
->freq_n
;
521 drv_data
->irq
= platform_get_irq(pd
, 0);
522 if (drv_data
->irq
< 0) {
524 goto exit_unmap_regs
;
526 drv_data
->adapter
.dev
.parent
= &pd
->dev
;
527 drv_data
->adapter
.algo
= &mv64xxx_i2c_algo
;
528 drv_data
->adapter
.owner
= THIS_MODULE
;
529 drv_data
->adapter
.class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
;
530 drv_data
->adapter
.timeout
= msecs_to_jiffies(pdata
->timeout
);
531 drv_data
->adapter
.nr
= pd
->id
;
532 platform_set_drvdata(pd
, drv_data
);
533 i2c_set_adapdata(&drv_data
->adapter
, drv_data
);
535 mv64xxx_i2c_hw_init(drv_data
);
537 if (request_irq(drv_data
->irq
, mv64xxx_i2c_intr
, 0,
538 MV64XXX_I2C_CTLR_NAME
, drv_data
)) {
539 dev_err(&drv_data
->adapter
.dev
,
540 "mv64xxx: Can't register intr handler irq: %d\n",
543 goto exit_unmap_regs
;
544 } else if ((rc
= i2c_add_numbered_adapter(&drv_data
->adapter
)) != 0) {
545 dev_err(&drv_data
->adapter
.dev
,
546 "mv64xxx: Can't add i2c adapter, rc: %d\n", -rc
);
553 free_irq(drv_data
->irq
, drv_data
);
555 mv64xxx_i2c_unmap_regs(drv_data
);
562 mv64xxx_i2c_remove(struct platform_device
*dev
)
564 struct mv64xxx_i2c_data
*drv_data
= platform_get_drvdata(dev
);
567 rc
= i2c_del_adapter(&drv_data
->adapter
);
568 free_irq(drv_data
->irq
, drv_data
);
569 mv64xxx_i2c_unmap_regs(drv_data
);
575 static struct platform_driver mv64xxx_i2c_driver
= {
576 .probe
= mv64xxx_i2c_probe
,
577 .remove
= __devexit_p(mv64xxx_i2c_remove
),
579 .owner
= THIS_MODULE
,
580 .name
= MV64XXX_I2C_CTLR_NAME
,
585 mv64xxx_i2c_init(void)
587 return platform_driver_register(&mv64xxx_i2c_driver
);
591 mv64xxx_i2c_exit(void)
593 platform_driver_unregister(&mv64xxx_i2c_driver
);
596 module_init(mv64xxx_i2c_init
);
597 module_exit(mv64xxx_i2c_exit
);
599 MODULE_AUTHOR("Mark A. Greer <mgreer@mvista.com>");
600 MODULE_DESCRIPTION("Marvell mv64xxx host bridge i2c ctlr driver");
601 MODULE_LICENSE("GPL");