blob: bcd1c01cde9e0bde6012a786537a8949bf557878 [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łecki8369ae32011-05-09 18:56:46 +020010#include <linux/bcma/bcma.h>
Geert Uytterhoeveneef72692011-06-26 10:19:44 +020011#include <linux/slab.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020012
13MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
14MODULE_LICENSE("GPL");
15
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +010016/* contains the number the next bus should get. */
17static unsigned int bcma_bus_next_num = 0;
18
19/* bcma_buses_mutex locks the bcma_bus_next_num */
20static DEFINE_MUTEX(bcma_buses_mutex);
21
Rafał Miłecki8369ae32011-05-09 18:56:46 +020022static int bcma_bus_match(struct device *dev, struct device_driver *drv);
23static int bcma_device_probe(struct device *dev);
24static int bcma_device_remove(struct device *dev);
David Woodhouse886b66e2011-08-19 22:14:47 +020025static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020026
27static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
28{
29 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
30 return sprintf(buf, "0x%03X\n", core->id.manuf);
31}
32static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
33{
34 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
35 return sprintf(buf, "0x%03X\n", core->id.id);
36}
37static ssize_t rev_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%02X\n", core->id.rev);
41}
42static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
43{
44 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
45 return sprintf(buf, "0x%X\n", core->id.class);
46}
47static struct device_attribute bcma_device_attrs[] = {
48 __ATTR_RO(manuf),
49 __ATTR_RO(id),
50 __ATTR_RO(rev),
51 __ATTR_RO(class),
52 __ATTR_NULL,
53};
54
55static struct bus_type bcma_bus_type = {
56 .name = "bcma",
57 .match = bcma_bus_match,
58 .probe = bcma_device_probe,
59 .remove = bcma_device_remove,
David Woodhouse886b66e2011-08-19 22:14:47 +020060 .uevent = bcma_device_uevent,
Rafał Miłecki8369ae32011-05-09 18:56:46 +020061 .dev_attrs = bcma_device_attrs,
62};
63
64static struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
65{
66 struct bcma_device *core;
67
68 list_for_each_entry(core, &bus->cores, list) {
69 if (core->id.id == coreid)
70 return core;
71 }
72 return NULL;
73}
74
75static void bcma_release_core_dev(struct device *dev)
76{
77 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
Hauke Mehrtensecd177c2011-07-23 01:20:08 +020078 if (core->io_addr)
79 iounmap(core->io_addr);
80 if (core->io_wrap)
81 iounmap(core->io_wrap);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020082 kfree(core);
83}
84
85static int bcma_register_cores(struct bcma_bus *bus)
86{
87 struct bcma_device *core;
88 int err, dev_id = 0;
89
90 list_for_each_entry(core, &bus->cores, list) {
91 /* We support that cores ourself */
92 switch (core->id.id) {
93 case BCMA_CORE_CHIPCOMMON:
94 case BCMA_CORE_PCI:
95 case BCMA_CORE_PCIE:
Hauke Mehrtens21e05342011-07-23 01:20:09 +020096 case BCMA_CORE_MIPS_74K:
Rafał Miłecki8369ae32011-05-09 18:56:46 +020097 continue;
98 }
99
100 core->dev.release = bcma_release_core_dev;
101 core->dev.bus = &bcma_bus_type;
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +0100102 dev_set_name(&core->dev, "bcma%d:%d", bus->num, dev_id);
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200103
104 switch (bus->hosttype) {
105 case BCMA_HOSTTYPE_PCI:
106 core->dev.parent = &bus->host_pci->dev;
Rafał Miłecki1bdcd092011-05-18 11:40:22 +0200107 core->dma_dev = &bus->host_pci->dev;
108 core->irq = bus->host_pci->irq;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200109 break;
Hauke Mehrtensecd177c2011-07-23 01:20:08 +0200110 case BCMA_HOSTTYPE_SOC:
111 core->dev.dma_mask = &core->dev.coherent_dma_mask;
112 core->dma_dev = &core->dev;
113 break;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200114 case BCMA_HOSTTYPE_SDIO:
115 break;
116 }
117
118 err = device_register(&core->dev);
119 if (err) {
120 pr_err("Could not register dev for core 0x%03X\n",
121 core->id.id);
122 continue;
123 }
124 core->dev_registered = true;
125 dev_id++;
126 }
127
128 return 0;
129}
130
131static void bcma_unregister_cores(struct bcma_bus *bus)
132{
133 struct bcma_device *core;
134
135 list_for_each_entry(core, &bus->cores, list) {
136 if (core->dev_registered)
137 device_unregister(&core->dev);
138 }
139}
140
Hauke Mehrtensd1a7a8e2012-01-31 00:03:34 +0100141int __devinit bcma_bus_register(struct bcma_bus *bus)
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200142{
143 int err;
144 struct bcma_device *core;
145
Hauke Mehrtens8f9ada42012-01-31 00:03:36 +0100146 mutex_lock(&bcma_buses_mutex);
147 bus->num = bcma_bus_next_num++;
148 mutex_unlock(&bcma_buses_mutex);
149
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200150 /* Scan for devices (cores) */
151 err = bcma_bus_scan(bus);
152 if (err) {
153 pr_err("Failed to scan: %d\n", err);
154 return -1;
155 }
156
157 /* Init CC core */
158 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
159 if (core) {
160 bus->drv_cc.core = core;
161 bcma_core_chipcommon_init(&bus->drv_cc);
162 }
163
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200164 /* Init MIPS core */
165 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
166 if (core) {
167 bus->drv_mips.core = core;
168 bcma_core_mips_init(&bus->drv_mips);
169 }
170
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200171 /* Init PCIE core */
172 core = bcma_find_core(bus, BCMA_CORE_PCIE);
173 if (core) {
174 bus->drv_pci.core = core;
175 bcma_core_pci_init(&bus->drv_pci);
176 }
177
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200178 /* Try to get SPROM */
179 err = bcma_sprom_get(bus);
Hauke Mehrtens534e7a42011-07-09 13:22:03 +0200180 if (err == -ENOENT) {
181 pr_err("No SPROM available\n");
182 } else if (err) {
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200183 pr_err("Failed to get SPROM: %d\n", err);
184 return -ENOENT;
185 }
186
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200187 /* Register found cores */
188 bcma_register_cores(bus);
189
190 pr_info("Bus registered\n");
191
192 return 0;
193}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200194
195void bcma_bus_unregister(struct bcma_bus *bus)
196{
197 bcma_unregister_cores(bus);
198}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200199
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200200int __init bcma_bus_early_register(struct bcma_bus *bus,
201 struct bcma_device *core_cc,
202 struct bcma_device *core_mips)
203{
204 int err;
205 struct bcma_device *core;
206 struct bcma_device_id match;
207
208 bcma_init_bus(bus);
209
210 match.manuf = BCMA_MANUF_BCM;
211 match.id = BCMA_CORE_CHIPCOMMON;
212 match.class = BCMA_CL_SIM;
213 match.rev = BCMA_ANY_REV;
214
215 /* Scan for chip common core */
216 err = bcma_bus_scan_early(bus, &match, core_cc);
217 if (err) {
218 pr_err("Failed to scan for common core: %d\n", err);
219 return -1;
220 }
221
222 match.manuf = BCMA_MANUF_MIPS;
223 match.id = BCMA_CORE_MIPS_74K;
224 match.class = BCMA_CL_SIM;
225 match.rev = BCMA_ANY_REV;
226
227 /* Scan for mips core */
228 err = bcma_bus_scan_early(bus, &match, core_mips);
229 if (err) {
230 pr_err("Failed to scan for mips core: %d\n", err);
231 return -1;
232 }
233
234 /* Init CC core */
235 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
236 if (core) {
237 bus->drv_cc.core = core;
238 bcma_core_chipcommon_init(&bus->drv_cc);
239 }
240
Hauke Mehrtens21e05342011-07-23 01:20:09 +0200241 /* Init MIPS core */
242 core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
243 if (core) {
244 bus->drv_mips.core = core;
245 bcma_core_mips_init(&bus->drv_mips);
246 }
247
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200248 pr_info("Early bus registered\n");
249
250 return 0;
251}
252
Rafał Miłecki775ab522011-12-09 22:16:07 +0100253#ifdef CONFIG_PM
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100254int bcma_bus_suspend(struct bcma_bus *bus)
255{
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100256 struct bcma_device *core;
257
258 list_for_each_entry(core, &bus->cores, list) {
259 struct device_driver *drv = core->dev.driver;
260 if (drv) {
261 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
262 if (adrv->suspend)
263 adrv->suspend(core);
264 }
265 }
Linus Torvalds685a4ef2012-01-13 23:58:40 +0100266 return 0;
267}
268
Rafał Miłecki775ab522011-12-09 22:16:07 +0100269int bcma_bus_resume(struct bcma_bus *bus)
270{
271 struct bcma_device *core;
272
273 /* Init CC core */
274 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
275 if (core) {
276 bus->drv_cc.setup_done = false;
277 bcma_core_chipcommon_init(&bus->drv_cc);
278 }
279
Linus Torvalds7d5869e2012-01-13 23:58:41 +0100280 list_for_each_entry(core, &bus->cores, list) {
281 struct device_driver *drv = core->dev.driver;
282 if (drv) {
283 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
284 if (adrv->resume)
285 adrv->resume(core);
286 }
287 }
288
Rafał Miłecki775ab522011-12-09 22:16:07 +0100289 return 0;
290}
291#endif
292
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200293int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
294{
295 drv->drv.name = drv->name;
296 drv->drv.bus = &bcma_bus_type;
297 drv->drv.owner = owner;
298
299 return driver_register(&drv->drv);
300}
301EXPORT_SYMBOL_GPL(__bcma_driver_register);
302
303void bcma_driver_unregister(struct bcma_driver *drv)
304{
305 driver_unregister(&drv->drv);
306}
307EXPORT_SYMBOL_GPL(bcma_driver_unregister);
308
309static int bcma_bus_match(struct device *dev, struct device_driver *drv)
310{
311 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
312 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
313 const struct bcma_device_id *cid = &core->id;
314 const struct bcma_device_id *did;
315
316 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
317 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
318 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
319 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
320 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
321 return 1;
322 }
323 return 0;
324}
325
326static int bcma_device_probe(struct device *dev)
327{
328 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
329 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
330 drv);
331 int err = 0;
332
333 if (adrv->probe)
334 err = adrv->probe(core);
335
336 return err;
337}
338
339static int bcma_device_remove(struct device *dev)
340{
341 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
342 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
343 drv);
344
345 if (adrv->remove)
346 adrv->remove(core);
347
348 return 0;
349}
350
David Woodhouse886b66e2011-08-19 22:14:47 +0200351static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
352{
353 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
354
355 return add_uevent_var(env,
356 "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
357 core->id.manuf, core->id.id,
358 core->id.rev, core->id.class);
359}
360
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200361static int __init bcma_modinit(void)
362{
363 int err;
364
365 err = bus_register(&bcma_bus_type);
366 if (err)
367 return err;
368
369#ifdef CONFIG_BCMA_HOST_PCI
370 err = bcma_host_pci_init();
371 if (err) {
372 pr_err("PCI host initialization failed\n");
373 err = 0;
374 }
375#endif
376
377 return err;
378}
379fs_initcall(bcma_modinit);
380
381static void __exit bcma_modexit(void)
382{
383#ifdef CONFIG_BCMA_HOST_PCI
384 bcma_host_pci_exit();
385#endif
386 bus_unregister(&bcma_bus_type);
387}
388module_exit(bcma_modexit)