brcm2708: add linux 4.9 support
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-4.9 / 0107-i2c-bcm2835-Fix-hang-for-writing-messages-larger-tha.patch
1 From b302b7c9025334ef6680955a906cee579bf3c85c 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
5 bytes
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
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
16 stop the madness.
17
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.
23
24 Implementing the logic from the downstream i2c-bcm2708 driver solved
25 the hang problem.
26
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>
30 ---
31 drivers/i2c/busses/i2c-bcm2835.c | 22 ++++++++++++++--------
32 1 file changed, 14 insertions(+), 8 deletions(-)
33
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 {
37 int irq;
38 struct i2c_adapter adapter;
39 struct completion completion;
40 + struct i2c_msg *curr_msg;
41 u32 msg_err;
42 u8 *msg_buf;
43 size_t msg_buf_remaining;
44 @@ -126,14 +127,13 @@ static irqreturn_t bcm2835_i2c_isr(int t
45 return IRQ_HANDLED;
46 }
47
48 - if (val & BCM2835_I2C_S_RXD) {
49 - bcm2835_drain_rxfifo(i2c_dev);
50 - if (!(val & BCM2835_I2C_S_DONE))
51 - return IRQ_HANDLED;
52 - }
53 -
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);
59 + }
60 +
61 + if ((val & BCM2835_I2C_S_RXD) || i2c_dev->msg_buf_remaining)
62 i2c_dev->msg_err = BCM2835_I2C_S_LEN;
63 else
64 i2c_dev->msg_err = 0;
65 @@ -141,11 +141,16 @@ static irqreturn_t bcm2835_i2c_isr(int t
66 return IRQ_HANDLED;
67 }
68
69 - if (val & BCM2835_I2C_S_TXD) {
70 + if (val & BCM2835_I2C_S_TXW) {
71 bcm2835_fill_txfifo(i2c_dev);
72 return IRQ_HANDLED;
73 }
74
75 + if (val & BCM2835_I2C_S_RXR) {
76 + bcm2835_drain_rxfifo(i2c_dev);
77 + return IRQ_HANDLED;
78 + }
79 +
80 return IRQ_NONE;
81 }
82
83 @@ -155,6 +160,7 @@ static int bcm2835_i2c_xfer_msg(struct b
84 u32 c;
85 unsigned long time_left;
86
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);