]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/bcma/main.c
Merge tag 'iwlwifi-next-for-kalle-2015-08-04' of https://git.kernel.org/pub/scm/linux...
[karo-tx-linux.git] / drivers / bcma / main.c
1 /*
2  * Broadcom specific AMBA
3  * Bus subsystem
4  *
5  * Licensed under the GNU/GPL. See COPYING for details.
6  */
7
8 #include "bcma_private.h"
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/bcma/bcma.h>
12 #include <linux/slab.h>
13 #include <linux/of_address.h>
14 #include <linux/of_irq.h>
15 #include <linux/of_platform.h>
16
17 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
18 MODULE_LICENSE("GPL");
19
20 /* contains the number the next bus should get. */
21 static unsigned int bcma_bus_next_num = 0;
22
23 /* bcma_buses_mutex locks the bcma_bus_next_num */
24 static DEFINE_MUTEX(bcma_buses_mutex);
25
26 static int bcma_bus_match(struct device *dev, struct device_driver *drv);
27 static int bcma_device_probe(struct device *dev);
28 static int bcma_device_remove(struct device *dev);
29 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
30
31 static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
32 {
33         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
34         return sprintf(buf, "0x%03X\n", core->id.manuf);
35 }
36 static DEVICE_ATTR_RO(manuf);
37
38 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
39 {
40         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
41         return sprintf(buf, "0x%03X\n", core->id.id);
42 }
43 static DEVICE_ATTR_RO(id);
44
45 static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
46 {
47         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
48         return sprintf(buf, "0x%02X\n", core->id.rev);
49 }
50 static DEVICE_ATTR_RO(rev);
51
52 static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
53 {
54         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
55         return sprintf(buf, "0x%X\n", core->id.class);
56 }
57 static DEVICE_ATTR_RO(class);
58
59 static struct attribute *bcma_device_attrs[] = {
60         &dev_attr_manuf.attr,
61         &dev_attr_id.attr,
62         &dev_attr_rev.attr,
63         &dev_attr_class.attr,
64         NULL,
65 };
66 ATTRIBUTE_GROUPS(bcma_device);
67
68 static struct bus_type bcma_bus_type = {
69         .name           = "bcma",
70         .match          = bcma_bus_match,
71         .probe          = bcma_device_probe,
72         .remove         = bcma_device_remove,
73         .uevent         = bcma_device_uevent,
74         .dev_groups     = bcma_device_groups,
75 };
76
77 static u16 bcma_cc_core_id(struct bcma_bus *bus)
78 {
79         if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
80                 return BCMA_CORE_4706_CHIPCOMMON;
81         return BCMA_CORE_CHIPCOMMON;
82 }
83
84 struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
85                                         u8 unit)
86 {
87         struct bcma_device *core;
88
89         list_for_each_entry(core, &bus->cores, list) {
90                 if (core->id.id == coreid && core->core_unit == unit)
91                         return core;
92         }
93         return NULL;
94 }
95 EXPORT_SYMBOL_GPL(bcma_find_core_unit);
96
97 bool bcma_wait_value(struct bcma_device *core, u16 reg, u32 mask, u32 value,
98                      int timeout)
99 {
100         unsigned long deadline = jiffies + timeout;
101         u32 val;
102
103         do {
104                 val = bcma_read32(core, reg);
105                 if ((val & mask) == value)
106                         return true;
107                 cpu_relax();
108                 udelay(10);
109         } while (!time_after_eq(jiffies, deadline));
110
111         bcma_warn(core->bus, "Timeout waiting for register 0x%04X!\n", reg);
112
113         return false;
114 }
115
116 static void bcma_release_core_dev(struct device *dev)
117 {
118         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
119         if (core->io_addr)
120                 iounmap(core->io_addr);
121         if (core->io_wrap)
122                 iounmap(core->io_wrap);
123         kfree(core);
124 }
125
126 static bool bcma_is_core_needed_early(u16 core_id)
127 {
128         switch (core_id) {
129         case BCMA_CORE_NS_NAND:
130         case BCMA_CORE_NS_QSPI:
131                 return true;
132         }
133
134         return false;
135 }
136
137 #if defined(CONFIG_OF) && defined(CONFIG_OF_ADDRESS)
138 static struct device_node *bcma_of_find_child_device(struct platform_device *parent,
139                                                      struct bcma_device *core)
140 {
141         struct device_node *node;
142         u64 size;
143         const __be32 *reg;
144
145         if (!parent || !parent->dev.of_node)
146                 return NULL;
147
148         for_each_child_of_node(parent->dev.of_node, node) {
149                 reg = of_get_address(node, 0, &size, NULL);
150                 if (!reg)
151                         continue;
152                 if (of_translate_address(node, reg) == core->addr)
153                         return node;
154         }
155         return NULL;
156 }
157
158 static int bcma_of_irq_parse(struct platform_device *parent,
159                              struct bcma_device *core,
160                              struct of_phandle_args *out_irq, int num)
161 {
162         __be32 laddr[1];
163         int rc;
164
165         if (core->dev.of_node) {
166                 rc = of_irq_parse_one(core->dev.of_node, num, out_irq);
167                 if (!rc)
168                         return rc;
169         }
170
171         out_irq->np = parent->dev.of_node;
172         out_irq->args_count = 1;
173         out_irq->args[0] = num;
174
175         laddr[0] = cpu_to_be32(core->addr);
176         return of_irq_parse_raw(laddr, out_irq);
177 }
178
179 static unsigned int bcma_of_get_irq(struct platform_device *parent,
180                                     struct bcma_device *core, int num)
181 {
182         struct of_phandle_args out_irq;
183         int ret;
184
185         if (!parent || !parent->dev.of_node)
186                 return 0;
187
188         ret = bcma_of_irq_parse(parent, core, &out_irq, num);
189         if (ret) {
190                 bcma_debug(core->bus, "bcma_of_get_irq() failed with rc=%d\n",
191                            ret);
192                 return 0;
193         }
194
195         return irq_create_of_mapping(&out_irq);
196 }
197
198 static void bcma_of_fill_device(struct platform_device *parent,
199                                 struct bcma_device *core)
200 {
201         struct device_node *node;
202
203         node = bcma_of_find_child_device(parent, core);
204         if (node)
205                 core->dev.of_node = node;
206
207         core->irq = bcma_of_get_irq(parent, core, 0);
208 }
209 #else
210 static void bcma_of_fill_device(struct platform_device *parent,
211                                 struct bcma_device *core)
212 {
213 }
214 static inline unsigned int bcma_of_get_irq(struct platform_device *parent,
215                                            struct bcma_device *core, int num)
216 {
217         return 0;
218 }
219 #endif /* CONFIG_OF */
220
221 unsigned int bcma_core_irq(struct bcma_device *core, int num)
222 {
223         struct bcma_bus *bus = core->bus;
224         unsigned int mips_irq;
225
226         switch (bus->hosttype) {
227         case BCMA_HOSTTYPE_PCI:
228                 return bus->host_pci->irq;
229         case BCMA_HOSTTYPE_SOC:
230                 if (bus->drv_mips.core && num == 0) {
231                         mips_irq = bcma_core_mips_irq(core);
232                         return mips_irq <= 4 ? mips_irq + 2 : 0;
233                 }
234                 if (bus->host_pdev)
235                         return bcma_of_get_irq(bus->host_pdev, core, num);
236                 return 0;
237         case BCMA_HOSTTYPE_SDIO:
238                 return 0;
239         }
240
241         return 0;
242 }
243 EXPORT_SYMBOL(bcma_core_irq);
244
245 void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core)
246 {
247         core->dev.release = bcma_release_core_dev;
248         core->dev.bus = &bcma_bus_type;
249         dev_set_name(&core->dev, "bcma%d:%d", bus->num, core->core_index);
250
251         switch (bus->hosttype) {
252         case BCMA_HOSTTYPE_PCI:
253                 core->dev.parent = &bus->host_pci->dev;
254                 core->dma_dev = &bus->host_pci->dev;
255                 core->irq = bus->host_pci->irq;
256                 break;
257         case BCMA_HOSTTYPE_SOC:
258                 core->dev.dma_mask = &core->dev.coherent_dma_mask;
259                 if (bus->host_pdev) {
260                         core->dma_dev = &bus->host_pdev->dev;
261                         core->dev.parent = &bus->host_pdev->dev;
262                         bcma_of_fill_device(bus->host_pdev, core);
263                 } else {
264                         core->dma_dev = &core->dev;
265                 }
266                 break;
267         case BCMA_HOSTTYPE_SDIO:
268                 break;
269         }
270 }
271
272 void bcma_init_bus(struct bcma_bus *bus)
273 {
274         mutex_lock(&bcma_buses_mutex);
275         bus->num = bcma_bus_next_num++;
276         mutex_unlock(&bcma_buses_mutex);
277
278         INIT_LIST_HEAD(&bus->cores);
279         bus->nr_cores = 0;
280
281         bcma_detect_chip(bus);
282 }
283
284 static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
285 {
286         int err;
287
288         err = device_register(&core->dev);
289         if (err) {
290                 bcma_err(bus, "Could not register dev for core 0x%03X\n",
291                          core->id.id);
292                 put_device(&core->dev);
293                 return;
294         }
295         core->dev_registered = true;
296 }
297
298 static int bcma_register_devices(struct bcma_bus *bus)
299 {
300         struct bcma_device *core;
301         int err;
302
303         list_for_each_entry(core, &bus->cores, list) {
304                 /* We support that cores ourself */
305                 switch (core->id.id) {
306                 case BCMA_CORE_4706_CHIPCOMMON:
307                 case BCMA_CORE_CHIPCOMMON:
308                 case BCMA_CORE_NS_CHIPCOMMON_B:
309                 case BCMA_CORE_PCI:
310                 case BCMA_CORE_PCIE:
311                 case BCMA_CORE_PCIE2:
312                 case BCMA_CORE_MIPS_74K:
313                 case BCMA_CORE_4706_MAC_GBIT_COMMON:
314                         continue;
315                 }
316
317                 /* Early cores were already registered */
318                 if (bcma_is_core_needed_early(core->id.id))
319                         continue;
320
321                 /* Only first GMAC core on BCM4706 is connected and working */
322                 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
323                     core->core_unit > 0)
324                         continue;
325
326                 bcma_register_core(bus, core);
327         }
328
329 #ifdef CONFIG_BCMA_DRIVER_MIPS
330         if (bus->drv_cc.pflash.present) {
331                 err = platform_device_register(&bcma_pflash_dev);
332                 if (err)
333                         bcma_err(bus, "Error registering parallel flash\n");
334         }
335 #endif
336
337 #ifdef CONFIG_BCMA_SFLASH
338         if (bus->drv_cc.sflash.present) {
339                 err = platform_device_register(&bcma_sflash_dev);
340                 if (err)
341                         bcma_err(bus, "Error registering serial flash\n");
342         }
343 #endif
344
345 #ifdef CONFIG_BCMA_NFLASH
346         if (bus->drv_cc.nflash.present) {
347                 err = platform_device_register(&bcma_nflash_dev);
348                 if (err)
349                         bcma_err(bus, "Error registering NAND flash\n");
350         }
351 #endif
352         err = bcma_gpio_init(&bus->drv_cc);
353         if (err == -ENOTSUPP)
354                 bcma_debug(bus, "GPIO driver not activated\n");
355         else if (err)
356                 bcma_err(bus, "Error registering GPIO driver: %i\n", err);
357
358         if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
359                 err = bcma_chipco_watchdog_register(&bus->drv_cc);
360                 if (err)
361                         bcma_err(bus, "Error registering watchdog driver\n");
362         }
363
364         return 0;
365 }
366
367 void bcma_unregister_cores(struct bcma_bus *bus)
368 {
369         struct bcma_device *core, *tmp;
370
371         list_for_each_entry_safe(core, tmp, &bus->cores, list) {
372                 if (!core->dev_registered)
373                         continue;
374                 list_del(&core->list);
375                 device_unregister(&core->dev);
376         }
377         if (bus->hosttype == BCMA_HOSTTYPE_SOC)
378                 platform_device_unregister(bus->drv_cc.watchdog);
379
380         /* Now noone uses internally-handled cores, we can free them */
381         list_for_each_entry_safe(core, tmp, &bus->cores, list) {
382                 list_del(&core->list);
383                 kfree(core);
384         }
385 }
386
387 int bcma_bus_register(struct bcma_bus *bus)
388 {
389         int err;
390         struct bcma_device *core;
391
392         /* Scan for devices (cores) */
393         err = bcma_bus_scan(bus);
394         if (err) {
395                 bcma_err(bus, "Failed to scan: %d\n", err);
396                 return err;
397         }
398
399         /* Early init CC core */
400         core = bcma_find_core(bus, bcma_cc_core_id(bus));
401         if (core) {
402                 bus->drv_cc.core = core;
403                 bcma_core_chipcommon_early_init(&bus->drv_cc);
404         }
405
406         /* Early init PCIE core */
407         core = bcma_find_core(bus, BCMA_CORE_PCIE);
408         if (core) {
409                 bus->drv_pci[0].core = core;
410                 bcma_core_pci_early_init(&bus->drv_pci[0]);
411         }
412
413         /* TODO: remove check for IS_BUILTIN(CONFIG_BCMA) check when
414          * of_default_bus_match_table is exported or in some other way
415          * accessible. This is just a temporary workaround.
416          */
417         if (IS_BUILTIN(CONFIG_BCMA) && bus->host_pdev) {
418                 struct device *dev = &bus->host_pdev->dev;
419
420                 of_platform_populate(dev->of_node, of_default_bus_match_table,
421                                      NULL, dev);
422         }
423
424         /* Cores providing flash access go before SPROM init */
425         list_for_each_entry(core, &bus->cores, list) {
426                 if (bcma_is_core_needed_early(core->id.id))
427                         bcma_register_core(bus, core);
428         }
429
430         /* Try to get SPROM */
431         err = bcma_sprom_get(bus);
432         if (err == -ENOENT) {
433                 bcma_err(bus, "No SPROM available\n");
434         } else if (err)
435                 bcma_err(bus, "Failed to get SPROM: %d\n", err);
436
437         /* Init CC core */
438         core = bcma_find_core(bus, bcma_cc_core_id(bus));
439         if (core) {
440                 bus->drv_cc.core = core;
441                 bcma_core_chipcommon_init(&bus->drv_cc);
442         }
443
444         /* Init CC core */
445         core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
446         if (core) {
447                 bus->drv_cc_b.core = core;
448                 bcma_core_chipcommon_b_init(&bus->drv_cc_b);
449         }
450
451         /* Init MIPS core */
452         core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
453         if (core) {
454                 bus->drv_mips.core = core;
455                 bcma_core_mips_init(&bus->drv_mips);
456         }
457
458         /* Init PCIE core */
459         core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
460         if (core) {
461                 bus->drv_pci[0].core = core;
462                 bcma_core_pci_init(&bus->drv_pci[0]);
463         }
464
465         /* Init PCIE core */
466         core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
467         if (core) {
468                 bus->drv_pci[1].core = core;
469                 bcma_core_pci_init(&bus->drv_pci[1]);
470         }
471
472         /* Init PCIe Gen 2 core */
473         core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
474         if (core) {
475                 bus->drv_pcie2.core = core;
476                 bcma_core_pcie2_init(&bus->drv_pcie2);
477         }
478
479         /* Init GBIT MAC COMMON core */
480         core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
481         if (core) {
482                 bus->drv_gmac_cmn.core = core;
483                 bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
484         }
485
486         /* Register found cores */
487         bcma_register_devices(bus);
488
489         bcma_info(bus, "Bus registered\n");
490
491         return 0;
492 }
493
494 void bcma_bus_unregister(struct bcma_bus *bus)
495 {
496         int err;
497
498         err = bcma_gpio_unregister(&bus->drv_cc);
499         if (err == -EBUSY)
500                 bcma_err(bus, "Some GPIOs are still in use.\n");
501         else if (err)
502                 bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
503
504         bcma_core_chipcommon_b_free(&bus->drv_cc_b);
505
506         bcma_unregister_cores(bus);
507 }
508
509 /*
510  * This is a special version of bus registration function designed for SoCs.
511  * It scans bus and performs basic initialization of main cores only.
512  * Please note it requires memory allocation, however it won't try to sleep.
513  */
514 int __init bcma_bus_early_register(struct bcma_bus *bus)
515 {
516         int err;
517         struct bcma_device *core;
518
519         /* Scan for devices (cores) */
520         err = bcma_bus_scan(bus);
521         if (err) {
522                 bcma_err(bus, "Failed to scan bus: %d\n", err);
523                 return -1;
524         }
525
526         /* Early init CC core */
527         core = bcma_find_core(bus, bcma_cc_core_id(bus));
528         if (core) {
529                 bus->drv_cc.core = core;
530                 bcma_core_chipcommon_early_init(&bus->drv_cc);
531         }
532
533         /* Early init MIPS core */
534         core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
535         if (core) {
536                 bus->drv_mips.core = core;
537                 bcma_core_mips_early_init(&bus->drv_mips);
538         }
539
540         bcma_info(bus, "Early bus registered\n");
541
542         return 0;
543 }
544
545 #ifdef CONFIG_PM
546 int bcma_bus_suspend(struct bcma_bus *bus)
547 {
548         struct bcma_device *core;
549
550         list_for_each_entry(core, &bus->cores, list) {
551                 struct device_driver *drv = core->dev.driver;
552                 if (drv) {
553                         struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
554                         if (adrv->suspend)
555                                 adrv->suspend(core);
556                 }
557         }
558         return 0;
559 }
560
561 int bcma_bus_resume(struct bcma_bus *bus)
562 {
563         struct bcma_device *core;
564
565         /* Init CC core */
566         if (bus->drv_cc.core) {
567                 bus->drv_cc.setup_done = false;
568                 bcma_core_chipcommon_init(&bus->drv_cc);
569         }
570
571         list_for_each_entry(core, &bus->cores, list) {
572                 struct device_driver *drv = core->dev.driver;
573                 if (drv) {
574                         struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
575                         if (adrv->resume)
576                                 adrv->resume(core);
577                 }
578         }
579
580         return 0;
581 }
582 #endif
583
584 int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
585 {
586         drv->drv.name = drv->name;
587         drv->drv.bus = &bcma_bus_type;
588         drv->drv.owner = owner;
589
590         return driver_register(&drv->drv);
591 }
592 EXPORT_SYMBOL_GPL(__bcma_driver_register);
593
594 void bcma_driver_unregister(struct bcma_driver *drv)
595 {
596         driver_unregister(&drv->drv);
597 }
598 EXPORT_SYMBOL_GPL(bcma_driver_unregister);
599
600 static int bcma_bus_match(struct device *dev, struct device_driver *drv)
601 {
602         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
603         struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
604         const struct bcma_device_id *cid = &core->id;
605         const struct bcma_device_id *did;
606
607         for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
608             if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
609                 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
610                 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
611                 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
612                         return 1;
613         }
614         return 0;
615 }
616
617 static int bcma_device_probe(struct device *dev)
618 {
619         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
620         struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
621                                                drv);
622         int err = 0;
623
624         if (adrv->probe)
625                 err = adrv->probe(core);
626
627         return err;
628 }
629
630 static int bcma_device_remove(struct device *dev)
631 {
632         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
633         struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
634                                                drv);
635
636         if (adrv->remove)
637                 adrv->remove(core);
638
639         return 0;
640 }
641
642 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
643 {
644         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
645
646         return add_uevent_var(env,
647                               "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
648                               core->id.manuf, core->id.id,
649                               core->id.rev, core->id.class);
650 }
651
652 static int __init bcma_modinit(void)
653 {
654         int err;
655
656         err = bus_register(&bcma_bus_type);
657         if (err)
658                 return err;
659
660         err = bcma_host_soc_register_driver();
661         if (err) {
662                 pr_err("SoC host initialization failed\n");
663                 err = 0;
664         }
665 #ifdef CONFIG_BCMA_HOST_PCI
666         err = bcma_host_pci_init();
667         if (err) {
668                 pr_err("PCI host initialization failed\n");
669                 err = 0;
670         }
671 #endif
672
673         return err;
674 }
675 fs_initcall(bcma_modinit);
676
677 static void __exit bcma_modexit(void)
678 {
679 #ifdef CONFIG_BCMA_HOST_PCI
680         bcma_host_pci_exit();
681 #endif
682         bcma_host_soc_unregister_driver();
683         bus_unregister(&bcma_bus_type);
684 }
685 module_exit(bcma_modexit)