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
)(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;
242 command
= readl(bus
->base
+ ASPEED_I2C_CMD_REG
);
243 irq_status
= readl(bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
245 /* Slave was requested, restart state machine. */
246 if (irq_status
& ASPEED_I2CD_INTR_SLAVE_MATCH
) {
247 status_ack
|= ASPEED_I2CD_INTR_SLAVE_MATCH
;
248 bus
->slave_state
= ASPEED_I2C_SLAVE_START
;
251 /* Slave is not currently active, irq was for someone else. */
252 if (bus
->slave_state
== ASPEED_I2C_SLAVE_STOP
) {
257 dev_dbg(bus
->dev
, "slave irq status 0x%08x, cmd 0x%08x\n",
258 irq_status
, command
);
260 /* Slave was sent something. */
261 if (irq_status
& ASPEED_I2CD_INTR_RX_DONE
) {
262 value
= readl(bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
) >> 8;
263 /* Handle address frame. */
264 if (bus
->slave_state
== ASPEED_I2C_SLAVE_START
) {
267 ASPEED_I2C_SLAVE_READ_REQUESTED
;
270 ASPEED_I2C_SLAVE_WRITE_REQUESTED
;
272 status_ack
|= ASPEED_I2CD_INTR_RX_DONE
;
275 /* Slave was asked to stop. */
276 if (irq_status
& ASPEED_I2CD_INTR_NORMAL_STOP
) {
277 status_ack
|= ASPEED_I2CD_INTR_NORMAL_STOP
;
278 bus
->slave_state
= ASPEED_I2C_SLAVE_STOP
;
280 if (irq_status
& ASPEED_I2CD_INTR_TX_NAK
) {
281 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
282 bus
->slave_state
= ASPEED_I2C_SLAVE_STOP
;
285 switch (bus
->slave_state
) {
286 case ASPEED_I2C_SLAVE_READ_REQUESTED
:
287 if (irq_status
& ASPEED_I2CD_INTR_TX_ACK
)
288 dev_err(bus
->dev
, "Unexpected ACK on read request.\n");
289 bus
->slave_state
= ASPEED_I2C_SLAVE_READ_PROCESSED
;
291 i2c_slave_event(slave
, I2C_SLAVE_READ_REQUESTED
, &value
);
292 writel(value
, bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
293 writel(ASPEED_I2CD_S_TX_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
295 case ASPEED_I2C_SLAVE_READ_PROCESSED
:
296 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
297 if (!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))
299 "Expected ACK after processed read.\n");
300 i2c_slave_event(slave
, I2C_SLAVE_READ_PROCESSED
, &value
);
301 writel(value
, bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
302 writel(ASPEED_I2CD_S_TX_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
304 case ASPEED_I2C_SLAVE_WRITE_REQUESTED
:
305 bus
->slave_state
= ASPEED_I2C_SLAVE_WRITE_RECEIVED
;
306 i2c_slave_event(slave
, I2C_SLAVE_WRITE_REQUESTED
, &value
);
308 case ASPEED_I2C_SLAVE_WRITE_RECEIVED
:
309 i2c_slave_event(slave
, I2C_SLAVE_WRITE_RECEIVED
, &value
);
311 case ASPEED_I2C_SLAVE_STOP
:
312 i2c_slave_event(slave
, I2C_SLAVE_STOP
, &value
);
315 dev_err(bus
->dev
, "unhandled slave_state: %d\n",
320 if (status_ack
!= irq_status
)
322 "irq handled != irq. expected %x, but was %x\n",
323 irq_status
, status_ack
);
324 writel(status_ack
, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
329 #endif /* CONFIG_I2C_SLAVE */
331 /* precondition: bus.lock has been acquired. */
332 static void aspeed_i2c_do_start(struct aspeed_i2c_bus
*bus
)
334 u32 command
= ASPEED_I2CD_M_START_CMD
| ASPEED_I2CD_M_TX_CMD
;
335 struct i2c_msg
*msg
= &bus
->msgs
[bus
->msgs_index
];
336 u8 slave_addr
= i2c_8bit_addr_from_msg(msg
);
338 bus
->master_state
= ASPEED_I2C_MASTER_START
;
341 if (msg
->flags
& I2C_M_RD
) {
342 command
|= ASPEED_I2CD_M_RX_CMD
;
343 /* Need to let the hardware know to NACK after RX. */
344 if (msg
->len
== 1 && !(msg
->flags
& I2C_M_RECV_LEN
))
345 command
|= ASPEED_I2CD_M_S_RX_CMD_LAST
;
348 writel(slave_addr
, bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
349 writel(command
, bus
->base
+ ASPEED_I2C_CMD_REG
);
352 /* precondition: bus.lock has been acquired. */
353 static void aspeed_i2c_do_stop(struct aspeed_i2c_bus
*bus
)
355 bus
->master_state
= ASPEED_I2C_MASTER_STOP
;
356 writel(ASPEED_I2CD_M_STOP_CMD
, bus
->base
+ ASPEED_I2C_CMD_REG
);
359 /* precondition: bus.lock has been acquired. */
360 static void aspeed_i2c_next_msg_or_stop(struct aspeed_i2c_bus
*bus
)
362 if (bus
->msgs_index
+ 1 < bus
->msgs_count
) {
364 aspeed_i2c_do_start(bus
);
366 aspeed_i2c_do_stop(bus
);
370 static int aspeed_i2c_is_irq_error(u32 irq_status
)
372 if (irq_status
& ASPEED_I2CD_INTR_ARBIT_LOSS
)
374 if (irq_status
& (ASPEED_I2CD_INTR_SDA_DL_TIMEOUT
|
375 ASPEED_I2CD_INTR_SCL_TIMEOUT
))
377 if (irq_status
& (ASPEED_I2CD_INTR_ABNORMAL
))
383 static bool aspeed_i2c_master_irq(struct aspeed_i2c_bus
*bus
)
385 u32 irq_status
, status_ack
= 0, command
= 0;
390 irq_status
= readl(bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
391 /* Ack all interrupt bits. */
392 writel(irq_status
, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
394 if (irq_status
& ASPEED_I2CD_INTR_BUS_RECOVER_DONE
) {
395 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
396 status_ack
|= ASPEED_I2CD_INTR_BUS_RECOVER_DONE
;
401 * We encountered an interrupt that reports an error: the hardware
402 * should clear the command queue effectively taking us back to the
405 ret
= aspeed_i2c_is_irq_error(irq_status
);
407 dev_dbg(bus
->dev
, "received error interrupt: 0x%08x\n",
410 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
414 /* We are in an invalid state; reset bus to a known state. */
416 dev_err(bus
->dev
, "bus in unknown state\n");
418 if (bus
->master_state
!= ASPEED_I2C_MASTER_STOP
)
419 aspeed_i2c_do_stop(bus
);
420 goto out_no_complete
;
422 msg
= &bus
->msgs
[bus
->msgs_index
];
425 * START is a special case because we still have to handle a subsequent
426 * TX or RX immediately after we handle it, so we handle it here and
427 * then update the state and handle the new state below.
429 if (bus
->master_state
== ASPEED_I2C_MASTER_START
) {
430 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))) {
431 pr_devel("no slave present at %02x\n", msg
->addr
);
432 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
433 bus
->cmd_err
= -ENXIO
;
434 aspeed_i2c_do_stop(bus
);
435 goto out_no_complete
;
437 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
438 if (msg
->len
== 0) { /* SMBUS_QUICK */
439 aspeed_i2c_do_stop(bus
);
440 goto out_no_complete
;
442 if (msg
->flags
& I2C_M_RD
)
443 bus
->master_state
= ASPEED_I2C_MASTER_RX_FIRST
;
445 bus
->master_state
= ASPEED_I2C_MASTER_TX_FIRST
;
448 switch (bus
->master_state
) {
449 case ASPEED_I2C_MASTER_TX
:
450 if (unlikely(irq_status
& ASPEED_I2CD_INTR_TX_NAK
)) {
451 dev_dbg(bus
->dev
, "slave NACKed TX\n");
452 status_ack
|= ASPEED_I2CD_INTR_TX_NAK
;
454 } else if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_TX_ACK
))) {
455 dev_err(bus
->dev
, "slave failed to ACK TX\n");
458 status_ack
|= ASPEED_I2CD_INTR_TX_ACK
;
459 /* fallthrough intended */
460 case ASPEED_I2C_MASTER_TX_FIRST
:
461 if (bus
->buf_index
< msg
->len
) {
462 bus
->master_state
= ASPEED_I2C_MASTER_TX
;
463 writel(msg
->buf
[bus
->buf_index
++],
464 bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
);
465 writel(ASPEED_I2CD_M_TX_CMD
,
466 bus
->base
+ ASPEED_I2C_CMD_REG
);
468 aspeed_i2c_next_msg_or_stop(bus
);
470 goto out_no_complete
;
471 case ASPEED_I2C_MASTER_RX_FIRST
:
472 /* RX may not have completed yet (only address cycle) */
473 if (!(irq_status
& ASPEED_I2CD_INTR_RX_DONE
))
474 goto out_no_complete
;
475 /* fallthrough intended */
476 case ASPEED_I2C_MASTER_RX
:
477 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_RX_DONE
))) {
478 dev_err(bus
->dev
, "master failed to RX\n");
481 status_ack
|= ASPEED_I2CD_INTR_RX_DONE
;
483 recv_byte
= readl(bus
->base
+ ASPEED_I2C_BYTE_BUF_REG
) >> 8;
484 msg
->buf
[bus
->buf_index
++] = recv_byte
;
486 if (msg
->flags
& I2C_M_RECV_LEN
) {
487 if (unlikely(recv_byte
> I2C_SMBUS_BLOCK_MAX
)) {
488 bus
->cmd_err
= -EPROTO
;
489 aspeed_i2c_do_stop(bus
);
490 goto out_no_complete
;
492 msg
->len
= recv_byte
+
493 ((msg
->flags
& I2C_CLIENT_PEC
) ? 2 : 1);
494 msg
->flags
&= ~I2C_M_RECV_LEN
;
497 if (bus
->buf_index
< msg
->len
) {
498 bus
->master_state
= ASPEED_I2C_MASTER_RX
;
499 command
= ASPEED_I2CD_M_RX_CMD
;
500 if (bus
->buf_index
+ 1 == msg
->len
)
501 command
|= ASPEED_I2CD_M_S_RX_CMD_LAST
;
502 writel(command
, bus
->base
+ ASPEED_I2C_CMD_REG
);
504 aspeed_i2c_next_msg_or_stop(bus
);
506 goto out_no_complete
;
507 case ASPEED_I2C_MASTER_STOP
:
508 if (unlikely(!(irq_status
& ASPEED_I2CD_INTR_NORMAL_STOP
))) {
509 dev_err(bus
->dev
, "master failed to STOP\n");
511 /* Do not STOP as we have already tried. */
513 status_ack
|= ASPEED_I2CD_INTR_NORMAL_STOP
;
516 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
518 case ASPEED_I2C_MASTER_INACTIVE
:
520 "master received interrupt 0x%08x, but is inactive\n",
523 /* Do not STOP as we should be inactive. */
526 WARN(1, "unknown master state\n");
527 bus
->master_state
= ASPEED_I2C_MASTER_INACTIVE
;
528 bus
->cmd_err
= -EINVAL
;
533 aspeed_i2c_do_stop(bus
);
534 goto out_no_complete
;
538 bus
->master_xfer_result
= bus
->cmd_err
;
540 bus
->master_xfer_result
= bus
->msgs_index
+ 1;
541 complete(&bus
->cmd_complete
);
543 if (irq_status
!= status_ack
)
545 "irq handled != irq. expected 0x%08x, but was 0x%08x\n",
546 irq_status
, status_ack
);
550 static irqreturn_t
aspeed_i2c_bus_irq(int irq
, void *dev_id
)
552 struct aspeed_i2c_bus
*bus
= dev_id
;
555 spin_lock(&bus
->lock
);
557 #if IS_ENABLED(CONFIG_I2C_SLAVE)
558 if (aspeed_i2c_slave_irq(bus
)) {
559 dev_dbg(bus
->dev
, "irq handled by slave.\n");
563 #endif /* CONFIG_I2C_SLAVE */
565 ret
= aspeed_i2c_master_irq(bus
);
568 spin_unlock(&bus
->lock
);
569 return ret
? IRQ_HANDLED
: IRQ_NONE
;
572 static int aspeed_i2c_master_xfer(struct i2c_adapter
*adap
,
573 struct i2c_msg
*msgs
, int num
)
575 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(adap
);
576 unsigned long time_left
, flags
;
579 spin_lock_irqsave(&bus
->lock
, flags
);
582 /* If bus is busy, attempt recovery. We assume a single master
585 if (readl(bus
->base
+ ASPEED_I2C_CMD_REG
) & ASPEED_I2CD_BUS_BUSY_STS
) {
586 spin_unlock_irqrestore(&bus
->lock
, flags
);
587 ret
= aspeed_i2c_recover_bus(bus
);
590 spin_lock_irqsave(&bus
->lock
, flags
);
596 bus
->msgs_count
= num
;
598 reinit_completion(&bus
->cmd_complete
);
599 aspeed_i2c_do_start(bus
);
600 spin_unlock_irqrestore(&bus
->lock
, flags
);
602 time_left
= wait_for_completion_timeout(&bus
->cmd_complete
,
608 return bus
->master_xfer_result
;
611 static u32
aspeed_i2c_functionality(struct i2c_adapter
*adap
)
613 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
| I2C_FUNC_SMBUS_BLOCK_DATA
;
616 #if IS_ENABLED(CONFIG_I2C_SLAVE)
617 /* precondition: bus.lock has been acquired. */
618 static void __aspeed_i2c_reg_slave(struct aspeed_i2c_bus
*bus
, u16 slave_addr
)
620 u32 addr_reg_val
, func_ctrl_reg_val
;
622 /* Set slave addr. */
623 addr_reg_val
= readl(bus
->base
+ ASPEED_I2C_DEV_ADDR_REG
);
624 addr_reg_val
&= ~ASPEED_I2CD_DEV_ADDR_MASK
;
625 addr_reg_val
|= slave_addr
& ASPEED_I2CD_DEV_ADDR_MASK
;
626 writel(addr_reg_val
, bus
->base
+ ASPEED_I2C_DEV_ADDR_REG
);
628 /* Turn on slave mode. */
629 func_ctrl_reg_val
= readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
630 func_ctrl_reg_val
|= ASPEED_I2CD_SLAVE_EN
;
631 writel(func_ctrl_reg_val
, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
634 static int aspeed_i2c_reg_slave(struct i2c_client
*client
)
636 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(client
->adapter
);
639 spin_lock_irqsave(&bus
->lock
, flags
);
641 spin_unlock_irqrestore(&bus
->lock
, flags
);
645 __aspeed_i2c_reg_slave(bus
, client
->addr
);
648 bus
->slave_state
= ASPEED_I2C_SLAVE_STOP
;
649 spin_unlock_irqrestore(&bus
->lock
, flags
);
654 static int aspeed_i2c_unreg_slave(struct i2c_client
*client
)
656 struct aspeed_i2c_bus
*bus
= i2c_get_adapdata(client
->adapter
);
657 u32 func_ctrl_reg_val
;
660 spin_lock_irqsave(&bus
->lock
, flags
);
662 spin_unlock_irqrestore(&bus
->lock
, flags
);
666 /* Turn off slave mode. */
667 func_ctrl_reg_val
= readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
668 func_ctrl_reg_val
&= ~ASPEED_I2CD_SLAVE_EN
;
669 writel(func_ctrl_reg_val
, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
672 spin_unlock_irqrestore(&bus
->lock
, flags
);
676 #endif /* CONFIG_I2C_SLAVE */
678 static const struct i2c_algorithm aspeed_i2c_algo
= {
679 .master_xfer
= aspeed_i2c_master_xfer
,
680 .functionality
= aspeed_i2c_functionality
,
681 #if IS_ENABLED(CONFIG_I2C_SLAVE)
682 .reg_slave
= aspeed_i2c_reg_slave
,
683 .unreg_slave
= aspeed_i2c_unreg_slave
,
684 #endif /* CONFIG_I2C_SLAVE */
687 static u32
aspeed_i2c_get_clk_reg_val(u32 clk_high_low_max
, u32 divisor
)
689 u32 base_clk
, clk_high
, clk_low
, tmp
;
692 * The actual clock frequency of SCL is:
693 * SCL_freq = APB_freq / (base_freq * (SCL_high + SCL_low))
694 * = APB_freq / divisor
695 * where base_freq is a programmable clock divider; its value is
696 * base_freq = 1 << base_clk
697 * SCL_high is the number of base_freq clock cycles that SCL stays high
698 * and SCL_low is the number of base_freq clock cycles that SCL stays
699 * low for a period of SCL.
700 * The actual register has a minimum SCL_high and SCL_low minimum of 1;
701 * thus, they start counting at zero. So
702 * SCL_high = clk_high + 1
703 * SCL_low = clk_low + 1
705 * SCL_freq = APB_freq /
706 * ((1 << base_clk) * (clk_high + 1 + clk_low + 1))
707 * The documentation recommends clk_high >= clk_high_max / 2 and
708 * clk_low >= clk_low_max / 2 - 1 when possible; this last constraint
709 * gives us the following solution:
711 base_clk
= divisor
> clk_high_low_max
?
712 ilog2((divisor
- 1) / clk_high_low_max
) + 1 : 0;
713 tmp
= (divisor
+ (1 << base_clk
) - 1) >> base_clk
;
715 clk_high
= tmp
- clk_low
;
724 return ((clk_high
<< ASPEED_I2CD_TIME_SCL_HIGH_SHIFT
)
725 & ASPEED_I2CD_TIME_SCL_HIGH_MASK
)
726 | ((clk_low
<< ASPEED_I2CD_TIME_SCL_LOW_SHIFT
)
727 & ASPEED_I2CD_TIME_SCL_LOW_MASK
)
728 | (base_clk
& ASPEED_I2CD_TIME_BASE_DIVISOR_MASK
);
731 static u32
aspeed_i2c_24xx_get_clk_reg_val(u32 divisor
)
734 * clk_high and clk_low are each 3 bits wide, so each can hold a max
735 * value of 8 giving a clk_high_low_max of 16.
737 return aspeed_i2c_get_clk_reg_val(16, divisor
);
740 static u32
aspeed_i2c_25xx_get_clk_reg_val(u32 divisor
)
743 * clk_high and clk_low are each 4 bits wide, so each can hold a max
744 * value of 16 giving a clk_high_low_max of 32.
746 return aspeed_i2c_get_clk_reg_val(32, divisor
);
749 /* precondition: bus.lock has been acquired. */
750 static int aspeed_i2c_init_clk(struct aspeed_i2c_bus
*bus
)
752 u32 divisor
, clk_reg_val
;
754 divisor
= DIV_ROUND_UP(bus
->parent_clk_frequency
, bus
->bus_frequency
);
755 clk_reg_val
= readl(bus
->base
+ ASPEED_I2C_AC_TIMING_REG1
);
756 clk_reg_val
&= (ASPEED_I2CD_TIME_TBUF_MASK
|
757 ASPEED_I2CD_TIME_THDSTA_MASK
|
758 ASPEED_I2CD_TIME_TACST_MASK
);
759 clk_reg_val
|= bus
->get_clk_reg_val(divisor
);
760 writel(clk_reg_val
, bus
->base
+ ASPEED_I2C_AC_TIMING_REG1
);
761 writel(ASPEED_NO_TIMEOUT_CTRL
, bus
->base
+ ASPEED_I2C_AC_TIMING_REG2
);
766 /* precondition: bus.lock has been acquired. */
767 static int aspeed_i2c_init(struct aspeed_i2c_bus
*bus
,
768 struct platform_device
*pdev
)
770 u32 fun_ctrl_reg
= ASPEED_I2CD_MASTER_EN
;
773 /* Disable everything. */
774 writel(0, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
776 ret
= aspeed_i2c_init_clk(bus
);
780 if (!of_property_read_bool(pdev
->dev
.of_node
, "multi-master"))
781 fun_ctrl_reg
|= ASPEED_I2CD_MULTI_MASTER_DIS
;
783 /* Enable Master Mode */
784 writel(readl(bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
) | fun_ctrl_reg
,
785 bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
787 #if IS_ENABLED(CONFIG_I2C_SLAVE)
788 /* If slave has already been registered, re-enable it. */
790 __aspeed_i2c_reg_slave(bus
, bus
->slave
->addr
);
791 #endif /* CONFIG_I2C_SLAVE */
793 /* Set interrupt generation of I2C controller */
794 writel(ASPEED_I2CD_INTR_ALL
, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
799 static int aspeed_i2c_reset(struct aspeed_i2c_bus
*bus
)
801 struct platform_device
*pdev
= to_platform_device(bus
->dev
);
805 spin_lock_irqsave(&bus
->lock
, flags
);
807 /* Disable and ack all interrupts. */
808 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
809 writel(0xffffffff, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
811 ret
= aspeed_i2c_init(bus
, pdev
);
813 spin_unlock_irqrestore(&bus
->lock
, flags
);
818 static const struct of_device_id aspeed_i2c_bus_of_table
[] = {
820 .compatible
= "aspeed,ast2400-i2c-bus",
821 .data
= aspeed_i2c_24xx_get_clk_reg_val
,
824 .compatible
= "aspeed,ast2500-i2c-bus",
825 .data
= aspeed_i2c_25xx_get_clk_reg_val
,
829 MODULE_DEVICE_TABLE(of
, aspeed_i2c_bus_of_table
);
831 static int aspeed_i2c_probe_bus(struct platform_device
*pdev
)
833 const struct of_device_id
*match
;
834 struct aspeed_i2c_bus
*bus
;
835 struct clk
*parent_clk
;
836 struct resource
*res
;
839 bus
= devm_kzalloc(&pdev
->dev
, sizeof(*bus
), GFP_KERNEL
);
843 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
844 bus
->base
= devm_ioremap_resource(&pdev
->dev
, res
);
845 if (IS_ERR(bus
->base
))
846 return PTR_ERR(bus
->base
);
848 parent_clk
= devm_clk_get(&pdev
->dev
, NULL
);
849 if (IS_ERR(parent_clk
))
850 return PTR_ERR(parent_clk
);
851 bus
->parent_clk_frequency
= clk_get_rate(parent_clk
);
852 /* We just need the clock rate, we don't actually use the clk object. */
853 devm_clk_put(&pdev
->dev
, parent_clk
);
855 bus
->rst
= devm_reset_control_get_shared(&pdev
->dev
, NULL
);
856 if (IS_ERR(bus
->rst
)) {
858 "missing or invalid reset controller device tree entry\n");
859 return PTR_ERR(bus
->rst
);
861 reset_control_deassert(bus
->rst
);
863 ret
= of_property_read_u32(pdev
->dev
.of_node
,
864 "bus-frequency", &bus
->bus_frequency
);
867 "Could not read bus-frequency property\n");
868 bus
->bus_frequency
= 100000;
871 match
= of_match_node(aspeed_i2c_bus_of_table
, pdev
->dev
.of_node
);
873 bus
->get_clk_reg_val
= aspeed_i2c_24xx_get_clk_reg_val
;
875 bus
->get_clk_reg_val
= (u32 (*)(u32
))match
->data
;
877 /* Initialize the I2C adapter */
878 spin_lock_init(&bus
->lock
);
879 init_completion(&bus
->cmd_complete
);
880 bus
->adap
.owner
= THIS_MODULE
;
881 bus
->adap
.retries
= 0;
882 bus
->adap
.timeout
= 5 * HZ
;
883 bus
->adap
.algo
= &aspeed_i2c_algo
;
884 bus
->adap
.dev
.parent
= &pdev
->dev
;
885 bus
->adap
.dev
.of_node
= pdev
->dev
.of_node
;
886 strlcpy(bus
->adap
.name
, pdev
->name
, sizeof(bus
->adap
.name
));
887 i2c_set_adapdata(&bus
->adap
, bus
);
889 bus
->dev
= &pdev
->dev
;
891 /* Clean up any left over interrupt state. */
892 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
893 writel(0xffffffff, bus
->base
+ ASPEED_I2C_INTR_STS_REG
);
895 * bus.lock does not need to be held because the interrupt handler has
896 * not been enabled yet.
898 ret
= aspeed_i2c_init(bus
, pdev
);
902 irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
903 ret
= devm_request_irq(&pdev
->dev
, irq
, aspeed_i2c_bus_irq
,
904 0, dev_name(&pdev
->dev
), bus
);
908 ret
= i2c_add_adapter(&bus
->adap
);
912 platform_set_drvdata(pdev
, bus
);
914 dev_info(bus
->dev
, "i2c bus %d registered, irq %d\n",
920 static int aspeed_i2c_remove_bus(struct platform_device
*pdev
)
922 struct aspeed_i2c_bus
*bus
= platform_get_drvdata(pdev
);
925 spin_lock_irqsave(&bus
->lock
, flags
);
927 /* Disable everything. */
928 writel(0, bus
->base
+ ASPEED_I2C_FUN_CTRL_REG
);
929 writel(0, bus
->base
+ ASPEED_I2C_INTR_CTRL_REG
);
931 spin_unlock_irqrestore(&bus
->lock
, flags
);
933 reset_control_assert(bus
->rst
);
935 i2c_del_adapter(&bus
->adap
);
940 static struct platform_driver aspeed_i2c_bus_driver
= {
941 .probe
= aspeed_i2c_probe_bus
,
942 .remove
= aspeed_i2c_remove_bus
,
944 .name
= "aspeed-i2c-bus",
945 .of_match_table
= aspeed_i2c_bus_of_table
,
948 module_platform_driver(aspeed_i2c_bus_driver
);
950 MODULE_AUTHOR("Brendan Higgins <brendanhiggins@google.com>");
951 MODULE_DESCRIPTION("Aspeed I2C Bus Driver");
952 MODULE_LICENSE("GPL v2");