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_START
,
115 ASPEED_I2C_MASTER_TX_FIRST
,
116 ASPEED_I2C_MASTER_TX
,
117 ASPEED_I2C_MASTER_RX_FIRST
,
118 ASPEED_I2C_MASTER_RX
,
119 ASPEED_I2C_MASTER_STOP
,
120 ASPEED_I2C_MASTER_INACTIVE
,
123 enum aspeed_i2c_slave_state
{
124 ASPEED_I2C_SLAVE_START
,
125 ASPEED_I2C_SLAVE_READ_REQUESTED
,
126 ASPEED_I2C_SLAVE_READ_PROCESSED
,
127 ASPEED_I2C_SLAVE_WRITE_REQUESTED
,
128 ASPEED_I2C_SLAVE_WRITE_RECEIVED
,
129 ASPEED_I2C_SLAVE_STOP
,
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
)(u32 divisor
);
141 unsigned long parent_clk_frequency
;
143 /* Transaction state. */
144 enum aspeed_i2c_master_state master_state
;
145 struct i2c_msg
*msgs
;
151 /* Protected only by i2c_lock_bus */
152 int master_xfer_result
;
153 #if IS_ENABLED(CONFIG_I2C_SLAVE)
154 struct i2c_client
*slave
;
155 enum aspeed_i2c_slave_state slave_state
;
156 #endif /* CONFIG_I2C_SLAVE */
159 static int aspeed_i2c_reset(struct aspeed_i2c_bus
*bus
);
161 static int aspeed_i2c_recover_bus(struct aspeed_i2c_bus
*bus
)
163 unsigned long time_left
, flags
;
167 spin_lock_irqsave(&bus
->lock
, flags
);
168 command
= readl(bus
->base
+ ASPEED_I2C_CMD_REG
);
170 if (command
& ASPEED_I2CD_SDA_LINE_STS
) {
171 /* Bus is idle: no recovery needed. */
172 if (command
& ASPEED_I2CD_SCL_LINE_STS
)
174 dev_dbg(bus
->dev
, "SCL hung (state %x), attempting recovery\n",
177 reinit_completion(&bus
->cmd_complete
);
178 writel(ASPEED_I2CD_M_STOP_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
179 spin_unlock_irqrestore(&bus
->lock
, flags
);
181 time_left
= wait_for_completion_timeout(
182 &bus
->cmd_complete
, bus
->adap
.timeout
);
184 spin_lock_irqsave(&bus
->lock
, flags
);
187 else if (bus
->cmd_err
)
189 /* Recovery failed. */
190 else if (!(readl(bus
->base
+ ASPEED_I2C_CMD_REG
) &
191 ASPEED_I2CD_SCL_LINE_STS
))
195 dev_dbg(bus
->dev
, "SDA hung (state %x), attempting recovery\n",
198 reinit_completion(&bus
->cmd_complete
);
199 /* Writes 1 to 8 SCL clock cycles until SDA is released. */
200 writel(ASPEED_I2CD_BUS_RECOVER_CMD
,
201 bus
->base
+ ASPEED_I2C_CMD_REG
);
202 spin_unlock_irqrestore(&bus
->lock
, flags
);
204 time_left
= wait_for_completion_timeout(
205 &bus
->cmd_complete
, bus
->adap
.timeout
);
207 spin_lock_irqsave(&bus
->lock
, flags
);
210 else if (bus
->cmd_err
)
212 /* Recovery failed. */
213 else if (!(readl(bus
->base
+ ASPEED_I2C_CMD_REG
) &
214 ASPEED_I2CD_SDA_LINE_STS
))
219 spin_unlock_irqrestore(&bus
->lock
, flags
);
224 spin_unlock_irqrestore(&bus
->lock
, flags
);
226 return aspeed_i2c_reset(bus
);
229 #if IS_ENABLED(CONFIG_I2C_SLAVE)
230 static bool aspeed_i2c_slave_irq(struct aspeed_i2c_bus
*bus
)
232 u32 command
, irq_status
, status_ack
= 0;
233 struct i2c_client
*slave
= bus
->slave
;
234 bool irq_handled
= true;
237 spin_lock(&bus
->lock
);
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
);
328 spin_unlock(&bus
->lock
);
331 #endif /* CONFIG_I2C_SLAVE */
333 /* precondition: bus.lock has been acquired. */
334 static void aspeed_i2c_do_start(struct aspeed_i2c_bus
*bus
)
336 u32 command
= ASPEED_I2CD_M_START_CMD
| ASPEED_I2CD_M_TX_CMD
;
337 struct i2c_msg
*msg
= &bus
->msgs
[bus
->msgs_index
];
338 u8 slave_addr
= msg
->addr
<< 1;
340 bus
->master_state
= ASPEED_I2C_MASTER_START
;
343 if (msg
->flags
& I2C_M_RD
) {
345 command
|= ASPEED_I2CD_M_RX_CMD
;
346 /* Need to let the hardware know to NACK after RX. */
347 if (msg
->len
== 1 && !(msg
->flags
& I2C_M_RECV_LEN
))
348 command
|= ASPEED_I2CD_M_S_RX_CMD_LAST
;
351 writel(slave_addr
, bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
352 writel(command
, bus
->base
+ ASPEED_I2C_CMD_REG
);
355 /* precondition: bus.lock has been acquired. */
356 static void aspeed_i2c_do_stop(struct aspeed_i2c_bus
*bus
)
358 bus
->master_state
= ASPEED_I2C_MASTER_STOP
;
359 writel(ASPEED_I2CD_M_STOP_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
362 /* precondition: bus.lock has been acquired. */
363 static void aspeed_i2c_next_msg_or_stop(struct aspeed_i2c_bus
*bus
)
365 if (bus
->msgs_index
+ 1 < bus
->msgs_count
) {
367 aspeed_i2c_do_start(bus
);
369 aspeed_i2c_do_stop(bus
);
373 static int aspeed_i2c_is_irq_error(u32 irq_status
)
375 if (irq_status
& ASPEED_I2CD_INTR_ARBIT_LOSS
)
377 if (irq_status
& (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT
|
378 ASPEED_I2CD_INTR_SCL_TIMEOUT
))
380 if (irq_status
& (ASPEED_I2CD_INTR_ABNORMAL
))
386 static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus
*bus
)
388 u32 irq_status
, status_ack
= 0, command
= 0;
393 spin_lock(&bus
->lock
);
394 irq_status
= readl(bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
395 /* Ack all interrupt bits. */
396 writel(irq_status
, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
398 if (irq_status
& ASPEED_I2CD_INTR_BUS_RECOVER_DONE
) {
399 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
400 status_ack
|= ASPEED_I2CD_INTR_BUS_RECOVER_DONE
;
405 * We encountered an interrupt that reports an error: the hardware
406 * should clear the command queue effectively taking us back to the
409 ret
= aspeed_i2c_is_irq_error(irq_status
);
411 dev_dbg(bus
->dev
, "received error interrupt: 0x%08x",
414 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
418 /* We are in an invalid state; reset bus to a known state. */
420 dev_err(bus
->dev
, "bus in unknown state");
422 if (bus
->master_state
!= ASPEED_I2C_MASTER_STOP
)
423 aspeed_i2c_do_stop(bus
);
424 goto out_no_complete
;
426 msg
= &bus
->msgs
[bus
->msgs_index
];
429 * START is a special case because we still have to handle a subsequent
430 * TX or RX immediately after we handle it, so we handle it here and
431 * then update the state and handle the new state below.
433 if (bus
->master_state
== ASPEED_I2C_MASTER_START
) {
434 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))) {
435 pr_devel("no slave present at %02x", msg
->addr
);
436 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
437 bus
->cmd_err
= -ENXIO
;
438 aspeed_i2c_do_stop(bus
);
439 goto out_no_complete
;
441 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
442 if (msg
->len
== 0) { /* SMBUS_QUICK */
443 aspeed_i2c_do_stop(bus
);
444 goto out_no_complete
;
446 if (msg
->flags
& I2C_M_RD
)
447 bus
->master_state
= ASPEED_I2C_MASTER_RX_FIRST
;
449 bus
->master_state
= ASPEED_I2C_MASTER_TX_FIRST
;
452 switch (bus
->master_state
) {
453 case ASPEED_I2C_MASTER_TX
:
454 if (unlikely(irq_status
& ASPEED_I2CD_INTR_TX_NAK
)) {
455 dev_dbg(bus
->dev
, "slave NACKed TX");
456 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
458 } else if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))) {
459 dev_err(bus
->dev
, "slave failed to ACK TX");
462 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
463 /* fallthrough intended */
464 case ASPEED_I2C_MASTER_TX_FIRST
:
465 if (bus
->buf_index
< msg
->len
) {
466 bus
->master_state
= ASPEED_I2C_MASTER_TX
;
467 writel(msg
->buf
[bus
->buf_index
++],
468 bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
469 writel(ASPEED_I2CD_M_TX_CMD
,
470 bus
->base
+ ASPEED_I2C_CMD_REG
);
472 aspeed_i2c_next_msg_or_stop(bus
);
474 goto out_no_complete
;
475 case ASPEED_I2C_MASTER_RX_FIRST
:
476 /* RX may not have completed yet (only address cycle) */
477 if (!(irq_status
& ASPEED_I2CD_INTR_RX_DONE
))
478 goto out_no_complete
;
479 /* fallthrough intended */
480 case ASPEED_I2C_MASTER_RX
:
481 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_RX_DONE
))) {
482 dev_err(bus
->dev
, "master failed to RX");
485 status_ack
|= ASPEED_I2CD_INTR_RX_DONE
;
487 recv_byte
= readl(bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
) >> 8;
488 msg
->buf
[bus
->buf_index
++] = recv_byte
;
490 if (msg
->flags
& I2C_M_RECV_LEN
) {
491 if (unlikely(recv_byte
> I2C_SMBUS_BLOCK_MAX
)) {
492 bus
->cmd_err
= -EPROTO
;
493 aspeed_i2c_do_stop(bus
);
494 goto out_no_complete
;
496 msg
->len
= recv_byte
+
497 ((msg
->flags
& I2C_CLIENT_PEC
) ? 2 : 1);
498 msg
->flags
&= ~I2C_M_RECV_LEN
;
501 if (bus
->buf_index
< msg
->len
) {
502 bus
->master_state
= ASPEED_I2C_MASTER_RX
;
503 command
= ASPEED_I2CD_M_RX_CMD
;
504 if (bus
->buf_index
+ 1 == msg
->len
)
505 command
|= ASPEED_I2CD_M_S_RX_CMD_LAST
;
506 writel(command
, bus
->base
+ ASPEED_I2C_CMD_REG
);
508 aspeed_i2c_next_msg_or_stop(bus
);
510 goto out_no_complete
;
511 case ASPEED_I2C_MASTER_STOP
:
512 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_NORMAL_STOP
))) {
513 dev_err(bus
->dev
, "master failed to STOP");
515 /* Do not STOP as we have already tried. */
517 status_ack
|= ASPEED_I2CD_INTR_NORMAL_STOP
;
520 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
522 case ASPEED_I2C_MASTER_INACTIVE
:
524 "master received interrupt 0x%08x, but is inactive",
527 /* Do not STOP as we should be inactive. */
530 WARN(1, "unknown master state\n");
531 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
532 bus
->cmd_err
= -EINVAL
;
537 aspeed_i2c_do_stop(bus
);
538 goto out_no_complete
;
542 bus
->master_xfer_result
= bus
->cmd_err
;
544 bus
->master_xfer_result
= bus
->msgs_index
+ 1;
545 complete(&bus
->cmd_complete
);
547 if (irq_status
!= status_ack
)
549 "irq handled != irq. expected 0x%08x, but was 0x%08x\n",
550 irq_status
, status_ack
);
551 spin_unlock(&bus
->lock
);
555 static irqreturn_t
aspeed_i2c_bus_irq(int irq
, void *dev_id
)
557 struct aspeed_i2c_bus
*bus
= dev_id
;
559 #if IS_ENABLED(CONFIG_I2C_SLAVE)
560 if (aspeed_i2c_slave_irq(bus
)) {
561 dev_dbg(bus
->dev
, "irq handled by slave.\n");
564 #endif /* CONFIG_I2C_SLAVE */
566 return aspeed_i2c_master_irq(bus
) ? IRQ_HANDLED
: IRQ_NONE
;
569 static int aspeed_i2c_master_xfer(struct i2c_adapter
*adap
,
570 struct i2c_msg
*msgs
, int num
)
572 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(adap
);
573 unsigned long time_left
, flags
;
576 spin_lock_irqsave(&bus
->lock
, flags
);
579 /* If bus is busy, attempt recovery. We assume a single master
582 if (readl(bus
->base
+ ASPEED_I2C_CMD_REG
) & ASPEED_I2CD_BUS_BUSY_STS
) {
583 spin_unlock_irqrestore(&bus
->lock
, flags
);
584 ret
= aspeed_i2c_recover_bus(bus
);
587 spin_lock_irqsave(&bus
->lock
, flags
);
593 bus
->msgs_count
= num
;
595 reinit_completion(&bus
->cmd_complete
);
596 aspeed_i2c_do_start(bus
);
597 spin_unlock_irqrestore(&bus
->lock
, flags
);
599 time_left
= wait_for_completion_timeout(&bus
->cmd_complete
,
605 return bus
->master_xfer_result
;
608 static u32
aspeed_i2c_functionality(struct i2c_adapter
*adap
)
610 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_SMBUS_BLOCK_DATA
;
613 #if IS_ENABLED(CONFIG_I2C_SLAVE)
614 /* precondition: bus.lock has been acquired. */
615 static void __aspeed_i2c_reg_slave(struct aspeed_i2c_bus
*bus
, u16 slave_addr
)
617 u32 addr_reg_val
, func_ctrl_reg_val
;
619 /* Set slave addr. */
620 addr_reg_val
= readl(bus
->base
+ ASPEED_I2C_DEV_ADDR_REG
);
621 addr_reg_val
&= ~ASPEED_I2CD_DEV_ADDR_MASK
;
622 addr_reg_val
|= slave_addr
& ASPEED_I2CD_DEV_ADDR_MASK
;
623 writel(addr_reg_val
, bus
->base
+ ASPEED_I2C_DEV_ADDR_REG
);
625 /* Turn on slave mode. */
626 func_ctrl_reg_val
= readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
627 func_ctrl_reg_val
|= ASPEED_I2CD_SLAVE_EN
;
628 writel(func_ctrl_reg_val
, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
631 static int aspeed_i2c_reg_slave(struct i2c_client
*client
)
633 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(client
->adapter
);
636 spin_lock_irqsave(&bus
->lock
, flags
);
638 spin_unlock_irqrestore(&bus
->lock
, flags
);
642 __aspeed_i2c_reg_slave(bus
, client
->addr
);
645 bus
->slave_state
= ASPEED_I2C_SLAVE_STOP
;
646 spin_unlock_irqrestore(&bus
->lock
, flags
);
651 static int aspeed_i2c_unreg_slave(struct i2c_client
*client
)
653 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(client
->adapter
);
654 u32 func_ctrl_reg_val
;
657 spin_lock_irqsave(&bus
->lock
, flags
);
659 spin_unlock_irqrestore(&bus
->lock
, flags
);
663 /* Turn off slave mode. */
664 func_ctrl_reg_val
= readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
665 func_ctrl_reg_val
&= ~ASPEED_I2CD_SLAVE_EN
;
666 writel(func_ctrl_reg_val
, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
669 spin_unlock_irqrestore(&bus
->lock
, flags
);
673 #endif /* CONFIG_I2C_SLAVE */
675 static const struct i2c_algorithm aspeed_i2c_algo
= {
676 .master_xfer
= aspeed_i2c_master_xfer
,
677 .functionality
= aspeed_i2c_functionality
,
678 #if IS_ENABLED(CONFIG_I2C_SLAVE)
679 .reg_slave
= aspeed_i2c_reg_slave
,
680 .unreg_slave
= aspeed_i2c_unreg_slave
,
681 #endif /* CONFIG_I2C_SLAVE */
684 static u32
aspeed_i2c_get_clk_reg_val(u32 clk_high_low_max
, u32 divisor
)
686 u32 base_clk
, clk_high
, clk_low
, tmp
;
689 * The actual clock frequency of SCL is:
690 * SCL_freq = APB_freq / (base_freq * (SCL_high + SCL_low))
691 * = APB_freq / divisor
692 * where base_freq is a programmable clock divider; its value is
693 * base_freq = 1 << base_clk
694 * SCL_high is the number of base_freq clock cycles that SCL stays high
695 * and SCL_low is the number of base_freq clock cycles that SCL stays
696 * low for a period of SCL.
697 * The actual register has a minimum SCL_high and SCL_low minimum of 1;
698 * thus, they start counting at zero. So
699 * SCL_high = clk_high + 1
700 * SCL_low = clk_low + 1
702 * SCL_freq = APB_freq /
703 * ((1 << base_clk) * (clk_high + 1 + clk_low + 1))
704 * The documentation recommends clk_high >= clk_high_max / 2 and
705 * clk_low >= clk_low_max / 2 - 1 when possible; this last constraint
706 * gives us the following solution:
708 base_clk
= divisor
> clk_high_low_max
?
709 ilog2((divisor
- 1) / clk_high_low_max
) + 1 : 0;
710 tmp
= (divisor
+ (1 << base_clk
) - 1) >> base_clk
;
712 clk_high
= tmp
- clk_low
;
721 return ((clk_high
<< ASPEED_I2CD_TIME_SCL_HIGH_SHIFT
)
722 & ASPEED_I2CD_TIME_SCL_HIGH_MASK
)
723 | ((clk_low
<< ASPEED_I2CD_TIME_SCL_LOW_SHIFT
)
724 & ASPEED_I2CD_TIME_SCL_LOW_MASK
)
725 | (base_clk
& ASPEED_I2CD_TIME_BASE_DIVISOR_MASK
);
728 static u32
aspeed_i2c_24xx_get_clk_reg_val(u32 divisor
)
731 * clk_high and clk_low are each 3 bits wide, so each can hold a max
732 * value of 8 giving a clk_high_low_max of 16.
734 return aspeed_i2c_get_clk_reg_val(16, divisor
);
737 static u32
aspeed_i2c_25xx_get_clk_reg_val(u32 divisor
)
740 * clk_high and clk_low are each 4 bits wide, so each can hold a max
741 * value of 16 giving a clk_high_low_max of 32.
743 return aspeed_i2c_get_clk_reg_val(32, divisor
);
746 /* precondition: bus.lock has been acquired. */
747 static int aspeed_i2c_init_clk(struct aspeed_i2c_bus
*bus
)
749 u32 divisor
, clk_reg_val
;
751 divisor
= DIV_ROUND_UP(bus
->parent_clk_frequency
, bus
->bus_frequency
);
752 clk_reg_val
= readl(bus
->base
+ ASPEED_I2C_AC_TIMING_REG1
);
753 clk_reg_val
&= (ASPEED_I2CD_TIME_TBUF_MASK
|
754 ASPEED_I2CD_TIME_THDSTA_MASK
|
755 ASPEED_I2CD_TIME_TACST_MASK
);
756 clk_reg_val
|= bus
->get_clk_reg_val(divisor
);
757 writel(clk_reg_val
, bus
->base
+ ASPEED_I2C_AC_TIMING_REG1
);
758 writel(ASPEED_NO_TIMEOUT_CTRL
, bus
->base
+ ASPEED_I2C_AC_TIMING_REG2
);
763 /* precondition: bus.lock has been acquired. */
764 static int aspeed_i2c_init(struct aspeed_i2c_bus
*bus
,
765 struct platform_device
*pdev
)
767 u32 fun_ctrl_reg
= ASPEED_I2CD_MASTER_EN
;
770 /* Disable everything. */
771 writel(0, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
773 ret
= aspeed_i2c_init_clk(bus
);
777 if (!of_property_read_bool(pdev
->dev
.of_node
, "multi-master"))
778 fun_ctrl_reg
|= ASPEED_I2CD_MULTI_MASTER_DIS
;
780 /* Enable Master Mode */
781 writel(readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
) | fun_ctrl_reg
,
782 bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
784 #if IS_ENABLED(CONFIG_I2C_SLAVE)
785 /* If slave has already been registered, re-enable it. */
787 __aspeed_i2c_reg_slave(bus
, bus
->slave
->addr
);
788 #endif /* CONFIG_I2C_SLAVE */
790 /* Set interrupt generation of I2C controller */
791 writel(ASPEED_I2CD_INTR_ALL
, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
796 static int aspeed_i2c_reset(struct aspeed_i2c_bus
*bus
)
798 struct platform_device
*pdev
= to_platform_device(bus
->dev
);
802 spin_lock_irqsave(&bus
->lock
, flags
);
804 /* Disable and ack all interrupts. */
805 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
806 writel(0xffffffff, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
808 ret
= aspeed_i2c_init(bus
, pdev
);
810 spin_unlock_irqrestore(&bus
->lock
, flags
);
815 static const struct of_device_id aspeed_i2c_bus_of_table
[] = {
817 .compatible
= "aspeed,ast2400-i2c-bus",
818 .data
= aspeed_i2c_24xx_get_clk_reg_val
,
821 .compatible
= "aspeed,ast2500-i2c-bus",
822 .data
= aspeed_i2c_25xx_get_clk_reg_val
,
826 MODULE_DEVICE_TABLE(of
, aspeed_i2c_bus_of_table
);
828 static int aspeed_i2c_probe_bus(struct platform_device
*pdev
)
830 const struct of_device_id
*match
;
831 struct aspeed_i2c_bus
*bus
;
832 struct clk
*parent_clk
;
833 struct resource
*res
;
836 bus
= devm_kzalloc(&pdev
->dev
, sizeof(*bus
), GFP_KERNEL
);
840 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
841 bus
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
842 if (IS_ERR(bus
->base
))
843 return PTR_ERR(bus
->base
);
845 parent_clk
= devm_clk_get(&pdev
->dev
, NULL
);
846 if (IS_ERR(parent_clk
))
847 return PTR_ERR(parent_clk
);
848 bus
->parent_clk_frequency
= clk_get_rate(parent_clk
);
849 /* We just need the clock rate, we don't actually use the clk object. */
850 devm_clk_put(&pdev
->dev
, parent_clk
);
852 bus
->rst
= devm_reset_control_get_shared(&pdev
->dev
, NULL
);
853 if (IS_ERR(bus
->rst
)) {
855 "missing or invalid reset controller device tree entry");
856 return PTR_ERR(bus
->rst
);
858 reset_control_deassert(bus
->rst
);
860 ret
= of_property_read_u32(pdev
->dev
.of_node
,
861 "bus-frequency", &bus
->bus_frequency
);
864 "Could not read bus-frequency property\n");
865 bus
->bus_frequency
= 100000;
868 match
= of_match_node(aspeed_i2c_bus_of_table
, pdev
->dev
.of_node
);
870 bus
->get_clk_reg_val
= aspeed_i2c_24xx_get_clk_reg_val
;
872 bus
->get_clk_reg_val
= match
->data
;
874 /* Initialize the I2C adapter */
875 spin_lock_init(&bus
->lock
);
876 init_completion(&bus
->cmd_complete
);
877 bus
->adap
.owner
= THIS_MODULE
;
878 bus
->adap
.retries
= 0;
879 bus
->adap
.timeout
= 5 * HZ
;
880 bus
->adap
.algo
= &aspeed_i2c_algo
;
881 bus
->adap
.dev
.parent
= &pdev
->dev
;
882 bus
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
883 strlcpy(bus
->adap
.name
, pdev
->name
, sizeof(bus
->adap
.name
));
884 i2c_set_adapdata(&bus
->adap
, bus
);
886 bus
->dev
= &pdev
->dev
;
888 /* Clean up any left over interrupt state. */
889 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
890 writel(0xffffffff, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
892 * bus.lock does not need to be held because the interrupt handler has
893 * not been enabled yet.
895 ret
= aspeed_i2c_init(bus
, pdev
);
899 irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
900 ret
= devm_request_irq(&pdev
->dev
, irq
, aspeed_i2c_bus_irq
,
901 0, dev_name(&pdev
->dev
), bus
);
905 ret
= i2c_add_adapter(&bus
->adap
);
909 platform_set_drvdata(pdev
, bus
);
911 dev_info(bus
->dev
, "i2c bus %d registered, irq %d\n",
917 static int aspeed_i2c_remove_bus(struct platform_device
*pdev
)
919 struct aspeed_i2c_bus
*bus
= platform_get_drvdata(pdev
);
922 spin_lock_irqsave(&bus
->lock
, flags
);
924 /* Disable everything. */
925 writel(0, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
926 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
928 spin_unlock_irqrestore(&bus
->lock
, flags
);
930 reset_control_assert(bus
->rst
);
932 i2c_del_adapter(&bus
->adap
);
937 static struct platform_driver aspeed_i2c_bus_driver
= {
938 .probe
= aspeed_i2c_probe_bus
,
939 .remove
= aspeed_i2c_remove_bus
,
941 .name
= "aspeed-i2c-bus",
942 .of_match_table
= aspeed_i2c_bus_of_table
,
945 module_platform_driver(aspeed_i2c_bus_driver
);
947 MODULE_AUTHOR("Brendan Higgins <brendanhiggins@google.com>");
948 MODULE_DESCRIPTION("Aspeed I2C Bus Driver");
949 MODULE_LICENSE("GPL v2");