1 From 89b466914a0d49f08d00be25556ba22497b9a284 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= <noralf@tronnes.org>
3 Date: Sat, 17 Sep 2016 15:07:10 +0200
4 Subject: [PATCH] i2c: bcm2835: Fix hang for writing messages larger than 16
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 Writing messages larger than the FIFO size results in a hang, rendering
11 the machine unusable. This is because the RXD status flag is set on the
12 first interrupt which results in bcm2835_drain_rxfifo() stealing bytes
13 from the buffer. The controller continues to trigger interrupts waiting
14 for the missing bytes, but bcm2835_fill_txfifo() has none to give.
15 In this situation wait_for_completion_timeout() apparently is unable to
18 The BCM2835 ARM Peripherals datasheet has this to say about the flags:
19 TXD: is set when the FIFO has space for at least one byte of data.
20 RXD: is set when the FIFO contains at least one byte of data.
21 TXW: is set during a write transfer and the FIFO is less than full.
22 RXR: is set during a read transfer and the FIFO is or more full.
24 Implementing the logic from the downstream i2c-bcm2708 driver solved
27 Signed-off-by: Noralf Trønnes <noralf@tronnes.org>
28 Reviewed-by: Eric Anholt <eric@anholt.net>
29 Reviewed-by: Martin Sperl <kernel@martin.sperl.org>
31 drivers/i2c/busses/i2c-bcm2835.c | 22 ++++++++++++++--------
32 1 file changed, 14 insertions(+), 8 deletions(-)
34 --- a/drivers/i2c/busses/i2c-bcm2835.c
35 +++ b/drivers/i2c/busses/i2c-bcm2835.c
36 @@ -64,6 +64,7 @@ struct bcm2835_i2c_dev {
38 struct i2c_adapter adapter;
39 struct completion completion;
40 + struct i2c_msg *curr_msg;
43 size_t msg_buf_remaining;
44 @@ -126,14 +127,13 @@ static irqreturn_t bcm2835_i2c_isr(int t
48 - if (val & BCM2835_I2C_S_RXD) {
49 - bcm2835_drain_rxfifo(i2c_dev);
50 - if (!(val & BCM2835_I2C_S_DONE))
54 if (val & BCM2835_I2C_S_DONE) {
55 - if (i2c_dev->msg_buf_remaining)
56 + if (i2c_dev->curr_msg->flags & I2C_M_RD) {
57 + bcm2835_drain_rxfifo(i2c_dev);
58 + val = bcm2835_i2c_readl(i2c_dev, BCM2835_I2C_S);
61 + if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
62 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
65 @@ -141,11 +141,16 @@ static irqreturn_t bcm2835_i2c_isr(int t
69 - if (val & BCM2835_I2C_S_TXD) {
70 + if (val & BCM2835_I2C_S_TXW) {
71 bcm2835_fill_txfifo(i2c_dev);
75 + if (val & BCM2835_I2C_S_RXR) {
76 + bcm2835_drain_rxfifo(i2c_dev);
83 @@ -155,6 +160,7 @@ static int bcm2835_i2c_xfer_msg(struct b
85 unsigned long time_left;
87 + i2c_dev->curr_msg = msg;
88 i2c_dev->msg_buf = msg->buf;
89 i2c_dev->msg_buf_remaining = msg->len;
90 reinit_completion(&i2c_dev->completion);