main.c 14.1 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/platform_device.h>
11
#include <linux/bcma/bcma.h>
12
#include <linux/slab.h>
13
#include <linux/of_address.h>
14 15 16 17

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

H
Hauke Mehrtens 已提交
18 19 20 21 22 23
/* 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);

24 25 26
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);
27
static int bcma_device_uevent(struct device *dev, struct kobj_uevent_env *env);
28 29 30 31 32 33

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);
}
34 35
static DEVICE_ATTR_RO(manuf);

36 37 38 39 40
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);
}
41 42
static DEVICE_ATTR_RO(id);

43 44 45 46 47
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);
}
48 49
static DEVICE_ATTR_RO(rev);

50 51 52 53 54
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);
}
55 56 57 58 59 60 61 62
static DEVICE_ATTR_RO(class);

static struct attribute *bcma_device_attrs[] = {
	&dev_attr_manuf.attr,
	&dev_attr_id.attr,
	&dev_attr_rev.attr,
	&dev_attr_class.attr,
	NULL,
63
};
64
ATTRIBUTE_GROUPS(bcma_device);
65 66 67 68 69 70

static struct bus_type bcma_bus_type = {
	.name		= "bcma",
	.match		= bcma_bus_match,
	.probe		= bcma_device_probe,
	.remove		= bcma_device_remove,
71
	.uevent		= bcma_device_uevent,
72
	.dev_groups	= bcma_device_groups,
73 74
};

75 76 77 78 79 80 81
static u16 bcma_cc_core_id(struct bcma_bus *bus)
{
	if (bus->chipinfo.id == BCMA_CHIP_ID_BCM4706)
		return BCMA_CORE_4706_CHIPCOMMON;
	return BCMA_CORE_CHIPCOMMON;
}

82 83
struct bcma_device *bcma_find_core_unit(struct bcma_bus *bus, u16 coreid,
					u8 unit)
84 85 86 87 88 89 90 91 92
{
	struct bcma_device *core;

	list_for_each_entry(core, &bus->cores, list) {
		if (core->id.id == coreid && core->core_unit == unit)
			return core;
	}
	return NULL;
}
93
EXPORT_SYMBOL_GPL(bcma_find_core_unit);
94

R
Rafał Miłecki 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
bool bcma_wait_value(struct bcma_device *core, u16 reg, u32 mask, u32 value,
		     int timeout)
{
	unsigned long deadline = jiffies + timeout;
	u32 val;

	do {
		val = bcma_read32(core, reg);
		if ((val & mask) == value)
			return true;
		cpu_relax();
		udelay(10);
	} while (!time_after_eq(jiffies, deadline));

	bcma_warn(core->bus, "Timeout waiting for register 0x%04X!\n", reg);

	return false;
}

114 115 116
static void bcma_release_core_dev(struct device *dev)
{
	struct bcma_device *core = container_of(dev, struct bcma_device, dev);
H
Hauke Mehrtens 已提交
117 118 119 120
	if (core->io_addr)
		iounmap(core->io_addr);
	if (core->io_wrap)
		iounmap(core->io_wrap);
121 122 123
	kfree(core);
}

124 125 126 127 128 129 130 131 132 133 134
static bool bcma_is_core_needed_early(u16 core_id)
{
	switch (core_id) {
	case BCMA_CORE_NS_NAND:
	case BCMA_CORE_NS_QSPI:
		return true;
	}

	return false;
}

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
#ifdef CONFIG_OF
static struct device_node *bcma_of_find_child_device(struct platform_device *parent,
						     struct bcma_device *core)
{
	struct device_node *node;
	u64 size;
	const __be32 *reg;

	if (!parent || !parent->dev.of_node)
		return NULL;

	for_each_child_of_node(parent->dev.of_node, node) {
		reg = of_get_address(node, 0, &size, NULL);
		if (!reg)
			continue;
		if (of_translate_address(node, reg) == core->addr)
			return node;
	}
	return NULL;
}

static void bcma_of_fill_device(struct platform_device *parent,
				struct bcma_device *core)
{
	struct device_node *node;

	node = bcma_of_find_child_device(parent, core);
	if (node)
		core->dev.of_node = node;
}
#else
static void bcma_of_fill_device(struct platform_device *parent,
				struct bcma_device *core)
{
}
#endif /* CONFIG_OF */

172
void bcma_prepare_core(struct bcma_bus *bus, struct bcma_device *core)
173 174 175 176 177 178 179 180 181 182 183 184 185
{
	core->dev.release = bcma_release_core_dev;
	core->dev.bus = &bcma_bus_type;
	dev_set_name(&core->dev, "bcma%d:%d", bus->num, core->core_index);

	switch (bus->hosttype) {
	case BCMA_HOSTTYPE_PCI:
		core->dev.parent = &bus->host_pci->dev;
		core->dma_dev = &bus->host_pci->dev;
		core->irq = bus->host_pci->irq;
		break;
	case BCMA_HOSTTYPE_SOC:
		core->dev.dma_mask = &core->dev.coherent_dma_mask;
186 187 188 189 190 191 192
		if (bus->host_pdev) {
			core->dma_dev = &bus->host_pdev->dev;
			core->dev.parent = &bus->host_pdev->dev;
			bcma_of_fill_device(bus->host_pdev, core);
		} else {
			core->dma_dev = &core->dev;
		}
193 194 195 196
		break;
	case BCMA_HOSTTYPE_SDIO:
		break;
	}
197 198 199 200 201
}

static void bcma_register_core(struct bcma_bus *bus, struct bcma_device *core)
{
	int err;
202 203 204 205 206 207 208 209 210 211 212 213

	err = device_register(&core->dev);
	if (err) {
		bcma_err(bus, "Could not register dev for core 0x%03X\n",
			 core->id.id);
		put_device(&core->dev);
		return;
	}
	core->dev_registered = true;
}

static int bcma_register_devices(struct bcma_bus *bus)
214 215
{
	struct bcma_device *core;
216
	int err;
217 218 219 220

	list_for_each_entry(core, &bus->cores, list) {
		/* We support that cores ourself */
		switch (core->id.id) {
221
		case BCMA_CORE_4706_CHIPCOMMON:
222
		case BCMA_CORE_CHIPCOMMON:
223
		case BCMA_CORE_NS_CHIPCOMMON_B:
224 225
		case BCMA_CORE_PCI:
		case BCMA_CORE_PCIE:
226
		case BCMA_CORE_PCIE2:
H
Hauke Mehrtens 已提交
227
		case BCMA_CORE_MIPS_74K:
228
		case BCMA_CORE_4706_MAC_GBIT_COMMON:
229 230 231
			continue;
		}

232 233 234 235
		/* Early cores were already registered */
		if (bcma_is_core_needed_early(core->id.id))
			continue;

236 237 238 239 240
		/* Only first GMAC core on BCM4706 is connected and working */
		if (core->id.id == BCMA_CORE_4706_MAC_GBIT &&
		    core->core_unit > 0)
			continue;

241
		bcma_register_core(bus, core);
242 243
	}

244 245 246 247 248 249 250 251
#ifdef CONFIG_BCMA_DRIVER_MIPS
	if (bus->drv_cc.pflash.present) {
		err = platform_device_register(&bcma_pflash_dev);
		if (err)
			bcma_err(bus, "Error registering parallel flash\n");
	}
#endif

252 253 254 255 256 257 258 259
#ifdef CONFIG_BCMA_SFLASH
	if (bus->drv_cc.sflash.present) {
		err = platform_device_register(&bcma_sflash_dev);
		if (err)
			bcma_err(bus, "Error registering serial flash\n");
	}
#endif

260 261 262 263 264 265 266
#ifdef CONFIG_BCMA_NFLASH
	if (bus->drv_cc.nflash.present) {
		err = platform_device_register(&bcma_nflash_dev);
		if (err)
			bcma_err(bus, "Error registering NAND flash\n");
	}
#endif
H
Hauke Mehrtens 已提交
267 268 269 270 271
	err = bcma_gpio_init(&bus->drv_cc);
	if (err == -ENOTSUPP)
		bcma_debug(bus, "GPIO driver not activated\n");
	else if (err)
		bcma_err(bus, "Error registering GPIO driver: %i\n", err);
272

H
Hauke Mehrtens 已提交
273 274 275 276 277 278
	if (bus->hosttype == BCMA_HOSTTYPE_SOC) {
		err = bcma_chipco_watchdog_register(&bus->drv_cc);
		if (err)
			bcma_err(bus, "Error registering watchdog driver\n");
	}

279 280 281 282 283
	return 0;
}

static void bcma_unregister_cores(struct bcma_bus *bus)
{
P
Piotr Haber 已提交
284
	struct bcma_device *core, *tmp;
285

P
Piotr Haber 已提交
286 287
	list_for_each_entry_safe(core, tmp, &bus->cores, list) {
		list_del(&core->list);
288 289 290
		if (core->dev_registered)
			device_unregister(&core->dev);
	}
H
Hauke Mehrtens 已提交
291 292
	if (bus->hosttype == BCMA_HOSTTYPE_SOC)
		platform_device_unregister(bus->drv_cc.watchdog);
293 294
}

295
int bcma_bus_register(struct bcma_bus *bus)
296 297 298 299
{
	int err;
	struct bcma_device *core;

H
Hauke Mehrtens 已提交
300 301 302 303
	mutex_lock(&bcma_buses_mutex);
	bus->num = bcma_bus_next_num++;
	mutex_unlock(&bcma_buses_mutex);

304 305 306
	/* Scan for devices (cores) */
	err = bcma_bus_scan(bus);
	if (err) {
307
		bcma_err(bus, "Failed to scan: %d\n", err);
308
		return err;
309 310
	}

H
Hauke Mehrtens 已提交
311 312 313 314 315 316 317
	/* Early init CC core */
	core = bcma_find_core(bus, bcma_cc_core_id(bus));
	if (core) {
		bus->drv_cc.core = core;
		bcma_core_chipcommon_early_init(&bus->drv_cc);
	}

318 319 320 321 322 323
	/* Cores providing flash access go before SPROM init */
	list_for_each_entry(core, &bus->cores, list) {
		if (bcma_is_core_needed_early(core->id.id))
			bcma_register_core(bus, core);
	}

H
Hauke Mehrtens 已提交
324 325 326 327 328 329 330
	/* Try to get SPROM */
	err = bcma_sprom_get(bus);
	if (err == -ENOENT) {
		bcma_err(bus, "No SPROM available\n");
	} else if (err)
		bcma_err(bus, "Failed to get SPROM: %d\n", err);

331
	/* Init CC core */
332
	core = bcma_find_core(bus, bcma_cc_core_id(bus));
333 334 335 336 337
	if (core) {
		bus->drv_cc.core = core;
		bcma_core_chipcommon_init(&bus->drv_cc);
	}

338 339 340 341 342 343 344
	/* Init CC core */
	core = bcma_find_core(bus, BCMA_CORE_NS_CHIPCOMMON_B);
	if (core) {
		bus->drv_cc_b.core = core;
		bcma_core_chipcommon_b_init(&bus->drv_cc_b);
	}

H
Hauke Mehrtens 已提交
345 346 347 348 349 350 351
	/* 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);
	}

352
	/* Init PCIE core */
353 354 355 356 357 358 359 360
	core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 0);
	if (core) {
		bus->drv_pci[0].core = core;
		bcma_core_pci_init(&bus->drv_pci[0]);
	}

	/* Init PCIE core */
	core = bcma_find_core_unit(bus, BCMA_CORE_PCIE, 1);
361
	if (core) {
362 363
		bus->drv_pci[1].core = core;
		bcma_core_pci_init(&bus->drv_pci[1]);
364 365
	}

366 367 368 369 370 371 372
	/* Init PCIe Gen 2 core */
	core = bcma_find_core_unit(bus, BCMA_CORE_PCIE2, 0);
	if (core) {
		bus->drv_pcie2.core = core;
		bcma_core_pcie2_init(&bus->drv_pcie2);
	}

373 374 375 376 377 378 379
	/* Init GBIT MAC COMMON core */
	core = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);
	if (core) {
		bus->drv_gmac_cmn.core = core;
		bcma_core_gmac_cmn_init(&bus->drv_gmac_cmn);
	}

380
	/* Register found cores */
381
	bcma_register_devices(bus);
382

383
	bcma_info(bus, "Bus registered\n");
384 385 386 387 388 389

	return 0;
}

void bcma_bus_unregister(struct bcma_bus *bus)
{
390
	struct bcma_device *cores[3];
391 392 393 394 395 396 397
	int err;

	err = bcma_gpio_unregister(&bus->drv_cc);
	if (err == -EBUSY)
		bcma_err(bus, "Some GPIOs are still in use.\n");
	else if (err)
		bcma_err(bus, "Can not unregister GPIO driver: %i\n", err);
398

399 400
	bcma_core_chipcommon_b_free(&bus->drv_cc_b);

401 402 403 404
	cores[0] = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
	cores[1] = bcma_find_core(bus, BCMA_CORE_PCIE);
	cores[2] = bcma_find_core(bus, BCMA_CORE_4706_MAC_GBIT_COMMON);

405
	bcma_unregister_cores(bus);
406 407 408 409

	kfree(cores[2]);
	kfree(cores[1]);
	kfree(cores[0]);
410 411
}

412 413 414 415 416 417 418 419 420
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;

	match.manuf = BCMA_MANUF_BCM;
421
	match.id = bcma_cc_core_id(bus);
422 423 424 425 426 427
	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) {
428
		bcma_err(bus, "Failed to scan for common core: %d\n", err);
429 430 431 432 433 434 435 436 437 438 439
		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) {
440
		bcma_err(bus, "Failed to scan for mips core: %d\n", err);
441 442 443
		return -1;
	}

444
	/* Early init CC core */
445
	core = bcma_find_core(bus, bcma_cc_core_id(bus));
446 447
	if (core) {
		bus->drv_cc.core = core;
448
		bcma_core_chipcommon_early_init(&bus->drv_cc);
449 450
	}

451
	/* Early init MIPS core */
H
Hauke Mehrtens 已提交
452 453 454
	core = bcma_find_core(bus, BCMA_CORE_MIPS_74K);
	if (core) {
		bus->drv_mips.core = core;
455
		bcma_core_mips_early_init(&bus->drv_mips);
H
Hauke Mehrtens 已提交
456 457
	}

458
	bcma_info(bus, "Early bus registered\n");
459 460 461 462

	return 0;
}

463
#ifdef CONFIG_PM
464 465
int bcma_bus_suspend(struct bcma_bus *bus)
{
466 467 468 469 470 471 472 473 474 475
	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);
		}
	}
476 477 478
	return 0;
}

479 480 481 482 483
int bcma_bus_resume(struct bcma_bus *bus)
{
	struct bcma_device *core;

	/* Init CC core */
484
	if (bus->drv_cc.core) {
485 486 487 488
		bus->drv_cc.setup_done = false;
		bcma_core_chipcommon_init(&bus->drv_cc);
	}

489 490 491 492 493 494 495 496 497
	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);
		}
	}

498 499 500 501
	return 0;
}
#endif

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
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;
}

560 561 562 563 564 565 566 567 568 569
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);
}

570 571 572 573 574 575 576 577
static int __init bcma_modinit(void)
{
	int err;

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

578 579 580 581 582
	err = bcma_host_soc_register_driver();
	if (err) {
		pr_err("SoC host initialization failed\n");
		err = 0;
	}
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
#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
600
	bcma_host_soc_unregister_driver();
601 602 603
	bus_unregister(&bcma_bus_type);
}
module_exit(bcma_modexit)