pandora: reserve CMA area for c64_tools
[pandora-kernel.git] / drivers / bcma / main.c
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/module.h>
10 #include <linux/bcma/bcma.h>
11 #include <linux/slab.h>
12
13 MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
14 MODULE_LICENSE("GPL");
15
16 static int bcma_bus_match(struct device *dev, struct device_driver *drv);
17 static int bcma_device_probe(struct device *dev);
18 static int bcma_device_remove(struct device *dev);
19 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
20
21 static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
22 {
23         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
24         return sprintf(buf, "0x%03X\n", core->id.manuf);
25 }
26 static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
27 {
28         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
29         return sprintf(buf, "0x%03X\n", core->id.id);
30 }
31 static ssize_t rev_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%02X\n", core->id.rev);
35 }
36 static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
37 {
38         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
39         return sprintf(buf, "0x%X\n", core->id.class);
40 }
41 static struct device_attribute bcma_device_attrs[] = {
42         __ATTR_RO(manuf),
43         __ATTR_RO(id),
44         __ATTR_RO(rev),
45         __ATTR_RO(class),
46         __ATTR_NULL,
47 };
48
49 static struct bus_type bcma_bus_type = {
50         .name           = "bcma",
51         .match          = bcma_bus_match,
52         .probe          = bcma_device_probe,
53         .remove         = bcma_device_remove,
54         .uevent         = bcma_device_uevent,
55         .dev_attrs      = bcma_device_attrs,
56 };
57
58 static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
59 {
60         struct bcma_device *core;
61
62         list_for_each_entry(core, &bus->cores, list) {
63                 if (core->id.id == coreid)
64                         return core;
65         }
66         return NULL;
67 }
68
69 static void bcma_release_core_dev(struct device *dev)
70 {
71         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
72         if (core->io_addr)
73                 iounmap(core->io_addr);
74         if (core->io_wrap)
75                 iounmap(core->io_wrap);
76         kfree(core);
77 }
78
79 static int bcma_register_cores(struct bcma_bus *bus)
80 {
81         struct bcma_device *core;
82         int err, dev_id = 0;
83
84         list_for_each_entry(core, &bus->cores, list) {
85                 /* We support that cores ourself */
86                 switch (core->id.id) {
87                 case BCMA_CORE_CHIPCOMMON:
88                 case BCMA_CORE_PCI:
89                 case BCMA_CORE_PCIE:
90                 case BCMA_CORE_MIPS_74K:
91                         continue;
92                 }
93
94                 core->dev.release = bcma_release_core_dev;
95                 core->dev.bus = &bcma_bus_type;
96                 dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id);
97
98                 switch (bus->hosttype) {
99                 case BCMA_HOSTTYPE_PCI:
100                         core->dev.parent = &bus->host_pci->dev;
101                         core->dma_dev = &bus->host_pci->dev;
102                         core->irq = bus->host_pci->irq;
103                         break;
104                 case BCMA_HOSTTYPE_SOC:
105                         core->dev.dma_mask = &core->dev.coherent_dma_mask;
106                         core->dma_dev = &core->dev;
107                         break;
108                 case BCMA_HOSTTYPE_SDIO:
109                         break;
110                 }
111
112                 err = device_register(&core->dev);
113                 if (err) {
114                         pr_err("Could not register dev for core 0x%03X\n",
115                                core->id.id);
116                         continue;
117                 }
118                 core->dev_registered = true;
119                 dev_id++;
120         }
121
122         return 0;
123 }
124
125 static void bcma_unregister_cores(struct bcma_bus *bus)
126 {
127         struct bcma_device *core, *tmp;
128
129         list_for_each_entry_safe(core, tmp, &bus->cores, list) {
130                 list_del(&core->list);
131                 if (core->dev_registered)
132                         device_unregister(&core->dev);
133         }
134 }
135
136 int bcma_bus_register(struct bcma_bus *bus)
137 {
138         int err;
139         struct bcma_device *core;
140
141         /* Scan for devices (cores) */
142         err = bcma_bus_scan(bus);
143         if (err) {
144                 pr_err("Failed to scan: %d\n", err);
145                 return -1;
146         }
147
148         /* Init CC core */
149         core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
150         if (core) {
151                 bus->drv_cc.core = core;
152                 bcma_core_chipcommon_init(&bus->drv_cc);
153         }
154
155         /* Init MIPS core */
156         core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
157         if (core) {
158                 bus->drv_mips.core = core;
159                 bcma_core_mips_init(&bus->drv_mips);
160         }
161
162         /* Init PCIE core */
163         core = bcma_find_core(bus, BCMA_CORE_PCIE);
164         if (core) {
165                 bus->drv_pci.core = core;
166                 bcma_core_pci_init(&bus->drv_pci);
167         }
168
169         /* Try to get SPROM */
170         err = bcma_sprom_get(bus);
171         if (err == -ENOENT) {
172                 pr_err("No SPROM available\n");
173         } else if (err) {
174                 pr_err("Failed to get SPROM: %d\n", err);
175                 return -ENOENT;
176         }
177
178         /* Register found cores */
179         bcma_register_cores(bus);
180
181         pr_info("Bus registered\n");
182
183         return 0;
184 }
185
186 void bcma_bus_unregister(struct bcma_bus *bus)
187 {
188         bcma_unregister_cores(bus);
189 }
190
191 int __init bcma_bus_early_register(struct bcma_bus *bus,
192                                    struct bcma_device *core_cc,
193                                    struct bcma_device *core_mips)
194 {
195         int err;
196         struct bcma_device *core;
197         struct bcma_device_id match;
198
199         bcma_init_bus(bus);
200
201         match.manuf = BCMA_MANUF_BCM;
202         match.id = BCMA_CORE_CHIPCOMMON;
203         match.class = BCMA_CL_SIM;
204         match.rev = BCMA_ANY_REV;
205
206         /* Scan for chip common core */
207         err = bcma_bus_scan_early(bus, &match, core_cc);
208         if (err) {
209                 pr_err("Failed to scan for common core: %d\n", err);
210                 return -1;
211         }
212
213         match.manuf = BCMA_MANUF_MIPS;
214         match.id = BCMA_CORE_MIPS_74K;
215         match.class = BCMA_CL_SIM;
216         match.rev = BCMA_ANY_REV;
217
218         /* Scan for mips core */
219         err = bcma_bus_scan_early(bus, &match, core_mips);
220         if (err) {
221                 pr_err("Failed to scan for mips core: %d\n", err);
222                 return -1;
223         }
224
225         /* Init CC core */
226         core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
227         if (core) {
228                 bus->drv_cc.core = core;
229                 bcma_core_chipcommon_init(&bus->drv_cc);
230         }
231
232         /* Init MIPS core */
233         core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
234         if (core) {
235                 bus->drv_mips.core = core;
236                 bcma_core_mips_init(&bus->drv_mips);
237         }
238
239         pr_info("Early bus registered\n");
240
241         return 0;
242 }
243
244 #ifdef CONFIG_PM
245 int bcma_bus_resume(struct bcma_bus *bus)
246 {
247         struct bcma_device *core;
248
249         /* Init CC core */
250         core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
251         if (core) {
252                 bus->drv_cc.setup_done = false;
253                 bcma_core_chipcommon_init(&bus->drv_cc);
254         }
255
256         return 0;
257 }
258 #endif
259
260 int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
261 {
262         drv->drv.name = drv->name;
263         drv->drv.bus = &bcma_bus_type;
264         drv->drv.owner = owner;
265
266         return driver_register(&drv->drv);
267 }
268 EXPORT_SYMBOL_GPL(__bcma_driver_register);
269
270 void bcma_driver_unregister(struct bcma_driver *drv)
271 {
272         driver_unregister(&drv->drv);
273 }
274 EXPORT_SYMBOL_GPL(bcma_driver_unregister);
275
276 static int bcma_bus_match(struct device *dev, struct device_driver *drv)
277 {
278         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
279         struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
280         const struct bcma_device_id *cid = &core->id;
281         const struct bcma_device_id *did;
282
283         for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
284             if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
285                 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
286                 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
287                 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
288                         return 1;
289         }
290         return 0;
291 }
292
293 static int bcma_device_probe(struct device *dev)
294 {
295         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
296         struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
297                                                drv);
298         int err = 0;
299
300         if (adrv->probe)
301                 err = adrv->probe(core);
302
303         return err;
304 }
305
306 static int bcma_device_remove(struct device *dev)
307 {
308         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
309         struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
310                                                drv);
311
312         if (adrv->remove)
313                 adrv->remove(core);
314
315         return 0;
316 }
317
318 static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
319 {
320         struct bcma_device *core = container_of(dev, struct bcma_device, dev);
321
322         return add_uevent_var(env,
323                               "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
324                               core->id.manuf, core->id.id,
325                               core->id.rev, core->id.class);
326 }
327
328 static int __init bcma_modinit(void)
329 {
330         int err;
331
332         err = bus_register(&bcma_bus_type);
333         if (err)
334                 return err;
335
336 #ifdef CONFIG_BCMA_HOST_PCI
337         err = bcma_host_pci_init();
338         if (err) {
339                 pr_err("PCI host initialization failed\n");
340                 err = 0;
341         }
342 #endif
343
344         return err;
345 }
346 fs_initcall(bcma_modinit);
347
348 static void __exit bcma_modexit(void)
349 {
350 #ifdef CONFIG_BCMA_HOST_PCI
351         bcma_host_pci_exit();
352 #endif
353         bus_unregister(&bcma_bus_type);
354 }
355 module_exit(bcma_modexit)