brcm2708: update to v3.18
[openwrt/svn-archive/archive.git] / target / linux / brcm2708 / patches-3.18 / 0112-Fix-grabbing-lock-from-atomic-context-in-i2c-driver.patch
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
5
6 2 main changes:
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.
11
12 removed dead code and update comment
13
14 fixed typo in comment
15 ---
16 drivers/i2c/busses/i2c-bcm2708.c | 90 +++++++++++++++++++++++++++++-----------
17 1 file changed, 65 insertions(+), 25 deletions(-)
18
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
23 @@ -68,6 +68,7 @@
24 #define BSC_S_TA 0x00000001
25
26 #define I2C_TIMEOUT_MS 150
27 +#define I2C_WAIT_LOOP_COUNT 40
28
29 #define DRV_NAME "bcm2708_i2c"
30
31 @@ -86,6 +87,7 @@ struct bcm2708_i2c {
32 void __iomem *base;
33 int irq;
34 struct clk *clk;
35 + u32 cdiv;
36
37 struct completion done;
38
39 @@ -109,10 +111,10 @@ static void bcm2708_i2c_init_pinmode(int id)
40 int pin;
41 u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
42
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 */
51 }
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++]);
54 }
55
56 -static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
57 +static inline int bcm2708_bsc_setup(struct bcm2708_i2c *bi)
58 {
59 - unsigned long bus_hz;
60 u32 cdiv, s;
61 u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
62 + int wait_loops = I2C_WAIT_LOOP_COUNT;
63
64 - bus_hz = clk_get_rate(bi->clk);
65 - cdiv = bus_hz / baudrate;
66 - if (cdiv > 0xffff)
67 - cdiv = 0xffff;
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.
70 + */
71 + cdiv = bi->cdiv;
72
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++]);
87 + }
88 /* Start write transfer (no interrupts, don't clear FIFO) */
89 bcm2708_wr(bi, BSC_C, BSC_C_I2CEN | BSC_C_ST);
90 +
91 /* poll for transfer start bit (should only take 1-20 polls) */
92 do {
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);
96 +
97 + /* did we time out or some error occured? */
98 + if (wait_loops < 0 || (s & (BSC_S_ERR | BSC_S_CLKT))) {
99 + return -1;
100 + }
101 +
102 /* Send next read message before the write transfer finishes. */
103 bi->nmsgs--;
104 bi->msg++;
105 @@ -197,6 +207,8 @@ static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
106 }
107 }
108 bcm2708_wr(bi, BSC_C, c);
109 +
110 + return 0;
111 }
112
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;
116 bool handled = true;
117 u32 s;
118 + int ret;
119
120 spin_lock(&bi->lock);
121
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) {
127 goto early_exit;
128 + }
129
130 s = bcm2708_rd(bi, BSC_S);
131
132 @@ -218,13 +232,16 @@ static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
133 bcm2708_bsc_reset(bi);
134 bi->error = true;
135
136 + bi->msg = 0; /* to inform the that all work is done */
137 + bi->nmsgs = 0;
138 /* wake up our bh */
139 complete(&bi->done);
140 } else if (s & BSC_S_DONE) {
141 bi->nmsgs--;
142
143 - if (bi->msg->flags & I2C_M_RD)
144 + if (bi->msg->flags & I2C_M_RD) {
145 bcm2708_bsc_fifo_drain(bi);
146 + }
147
148 bcm2708_bsc_reset(bi);
149
150 @@ -232,8 +249,19 @@ static irqreturn_t bcm2708_i2c_interrupt(int irq, void *dev_id)
151 /* advance to next message */
152 bi->msg++;
153 bi->pos = 0;
154 - bcm2708_bsc_setup(bi);
155 + ret = bcm2708_bsc_setup(bi);
156 + if (ret < 0) {
157 + bcm2708_bsc_reset(bi);
158 + bi->error = true;
159 + bi->msg = 0; /* to inform the that all work is done */
160 + bi->nmsgs = 0;
161 + /* wake up our bh */
162 + complete(&bi->done);
163 + goto early_exit;
164 + }
165 } else {
166 + bi->msg = 0; /* to inform the that all work is done */
167 + bi->nmsgs = 0;
168 /* wake up our bh */
169 complete(&bi->done);
170 }
171 @@ -266,22 +294,33 @@ static int bcm2708_i2c_master_xfer(struct i2c_adapter *adap,
172 bi->nmsgs = num;
173 bi->error = false;
174
175 - bcm2708_bsc_setup(bi);
176 + ret = bcm2708_bsc_setup(bi);
177
178 - /* unlockig _after_ the setup to avoid races with the interrupt routine */
179 spin_unlock_irqrestore(&bi->lock, flags);
180
181 - ret = wait_for_completion_timeout(&bi->done,
182 - msecs_to_jiffies(I2C_TIMEOUT_MS));
183 + /* check the result of the setup */
184 + if (ret < 0)
185 + {
186 + dev_err(&adap->dev, "transfer setup timed out\n");
187 + goto error_timeout;
188 + }
189 +
190 + ret = wait_for_completion_timeout(&bi->done, msecs_to_jiffies(I2C_TIMEOUT_MS));
191 if (ret == 0) {
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);
196 - return -ETIMEDOUT;
197 + goto error_timeout;
198 }
199
200 - return bi->error ? -EIO : num;
201 + ret = bi->error ? -EIO : num;
202 + return ret;
203 +
204 +error_timeout:
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 */
208 + bi->nmsgs = 0;
209 + spin_unlock_irqrestore(&bi->lock, flags);
210 + return -ETIMEDOUT;
211 }
212
213 static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
214 @@ -406,6 +445,7 @@ static int bcm2708_i2c_probe(struct platform_device *pdev)
215 cdiv = 0xffff;
216 baudrate = bus_hz / cdiv;
217 }
218 + bi->cdiv = cdiv;
219
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);
222 --
223 1.8.3.2
224