2 * Aspeed 24XX/25XX I2C Controller.
4 * Copyright (C) 2012-2017 ASPEED Technology Inc.
5 * Copyright 2017 IBM Corporation
6 * Copyright 2017 Google, Inc.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/clk.h>
14 #include <linux/completion.h>
15 #include <linux/err.h>
16 #include <linux/errno.h>
17 #include <linux/i2c.h>
18 #include <linux/init.h>
19 #include <linux/interrupt.h>
21 #include <linux/irq.h>
22 #include <linux/irqchip/chained_irq.h>
23 #include <linux/irqdomain.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/of_address.h>
27 #include <linux/of_irq.h>
28 #include <linux/of_platform.h>
29 #include <linux/platform_device.h>
30 #include <linux/reset.h>
31 #include <linux/slab.h>
34 #define ASPEED_I2C_FUN_CTRL_REG 0x00
35 #define ASPEED_I2C_AC_TIMING_REG1 0x04
36 #define ASPEED_I2C_AC_TIMING_REG2 0x08
37 #define ASPEED_I2C_INTR_CTRL_REG 0x0c
38 #define ASPEED_I2C_INTR_STS_REG 0x10
39 #define ASPEED_I2C_CMD_REG 0x14
40 #define ASPEED_I2C_DEV_ADDR_REG 0x18
41 #define ASPEED_I2C_BYTE_BUF_REG 0x20
43 /* Global Register Definition */
44 /* 0x00 : I2C Interrupt Status Register */
45 /* 0x08 : I2C Interrupt Target Assignment */
47 /* Device Register Definition */
48 /* 0x00 : I2CD Function Control Register */
49 #define ASPEED_I2CD_MULTI_MASTER_DIS BIT(15)
50 #define ASPEED_I2CD_SDA_DRIVE_1T_EN BIT(8)
51 #define ASPEED_I2CD_M_SDA_DRIVE_1T_EN BIT(7)
52 #define ASPEED_I2CD_M_HIGH_SPEED_EN BIT(6)
53 #define ASPEED_I2CD_SLAVE_EN BIT(1)
54 #define ASPEED_I2CD_MASTER_EN BIT(0)
56 /* 0x04 : I2CD Clock and AC Timing Control Register #1 */
57 #define ASPEED_I2CD_TIME_TBUF_MASK GENMASK(31, 28)
58 #define ASPEED_I2CD_TIME_THDSTA_MASK GENMASK(27, 24)
59 #define ASPEED_I2CD_TIME_TACST_MASK GENMASK(23, 20)
60 #define ASPEED_I2CD_TIME_SCL_HIGH_SHIFT 16
61 #define ASPEED_I2CD_TIME_SCL_HIGH_MASK GENMASK(19, 16)
62 #define ASPEED_I2CD_TIME_SCL_LOW_SHIFT 12
63 #define ASPEED_I2CD_TIME_SCL_LOW_MASK GENMASK(15, 12)
64 #define ASPEED_I2CD_TIME_BASE_DIVISOR_MASK GENMASK(3, 0)
65 #define ASPEED_I2CD_TIME_SCL_REG_MAX GENMASK(3, 0)
66 /* 0x08 : I2CD Clock and AC Timing Control Register #2 */
67 #define ASPEED_NO_TIMEOUT_CTRL 0
69 /* 0x0c : I2CD Interrupt Control Register &
70 * 0x10 : I2CD Interrupt Status Register
72 * These share bit definitions, so use the same values for the enable &
75 #define ASPEED_I2CD_INTR_SDA_DL_TIMEOUT BIT(14)
76 #define ASPEED_I2CD_INTR_BUS_RECOVER_DONE BIT(13)
77 #define ASPEED_I2CD_INTR_SLAVE_MATCH BIT(7)
78 #define ASPEED_I2CD_INTR_SCL_TIMEOUT BIT(6)
79 #define ASPEED_I2CD_INTR_ABNORMAL BIT(5)
80 #define ASPEED_I2CD_INTR_NORMAL_STOP BIT(4)
81 #define ASPEED_I2CD_INTR_ARBIT_LOSS BIT(3)
82 #define ASPEED_I2CD_INTR_RX_DONE BIT(2)
83 #define ASPEED_I2CD_INTR_TX_NAK BIT(1)
84 #define ASPEED_I2CD_INTR_TX_ACK BIT(0)
85 #define ASPEED_I2CD_INTR_ALL \
86 (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT | \
87 ASPEED_I2CD_INTR_BUS_RECOVER_DONE | \
88 ASPEED_I2CD_INTR_SCL_TIMEOUT | \
89 ASPEED_I2CD_INTR_ABNORMAL | \
90 ASPEED_I2CD_INTR_NORMAL_STOP | \
91 ASPEED_I2CD_INTR_ARBIT_LOSS | \
92 ASPEED_I2CD_INTR_RX_DONE | \
93 ASPEED_I2CD_INTR_TX_NAK | \
94 ASPEED_I2CD_INTR_TX_ACK)
96 /* 0x14 : I2CD Command/Status Register */
97 #define ASPEED_I2CD_SCL_LINE_STS BIT(18)
98 #define ASPEED_I2CD_SDA_LINE_STS BIT(17)
99 #define ASPEED_I2CD_BUS_BUSY_STS BIT(16)
100 #define ASPEED_I2CD_BUS_RECOVER_CMD BIT(11)
103 #define ASPEED_I2CD_M_STOP_CMD BIT(5)
104 #define ASPEED_I2CD_M_S_RX_CMD_LAST BIT(4)
105 #define ASPEED_I2CD_M_RX_CMD BIT(3)
106 #define ASPEED_I2CD_S_TX_CMD BIT(2)
107 #define ASPEED_I2CD_M_TX_CMD BIT(1)
108 #define ASPEED_I2CD_M_START_CMD BIT(0)
110 /* 0x18 : I2CD Slave Device Address Register */
111 #define ASPEED_I2CD_DEV_ADDR_MASK GENMASK(6, 0)
113 enum aspeed_i2c_master_state
{
114 ASPEED_I2C_MASTER_INACTIVE
,
115 ASPEED_I2C_MASTER_START
,
116 ASPEED_I2C_MASTER_TX_FIRST
,
117 ASPEED_I2C_MASTER_TX
,
118 ASPEED_I2C_MASTER_RX_FIRST
,
119 ASPEED_I2C_MASTER_RX
,
120 ASPEED_I2C_MASTER_STOP
,
123 enum aspeed_i2c_slave_state
{
124 ASPEED_I2C_SLAVE_STOP
,
125 ASPEED_I2C_SLAVE_START
,
126 ASPEED_I2C_SLAVE_READ_REQUESTED
,
127 ASPEED_I2C_SLAVE_READ_PROCESSED
,
128 ASPEED_I2C_SLAVE_WRITE_REQUESTED
,
129 ASPEED_I2C_SLAVE_WRITE_RECEIVED
,
132 struct aspeed_i2c_bus
{
133 struct i2c_adapter adap
;
136 struct reset_control
*rst
;
137 /* Synchronizes I/O mem access to base. */
139 struct completion cmd_complete
;
140 u32 (*get_clk_reg_val
)(struct device
*dev
,
142 unsigned long parent_clk_frequency
;
144 /* Transaction state. */
145 enum aspeed_i2c_master_state master_state
;
146 struct i2c_msg
*msgs
;
152 /* Protected only by i2c_lock_bus */
153 int master_xfer_result
;
154 #if IS_ENABLED(CONFIG_I2C_SLAVE)
155 struct i2c_client
*slave
;
156 enum aspeed_i2c_slave_state slave_state
;
157 #endif /* CONFIG_I2C_SLAVE */
160 static int aspeed_i2c_reset(struct aspeed_i2c_bus
*bus
);
162 static int aspeed_i2c_recover_bus(struct aspeed_i2c_bus
*bus
)
164 unsigned long time_left
, flags
;
168 spin_lock_irqsave(&bus
->lock
, flags
);
169 command
= readl(bus
->base
+ ASPEED_I2C_CMD_REG
);
171 if (command
& ASPEED_I2CD_SDA_LINE_STS
) {
172 /* Bus is idle: no recovery needed. */
173 if (command
& ASPEED_I2CD_SCL_LINE_STS
)
175 dev_dbg(bus
->dev
, "SCL hung (state %x), attempting recovery\n",
178 reinit_completion(&bus
->cmd_complete
);
179 writel(ASPEED_I2CD_M_STOP_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
180 spin_unlock_irqrestore(&bus
->lock
, flags
);
182 time_left
= wait_for_completion_timeout(
183 &bus
->cmd_complete
, bus
->adap
.timeout
);
185 spin_lock_irqsave(&bus
->lock
, flags
);
188 else if (bus
->cmd_err
)
190 /* Recovery failed. */
191 else if (!(readl(bus
->base
+ ASPEED_I2C_CMD_REG
) &
192 ASPEED_I2CD_SCL_LINE_STS
))
196 dev_dbg(bus
->dev
, "SDA hung (state %x), attempting recovery\n",
199 reinit_completion(&bus
->cmd_complete
);
200 /* Writes 1 to 8 SCL clock cycles until SDA is released. */
201 writel(ASPEED_I2CD_BUS_RECOVER_CMD
,
202 bus
->base
+ ASPEED_I2C_CMD_REG
);
203 spin_unlock_irqrestore(&bus
->lock
, flags
);
205 time_left
= wait_for_completion_timeout(
206 &bus
->cmd_complete
, bus
->adap
.timeout
);
208 spin_lock_irqsave(&bus
->lock
, flags
);
211 else if (bus
->cmd_err
)
213 /* Recovery failed. */
214 else if (!(readl(bus
->base
+ ASPEED_I2C_CMD_REG
) &
215 ASPEED_I2CD_SDA_LINE_STS
))
220 spin_unlock_irqrestore(&bus
->lock
, flags
);
225 spin_unlock_irqrestore(&bus
->lock
, flags
);
227 return aspeed_i2c_reset(bus
);
230 #if IS_ENABLED(CONFIG_I2C_SLAVE)
231 static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus
*bus
)
233 u32 command
, irq_status
, status_ack
= 0;
234 struct i2c_client
*slave
= bus
->slave
;
235 bool irq_handled
= true;
243 command
= readl(bus
->base
+ ASPEED_I2C_CMD_REG
);
244 irq_status
= readl(bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
246 /* Slave was requested, restart state machine. */
247 if (irq_status
& ASPEED_I2CD_INTR_SLAVE_MATCH
) {
248 status_ack
|= ASPEED_I2CD_INTR_SLAVE_MATCH
;
249 bus
->slave_state
= ASPEED_I2C_SLAVE_START
;
252 /* Slave is not currently active, irq was for someone else. */
253 if (bus
->slave_state
== ASPEED_I2C_SLAVE_STOP
) {
258 dev_dbg(bus
->dev
, "slave irq status 0x%08x, cmd 0x%08x\n",
259 irq_status
, command
);
261 /* Slave was sent something. */
262 if (irq_status
& ASPEED_I2CD_INTR_RX_DONE
) {
263 value
= readl(bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
) >> 8;
264 /* Handle address frame. */
265 if (bus
->slave_state
== ASPEED_I2C_SLAVE_START
) {
268 ASPEED_I2C_SLAVE_READ_REQUESTED
;
271 ASPEED_I2C_SLAVE_WRITE_REQUESTED
;
273 status_ack
|= ASPEED_I2CD_INTR_RX_DONE
;
276 /* Slave was asked to stop. */
277 if (irq_status
& ASPEED_I2CD_INTR_NORMAL_STOP
) {
278 status_ack
|= ASPEED_I2CD_INTR_NORMAL_STOP
;
279 bus
->slave_state
= ASPEED_I2C_SLAVE_STOP
;
281 if (irq_status
& ASPEED_I2CD_INTR_TX_NAK
) {
282 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
283 bus
->slave_state
= ASPEED_I2C_SLAVE_STOP
;
286 switch (bus
->slave_state
) {
287 case ASPEED_I2C_SLAVE_READ_REQUESTED
:
288 if (irq_status
& ASPEED_I2CD_INTR_TX_ACK
)
289 dev_err(bus
->dev
, "Unexpected ACK on read request.\n");
290 bus
->slave_state
= ASPEED_I2C_SLAVE_READ_PROCESSED
;
292 i2c_slave_event(slave
, I2C_SLAVE_READ_REQUESTED
, &value
);
293 writel(value
, bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
294 writel(ASPEED_I2CD_S_TX_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
296 case ASPEED_I2C_SLAVE_READ_PROCESSED
:
297 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
298 if (!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))
300 "Expected ACK after processed read.\n");
301 i2c_slave_event(slave
, I2C_SLAVE_READ_PROCESSED
, &value
);
302 writel(value
, bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
303 writel(ASPEED_I2CD_S_TX_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
305 case ASPEED_I2C_SLAVE_WRITE_REQUESTED
:
306 bus
->slave_state
= ASPEED_I2C_SLAVE_WRITE_RECEIVED
;
307 i2c_slave_event(slave
, I2C_SLAVE_WRITE_REQUESTED
, &value
);
309 case ASPEED_I2C_SLAVE_WRITE_RECEIVED
:
310 i2c_slave_event(slave
, I2C_SLAVE_WRITE_RECEIVED
, &value
);
312 case ASPEED_I2C_SLAVE_STOP
:
313 i2c_slave_event(slave
, I2C_SLAVE_STOP
, &value
);
316 dev_err(bus
->dev
, "unhandled slave_state: %d\n",
321 if (status_ack
!= irq_status
)
323 "irq handled != irq. expected %x, but was %x\n",
324 irq_status
, status_ack
);
325 writel(status_ack
, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
330 #endif /* CONFIG_I2C_SLAVE */
332 /* precondition: bus.lock has been acquired. */
333 static void aspeed_i2c_do_start(struct aspeed_i2c_bus
*bus
)
335 u32 command
= ASPEED_I2CD_M_START_CMD
| ASPEED_I2CD_M_TX_CMD
;
336 struct i2c_msg
*msg
= &bus
->msgs
[bus
->msgs_index
];
337 u8 slave_addr
= i2c_8bit_addr_from_msg(msg
);
339 bus
->master_state
= ASPEED_I2C_MASTER_START
;
342 if (msg
->flags
& I2C_M_RD
) {
343 command
|= ASPEED_I2CD_M_RX_CMD
;
344 /* Need to let the hardware know to NACK after RX. */
345 if (msg
->len
== 1 && !(msg
->flags
& I2C_M_RECV_LEN
))
346 command
|= ASPEED_I2CD_M_S_RX_CMD_LAST
;
349 writel(slave_addr
, bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
350 writel(command
, bus
->base
+ ASPEED_I2C_CMD_REG
);
353 /* precondition: bus.lock has been acquired. */
354 static void aspeed_i2c_do_stop(struct aspeed_i2c_bus
*bus
)
356 bus
->master_state
= ASPEED_I2C_MASTER_STOP
;
357 writel(ASPEED_I2CD_M_STOP_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
360 /* precondition: bus.lock has been acquired. */
361 static void aspeed_i2c_next_msg_or_stop(struct aspeed_i2c_bus
*bus
)
363 if (bus
->msgs_index
+ 1 < bus
->msgs_count
) {
365 aspeed_i2c_do_start(bus
);
367 aspeed_i2c_do_stop(bus
);
371 static int aspeed_i2c_is_irq_error(u32 irq_status
)
373 if (irq_status
& ASPEED_I2CD_INTR_ARBIT_LOSS
)
375 if (irq_status
& (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT
|
376 ASPEED_I2CD_INTR_SCL_TIMEOUT
))
378 if (irq_status
& (ASPEED_I2CD_INTR_ABNORMAL
))
384 static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus
*bus
)
386 u32 irq_status
, status_ack
= 0, command
= 0;
391 irq_status
= readl(bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
392 /* Ack all interrupt bits. */
393 writel(irq_status
, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
395 if (irq_status
& ASPEED_I2CD_INTR_BUS_RECOVER_DONE
) {
396 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
397 status_ack
|= ASPEED_I2CD_INTR_BUS_RECOVER_DONE
;
402 * We encountered an interrupt that reports an error: the hardware
403 * should clear the command queue effectively taking us back to the
406 ret
= aspeed_i2c_is_irq_error(irq_status
);
408 dev_dbg(bus
->dev
, "received error interrupt: 0x%08x\n",
411 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
415 /* We are in an invalid state; reset bus to a known state. */
417 dev_err(bus
->dev
, "bus in unknown state\n");
419 if (bus
->master_state
!= ASPEED_I2C_MASTER_STOP
)
420 aspeed_i2c_do_stop(bus
);
421 goto out_no_complete
;
423 msg
= &bus
->msgs
[bus
->msgs_index
];
426 * START is a special case because we still have to handle a subsequent
427 * TX or RX immediately after we handle it, so we handle it here and
428 * then update the state and handle the new state below.
430 if (bus
->master_state
== ASPEED_I2C_MASTER_START
) {
431 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))) {
432 pr_devel("no slave present at %02x\n", msg
->addr
);
433 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
434 bus
->cmd_err
= -ENXIO
;
435 aspeed_i2c_do_stop(bus
);
436 goto out_no_complete
;
438 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
439 if (msg
->len
== 0) { /* SMBUS_QUICK */
440 aspeed_i2c_do_stop(bus
);
441 goto out_no_complete
;
443 if (msg
->flags
& I2C_M_RD
)
444 bus
->master_state
= ASPEED_I2C_MASTER_RX_FIRST
;
446 bus
->master_state
= ASPEED_I2C_MASTER_TX_FIRST
;
449 switch (bus
->master_state
) {
450 case ASPEED_I2C_MASTER_TX
:
451 if (unlikely(irq_status
& ASPEED_I2CD_INTR_TX_NAK
)) {
452 dev_dbg(bus
->dev
, "slave NACKed TX\n");
453 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
455 } else if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))) {
456 dev_err(bus
->dev
, "slave failed to ACK TX\n");
459 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
460 /* fallthrough intended */
461 case ASPEED_I2C_MASTER_TX_FIRST
:
462 if (bus
->buf_index
< msg
->len
) {
463 bus
->master_state
= ASPEED_I2C_MASTER_TX
;
464 writel(msg
->buf
[bus
->buf_index
++],
465 bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
466 writel(ASPEED_I2CD_M_TX_CMD
,
467 bus
->base
+ ASPEED_I2C_CMD_REG
);
469 aspeed_i2c_next_msg_or_stop(bus
);
471 goto out_no_complete
;
472 case ASPEED_I2C_MASTER_RX_FIRST
:
473 /* RX may not have completed yet (only address cycle) */
474 if (!(irq_status
& ASPEED_I2CD_INTR_RX_DONE
))
475 goto out_no_complete
;
476 /* fallthrough intended */
477 case ASPEED_I2C_MASTER_RX
:
478 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_RX_DONE
))) {
479 dev_err(bus
->dev
, "master failed to RX\n");
482 status_ack
|= ASPEED_I2CD_INTR_RX_DONE
;
484 recv_byte
= readl(bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
) >> 8;
485 msg
->buf
[bus
->buf_index
++] = recv_byte
;
487 if (msg
->flags
& I2C_M_RECV_LEN
) {
488 if (unlikely(recv_byte
> I2C_SMBUS_BLOCK_MAX
)) {
489 bus
->cmd_err
= -EPROTO
;
490 aspeed_i2c_do_stop(bus
);
491 goto out_no_complete
;
493 msg
->len
= recv_byte
+
494 ((msg
->flags
& I2C_CLIENT_PEC
) ? 2 : 1);
495 msg
->flags
&= ~I2C_M_RECV_LEN
;
498 if (bus
->buf_index
< msg
->len
) {
499 bus
->master_state
= ASPEED_I2C_MASTER_RX
;
500 command
= ASPEED_I2CD_M_RX_CMD
;
501 if (bus
->buf_index
+ 1 == msg
->len
)
502 command
|= ASPEED_I2CD_M_S_RX_CMD_LAST
;
503 writel(command
, bus
->base
+ ASPEED_I2C_CMD_REG
);
505 aspeed_i2c_next_msg_or_stop(bus
);
507 goto out_no_complete
;
508 case ASPEED_I2C_MASTER_STOP
:
509 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_NORMAL_STOP
))) {
510 dev_err(bus
->dev
, "master failed to STOP\n");
512 /* Do not STOP as we have already tried. */
514 status_ack
|= ASPEED_I2CD_INTR_NORMAL_STOP
;
517 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
519 case ASPEED_I2C_MASTER_INACTIVE
:
521 "master received interrupt 0x%08x, but is inactive\n",
524 /* Do not STOP as we should be inactive. */
527 WARN(1, "unknown master state\n");
528 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
529 bus
->cmd_err
= -EINVAL
;
534 aspeed_i2c_do_stop(bus
);
535 goto out_no_complete
;
539 bus
->master_xfer_result
= bus
->cmd_err
;
541 bus
->master_xfer_result
= bus
->msgs_index
+ 1;
542 complete(&bus
->cmd_complete
);
544 if (irq_status
!= status_ack
)
546 "irq handled != irq. expected 0x%08x, but was 0x%08x\n",
547 irq_status
, status_ack
);
551 static irqreturn_t
aspeed_i2c_bus_irq(int irq
, void *dev_id
)
553 struct aspeed_i2c_bus
*bus
= dev_id
;
556 spin_lock(&bus
->lock
);
558 #if IS_ENABLED(CONFIG_I2C_SLAVE)
559 if (IS_ENABLED(CONFIG_I2C_SLAVE
) && aspeed_i2c_slave_irq(bus
)) {
560 dev_dbg(bus
->dev
, "irq handled by slave.\n");
564 #endif /* CONFIG_I2C_SLAVE */
566 ret
= aspeed_i2c_master_irq(bus
);
568 #if IS_ENABLED(CONFIG_I2C_SLAVE)
571 spin_unlock(&bus
->lock
);
572 return ret
? IRQ_HANDLED
: IRQ_NONE
;
575 static int aspeed_i2c_master_xfer(struct i2c_adapter
*adap
,
576 struct i2c_msg
*msgs
, int num
)
578 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(adap
);
579 unsigned long time_left
, flags
;
582 spin_lock_irqsave(&bus
->lock
, flags
);
585 /* If bus is busy, attempt recovery. We assume a single master
588 if (readl(bus
->base
+ ASPEED_I2C_CMD_REG
) & ASPEED_I2CD_BUS_BUSY_STS
) {
589 spin_unlock_irqrestore(&bus
->lock
, flags
);
590 ret
= aspeed_i2c_recover_bus(bus
);
593 spin_lock_irqsave(&bus
->lock
, flags
);
599 bus
->msgs_count
= num
;
601 reinit_completion(&bus
->cmd_complete
);
602 aspeed_i2c_do_start(bus
);
603 spin_unlock_irqrestore(&bus
->lock
, flags
);
605 time_left
= wait_for_completion_timeout(&bus
->cmd_complete
,
611 return bus
->master_xfer_result
;
614 static u32
aspeed_i2c_functionality(struct i2c_adapter
*adap
)
616 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_SMBUS_BLOCK_DATA
;
619 #if IS_ENABLED(CONFIG_I2C_SLAVE)
620 /* precondition: bus.lock has been acquired. */
621 static void __aspeed_i2c_reg_slave(struct aspeed_i2c_bus
*bus
, u16 slave_addr
)
623 u32 addr_reg_val
, func_ctrl_reg_val
;
625 /* Set slave addr. */
626 addr_reg_val
= readl(bus
->base
+ ASPEED_I2C_DEV_ADDR_REG
);
627 addr_reg_val
&= ~ASPEED_I2CD_DEV_ADDR_MASK
;
628 addr_reg_val
|= slave_addr
& ASPEED_I2CD_DEV_ADDR_MASK
;
629 writel(addr_reg_val
, bus
->base
+ ASPEED_I2C_DEV_ADDR_REG
);
631 /* Turn on slave mode. */
632 func_ctrl_reg_val
= readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
633 func_ctrl_reg_val
|= ASPEED_I2CD_SLAVE_EN
;
634 writel(func_ctrl_reg_val
, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
637 static int aspeed_i2c_reg_slave(struct i2c_client
*client
)
639 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(client
->adapter
);
642 spin_lock_irqsave(&bus
->lock
, flags
);
644 spin_unlock_irqrestore(&bus
->lock
, flags
);
648 __aspeed_i2c_reg_slave(bus
, client
->addr
);
651 bus
->slave_state
= ASPEED_I2C_SLAVE_STOP
;
652 spin_unlock_irqrestore(&bus
->lock
, flags
);
657 static int aspeed_i2c_unreg_slave(struct i2c_client
*client
)
659 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(client
->adapter
);
660 u32 func_ctrl_reg_val
;
663 spin_lock_irqsave(&bus
->lock
, flags
);
665 spin_unlock_irqrestore(&bus
->lock
, flags
);
669 /* Turn off slave mode. */
670 func_ctrl_reg_val
= readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
671 func_ctrl_reg_val
&= ~ASPEED_I2CD_SLAVE_EN
;
672 writel(func_ctrl_reg_val
, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
675 spin_unlock_irqrestore(&bus
->lock
, flags
);
679 #endif /* CONFIG_I2C_SLAVE */
681 static const struct i2c_algorithm aspeed_i2c_algo
= {
682 .master_xfer
= aspeed_i2c_master_xfer
,
683 .functionality
= aspeed_i2c_functionality
,
684 #if IS_ENABLED(CONFIG_I2C_SLAVE)
685 .reg_slave
= aspeed_i2c_reg_slave
,
686 .unreg_slave
= aspeed_i2c_unreg_slave
,
687 #endif /* CONFIG_I2C_SLAVE */
690 static u32
aspeed_i2c_get_clk_reg_val(struct device
*dev
,
691 u32 clk_high_low_mask
,
694 u32 base_clk_divisor
, clk_high_low_max
, clk_high
, clk_low
, tmp
;
697 * SCL_high and SCL_low represent a value 1 greater than what is stored
698 * since a zero divider is meaningless. Thus, the max value each can
699 * store is every bit set + 1. Since SCL_high and SCL_low are added
700 * together (see below), the max value of both is the max value of one
703 clk_high_low_max
= (clk_high_low_mask
+ 1) * 2;
706 * The actual clock frequency of SCL is:
707 * SCL_freq = APB_freq / (base_freq * (SCL_high + SCL_low))
708 * = APB_freq / divisor
709 * where base_freq is a programmable clock divider; its value is
710 * base_freq = 1 << base_clk_divisor
711 * SCL_high is the number of base_freq clock cycles that SCL stays high
712 * and SCL_low is the number of base_freq clock cycles that SCL stays
713 * low for a period of SCL.
714 * The actual register has a minimum SCL_high and SCL_low minimum of 1;
715 * thus, they start counting at zero. So
716 * SCL_high = clk_high + 1
717 * SCL_low = clk_low + 1
719 * SCL_freq = APB_freq /
720 * ((1 << base_clk_divisor) * (clk_high + 1 + clk_low + 1))
721 * The documentation recommends clk_high >= clk_high_max / 2 and
722 * clk_low >= clk_low_max / 2 - 1 when possible; this last constraint
723 * gives us the following solution:
725 base_clk_divisor
= divisor
> clk_high_low_max
?
726 ilog2((divisor
- 1) / clk_high_low_max
) + 1 : 0;
728 if (base_clk_divisor
> ASPEED_I2CD_TIME_BASE_DIVISOR_MASK
) {
729 base_clk_divisor
= ASPEED_I2CD_TIME_BASE_DIVISOR_MASK
;
730 clk_low
= clk_high_low_mask
;
731 clk_high
= clk_high_low_mask
;
733 "clamping clock divider: divider requested, %u, is greater than largest possible divider, %u.\n",
734 divisor
, (1 << base_clk_divisor
) * clk_high_low_max
);
736 tmp
= (divisor
+ (1 << base_clk_divisor
) - 1)
739 clk_high
= tmp
- clk_low
;
749 return ((clk_high
<< ASPEED_I2CD_TIME_SCL_HIGH_SHIFT
)
750 & ASPEED_I2CD_TIME_SCL_HIGH_MASK
)
751 | ((clk_low
<< ASPEED_I2CD_TIME_SCL_LOW_SHIFT
)
752 & ASPEED_I2CD_TIME_SCL_LOW_MASK
)
754 & ASPEED_I2CD_TIME_BASE_DIVISOR_MASK
);
757 static u32
aspeed_i2c_24xx_get_clk_reg_val(struct device
*dev
, u32 divisor
)
760 * clk_high and clk_low are each 3 bits wide, so each can hold a max
761 * value of 8 giving a clk_high_low_max of 16.
763 return aspeed_i2c_get_clk_reg_val(dev
, GENMASK(2, 0), divisor
);
766 static u32
aspeed_i2c_25xx_get_clk_reg_val(struct device
*dev
, u32 divisor
)
769 * clk_high and clk_low are each 4 bits wide, so each can hold a max
770 * value of 16 giving a clk_high_low_max of 32.
772 return aspeed_i2c_get_clk_reg_val(dev
, GENMASK(3, 0), divisor
);
775 /* precondition: bus.lock has been acquired. */
776 static int aspeed_i2c_init_clk(struct aspeed_i2c_bus
*bus
)
778 u32 divisor
, clk_reg_val
;
780 divisor
= DIV_ROUND_UP(bus
->parent_clk_frequency
, bus
->bus_frequency
);
781 clk_reg_val
= readl(bus
->base
+ ASPEED_I2C_AC_TIMING_REG1
);
782 clk_reg_val
&= (ASPEED_I2CD_TIME_TBUF_MASK
|
783 ASPEED_I2CD_TIME_THDSTA_MASK
|
784 ASPEED_I2CD_TIME_TACST_MASK
);
785 clk_reg_val
|= bus
->get_clk_reg_val(bus
->dev
, divisor
);
786 writel(clk_reg_val
, bus
->base
+ ASPEED_I2C_AC_TIMING_REG1
);
787 writel(ASPEED_NO_TIMEOUT_CTRL
, bus
->base
+ ASPEED_I2C_AC_TIMING_REG2
);
792 /* precondition: bus.lock has been acquired. */
793 static int aspeed_i2c_init(struct aspeed_i2c_bus
*bus
,
794 struct platform_device
*pdev
)
796 u32 fun_ctrl_reg
= ASPEED_I2CD_MASTER_EN
;
799 /* Disable everything. */
800 writel(0, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
802 ret
= aspeed_i2c_init_clk(bus
);
806 if (!of_property_read_bool(pdev
->dev
.of_node
, "multi-master"))
807 fun_ctrl_reg
|= ASPEED_I2CD_MULTI_MASTER_DIS
;
809 /* Enable Master Mode */
810 writel(readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
) | fun_ctrl_reg
,
811 bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
813 #if IS_ENABLED(CONFIG_I2C_SLAVE)
814 /* If slave has already been registered, re-enable it. */
816 __aspeed_i2c_reg_slave(bus
, bus
->slave
->addr
);
817 #endif /* CONFIG_I2C_SLAVE */
819 /* Set interrupt generation of I2C controller */
820 writel(ASPEED_I2CD_INTR_ALL
, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
825 static int aspeed_i2c_reset(struct aspeed_i2c_bus
*bus
)
827 struct platform_device
*pdev
= to_platform_device(bus
->dev
);
831 spin_lock_irqsave(&bus
->lock
, flags
);
833 /* Disable and ack all interrupts. */
834 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
835 writel(0xffffffff, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
837 ret
= aspeed_i2c_init(bus
, pdev
);
839 spin_unlock_irqrestore(&bus
->lock
, flags
);
844 static const struct of_device_id aspeed_i2c_bus_of_table
[] = {
846 .compatible
= "aspeed,ast2400-i2c-bus",
847 .data
= aspeed_i2c_24xx_get_clk_reg_val
,
850 .compatible
= "aspeed,ast2500-i2c-bus",
851 .data
= aspeed_i2c_25xx_get_clk_reg_val
,
855 MODULE_DEVICE_TABLE(of
, aspeed_i2c_bus_of_table
);
857 static int aspeed_i2c_probe_bus(struct platform_device
*pdev
)
859 const struct of_device_id
*match
;
860 struct aspeed_i2c_bus
*bus
;
861 struct clk
*parent_clk
;
862 struct resource
*res
;
865 bus
= devm_kzalloc(&pdev
->dev
, sizeof(*bus
), GFP_KERNEL
);
869 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
870 bus
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
871 if (IS_ERR(bus
->base
))
872 return PTR_ERR(bus
->base
);
874 parent_clk
= devm_clk_get(&pdev
->dev
, NULL
);
875 if (IS_ERR(parent_clk
))
876 return PTR_ERR(parent_clk
);
877 bus
->parent_clk_frequency
= clk_get_rate(parent_clk
);
878 /* We just need the clock rate, we don't actually use the clk object. */
879 devm_clk_put(&pdev
->dev
, parent_clk
);
881 bus
->rst
= devm_reset_control_get_shared(&pdev
->dev
, NULL
);
882 if (IS_ERR(bus
->rst
)) {
884 "missing or invalid reset controller device tree entry\n");
885 return PTR_ERR(bus
->rst
);
887 reset_control_deassert(bus
->rst
);
889 ret
= of_property_read_u32(pdev
->dev
.of_node
,
890 "bus-frequency", &bus
->bus_frequency
);
893 "Could not read bus-frequency property\n");
894 bus
->bus_frequency
= 100000;
897 match
= of_match_node(aspeed_i2c_bus_of_table
, pdev
->dev
.of_node
);
899 bus
->get_clk_reg_val
= aspeed_i2c_24xx_get_clk_reg_val
;
901 bus
->get_clk_reg_val
= (u32 (*)(struct device
*, u32
))
904 /* Initialize the I2C adapter */
905 spin_lock_init(&bus
->lock
);
906 init_completion(&bus
->cmd_complete
);
907 bus
->adap
.owner
= THIS_MODULE
;
908 bus
->adap
.retries
= 0;
909 bus
->adap
.timeout
= 5 * HZ
;
910 bus
->adap
.algo
= &aspeed_i2c_algo
;
911 bus
->adap
.dev
.parent
= &pdev
->dev
;
912 bus
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
913 strlcpy(bus
->adap
.name
, pdev
->name
, sizeof(bus
->adap
.name
));
914 i2c_set_adapdata(&bus
->adap
, bus
);
916 bus
->dev
= &pdev
->dev
;
918 /* Clean up any left over interrupt state. */
919 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
920 writel(0xffffffff, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
922 * bus.lock does not need to be held because the interrupt handler has
923 * not been enabled yet.
925 ret
= aspeed_i2c_init(bus
, pdev
);
929 irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
930 ret
= devm_request_irq(&pdev
->dev
, irq
, aspeed_i2c_bus_irq
,
931 0, dev_name(&pdev
->dev
), bus
);
935 ret
= i2c_add_adapter(&bus
->adap
);
939 platform_set_drvdata(pdev
, bus
);
941 dev_info(bus
->dev
, "i2c bus %d registered, irq %d\n",
947 static int aspeed_i2c_remove_bus(struct platform_device
*pdev
)
949 struct aspeed_i2c_bus
*bus
= platform_get_drvdata(pdev
);
952 spin_lock_irqsave(&bus
->lock
, flags
);
954 /* Disable everything. */
955 writel(0, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
956 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
958 spin_unlock_irqrestore(&bus
->lock
, flags
);
960 reset_control_assert(bus
->rst
);
962 i2c_del_adapter(&bus
->adap
);
967 static struct platform_driver aspeed_i2c_bus_driver
= {
968 .probe
= aspeed_i2c_probe_bus
,
969 .remove
= aspeed_i2c_remove_bus
,
971 .name
= "aspeed-i2c-bus",
972 .of_match_table
= aspeed_i2c_bus_of_table
,
975 module_platform_driver(aspeed_i2c_bus_driver
);
977 MODULE_AUTHOR("Brendan Higgins <brendanhiggins@google.com>");
978 MODULE_DESCRIPTION("Aspeed I2C Bus Driver");
979 MODULE_LICENSE("GPL v2");