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>
15 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
16 MODULE_LICENSE("GPL");
18 /* contains the number the next bus should get. */
19 static unsigned int bcma_bus_next_num
= 0;
21 /* bcma_buses_mutex locks the bcma_bus_next_num */
22 static DEFINE_MUTEX(bcma_buses_mutex
);
24 static int bcma_bus_match(struct device
*dev
, struct device_driver
*drv
);
25 static int bcma_device_probe(struct device
*dev
);
26 static int bcma_device_remove(struct device
*dev
);
27 static int bcma_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
);
29 static ssize_t
manuf_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
31 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
32 return sprintf(buf
, "0x%03X\n", core
->id
.manuf
);
34 static DEVICE_ATTR_RO(manuf
);
36 static ssize_t
id_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
38 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
39 return sprintf(buf
, "0x%03X\n", core
->id
.id
);
41 static DEVICE_ATTR_RO(id
);
43 static ssize_t
rev_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
45 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
46 return sprintf(buf
, "0x%02X\n", core
->id
.rev
);
48 static DEVICE_ATTR_RO(rev
);
50 static ssize_t
class_show(struct device
*dev
, struct device_attribute
*attr
, char *buf
)
52 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
53 return sprintf(buf
, "0x%X\n", core
->id
.class);
55 static DEVICE_ATTR_RO(class);
57 static struct attribute
*bcma_device_attrs
[] = {
64 ATTRIBUTE_GROUPS(bcma_device
);
66 static struct bus_type bcma_bus_type
= {
68 .match
= bcma_bus_match
,
69 .probe
= bcma_device_probe
,
70 .remove
= bcma_device_remove
,
71 .uevent
= bcma_device_uevent
,
72 .dev_groups
= bcma_device_groups
,
75 static u16
bcma_cc_core_id(struct bcma_bus
*bus
)
77 if (bus
->chipinfo
.id
== BCMA_CHIP_ID_BCM4706
)
78 return BCMA_CORE_4706_CHIPCOMMON
;
79 return BCMA_CORE_CHIPCOMMON
;
82 struct bcma_device
*bcma_find_core_unit(struct bcma_bus
*bus
, u16 coreid
,
85 struct bcma_device
*core
;
87 list_for_each_entry(core
, &bus
->cores
, list
) {
88 if (core
->id
.id
== coreid
&& core
->core_unit
== unit
)
93 EXPORT_SYMBOL_GPL(bcma_find_core_unit
);
95 bool bcma_wait_value(struct bcma_device
*core
, u16 reg
, u32 mask
, u32 value
,
98 unsigned long deadline
= jiffies
+ timeout
;
102 val
= bcma_read32(core
, reg
);
103 if ((val
& mask
) == value
)
107 } while (!time_after_eq(jiffies
, deadline
));
109 bcma_warn(core
->bus
, "Timeout waiting for register 0x%04X!\n", reg
);
114 static void bcma_release_core_dev(struct device
*dev
)
116 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
118 iounmap(core
->io_addr
);
120 iounmap(core
->io_wrap
);
124 static bool bcma_is_core_needed_early(u16 core_id
)
127 case BCMA_CORE_NS_NAND
:
128 case BCMA_CORE_NS_QSPI
:
136 static struct device_node
*bcma_of_find_child_device(struct platform_device
*parent
,
137 struct bcma_device
*core
)
139 struct device_node
*node
;
143 if (!parent
|| !parent
->dev
.of_node
)
146 for_each_child_of_node(parent
->dev
.of_node
, node
) {
147 reg
= of_get_address(node
, 0, &size
, NULL
);
150 if (of_translate_address(node
, reg
) == core
->addr
)
156 static void bcma_of_fill_device(struct platform_device
*parent
,
157 struct bcma_device
*core
)
159 struct device_node
*node
;
161 node
= bcma_of_find_child_device(parent
, core
);
163 core
->dev
.of_node
= node
;
166 static void bcma_of_fill_device(struct platform_device
*parent
,
167 struct bcma_device
*core
)
170 #endif /* CONFIG_OF */
172 static void bcma_register_core(struct bcma_bus
*bus
, struct bcma_device
*core
)
176 core
->dev
.release
= bcma_release_core_dev
;
177 core
->dev
.bus
= &bcma_bus_type
;
178 dev_set_name(&core
->dev
, "bcma%d:%d", bus
->num
, core
->core_index
);
180 switch (bus
->hosttype
) {
181 case BCMA_HOSTTYPE_PCI
:
182 core
->dev
.parent
= &bus
->host_pci
->dev
;
183 core
->dma_dev
= &bus
->host_pci
->dev
;
184 core
->irq
= bus
->host_pci
->irq
;
186 case BCMA_HOSTTYPE_SOC
:
187 core
->dev
.dma_mask
= &core
->dev
.coherent_dma_mask
;
188 if (bus
->host_pdev
) {
189 core
->dma_dev
= &bus
->host_pdev
->dev
;
190 core
->dev
.parent
= &bus
->host_pdev
->dev
;
191 bcma_of_fill_device(bus
->host_pdev
, core
);
193 core
->dma_dev
= &core
->dev
;
196 case BCMA_HOSTTYPE_SDIO
:
200 err
= device_register(&core
->dev
);
202 bcma_err(bus
, "Could not register dev for core 0x%03X\n",
204 put_device(&core
->dev
);
207 core
->dev_registered
= true;
210 static int bcma_register_devices(struct bcma_bus
*bus
)
212 struct bcma_device
*core
;
215 list_for_each_entry(core
, &bus
->cores
, list
) {
216 /* We support that cores ourself */
217 switch (core
->id
.id
) {
218 case BCMA_CORE_4706_CHIPCOMMON
:
219 case BCMA_CORE_CHIPCOMMON
:
220 case BCMA_CORE_NS_CHIPCOMMON_B
:
223 case BCMA_CORE_PCIE2
:
224 case BCMA_CORE_MIPS_74K
:
225 case BCMA_CORE_4706_MAC_GBIT_COMMON
:
229 /* Early cores were already registered */
230 if (bcma_is_core_needed_early(core
->id
.id
))
233 /* Only first GMAC core on BCM4706 is connected and working */
234 if (core
->id
.id
== BCMA_CORE_4706_MAC_GBIT
&&
238 bcma_register_core(bus
, core
);
241 #ifdef CONFIG_BCMA_DRIVER_MIPS
242 if (bus
->drv_cc
.pflash
.present
) {
243 err
= platform_device_register(&bcma_pflash_dev
);
245 bcma_err(bus
, "Error registering parallel flash\n");
249 #ifdef CONFIG_BCMA_SFLASH
250 if (bus
->drv_cc
.sflash
.present
) {
251 err
= platform_device_register(&bcma_sflash_dev
);
253 bcma_err(bus
, "Error registering serial flash\n");
257 #ifdef CONFIG_BCMA_NFLASH
258 if (bus
->drv_cc
.nflash
.present
) {
259 err
= platform_device_register(&bcma_nflash_dev
);
261 bcma_err(bus
, "Error registering NAND flash\n");
264 err
= bcma_gpio_init(&bus
->drv_cc
);
265 if (err
== -ENOTSUPP
)
266 bcma_debug(bus
, "GPIO driver not activated\n");
268 bcma_err(bus
, "Error registering GPIO driver: %i\n", err
);
270 if (bus
->hosttype
== BCMA_HOSTTYPE_SOC
) {
271 err
= bcma_chipco_watchdog_register(&bus
->drv_cc
);
273 bcma_err(bus
, "Error registering watchdog driver\n");
279 static void bcma_unregister_cores(struct bcma_bus
*bus
)
281 struct bcma_device
*core
, *tmp
;
283 list_for_each_entry_safe(core
, tmp
, &bus
->cores
, list
) {
284 list_del(&core
->list
);
285 if (core
->dev_registered
)
286 device_unregister(&core
->dev
);
288 if (bus
->hosttype
== BCMA_HOSTTYPE_SOC
)
289 platform_device_unregister(bus
->drv_cc
.watchdog
);
292 int bcma_bus_register(struct bcma_bus
*bus
)
295 struct bcma_device
*core
;
297 mutex_lock(&bcma_buses_mutex
);
298 bus
->num
= bcma_bus_next_num
++;
299 mutex_unlock(&bcma_buses_mutex
);
301 /* Scan for devices (cores) */
302 err
= bcma_bus_scan(bus
);
304 bcma_err(bus
, "Failed to scan: %d\n", err
);
308 /* Early init CC core */
309 core
= bcma_find_core(bus
, bcma_cc_core_id(bus
));
311 bus
->drv_cc
.core
= core
;
312 bcma_core_chipcommon_early_init(&bus
->drv_cc
);
315 /* Cores providing flash access go before SPROM init */
316 list_for_each_entry(core
, &bus
->cores
, list
) {
317 if (bcma_is_core_needed_early(core
->id
.id
))
318 bcma_register_core(bus
, core
);
321 /* Try to get SPROM */
322 err
= bcma_sprom_get(bus
);
323 if (err
== -ENOENT
) {
324 bcma_err(bus
, "No SPROM available\n");
326 bcma_err(bus
, "Failed to get SPROM: %d\n", err
);
329 core
= bcma_find_core(bus
, bcma_cc_core_id(bus
));
331 bus
->drv_cc
.core
= core
;
332 bcma_core_chipcommon_init(&bus
->drv_cc
);
336 core
= bcma_find_core(bus
, BCMA_CORE_NS_CHIPCOMMON_B
);
338 bus
->drv_cc_b
.core
= core
;
339 bcma_core_chipcommon_b_init(&bus
->drv_cc_b
);
343 core
= bcma_find_core(bus
, BCMA_CORE_MIPS_74K
);
345 bus
->drv_mips
.core
= core
;
346 bcma_core_mips_init(&bus
->drv_mips
);
350 core
= bcma_find_core_unit(bus
, BCMA_CORE_PCIE
, 0);
352 bus
->drv_pci
[0].core
= core
;
353 bcma_core_pci_init(&bus
->drv_pci
[0]);
357 core
= bcma_find_core_unit(bus
, BCMA_CORE_PCIE
, 1);
359 bus
->drv_pci
[1].core
= core
;
360 bcma_core_pci_init(&bus
->drv_pci
[1]);
363 /* Init PCIe Gen 2 core */
364 core
= bcma_find_core_unit(bus
, BCMA_CORE_PCIE2
, 0);
366 bus
->drv_pcie2
.core
= core
;
367 bcma_core_pcie2_init(&bus
->drv_pcie2
);
370 /* Init GBIT MAC COMMON core */
371 core
= bcma_find_core(bus
, BCMA_CORE_4706_MAC_GBIT_COMMON
);
373 bus
->drv_gmac_cmn
.core
= core
;
374 bcma_core_gmac_cmn_init(&bus
->drv_gmac_cmn
);
377 /* Register found cores */
378 bcma_register_devices(bus
);
380 bcma_info(bus
, "Bus registered\n");
385 void bcma_bus_unregister(struct bcma_bus
*bus
)
387 struct bcma_device
*cores
[3];
390 err
= bcma_gpio_unregister(&bus
->drv_cc
);
392 bcma_err(bus
, "Some GPIOs are still in use.\n");
394 bcma_err(bus
, "Can not unregister GPIO driver: %i\n", err
);
396 bcma_core_chipcommon_b_free(&bus
->drv_cc_b
);
398 cores
[0] = bcma_find_core(bus
, BCMA_CORE_MIPS_74K
);
399 cores
[1] = bcma_find_core(bus
, BCMA_CORE_PCIE
);
400 cores
[2] = bcma_find_core(bus
, BCMA_CORE_4706_MAC_GBIT_COMMON
);
402 bcma_unregister_cores(bus
);
409 int __init
bcma_bus_early_register(struct bcma_bus
*bus
,
410 struct bcma_device
*core_cc
,
411 struct bcma_device
*core_mips
)
414 struct bcma_device
*core
;
415 struct bcma_device_id match
;
417 match
.manuf
= BCMA_MANUF_BCM
;
418 match
.id
= bcma_cc_core_id(bus
);
419 match
.class = BCMA_CL_SIM
;
420 match
.rev
= BCMA_ANY_REV
;
422 /* Scan for chip common core */
423 err
= bcma_bus_scan_early(bus
, &match
, core_cc
);
425 bcma_err(bus
, "Failed to scan for common core: %d\n", err
);
429 match
.manuf
= BCMA_MANUF_MIPS
;
430 match
.id
= BCMA_CORE_MIPS_74K
;
431 match
.class = BCMA_CL_SIM
;
432 match
.rev
= BCMA_ANY_REV
;
434 /* Scan for mips core */
435 err
= bcma_bus_scan_early(bus
, &match
, core_mips
);
437 bcma_err(bus
, "Failed to scan for mips core: %d\n", err
);
441 /* Early init CC core */
442 core
= bcma_find_core(bus
, bcma_cc_core_id(bus
));
444 bus
->drv_cc
.core
= core
;
445 bcma_core_chipcommon_early_init(&bus
->drv_cc
);
448 /* Early init MIPS core */
449 core
= bcma_find_core(bus
, BCMA_CORE_MIPS_74K
);
451 bus
->drv_mips
.core
= core
;
452 bcma_core_mips_early_init(&bus
->drv_mips
);
455 bcma_info(bus
, "Early bus registered\n");
461 int bcma_bus_suspend(struct bcma_bus
*bus
)
463 struct bcma_device
*core
;
465 list_for_each_entry(core
, &bus
->cores
, list
) {
466 struct device_driver
*drv
= core
->dev
.driver
;
468 struct bcma_driver
*adrv
= container_of(drv
, struct bcma_driver
, drv
);
476 int bcma_bus_resume(struct bcma_bus
*bus
)
478 struct bcma_device
*core
;
481 if (bus
->drv_cc
.core
) {
482 bus
->drv_cc
.setup_done
= false;
483 bcma_core_chipcommon_init(&bus
->drv_cc
);
486 list_for_each_entry(core
, &bus
->cores
, list
) {
487 struct device_driver
*drv
= core
->dev
.driver
;
489 struct bcma_driver
*adrv
= container_of(drv
, struct bcma_driver
, drv
);
499 int __bcma_driver_register(struct bcma_driver
*drv
, struct module
*owner
)
501 drv
->drv
.name
= drv
->name
;
502 drv
->drv
.bus
= &bcma_bus_type
;
503 drv
->drv
.owner
= owner
;
505 return driver_register(&drv
->drv
);
507 EXPORT_SYMBOL_GPL(__bcma_driver_register
);
509 void bcma_driver_unregister(struct bcma_driver
*drv
)
511 driver_unregister(&drv
->drv
);
513 EXPORT_SYMBOL_GPL(bcma_driver_unregister
);
515 static int bcma_bus_match(struct device
*dev
, struct device_driver
*drv
)
517 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
518 struct bcma_driver
*adrv
= container_of(drv
, struct bcma_driver
, drv
);
519 const struct bcma_device_id
*cid
= &core
->id
;
520 const struct bcma_device_id
*did
;
522 for (did
= adrv
->id_table
; did
->manuf
|| did
->id
|| did
->rev
; did
++) {
523 if ((did
->manuf
== cid
->manuf
|| did
->manuf
== BCMA_ANY_MANUF
) &&
524 (did
->id
== cid
->id
|| did
->id
== BCMA_ANY_ID
) &&
525 (did
->rev
== cid
->rev
|| did
->rev
== BCMA_ANY_REV
) &&
526 (did
->class == cid
->class || did
->class == BCMA_ANY_CLASS
))
532 static int bcma_device_probe(struct device
*dev
)
534 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
535 struct bcma_driver
*adrv
= container_of(dev
->driver
, struct bcma_driver
,
540 err
= adrv
->probe(core
);
545 static int bcma_device_remove(struct device
*dev
)
547 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
548 struct bcma_driver
*adrv
= container_of(dev
->driver
, struct bcma_driver
,
557 static int bcma_device_uevent(struct device
*dev
, struct kobj_uevent_env
*env
)
559 struct bcma_device
*core
= container_of(dev
, struct bcma_device
, dev
);
561 return add_uevent_var(env
,
562 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
563 core
->id
.manuf
, core
->id
.id
,
564 core
->id
.rev
, core
->id
.class);
567 static int __init
bcma_modinit(void)
571 err
= bus_register(&bcma_bus_type
);
575 err
= bcma_host_soc_register_driver();
577 pr_err("SoC host initialization failed\n");
580 #ifdef CONFIG_BCMA_HOST_PCI
581 err
= bcma_host_pci_init();
583 pr_err("PCI host initialization failed\n");
590 fs_initcall(bcma_modinit
);
592 static void __exit
bcma_modexit(void)
594 #ifdef CONFIG_BCMA_HOST_PCI
595 bcma_host_pci_exit();
597 bcma_host_soc_unregister_driver();
598 bus_unregister(&bcma_bus_type
);
600 module_exit(bcma_modexit
)