ramips: add support for PandoraBox PBR-M1
[15.05/openwrt.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 --- a/drivers/i2c/busses/i2c-bcm2708.c
20 +++ b/drivers/i2c/busses/i2c-bcm2708.c
21 @@ -68,6 +68,7 @@
22  #define BSC_S_TA               0x00000001
23  
24  #define I2C_TIMEOUT_MS 150
25 +#define I2C_WAIT_LOOP_COUNT 40
26  
27  #define DRV_NAME       "bcm2708_i2c"
28  
29 @@ -86,6 +87,7 @@ struct bcm2708_i2c {
30         void __iomem *base;
31         int irq;
32         struct clk *clk;
33 +       u32 cdiv;
34  
35         struct completion done;
36  
37 @@ -109,10 +111,10 @@ static void bcm2708_i2c_init_pinmode(int
38         int pin;
39         u32 *gpio = ioremap(GPIO_BASE, SZ_16K);
40  
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 */
49         }
50 @@ -151,16 +153,16 @@ static inline void bcm2708_bsc_fifo_fill
51                 bcm2708_wr(bi, BSC_FIFO, bi->msg->buf[bi->pos++]);
52  }
53  
54 -static inline void bcm2708_bsc_setup(struct bcm2708_i2c *bi)
55 +static inline int bcm2708_bsc_setup(struct bcm2708_i2c *bi)
56  {
57 -       unsigned long bus_hz;
58         u32 cdiv, s;
59         u32 c = BSC_C_I2CEN | BSC_C_INTD | BSC_C_ST | BSC_C_CLEAR_1;
60 +       int wait_loops = I2C_WAIT_LOOP_COUNT;
61  
62 -       bus_hz = clk_get_rate(bi->clk);
63 -       cdiv = bus_hz / baudrate;
64 -       if (cdiv > 0xffff)
65 -               cdiv = 0xffff;
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.
68 +        */
69 +       cdiv = bi->cdiv;
70  
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++]);
85 +                       }
86                         /* Start write transfer (no interrupts, don't clear FIFO) */
87                         bcm2708_wr(bi, BSC_C, BSC_C_I2CEN | BSC_C_ST);
88 +
89                         /* poll for transfer start bit (should only take 1-20 polls) */
90                         do {
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);
94 +
95 +                       /* did we time out or some error occured? */
96 +                       if (wait_loops < 0 || (s & (BSC_S_ERR | BSC_S_CLKT))) {
97 +                               return -1;
98 +                       }
99 +
100                         /* Send next read message before the write transfer finishes. */
101                         bi->nmsgs--;
102                         bi->msg++;
103 @@ -197,6 +207,8 @@ static inline void bcm2708_bsc_setup(str
104                 }
105         }
106         bcm2708_wr(bi, BSC_C, c);
107 +
108 +       return 0;
109  }
110  
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;
114         bool handled = true;
115         u32 s;
116 +       int ret;
117  
118         spin_lock(&bi->lock);
119  
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) {
125                 goto early_exit;
126 +       }
127  
128         s = bcm2708_rd(bi, BSC_S);
129  
130 @@ -218,13 +232,16 @@ static irqreturn_t bcm2708_i2c_interrupt
131                 bcm2708_bsc_reset(bi);
132                 bi->error = true;
133  
134 +               bi->msg = 0; /* to inform the that all work is done */
135 +               bi->nmsgs = 0;
136                 /* wake up our bh */
137                 complete(&bi->done);
138         } else if (s & BSC_S_DONE) {
139                 bi->nmsgs--;
140  
141 -               if (bi->msg->flags & I2C_M_RD)
142 +               if (bi->msg->flags & I2C_M_RD) {
143                         bcm2708_bsc_fifo_drain(bi);
144 +               }
145  
146                 bcm2708_bsc_reset(bi);
147  
148 @@ -232,8 +249,19 @@ static irqreturn_t bcm2708_i2c_interrupt
149                         /* advance to next message */
150                         bi->msg++;
151                         bi->pos = 0;
152 -                       bcm2708_bsc_setup(bi);
153 +                       ret = bcm2708_bsc_setup(bi);
154 +                       if (ret < 0) {
155 +                               bcm2708_bsc_reset(bi);
156 +                               bi->error = true;
157 +                               bi->msg = 0; /* to inform the that all work is done */
158 +                               bi->nmsgs = 0;
159 +                               /* wake up our bh */
160 +                               complete(&bi->done);
161 +                               goto early_exit;
162 +                       }
163                 } else {
164 +                       bi->msg = 0; /* to inform the that all work is done */
165 +                       bi->nmsgs = 0;
166                         /* wake up our bh */
167                         complete(&bi->done);
168                 }
169 @@ -266,22 +294,33 @@ static int bcm2708_i2c_master_xfer(struc
170         bi->nmsgs = num;
171         bi->error = false;
172  
173 -       bcm2708_bsc_setup(bi);
174 +       ret = bcm2708_bsc_setup(bi);
175  
176 -       /* unlockig _after_ the setup to avoid races with the interrupt routine */
177         spin_unlock_irqrestore(&bi->lock, flags);
178  
179 -       ret = wait_for_completion_timeout(&bi->done,
180 -                       msecs_to_jiffies(I2C_TIMEOUT_MS));
181 +       /* check the result of the setup */
182 +       if (ret < 0)
183 +       {
184 +               dev_err(&adap->dev, "transfer setup timed out\n");
185 +               goto error_timeout;
186 +       }
187 +
188 +       ret = wait_for_completion_timeout(&bi->done, msecs_to_jiffies(I2C_TIMEOUT_MS));
189         if (ret == 0) {
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);
194 -               return -ETIMEDOUT;
195 +               goto error_timeout;
196         }
197  
198 -       return bi->error ? -EIO : num;
199 +       ret = bi->error ? -EIO : num;
200 +       return ret;
201 +
202 +error_timeout:
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 */
206 +       bi->nmsgs = 0;
207 +       spin_unlock_irqrestore(&bi->lock, flags);
208 +       return -ETIMEDOUT;
209  }
210  
211  static u32 bcm2708_i2c_functionality(struct i2c_adapter *adap)
212 @@ -406,6 +445,7 @@ static int bcm2708_i2c_probe(struct plat
213                 cdiv = 0xffff;
214                 baudrate = bus_hz / cdiv;
215         }
216 +       bi->cdiv = cdiv;
217  
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);