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 --- a/drivers/i2c/busses/i2c-bcm2708.c
20 +++ b/drivers/i2c/busses/i2c-bcm2708.c
22 #define BSC_S_TA 0x00000001
24 #define I2C_TIMEOUT_MS 150
25 +#define I2C_WAIT_LOOP_COUNT 40
27 #define DRV_NAME "bcm2708_i2c"
29 @@ -86,6 +87,7 @@ struct bcm2708_i2c {
35 struct completion done;
37 @@ -109,10 +111,10 @@ static void bcm2708_i2c_init_pinmode(int
39 u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
41 - BUG_ON(id != 0 && id != 1);
42 + BUG_ON(id != 0 && id != 1);
43 /* BSC0 is on GPIO 0 & 1, BSC1 is on GPIO 2 & 3 */
44 for (pin = id*2+0; pin <= id*2+1; pin++) {
45 -printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
46 + printk("bcm2708_i2c_init_pinmode(%d,%d)\n", id, pin);
47 INP_GPIO(pin); /* set mode to GPIO input first */
48 SET_GPIO_ALT(pin, 0); /* set mode to ALT 0 */
50 @@ -151,16 +153,16 @@ static inline void bcm2708_bsc_fifo_fill
51 bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
54 -static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
55 +static inline int bcm2708_bsc_setup(struct bcm2708_i2c *bi)
57 - unsigned long bus_hz;
59 u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
60 + int wait_loops = I2C_WAIT_LOOP_COUNT;
62 - bus_hz = clk_get_rate(bi->clk);
63 - cdiv = bus_hz / baudrate;
66 + /* Can't call clk_get_rate as it locks a mutex and here we are spinlocked.
67 + * Use the value that we cached in the probe.
71 if (bi->msg->flags & I2C_M_RD)
72 c |= BSC_C_INTR | BSC_C_READ;
73 @@ -177,17 +179,25 @@ static inline void bcm2708_bsc_setup(str
74 - Both messages to same slave address
75 - Write message can fit inside FIFO (16 bytes or less) */
76 if ( (bi->nmsgs > 1) &&
77 - !(bi->msg[0].flags & I2C_M_RD) && (bi->msg[1].flags & I2C_M_RD) &&
78 - (bi->msg[0].addr == bi->msg[1].addr) && (bi->msg[0].len <= 16)) {
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 /* Fill FIFO with entire write message (16 byte FIFO) */
82 - while (bi->pos < bi->msg->len)
83 + while (bi->pos < bi->msg->len) {
84 bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
86 /* Start write transfer (no interrupts, don't clear FIFO) */
87 bcm2708_wr(bi, BSC_C, BSC_C_I2CEN | BSC_C_ST);
89 /* poll for transfer start bit (should only take 1-20 polls) */
91 s = bcm2708_rd(bi, BSC_S);
92 - } while (!(s & (BSC_S_TA | BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE)));
93 + } while (!(s & (BSC_S_TA | BSC_S_ERR | BSC_S_CLKT | BSC_S_DONE)) && --wait_loops >= 0);
95 + /* did we time out or some error occured? */
96 + if (wait_loops < 0 || (s & (BSC_S_ERR | BSC_S_CLKT))) {
100 /* Send next read message before the write transfer finishes. */
103 @@ -197,6 +207,8 @@ static inline void bcm2708_bsc_setup(str
106 bcm2708_wr(bi, BSC_C, c);
111 static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
112 @@ -204,13 +216,15 @@ static irqreturn_t bcm2708_i2c_interrupt
113 struct bcm2708_i2c *bi = dev_id;
118 spin_lock(&bi->lock);
120 /* we may see camera interrupts on the "other" I2C channel
121 - Just return if we've not sent anything */
122 - if (!bi->nmsgs || !bi->msg )
123 + Just return if we've not sent anything */
124 + if (!bi->nmsgs || !bi->msg) {
128 s = bcm2708_rd(bi, BSC_S);
130 @@ -218,13 +232,16 @@ static irqreturn_t bcm2708_i2c_interrupt
131 bcm2708_bsc_reset(bi);
134 + bi->msg = 0; /* to inform the that all work is done */
138 } else if (s & BSC_S_DONE) {
141 - if (bi->msg->flags & I2C_M_RD)
142 + if (bi->msg->flags & I2C_M_RD) {
143 bcm2708_bsc_fifo_drain(bi);
146 bcm2708_bsc_reset(bi);
148 @@ -232,8 +249,19 @@ static irqreturn_t bcm2708_i2c_interrupt
149 /* advance to next message */
152 - bcm2708_bsc_setup(bi);
153 + ret = bcm2708_bsc_setup(bi);
155 + bcm2708_bsc_reset(bi);
157 + bi->msg = 0; /* to inform the that all work is done */
159 + /* wake up our bh */
160 + complete(&bi->done);
164 + bi->msg = 0; /* to inform the that all work is done */
169 @@ -266,22 +294,33 @@ static int bcm2708_i2c_master_xfer(struc
173 - bcm2708_bsc_setup(bi);
174 + ret = bcm2708_bsc_setup(bi);
176 - /* unlockig _after_ the setup to avoid races with the interrupt routine */
177 spin_unlock_irqrestore(&bi->lock, flags);
179 - ret = wait_for_completion_timeout(&bi->done,
180 - msecs_to_jiffies(I2C_TIMEOUT_MS));
181 + /* check the result of the setup */
184 + dev_err(&adap->dev, "transfer setup timed out\n");
185 + goto error_timeout;
188 + ret = wait_for_completion_timeout(&bi->done, msecs_to_jiffies(I2C_TIMEOUT_MS));
190 dev_err(&adap->dev, "transfer timed out\n");
191 - spin_lock_irqsave(&bi->lock, flags);
192 - bcm2708_bsc_reset(bi);
193 - spin_unlock_irqrestore(&bi->lock, flags);
195 + goto error_timeout;
198 - return bi->error ? -EIO : num;
199 + ret = bi->error ? -EIO : num;
203 + spin_lock_irqsave(&bi->lock, flags);
204 + bcm2708_bsc_reset(bi);
205 + bi->msg = 0; /* to inform the interrupt handler that there's nothing else to be done */
207 + spin_unlock_irqrestore(&bi->lock, flags);
211 static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
212 @@ -406,6 +445,7 @@ static int bcm2708_i2c_probe(struct plat
214 baudrate = bus_hz / cdiv;
218 dev_info(&pdev->dev, "BSC%d Controller at 0x%08lx (irq %d) (baudrate %d)\n",
219 pdev->id, (unsigned long)regs->start, irq, baudrate);