blob: 8d973c4fc84e3185af77b6c4da0a64103be2e7b9 [file] [log] [blame]
Rafał Miłecki8369ae32011-05-09 18:56:46 +02001/*
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"
Paul Gortmaker200351c2011-07-01 16:06:37 -04009#include <linux/module.h>
Rafał Miłeckid57ef3a2012-08-10 21:23:53 +020010#include <linux/platform_device.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020011#include <linux/bcma/bcma.h>
Geert Uytterhoeveneef72692011-06-26 10:19:44 +020012#include <linux/slab.h>
Hauke Mehrtens2101e532014-09-26 00:09:19 +020013#include <linux/of_address.h>
Hauke Mehrtens71783572014-11-01 16:54:56 +010014#include <linux/of_irq.h>
Rafał Miłeckicae761b2015-06-28 17:17:13 +020015#include <linux/of_platform.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020016
17MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
18MODULE_LICENSE("GPL");
19
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +010020/* contains the number the next bus should get. */
21static unsigned int bcma_bus_next_num = 0;
22
23/* bcma_buses_mutex locks the bcma_bus_next_num */
24static DEFINE_MUTEX(bcma_buses_mutex);
25
Rafał Miłecki8369ae32011-05-09 18:56:46 +020026static int bcma_bus_match(struct device *dev, struct device_driver *drv);
27static int bcma_device_probe(struct device *dev);
28static int bcma_device_remove(struct device *dev);
David Woodhouse886b66e2011-08-19 22:14:47 +020029static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020030
31static 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}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070036static DEVICE_ATTR_RO(manuf);
37
Rafał Miłecki8369ae32011-05-09 18:56:46 +020038static 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}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070043static DEVICE_ATTR_RO(id);
44
Rafał Miłecki8369ae32011-05-09 18:56:46 +020045static 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}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070050static DEVICE_ATTR_RO(rev);
51
Rafał Miłecki8369ae32011-05-09 18:56:46 +020052static 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}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070057static DEVICE_ATTR_RO(class);
58
59static 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,
Rafał Miłecki8369ae32011-05-09 18:56:46 +020065};
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070066ATTRIBUTE_GROUPS(bcma_device);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020067
68static struct bus_type bcma_bus_type = {
69 .name = "bcma",
70 .match = bcma_bus_match,
71 .probe = bcma_device_probe,
72 .remove = bcma_device_remove,
David Woodhouse886b66e2011-08-19 22:14:47 +020073 .uevent = bcma_device_uevent,
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070074 .dev_groups = bcma_device_groups,
Rafał Miłecki8369ae32011-05-09 18:56:46 +020075};
76
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +020077static 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
Hauke Mehrtens929a03a2013-01-04 00:51:20 +010084struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
85 u8 unit)
Hauke Mehrtensdfae7142012-09-29 20:40:18 +020086{
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}
Hauke Mehrtensb2395b82014-01-05 01:10:43 +010095EXPORT_SYMBOL_GPL(bcma_find_core_unit);
Hauke Mehrtensdfae7142012-09-29 20:40:18 +020096
Rafał Miłecki88f9b652013-06-26 10:02:11 +020097bool 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200116static void bcma_release_core_dev(struct device *dev)
117{
118 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
Hauke Mehrtensecd177c2011-07-23 01:20:08 +0200119 if (core->io_addr)
120 iounmap(core->io_addr);
121 if (core->io_wrap)
122 iounmap(core->io_wrap);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200123 kfree(core);
124}
125
Rafał Miłecki37a7f872014-09-05 00:18:49 +0200126static 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
Hauke Mehrtens78afe832014-10-09 23:39:41 +0200137#if defined(CONFIG_OF) && defined(CONFIG_OF_ADDRESS)
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200138static 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
Hauke Mehrtens71783572014-11-01 16:54:56 +0100158static 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
179static 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
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200198static 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;
Hauke Mehrtens71783572014-11-01 16:54:56 +0100206
207 core->irq = bcma_of_get_irq(parent, core, 0);
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200208}
209#else
210static void bcma_of_fill_device(struct platform_device *parent,
211 struct bcma_device *core)
212{
213}
Hauke Mehrtens71783572014-11-01 16:54:56 +0100214static inline unsigned int bcma_of_get_irq(struct platform_device *parent,
215 struct bcma_device *core, int num)
216{
217 return 0;
218}
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200219#endif /* CONFIG_OF */
220
Hauke Mehrtens85eb92e2014-11-01 16:54:55 +0100221unsigned 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 }
Hauke Mehrtens71783572014-11-01 16:54:56 +0100234 if (bus->host_pdev)
235 return bcma_of_get_irq(bus->host_pdev, core, num);
236 return 0;
Hauke Mehrtens85eb92e2014-11-01 16:54:55 +0100237 case BCMA_HOSTTYPE_SDIO:
238 return 0;
239 }
240
241 return 0;
242}
243EXPORT_SYMBOL(bcma_core_irq);
244
Rafał Miłeckiab54bc82014-10-03 17:00:24 +0200245void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core)
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200246{
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200247 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;
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200259 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 }
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200266 break;
267 case BCMA_HOSTTYPE_SDIO:
268 break;
269 }
Rafał Miłeckiab54bc82014-10-03 17:00:24 +0200270}
271
Rafał Miłecki799038e2015-01-16 20:59:39 +0100272void 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
Rafał Miłeckiab54bc82014-10-03 17:00:24 +0200284static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
285{
286 int err;
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200287
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
298static int bcma_register_devices(struct bcma_bus *bus)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200299{
300 struct bcma_device *core;
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200301 int err;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200302
303 list_for_each_entry(core, &bus->cores, list) {
304 /* We support that cores ourself */
305 switch (core->id.id) {
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200306 case BCMA_CORE_4706_CHIPCOMMON:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200307 case BCMA_CORE_CHIPCOMMON:
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200308 case BCMA_CORE_NS_CHIPCOMMON_B:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200309 case BCMA_CORE_PCI:
310 case BCMA_CORE_PCIE:
Rafał Miłeckif4738322014-07-05 01:10:41 +0200311 case BCMA_CORE_PCIE2:
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200312 case BCMA_CORE_MIPS_74K:
Rafał Miłeckie1ac4b42012-07-11 09:23:43 +0200313 case BCMA_CORE_4706_MAC_GBIT_COMMON:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200314 continue;
315 }
316
Rafał Miłecki37a7f872014-09-05 00:18:49 +0200317 /* Early cores were already registered */
318 if (bcma_is_core_needed_early(core->id.id))
319 continue;
320
Rafał Miłecki10419d02013-02-19 19:41:42 +0100321 /* 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
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200326 bcma_register_core(bus, core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200327 }
328
Rafał Miłecki73e4dbe42013-01-25 11:37:26 +0100329#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
Rafał Miłeckid57ef3a2012-08-10 21:23:53 +0200337#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
Rafał Miłecki371a0042012-08-12 13:08:05 +0200345#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
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000352 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);
Rafał Miłecki371a0042012-08-12 13:08:05 +0200357
Hauke Mehrtensa4855f392012-12-05 18:46:02 +0100358 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200364 return 0;
365}
366
Rafał Miłecki9b6cc9a2015-02-08 17:11:50 +0100367void bcma_unregister_cores(struct bcma_bus *bus)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200368{
Piotr Haber1fffa902012-10-11 14:05:15 +0200369 struct bcma_device *core, *tmp;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200370
Piotr Haber1fffa902012-10-11 14:05:15 +0200371 list_for_each_entry_safe(core, tmp, &bus->cores, list) {
Rafał Miłecki17fbaa62015-01-24 18:47:19 +0100372 if (!core->dev_registered)
373 continue;
Piotr Haber1fffa902012-10-11 14:05:15 +0200374 list_del(&core->list);
Rafał Miłecki17fbaa62015-01-24 18:47:19 +0100375 device_unregister(&core->dev);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200376 }
Hauke Mehrtensa4855f392012-12-05 18:46:02 +0100377 if (bus->hosttype == BCMA_HOSTTYPE_SOC)
378 platform_device_unregister(bus->drv_cc.watchdog);
Rafał Miłecki17fbaa62015-01-24 18:47:19 +0100379
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 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200385}
386
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800387int bcma_bus_register(struct bcma_bus *bus)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200388{
389 int err;
390 struct bcma_device *core;
391
392 /* Scan for devices (cores) */
393 err = bcma_bus_scan(bus);
394 if (err) {
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200395 bcma_err(bus, "Failed to scan: %d\n", err);
Hauke Mehrtens1cabf7d2013-07-15 13:15:07 +0200396 return err;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200397 }
398
Hauke Mehrtens30cfb022012-09-29 20:29:50 +0200399 /* 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
Rafał Miłeckib5040752015-01-25 11:11:14 +0100406 /* 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
Hauke Mehrtens92ff7a62015-07-25 21:10:27 +0200413 /* 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) {
Rafał Miłeckicae761b2015-06-28 17:17:13 +0200418 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
Rafał Miłecki37a7f872014-09-05 00:18:49 +0200424 /* 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
Hauke Mehrtens30cfb022012-09-29 20:29:50 +0200430 /* 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200437 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200438 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200439 if (core) {
440 bus->drv_cc.core = core;
441 bcma_core_chipcommon_init(&bus->drv_cc);
442 }
443
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200444 /* 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
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200451 /* 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200458 /* Init PCIE core */
Hauke Mehrtensdfae7142012-09-29 20:40:18 +0200459 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200460 if (core) {
Hauke Mehrtensdfae7142012-09-29 20:40:18 +0200461 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]);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200470 }
471
Rafał Miłeckif4738322014-07-05 01:10:41 +0200472 /* 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
Rafał Miłeckie1ac4b42012-07-11 09:23:43 +0200479 /* 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200486 /* Register found cores */
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200487 bcma_register_devices(bus);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200488
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200489 bcma_info(bus, "Bus registered\n");
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200490
491 return 0;
492}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200493
494void bcma_bus_unregister(struct bcma_bus *bus)
495{
Hauke Mehrtensc50ae942013-02-03 23:25:33 +0100496 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);
Saul St. Johnee915922012-08-16 15:42:30 -0500503
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200504 bcma_core_chipcommon_b_free(&bus->drv_cc_b);
505
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200506 bcma_unregister_cores(bus);
507}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200508
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100509/*
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 */
514int __init bcma_bus_early_register(struct bcma_bus *bus)
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200515{
516 int err;
517 struct bcma_device *core;
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200518
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100519 /* Scan for devices (cores) */
520 err = bcma_bus_scan(bus);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200521 if (err) {
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100522 bcma_err(bus, "Failed to scan bus: %d\n", err);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200523 return -1;
524 }
525
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200526 /* Early init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200527 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200528 if (core) {
529 bus->drv_cc.core = core;
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200530 bcma_core_chipcommon_early_init(&bus->drv_cc);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200531 }
532
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200533 /* Early init MIPS core */
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200534 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
535 if (core) {
536 bus->drv_mips.core = core;
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200537 bcma_core_mips_early_init(&bus->drv_mips);
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200538 }
539
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200540 bcma_info(bus, "Early bus registered\n");
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200541
542 return 0;
543}
544
Rafał Miłecki775ab522011-12-09 22:16:07 +0100545#ifdef CONFIG_PM
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100546int bcma_bus_suspend(struct bcma_bus *bus)
547{
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100548 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 }
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100558 return 0;
559}
560
Rafał Miłecki775ab522011-12-09 22:16:07 +0100561int bcma_bus_resume(struct bcma_bus *bus)
562{
563 struct bcma_device *core;
564
565 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200566 if (bus->drv_cc.core) {
Rafał Miłecki775ab522011-12-09 22:16:07 +0100567 bus->drv_cc.setup_done = false;
568 bcma_core_chipcommon_init(&bus->drv_cc);
569 }
570
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100571 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
Rafał Miłecki775ab522011-12-09 22:16:07 +0100580 return 0;
581}
582#endif
583
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200584int __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}
592EXPORT_SYMBOL_GPL(__bcma_driver_register);
593
594void bcma_driver_unregister(struct bcma_driver *drv)
595{
596 driver_unregister(&drv->drv);
597}
598EXPORT_SYMBOL_GPL(bcma_driver_unregister);
599
600static 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
617static 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
630static 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
David Woodhouse886b66e2011-08-19 22:14:47 +0200642static 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
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200652static int __init bcma_modinit(void)
653{
654 int err;
655
656 err = bus_register(&bcma_bus_type);
657 if (err)
658 return err;
659
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200660 err = bcma_host_soc_register_driver();
661 if (err) {
662 pr_err("SoC host initialization failed\n");
663 err = 0;
664 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200665#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}
675fs_initcall(bcma_modinit);
676
677static void __exit bcma_modexit(void)
678{
679#ifdef CONFIG_BCMA_HOST_PCI
680 bcma_host_pci_exit();
681#endif
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200682 bcma_host_soc_unregister_driver();
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200683 bus_unregister(&bcma_bus_type);
684}
685module_exit(bcma_modexit)