main.c revision eef726994fc4ce2f9d0c880016bd104628ef44c9
1/*
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"
9#include <linux/bcma/bcma.h>
10#include <linux/slab.h>
11
12MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
13MODULE_LICENSE("GPL");
14
15static int bcma_bus_match(struct device *dev, struct device_driver *drv);
16static int bcma_device_probe(struct device *dev);
17static int bcma_device_remove(struct device *dev);
18
19static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
20{
21	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
22	return sprintf(buf, "0x%03X\n", core->id.manuf);
23}
24static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
25{
26	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
27	return sprintf(buf, "0x%03X\n", core->id.id);
28}
29static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
30{
31	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
32	return sprintf(buf, "0x%02X\n", core->id.rev);
33}
34static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
35{
36	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
37	return sprintf(buf, "0x%X\n", core->id.class);
38}
39static struct device_attribute bcma_device_attrs[] = {
40	__ATTR_RO(manuf),
41	__ATTR_RO(id),
42	__ATTR_RO(rev),
43	__ATTR_RO(class),
44	__ATTR_NULL,
45};
46
47static struct bus_type bcma_bus_type = {
48	.name		= "bcma",
49	.match		= bcma_bus_match,
50	.probe		= bcma_device_probe,
51	.remove		= bcma_device_remove,
52	.dev_attrs	= bcma_device_attrs,
53};
54
55static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
56{
57	struct bcma_device *core;
58
59	list_for_each_entry(core, &bus->cores, list) {
60		if (core->id.id == coreid)
61			return core;
62	}
63	return NULL;
64}
65
66static void bcma_release_core_dev(struct device *dev)
67{
68	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
69	kfree(core);
70}
71
72static int bcma_register_cores(struct bcma_bus *bus)
73{
74	struct bcma_device *core;
75	int err, dev_id = 0;
76
77	list_for_each_entry(core, &bus->cores, list) {
78		/* We support that cores ourself */
79		switch (core->id.id) {
80		case BCMA_CORE_CHIPCOMMON:
81		case BCMA_CORE_PCI:
82		case BCMA_CORE_PCIE:
83			continue;
84		}
85
86		core->dev.release = bcma_release_core_dev;
87		core->dev.bus = &bcma_bus_type;
88		dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id);
89
90		switch (bus->hosttype) {
91		case BCMA_HOSTTYPE_PCI:
92			core->dev.parent = &bus->host_pci->dev;
93			core->dma_dev = &bus->host_pci->dev;
94			core->irq = bus->host_pci->irq;
95			break;
96		case BCMA_HOSTTYPE_NONE:
97		case BCMA_HOSTTYPE_SDIO:
98			break;
99		}
100
101		err = device_register(&core->dev);
102		if (err) {
103			pr_err("Could not register dev for core 0x%03X\n",
104			       core->id.id);
105			continue;
106		}
107		core->dev_registered = true;
108		dev_id++;
109	}
110
111	return 0;
112}
113
114static void bcma_unregister_cores(struct bcma_bus *bus)
115{
116	struct bcma_device *core;
117
118	list_for_each_entry(core, &bus->cores, list) {
119		if (core->dev_registered)
120			device_unregister(&core->dev);
121	}
122}
123
124int bcma_bus_register(struct bcma_bus *bus)
125{
126	int err;
127	struct bcma_device *core;
128
129	/* Scan for devices (cores) */
130	err = bcma_bus_scan(bus);
131	if (err) {
132		pr_err("Failed to scan: %d\n", err);
133		return -1;
134	}
135
136	/* Init CC core */
137	core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
138	if (core) {
139		bus->drv_cc.core = core;
140		bcma_core_chipcommon_init(&bus->drv_cc);
141	}
142
143	/* Init PCIE core */
144	core = bcma_find_core(bus, BCMA_CORE_PCIE);
145	if (core) {
146		bus->drv_pci.core = core;
147		bcma_core_pci_init(&bus->drv_pci);
148	}
149
150	/* Try to get SPROM */
151	err = bcma_sprom_get(bus);
152	if (err) {
153		pr_err("Failed to get SPROM: %d\n", err);
154		return -ENOENT;
155	}
156
157	/* Register found cores */
158	bcma_register_cores(bus);
159
160	pr_info("Bus registered\n");
161
162	return 0;
163}
164
165void bcma_bus_unregister(struct bcma_bus *bus)
166{
167	bcma_unregister_cores(bus);
168}
169
170int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
171{
172	drv->drv.name = drv->name;
173	drv->drv.bus = &bcma_bus_type;
174	drv->drv.owner = owner;
175
176	return driver_register(&drv->drv);
177}
178EXPORT_SYMBOL_GPL(__bcma_driver_register);
179
180void bcma_driver_unregister(struct bcma_driver *drv)
181{
182	driver_unregister(&drv->drv);
183}
184EXPORT_SYMBOL_GPL(bcma_driver_unregister);
185
186static int bcma_bus_match(struct device *dev, struct device_driver *drv)
187{
188	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
189	struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
190	const struct bcma_device_id *cid = &core->id;
191	const struct bcma_device_id *did;
192
193	for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
194	    if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
195		(did->id == cid->id || did->id == BCMA_ANY_ID) &&
196		(did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
197		(did->class == cid->class || did->class == BCMA_ANY_CLASS))
198			return 1;
199	}
200	return 0;
201}
202
203static int bcma_device_probe(struct device *dev)
204{
205	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
206	struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
207					       drv);
208	int err = 0;
209
210	if (adrv->probe)
211		err = adrv->probe(core);
212
213	return err;
214}
215
216static int bcma_device_remove(struct device *dev)
217{
218	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
219	struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
220					       drv);
221
222	if (adrv->remove)
223		adrv->remove(core);
224
225	return 0;
226}
227
228static int __init bcma_modinit(void)
229{
230	int err;
231
232	err = bus_register(&bcma_bus_type);
233	if (err)
234		return err;
235
236#ifdef CONFIG_BCMA_HOST_PCI
237	err = bcma_host_pci_init();
238	if (err) {
239		pr_err("PCI host initialization failed\n");
240		err = 0;
241	}
242#endif
243
244	return err;
245}
246fs_initcall(bcma_modinit);
247
248static void __exit bcma_modexit(void)
249{
250#ifdef CONFIG_BCMA_HOST_PCI
251	bcma_host_pci_exit();
252#endif
253	bus_unregister(&bcma_bus_type);
254}
255module_exit(bcma_modexit)
256