blob: 2648522432bec1cb9f2466b9c3bc6ca97c9c4553 [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"
9#include <linux/bcma/bcma.h>
Geert Uytterhoeveneef72692011-06-26 10:19:44 +020010#include <linux/slab.h>
Rafał Miłecki8369ae32011-05-09 18:56:46 +020011
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);
Hauke Mehrtensecd177c2011-07-23 01:20:08 +020069 if (core->io_addr)
70 iounmap(core->io_addr);
71 if (core->io_wrap)
72 iounmap(core->io_wrap);
Rafał Miłecki8369ae32011-05-09 18:56:46 +020073 kfree(core);
74}
75
76static int bcma_register_cores(struct bcma_bus *bus)
77{
78 struct bcma_device *core;
79 int err, dev_id = 0;
80
81 list_for_each_entry(core, &bus->cores, list) {
82 /* We support that cores ourself */
83 switch (core->id.id) {
84 case BCMA_CORE_CHIPCOMMON:
85 case BCMA_CORE_PCI:
86 case BCMA_CORE_PCIE:
87 continue;
88 }
89
90 core->dev.release = bcma_release_core_dev;
91 core->dev.bus = &bcma_bus_type;
92 dev_set_name(&core->dev, "bcma%d:%d", 0/*bus->num*/, dev_id);
93
94 switch (bus->hosttype) {
95 case BCMA_HOSTTYPE_PCI:
96 core->dev.parent = &bus->host_pci->dev;
Rafał Miłecki1bdcd092011-05-18 11:40:22 +020097 core->dma_dev = &bus->host_pci->dev;
98 core->irq = bus->host_pci->irq;
Rafał Miłecki8369ae32011-05-09 18:56:46 +020099 break;
Hauke Mehrtensecd177c2011-07-23 01:20:08 +0200100 case BCMA_HOSTTYPE_SOC:
101 core->dev.dma_mask = &core->dev.coherent_dma_mask;
102 core->dma_dev = &core->dev;
103 break;
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200104 case BCMA_HOSTTYPE_SDIO:
105 break;
106 }
107
108 err = device_register(&core->dev);
109 if (err) {
110 pr_err("Could not register dev for core 0x%03X\n",
111 core->id.id);
112 continue;
113 }
114 core->dev_registered = true;
115 dev_id++;
116 }
117
118 return 0;
119}
120
121static void bcma_unregister_cores(struct bcma_bus *bus)
122{
123 struct bcma_device *core;
124
125 list_for_each_entry(core, &bus->cores, list) {
126 if (core->dev_registered)
127 device_unregister(&core->dev);
128 }
129}
130
131int bcma_bus_register(struct bcma_bus *bus)
132{
133 int err;
134 struct bcma_device *core;
135
136 /* Scan for devices (cores) */
137 err = bcma_bus_scan(bus);
138 if (err) {
139 pr_err("Failed to scan: %d\n", err);
140 return -1;
141 }
142
143 /* Init CC core */
144 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
145 if (core) {
146 bus->drv_cc.core = core;
147 bcma_core_chipcommon_init(&bus->drv_cc);
148 }
149
150 /* Init PCIE core */
151 core = bcma_find_core(bus, BCMA_CORE_PCIE);
152 if (core) {
153 bus->drv_pci.core = core;
154 bcma_core_pci_init(&bus->drv_pci);
155 }
156
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200157 /* Try to get SPROM */
158 err = bcma_sprom_get(bus);
Hauke Mehrtens534e7a42011-07-09 13:22:03 +0200159 if (err == -ENOENT) {
160 pr_err("No SPROM available\n");
161 } else if (err) {
Rafał Miłecki27f18dc2011-06-02 02:08:51 +0200162 pr_err("Failed to get SPROM: %d\n", err);
163 return -ENOENT;
164 }
165
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200166 /* Register found cores */
167 bcma_register_cores(bus);
168
169 pr_info("Bus registered\n");
170
171 return 0;
172}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200173
174void bcma_bus_unregister(struct bcma_bus *bus)
175{
176 bcma_unregister_cores(bus);
177}
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200178
Hauke Mehrtens517f43e2011-07-23 01:20:07 +0200179int __init bcma_bus_early_register(struct bcma_bus *bus,
180 struct bcma_device *core_cc,
181 struct bcma_device *core_mips)
182{
183 int err;
184 struct bcma_device *core;
185 struct bcma_device_id match;
186
187 bcma_init_bus(bus);
188
189 match.manuf = BCMA_MANUF_BCM;
190 match.id = BCMA_CORE_CHIPCOMMON;
191 match.class = BCMA_CL_SIM;
192 match.rev = BCMA_ANY_REV;
193
194 /* Scan for chip common core */
195 err = bcma_bus_scan_early(bus, &match, core_cc);
196 if (err) {
197 pr_err("Failed to scan for common core: %d\n", err);
198 return -1;
199 }
200
201 match.manuf = BCMA_MANUF_MIPS;
202 match.id = BCMA_CORE_MIPS_74K;
203 match.class = BCMA_CL_SIM;
204 match.rev = BCMA_ANY_REV;
205
206 /* Scan for mips core */
207 err = bcma_bus_scan_early(bus, &match, core_mips);
208 if (err) {
209 pr_err("Failed to scan for mips core: %d\n", err);
210 return -1;
211 }
212
213 /* Init CC core */
214 core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
215 if (core) {
216 bus->drv_cc.core = core;
217 bcma_core_chipcommon_init(&bus->drv_cc);
218 }
219
220 pr_info("Early bus registered\n");
221
222 return 0;
223}
224
Rafał Miłecki8369ae32011-05-09 18:56:46 +0200225int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
226{
227 drv->drv.name = drv->name;
228 drv->drv.bus = &bcma_bus_type;
229 drv->drv.owner = owner;
230
231 return driver_register(&drv->drv);
232}
233EXPORT_SYMBOL_GPL(__bcma_driver_register);
234
235void bcma_driver_unregister(struct bcma_driver *drv)
236{
237 driver_unregister(&drv->drv);
238}
239EXPORT_SYMBOL_GPL(bcma_driver_unregister);
240
241static int bcma_bus_match(struct device *dev, struct device_driver *drv)
242{
243 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
244 struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
245 const struct bcma_device_id *cid = &core->id;
246 const struct bcma_device_id *did;
247
248 for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
249 if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
250 (did->id == cid->id || did->id == BCMA_ANY_ID) &&
251 (did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
252 (did->class == cid->class || did->class == BCMA_ANY_CLASS))
253 return 1;
254 }
255 return 0;
256}
257
258static int bcma_device_probe(struct device *dev)
259{
260 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
261 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
262 drv);
263 int err = 0;
264
265 if (adrv->probe)
266 err = adrv->probe(core);
267
268 return err;
269}
270
271static int bcma_device_remove(struct device *dev)
272{
273 struct bcma_device *core = container_of(dev, struct bcma_device, dev);
274 struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
275 drv);
276
277 if (adrv->remove)
278 adrv->remove(core);
279
280 return 0;
281}
282
283static int __init bcma_modinit(void)
284{
285 int err;
286
287 err = bus_register(&bcma_bus_type);
288 if (err)
289 return err;
290
291#ifdef CONFIG_BCMA_HOST_PCI
292 err = bcma_host_pci_init();
293 if (err) {
294 pr_err("PCI host initialization failed\n");
295 err = 0;
296 }
297#endif
298
299 return err;
300}
301fs_initcall(bcma_modinit);
302
303static void __exit bcma_modexit(void)
304{
305#ifdef CONFIG_BCMA_HOST_PCI
306 bcma_host_pci_exit();
307#endif
308 bus_unregister(&bcma_bus_type);
309}
310module_exit(bcma_modexit)