2 * Broadcom specific AMBA
5 * Licensed under the GNU/GPL. See COPYING for details.
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>
16 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
17 MODULE_LICENSE("GPL");
19 /* contains the number the next bus should get. */
20 static unsigned int bcma_bus_next_num
= 0;
22 /* bcma_buses_mutex locks the bcma_bus_next_num */
23 static DEFINE_MUTEX(bcma_buses_mutex
);
25 static int bcma_bus_match(struct device
*dev
, struct device_driver
*drv
);
26 static int bcma_device_probe(struct device
*dev
);
27 static int bcma_device_remove(struct device
*dev
);
28 static int bcma_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
);
30 static ssize_t
manuf_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
32 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
33 return sprintf(buf
, "0x%03X\n", core
->id
.manuf
);
35 static DEVICE_ATTR_RO(manuf
);
37 static ssize_t
id_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
39 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
40 return sprintf(buf
, "0x%03X\n", core
->id
.id
);
42 static DEVICE_ATTR_RO(id
);
44 static ssize_t
rev_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
46 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
47 return sprintf(buf
, "0x%02X\n", core
->id
.rev
);
49 static DEVICE_ATTR_RO(rev
);
51 static ssize_t
class_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
53 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
54 return sprintf(buf
, "0x%X\n", core
->id
.class);
56 static DEVICE_ATTR_RO(class);
58 static struct attribute
*bcma_device_attrs
[] = {
65 ATTRIBUTE_GROUPS(bcma_device
);
67 static struct bus_type bcma_bus_type
= {
69 .match
= bcma_bus_match
,
70 .probe
= bcma_device_probe
,
71 .remove
= bcma_device_remove
,
72 .uevent
= bcma_device_uevent
,
73 .dev_groups
= bcma_device_groups
,
76 static u16
bcma_cc_core_id(struct bcma_bus
*bus
)
78 if (bus
->chipinfo
.id
== BCMA_CHIP_ID_BCM4706
)
79 return BCMA_CORE_4706_CHIPCOMMON
;
80 return BCMA_CORE_CHIPCOMMON
;
83 struct bcma_device
*bcma_find_core_unit(struct bcma_bus
*bus
, u16 coreid
,
86 struct bcma_device
*core
;
88 list_for_each_entry(core
, &bus
->cores
, list
) {
89 if (core
->id
.id
== coreid
&& core
->core_unit
== unit
)
94 EXPORT_SYMBOL_GPL(bcma_find_core_unit
);
96 bool bcma_wait_value(struct bcma_device
*core
, u16 reg
, u32 mask
, u32 value
,
99 unsigned long deadline
= jiffies
+ timeout
;
103 val
= bcma_read32(core
, reg
);
104 if ((val
& mask
) == value
)
108 } while (!time_after_eq(jiffies
, deadline
));
110 bcma_warn(core
->bus
, "Timeout waiting for register 0x%04X!\n", reg
);
115 static void bcma_release_core_dev(struct device
*dev
)
117 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
119 iounmap(core
->io_addr
);
121 iounmap(core
->io_wrap
);
125 static bool bcma_is_core_needed_early(u16 core_id
)
128 case BCMA_CORE_NS_NAND
:
129 case BCMA_CORE_NS_QSPI
:
136 #if defined(CONFIG_OF) && defined(CONFIG_OF_ADDRESS)
137 static struct device_node
*bcma_of_find_child_device(struct platform_device
*parent
,
138 struct bcma_device
*core
)
140 struct device_node
*node
;
144 if (!parent
|| !parent
->dev
.of_node
)
147 for_each_child_of_node(parent
->dev
.of_node
, node
) {
148 reg
= of_get_address(node
, 0, &size
, NULL
);
151 if (of_translate_address(node
, reg
) == core
->addr
)
157 static int bcma_of_irq_parse(struct platform_device
*parent
,
158 struct bcma_device
*core
,
159 struct of_phandle_args
*out_irq
, int num
)
164 if (core
->dev
.of_node
) {
165 rc
= of_irq_parse_one(core
->dev
.of_node
, num
, out_irq
);
170 out_irq
->np
= parent
->dev
.of_node
;
171 out_irq
->args_count
= 1;
172 out_irq
->args
[0] = num
;
174 laddr
[0] = cpu_to_be32(core
->addr
);
175 return of_irq_parse_raw(laddr
, out_irq
);
178 static unsigned int bcma_of_get_irq(struct platform_device
*parent
,
179 struct bcma_device
*core
, int num
)
181 struct of_phandle_args out_irq
;
184 if (!parent
|| !parent
->dev
.of_node
)
187 ret
= bcma_of_irq_parse(parent
, core
, &out_irq
, num
);
189 bcma_debug(core
->bus
, "bcma_of_get_irq() failed with rc=%d\n",
194 return irq_create_of_mapping(&out_irq
);
197 static void bcma_of_fill_device(struct platform_device
*parent
,
198 struct bcma_device
*core
)
200 struct device_node
*node
;
202 node
= bcma_of_find_child_device(parent
, core
);
204 core
->dev
.of_node
= node
;
206 core
->irq
= bcma_of_get_irq(parent
, core
, 0);
209 static void bcma_of_fill_device(struct platform_device
*parent
,
210 struct bcma_device
*core
)
213 static inline unsigned int bcma_of_get_irq(struct platform_device
*parent
,
214 struct bcma_device
*core
, int num
)
218 #endif /* CONFIG_OF */
220 unsigned int bcma_core_irq(struct bcma_device
*core
, int num
)
222 struct bcma_bus
*bus
= core
->bus
;
223 unsigned int mips_irq
;
225 switch (bus
->hosttype
) {
226 case BCMA_HOSTTYPE_PCI
:
227 return bus
->host_pci
->irq
;
228 case BCMA_HOSTTYPE_SOC
:
229 if (bus
->drv_mips
.core
&& num
== 0) {
230 mips_irq
= bcma_core_mips_irq(core
);
231 return mips_irq
<= 4 ? mips_irq
+ 2 : 0;
234 return bcma_of_get_irq(bus
->host_pdev
, core
, num
);
236 case BCMA_HOSTTYPE_SDIO
:
242 EXPORT_SYMBOL(bcma_core_irq
);
244 void bcma_prepare_core(struct bcma_bus
*bus
, struct bcma_device
*core
)
246 core
->dev
.release
= bcma_release_core_dev
;
247 core
->dev
.bus
= &bcma_bus_type
;
248 dev_set_name(&core
->dev
, "bcma%d:%d", bus
->num
, core
->core_index
);
250 switch (bus
->hosttype
) {
251 case BCMA_HOSTTYPE_PCI
:
252 core
->dev
.parent
= &bus
->host_pci
->dev
;
253 core
->dma_dev
= &bus
->host_pci
->dev
;
254 core
->irq
= bus
->host_pci
->irq
;
256 case BCMA_HOSTTYPE_SOC
:
257 core
->dev
.dma_mask
= &core
->dev
.coherent_dma_mask
;
258 if (bus
->host_pdev
) {
259 core
->dma_dev
= &bus
->host_pdev
->dev
;
260 core
->dev
.parent
= &bus
->host_pdev
->dev
;
261 bcma_of_fill_device(bus
->host_pdev
, core
);
263 core
->dma_dev
= &core
->dev
;
266 case BCMA_HOSTTYPE_SDIO
:
271 void bcma_init_bus(struct bcma_bus
*bus
)
273 mutex_lock(&bcma_buses_mutex
);
274 bus
->num
= bcma_bus_next_num
++;
275 mutex_unlock(&bcma_buses_mutex
);
277 INIT_LIST_HEAD(&bus
->cores
);
280 bcma_detect_chip(bus
);
283 static void bcma_register_core(struct bcma_bus
*bus
, struct bcma_device
*core
)
287 err
= device_register(&core
->dev
);
289 bcma_err(bus
, "Could not register dev for core 0x%03X\n",
291 put_device(&core
->dev
);
294 core
->dev_registered
= true;
297 static int bcma_register_devices(struct bcma_bus
*bus
)
299 struct bcma_device
*core
;
302 list_for_each_entry(core
, &bus
->cores
, list
) {
303 /* We support that cores ourself */
304 switch (core
->id
.id
) {
305 case BCMA_CORE_4706_CHIPCOMMON
:
306 case BCMA_CORE_CHIPCOMMON
:
307 case BCMA_CORE_NS_CHIPCOMMON_B
:
310 case BCMA_CORE_PCIE2
:
311 case BCMA_CORE_MIPS_74K
:
312 case BCMA_CORE_4706_MAC_GBIT_COMMON
:
316 /* Early cores were already registered */
317 if (bcma_is_core_needed_early(core
->id
.id
))
320 /* Only first GMAC core on BCM4706 is connected and working */
321 if (core
->id
.id
== BCMA_CORE_4706_MAC_GBIT
&&
325 bcma_register_core(bus
, core
);
328 #ifdef CONFIG_BCMA_DRIVER_MIPS
329 if (bus
->drv_cc
.pflash
.present
) {
330 err
= platform_device_register(&bcma_pflash_dev
);
332 bcma_err(bus
, "Error registering parallel flash\n");
336 #ifdef CONFIG_BCMA_SFLASH
337 if (bus
->drv_cc
.sflash
.present
) {
338 err
= platform_device_register(&bcma_sflash_dev
);
340 bcma_err(bus
, "Error registering serial flash\n");
344 #ifdef CONFIG_BCMA_NFLASH
345 if (bus
->drv_cc
.nflash
.present
) {
346 err
= platform_device_register(&bcma_nflash_dev
);
348 bcma_err(bus
, "Error registering NAND flash\n");
351 err
= bcma_gpio_init(&bus
->drv_cc
);
352 if (err
== -ENOTSUPP
)
353 bcma_debug(bus
, "GPIO driver not activated\n");
355 bcma_err(bus
, "Error registering GPIO driver: %i\n", err
);
357 if (bus
->hosttype
== BCMA_HOSTTYPE_SOC
) {
358 err
= bcma_chipco_watchdog_register(&bus
->drv_cc
);
360 bcma_err(bus
, "Error registering watchdog driver\n");
366 void bcma_unregister_cores(struct bcma_bus
*bus
)
368 struct bcma_device
*core
, *tmp
;
370 list_for_each_entry_safe(core
, tmp
, &bus
->cores
, list
) {
371 if (!core
->dev_registered
)
373 list_del(&core
->list
);
374 device_unregister(&core
->dev
);
376 if (bus
->hosttype
== BCMA_HOSTTYPE_SOC
)
377 platform_device_unregister(bus
->drv_cc
.watchdog
);
379 /* Now noone uses internally-handled cores, we can free them */
380 list_for_each_entry_safe(core
, tmp
, &bus
->cores
, list
) {
381 list_del(&core
->list
);
386 int bcma_bus_register(struct bcma_bus
*bus
)
389 struct bcma_device
*core
;
391 /* Scan for devices (cores) */
392 err
= bcma_bus_scan(bus
);
394 bcma_err(bus
, "Failed to scan: %d\n", err
);
398 /* Early init CC core */
399 core
= bcma_find_core(bus
, bcma_cc_core_id(bus
));
401 bus
->drv_cc
.core
= core
;
402 bcma_core_chipcommon_early_init(&bus
->drv_cc
);
405 /* Early init PCIE core */
406 core
= bcma_find_core(bus
, BCMA_CORE_PCIE
);
408 bus
->drv_pci
[0].core
= core
;
409 bcma_core_pci_early_init(&bus
->drv_pci
[0]);
412 /* Cores providing flash access go before SPROM init */
413 list_for_each_entry(core
, &bus
->cores
, list
) {
414 if (bcma_is_core_needed_early(core
->id
.id
))
415 bcma_register_core(bus
, core
);
418 /* Try to get SPROM */
419 err
= bcma_sprom_get(bus
);
420 if (err
== -ENOENT
) {
421 bcma_err(bus
, "No SPROM available\n");
423 bcma_err(bus
, "Failed to get SPROM: %d\n", err
);
426 core
= bcma_find_core(bus
, bcma_cc_core_id(bus
));
428 bus
->drv_cc
.core
= core
;
429 bcma_core_chipcommon_init(&bus
->drv_cc
);
433 core
= bcma_find_core(bus
, BCMA_CORE_NS_CHIPCOMMON_B
);
435 bus
->drv_cc_b
.core
= core
;
436 bcma_core_chipcommon_b_init(&bus
->drv_cc_b
);
440 core
= bcma_find_core(bus
, BCMA_CORE_MIPS_74K
);
442 bus
->drv_mips
.core
= core
;
443 bcma_core_mips_init(&bus
->drv_mips
);
447 core
= bcma_find_core_unit(bus
, BCMA_CORE_PCIE
, 0);
449 bus
->drv_pci
[0].core
= core
;
450 bcma_core_pci_init(&bus
->drv_pci
[0]);
454 core
= bcma_find_core_unit(bus
, BCMA_CORE_PCIE
, 1);
456 bus
->drv_pci
[1].core
= core
;
457 bcma_core_pci_init(&bus
->drv_pci
[1]);
460 /* Init PCIe Gen 2 core */
461 core
= bcma_find_core_unit(bus
, BCMA_CORE_PCIE2
, 0);
463 bus
->drv_pcie2
.core
= core
;
464 bcma_core_pcie2_init(&bus
->drv_pcie2
);
467 /* Init GBIT MAC COMMON core */
468 core
= bcma_find_core(bus
, BCMA_CORE_4706_MAC_GBIT_COMMON
);
470 bus
->drv_gmac_cmn
.core
= core
;
471 bcma_core_gmac_cmn_init(&bus
->drv_gmac_cmn
);
474 /* Register found cores */
475 bcma_register_devices(bus
);
477 bcma_info(bus
, "Bus registered\n");
482 void bcma_bus_unregister(struct bcma_bus
*bus
)
486 err
= bcma_gpio_unregister(&bus
->drv_cc
);
488 bcma_err(bus
, "Some GPIOs are still in use.\n");
490 bcma_err(bus
, "Can not unregister GPIO driver: %i\n", err
);
492 bcma_core_chipcommon_b_free(&bus
->drv_cc_b
);
494 bcma_unregister_cores(bus
);
498 * This is a special version of bus registration function designed for SoCs.
499 * It scans bus and performs basic initialization of main cores only.
500 * Please note it requires memory allocation, however it won't try to sleep.
502 int __init
bcma_bus_early_register(struct bcma_bus
*bus
)
505 struct bcma_device
*core
;
507 /* Scan for devices (cores) */
508 err
= bcma_bus_scan(bus
);
510 bcma_err(bus
, "Failed to scan bus: %d\n", err
);
514 /* Early init CC core */
515 core
= bcma_find_core(bus
, bcma_cc_core_id(bus
));
517 bus
->drv_cc
.core
= core
;
518 bcma_core_chipcommon_early_init(&bus
->drv_cc
);
521 /* Early init MIPS core */
522 core
= bcma_find_core(bus
, BCMA_CORE_MIPS_74K
);
524 bus
->drv_mips
.core
= core
;
525 bcma_core_mips_early_init(&bus
->drv_mips
);
528 bcma_info(bus
, "Early bus registered\n");
534 int bcma_bus_suspend(struct bcma_bus
*bus
)
536 struct bcma_device
*core
;
538 list_for_each_entry(core
, &bus
->cores
, list
) {
539 struct device_driver
*drv
= core
->dev
.driver
;
541 struct bcma_driver
*adrv
= container_of(drv
, struct bcma_driver
, drv
);
549 int bcma_bus_resume(struct bcma_bus
*bus
)
551 struct bcma_device
*core
;
554 if (bus
->drv_cc
.core
) {
555 bus
->drv_cc
.setup_done
= false;
556 bcma_core_chipcommon_init(&bus
->drv_cc
);
559 list_for_each_entry(core
, &bus
->cores
, list
) {
560 struct device_driver
*drv
= core
->dev
.driver
;
562 struct bcma_driver
*adrv
= container_of(drv
, struct bcma_driver
, drv
);
572 int __bcma_driver_register(struct bcma_driver
*drv
, struct module
*owner
)
574 drv
->drv
.name
= drv
->name
;
575 drv
->drv
.bus
= &bcma_bus_type
;
576 drv
->drv
.owner
= owner
;
578 return driver_register(&drv
->drv
);
580 EXPORT_SYMBOL_GPL(__bcma_driver_register
);
582 void bcma_driver_unregister(struct bcma_driver
*drv
)
584 driver_unregister(&drv
->drv
);
586 EXPORT_SYMBOL_GPL(bcma_driver_unregister
);
588 static int bcma_bus_match(struct device
*dev
, struct device_driver
*drv
)
590 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
591 struct bcma_driver
*adrv
= container_of(drv
, struct bcma_driver
, drv
);
592 const struct bcma_device_id
*cid
= &core
->id
;
593 const struct bcma_device_id
*did
;
595 for (did
= adrv
->id_table
; did
->manuf
|| did
->id
|| did
->rev
; did
++) {
596 if ((did
->manuf
== cid
->manuf
|| did
->manuf
== BCMA_ANY_MANUF
) &&
597 (did
->id
== cid
->id
|| did
->id
== BCMA_ANY_ID
) &&
598 (did
->rev
== cid
->rev
|| did
->rev
== BCMA_ANY_REV
) &&
599 (did
->class == cid
->class || did
->class == BCMA_ANY_CLASS
))
605 static int bcma_device_probe(struct device
*dev
)
607 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
608 struct bcma_driver
*adrv
= container_of(dev
->driver
, struct bcma_driver
,
613 err
= adrv
->probe(core
);
618 static int bcma_device_remove(struct device
*dev
)
620 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
621 struct bcma_driver
*adrv
= container_of(dev
->driver
, struct bcma_driver
,
630 static int bcma_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
632 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
634 return add_uevent_var(env
,
635 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
636 core
->id
.manuf
, core
->id
.id
,
637 core
->id
.rev
, core
->id
.class);
640 static int __init
bcma_modinit(void)
644 err
= bus_register(&bcma_bus_type
);
648 err
= bcma_host_soc_register_driver();
650 pr_err("SoC host initialization failed\n");
653 #ifdef CONFIG_BCMA_HOST_PCI
654 err
= bcma_host_pci_init();
656 pr_err("PCI host initialization failed\n");
663 fs_initcall(bcma_modinit
);
665 static void __exit
bcma_modexit(void)
667 #ifdef CONFIG_BCMA_HOST_PCI
668 bcma_host_pci_exit();
670 bcma_host_soc_unregister_driver();
671 bus_unregister(&bcma_bus_type
);
673 module_exit(bcma_modexit
)