blob: 59128478a90fd3200c131be97225b8d97eb8763d [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
Rafał Miłeckicae761b2015-06-28 17:17:13 +0200413 if (bus->host_pdev) {
414 struct device *dev = &bus->host_pdev->dev;
415
416 of_platform_populate(dev->of_node, of_default_bus_match_table,
417 NULL, dev);
418 }
419
Rafał Miłecki37a7f872014-09-05 00:18:49 +0200420 /* Cores providing flash access go before SPROM init */
421 list_for_each_entry(core, &bus->cores, list) {
422 if (bcma_is_core_needed_early(core->id.id))
423 bcma_register_core(bus, core);
424 }
425
Hauke Mehrtens30cfb022012-09-29 20:29:50 +0200426 /* Try to get SPROM */
427 err = bcma_sprom_get(bus);
428 if (err == -ENOENT) {
429 bcma_err(bus, "No SPROM available\n");
430 } else if (err)
431 bcma_err(bus, "Failed to get SPROM: %d\n", err);
432
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200433 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200434 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200435 if (core) {
436 bus->drv_cc.core = core;
437 bcma_core_chipcommon_init(&bus->drv_cc);
438 }
439
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200440 /* Init CC core */
441 core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
442 if (core) {
443 bus->drv_cc_b.core = core;
444 bcma_core_chipcommon_b_init(&bus->drv_cc_b);
445 }
446
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200447 /* Init MIPS core */
448 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
449 if (core) {
450 bus->drv_mips.core = core;
451 bcma_core_mips_init(&bus->drv_mips);
452 }
453
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200454 /* Init PCIE core */
Hauke Mehrtensdfae7142012-09-29 20:40:18 +0200455 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200456 if (core) {
Hauke Mehrtensdfae7142012-09-29 20:40:18 +0200457 bus->drv_pci[0].core = core;
458 bcma_core_pci_init(&bus->drv_pci[0]);
459 }
460
461 /* Init PCIE core */
462 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
463 if (core) {
464 bus->drv_pci[1].core = core;
465 bcma_core_pci_init(&bus->drv_pci[1]);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200466 }
467
Rafał Miłeckif4738322014-07-05 01:10:41 +0200468 /* Init PCIe Gen 2 core */
469 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
470 if (core) {
471 bus->drv_pcie2.core = core;
472 bcma_core_pcie2_init(&bus->drv_pcie2);
473 }
474
Rafał Miłeckie1ac4b42012-07-11 09:23:43 +0200475 /* Init GBIT MAC COMMON core */
476 core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
477 if (core) {
478 bus->drv_gmac_cmn.core = core;
479 bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
480 }
481
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200482 /* Register found cores */
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200483 bcma_register_devices(bus);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200484
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200485 bcma_info(bus, "Bus registered\n");
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200486
487 return 0;
488}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200489
490void bcma_bus_unregister(struct bcma_bus *bus)
491{
Hauke Mehrtensc50ae942013-02-03 23:25:33 +0100492 int err;
493
494 err = bcma_gpio_unregister(&bus->drv_cc);
495 if (err == -EBUSY)
496 bcma_err(bus, "Some GPIOs are still in use.\n");
497 else if (err)
498 bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
Saul St. Johnee915922012-08-16 15:42:30 -0500499
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200500 bcma_core_chipcommon_b_free(&bus->drv_cc_b);
501
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200502 bcma_unregister_cores(bus);
503}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200504
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100505/*
506 * This is a special version of bus registration function designed for SoCs.
507 * It scans bus and performs basic initialization of main cores only.
508 * Please note it requires memory allocation, however it won't try to sleep.
509 */
510int __init bcma_bus_early_register(struct bcma_bus *bus)
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200511{
512 int err;
513 struct bcma_device *core;
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200514
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100515 /* Scan for devices (cores) */
516 err = bcma_bus_scan(bus);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200517 if (err) {
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100518 bcma_err(bus, "Failed to scan bus: %d\n", err);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200519 return -1;
520 }
521
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200522 /* Early init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200523 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200524 if (core) {
525 bus->drv_cc.core = core;
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200526 bcma_core_chipcommon_early_init(&bus->drv_cc);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200527 }
528
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200529 /* Early init MIPS core */
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200530 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
531 if (core) {
532 bus->drv_mips.core = core;
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200533 bcma_core_mips_early_init(&bus->drv_mips);
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200534 }
535
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200536 bcma_info(bus, "Early bus registered\n");
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200537
538 return 0;
539}
540
Rafał Miłecki775ab522011-12-09 22:16:07 +0100541#ifdef CONFIG_PM
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100542int bcma_bus_suspend(struct bcma_bus *bus)
543{
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100544 struct bcma_device *core;
545
546 list_for_each_entry(core, &bus->cores, list) {
547 struct device_driver *drv = core->dev.driver;
548 if (drv) {
549 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
550 if (adrv->suspend)
551 adrv->suspend(core);
552 }
553 }
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100554 return 0;
555}
556
Rafał Miłecki775ab522011-12-09 22:16:07 +0100557int bcma_bus_resume(struct bcma_bus *bus)
558{
559 struct bcma_device *core;
560
561 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200562 if (bus->drv_cc.core) {
Rafał Miłecki775ab522011-12-09 22:16:07 +0100563 bus->drv_cc.setup_done = false;
564 bcma_core_chipcommon_init(&bus->drv_cc);
565 }
566
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100567 list_for_each_entry(core, &bus->cores, list) {
568 struct device_driver *drv = core->dev.driver;
569 if (drv) {
570 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
571 if (adrv->resume)
572 adrv->resume(core);
573 }
574 }
575
Rafał Miłecki775ab522011-12-09 22:16:07 +0100576 return 0;
577}
578#endif
579
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200580int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
581{
582 drv->drv.name = drv->name;
583 drv->drv.bus = &bcma_bus_type;
584 drv->drv.owner = owner;
585
586 return driver_register(&drv->drv);
587}
588EXPORT_SYMBOL_GPL(__bcma_driver_register);
589
590void bcma_driver_unregister(struct bcma_driver *drv)
591{
592 driver_unregister(&drv->drv);
593}
594EXPORT_SYMBOL_GPL(bcma_driver_unregister);
595
596static int bcma_bus_match(struct device *dev, struct device_driver *drv)
597{
598 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
599 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
600 const struct bcma_device_id *cid = &core->id;
601 const struct bcma_device_id *did;
602
603 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
604 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
605 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
606 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
607 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
608 return 1;
609 }
610 return 0;
611}
612
613static int bcma_device_probe(struct device *dev)
614{
615 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
616 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
617 drv);
618 int err = 0;
619
620 if (adrv->probe)
621 err = adrv->probe(core);
622
623 return err;
624}
625
626static int bcma_device_remove(struct device *dev)
627{
628 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
629 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
630 drv);
631
632 if (adrv->remove)
633 adrv->remove(core);
634
635 return 0;
636}
637
David Woodhouse886b66e2011-08-19 22:14:47 +0200638static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
639{
640 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
641
642 return add_uevent_var(env,
643 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
644 core->id.manuf, core->id.id,
645 core->id.rev, core->id.class);
646}
647
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200648static int __init bcma_modinit(void)
649{
650 int err;
651
652 err = bus_register(&bcma_bus_type);
653 if (err)
654 return err;
655
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200656 err = bcma_host_soc_register_driver();
657 if (err) {
658 pr_err("SoC host initialization failed\n");
659 err = 0;
660 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200661#ifdef CONFIG_BCMA_HOST_PCI
662 err = bcma_host_pci_init();
663 if (err) {
664 pr_err("PCI host initialization failed\n");
665 err = 0;
666 }
667#endif
668
669 return err;
670}
671fs_initcall(bcma_modinit);
672
673static void __exit bcma_modexit(void)
674{
675#ifdef CONFIG_BCMA_HOST_PCI
676 bcma_host_pci_exit();
677#endif
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200678 bcma_host_soc_unregister_driver();
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200679 bus_unregister(&bcma_bus_type);
680}
681module_exit(bcma_modexit)