kernel: backport BCM5357 fix for GPIOs
[openwrt.git] / target / linux / generic / patches-3.13 / 025-bcma_backport.patch
1 --- a/drivers/bcma/Kconfig
2 +++ b/drivers/bcma/Kconfig
3 @@ -75,6 +75,7 @@ config BCMA_DRIVER_GMAC_CMN
4  config BCMA_DRIVER_GPIO
5         bool "BCMA GPIO driver"
6         depends on BCMA && GPIOLIB
7 +       select IRQ_DOMAIN if BCMA_HOST_SOC
8         help
9           Driver to provide access to the GPIO pins of the bcma bus.
10  
11 --- a/drivers/bcma/bcma_private.h
12 +++ b/drivers/bcma/bcma_private.h
13 @@ -33,8 +33,6 @@ int __init bcma_bus_early_register(struc
14  int bcma_bus_suspend(struct bcma_bus *bus);
15  int bcma_bus_resume(struct bcma_bus *bus);
16  #endif
17 -struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
18 -                                       u8 unit);
19  
20  /* scan.c */
21  int bcma_bus_scan(struct bcma_bus *bus);
22 --- a/drivers/bcma/driver_chipcommon_sflash.c
23 +++ b/drivers/bcma/driver_chipcommon_sflash.c
24 @@ -38,7 +38,7 @@ static const struct bcma_sflash_tbl_e bc
25         { "M25P32", 0x15, 0x10000, 64, },
26         { "M25P64", 0x16, 0x10000, 128, },
27         { "M25FL128", 0x17, 0x10000, 256, },
28 -       { 0 },
29 +       { NULL },
30  };
31  
32  static const struct bcma_sflash_tbl_e bcma_sflash_sst_tbl[] = {
33 @@ -56,7 +56,7 @@ static const struct bcma_sflash_tbl_e bc
34         { "SST25VF016", 0x41, 0x1000, 512, },
35         { "SST25VF032", 0x4a, 0x1000, 1024, },
36         { "SST25VF064", 0x4b, 0x1000, 2048, },
37 -       { 0 },
38 +       { NULL },
39  };
40  
41  static const struct bcma_sflash_tbl_e bcma_sflash_at_tbl[] = {
42 @@ -67,7 +67,7 @@ static const struct bcma_sflash_tbl_e bc
43         { "AT45DB161", 0x2c, 512, 4096, },
44         { "AT45DB321", 0x34, 512, 8192, },
45         { "AT45DB642", 0x3c, 1024, 8192, },
46 -       { 0 },
47 +       { NULL },
48  };
49  
50  static void bcma_sflash_cmd(struct bcma_drv_cc *cc, u32 opcode)
51 --- a/drivers/bcma/driver_gpio.c
52 +++ b/drivers/bcma/driver_gpio.c
53 @@ -9,6 +9,9 @@
54   */
55  
56  #include <linux/gpio.h>
57 +#include <linux/irq.h>
58 +#include <linux/interrupt.h>
59 +#include <linux/irqdomain.h>
60  #include <linux/export.h>
61  #include <linux/bcma/bcma.h>
62  
63 @@ -73,19 +76,136 @@ static void bcma_gpio_free(struct gpio_c
64         bcma_chipco_gpio_pullup(cc, 1 << gpio, 0);
65  }
66  
67 +#if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
68  static int bcma_gpio_to_irq(struct gpio_chip *chip, unsigned gpio)
69  {
70         struct bcma_drv_cc *cc = bcma_gpio_get_cc(chip);
71  
72         if (cc->core->bus->hosttype == BCMA_HOSTTYPE_SOC)
73 -               return bcma_core_irq(cc->core);
74 +               return irq_find_mapping(cc->irq_domain, gpio);
75         else
76                 return -EINVAL;
77  }
78  
79 +static void bcma_gpio_irq_unmask(struct irq_data *d)
80 +{
81 +       struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
82 +       int gpio = irqd_to_hwirq(d);
83 +       u32 val = bcma_chipco_gpio_in(cc, BIT(gpio));
84 +
85 +       bcma_chipco_gpio_polarity(cc, BIT(gpio), val);
86 +       bcma_chipco_gpio_intmask(cc, BIT(gpio), BIT(gpio));
87 +}
88 +
89 +static void bcma_gpio_irq_mask(struct irq_data *d)
90 +{
91 +       struct bcma_drv_cc *cc = irq_data_get_irq_chip_data(d);
92 +       int gpio = irqd_to_hwirq(d);
93 +
94 +       bcma_chipco_gpio_intmask(cc, BIT(gpio), 0);
95 +}
96 +
97 +static struct irq_chip bcma_gpio_irq_chip = {
98 +       .name           = "BCMA-GPIO",
99 +       .irq_mask       = bcma_gpio_irq_mask,
100 +       .irq_unmask     = bcma_gpio_irq_unmask,
101 +};
102 +
103 +static irqreturn_t bcma_gpio_irq_handler(int irq, void *dev_id)
104 +{
105 +       struct bcma_drv_cc *cc = dev_id;
106 +       u32 val = bcma_cc_read32(cc, BCMA_CC_GPIOIN);
107 +       u32 mask = bcma_cc_read32(cc, BCMA_CC_GPIOIRQ);
108 +       u32 pol = bcma_cc_read32(cc, BCMA_CC_GPIOPOL);
109 +       unsigned long irqs = (val ^ pol) & mask;
110 +       int gpio;
111 +
112 +       if (!irqs)
113 +               return IRQ_NONE;
114 +
115 +       for_each_set_bit(gpio, &irqs, cc->gpio.ngpio)
116 +               generic_handle_irq(bcma_gpio_to_irq(&cc->gpio, gpio));
117 +       bcma_chipco_gpio_polarity(cc, irqs, val & irqs);
118 +
119 +       return IRQ_HANDLED;
120 +}
121 +
122 +static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
123 +{
124 +       struct gpio_chip *chip = &cc->gpio;
125 +       int gpio, hwirq, err;
126 +
127 +       if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
128 +               return 0;
129 +
130 +       cc->irq_domain = irq_domain_add_linear(NULL, chip->ngpio,
131 +                                              &irq_domain_simple_ops, cc);
132 +       if (!cc->irq_domain) {
133 +               err = -ENODEV;
134 +               goto err_irq_domain;
135 +       }
136 +       for (gpio = 0; gpio < chip->ngpio; gpio++) {
137 +               int irq = irq_create_mapping(cc->irq_domain, gpio);
138 +
139 +               irq_set_chip_data(irq, cc);
140 +               irq_set_chip_and_handler(irq, &bcma_gpio_irq_chip,
141 +                                        handle_simple_irq);
142 +       }
143 +
144 +       hwirq = bcma_core_irq(cc->core);
145 +       err = request_irq(hwirq, bcma_gpio_irq_handler, IRQF_SHARED, "gpio",
146 +                         cc);
147 +       if (err)
148 +               goto err_req_irq;
149 +
150 +       bcma_chipco_gpio_intmask(cc, ~0, 0);
151 +       bcma_cc_set32(cc, BCMA_CC_IRQMASK, BCMA_CC_IRQ_GPIO);
152 +
153 +       return 0;
154 +
155 +err_req_irq:
156 +       for (gpio = 0; gpio < chip->ngpio; gpio++) {
157 +               int irq = irq_find_mapping(cc->irq_domain, gpio);
158 +
159 +               irq_dispose_mapping(irq);
160 +       }
161 +       irq_domain_remove(cc->irq_domain);
162 +err_irq_domain:
163 +       return err;
164 +}
165 +
166 +static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
167 +{
168 +       struct gpio_chip *chip = &cc->gpio;
169 +       int gpio;
170 +
171 +       if (cc->core->bus->hosttype != BCMA_HOSTTYPE_SOC)
172 +               return;
173 +
174 +       bcma_cc_mask32(cc, BCMA_CC_IRQMASK, ~BCMA_CC_IRQ_GPIO);
175 +       free_irq(bcma_core_irq(cc->core), cc);
176 +       for (gpio = 0; gpio < chip->ngpio; gpio++) {
177 +               int irq = irq_find_mapping(cc->irq_domain, gpio);
178 +
179 +               irq_dispose_mapping(irq);
180 +       }
181 +       irq_domain_remove(cc->irq_domain);
182 +}
183 +#else
184 +static int bcma_gpio_irq_domain_init(struct bcma_drv_cc *cc)
185 +{
186 +       return 0;
187 +}
188 +
189 +static void bcma_gpio_irq_domain_exit(struct bcma_drv_cc *cc)
190 +{
191 +}
192 +#endif
193 +
194  int bcma_gpio_init(struct bcma_drv_cc *cc)
195  {
196         struct gpio_chip *chip = &cc->gpio;
197 +       int err;
198  
199         chip->label             = "bcma_gpio";
200         chip->owner             = THIS_MODULE;
201 @@ -95,8 +215,17 @@ int bcma_gpio_init(struct bcma_drv_cc *c
202         chip->set               = bcma_gpio_set_value;
203         chip->direction_input   = bcma_gpio_direction_input;
204         chip->direction_output  = bcma_gpio_direction_output;
205 +#if IS_BUILTIN(CONFIG_BCMA_HOST_SOC)
206         chip->to_irq            = bcma_gpio_to_irq;
207 -       chip->ngpio             = 16;
208 +#endif
209 +       switch (cc->core->bus->chipinfo.id) {
210 +       case BCMA_CHIP_ID_BCM5357:
211 +               chip->ngpio     = 32;
212 +               break;
213 +       default:
214 +               chip->ngpio     = 16;
215 +       }
216 +
217         /* There is just one SoC in one device and its GPIO addresses should be
218          * deterministic to address them more easily. The other buses could get
219          * a random base number. */
220 @@ -105,10 +234,21 @@ int bcma_gpio_init(struct bcma_drv_cc *c
221         else
222                 chip->base              = -1;
223  
224 -       return gpiochip_add(chip);
225 +       err = bcma_gpio_irq_domain_init(cc);
226 +       if (err)
227 +               return err;
228 +
229 +       err = gpiochip_add(chip);
230 +       if (err) {
231 +               bcma_gpio_irq_domain_exit(cc);
232 +               return err;
233 +       }
234 +
235 +       return 0;
236  }
237  
238  int bcma_gpio_unregister(struct bcma_drv_cc *cc)
239  {
240 +       bcma_gpio_irq_domain_exit(cc);
241         return gpiochip_remove(&cc->gpio);
242  }
243 --- a/drivers/bcma/host_pci.c
244 +++ b/drivers/bcma/host_pci.c
245 @@ -238,7 +238,6 @@ static void bcma_host_pci_remove(struct
246         pci_release_regions(dev);
247         pci_disable_device(dev);
248         kfree(bus);
249 -       pci_set_drvdata(dev, NULL);
250  }
251  
252  #ifdef CONFIG_PM_SLEEP
253 @@ -270,7 +269,7 @@ static SIMPLE_DEV_PM_OPS(bcma_pm_ops, bc
254  
255  #endif /* CONFIG_PM_SLEEP */
256  
257 -static DEFINE_PCI_DEVICE_TABLE(bcma_pci_bridge_tbl) = {
258 +static const struct pci_device_id bcma_pci_bridge_tbl[] = {
259         { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x0576) },
260         { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 0x4313) },
261         { PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, 43224) },
262 --- a/drivers/bcma/main.c
263 +++ b/drivers/bcma/main.c
264 @@ -78,18 +78,6 @@ static u16 bcma_cc_core_id(struct bcma_b
265         return BCMA_CORE_CHIPCOMMON;
266  }
267  
268 -struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
269 -{
270 -       struct bcma_device *core;
271 -
272 -       list_for_each_entry(core, &bus->cores, list) {
273 -               if (core->id.id == coreid)
274 -                       return core;
275 -       }
276 -       return NULL;
277 -}
278 -EXPORT_SYMBOL_GPL(bcma_find_core);
279 -
280  struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
281                                         u8 unit)
282  {
283 @@ -101,6 +89,7 @@ struct bcma_device *bcma_find_core_unit(
284         }
285         return NULL;
286  }
287 +EXPORT_SYMBOL_GPL(bcma_find_core_unit);
288  
289  bool bcma_wait_value(struct bcma_device *core, u16 reg, u32 mask, u32 value,
290                      int timeout)
291 @@ -176,6 +165,7 @@ static int bcma_register_cores(struct bc
292                         bcma_err(bus,
293                                  "Could not register dev for core 0x%03X\n",
294                                  core->id.id);
295 +                       put_device(&core->dev);
296                         continue;
297                 }
298                 core->dev_registered = true;
299 --- a/include/linux/bcma/bcma.h
300 +++ b/include/linux/bcma/bcma.h
301 @@ -418,7 +418,14 @@ static inline void bcma_maskset16(struct
302         bcma_write16(cc, offset, (bcma_read16(cc, offset) & mask) | set);
303  }
304  
305 -extern struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid);
306 +extern struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
307 +                                              u8 unit);
308 +static inline struct bcma_device *bcma_find_core(struct bcma_bus *bus,
309 +                                                u16 coreid)
310 +{
311 +       return bcma_find_core_unit(bus, coreid, 0);
312 +}
313 +
314  extern bool bcma_core_is_enabled(struct bcma_device *core);
315  extern void bcma_core_disable(struct bcma_device *core, u32 flags);
316  extern int bcma_core_enable(struct bcma_device *core, u32 flags);
317 --- a/include/linux/bcma/bcma_driver_chipcommon.h
318 +++ b/include/linux/bcma/bcma_driver_chipcommon.h
319 @@ -640,6 +640,7 @@ struct bcma_drv_cc {
320         spinlock_t gpio_lock;
321  #ifdef CONFIG_BCMA_DRIVER_GPIO
322         struct gpio_chip gpio;
323 +       struct irq_domain *irq_domain;
324  #endif
325  };
326