blob: c3c5e0a2d5bed2b21344d750e77693f3e4baac24 [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łecki8369ae32011-05-09 18:56:46 +020015
16MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
17MODULE_LICENSE("GPL");
18
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +010019/* contains the number the next bus should get. */
20static unsigned int bcma_bus_next_num = 0;
21
22/* bcma_buses_mutex locks the bcma_bus_next_num */
23static DEFINE_MUTEX(bcma_buses_mutex);
24
Rafał Miłecki8369ae32011-05-09 18:56:46 +020025static int bcma_bus_match(struct device *dev, struct device_driver *drv);
26static int bcma_device_probe(struct device *dev);
27static int bcma_device_remove(struct device *dev);
David Woodhouse886b66e2011-08-19 22:14:47 +020028static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020029
30static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
31{
32 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
33 return sprintf(buf, "0x%03X\n", core->id.manuf);
34}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070035static DEVICE_ATTR_RO(manuf);
36
Rafał Miłecki8369ae32011-05-09 18:56:46 +020037static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
38{
39 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
40 return sprintf(buf, "0x%03X\n", core->id.id);
41}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070042static DEVICE_ATTR_RO(id);
43
Rafał Miłecki8369ae32011-05-09 18:56:46 +020044static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
45{
46 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
47 return sprintf(buf, "0x%02X\n", core->id.rev);
48}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070049static DEVICE_ATTR_RO(rev);
50
Rafał Miłecki8369ae32011-05-09 18:56:46 +020051static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
52{
53 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
54 return sprintf(buf, "0x%X\n", core->id.class);
55}
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070056static DEVICE_ATTR_RO(class);
57
58static struct attribute *bcma_device_attrs[] = {
59 &dev_attr_manuf.attr,
60 &dev_attr_id.attr,
61 &dev_attr_rev.attr,
62 &dev_attr_class.attr,
63 NULL,
Rafał Miłecki8369ae32011-05-09 18:56:46 +020064};
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070065ATTRIBUTE_GROUPS(bcma_device);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020066
67static struct bus_type bcma_bus_type = {
68 .name = "bcma",
69 .match = bcma_bus_match,
70 .probe = bcma_device_probe,
71 .remove = bcma_device_remove,
David Woodhouse886b66e2011-08-19 22:14:47 +020072 .uevent = bcma_device_uevent,
Greg Kroah-Hartmanfdcf4f82013-10-06 23:55:45 -070073 .dev_groups = bcma_device_groups,
Rafał Miłecki8369ae32011-05-09 18:56:46 +020074};
75
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +020076static u16 bcma_cc_core_id(struct bcma_bus *bus)
77{
78 if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
79 return BCMA_CORE_4706_CHIPCOMMON;
80 return BCMA_CORE_CHIPCOMMON;
81}
82
Hauke Mehrtens929a03a2013-01-04 00:51:20 +010083struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
84 u8 unit)
Hauke Mehrtensdfae7142012-09-29 20:40:18 +020085{
86 struct bcma_device *core;
87
88 list_for_each_entry(core, &bus->cores, list) {
89 if (core->id.id == coreid && core->core_unit == unit)
90 return core;
91 }
92 return NULL;
93}
Hauke Mehrtensb2395b82014-01-05 01:10:43 +010094EXPORT_SYMBOL_GPL(bcma_find_core_unit);
Hauke Mehrtensdfae7142012-09-29 20:40:18 +020095
Rafał Miłecki88f9b652013-06-26 10:02:11 +020096bool bcma_wait_value(struct bcma_device *core, u16 reg, u32 mask, u32 value,
97 int timeout)
98{
99 unsigned long deadline = jiffies + timeout;
100 u32 val;
101
102 do {
103 val = bcma_read32(core, reg);
104 if ((val & mask) == value)
105 return true;
106 cpu_relax();
107 udelay(10);
108 } while (!time_after_eq(jiffies, deadline));
109
110 bcma_warn(core->bus, "Timeout waiting for register 0x%04X!\n", reg);
111
112 return false;
113}
114
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200115static void bcma_release_core_dev(struct device *dev)
116{
117 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
Hauke Mehrtensecd177c2011-07-23 01:20:08 +0200118 if (core->io_addr)
119 iounmap(core->io_addr);
120 if (core->io_wrap)
121 iounmap(core->io_wrap);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200122 kfree(core);
123}
124
Rafał Miłecki37a7f872014-09-05 00:18:49 +0200125static bool bcma_is_core_needed_early(u16 core_id)
126{
127 switch (core_id) {
128 case BCMA_CORE_NS_NAND:
129 case BCMA_CORE_NS_QSPI:
130 return true;
131 }
132
133 return false;
134}
135
Hauke Mehrtens78afe832014-10-09 23:39:41 +0200136#if defined(CONFIG_OF) && defined(CONFIG_OF_ADDRESS)
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200137static struct device_node *bcma_of_find_child_device(struct platform_device *parent,
138 struct bcma_device *core)
139{
140 struct device_node *node;
141 u64 size;
142 const __be32 *reg;
143
144 if (!parent || !parent->dev.of_node)
145 return NULL;
146
147 for_each_child_of_node(parent->dev.of_node, node) {
148 reg = of_get_address(node, 0, &size, NULL);
149 if (!reg)
150 continue;
151 if (of_translate_address(node, reg) == core->addr)
152 return node;
153 }
154 return NULL;
155}
156
Hauke Mehrtens71783572014-11-01 16:54:56 +0100157static int bcma_of_irq_parse(struct platform_device *parent,
158 struct bcma_device *core,
159 struct of_phandle_args *out_irq, int num)
160{
161 __be32 laddr[1];
162 int rc;
163
164 if (core->dev.of_node) {
165 rc = of_irq_parse_one(core->dev.of_node, num, out_irq);
166 if (!rc)
167 return rc;
168 }
169
170 out_irq->np = parent->dev.of_node;
171 out_irq->args_count = 1;
172 out_irq->args[0] = num;
173
174 laddr[0] = cpu_to_be32(core->addr);
175 return of_irq_parse_raw(laddr, out_irq);
176}
177
178static unsigned int bcma_of_get_irq(struct platform_device *parent,
179 struct bcma_device *core, int num)
180{
181 struct of_phandle_args out_irq;
182 int ret;
183
184 if (!parent || !parent->dev.of_node)
185 return 0;
186
187 ret = bcma_of_irq_parse(parent, core, &out_irq, num);
188 if (ret) {
189 bcma_debug(core->bus, "bcma_of_get_irq() failed with rc=%d\n",
190 ret);
191 return 0;
192 }
193
194 return irq_create_of_mapping(&out_irq);
195}
196
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200197static void bcma_of_fill_device(struct platform_device *parent,
198 struct bcma_device *core)
199{
200 struct device_node *node;
201
202 node = bcma_of_find_child_device(parent, core);
203 if (node)
204 core->dev.of_node = node;
Hauke Mehrtens71783572014-11-01 16:54:56 +0100205
206 core->irq = bcma_of_get_irq(parent, core, 0);
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200207}
208#else
209static void bcma_of_fill_device(struct platform_device *parent,
210 struct bcma_device *core)
211{
212}
Hauke Mehrtens71783572014-11-01 16:54:56 +0100213static inline unsigned int bcma_of_get_irq(struct platform_device *parent,
214 struct bcma_device *core, int num)
215{
216 return 0;
217}
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200218#endif /* CONFIG_OF */
219
Hauke Mehrtens85eb92e2014-11-01 16:54:55 +0100220unsigned int bcma_core_irq(struct bcma_device *core, int num)
221{
222 struct bcma_bus *bus = core->bus;
223 unsigned int mips_irq;
224
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;
232 }
Hauke Mehrtens71783572014-11-01 16:54:56 +0100233 if (bus->host_pdev)
234 return bcma_of_get_irq(bus->host_pdev, core, num);
235 return 0;
Hauke Mehrtens85eb92e2014-11-01 16:54:55 +0100236 case BCMA_HOSTTYPE_SDIO:
237 return 0;
238 }
239
240 return 0;
241}
242EXPORT_SYMBOL(bcma_core_irq);
243
Rafał Miłeckiab54bc82014-10-03 17:00:24 +0200244void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core)
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200245{
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200246 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);
249
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;
255 break;
256 case BCMA_HOSTTYPE_SOC:
257 core->dev.dma_mask = &core->dev.coherent_dma_mask;
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200258 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);
262 } else {
263 core->dma_dev = &core->dev;
264 }
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200265 break;
266 case BCMA_HOSTTYPE_SDIO:
267 break;
268 }
Rafał Miłeckiab54bc82014-10-03 17:00:24 +0200269}
270
Rafał Miłecki799038e2015-01-16 20:59:39 +0100271void bcma_init_bus(struct bcma_bus *bus)
272{
273 mutex_lock(&bcma_buses_mutex);
274 bus->num = bcma_bus_next_num++;
275 mutex_unlock(&bcma_buses_mutex);
276
277 INIT_LIST_HEAD(&bus->cores);
278 bus->nr_cores = 0;
279
280 bcma_detect_chip(bus);
281}
282
Rafał Miłeckiab54bc82014-10-03 17:00:24 +0200283static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
284{
285 int err;
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200286
287 err = device_register(&core->dev);
288 if (err) {
289 bcma_err(bus, "Could not register dev for core 0x%03X\n",
290 core->id.id);
291 put_device(&core->dev);
292 return;
293 }
294 core->dev_registered = true;
295}
296
297static int bcma_register_devices(struct bcma_bus *bus)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200298{
299 struct bcma_device *core;
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200300 int err;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200301
302 list_for_each_entry(core, &bus->cores, list) {
303 /* We support that cores ourself */
304 switch (core->id.id) {
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200305 case BCMA_CORE_4706_CHIPCOMMON:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200306 case BCMA_CORE_CHIPCOMMON:
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200307 case BCMA_CORE_NS_CHIPCOMMON_B:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200308 case BCMA_CORE_PCI:
309 case BCMA_CORE_PCIE:
Rafał Miłeckif4738322014-07-05 01:10:41 +0200310 case BCMA_CORE_PCIE2:
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200311 case BCMA_CORE_MIPS_74K:
Rafał Miłeckie1ac4b42012-07-11 09:23:43 +0200312 case BCMA_CORE_4706_MAC_GBIT_COMMON:
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200313 continue;
314 }
315
Rafał Miłecki37a7f872014-09-05 00:18:49 +0200316 /* Early cores were already registered */
317 if (bcma_is_core_needed_early(core->id.id))
318 continue;
319
Rafał Miłecki10419d02013-02-19 19:41:42 +0100320 /* Only first GMAC core on BCM4706 is connected and working */
321 if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
322 core->core_unit > 0)
323 continue;
324
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200325 bcma_register_core(bus, core);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200326 }
327
Rafał Miłecki73e4dbe42013-01-25 11:37:26 +0100328#ifdef CONFIG_BCMA_DRIVER_MIPS
329 if (bus->drv_cc.pflash.present) {
330 err = platform_device_register(&bcma_pflash_dev);
331 if (err)
332 bcma_err(bus, "Error registering parallel flash\n");
333 }
334#endif
335
Rafał Miłeckid57ef3a2012-08-10 21:23:53 +0200336#ifdef CONFIG_BCMA_SFLASH
337 if (bus->drv_cc.sflash.present) {
338 err = platform_device_register(&bcma_sflash_dev);
339 if (err)
340 bcma_err(bus, "Error registering serial flash\n");
341 }
342#endif
343
Rafał Miłecki371a0042012-08-12 13:08:05 +0200344#ifdef CONFIG_BCMA_NFLASH
345 if (bus->drv_cc.nflash.present) {
346 err = platform_device_register(&bcma_nflash_dev);
347 if (err)
348 bcma_err(bus, "Error registering NAND flash\n");
349 }
350#endif
Hauke Mehrtenscf0936b2012-11-20 22:24:30 +0000351 err = bcma_gpio_init(&bus->drv_cc);
352 if (err == -ENOTSUPP)
353 bcma_debug(bus, "GPIO driver not activated\n");
354 else if (err)
355 bcma_err(bus, "Error registering GPIO driver: %i\n", err);
Rafał Miłecki371a0042012-08-12 13:08:05 +0200356
Hauke Mehrtensa4855f392012-12-05 18:46:02 +0100357 if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
358 err = bcma_chipco_watchdog_register(&bus->drv_cc);
359 if (err)
360 bcma_err(bus, "Error registering watchdog driver\n");
361 }
362
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200363 return 0;
364}
365
366static void bcma_unregister_cores(struct bcma_bus *bus)
367{
Piotr Haber1fffa902012-10-11 14:05:15 +0200368 struct bcma_device *core, *tmp;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200369
Piotr Haber1fffa902012-10-11 14:05:15 +0200370 list_for_each_entry_safe(core, tmp, &bus->cores, list) {
371 list_del(&core->list);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200372 if (core->dev_registered)
373 device_unregister(&core->dev);
374 }
Hauke Mehrtensa4855f392012-12-05 18:46:02 +0100375 if (bus->hosttype == BCMA_HOSTTYPE_SOC)
376 platform_device_unregister(bus->drv_cc.watchdog);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200377}
378
Greg Kroah-Hartman0f58a012012-12-21 15:12:59 -0800379int bcma_bus_register(struct bcma_bus *bus)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200380{
381 int err;
382 struct bcma_device *core;
383
384 /* Scan for devices (cores) */
385 err = bcma_bus_scan(bus);
386 if (err) {
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200387 bcma_err(bus, "Failed to scan: %d\n", err);
Hauke Mehrtens1cabf7d2013-07-15 13:15:07 +0200388 return err;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200389 }
390
Hauke Mehrtens30cfb022012-09-29 20:29:50 +0200391 /* Early init CC core */
392 core = bcma_find_core(bus, bcma_cc_core_id(bus));
393 if (core) {
394 bus->drv_cc.core = core;
395 bcma_core_chipcommon_early_init(&bus->drv_cc);
396 }
397
Rafał Miłecki37a7f872014-09-05 00:18:49 +0200398 /* Cores providing flash access go before SPROM init */
399 list_for_each_entry(core, &bus->cores, list) {
400 if (bcma_is_core_needed_early(core->id.id))
401 bcma_register_core(bus, core);
402 }
403
Hauke Mehrtens30cfb022012-09-29 20:29:50 +0200404 /* Try to get SPROM */
405 err = bcma_sprom_get(bus);
406 if (err == -ENOENT) {
407 bcma_err(bus, "No SPROM available\n");
408 } else if (err)
409 bcma_err(bus, "Failed to get SPROM: %d\n", err);
410
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200411 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200412 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200413 if (core) {
414 bus->drv_cc.core = core;
415 bcma_core_chipcommon_init(&bus->drv_cc);
416 }
417
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200418 /* Init CC core */
419 core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
420 if (core) {
421 bus->drv_cc_b.core = core;
422 bcma_core_chipcommon_b_init(&bus->drv_cc_b);
423 }
424
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200425 /* Init MIPS core */
426 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
427 if (core) {
428 bus->drv_mips.core = core;
429 bcma_core_mips_init(&bus->drv_mips);
430 }
431
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200432 /* Init PCIE core */
Hauke Mehrtensdfae7142012-09-29 20:40:18 +0200433 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200434 if (core) {
Hauke Mehrtensdfae7142012-09-29 20:40:18 +0200435 bus->drv_pci[0].core = core;
436 bcma_core_pci_init(&bus->drv_pci[0]);
437 }
438
439 /* Init PCIE core */
440 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
441 if (core) {
442 bus->drv_pci[1].core = core;
443 bcma_core_pci_init(&bus->drv_pci[1]);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200444 }
445
Rafał Miłeckif4738322014-07-05 01:10:41 +0200446 /* Init PCIe Gen 2 core */
447 core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
448 if (core) {
449 bus->drv_pcie2.core = core;
450 bcma_core_pcie2_init(&bus->drv_pcie2);
451 }
452
Rafał Miłeckie1ac4b42012-07-11 09:23:43 +0200453 /* Init GBIT MAC COMMON core */
454 core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
455 if (core) {
456 bus->drv_gmac_cmn.core = core;
457 bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
458 }
459
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200460 /* Register found cores */
Rafał Miłecki6e094bd2014-09-05 00:18:48 +0200461 bcma_register_devices(bus);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200462
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200463 bcma_info(bus, "Bus registered\n");
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200464
465 return 0;
466}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200467
468void bcma_bus_unregister(struct bcma_bus *bus)
469{
Saul St. Johnee915922012-08-16 15:42:30 -0500470 struct bcma_device *cores[3];
Hauke Mehrtensc50ae942013-02-03 23:25:33 +0100471 int err;
472
473 err = bcma_gpio_unregister(&bus->drv_cc);
474 if (err == -EBUSY)
475 bcma_err(bus, "Some GPIOs are still in use.\n");
476 else if (err)
477 bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
Saul St. Johnee915922012-08-16 15:42:30 -0500478
Hauke Mehrtens1716bcf2014-09-08 22:53:36 +0200479 bcma_core_chipcommon_b_free(&bus->drv_cc_b);
480
Saul St. Johnee915922012-08-16 15:42:30 -0500481 cores[0] = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
482 cores[1] = bcma_find_core(bus, BCMA_CORE_PCIE);
483 cores[2] = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
484
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200485 bcma_unregister_cores(bus);
Saul St. Johnee915922012-08-16 15:42:30 -0500486
487 kfree(cores[2]);
488 kfree(cores[1]);
489 kfree(cores[0]);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200490}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200491
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100492/*
493 * This is a special version of bus registration function designed for SoCs.
494 * It scans bus and performs basic initialization of main cores only.
495 * Please note it requires memory allocation, however it won't try to sleep.
496 */
497int __init bcma_bus_early_register(struct bcma_bus *bus)
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200498{
499 int err;
500 struct bcma_device *core;
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200501
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100502 /* Scan for devices (cores) */
503 err = bcma_bus_scan(bus);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200504 if (err) {
Rafał Miłeckic5ed1df2015-01-19 08:30:30 +0100505 bcma_err(bus, "Failed to scan bus: %d\n", err);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200506 return -1;
507 }
508
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200509 /* Early init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200510 core = bcma_find_core(bus, bcma_cc_core_id(bus));
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200511 if (core) {
512 bus->drv_cc.core = core;
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200513 bcma_core_chipcommon_early_init(&bus->drv_cc);
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200514 }
515
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200516 /* Early init MIPS core */
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200517 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
518 if (core) {
519 bus->drv_mips.core = core;
Hauke Mehrtens49655bb2012-09-29 20:29:49 +0200520 bcma_core_mips_early_init(&bus->drv_mips);
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200521 }
522
Rafał Miłecki3d9d8af2012-07-05 22:07:32 +0200523 bcma_info(bus, "Early bus registered\n");
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200524
525 return 0;
526}
527
Rafał Miłecki775ab522011-12-09 22:16:07 +0100528#ifdef CONFIG_PM
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100529int bcma_bus_suspend(struct bcma_bus *bus)
530{
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100531 struct bcma_device *core;
532
533 list_for_each_entry(core, &bus->cores, list) {
534 struct device_driver *drv = core->dev.driver;
535 if (drv) {
536 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
537 if (adrv->suspend)
538 adrv->suspend(core);
539 }
540 }
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100541 return 0;
542}
543
Rafał Miłecki775ab522011-12-09 22:16:07 +0100544int bcma_bus_resume(struct bcma_bus *bus)
545{
546 struct bcma_device *core;
547
548 /* Init CC core */
Rafał Miłecki6d5cfc92012-07-10 23:45:49 +0200549 if (bus->drv_cc.core) {
Rafał Miłecki775ab522011-12-09 22:16:07 +0100550 bus->drv_cc.setup_done = false;
551 bcma_core_chipcommon_init(&bus->drv_cc);
552 }
553
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100554 list_for_each_entry(core, &bus->cores, list) {
555 struct device_driver *drv = core->dev.driver;
556 if (drv) {
557 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
558 if (adrv->resume)
559 adrv->resume(core);
560 }
561 }
562
Rafał Miłecki775ab522011-12-09 22:16:07 +0100563 return 0;
564}
565#endif
566
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200567int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
568{
569 drv->drv.name = drv->name;
570 drv->drv.bus = &bcma_bus_type;
571 drv->drv.owner = owner;
572
573 return driver_register(&drv->drv);
574}
575EXPORT_SYMBOL_GPL(__bcma_driver_register);
576
577void bcma_driver_unregister(struct bcma_driver *drv)
578{
579 driver_unregister(&drv->drv);
580}
581EXPORT_SYMBOL_GPL(bcma_driver_unregister);
582
583static int bcma_bus_match(struct device *dev, struct device_driver *drv)
584{
585 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
586 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
587 const struct bcma_device_id *cid = &core->id;
588 const struct bcma_device_id *did;
589
590 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
591 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
592 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
593 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
594 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
595 return 1;
596 }
597 return 0;
598}
599
600static int bcma_device_probe(struct device *dev)
601{
602 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
603 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
604 drv);
605 int err = 0;
606
607 if (adrv->probe)
608 err = adrv->probe(core);
609
610 return err;
611}
612
613static int bcma_device_remove(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
619 if (adrv->remove)
620 adrv->remove(core);
621
622 return 0;
623}
624
David Woodhouse886b66e2011-08-19 22:14:47 +0200625static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
626{
627 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
628
629 return add_uevent_var(env,
630 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
631 core->id.manuf, core->id.id,
632 core->id.rev, core->id.class);
633}
634
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200635static int __init bcma_modinit(void)
636{
637 int err;
638
639 err = bus_register(&bcma_bus_type);
640 if (err)
641 return err;
642
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200643 err = bcma_host_soc_register_driver();
644 if (err) {
645 pr_err("SoC host initialization failed\n");
646 err = 0;
647 }
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200648#ifdef CONFIG_BCMA_HOST_PCI
649 err = bcma_host_pci_init();
650 if (err) {
651 pr_err("PCI host initialization failed\n");
652 err = 0;
653 }
654#endif
655
656 return err;
657}
658fs_initcall(bcma_modinit);
659
660static void __exit bcma_modexit(void)
661{
662#ifdef CONFIG_BCMA_HOST_PCI
663 bcma_host_pci_exit();
664#endif
Hauke Mehrtens2101e532014-09-26 00:09:19 +0200665 bcma_host_soc_unregister_driver();
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200666 bus_unregister(&bcma_bus_type);
667}
668module_exit(bcma_modexit)