main.c 9.0 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Broadcom specific AMBA
 * Bus subsystem
 *
 * Licensed under the GNU/GPL. See COPYING for details.
 */

#include "bcma_private.h"
9
#include <linux/module.h>
10
#include <linux/bcma/bcma.h>
11
#include <linux/slab.h>
12 13 14 15

MODULE_DESCRIPTION("Broadcom's specific AMBA driver");
MODULE_LICENSE("GPL");

H
Hauke Mehrtens 已提交
16 17 18 19 20 21
/* contains the number the next bus should get. */
static unsigned int bcma_bus_next_num = 0;

/* bcma_buses_mutex locks the bcma_bus_next_num */
static DEFINE_MUTEX(bcma_buses_mutex);

22 23 24
static int bcma_bus_match(struct device *dev, struct device_driver *drv);
static int bcma_device_probe(struct device *dev);
static int bcma_device_remove(struct device *dev);
25
static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

static ssize_t manuf_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
	return sprintf(buf, "0x%03X\n", core->id.manuf);
}
static ssize_t id_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
	return sprintf(buf, "0x%03X\n", core->id.id);
}
static ssize_t rev_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
	return sprintf(buf, "0x%02X\n", core->id.rev);
}
static ssize_t class_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
	return sprintf(buf, "0x%X\n", core->id.class);
}
static struct device_attribute bcma_device_attrs[] = {
	__ATTR_RO(manuf),
	__ATTR_RO(id),
	__ATTR_RO(rev),
	__ATTR_RO(class),
	__ATTR_NULL,
};

static struct bus_type bcma_bus_type = {
	.name		= "bcma",
	.match		= bcma_bus_match,
	.probe		= bcma_device_probe,
	.remove		= bcma_device_remove,
60
	.uevent		= bcma_device_uevent,
61 62 63
	.dev_attrs	= bcma_device_attrs,
};

H
Hauke Mehrtens 已提交
64
struct bcma_device *bcma_find_core(struct bcma_bus *bus, u16 coreid)
65 66 67 68 69 70 71 72 73
{
	struct bcma_device *core;

	list_for_each_entry(core, &bus->cores, list) {
		if (core->id.id == coreid)
			return core;
	}
	return NULL;
}
H
Hauke Mehrtens 已提交
74
EXPORT_SYMBOL_GPL(bcma_find_core);
75 76 77 78

static void bcma_release_core_dev(struct device *dev)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
H
Hauke Mehrtens 已提交
79 80 81 82
	if (core->io_addr)
		iounmap(core->io_addr);
	if (core->io_wrap)
		iounmap(core->io_wrap);
83 84 85 86 87 88 89 90 91 92 93 94 95 96
	kfree(core);
}

static int bcma_register_cores(struct bcma_bus *bus)
{
	struct bcma_device *core;
	int err, dev_id = 0;

	list_for_each_entry(core, &bus->cores, list) {
		/* We support that cores ourself */
		switch (core->id.id) {
		case BCMA_CORE_CHIPCOMMON:
		case BCMA_CORE_PCI:
		case BCMA_CORE_PCIE:
H
Hauke Mehrtens 已提交
97
		case BCMA_CORE_MIPS_74K:
98 99 100 101 102
			continue;
		}

		core->dev.release = bcma_release_core_dev;
		core->dev.bus = &bcma_bus_type;
H
Hauke Mehrtens 已提交
103
		dev_set_name(&core->dev, "bcma%d:%d", bus->num, dev_id);
104 105 106 107

		switch (bus->hosttype) {
		case BCMA_HOSTTYPE_PCI:
			core->dev.parent = &bus->host_pci->dev;
108 109
			core->dma_dev = &bus->host_pci->dev;
			core->irq = bus->host_pci->irq;
110
			break;
H
Hauke Mehrtens 已提交
111 112 113 114
		case BCMA_HOSTTYPE_SOC:
			core->dev.dma_mask = &core->dev.coherent_dma_mask;
			core->dma_dev = &core->dev;
			break;
115 116 117 118 119 120
		case BCMA_HOSTTYPE_SDIO:
			break;
		}

		err = device_register(&core->dev);
		if (err) {
121 122 123
			bcma_err(bus,
				 "Could not register dev for core 0x%03X\n",
				 core->id.id);
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
			continue;
		}
		core->dev_registered = true;
		dev_id++;
	}

	return 0;
}

static void bcma_unregister_cores(struct bcma_bus *bus)
{
	struct bcma_device *core;

	list_for_each_entry(core, &bus->cores, list) {
		if (core->dev_registered)
			device_unregister(&core->dev);
	}
}

143
int __devinit bcma_bus_register(struct bcma_bus *bus)
144 145 146 147
{
	int err;
	struct bcma_device *core;

H
Hauke Mehrtens 已提交
148 149 150 151
	mutex_lock(&bcma_buses_mutex);
	bus->num = bcma_bus_next_num++;
	mutex_unlock(&bcma_buses_mutex);

152 153 154
	/* Scan for devices (cores) */
	err = bcma_bus_scan(bus);
	if (err) {
155
		bcma_err(bus, "Failed to scan: %d\n", err);
156 157 158 159 160 161 162 163 164 165
		return -1;
	}

	/* Init CC core */
	core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
	if (core) {
		bus->drv_cc.core = core;
		bcma_core_chipcommon_init(&bus->drv_cc);
	}

H
Hauke Mehrtens 已提交
166 167 168 169 170 171 172
	/* Init MIPS core */
	core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
	if (core) {
		bus->drv_mips.core = core;
		bcma_core_mips_init(&bus->drv_mips);
	}

173 174 175 176 177 178 179
	/* Init PCIE core */
	core = bcma_find_core(bus, BCMA_CORE_PCIE);
	if (core) {
		bus->drv_pci.core = core;
		bcma_core_pci_init(&bus->drv_pci);
	}

180 181
	/* Try to get SPROM */
	err = bcma_sprom_get(bus);
182
	if (err == -ENOENT) {
183
		bcma_err(bus, "No SPROM available\n");
184
	} else if (err)
185
		bcma_err(bus, "Failed to get SPROM: %d\n", err);
186

187 188 189
	/* Register found cores */
	bcma_register_cores(bus);

190
	bcma_info(bus, "Bus registered\n");
191 192 193 194 195 196 197 198 199

	return 0;
}

void bcma_bus_unregister(struct bcma_bus *bus)
{
	bcma_unregister_cores(bus);
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
int __init bcma_bus_early_register(struct bcma_bus *bus,
				   struct bcma_device *core_cc,
				   struct bcma_device *core_mips)
{
	int err;
	struct bcma_device *core;
	struct bcma_device_id match;

	bcma_init_bus(bus);

	match.manuf = BCMA_MANUF_BCM;
	match.id = BCMA_CORE_CHIPCOMMON;
	match.class = BCMA_CL_SIM;
	match.rev = BCMA_ANY_REV;

	/* Scan for chip common core */
	err = bcma_bus_scan_early(bus, &match, core_cc);
	if (err) {
218
		bcma_err(bus, "Failed to scan for common core: %d\n", err);
219 220 221 222 223 224 225 226 227 228 229
		return -1;
	}

	match.manuf = BCMA_MANUF_MIPS;
	match.id = BCMA_CORE_MIPS_74K;
	match.class = BCMA_CL_SIM;
	match.rev = BCMA_ANY_REV;

	/* Scan for mips core */
	err = bcma_bus_scan_early(bus, &match, core_mips);
	if (err) {
230
		bcma_err(bus, "Failed to scan for mips core: %d\n", err);
231 232 233 234 235 236 237 238 239 240
		return -1;
	}

	/* Init CC core */
	core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
	if (core) {
		bus->drv_cc.core = core;
		bcma_core_chipcommon_init(&bus->drv_cc);
	}

H
Hauke Mehrtens 已提交
241 242 243 244 245 246 247
	/* Init MIPS core */
	core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
	if (core) {
		bus->drv_mips.core = core;
		bcma_core_mips_init(&bus->drv_mips);
	}

248
	bcma_info(bus, "Early bus registered\n");
249 250 251 252

	return 0;
}

253
#ifdef CONFIG_PM
254 255
int bcma_bus_suspend(struct bcma_bus *bus)
{
256 257 258 259 260 261 262 263 264 265
	struct bcma_device *core;

	list_for_each_entry(core, &bus->cores, list) {
		struct device_driver *drv = core->dev.driver;
		if (drv) {
			struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
			if (adrv->suspend)
				adrv->suspend(core);
		}
	}
266 267 268
	return 0;
}

269 270 271 272 273 274 275 276 277 278 279
int bcma_bus_resume(struct bcma_bus *bus)
{
	struct bcma_device *core;

	/* Init CC core */
	core = bcma_find_core(bus, BCMA_CORE_CHIPCOMMON);
	if (core) {
		bus->drv_cc.setup_done = false;
		bcma_core_chipcommon_init(&bus->drv_cc);
	}

280 281 282 283 284 285 286 287 288
	list_for_each_entry(core, &bus->cores, list) {
		struct device_driver *drv = core->dev.driver;
		if (drv) {
			struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
			if (adrv->resume)
				adrv->resume(core);
		}
	}

289 290 291 292
	return 0;
}
#endif

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
int __bcma_driver_register(struct bcma_driver *drv, struct module *owner)
{
	drv->drv.name = drv->name;
	drv->drv.bus = &bcma_bus_type;
	drv->drv.owner = owner;

	return driver_register(&drv->drv);
}
EXPORT_SYMBOL_GPL(__bcma_driver_register);

void bcma_driver_unregister(struct bcma_driver *drv)
{
	driver_unregister(&drv->drv);
}
EXPORT_SYMBOL_GPL(bcma_driver_unregister);

static int bcma_bus_match(struct device *dev, struct device_driver *drv)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
	struct bcma_driver *adrv = container_of(drv, struct bcma_driver, drv);
	const struct bcma_device_id *cid = &core->id;
	const struct bcma_device_id *did;

	for (did = adrv->id_table; did->manuf || did->id || did->rev; did++) {
	    if ((did->manuf == cid->manuf || did->manuf == BCMA_ANY_MANUF) &&
		(did->id == cid->id || did->id == BCMA_ANY_ID) &&
		(did->rev == cid->rev || did->rev == BCMA_ANY_REV) &&
		(did->class == cid->class || did->class == BCMA_ANY_CLASS))
			return 1;
	}
	return 0;
}

static int bcma_device_probe(struct device *dev)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
	struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
					       drv);
	int err = 0;

	if (adrv->probe)
		err = adrv->probe(core);

	return err;
}

static int bcma_device_remove(struct device *dev)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
	struct bcma_driver *adrv = container_of(dev->driver, struct bcma_driver,
					       drv);

	if (adrv->remove)
		adrv->remove(core);

	return 0;
}

351 352 353 354 355 356 357 358 359 360
static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);

	return add_uevent_var(env,
			      "MODALIAS=bcma:m%04Xid%04Xrev%02Xcl%02X",
			      core->id.manuf, core->id.id,
			      core->id.rev, core->id.class);
}

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
static int __init bcma_modinit(void)
{
	int err;

	err = bus_register(&bcma_bus_type);
	if (err)
		return err;

#ifdef CONFIG_BCMA_HOST_PCI
	err = bcma_host_pci_init();
	if (err) {
		pr_err("PCI host initialization failed\n");
		err = 0;
	}
#endif

	return err;
}
fs_initcall(bcma_modinit);

static void __exit bcma_modexit(void)
{
#ifdef CONFIG_BCMA_HOST_PCI
	bcma_host_pci_exit();
#endif
	bus_unregister(&bcma_bus_type);
}
module_exit(bcma_modexit)