1 From 58ee33ad32d4a00735252718f8bac3f7592af6e7 Mon Sep 17 00:00:00 2001
2 From: jeanleflambeur <catalin.vasile@gmail.com>
3 Date: Sun, 1 Feb 2015 12:35:38 +0100
4 Subject: [PATCH 112/114] Fix grabbing lock from atomic context in i2c driver
7 - check for timeouts in the bcm2708_bsc_setup function as indicated by this comment:
8 /* poll for transfer start bit (should only take 1-20 polls) */
9 This implies that the setup function can now fail so account for this everywhere it's called
10 - Removed the clk_get_rate call from inside the setup function as it locks a mutex and that's not ok since we call it from under a spin lock.
12 removed dead code and update comment
16 drivers/i2c/busses/i2c-bcm2708.c | 90 +++++++++++++++++++++++++++++-----------
17 1 file changed, 65 insertions(+), 25 deletions(-)
19 diff --git a/drivers/i2c/busses/i2c-bcm2708.c b/drivers/i2c/busses/i2c-bcm2708.c
20 index fda59ba..81e9374 100644
21 --- a/drivers/i2c/busses/i2c-bcm2708.c
22 +++ b/drivers/i2c/busses/i2c-bcm2708.c
24 #define BSC_S_TA 0x00000001
26 #define I2C_TIMEOUT_MS 150
27 +#define I2C_WAIT_LOOP_COUNT 40
29 #define DRV_NAME "bcm2708_i2c"
31 @@ -86,6 +87,7 @@ struct bcm2708_i2c {
37 struct completion done;
39 @@ -109,10 +111,10 @@ static void bcm2708_i2c_init_pinmode(int id)
41 u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
43 - BUG_ON(id != 0 && id != 1);
44 + BUG_ON(id != 0 && id != 1);
45 /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
46 for (pin = id*2+0; pin <= id*2+1; pin++) {
47 -printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
48 + printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
49 INP_GPIO(pin); /* set mode to GPIO input first */
50 SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
52 @@ -151,16 +153,16 @@ static inline void bcm2708_bsc_fifo_fill(struct bcm2708_i2c *bi)
53 bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
56 -static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
57 +static inline int bcm2708_bsc_setup(struct bcm2708_i2c *bi)
59 - unsigned long bus_hz;
61 u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
62 + int wait_loops = I2C_WAIT_LOOP_COUNT;
64 - bus_hz = clk_get_rate(bi->clk);
65 - cdiv = bus_hz / baudrate;
68 + /* Can't call clk_get_rate as it locks a mutex and here we are spinlocked.
69 + * Use the value that we cached in the probe.
73 if (bi->msg->flags & I2C_M_RD)
74 c |= BSC_C_INTR | BSC_C_READ;
75 @@ -177,17 +179,25 @@ static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
76 - Both messages to same slave address
77 - Write message can fit inside FIFO (16 bytes or less) */
78 if ( (bi->nmsgs > 1) &&
79 - !(bi->msg[0].flags & I2C_M_RD) && (bi->msg[1].flags & I2C_M_RD) &&
80 - (bi->msg[0].addr == bi->msg[1].addr) && (bi->msg[0].len <= 16)) {
81 + !(bi->msg[0].flags & I2C_M_RD) && (bi->msg[1].flags & I2C_M_RD) &&
82 + (bi->msg[0].addr == bi->msg[1].addr) && (bi->msg[0].len <= 16)) {
83 /* Fill FIFO with entire write message (16 byte FIFO) */
84 - while (bi->pos < bi->msg->len)
85 + while (bi->pos < bi->msg->len) {
86 bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
88 /* Start write transfer (no interrupts, don't clear FIFO) */
89 bcm2708_wr(bi, BSC_C, BSC_C_I2CEN | BSC_C_ST);
91 /* poll for transfer start bit (should only take 1-20 polls) */
93 s = bcm2708_rd(bi, BSC_S);
94 - } while (!(s & (BSC_S_TA | BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE)));
95 + } while (!(s & (BSC_S_TA | BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE)) && --wait_loops >= 0);
97 + /* did we time out or some error occured? */
98 + if (wait_loops < 0 || (s & (BSC_S_ERR | BSC_S_CLKT))) {
102 /* Send next read message before the write transfer finishes. */
105 @@ -197,6 +207,8 @@ static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
108 bcm2708_wr(bi, BSC_C, c);
113 static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
114 @@ -204,13 +216,15 @@ static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
115 struct bcm2708_i2c *bi = dev_id;
120 spin_lock(&bi->lock);
122 /* we may see camera interrupts on the "other" I2C channel
123 - Just return if we've not sent anything */
124 - if (!bi->nmsgs || !bi->msg )
125 + Just return if we've not sent anything */
126 + if (!bi->nmsgs || !bi->msg) {
130 s = bcm2708_rd(bi, BSC_S);
132 @@ -218,13 +232,16 @@ static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
133 bcm2708_bsc_reset(bi);
136 + bi->msg = 0; /* to inform the that all work is done */
140 } else if (s & BSC_S_DONE) {
143 - if (bi->msg->flags & I2C_M_RD)
144 + if (bi->msg->flags & I2C_M_RD) {
145 bcm2708_bsc_fifo_drain(bi);
148 bcm2708_bsc_reset(bi);
150 @@ -232,8 +249,19 @@ static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
151 /* advance to next message */
154 - bcm2708_bsc_setup(bi);
155 + ret = bcm2708_bsc_setup(bi);
157 + bcm2708_bsc_reset(bi);
159 + bi->msg = 0; /* to inform the that all work is done */
161 + /* wake up our bh */
162 + complete(&bi->done);
166 + bi->msg = 0; /* to inform the that all work is done */
171 @@ -266,22 +294,33 @@ static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
175 - bcm2708_bsc_setup(bi);
176 + ret = bcm2708_bsc_setup(bi);
178 - /* unlockig _after_ the setup to avoid races with the interrupt routine */
179 spin_unlock_irqrestore(&bi->lock, flags);
181 - ret = wait_for_completion_timeout(&bi->done,
182 - msecs_to_jiffies(I2C_TIMEOUT_MS));
183 + /* check the result of the setup */
186 + dev_err(&adap->dev, "transfer setup timed out\n");
187 + goto error_timeout;
190 + ret = wait_for_completion_timeout(&bi->done, msecs_to_jiffies(I2C_TIMEOUT_MS));
192 dev_err(&adap->dev, "transfer timed out\n");
193 - spin_lock_irqsave(&bi->lock, flags);
194 - bcm2708_bsc_reset(bi);
195 - spin_unlock_irqrestore(&bi->lock, flags);
197 + goto error_timeout;
200 - return bi->error ? -EIO : num;
201 + ret = bi->error ? -EIO : num;
205 + spin_lock_irqsave(&bi->lock, flags);
206 + bcm2708_bsc_reset(bi);
207 + bi->msg = 0; /* to inform the interrupt handler that there's nothing else to be done */
209 + spin_unlock_irqrestore(&bi->lock, flags);
213 static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
214 @@ -406,6 +445,7 @@ static int bcm2708_i2c_probe(struct platform_device *pdev)
216 baudrate = bus_hz / cdiv;
220 dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
221 pdev->id, (unsigned long)regs->start, irq, baudrate);